Remove earcon and gnus-audio.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; For Emacs < 22.2.
29 (eval-and-compile
30   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31 (eval-when-compile
32   (require 'cl))
33 (eval-when-compile
34   (when (featurep 'xemacs)
35     (require 'easy-mmode))) ; for `define-minor-mode'
36
37 (defvar tool-bar-mode)
38 (defvar gnus-tmp-header)
39
40 (require 'gnus)
41 (require 'gnus-group)
42 (require 'gnus-spec)
43 (require 'gnus-range)
44 (require 'gnus-int)
45 (require 'gnus-undo)
46 (require 'gnus-util)
47 (require 'gmm-utils)
48 (require 'mm-decode)
49 (require 'nnoo)
50
51 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
52 (autoload 'gnus-cache-write-active "gnus-cache")
53 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
54 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
55 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
56 (autoload 'mm-uu-dissect "mm-uu")
57 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
58   "Deuglify broken Outlook (Express) articles and redisplay."
59   t)
60 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
61 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
62 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
63
64 (defcustom gnus-kill-summary-on-exit t
65   "*If non-nil, kill the summary buffer when you exit from it.
66 If nil, the summary will become a \"*Dead Summary*\" buffer, and
67 it will be killed sometime later."
68   :group 'gnus-summary-exit
69   :type 'boolean)
70
71 (defcustom gnus-summary-next-group-on-exit t
72   "If non-nil, go to the next unread newsgroup on summary exit.
73 See `gnus-group-goto-unread'."
74   :link '(custom-manual "(gnus)Group Maneuvering")
75   :group 'gnus-summary-exit
76   :version "23.1" ;; No Gnus
77   :type 'boolean)
78
79 (defcustom gnus-summary-stop-at-end-of-message nil
80   "If non-nil, don't select the next message when using `SPC'."
81   :link '(custom-manual "(gnus)Group Maneuvering")
82   :group 'gnus-summary-maneuvering
83   :version "24.1"
84   :type 'boolean)
85
86 (defcustom gnus-fetch-old-headers nil
87   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
88 If an unread article in the group refers to an older, already
89 read (or just marked as read) article, the old article will not
90 normally be displayed in the Summary buffer.  If this variable is
91 t, Gnus will attempt to grab the headers to the old articles, and
92 thereby build complete threads.  If it has the value `some', all
93 old headers will be fetched but only enough headers to connect
94 otherwise loose threads will be displayed.  This variable can
95 also be a number.  In that case, no more than that number of old
96 headers will be fetched.  If it has the value `invisible', all
97 old headers will be fetched, but none will be displayed.
98
99 The server has to support NOV for any of this to work.
100
101 This feature can seriously impact performance it ignores all
102 locally cached header entries.  Setting it to t for groups for a
103 server that doesn't expire articles (such as news.gmane.org),
104 leads to very slow summary generation."
105   :group 'gnus-thread
106   :type '(choice (const :tag "off" nil)
107                  (const :tag "on" t)
108                  (const some)
109                  (const invisible)
110                  number
111                  (sexp :menu-tag "other" t)))
112
113 (defcustom gnus-refer-thread-limit 500
114   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
115 If t, fetch all the available old headers."
116   :group 'gnus-thread
117   :type '(choice number
118                  (sexp :menu-tag "other" t)))
119
120 (defcustom gnus-summary-make-false-root 'adopt
121   "*nil means that Gnus won't gather loose threads.
122 If the root of a thread has expired or been read in a previous
123 session, the information necessary to build a complete thread has been
124 lost.  Instead of having many small sub-threads from this original thread
125 scattered all over the summary buffer, Gnus can gather them.
126
127 If non-nil, Gnus will try to gather all loose sub-threads from an
128 original thread into one large thread.
129
130 If this variable is non-nil, it should be one of `none', `adopt',
131 `dummy' or `empty'.
132
133 If this variable is `none', Gnus will not make a false root, but just
134 present the sub-threads after another.
135 If this variable is `dummy', Gnus will create a dummy root that will
136 have all the sub-threads as children.
137 If this variable is `adopt', Gnus will make one of the \"children\"
138 the parent and mark all the step-children as such.
139 If this variable is `empty', the \"children\" are printed with empty
140 subject fields.  (Or rather, they will be printed with a string
141 given by the `gnus-summary-same-subject' variable.)"
142   :group 'gnus-thread
143   :type '(choice (const :tag "off" nil)
144                  (const none)
145                  (const dummy)
146                  (const adopt)
147                  (const empty)))
148
149 (defcustom gnus-summary-make-false-root-always nil
150   "Always make a false dummy root."
151   :version "22.1"
152   :group 'gnus-thread
153   :type 'boolean)
154
155 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
156   "*A regexp to match subjects to be excluded from loose thread gathering.
157 As loose thread gathering is done on subjects only, that means that
158 there can be many false gatherings performed.  By rooting out certain
159 common subjects, gathering might become saner."
160   :group 'gnus-thread
161   :type 'regexp)
162
163 (defcustom gnus-summary-gather-subject-limit nil
164   "*Maximum length of subject comparisons when gathering loose threads.
165 Use nil to compare full subjects.  Setting this variable to a low
166 number will help gather threads that have been corrupted by
167 newsreaders chopping off subject lines, but it might also mean that
168 unrelated articles that have subject that happen to begin with the
169 same few characters will be incorrectly gathered.
170
171 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
172 comparing subjects."
173   :group 'gnus-thread
174   :type '(choice (const :tag "off" nil)
175                  (const fuzzy)
176                  (sexp :menu-tag "on" t)))
177
178 (defcustom gnus-simplify-subject-functions nil
179   "List of functions taking a string argument that simplify subjects.
180 The functions are applied recursively.
181
182 Useful functions to put in this list include:
183 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
184 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
185   :group 'gnus-thread
186   :type '(repeat function))
187
188 (defcustom gnus-simplify-ignored-prefixes nil
189   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
190   :group 'gnus-thread
191   :type '(choice (const :tag "off" nil)
192                  regexp))
193
194 (defcustom gnus-build-sparse-threads nil
195   "*If non-nil, fill in the gaps in threads.
196 If `some', only fill in the gaps that are needed to tie loose threads
197 together.  If `more', fill in all leaf nodes that Gnus can find.  If
198 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
199   :group 'gnus-thread
200   :type '(choice (const :tag "off" nil)
201                  (const some)
202                  (const more)
203                  (sexp :menu-tag "all" t)))
204
205 (defcustom gnus-summary-thread-gathering-function
206   'gnus-gather-threads-by-subject
207   "*Function used for gathering loose threads.
208 There are two pre-defined functions: `gnus-gather-threads-by-subject',
209 which only takes Subjects into consideration; and
210 `gnus-gather-threads-by-references', which compared the References
211 headers of the articles to find matches."
212   :group 'gnus-thread
213   :type '(radio (function-item gnus-gather-threads-by-subject)
214                 (function-item gnus-gather-threads-by-references)
215                 (function :tag "other")))
216
217 (defcustom gnus-summary-same-subject ""
218   "*String indicating that the current article has the same subject as the previous.
219 This variable will only be used if the value of
220 `gnus-summary-make-false-root' is `empty'."
221   :group 'gnus-summary-format
222   :type 'string)
223
224 (defcustom gnus-summary-goto-unread nil
225   "*If t, many commands will go to the next unread article.
226 This applies to marking commands as well as other commands that
227 \"naturally\" select the next article, like, for instance, `SPC' at
228 the end of an article.
229
230 If nil, the marking commands do NOT go to the next unread article
231 \(they go to the next article instead).  If `never', commands that
232 usually go to the next unread article, will go to the next article,
233 whether it is read or not."
234   :version "24.1"
235   :group 'gnus-summary-marks
236   :link '(custom-manual "(gnus)Setting Marks")
237   :type '(choice (const :tag "off" nil)
238                  (const never)
239                  (sexp :menu-tag "on" t)))
240
241 (defcustom gnus-summary-default-score 0
242   "*Default article score level.
243 All scores generated by the score files will be added to this score.
244 If this variable is nil, scoring will be disabled."
245   :group 'gnus-score-default
246   :type '(choice (const :tag "disable")
247                  integer))
248
249 (defcustom gnus-summary-default-high-score 0
250   "*Default threshold for a high scored article.
251 An article will be highlighted as high scored if its score is greater
252 than this score."
253   :version "22.1"
254   :group 'gnus-score-default
255   :type 'integer)
256
257 (defcustom gnus-summary-default-low-score 0
258   "*Default threshold for a low scored article.
259 An article will be highlighted as low scored if its score is smaller
260 than this score."
261   :version "22.1"
262   :group 'gnus-score-default
263   :type 'integer)
264
265 (defcustom gnus-summary-zcore-fuzz 0
266   "*Fuzziness factor for the zcore in the summary buffer.
267 Articles with scores closer than this to `gnus-summary-default-score'
268 will not be marked."
269   :group 'gnus-summary-format
270   :type 'integer)
271
272 (defcustom gnus-simplify-subject-fuzzy-regexp nil
273   "*Strings to be removed when doing fuzzy matches.
274 This can either be a regular expression or list of regular expressions
275 that will be removed from subject strings if fuzzy subject
276 simplification is selected."
277   :group 'gnus-thread
278   :type '(repeat regexp))
279
280 (defcustom gnus-show-threads t
281   "*If non-nil, display threads in summary mode."
282   :group 'gnus-thread
283   :type 'boolean)
284
285 (defcustom gnus-thread-hide-subtree nil
286   "*If non-nil, hide all threads initially.
287 This can be a predicate specifier which says which threads to hide.
288 If threads are hidden, you have to run the command
289 `gnus-summary-show-thread' by hand or select an article."
290   :group 'gnus-thread
291   :type '(radio (sexp :format "Non-nil\n"
292                       :match (lambda (widget value)
293                                (not (or (consp value) (functionp value))))
294                       :value t)
295                 (const nil)
296                 (sexp :tag "Predicate specifier")))
297
298 (defcustom gnus-thread-hide-killed t
299   "*If non-nil, hide killed threads automatically."
300   :group 'gnus-thread
301   :type 'boolean)
302
303 (defcustom gnus-thread-ignore-subject t
304   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
305 If nil, articles that have different subjects from their parents will
306 start separate threads."
307   :group 'gnus-thread
308   :type 'boolean)
309
310 (defcustom gnus-thread-operation-ignore-subject t
311   "*If non-nil, subjects will be ignored when doing thread commands.
312 This affects commands like `gnus-summary-kill-thread' and
313 `gnus-summary-lower-thread'.
314
315 If this variable is nil, articles in the same thread with different
316 subjects will not be included in the operation in question.  If this
317 variable is `fuzzy', only articles that have subjects that are fuzzily
318 equal will be included."
319   :group 'gnus-thread
320   :type '(choice (const :tag "off" nil)
321                  (const fuzzy)
322                  (sexp :tag "on" t)))
323
324 (defcustom gnus-thread-indent-level 4
325   "*Number that says how much each sub-thread should be indented."
326   :group 'gnus-thread
327   :type 'integer)
328
329 (defcustom gnus-auto-extend-newsgroup t
330   "*If non-nil, extend newsgroup forward and backward when requested."
331   :group 'gnus-summary-choose
332   :type 'boolean)
333
334 (defcustom gnus-auto-select-first t
335   "If non-nil, select an article on group entry.
336 An article is selected automatically when entering a group
337 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
338 `gnus-summary-catchup-and-goto-next-group'.
339
340 Which article is selected is controlled by the variable
341 `gnus-auto-select-subject'.
342
343 If you want to prevent automatic selection of articles in some
344 newsgroups, set the variable to nil in `gnus-select-group-hook'."
345   ;; Commands include...
346   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
347   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
348   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
349   :group 'gnus-group-select
350   :type '(choice (const :tag "none" nil)
351                  (sexp :menu-tag "first" t)))
352
353 (defcustom gnus-auto-select-subject 'unseen-or-unread
354   "*Says what subject to place under point when entering a group.
355
356 This variable can either be the symbols `first' (place point on the
357 first subject), `unread' (place point on the subject line of the first
358 unread article), `best' (place point on the subject line of the
359 higest-scored article), `unseen' (place point on the subject line of
360 the first unseen article), `unseen-or-unread' (place point on the subject
361 line of the first unseen article or, if all article have been seen, on the
362 subject line of the first unread article), or a function to be called to
363 place point on some subject line."
364   :version "24.1"
365   :group 'gnus-group-select
366   :type '(choice (const best)
367                  (const unread)
368                  (const first)
369                  (const unseen)
370                  (const unseen-or-unread)))
371
372 (defcustom gnus-auto-select-next t
373   "*If non-nil, offer to go to the next group from the end of the previous.
374 If the value is t and the next newsgroup is empty, Gnus will exit
375 summary mode and go back to group mode.  If the value is neither nil
376 nor t, Gnus will select the following unread newsgroup.  In
377 particular, if the value is the symbol `quietly', the next unread
378 newsgroup will be selected without any confirmation, and if it is
379 `almost-quietly', the next group will be selected without any
380 confirmation if you are located on the last article in the group.
381 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
382 will go to the next group without confirmation."
383   :group 'gnus-summary-maneuvering
384   :type '(choice (const :tag "off" nil)
385                  (const quietly)
386                  (const almost-quietly)
387                  (const slightly-quietly)
388                  (sexp :menu-tag "on" t)))
389
390 (defcustom gnus-auto-select-same nil
391   "*If non-nil, select the next article with the same subject.
392 If there are no more articles with the same subject, go to
393 the first unread article."
394   :group 'gnus-summary-maneuvering
395   :type 'boolean)
396
397 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
398   "What article should be selected after exiting an ephemeral group.
399 Valid values include:
400
401 `next'
402   Select the next article.
403 `next-unread'
404   Select the next unread article.
405 `next-noselect'
406   Move the cursor to the next article.  This is the default.
407 `next-unread-noselect'
408   Move the cursor to the next unread article.
409
410 If it has any other value or there is no next (unread) article, the
411 article selected before entering to the ephemeral group will appear."
412   :version "23.1" ;; No Gnus
413   :group 'gnus-summary-maneuvering
414   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
415                  (const next) (const next-unread)
416                  (const next-noselect) (const next-unread-noselect)
417                  (sexp :tag "other" :value nil)))
418
419 (defcustom gnus-auto-goto-ignores 'unfetched
420   "*Says how to handle unfetched articles when maneuvering.
421
422 This variable can either be the symbols nil (maneuver to any
423 article), `undownloaded' (maneuvering while unplugged ignores articles
424 that have not been fetched), `always-undownloaded' (maneuvering always
425 ignores articles that have not been fetched), `unfetched' (maneuvering
426 ignores articles whose headers have not been fetched).
427
428 NOTE: The list of unfetched articles will always be nil when plugged
429 and, when unplugged, a subset of the undownloaded article list."
430   :version "22.1"
431   :group 'gnus-summary-maneuvering
432   :type '(choice (const :tag "None" nil)
433                  (const :tag "Undownloaded when unplugged" undownloaded)
434                  (const :tag "Undownloaded" always-undownloaded)
435                  (const :tag "Unfetched" unfetched)))
436
437 (defcustom gnus-summary-check-current nil
438   "*If non-nil, consider the current article when moving.
439 The \"unread\" movement commands will stay on the same line if the
440 current article is unread."
441   :group 'gnus-summary-maneuvering
442   :type 'boolean)
443
444 (defcustom gnus-auto-center-summary 2
445   "*If non-nil, always center the current summary buffer.
446 In particular, if `vertical' do only vertical recentering.  If non-nil
447 and non-`vertical', do both horizontal and vertical recentering."
448   :group 'gnus-summary-maneuvering
449   :type '(choice (const :tag "none" nil)
450                  (const vertical)
451                  (integer :tag "height")
452                  (sexp :menu-tag "both" t)))
453
454 (defvar gnus-auto-center-group t
455   "*If non-nil, always center the group buffer.")
456
457 (defcustom gnus-show-all-headers nil
458   "*If non-nil, don't hide any headers."
459   :group 'gnus-article-hiding
460   :group 'gnus-article-headers
461   :type 'boolean)
462
463 (defcustom gnus-summary-ignore-duplicates nil
464   "*If non-nil, ignore articles with identical Message-ID headers."
465   :group 'gnus-summary
466   :type 'boolean)
467
468 (defcustom gnus-single-article-buffer nil
469   "*If non-nil, display all articles in the same buffer.
470 If nil, each group will get its own article buffer."
471   :version "24.1"
472   :group 'gnus-article-various
473   :type 'boolean)
474
475 (defcustom gnus-break-pages t
476   "*If non-nil, do page breaking on articles.
477 The page delimiter is specified by the `gnus-page-delimiter'
478 variable."
479   :group 'gnus-article-various
480   :type 'boolean)
481
482 (defcustom gnus-move-split-methods nil
483   "*Variable used to suggest where articles are to be moved to.
484 It uses the same syntax as the `gnus-split-methods' variable.
485 However, whereas `gnus-split-methods' specifies file names as targets,
486 this variable specifies group names."
487   :group 'gnus-summary-mail
488   :type '(repeat (choice (list :value (fun) function)
489                          (cons :value ("" "") regexp (repeat string))
490                          (sexp :value nil))))
491
492 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
493   "Function used to compute default prefix for article move/copy/etc prompts.
494 The function should take one argument, a group name, and return a
495 string with the suggested prefix."
496   :group 'gnus-summary-mail
497   :type 'function)
498
499 ;; FIXME: Although the custom type is `character' for the following variables,
500 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
501
502 (defcustom gnus-unread-mark ?           ;Whitespace
503   "*Mark used for unread articles."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-ticked-mark ?!
508   "*Mark used for ticked articles."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-dormant-mark ??
513   "*Mark used for dormant articles."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-del-mark ?r
518   "*Mark used for del'd articles."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-read-mark ?R
523   "*Mark used for read articles."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-expirable-mark ?E
528   "*Mark used for expirable articles."
529   :group 'gnus-summary-marks
530   :type 'character)
531
532 (defcustom gnus-killed-mark ?K
533   "*Mark used for killed articles."
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-spam-mark ?$
538   "*Mark used for spam articles."
539   :version "22.1"
540   :group 'gnus-summary-marks
541   :type 'character)
542
543 (defcustom gnus-kill-file-mark ?X
544   "*Mark used for articles killed by kill files."
545   :group 'gnus-summary-marks
546   :type 'character)
547
548 (defcustom gnus-low-score-mark ?Y
549   "*Mark used for articles with a low score."
550   :group 'gnus-summary-marks
551   :type 'character)
552
553 (defcustom gnus-catchup-mark ?C
554   "*Mark used for articles that are caught up."
555   :group 'gnus-summary-marks
556   :type 'character)
557
558 (defcustom gnus-replied-mark ?A
559   "*Mark used for articles that have been replied to."
560   :group 'gnus-summary-marks
561   :type 'character)
562
563 (defcustom gnus-forwarded-mark ?F
564   "*Mark used for articles that have been forwarded."
565   :version "22.1"
566   :group 'gnus-summary-marks
567   :type 'character)
568
569 (defcustom gnus-recent-mark ?N
570   "*Mark used for articles that are recent."
571   :version "22.1"
572   :group 'gnus-summary-marks
573   :type 'character)
574
575 (defcustom gnus-cached-mark ?*
576   "*Mark used for articles that are in the cache."
577   :group 'gnus-summary-marks
578   :type 'character)
579
580 (defcustom gnus-saved-mark ?S
581   "*Mark used for articles that have been saved."
582   :group 'gnus-summary-marks
583   :type 'character)
584
585 (defcustom gnus-unseen-mark ?.
586   "*Mark used for articles that haven't been seen."
587   :version "22.1"
588   :group 'gnus-summary-marks
589   :type 'character)
590
591 (defcustom gnus-no-mark ?               ;Whitespace
592   "*Mark used for articles that have no other secondary mark."
593   :version "22.1"
594   :group 'gnus-summary-marks
595   :type 'character)
596
597 (defcustom gnus-ancient-mark ?O
598   "*Mark used for ancient articles."
599   :group 'gnus-summary-marks
600   :type 'character)
601
602 (defcustom gnus-sparse-mark ?Q
603   "*Mark used for sparsely reffed articles."
604   :group 'gnus-summary-marks
605   :type 'character)
606
607 (defcustom gnus-canceled-mark ?G
608   "*Mark used for canceled articles."
609   :group 'gnus-summary-marks
610   :type 'character)
611
612 (defcustom gnus-duplicate-mark ?M
613   "*Mark used for duplicate articles."
614   :group 'gnus-summary-marks
615   :type 'character)
616
617 (defcustom gnus-undownloaded-mark ?-
618   "*Mark used for articles that weren't downloaded."
619   :version "22.1"
620   :group 'gnus-summary-marks
621   :type 'character)
622
623 (defcustom gnus-downloaded-mark ?+
624   "*Mark used for articles that were downloaded."
625   :group 'gnus-summary-marks
626   :type 'character)
627
628 (defcustom gnus-downloadable-mark ?%
629   "*Mark used for articles that are to be downloaded."
630   :group 'gnus-summary-marks
631   :type 'character)
632
633 (defcustom gnus-unsendable-mark ?=
634   "*Mark used for articles that won't be sent."
635   :group 'gnus-summary-marks
636   :type 'character)
637
638 (defcustom gnus-score-over-mark ?+
639   "*Score mark used for articles with high scores."
640   :group 'gnus-summary-marks
641   :type 'character)
642
643 (defcustom gnus-score-below-mark ?-
644   "*Score mark used for articles with low scores."
645   :group 'gnus-summary-marks
646   :type 'character)
647
648 (defcustom gnus-empty-thread-mark ?     ;Whitespace
649   "*There is no thread under the article."
650   :group 'gnus-summary-marks
651   :type 'character)
652
653 (defcustom gnus-not-empty-thread-mark ?=
654   "*There is a thread under the article."
655   :group 'gnus-summary-marks
656   :type 'character)
657
658 (defcustom gnus-view-pseudo-asynchronously nil
659   "*If non-nil, Gnus will view pseudo-articles asynchronously."
660   :group 'gnus-extract-view
661   :type 'boolean)
662
663 (defcustom gnus-auto-expirable-marks
664   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
665         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
666         gnus-duplicate-mark)
667   "*The list of marks converted into expiration if a group is auto-expirable."
668   :version "24.1"
669   :group 'gnus-summary
670   :type '(repeat character))
671
672 (defcustom gnus-inhibit-user-auto-expire t
673   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
674   :version "21.1"
675   :group 'gnus-summary
676   :type 'boolean)
677
678 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
679   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
680 If nil, the expirable marks will be unchanged except that the marks
681 will be removed when copying or moving articles to a group that has
682 not turned auto-expire on.  If non-nil, articles that have been read
683 will be marked as expirable when being copied or moved to a group in
684 which auto-expire is turned on."
685   :version "23.2"
686   :type 'boolean
687   :group 'gnus-summary-marks)
688
689 (defcustom gnus-view-pseudos nil
690   "*If `automatic', pseudo-articles will be viewed automatically.
691 If `not-confirm', pseudos will be viewed automatically, and the user
692 will not be asked to confirm the command."
693   :group 'gnus-extract-view
694   :type '(choice (const :tag "off" nil)
695                  (const automatic)
696                  (const not-confirm)))
697
698 (defcustom gnus-view-pseudos-separately t
699   "*If non-nil, one pseudo-article will be created for each file to be viewed.
700 If nil, all files that use the same viewing command will be given as a
701 list of parameters to that command."
702   :group 'gnus-extract-view
703   :type 'boolean)
704
705 (defcustom gnus-insert-pseudo-articles t
706   "*If non-nil, insert pseudo-articles when decoding articles."
707   :group 'gnus-extract-view
708   :type 'boolean)
709
710 (defcustom gnus-summary-dummy-line-format
711   "   %(:                             :%) %S\n"
712   "*The format specification for the dummy roots in the summary buffer.
713 It works along the same lines as a normal formatting string,
714 with some simple extensions.
715
716 %S  The subject
717
718 General format specifiers can also be used.
719 See `(gnus)Formatting Variables'."
720   :link '(custom-manual "(gnus)Formatting Variables")
721   :group 'gnus-threading
722   :type 'string)
723
724 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
725   "*The format specification for the summary mode line.
726 It works along the same lines as a normal formatting string,
727 with some simple extensions:
728
729 %G  Group name
730 %p  Unprefixed group name
731 %A  Current article number
732 %z  Current article score
733 %V  Gnus version
734 %U  Number of unread articles in the group
735 %e  Number of unselected articles in the group
736 %Z  A string with unread/unselected article counts
737 %g  Shortish group name
738 %S  Subject of the current article
739 %u  User-defined spec
740 %s  Current score file name
741 %d  Number of dormant articles
742 %r  Number of articles that have been marked as read in this session
743 %E  Number of articles expunged by the score files"
744   :group 'gnus-summary-format
745   :type 'string)
746
747 (defcustom gnus-list-identifiers nil
748   "Regexp that matches list identifiers to be removed from subject.
749 This can also be a list of regexps."
750   :version "21.1"
751   :group 'gnus-summary-format
752   :group 'gnus-article-hiding
753   :type '(choice (const :tag "none" nil)
754                  (regexp :value ".*")
755                  (repeat :value (".*") regexp)))
756
757 (defcustom gnus-summary-mark-below 0
758   "*Mark all articles with a score below this variable as read.
759 This variable is local to each summary buffer and usually set by the
760 score file."
761   :group 'gnus-score-default
762   :type 'integer)
763
764 (defun gnus-widget-reversible-match (widget value)
765   "Ignoring WIDGET, convert VALUE to internal form.
766 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
767   ;; (debug value)
768   (or (symbolp value)
769       (and (listp value)
770            (eq (length value) 2)
771            (eq (nth 0 value) 'not)
772            (symbolp (nth 1 value)))))
773
774 (defun gnus-widget-reversible-to-internal (widget value)
775   "Ignoring WIDGET, convert VALUE to internal form.
776 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
777 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
778   ;; (debug value)
779   (if (atom value)
780       (list value nil)
781     (list (nth 1 value) t)))
782
783 (defun gnus-widget-reversible-to-external (widget value)
784   "Ignoring WIDGET, convert VALUE to external form.
785 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
786 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
787   ;; (debug value)
788   (if (nth 1 value)
789       (list 'not (nth 0 value))
790     (nth 0 value)))
791
792 (define-widget 'gnus-widget-reversible 'group
793   "A `group' that convert values."
794   :match 'gnus-widget-reversible-match
795   :value-to-internal 'gnus-widget-reversible-to-internal
796   :value-to-external 'gnus-widget-reversible-to-external)
797
798 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
799   "*List of functions used for sorting articles in the summary buffer.
800
801 Each function takes two articles and returns non-nil if the first
802 article should be sorted before the other.  If you use more than one
803 function, the primary sort function should be the last.  You should
804 probably always include `gnus-article-sort-by-number' in the list of
805 sorting functions -- preferably first.  Also note that sorting by date
806 is often much slower than sorting by number, and the sorting order is
807 very similar.  (Sorting by date means sorting by the time the message
808 was sent, sorting by number means sorting by arrival time.)
809
810 Each item can also be a list `(not F)' where F is a function;
811 this reverses the sort order.
812
813 Ready-made functions include `gnus-article-sort-by-number',
814 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
815 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
816 and `gnus-article-sort-by-score'.
817
818 When threading is turned on, the variable `gnus-thread-sort-functions'
819 controls how articles are sorted."
820   :group 'gnus-summary-sort
821   :type '(repeat (gnus-widget-reversible
822                   (choice (function-item gnus-article-sort-by-number)
823                           (function-item gnus-article-sort-by-author)
824                           (function-item gnus-article-sort-by-subject)
825                           (function-item gnus-article-sort-by-date)
826                           (function-item gnus-article-sort-by-score)
827                           (function-item gnus-article-sort-by-random)
828                           (function :tag "other"))
829                   (boolean :tag "Reverse order"))))
830
831
832 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
833   "*List of functions used for sorting threads in the summary buffer.
834 By default, threads are sorted by article number.
835
836 Each function takes two threads and returns non-nil if the first
837 thread should be sorted before the other.  If you use more than one
838 function, the primary sort function should be the last.  You should
839 probably always include `gnus-thread-sort-by-number' in the list of
840 sorting functions -- preferably first.  Also note that sorting by date
841 is often much slower than sorting by number, and the sorting order is
842 very similar.  (Sorting by date means sorting by the time the message
843 was sent, sorting by number means sorting by arrival time.)
844
845 Each list item can also be a list `(not F)' where F is a
846 function; this specifies reversed sort order.
847
848 Ready-made functions include `gnus-thread-sort-by-number',
849 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
850 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
851 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
852 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
853 and `gnus-thread-sort-by-total-score' (see
854 `gnus-thread-score-function').
855
856 When threading is turned off, the variable
857 `gnus-article-sort-functions' controls how articles are sorted."
858   :group 'gnus-summary-sort
859   :type '(repeat
860           (gnus-widget-reversible
861            (choice (function-item gnus-thread-sort-by-number)
862                    (function-item gnus-thread-sort-by-author)
863                    (function-item gnus-thread-sort-by-recipient)
864                    (function-item gnus-thread-sort-by-subject)
865                    (function-item gnus-thread-sort-by-date)
866                    (function-item gnus-thread-sort-by-score)
867                    (function-item gnus-thread-sort-by-most-recent-number)
868                    (function-item gnus-thread-sort-by-most-recent-date)
869                    (function-item gnus-thread-sort-by-random)
870                    (function-item gnus-thread-sort-by-total-score)
871                    (function :tag "other"))
872            (boolean :tag "Reverse order"))))
873
874 (defcustom gnus-thread-score-function '+
875   "*Function used for calculating the total score of a thread.
876
877 The function is called with the scores of the article and each
878 subthread and should then return the score of the thread.
879
880 Some functions you can use are `+', `max', or `min'."
881   :group 'gnus-summary-sort
882   :type 'function)
883
884 (defcustom gnus-summary-expunge-below nil
885   "All articles that have a score less than this variable will be expunged.
886 This variable is local to the summary buffers."
887   :group 'gnus-score-default
888   :type '(choice (const :tag "off" nil)
889                  integer))
890
891 (defcustom gnus-thread-expunge-below nil
892   "All threads that have a total score less than this variable will be expunged.
893 See `gnus-thread-score-function' for en explanation of what a
894 \"thread score\" is.
895
896 This variable is local to the summary buffers."
897   :group 'gnus-threading
898   :group 'gnus-score-default
899   :type '(choice (const :tag "off" nil)
900                  integer))
901
902 (defcustom gnus-summary-mode-hook nil
903   "*A hook for Gnus summary mode.
904 This hook is run before any variables are set in the summary buffer."
905   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
906   :group 'gnus-summary-various
907   :type 'hook)
908
909 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
910 (when (featurep 'xemacs)
911   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
912   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
913   (add-hook 'gnus-summary-mode-hook
914             'gnus-xmas-switch-horizontal-scrollbar-off))
915
916 (defcustom gnus-summary-menu-hook nil
917   "*Hook run after the creation of the summary mode menu."
918   :group 'gnus-summary-visual
919   :type 'hook)
920
921 (defcustom gnus-summary-exit-hook nil
922   "*A hook called on exit from the summary buffer.
923 It will be called with point in the group buffer."
924   :group 'gnus-summary-exit
925   :type 'hook)
926
927 (defcustom gnus-summary-prepare-hook nil
928   "*A hook called after the summary buffer has been generated.
929 If you want to modify the summary buffer, you can use this hook."
930   :group 'gnus-summary-various
931   :type 'hook)
932
933 (defcustom gnus-summary-prepared-hook nil
934   "*A hook called as the last thing after the summary buffer has been generated."
935   :group 'gnus-summary-various
936   :type 'hook)
937
938 (defcustom gnus-summary-generate-hook nil
939   "*A hook run just before generating the summary buffer.
940 This hook is commonly used to customize threading variables and the
941 like."
942   :group 'gnus-summary-various
943   :type 'hook)
944
945 (defcustom gnus-select-group-hook nil
946   "*A hook called when a newsgroup is selected.
947
948 If you'd like to simplify subjects like the
949 `gnus-summary-next-same-subject' command does, you can use the
950 following hook:
951
952  (add-hook gnus-select-group-hook
953            (lambda ()
954              (mapcar (lambda (header)
955                        (mail-header-set-subject
956                         header
957                         (gnus-simplify-subject
958                          (mail-header-subject header) 're-only)))
959                      gnus-newsgroup-headers)))"
960   :group 'gnus-group-select
961   :type 'hook)
962
963 (defcustom gnus-select-article-hook nil
964   "*A hook called when an article is selected."
965   :group 'gnus-summary-choose
966   :options '(gnus-agent-fetch-selected-article)
967   :type 'hook)
968
969 (defcustom gnus-visual-mark-article-hook
970   (list 'gnus-highlight-selected-summary)
971   "*Hook run after selecting an article in the summary buffer.
972 It is meant to be used for highlighting the article in some way.  It
973 is not run if `gnus-visual' is nil."
974   :group 'gnus-summary-visual
975   :type 'hook)
976
977 (defcustom gnus-parse-headers-hook nil
978   "*A hook called before parsing the headers."
979   :group 'gnus-various
980   :type 'hook)
981
982 (defcustom gnus-exit-group-hook nil
983   "*A hook called when exiting summary mode.
984 This hook is not called from the non-updating exit commands like `Q'."
985   :group 'gnus-various
986   :type 'hook)
987
988 (defcustom gnus-summary-update-hook nil
989   "*A hook called when a summary line is changed.
990 The hook will not be called if `gnus-visual' is nil.
991
992 The default function `gnus-summary-highlight-line' will
993 highlight the line according to the `gnus-summary-highlight'
994 variable."
995   :group 'gnus-summary-visual
996   :type 'hook)
997
998 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
999   "*A hook called when an article is selected for the first time.
1000 The hook is intended to mark an article as read (or unread)
1001 automatically when it is selected."
1002   :group 'gnus-summary-choose
1003   :type 'hook)
1004
1005 (defcustom gnus-group-no-more-groups-hook nil
1006   "*A hook run when returning to group mode having no more (unread) groups."
1007   :group 'gnus-group-select
1008   :type 'hook)
1009
1010 (defcustom gnus-ps-print-hook nil
1011   "*A hook run before ps-printing something from Gnus."
1012   :group 'gnus-summary
1013   :type 'hook)
1014
1015 (defcustom gnus-summary-article-move-hook nil
1016   "*A hook called after an article is moved, copied, respooled, or crossposted."
1017   :version "22.1"
1018   :group 'gnus-summary
1019   :type 'hook)
1020
1021 (defcustom gnus-summary-article-delete-hook nil
1022   "*A hook called after an article is deleted."
1023   :version "22.1"
1024   :group 'gnus-summary
1025   :type 'hook)
1026
1027 (defcustom gnus-summary-article-expire-hook nil
1028   "*A hook called after an article is expired."
1029   :version "22.1"
1030   :group 'gnus-summary
1031   :type 'hook)
1032
1033 (defcustom gnus-summary-display-arrow
1034   (and (fboundp 'display-graphic-p)
1035        (display-graphic-p))
1036   "*If non-nil, display an arrow highlighting the current article."
1037   :version "22.1"
1038   :group 'gnus-summary
1039   :type 'boolean)
1040
1041 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1042   "Face used for highlighting the current article in the summary buffer."
1043   :group 'gnus-summary-visual
1044   :type 'face)
1045
1046 (defvar gnus-tmp-downloaded nil)
1047
1048 (defcustom gnus-summary-highlight
1049   '(((eq mark gnus-canceled-mark)
1050      . gnus-summary-cancelled)
1051     ((and uncached (> score default-high))
1052      . gnus-summary-high-undownloaded)
1053     ((and uncached (< score default-low))
1054      . gnus-summary-low-undownloaded)
1055     (uncached
1056      . gnus-summary-normal-undownloaded)
1057     ((and (> score default-high)
1058           (or (eq mark gnus-dormant-mark)
1059               (eq mark gnus-ticked-mark)))
1060      . gnus-summary-high-ticked)
1061     ((and (< score default-low)
1062           (or (eq mark gnus-dormant-mark)
1063               (eq mark gnus-ticked-mark)))
1064      . gnus-summary-low-ticked)
1065     ((or (eq mark gnus-dormant-mark)
1066          (eq mark gnus-ticked-mark))
1067      . gnus-summary-normal-ticked)
1068     ((and (> score default-high) (eq mark gnus-ancient-mark))
1069      . gnus-summary-high-ancient)
1070     ((and (< score default-low) (eq mark gnus-ancient-mark))
1071      . gnus-summary-low-ancient)
1072     ((eq mark gnus-ancient-mark)
1073      . gnus-summary-normal-ancient)
1074     ((and (> score default-high) (eq mark gnus-unread-mark))
1075      . gnus-summary-high-unread)
1076     ((and (< score default-low) (eq mark gnus-unread-mark))
1077      . gnus-summary-low-unread)
1078     ((eq mark gnus-unread-mark)
1079      . gnus-summary-normal-unread)
1080     ((> score default-high)
1081      . gnus-summary-high-read)
1082     ((< score default-low)
1083      . gnus-summary-low-read)
1084     (t
1085      . gnus-summary-normal-read))
1086   "*Controls the highlighting of summary buffer lines.
1087
1088 A list of (FORM . FACE) pairs.  When deciding how a particular
1089 summary line should be displayed, each form is evaluated.  The content
1090 of the face field after the first true form is used.  You can change
1091 how those summary lines are displayed, by editing the face field.
1092
1093 You can use the following variables in the FORM field.
1094
1095 score:        The article's score.
1096 default:      The default article score.
1097 default-high: The default score for high scored articles.
1098 default-low:  The default score for low scored articles.
1099 below:        The score below which articles are automatically marked as read.
1100 mark:         The article's mark.
1101 uncached:     Non-nil if the article is uncached."
1102   :group 'gnus-summary-visual
1103   :type '(repeat (cons (sexp :tag "Form" nil)
1104                        face)))
1105 (put 'gnus-summary-highlight 'risky-local-variable t)
1106
1107 (defcustom gnus-alter-header-function nil
1108   "Function called to allow alteration of article header structures.
1109 The function is called with one parameter, the article header vector,
1110 which it may alter in any way."
1111   :type '(choice (const :tag "None" nil)
1112                  function)
1113   :group 'gnus-summary)
1114
1115 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1116   "Function used to decode a string with encoded words.")
1117
1118 (defvar gnus-decode-encoded-address-function
1119   'mail-decode-encoded-address-string
1120   "Function used to decode addresses with encoded words.")
1121
1122 (defcustom gnus-extra-headers '(To Newsgroups)
1123   "*Extra headers to parse."
1124   :version "21.1"
1125   :group 'gnus-summary
1126   :type '(repeat symbol))
1127
1128 (defcustom gnus-ignored-from-addresses
1129   (and user-mail-address
1130        (not (string= user-mail-address ""))
1131        (regexp-quote user-mail-address))
1132   "*From headers that may be suppressed in favor of To headers.
1133 This can be a regexp or a list of regexps."
1134   :version "21.1"
1135   :group 'gnus-summary
1136   :type '(choice regexp
1137                  (repeat :tag "Regexp List" regexp)))
1138
1139 (defsubst gnus-ignored-from-addresses ()
1140   (gmm-regexp-concat gnus-ignored-from-addresses))
1141
1142 (defcustom gnus-summary-to-prefix "-> "
1143   "*String prefixed to the To field in the summary line when
1144 using `gnus-ignored-from-addresses'."
1145   :version "22.1"
1146   :group 'gnus-summary
1147   :type 'string)
1148
1149 (defcustom gnus-summary-newsgroup-prefix "=> "
1150   "*String prefixed to the Newsgroup field in the summary
1151 line when using `gnus-ignored-from-addresses'."
1152   :version "22.1"
1153   :group 'gnus-summary
1154   :type 'string)
1155
1156 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1157   "List of charsets that should be ignored.
1158 When these charsets are used in the \"charset\" parameter, the
1159 default charset will be used instead."
1160   :version "21.1"
1161   :type '(repeat symbol)
1162   :group 'gnus-charset)
1163
1164 (defcustom gnus-newsgroup-maximum-articles nil
1165   "The maximum number of articles a newsgroup.
1166 If this is a number, old articles in a newsgroup exceeding this number
1167 are silently ignored.  If it is nil, no article is ignored.  Note that
1168 setting this variable to a number might prevent you from reading very
1169 old articles."
1170   :group 'gnus-group-select
1171   :version "22.2"
1172   :type '(choice (const :tag "No limit" nil)
1173                  integer))
1174
1175 (gnus-define-group-parameter
1176  ignored-charsets
1177  :type list
1178  :function-document
1179  "Return the ignored charsets of GROUP."
1180  :variable gnus-group-ignored-charsets-alist
1181  :variable-default
1182  '(("alt\\.chinese\\.text" iso-8859-1))
1183  :variable-document
1184  "Alist of regexps (to match group names) and charsets that should be ignored.
1185 When these charsets are used in the \"charset\" parameter, the
1186 default charset will be used instead."
1187  :variable-group gnus-charset
1188  :variable-type '(repeat (cons (regexp :tag "Group")
1189                                (repeat symbol)))
1190  :parameter-type '(choice :tag "Ignored charsets"
1191                           :value nil
1192                           (repeat (symbol)))
1193  :parameter-document       "\
1194 List of charsets that should be ignored.
1195
1196 When these charsets are used in the \"charset\" parameter, the
1197 default charset will be used instead.")
1198
1199 (defcustom gnus-group-highlight-words-alist nil
1200   "Alist of group regexps and highlight regexps.
1201 This variable uses the same syntax as `gnus-emphasis-alist'."
1202   :version "21.1"
1203   :type '(repeat (cons (regexp :tag "Group")
1204                        (repeat (list (regexp :tag "Highlight regexp")
1205                                      (number :tag "Group for entire word" 0)
1206                                      (number :tag "Group for displayed part" 0)
1207                                      (symbol :tag "Face"
1208                                              gnus-emphasis-highlight-words)))))
1209   :group 'gnus-summary-visual)
1210
1211 (defcustom gnus-summary-show-article-charset-alist
1212   nil
1213   "Alist of number and charset.
1214 The article will be shown with the charset corresponding to the
1215 numbered argument.
1216 For example: ((1 . cn-gb-2312) (2 . big5))."
1217   :version "21.1"
1218   :type '(repeat (cons (number :tag "Argument" 1)
1219                        (symbol :tag "Charset")))
1220   :group 'gnus-charset)
1221
1222 (defcustom gnus-preserve-marks t
1223   "Whether marks are preserved when moving, copying and respooling messages."
1224   :version "21.1"
1225   :type 'boolean
1226   :group 'gnus-summary-marks)
1227
1228 (defcustom gnus-propagate-marks t
1229   "If non-nil, do not propagate marks to the backends."
1230   :version "23.1" ;; No Gnus
1231   :type 'boolean
1232   :group 'gnus-summary-marks)
1233
1234 (defcustom gnus-alter-articles-to-read-function nil
1235   "Function to be called to alter the list of articles to be selected."
1236   :type '(choice (const nil) function)
1237   :group 'gnus-summary)
1238
1239 (defcustom gnus-orphan-score nil
1240   "*All orphans get this score added.  Set in the score file."
1241   :group 'gnus-score-default
1242   :type '(choice (const nil)
1243                  integer))
1244
1245 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1246   "*A regexp to match MIME parts when saving multiple parts of a
1247 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1248 This regexp will be used by default when prompting the user for which
1249 type of files to save."
1250   :group 'gnus-summary
1251   :type 'regexp)
1252
1253 (defcustom gnus-read-all-available-headers nil
1254   "Whether Gnus should parse all headers made available to it.
1255 This is mostly relevant for slow back ends where the user may
1256 wish to widen the summary buffer to include all headers
1257 that were fetched."
1258   :version "22.1"
1259   :group 'gnus-summary
1260   :type '(choice boolean regexp))
1261
1262 (defcustom gnus-summary-pipe-output-default-command nil
1263   "Command (and optional arguments) used to pipe article to subprocess.
1264 This will be used as the default command if it is non-nil.  The value
1265 will be updated if you modify it when executing the command
1266 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1267   :version "23.1" ;; No Gnus
1268   :group 'gnus-summary
1269   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1270
1271 (defcustom gnus-summary-muttprint-program "muttprint"
1272   "Command (and optional arguments) used to run Muttprint.
1273 The value will be updated if you modify it when executing the command
1274 `gnus-summary-muttprint'."
1275   :version "22.1"
1276   :group 'gnus-summary
1277   :type 'string)
1278
1279 (defcustom gnus-article-loose-mime t
1280   "If non-nil, don't require MIME-Version header.
1281 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1282 supply the MIME-Version header or deliberately strip it from the mail.
1283 If non-nil (the default), Gnus will treat some articles as MIME
1284 even if the MIME-Version header is missing."
1285   :version "22.1"
1286   :type 'boolean
1287   :group 'gnus-article-mime)
1288
1289 (defcustom gnus-article-emulate-mime t
1290   "If non-nil, use MIME emulation for uuencode and the like.
1291 This means that Gnus will search message bodies for text that look
1292 like uuencoded bits, yEncoded bits, and so on, and present that using
1293 the normal Gnus MIME machinery."
1294   :version "22.1"
1295   :type 'boolean
1296   :group 'gnus-article-mime)
1297
1298 ;;; Internal variables
1299
1300 (defvar gnus-summary-display-cache nil)
1301 (defvar gnus-article-mime-handles nil)
1302 (defvar gnus-article-decoded-p nil)
1303 (defvar gnus-article-charset nil)
1304 (defvar gnus-article-ignored-charsets nil)
1305 (defvar gnus-scores-exclude-files nil)
1306 (defvar gnus-page-broken nil)
1307
1308 (defvar gnus-original-article nil)
1309 (defvar gnus-article-internal-prepare-hook nil)
1310 (defvar gnus-newsgroup-process-stack nil)
1311
1312 (defvar gnus-thread-indent-array nil)
1313 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1314 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1315   "Function called to sort the articles within a thread after it has been gathered together.")
1316
1317 (defvar gnus-summary-save-parts-type-history nil)
1318 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1319
1320 ;; Avoid highlighting in kill files.
1321 (defvar gnus-summary-inhibit-highlight nil)
1322 (defvar gnus-newsgroup-selected-overlay nil)
1323 (defvar gnus-inhibit-limiting nil)
1324 (defvar gnus-newsgroup-adaptive-score-file nil)
1325 (defvar gnus-current-score-file nil)
1326 (defvar gnus-current-move-group nil)
1327 (defvar gnus-current-copy-group nil)
1328 (defvar gnus-current-crosspost-group nil)
1329 (defvar gnus-newsgroup-display nil)
1330
1331 (defvar gnus-newsgroup-dependencies nil)
1332 (defvar gnus-newsgroup-adaptive nil)
1333 (defvar gnus-summary-display-article-function nil)
1334 (defvar gnus-summary-highlight-line-function nil
1335   "Function called after highlighting a summary line.")
1336
1337 (defvar gnus-summary-line-format-alist
1338   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1339     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1340     (?s gnus-tmp-subject-or-nil ?s)
1341     (?n gnus-tmp-name ?s)
1342     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1343         ?s)
1344     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1345             gnus-tmp-from) ?s)
1346     (?F gnus-tmp-from ?s)
1347     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1348     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1349     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1350     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1351     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1352     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1353     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1354     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1355     (?L gnus-tmp-lines ?s)
1356     (?O gnus-tmp-downloaded ?c)
1357     (?I gnus-tmp-indentation ?s)
1358     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1359     (?R gnus-tmp-replied ?c)
1360     (?\[ gnus-tmp-opening-bracket ?c)
1361     (?\] gnus-tmp-closing-bracket ?c)
1362     (?\> (make-string gnus-tmp-level ? ) ?s)
1363     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1364     (?i gnus-tmp-score ?d)
1365     (?z gnus-tmp-score-char ?c)
1366     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1367     (?U gnus-tmp-unread ?c)
1368     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1369         ?s)
1370     (?t (gnus-summary-number-of-articles-in-thread
1371          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1372         ?d)
1373     (?e (gnus-summary-number-of-articles-in-thread
1374          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1375         ?c)
1376     (?u gnus-tmp-user-defined ?s)
1377     (?P (gnus-pick-line-number) ?d)
1378     (?B gnus-tmp-thread-tree-header-string ?s)
1379     (user-date (gnus-user-date
1380                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1381   "An alist of format specifications that can appear in summary lines.
1382 These are paired with what variables they correspond with, along with
1383 the type of the variable (string, integer, character, etc).")
1384
1385 (defvar gnus-summary-dummy-line-format-alist
1386   `((?S gnus-tmp-subject ?s)
1387     (?N gnus-tmp-number ?d)
1388     (?u gnus-tmp-user-defined ?s)))
1389
1390 (defvar gnus-summary-mode-line-format-alist
1391   `((?G gnus-tmp-group-name ?s)
1392     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1393     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1394     (?A gnus-tmp-article-number ?d)
1395     (?Z gnus-tmp-unread-and-unselected ?s)
1396     (?V gnus-version ?s)
1397     (?U gnus-tmp-unread-and-unticked ?d)
1398     (?S gnus-tmp-subject ?s)
1399     (?e gnus-tmp-unselected ?d)
1400     (?u gnus-tmp-user-defined ?s)
1401     (?d (length gnus-newsgroup-dormant) ?d)
1402     (?t (length gnus-newsgroup-marked) ?d)
1403     (?h (length gnus-newsgroup-spam-marked) ?d)
1404     (?r (length gnus-newsgroup-reads) ?d)
1405     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1406     (?E gnus-newsgroup-expunged-tally ?d)
1407     (?s (gnus-current-score-file-nondirectory) ?s)))
1408
1409 ;; This is here rather than in gnus-art for compilation reasons.
1410 (defvar gnus-article-mode-line-format-alist
1411   (nconc '((?w (gnus-article-wash-status) ?s)
1412            (?m (gnus-article-mime-part-status) ?s))
1413          gnus-summary-mode-line-format-alist))
1414
1415 (defvar gnus-last-search-regexp nil
1416   "Default regexp for article search command.")
1417
1418 (defvar gnus-last-shell-command nil
1419   "Default shell command on article.")
1420
1421 (defvar gnus-newsgroup-agentized nil
1422   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1423 (defvar gnus-newsgroup-begin nil)
1424 (defvar gnus-newsgroup-end nil)
1425 (defvar gnus-newsgroup-last-rmail nil)
1426 (defvar gnus-newsgroup-last-mail nil)
1427 (defvar gnus-newsgroup-last-folder nil)
1428 (defvar gnus-newsgroup-last-file nil)
1429 (defvar gnus-newsgroup-last-directory nil)
1430 (defvar gnus-newsgroup-auto-expire nil)
1431 (defvar gnus-newsgroup-active nil)
1432
1433 (defvar gnus-newsgroup-data nil)
1434 (defvar gnus-newsgroup-data-reverse nil)
1435 (defvar gnus-newsgroup-limit nil)
1436 (defvar gnus-newsgroup-limits nil)
1437 (defvar gnus-summary-use-undownloaded-faces nil)
1438
1439 (defvar gnus-newsgroup-unreads nil
1440   "Sorted list of unread articles in the current newsgroup.")
1441
1442 (defvar gnus-newsgroup-unselected nil
1443   "Sorted list of unselected unread articles in the current newsgroup.")
1444
1445 (defvar gnus-newsgroup-reads nil
1446   "Alist of read articles and article marks in the current newsgroup.")
1447
1448 (defvar gnus-newsgroup-expunged-tally nil)
1449
1450 (defvar gnus-newsgroup-marked nil
1451   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1452
1453 (defvar gnus-newsgroup-spam-marked nil
1454   "List of ranges of articles that have been marked as spam.")
1455
1456 (defvar gnus-newsgroup-killed nil
1457   "List of ranges of articles that have been through the scoring process.")
1458
1459 (defvar gnus-newsgroup-cached nil
1460   "Sorted list of articles that come from the article cache.")
1461
1462 (defvar gnus-newsgroup-saved nil
1463   "List of articles that have been saved.")
1464
1465 (defvar gnus-newsgroup-kill-headers nil)
1466
1467 (defvar gnus-newsgroup-replied nil
1468   "List of articles that have been replied to in the current newsgroup.")
1469
1470 (defvar gnus-newsgroup-forwarded nil
1471   "List of articles that have been forwarded in the current newsgroup.")
1472
1473 (defvar gnus-newsgroup-recent nil
1474   "List of articles that have are recent in the current newsgroup.")
1475
1476 (defvar gnus-newsgroup-expirable nil
1477   "Sorted list of articles in the current newsgroup that can be expired.")
1478
1479 (defvar gnus-newsgroup-processable nil
1480   "List of articles in the current newsgroup that can be processed.")
1481
1482 (defvar gnus-newsgroup-downloadable nil
1483   "Sorted list of articles in the current newsgroup that can be processed.")
1484
1485 (defvar gnus-newsgroup-unfetched nil
1486   "Sorted list of articles in the current newsgroup whose headers have
1487 not been fetched into the agent.
1488
1489 This list will always be a subset of gnus-newsgroup-undownloaded.")
1490
1491 (defvar gnus-newsgroup-undownloaded nil
1492   "List of articles in the current newsgroup that haven't been downloaded.")
1493
1494 (defvar gnus-newsgroup-unsendable nil
1495   "List of articles in the current newsgroup that won't be sent.")
1496
1497 (defvar gnus-newsgroup-bookmarks nil
1498   "List of articles in the current newsgroup that have bookmarks.")
1499
1500 (defvar gnus-newsgroup-dormant nil
1501   "Sorted list of dormant articles in the current newsgroup.")
1502
1503 (defvar gnus-newsgroup-unseen nil
1504   "List of unseen articles in the current newsgroup.")
1505
1506 (defvar gnus-newsgroup-seen nil
1507   "Range of seen articles in the current newsgroup.")
1508
1509 (defvar gnus-newsgroup-articles nil
1510   "List of articles in the current newsgroup.")
1511
1512 (defvar gnus-newsgroup-scored nil
1513   "List of scored articles in the current newsgroup.")
1514
1515 (defvar gnus-newsgroup-headers nil
1516   "List of article headers in the current newsgroup.")
1517
1518 (defvar gnus-newsgroup-threads nil)
1519
1520 (defvar gnus-newsgroup-prepared nil
1521   "Whether the current group has been prepared properly.")
1522
1523 (defvar gnus-newsgroup-ancient nil
1524   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1525
1526 (defvar gnus-newsgroup-sparse nil)
1527
1528 (defvar gnus-current-article nil)
1529 (defvar gnus-article-current nil)
1530 (defvar gnus-current-headers nil)
1531 (defvar gnus-have-all-headers nil)
1532 (defvar gnus-last-article nil)
1533 (defvar gnus-newsgroup-history nil)
1534 (defvar gnus-newsgroup-charset nil)
1535 (defvar gnus-newsgroup-ephemeral-charset nil)
1536 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1537
1538 (defvar gnus-article-before-search nil)
1539
1540 (defvar gnus-summary-local-variables
1541   '(gnus-newsgroup-name
1542
1543     ;; Marks lists
1544     gnus-newsgroup-unreads
1545     gnus-newsgroup-unselected
1546     gnus-newsgroup-marked
1547     gnus-newsgroup-spam-marked
1548     gnus-newsgroup-reads
1549     gnus-newsgroup-saved
1550     gnus-newsgroup-replied
1551     gnus-newsgroup-forwarded
1552     gnus-newsgroup-recent
1553     gnus-newsgroup-expirable
1554     gnus-newsgroup-killed
1555     gnus-newsgroup-unseen
1556     gnus-newsgroup-seen
1557     gnus-newsgroup-cached
1558     gnus-newsgroup-downloadable
1559     gnus-newsgroup-undownloaded
1560     gnus-newsgroup-unsendable
1561
1562     gnus-newsgroup-begin gnus-newsgroup-end
1563     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1564     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1565     gnus-newsgroup-last-directory
1566     gnus-newsgroup-auto-expire
1567     gnus-newsgroup-processable
1568     gnus-newsgroup-unfetched
1569     gnus-newsgroup-articles
1570     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1571     gnus-newsgroup-headers gnus-newsgroup-threads
1572     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1573     gnus-current-article gnus-current-headers gnus-have-all-headers
1574     gnus-last-article gnus-article-internal-prepare-hook
1575     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1576     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1577     gnus-thread-expunge-below
1578     gnus-score-alist gnus-current-score-file
1579     (gnus-summary-expunge-below . global)
1580     (gnus-summary-mark-below . global)
1581     (gnus-orphan-score . global)
1582     gnus-newsgroup-active gnus-scores-exclude-files
1583     gnus-newsgroup-history gnus-newsgroup-ancient
1584     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1585     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1586     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1587     (gnus-newsgroup-expunged-tally . 0)
1588     gnus-cache-removable-articles
1589     gnus-newsgroup-data gnus-newsgroup-data-reverse
1590     gnus-newsgroup-limit gnus-newsgroup-limits
1591     gnus-newsgroup-charset gnus-newsgroup-display
1592     gnus-summary-use-undownloaded-faces)
1593   "Variables that are buffer-local to the summary buffers.")
1594
1595 (defvar gnus-newsgroup-variables nil
1596   "A list of variables that have separate values in different newsgroups.
1597 A list of newsgroup (summary buffer) local variables, or cons of
1598 variables and their default expressions to be evalled (when the default
1599 values are not nil), that should be made global while the summary buffer
1600 is active.
1601
1602 Note: The default expressions will be evaluated (using function `eval')
1603 before assignment to the local variable rather than just assigned to it.
1604 If the default expression is the symbol `global', that symbol will not
1605 be evaluated but the global value of the local variable will be used
1606 instead.
1607
1608 These variables can be used to set variables in the group parameters
1609 while still allowing them to affect operations done in other buffers.
1610 For example:
1611
1612 \(setq gnus-newsgroup-variables
1613      '(message-use-followup-to
1614        (gnus-visible-headers .
1615          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1616 ")
1617
1618 (eval-when-compile
1619   ;; Bind features so that require will believe that gnus-sum has
1620   ;; already been loaded (avoids infinite recursion)
1621   (let ((features (cons 'gnus-sum features)))
1622     (require 'gnus-art)))
1623
1624 ;; MIME stuff.
1625
1626 (defvar gnus-decode-encoded-word-methods
1627   '(mail-decode-encoded-word-string)
1628   "List of methods used to decode encoded words.
1629
1630 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1631 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1632 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1633 whose names match REGEXP.
1634
1635 For example:
1636 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1637  mail-decode-encoded-word-string
1638  (\"chinese\" . rfc1843-decode-string))")
1639
1640 (defvar gnus-decode-encoded-word-methods-cache nil)
1641
1642 (defun gnus-multi-decode-encoded-word-string (string)
1643   "Apply the functions from `gnus-encoded-word-methods' that match."
1644   (unless (and gnus-decode-encoded-word-methods-cache
1645                (eq gnus-newsgroup-name
1646                    (car gnus-decode-encoded-word-methods-cache)))
1647     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1648     (dolist (method gnus-decode-encoded-word-methods)
1649       (if (symbolp method)
1650           (nconc gnus-decode-encoded-word-methods-cache (list method))
1651         (if (and gnus-newsgroup-name
1652                  (string-match (car method) gnus-newsgroup-name))
1653             (nconc gnus-decode-encoded-word-methods-cache
1654                    (list (cdr method)))))))
1655   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1656     (setq string (funcall method string))))
1657
1658 ;; Subject simplification.
1659
1660 (defun gnus-simplify-whitespace (str)
1661   "Remove excessive whitespace from STR."
1662   ;; Multiple spaces.
1663   (while (string-match "[ \t][ \t]+" str)
1664     (setq str (concat (substring str 0 (match-beginning 0))
1665                         " "
1666                         (substring str (match-end 0)))))
1667   ;; Leading spaces.
1668   (when (string-match "^[ \t]+" str)
1669     (setq str (substring str (match-end 0))))
1670   ;; Trailing spaces.
1671   (when (string-match "[ \t]+$" str)
1672     (setq str (substring str 0 (match-beginning 0))))
1673   str)
1674
1675 (defun gnus-simplify-all-whitespace (str)
1676   "Remove all whitespace from STR."
1677   (while (string-match "[ \t\n]+" str)
1678     (setq str (replace-match "" nil nil str)))
1679   str)
1680
1681 (defsubst gnus-simplify-subject-re (subject)
1682   "Remove \"Re:\" from subject lines."
1683   (if (string-match message-subject-re-regexp subject)
1684       (substring subject (match-end 0))
1685     subject))
1686
1687 (defun gnus-simplify-subject (subject &optional re-only)
1688   "Remove `Re:' and words in parentheses.
1689 If RE-ONLY is non-nil, strip leading `Re:'s only."
1690   (let ((case-fold-search t))           ;Ignore case.
1691     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1692     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1693       (setq subject (substring subject (match-end 0))))
1694     ;; Remove uninteresting prefixes.
1695     (when (and (not re-only)
1696                gnus-simplify-ignored-prefixes
1697                (string-match gnus-simplify-ignored-prefixes subject))
1698       (setq subject (substring subject (match-end 0))))
1699     ;; Remove words in parentheses from end.
1700     (unless re-only
1701       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1702         (setq subject (substring subject 0 (match-beginning 0)))))
1703     ;; Return subject string.
1704     subject))
1705
1706 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1707 ;; all whitespace.
1708 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1709   (goto-char (point-min))
1710   (while (re-search-forward regexp nil t)
1711     (replace-match (or newtext ""))))
1712
1713 (defun gnus-simplify-buffer-fuzzy ()
1714   "Simplify string in the buffer fuzzily.
1715 The string in the accessible portion of the current buffer is simplified.
1716 It is assumed to be a single-line subject.
1717 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1718 matter is removed.  Additional things can be deleted by setting
1719 `gnus-simplify-subject-fuzzy-regexp'."
1720   (let ((case-fold-search t)
1721         (modified-tick))
1722     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1723
1724     (while (not (eq modified-tick (buffer-modified-tick)))
1725       (setq modified-tick (buffer-modified-tick))
1726       (cond
1727        ((listp gnus-simplify-subject-fuzzy-regexp)
1728         (mapc 'gnus-simplify-buffer-fuzzy-step
1729               gnus-simplify-subject-fuzzy-regexp))
1730        (gnus-simplify-subject-fuzzy-regexp
1731         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1732       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1733       (gnus-simplify-buffer-fuzzy-step
1734        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1735       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1736
1737     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1738     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1739     (gnus-simplify-buffer-fuzzy-step " $")
1740     (gnus-simplify-buffer-fuzzy-step "^ +")))
1741
1742 (defun gnus-simplify-subject-fuzzy (subject)
1743   "Simplify a subject string fuzzily.
1744 See `gnus-simplify-buffer-fuzzy' for details."
1745   (save-excursion
1746     (gnus-set-work-buffer)
1747     (let ((case-fold-search t))
1748       ;; Remove uninteresting prefixes.
1749       (when (and gnus-simplify-ignored-prefixes
1750                  (string-match gnus-simplify-ignored-prefixes subject))
1751         (setq subject (substring subject (match-end 0))))
1752       (insert subject)
1753       (inline (gnus-simplify-buffer-fuzzy))
1754       (buffer-string))))
1755
1756 (defsubst gnus-simplify-subject-fully (subject)
1757   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1758   (cond
1759    (gnus-simplify-subject-functions
1760     (gnus-map-function gnus-simplify-subject-functions subject))
1761    ((null gnus-summary-gather-subject-limit)
1762     (gnus-simplify-subject-re subject))
1763    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1764     (gnus-simplify-subject-fuzzy subject))
1765    ((numberp gnus-summary-gather-subject-limit)
1766     (truncate-string-to-width (gnus-simplify-subject-re subject)
1767                               gnus-summary-gather-subject-limit))
1768    (t
1769     subject)))
1770
1771 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1772   "Check whether two subjects are equal.
1773 If optional argument SIMPLE-FIRST is t, first argument is already
1774 simplified."
1775   (cond
1776    ((null simple-first)
1777     (equal (gnus-simplify-subject-fully s1)
1778            (gnus-simplify-subject-fully s2)))
1779    (t
1780     (equal s1
1781            (gnus-simplify-subject-fully s2)))))
1782
1783 (defun gnus-summary-bubble-group ()
1784   "Increase the score of the current group.
1785 This is a handy function to add to `gnus-summary-exit-hook' to
1786 increase the score of each group you read."
1787   (gnus-group-add-score gnus-newsgroup-name))
1788
1789 \f
1790 ;;;
1791 ;;; Gnus summary mode
1792 ;;;
1793
1794 (put 'gnus-summary-mode 'mode-class 'special)
1795
1796 (defvar gnus-article-commands-menu)
1797
1798 ;; Non-orthogonal keys
1799
1800 (gnus-define-keys gnus-summary-mode-map
1801   " " gnus-summary-next-page
1802   "\177" gnus-summary-prev-page
1803   [delete] gnus-summary-prev-page
1804   [backspace] gnus-summary-prev-page
1805   "\r" gnus-summary-scroll-up
1806   "\M-\r" gnus-summary-scroll-down
1807   "n" gnus-summary-next-unread-article
1808   "p" gnus-summary-prev-unread-article
1809   "N" gnus-summary-next-article
1810   "P" gnus-summary-prev-article
1811   "\M-\C-n" gnus-summary-next-same-subject
1812   "\M-\C-p" gnus-summary-prev-same-subject
1813   "\M-n" gnus-summary-next-unread-subject
1814   "\M-p" gnus-summary-prev-unread-subject
1815   "." gnus-summary-first-unread-article
1816   "," gnus-summary-best-unread-article
1817   "\M-s" gnus-summary-search-article-forward
1818   "\M-r" gnus-summary-search-article-backward
1819   "\M-S" gnus-summary-repeat-search-article-forward
1820   "\M-R" gnus-summary-repeat-search-article-backward
1821   "<" gnus-summary-beginning-of-article
1822   ">" gnus-summary-end-of-article
1823   "j" gnus-summary-goto-article
1824   "^" gnus-summary-refer-parent-article
1825   "\M-^" gnus-summary-refer-article
1826   "u" gnus-summary-tick-article-forward
1827   "!" gnus-summary-tick-article-forward
1828   "U" gnus-summary-tick-article-backward
1829   "d" gnus-summary-mark-as-read-forward
1830   "D" gnus-summary-mark-as-read-backward
1831   "E" gnus-summary-mark-as-expirable
1832   "\M-u" gnus-summary-clear-mark-forward
1833   "\M-U" gnus-summary-clear-mark-backward
1834   "k" gnus-summary-kill-same-subject-and-select
1835   "\C-k" gnus-summary-kill-same-subject
1836   "\M-\C-k" gnus-summary-kill-thread
1837   "\M-\C-l" gnus-summary-lower-thread
1838   "e" gnus-summary-edit-article
1839   "#" gnus-summary-mark-as-processable
1840   "\M-#" gnus-summary-unmark-as-processable
1841   "\M-\C-t" gnus-summary-toggle-threads
1842   "\M-\C-s" gnus-summary-show-thread
1843   "\M-\C-h" gnus-summary-hide-thread
1844   "\M-\C-f" gnus-summary-next-thread
1845   "\M-\C-b" gnus-summary-prev-thread
1846   [(meta down)] gnus-summary-next-thread
1847   [(meta up)] gnus-summary-prev-thread
1848   "\M-\C-u" gnus-summary-up-thread
1849   "\M-\C-d" gnus-summary-down-thread
1850   "&" gnus-summary-execute-command
1851   "c" gnus-summary-catchup-and-exit
1852   "\C-w" gnus-summary-mark-region-as-read
1853   "\C-t" gnus-summary-toggle-truncation
1854   "?" gnus-summary-mark-as-dormant
1855   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1856   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1857   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1858   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1859   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1860   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1861   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1862   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1863   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1864   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1865   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1866   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1867   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1868   "=" gnus-summary-expand-window
1869   "\C-x\C-s" gnus-summary-reselect-current-group
1870   "\M-g" gnus-summary-rescan-group
1871   "\C-c\C-r" gnus-summary-caesar-message
1872   "f" gnus-summary-followup
1873   "F" gnus-summary-followup-with-original
1874   "C" gnus-summary-cancel-article
1875   "r" gnus-summary-reply
1876   "R" gnus-summary-reply-with-original
1877   "\C-c\C-f" gnus-summary-mail-forward
1878   "o" gnus-summary-save-article
1879   "\C-o" gnus-summary-save-article-mail
1880   "|" gnus-summary-pipe-output
1881   "\M-k" gnus-summary-edit-local-kill
1882   "\M-K" gnus-summary-edit-global-kill
1883   ;; "V" gnus-version
1884   "\C-c\C-d" gnus-summary-describe-group
1885   "q" gnus-summary-exit
1886   "Q" gnus-summary-exit-no-update
1887   "\C-c\C-i" gnus-info-find-node
1888   gnus-mouse-2 gnus-mouse-pick-article
1889   [follow-link] mouse-face
1890   "m" gnus-summary-mail-other-window
1891   "a" gnus-summary-post-news
1892   "x" gnus-summary-limit-to-unread
1893   "s" gnus-summary-isearch-article
1894   "t" gnus-summary-toggle-header
1895   "g" gnus-summary-show-article
1896   "l" gnus-summary-goto-last-article
1897   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1898   "\C-d" gnus-summary-enter-digest-group
1899   "\M-\C-d" gnus-summary-read-document
1900   "\M-\C-e" gnus-summary-edit-parameters
1901   "\M-\C-a" gnus-summary-customize-parameters
1902   "\C-c\C-b" gnus-bug
1903   "*" gnus-cache-enter-article
1904   "\M-*" gnus-cache-remove-article
1905   "\M-&" gnus-summary-universal-argument
1906   "\C-l" gnus-recenter
1907   "I" gnus-summary-increase-score
1908   "L" gnus-summary-lower-score
1909   "\M-i" gnus-symbolic-argument
1910   "h" gnus-summary-select-article-buffer
1911
1912   "b" gnus-article-view-part
1913   "\M-t" gnus-summary-toggle-display-buttonized
1914
1915   "V" gnus-summary-score-map
1916   "X" gnus-uu-extract-map
1917   "S" gnus-summary-send-map)
1918
1919 ;; Sort of orthogonal keymap
1920 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1921   "t" gnus-summary-tick-article-forward
1922   "!" gnus-summary-tick-article-forward
1923   "d" gnus-summary-mark-as-read-forward
1924   "r" gnus-summary-mark-as-read-forward
1925   "c" gnus-summary-clear-mark-forward
1926   " " gnus-summary-clear-mark-forward
1927   "e" gnus-summary-mark-as-expirable
1928   "x" gnus-summary-mark-as-expirable
1929   "?" gnus-summary-mark-as-dormant
1930   "b" gnus-summary-set-bookmark
1931   "B" gnus-summary-remove-bookmark
1932   "#" gnus-summary-mark-as-processable
1933   "\M-#" gnus-summary-unmark-as-processable
1934   "S" gnus-summary-limit-include-expunged
1935   "C" gnus-summary-catchup
1936   "H" gnus-summary-catchup-to-here
1937   "h" gnus-summary-catchup-from-here
1938   "\C-c" gnus-summary-catchup-all
1939   "k" gnus-summary-kill-same-subject-and-select
1940   "K" gnus-summary-kill-same-subject
1941   "P" gnus-uu-mark-map)
1942
1943 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1944   "c" gnus-summary-clear-above
1945   "u" gnus-summary-tick-above
1946   "m" gnus-summary-mark-above
1947   "k" gnus-summary-kill-below)
1948
1949 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1950   "/" gnus-summary-limit-to-subject
1951   "n" gnus-summary-limit-to-articles
1952   "b" gnus-summary-limit-to-bodies
1953   "h" gnus-summary-limit-to-headers
1954   "w" gnus-summary-pop-limit
1955   "s" gnus-summary-limit-to-subject
1956   "a" gnus-summary-limit-to-author
1957   "u" gnus-summary-limit-to-unread
1958   "m" gnus-summary-limit-to-marks
1959   "M" gnus-summary-limit-exclude-marks
1960   "v" gnus-summary-limit-to-score
1961   "*" gnus-summary-limit-include-cached
1962   "D" gnus-summary-limit-include-dormant
1963   "T" gnus-summary-limit-include-thread
1964   "d" gnus-summary-limit-exclude-dormant
1965   "t" gnus-summary-limit-to-age
1966   "." gnus-summary-limit-to-unseen
1967   "x" gnus-summary-limit-to-extra
1968   "p" gnus-summary-limit-to-display-predicate
1969   "E" gnus-summary-limit-include-expunged
1970   "c" gnus-summary-limit-exclude-childless-dormant
1971   "C" gnus-summary-limit-mark-excluded-as-read
1972   "o" gnus-summary-insert-old-articles
1973   "N" gnus-summary-insert-new-articles
1974   "S" gnus-summary-limit-to-singletons
1975   "r" gnus-summary-limit-to-replied
1976   "R" gnus-summary-limit-to-recipient
1977   "A" gnus-summary-limit-to-address)
1978
1979 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1980   "n" gnus-summary-next-unread-article
1981   "p" gnus-summary-prev-unread-article
1982   "N" gnus-summary-next-article
1983   "P" gnus-summary-prev-article
1984   "\C-n" gnus-summary-next-same-subject
1985   "\C-p" gnus-summary-prev-same-subject
1986   "\M-n" gnus-summary-next-unread-subject
1987   "\M-p" gnus-summary-prev-unread-subject
1988   "f" gnus-summary-first-unread-article
1989   "b" gnus-summary-best-unread-article
1990   "j" gnus-summary-goto-article
1991   "g" gnus-summary-goto-subject
1992   "l" gnus-summary-goto-last-article
1993   "o" gnus-summary-pop-article)
1994
1995 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1996   "k" gnus-summary-kill-thread
1997   "E" gnus-summary-expire-thread
1998   "l" gnus-summary-lower-thread
1999   "i" gnus-summary-raise-thread
2000   "T" gnus-summary-toggle-threads
2001   "t" gnus-summary-rethread-current
2002   "^" gnus-summary-reparent-thread
2003   "\M-^" gnus-summary-reparent-children
2004   "s" gnus-summary-show-thread
2005   "S" gnus-summary-show-all-threads
2006   "h" gnus-summary-hide-thread
2007   "H" gnus-summary-hide-all-threads
2008   "n" gnus-summary-next-thread
2009   "p" gnus-summary-prev-thread
2010   "u" gnus-summary-up-thread
2011   "o" gnus-summary-top-thread
2012   "d" gnus-summary-down-thread
2013   "#" gnus-uu-mark-thread
2014   "\M-#" gnus-uu-unmark-thread)
2015
2016 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2017   "g" gnus-summary-prepare
2018   "c" gnus-summary-insert-cached-articles
2019   "d" gnus-summary-insert-dormant-articles
2020   "t" gnus-summary-insert-ticked-articles)
2021
2022 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2023   "c" gnus-summary-catchup-and-exit
2024   "C" gnus-summary-catchup-all-and-exit
2025   "E" gnus-summary-exit-no-update
2026   "Q" gnus-summary-exit
2027   "Z" gnus-summary-exit
2028   "n" gnus-summary-catchup-and-goto-next-group
2029   "p" gnus-summary-catchup-and-goto-prev-group
2030   "R" gnus-summary-reselect-current-group
2031   "G" gnus-summary-rescan-group
2032   "N" gnus-summary-next-group
2033   "s" gnus-summary-save-newsrc
2034   "P" gnus-summary-prev-group)
2035
2036 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2037   " " gnus-summary-next-page
2038   "n" gnus-summary-next-page
2039   "\177" gnus-summary-prev-page
2040   [delete] gnus-summary-prev-page
2041   "p" gnus-summary-prev-page
2042   "\r" gnus-summary-scroll-up
2043   "\M-\r" gnus-summary-scroll-down
2044   "<" gnus-summary-beginning-of-article
2045   ">" gnus-summary-end-of-article
2046   "b" gnus-summary-beginning-of-article
2047   "e" gnus-summary-end-of-article
2048   "^" gnus-summary-refer-parent-article
2049   "r" gnus-summary-refer-parent-article
2050   "C" gnus-summary-show-complete-article
2051   "D" gnus-summary-enter-digest-group
2052   "R" gnus-summary-refer-references
2053   "T" gnus-summary-refer-thread
2054   "g" gnus-summary-show-article
2055   "s" gnus-summary-isearch-article
2056   "P" gnus-summary-print-article
2057   "S" gnus-sticky-article
2058   "M" gnus-mailing-list-insinuate
2059   "t" gnus-article-babel)
2060
2061 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2062   "b" gnus-article-add-buttons
2063   "B" gnus-article-add-buttons-to-head
2064   "o" gnus-article-treat-overstrike
2065   "e" gnus-article-emphasize
2066   "w" gnus-article-fill-cited-article
2067   "Q" gnus-article-fill-long-lines
2068   "L" gnus-article-toggle-truncate-lines
2069   "C" gnus-article-capitalize-sentences
2070   "c" gnus-article-remove-cr
2071   "q" gnus-article-de-quoted-unreadable
2072   "6" gnus-article-de-base64-unreadable
2073   "Z" gnus-article-decode-HZ
2074   "A" gnus-article-treat-ansi-sequences
2075   "h" gnus-article-wash-html
2076   "u" gnus-article-unsplit-urls
2077   "s" gnus-summary-force-verify-and-decrypt
2078   "f" gnus-article-display-x-face
2079   "l" gnus-summary-stop-page-breaking
2080   "r" gnus-summary-caesar-message
2081   "m" gnus-summary-morse-message
2082   "t" gnus-summary-toggle-header
2083   "g" gnus-treat-smiley
2084   "v" gnus-summary-verbose-headers
2085   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2086   "p" gnus-article-verify-x-pgp-sig
2087   "d" gnus-article-treat-dumbquotes
2088   "i" gnus-summary-idna-message)
2089
2090 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2091   ;; mnemonic: deuglif*Y*
2092   "u" gnus-article-outlook-unwrap-lines
2093   "a" gnus-article-outlook-repair-attribution
2094   "c" gnus-article-outlook-rearrange-citation
2095   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2096
2097 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2098   "a" gnus-article-hide
2099   "h" gnus-article-hide-headers
2100   "b" gnus-article-hide-boring-headers
2101   "s" gnus-article-hide-signature
2102   "c" gnus-article-hide-citation
2103   "C" gnus-article-hide-citation-in-followups
2104   "l" gnus-article-hide-list-identifiers
2105   "B" gnus-article-strip-banner
2106   "P" gnus-article-hide-pem
2107   "\C-c" gnus-article-hide-citation-maybe)
2108
2109 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2110   "a" gnus-article-highlight
2111   "h" gnus-article-highlight-headers
2112   "c" gnus-article-highlight-citation
2113   "s" gnus-article-highlight-signature)
2114
2115 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2116   "f" gnus-article-treat-fold-headers
2117   "u" gnus-article-treat-unfold-headers
2118   "n" gnus-article-treat-fold-newsgroups)
2119
2120 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2121   "x" gnus-article-display-x-face
2122   "d" gnus-article-display-face
2123   "s" gnus-treat-smiley
2124   "D" gnus-article-remove-images
2125   "W" gnus-html-show-images
2126   "f" gnus-treat-from-picon
2127   "m" gnus-treat-mail-picon
2128   "n" gnus-treat-newsgroups-picon
2129   "g" gnus-treat-from-gravatar
2130   "h" gnus-treat-mail-gravatar)
2131
2132 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2133   "w" gnus-article-decode-mime-words
2134   "c" gnus-article-decode-charset
2135   "v" gnus-mime-view-all-parts
2136   "b" gnus-article-view-part)
2137
2138 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2139   "z" gnus-article-date-ut
2140   "u" gnus-article-date-ut
2141   "l" gnus-article-date-local
2142   "p" gnus-article-date-english
2143   "e" gnus-article-date-lapsed
2144   "o" gnus-article-date-original
2145   "i" gnus-article-date-iso8601
2146   "s" gnus-article-date-user)
2147
2148 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2149   "t" gnus-article-remove-trailing-blank-lines
2150   "l" gnus-article-strip-leading-blank-lines
2151   "m" gnus-article-strip-multiple-blank-lines
2152   "a" gnus-article-strip-blank-lines
2153   "A" gnus-article-strip-all-blank-lines
2154   "s" gnus-article-strip-leading-space
2155   "e" gnus-article-strip-trailing-space
2156   "w" gnus-article-remove-leading-whitespace)
2157
2158 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2159   "v" gnus-version
2160   "d" gnus-summary-describe-group
2161   "h" gnus-summary-describe-briefly
2162   "i" gnus-info-find-node
2163   "C" gnus-group-fetch-control)
2164
2165 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2166   "e" gnus-summary-expire-articles
2167   "\M-\C-e" gnus-summary-expire-articles-now
2168   "\177" gnus-summary-delete-article
2169   [delete] gnus-summary-delete-article
2170   [backspace] gnus-summary-delete-article
2171   "m" gnus-summary-move-article
2172   "r" gnus-summary-respool-article
2173   "w" gnus-summary-edit-article
2174   "c" gnus-summary-copy-article
2175   "B" gnus-summary-crosspost-article
2176   "q" gnus-summary-respool-query
2177   "t" gnus-summary-respool-trace
2178   "i" gnus-summary-import-article
2179   "I" gnus-summary-create-article
2180   "p" gnus-summary-article-posted-p)
2181
2182 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2183   "o" gnus-summary-save-article
2184   "m" gnus-summary-save-article-mail
2185   "F" gnus-summary-write-article-file
2186   "r" gnus-summary-save-article-rmail
2187   "f" gnus-summary-save-article-file
2188   "b" gnus-summary-save-article-body-file
2189   "B" gnus-summary-write-article-body-file
2190   "h" gnus-summary-save-article-folder
2191   "v" gnus-summary-save-article-vm
2192   "p" gnus-summary-pipe-output
2193   "P" gnus-summary-muttprint)
2194
2195 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2196   "b" gnus-summary-display-buttonized
2197   "m" gnus-summary-repair-multipart
2198   "v" gnus-article-view-part
2199   "o" gnus-article-save-part
2200   "O" gnus-article-save-part-and-strip
2201   "r" gnus-article-replace-part
2202   "d" gnus-article-delete-part
2203   "t" gnus-article-view-part-as-type
2204   "j" gnus-article-jump-to-part
2205   "c" gnus-article-copy-part
2206   "C" gnus-article-view-part-as-charset
2207   "e" gnus-article-view-part-externally
2208   "H" gnus-article-browse-html-article
2209   "E" gnus-article-encrypt-body
2210   "i" gnus-article-inline-part
2211   "|" gnus-article-pipe-part)
2212
2213 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2214   "p" gnus-summary-mark-as-processable
2215   "u" gnus-summary-unmark-as-processable
2216   "U" gnus-summary-unmark-all-processable
2217   "v" gnus-uu-mark-over
2218   "s" gnus-uu-mark-series
2219   "r" gnus-uu-mark-region
2220   "g" gnus-uu-unmark-region
2221   "R" gnus-uu-mark-by-regexp
2222   "G" gnus-uu-unmark-by-regexp
2223   "t" gnus-uu-mark-thread
2224   "T" gnus-uu-unmark-thread
2225   "a" gnus-uu-mark-all
2226   "b" gnus-uu-mark-buffer
2227   "S" gnus-uu-mark-sparse
2228   "k" gnus-summary-kill-process-mark
2229   "y" gnus-summary-yank-process-mark
2230   "w" gnus-summary-save-process-mark
2231   "i" gnus-uu-invert-processable)
2232
2233 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2234   ;;"x" gnus-uu-extract-any
2235   "m" gnus-summary-save-parts
2236   "u" gnus-uu-decode-uu
2237   "U" gnus-uu-decode-uu-and-save
2238   "s" gnus-uu-decode-unshar
2239   "S" gnus-uu-decode-unshar-and-save
2240   "o" gnus-uu-decode-save
2241   "O" gnus-uu-decode-save
2242   "b" gnus-uu-decode-binhex
2243   "B" gnus-uu-decode-binhex
2244   "Y" gnus-uu-decode-yenc
2245   "p" gnus-uu-decode-postscript
2246   "P" gnus-uu-decode-postscript-and-save)
2247
2248 (gnus-define-keys
2249     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2250   "u" gnus-uu-decode-uu-view
2251   "U" gnus-uu-decode-uu-and-save-view
2252   "s" gnus-uu-decode-unshar-view
2253   "S" gnus-uu-decode-unshar-and-save-view
2254   "o" gnus-uu-decode-save-view
2255   "O" gnus-uu-decode-save-view
2256   "b" gnus-uu-decode-binhex-view
2257   "B" gnus-uu-decode-binhex-view
2258   "p" gnus-uu-decode-postscript-view
2259   "P" gnus-uu-decode-postscript-and-save-view)
2260
2261 (defvar gnus-article-post-menu nil)
2262
2263 (defconst gnus-summary-menu-maxlen 20)
2264
2265 (defun gnus-summary-menu-split (menu)
2266   ;; If we have lots of elements, divide them into groups of 20
2267   ;; and make a pane (or submenu) for each one.
2268   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2269       (let ((menu menu) sublists next
2270             (i 1))
2271         (while menu
2272           ;; Pull off the next gnus-summary-menu-maxlen elements
2273           ;; and make them the next element of sublist.
2274           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2275           (if next
2276               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2277                       nil))
2278           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2279                                              (aref (car (last menu)) 0)) menu)
2280                                sublists))
2281           (setq i (1+ i))
2282           (setq menu next))
2283         (nreverse sublists))
2284     ;; Few elements--put them all in one pane.
2285     menu))
2286
2287 (defun gnus-summary-make-menu-bar ()
2288   (gnus-turn-off-edit-menu 'summary)
2289
2290   (unless (boundp 'gnus-summary-misc-menu)
2291
2292     (easy-menu-define
2293       gnus-summary-kill-menu gnus-summary-mode-map ""
2294       (cons
2295        "Score"
2296        (nconc
2297         (list
2298          ["Customize" gnus-score-customize t])
2299         (gnus-make-score-map 'increase)
2300         (gnus-make-score-map 'lower)
2301         '(("Mark"
2302            ["Kill below" gnus-summary-kill-below t]
2303            ["Mark above" gnus-summary-mark-above t]
2304            ["Tick above" gnus-summary-tick-above t]
2305            ["Clear above" gnus-summary-clear-above t])
2306           ["Current score" gnus-summary-current-score t]
2307           ["Set score" gnus-summary-set-score t]
2308           ["Switch current score file..." gnus-score-change-score-file t]
2309           ["Set mark below..." gnus-score-set-mark-below t]
2310           ["Set expunge below..." gnus-score-set-expunge-below t]
2311           ["Edit current score file" gnus-score-edit-current-scores t]
2312           ["Edit score file..." gnus-score-edit-file t]
2313           ["Trace score" gnus-score-find-trace t]
2314           ["Find words" gnus-score-find-favourite-words t]
2315           ["Rescore buffer" gnus-summary-rescore t]
2316           ["Increase score..." gnus-summary-increase-score t]
2317           ["Lower score..." gnus-summary-lower-score t]))))
2318
2319     ;; Define both the Article menu in the summary buffer and the
2320     ;; equivalent Commands menu in the article buffer here for
2321     ;; consistency.
2322     (let ((innards
2323            `(("Hide"
2324               ["All" gnus-article-hide t]
2325               ["Headers" gnus-article-hide-headers t]
2326               ["Signature" gnus-article-hide-signature t]
2327               ["Citation" gnus-article-hide-citation t]
2328               ["List identifiers" gnus-article-hide-list-identifiers t]
2329               ["Banner" gnus-article-strip-banner t]
2330               ["Boring headers" gnus-article-hide-boring-headers t])
2331              ("Highlight"
2332               ["All" gnus-article-highlight t]
2333               ["Headers" gnus-article-highlight-headers t]
2334               ["Signature" gnus-article-highlight-signature t]
2335               ["Citation" gnus-article-highlight-citation t])
2336              ("MIME"
2337               ["Words" gnus-article-decode-mime-words t]
2338               ["Charset" gnus-article-decode-charset t]
2339               ["QP" gnus-article-de-quoted-unreadable t]
2340               ["Base64" gnus-article-de-base64-unreadable t]
2341               ["View MIME buttons" gnus-summary-display-buttonized t]
2342               ["View all" gnus-mime-view-all-parts t]
2343               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2344               ["Encrypt body" gnus-article-encrypt-body
2345                :active (not (gnus-group-read-only-p))
2346                ,@(if (featurep 'xemacs) nil
2347                    '(:help "Encrypt the message body on disk"))]
2348               ["Extract all parts..." gnus-summary-save-parts t]
2349               ("Multipart"
2350                ["Repair multipart" gnus-summary-repair-multipart t]
2351                ["Pipe part..." gnus-article-pipe-part t]
2352                ["Inline part" gnus-article-inline-part t]
2353                ["View part as type..." gnus-article-view-part-as-type t]
2354                ["Encrypt body" gnus-article-encrypt-body
2355                 :active (not (gnus-group-read-only-p))
2356                ,@(if (featurep 'xemacs) nil
2357                    '(:help "Encrypt the message body on disk"))]
2358                ["View part externally" gnus-article-view-part-externally t]
2359                ["View HTML parts in browser" gnus-article-browse-html-article t]
2360                ["View part with charset..." gnus-article-view-part-as-charset t]
2361                ["Copy part" gnus-article-copy-part t]
2362                ["Save part..." gnus-article-save-part t]
2363                ["View part" gnus-article-view-part t]))
2364              ("Date"
2365               ["Local" gnus-article-date-local t]
2366               ["ISO8601" gnus-article-date-iso8601 t]
2367               ["UT" gnus-article-date-ut t]
2368               ["Original" gnus-article-date-original t]
2369               ["Lapsed" gnus-article-date-lapsed t]
2370               ["User-defined" gnus-article-date-user t])
2371              ("Display"
2372               ["Remove images" gnus-article-remove-images t]
2373               ["Toggle smiley" gnus-treat-smiley t]
2374               ["Show X-Face" gnus-article-display-x-face t]
2375               ["Show picons in From" gnus-treat-from-picon t]
2376               ["Show picons in mail headers" gnus-treat-mail-picon t]
2377               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2378               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2379               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2380               ("View as different encoding"
2381                ,@(gnus-summary-menu-split
2382                   (mapcar
2383                    (lambda (cs)
2384                      ;; Since easymenu under Emacs doesn't allow
2385                      ;; lambda forms for menu commands, we should
2386                      ;; provide intern'ed function symbols.
2387                      (let ((command (intern (format "\
2388 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2389                        (fset command
2390                              `(lambda ()
2391                                 (interactive)
2392                                 (let ((gnus-summary-show-article-charset-alist
2393                                        '((1 . ,cs))))
2394                                   (gnus-summary-show-article 1))))
2395                        `[,(symbol-name cs) ,command t]))
2396                    (sort (if (fboundp 'coding-system-list)
2397                              (coding-system-list)
2398                            (mapcar 'car mm-mime-mule-charset-alist))
2399                          'string<)))))
2400              ("Washing"
2401               ("Remove Blanks"
2402                ["Leading" gnus-article-strip-leading-blank-lines t]
2403                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2404                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2405                ["All of the above" gnus-article-strip-blank-lines t]
2406                ["All" gnus-article-strip-all-blank-lines t]
2407                ["Leading space" gnus-article-strip-leading-space t]
2408                ["Trailing space" gnus-article-strip-trailing-space t]
2409                ["Leading space in headers"
2410                 gnus-article-remove-leading-whitespace t])
2411               ["Overstrike" gnus-article-treat-overstrike t]
2412               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2413               ["Emphasis" gnus-article-emphasize t]
2414               ["Word wrap" gnus-article-fill-cited-article t]
2415               ["Fill long lines" gnus-article-fill-long-lines t]
2416               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2417               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2418               ["Remove CR" gnus-article-remove-cr t]
2419               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2420               ["Base64" gnus-article-de-base64-unreadable t]
2421               ["Rot 13" gnus-summary-caesar-message
2422                ,@(if (featurep 'xemacs) '(t)
2423                    '(:help "\"Caesar rotate\" article by 13"))]
2424               ["De-IDNA" gnus-summary-idna-message t]
2425               ["Morse decode" gnus-summary-morse-message t]
2426               ["Unix pipe..." gnus-summary-pipe-message t]
2427               ["Add buttons" gnus-article-add-buttons t]
2428               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2429               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2430               ["Verbose header" gnus-summary-verbose-headers t]
2431               ["Toggle header" gnus-summary-toggle-header t]
2432               ["Unfold headers" gnus-article-treat-unfold-headers t]
2433               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2434               ["Html" gnus-article-wash-html t]
2435               ["Unsplit URLs" gnus-article-unsplit-urls t]
2436               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2437               ["Decode HZ" gnus-article-decode-HZ t]
2438               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2439               ("(Outlook) Deuglify"
2440                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2441                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2442                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2443                ["Full (Outlook) deuglify"
2444                 gnus-article-outlook-deuglify-article t])
2445               )
2446              ("Output"
2447               ["Save in default format..." gnus-summary-save-article
2448                ,@(if (featurep 'xemacs) '(t)
2449                    '(:help "Save article using default method"))]
2450               ["Save in file..." gnus-summary-save-article-file
2451                ,@(if (featurep 'xemacs) '(t)
2452                    '(:help "Save article in file"))]
2453               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2454               ["Save in MH folder..." gnus-summary-save-article-folder t]
2455               ["Save in VM folder..." gnus-summary-save-article-vm t]
2456               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2457               ["Save body in file..." gnus-summary-save-article-body-file t]
2458               ["Pipe through a filter..." gnus-summary-pipe-output t]
2459               ["Print with Muttprint..." gnus-summary-muttprint t]
2460               ["Print" gnus-summary-print-article
2461                ,@(if (featurep 'xemacs) '(t)
2462                    '(:help "Generate and print a PostScript image"))])
2463              ("Copy, move,... (Backend)"
2464               ,@(if (featurep 'xemacs) nil
2465                   '(:help "Copying, moving, expiring articles..."))
2466               ["Respool article..." gnus-summary-respool-article t]
2467               ["Move article..." gnus-summary-move-article
2468                (gnus-check-backend-function
2469                 'request-move-article gnus-newsgroup-name)]
2470               ["Copy article..." gnus-summary-copy-article t]
2471               ["Crosspost article..." gnus-summary-crosspost-article
2472                (gnus-check-backend-function
2473                 'request-replace-article gnus-newsgroup-name)]
2474               ["Import file..." gnus-summary-import-article
2475                (gnus-check-backend-function
2476                 'request-accept-article gnus-newsgroup-name)]
2477               ["Create article..." gnus-summary-create-article
2478                (gnus-check-backend-function
2479                 'request-accept-article gnus-newsgroup-name)]
2480               ["Check if posted" gnus-summary-article-posted-p t]
2481               ["Edit article" gnus-summary-edit-article
2482                (not (gnus-group-read-only-p))]
2483               ["Delete article" gnus-summary-delete-article
2484                (gnus-check-backend-function
2485                 'request-expire-articles gnus-newsgroup-name)]
2486               ["Query respool" gnus-summary-respool-query t]
2487               ["Trace respool" gnus-summary-respool-trace t]
2488               ["Delete expirable articles" gnus-summary-expire-articles-now
2489                (gnus-check-backend-function
2490                 'request-expire-articles gnus-newsgroup-name)])
2491              ("Extract"
2492               ["Uudecode" gnus-uu-decode-uu
2493                ,@(if (featurep 'xemacs) '(t)
2494                    '(:help "Decode uuencoded article(s)"))]
2495               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2496               ["Unshar" gnus-uu-decode-unshar t]
2497               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2498               ["Save" gnus-uu-decode-save t]
2499               ["Binhex" gnus-uu-decode-binhex t]
2500               ["Postscript" gnus-uu-decode-postscript t]
2501               ["All MIME parts" gnus-summary-save-parts t])
2502              ("Cache"
2503               ["Enter article" gnus-cache-enter-article t]
2504               ["Remove article" gnus-cache-remove-article t])
2505              ["Translate" gnus-article-babel t]
2506              ["Select article buffer" gnus-summary-select-article-buffer t]
2507              ["Make article buffer sticky" gnus-sticky-article t]
2508              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2509              ["Isearch article..." gnus-summary-isearch-article t]
2510              ["Beginning of the article" gnus-summary-beginning-of-article t]
2511              ["End of the article" gnus-summary-end-of-article t]
2512              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2513              ["Fetch referenced articles" gnus-summary-refer-references t]
2514              ["Fetch current thread" gnus-summary-refer-thread t]
2515              ["Fetch article with id..." gnus-summary-refer-article t]
2516              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2517              ["Redisplay" gnus-summary-show-article t]
2518              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2519       (easy-menu-define
2520         gnus-summary-article-menu gnus-summary-mode-map ""
2521         (cons "Article" innards))
2522
2523       (if (not (keymapp gnus-summary-article-menu))
2524           (easy-menu-define
2525             gnus-article-commands-menu gnus-article-mode-map ""
2526             (cons "Commands" innards))
2527         ;; in Emacs, don't share menu.
2528         (setq gnus-article-commands-menu
2529               (copy-keymap gnus-summary-article-menu))
2530         (define-key gnus-article-mode-map [menu-bar commands]
2531           (cons "Commands" gnus-article-commands-menu))))
2532
2533     (easy-menu-define
2534       gnus-summary-thread-menu gnus-summary-mode-map ""
2535       '("Threads"
2536         ["Find all messages in thread" gnus-summary-refer-thread t]
2537         ["Toggle threading" gnus-summary-toggle-threads t]
2538         ["Hide threads" gnus-summary-hide-all-threads t]
2539         ["Show threads" gnus-summary-show-all-threads t]
2540         ["Hide thread" gnus-summary-hide-thread t]
2541         ["Show thread" gnus-summary-show-thread t]
2542         ["Go to next thread" gnus-summary-next-thread t]
2543         ["Go to previous thread" gnus-summary-prev-thread t]
2544         ["Go down thread" gnus-summary-down-thread t]
2545         ["Go up thread" gnus-summary-up-thread t]
2546         ["Top of thread" gnus-summary-top-thread t]
2547         ["Mark thread as read" gnus-summary-kill-thread t]
2548         ["Mark thread as expired" gnus-summary-expire-thread t]
2549         ["Lower thread score" gnus-summary-lower-thread t]
2550         ["Raise thread score" gnus-summary-raise-thread t]
2551         ["Rethread current" gnus-summary-rethread-current t]))
2552
2553     (easy-menu-define
2554       gnus-summary-post-menu gnus-summary-mode-map ""
2555       `("Post"
2556         ["Send a message (mail or news)" gnus-summary-post-news
2557          ,@(if (featurep 'xemacs) '(t)
2558              '(:help "Compose a new message (mail or news)"))]
2559         ["Followup" gnus-summary-followup
2560          ,@(if (featurep 'xemacs) '(t)
2561              '(:help "Post followup to this article"))]
2562         ["Followup and yank" gnus-summary-followup-with-original
2563          ,@(if (featurep 'xemacs) '(t)
2564              '(:help "Post followup to this article, quoting its contents"))]
2565         ["Supersede article" gnus-summary-supersede-article t]
2566         ["Cancel article" gnus-summary-cancel-article
2567          ,@(if (featurep 'xemacs) '(t)
2568              '(:help "Cancel an article you posted"))]
2569         ["Reply" gnus-summary-reply t]
2570         ["Reply and yank" gnus-summary-reply-with-original t]
2571         ["Wide reply" gnus-summary-wide-reply t]
2572         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2573          ,@(if (featurep 'xemacs) '(t)
2574              '(:help "Mail a reply, quoting this article"))]
2575         ["Very wide reply" gnus-summary-very-wide-reply t]
2576         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2577          ,@(if (featurep 'xemacs) '(t)
2578              '(:help "Mail a very wide reply, quoting this article"))]
2579         ["Mail forward" gnus-summary-mail-forward t]
2580         ["Post forward" gnus-summary-post-forward t]
2581         ["Digest and mail" gnus-uu-digest-mail-forward t]
2582         ["Digest and post" gnus-uu-digest-post-forward t]
2583         ["Resend message" gnus-summary-resend-message t]
2584         ["Resend message edit" gnus-summary-resend-message-edit t]
2585         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2586         ["Send a mail" gnus-summary-mail-other-window t]
2587         ["Create a local message" gnus-summary-news-other-window t]
2588         ["Uuencode and post" gnus-uu-post-news
2589          ,@(if (featurep 'xemacs) '(t)
2590              '(:help "Post a uuencoded article"))]
2591         ["Followup via news" gnus-summary-followup-to-mail t]
2592         ["Followup via news and yank"
2593          gnus-summary-followup-to-mail-with-original t]
2594         ["Strip signature on reply"
2595          (lambda ()
2596            (interactive)
2597            (if (not (memq message-cite-function
2598                           '(message-cite-original-without-signature
2599                             message-cite-original)))
2600                ;; Stupid workaround for XEmacs not honoring :visible.
2601                (message "Can't toggle this value of `message-cite-function'")
2602              (setq message-cite-function
2603                    (if (eq message-cite-function
2604                            'message-cite-original-without-signature)
2605                        'message-cite-original
2606                      'message-cite-original-without-signature))))
2607          ;; XEmacs barfs on :visible.
2608          ,@(if (featurep 'xemacs) nil
2609              '(:visible (memq message-cite-function
2610                               '(message-cite-original-without-signature
2611                                 message-cite-original))))
2612          :style toggle
2613          :selected (eq message-cite-function
2614                        'message-cite-original-without-signature)
2615          ,@(if (featurep 'xemacs) nil
2616              '(:help "Strip signature from cited article when replying."))]
2617         ;;("Draft"
2618         ;;["Send" gnus-summary-send-draft t]
2619         ;;["Send bounced" gnus-resend-bounced-mail t])
2620         ))
2621
2622     (cond
2623      ((not (keymapp gnus-summary-post-menu))
2624       (setq gnus-article-post-menu gnus-summary-post-menu))
2625      ((not gnus-article-post-menu)
2626       ;; Don't share post menu.
2627       (setq gnus-article-post-menu
2628             (copy-keymap gnus-summary-post-menu))))
2629     (define-key gnus-article-mode-map [menu-bar post]
2630       (cons "Post" gnus-article-post-menu))
2631
2632     (easy-menu-define
2633       gnus-summary-misc-menu gnus-summary-mode-map ""
2634       `("Gnus"
2635         ("Mark Read"
2636          ["Mark as read" gnus-summary-mark-as-read-forward t]
2637          ["Mark same subject and select"
2638           gnus-summary-kill-same-subject-and-select t]
2639          ["Mark same subject" gnus-summary-kill-same-subject t]
2640          ["Catchup" gnus-summary-catchup
2641           ,@(if (featurep 'xemacs) '(t)
2642               '(:help "Mark unread articles in this group as read"))]
2643          ["Catchup all" gnus-summary-catchup-all t]
2644          ["Catchup to here" gnus-summary-catchup-to-here t]
2645          ["Catchup from here" gnus-summary-catchup-from-here t]
2646          ["Catchup region" gnus-summary-mark-region-as-read
2647           (gnus-mark-active-p)]
2648          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2649         ("Mark Various"
2650          ["Tick" gnus-summary-tick-article-forward t]
2651          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2652          ["Remove marks" gnus-summary-clear-mark-forward t]
2653          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2654          ["Set bookmark" gnus-summary-set-bookmark t]
2655          ["Remove bookmark" gnus-summary-remove-bookmark t])
2656         ("Limit to"
2657          ["Marks..." gnus-summary-limit-to-marks t]
2658          ["Subject..." gnus-summary-limit-to-subject t]
2659          ["Author..." gnus-summary-limit-to-author t]
2660          ["Recipient..." gnus-summary-limit-to-recipient t]
2661          ["Address..." gnus-summary-limit-to-address t]
2662          ["Age..." gnus-summary-limit-to-age t]
2663          ["Extra..." gnus-summary-limit-to-extra t]
2664          ["Score..." gnus-summary-limit-to-score t]
2665          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2666          ["Unread" gnus-summary-limit-to-unread t]
2667          ["Unseen" gnus-summary-limit-to-unseen t]
2668          ["Singletons" gnus-summary-limit-to-singletons t]
2669          ["Replied" gnus-summary-limit-to-replied t]
2670          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2671          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2672          ["Pop limit" gnus-summary-pop-limit t]
2673          ["Show dormant" gnus-summary-limit-include-dormant t]
2674          ["Hide childless dormant"
2675           gnus-summary-limit-exclude-childless-dormant t]
2676          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2677          ["Hide marked" gnus-summary-limit-exclude-marks t]
2678          ["Show expunged" gnus-summary-limit-include-expunged t])
2679         ("Process Mark"
2680          ["Set mark" gnus-summary-mark-as-processable t]
2681          ["Remove mark" gnus-summary-unmark-as-processable t]
2682          ["Remove all marks" gnus-summary-unmark-all-processable t]
2683          ["Invert marks" gnus-uu-invert-processable t]
2684          ["Mark above" gnus-uu-mark-over t]
2685          ["Mark series" gnus-uu-mark-series t]
2686          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2687          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2688          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2689          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2690          ["Mark all" gnus-uu-mark-all t]
2691          ["Mark buffer" gnus-uu-mark-buffer t]
2692          ["Mark sparse" gnus-uu-mark-sparse t]
2693          ["Mark thread" gnus-uu-mark-thread t]
2694          ["Unmark thread" gnus-uu-unmark-thread t]
2695          ("Process Mark Sets"
2696           ["Kill" gnus-summary-kill-process-mark t]
2697           ["Yank" gnus-summary-yank-process-mark
2698            gnus-newsgroup-process-stack]
2699           ["Save" gnus-summary-save-process-mark t]
2700           ["Run command on marked..." gnus-summary-universal-argument t]))
2701         ("Registry Marks")
2702         ("Scroll article"
2703          ["Page forward" gnus-summary-next-page
2704           ,@(if (featurep 'xemacs) '(t)
2705               '(:help "Show next page of article"))]
2706          ["Page backward" gnus-summary-prev-page
2707           ,@(if (featurep 'xemacs) '(t)
2708               '(:help "Show previous page of article"))]
2709          ["Line forward" gnus-summary-scroll-up t])
2710         ("Move"
2711          ["Next unread article" gnus-summary-next-unread-article t]
2712          ["Previous unread article" gnus-summary-prev-unread-article t]
2713          ["Next article" gnus-summary-next-article t]
2714          ["Previous article" gnus-summary-prev-article t]
2715          ["Next unread subject" gnus-summary-next-unread-subject t]
2716          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2717          ["Next article same subject" gnus-summary-next-same-subject t]
2718          ["Previous article same subject" gnus-summary-prev-same-subject t]
2719          ["First unread article" gnus-summary-first-unread-article t]
2720          ["Best unread article" gnus-summary-best-unread-article t]
2721          ["Go to subject number..." gnus-summary-goto-subject t]
2722          ["Go to article number..." gnus-summary-goto-article t]
2723          ["Go to the last article" gnus-summary-goto-last-article t]
2724          ["Pop article off history" gnus-summary-pop-article t])
2725         ("Sort"
2726          ["Sort by number" gnus-summary-sort-by-number t]
2727          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2728          ["Sort by author" gnus-summary-sort-by-author t]
2729          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2730          ["Sort by subject" gnus-summary-sort-by-subject t]
2731          ["Sort by date" gnus-summary-sort-by-date t]
2732          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2733          ["Sort by score" gnus-summary-sort-by-score t]
2734          ["Sort by lines" gnus-summary-sort-by-lines t]
2735          ["Sort by characters" gnus-summary-sort-by-chars t]
2736          ["Randomize" gnus-summary-sort-by-random t]
2737          ["Original sort" gnus-summary-sort-by-original t])
2738         ("Help"
2739          ["Describe group" gnus-summary-describe-group t]
2740          ["Fetch control message" gnus-group-fetch-control
2741           ,@(if (featurep 'xemacs) nil
2742               '(:help "Display the archived control message for the current group"))]
2743          ["Read manual" gnus-info-find-node t])
2744         ("Modes"
2745          ["Pick and read" gnus-pick-mode t]
2746          ["Binary" gnus-binary-mode t])
2747         ("Regeneration"
2748          ["Regenerate" gnus-summary-prepare t]
2749          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2750          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2751          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2752          ["Toggle threading" gnus-summary-toggle-threads t])
2753         ["See old articles" gnus-summary-insert-old-articles t]
2754         ["See new articles" gnus-summary-insert-new-articles t]
2755         ["Filter articles..." gnus-summary-execute-command t]
2756         ["Run command on articles..." gnus-summary-universal-argument t]
2757         ["Search articles forward..." gnus-summary-search-article-forward t]
2758         ["Search articles backward..." gnus-summary-search-article-backward t]
2759         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2760         ["Expand window" gnus-summary-expand-window t]
2761         ["Expire expirable articles" gnus-summary-expire-articles
2762          (gnus-check-backend-function
2763           'request-expire-articles gnus-newsgroup-name)]
2764         ["Edit local kill file" gnus-summary-edit-local-kill t]
2765         ["Edit main kill file" gnus-summary-edit-global-kill t]
2766         ["Edit group parameters" gnus-summary-edit-parameters t]
2767         ["Customize group parameters" gnus-summary-customize-parameters t]
2768         ["Send a bug report" gnus-bug t]
2769         ("Exit"
2770          ["Catchup and exit" gnus-summary-catchup-and-exit
2771           ,@(if (featurep 'xemacs) '(t)
2772               '(:help "Mark unread articles in this group as read, then exit"))]
2773          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2774          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2775          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2776          ["Exit group" gnus-summary-exit
2777           ,@(if (featurep 'xemacs) '(t)
2778               '(:help "Exit current group, return to group selection mode"))]
2779          ["Exit group without updating" gnus-summary-exit-no-update t]
2780          ["Exit and goto next group" gnus-summary-next-group t]
2781          ["Exit and goto prev group" gnus-summary-prev-group t]
2782          ["Reselect group" gnus-summary-reselect-current-group t]
2783          ["Rescan group" gnus-summary-rescan-group t]
2784          ["Update dribble" gnus-summary-save-newsrc t])))
2785
2786     (gnus-run-hooks 'gnus-summary-menu-hook)))
2787
2788 (defvar gnus-summary-tool-bar-map nil)
2789
2790 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2791 ;; affect _new_ message buffers.  We might add a function that walks thru all
2792 ;; summary-mode buffers and force the update.
2793 (defun gnus-summary-tool-bar-update (&optional symbol value)
2794   "Update summary mode toolbar.
2795 Setter function for custom variables."
2796   (setq-default gnus-summary-tool-bar-map nil)
2797   (when symbol
2798     ;; When used as ":set" function:
2799     (set-default symbol value))
2800   (when (gnus-buffer-live-p gnus-summary-buffer)
2801     (with-current-buffer gnus-summary-buffer
2802       (gnus-summary-make-tool-bar))))
2803
2804 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2805                                      'gnus-summary-tool-bar-gnome
2806                                    'gnus-summary-tool-bar-retro)
2807   "Specifies the Gnus summary tool bar.
2808
2809 It can be either a list or a symbol refering to a list.  See
2810 `gmm-tool-bar-from-list' for the format of the list.  The
2811 default key map is `gnus-summary-mode-map'.
2812
2813 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2814 `gnus-summary-tool-bar-retro'."
2815   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2816                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2817                  (repeat :tag "User defined list" gmm-tool-bar-item)
2818                  (symbol))
2819   :version "23.1" ;; No Gnus
2820   :initialize 'custom-initialize-default
2821   :set 'gnus-summary-tool-bar-update
2822   :group 'gnus-summary)
2823
2824 (defcustom gnus-summary-tool-bar-gnome
2825   '((gnus-summary-post-news "mail/compose" nil)
2826     (gnus-summary-insert-new-articles "mail/inbox" nil
2827                                       :visible (or (not gnus-agent)
2828                                                    gnus-plugged))
2829     (gnus-summary-reply-with-original "mail/reply")
2830     (gnus-summary-reply "mail/reply" nil :visible nil)
2831     (gnus-summary-followup-with-original "mail/reply-all")
2832     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2833     (gnus-summary-mail-forward "mail/forward")
2834     (gnus-summary-save-article "mail/save")
2835     (gnus-summary-search-article-forward "search" nil :visible nil)
2836     (gnus-summary-print-article "print")
2837     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2838     ;; Some new commands that may need more suitable icons:
2839     (gnus-summary-save-newsrc "save" nil :visible nil)
2840     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2841     (gnus-summary-prev-article "left-arrow")
2842     (gnus-summary-next-article "right-arrow")
2843     (gnus-summary-next-page "next-page")
2844     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2845     ;;
2846     ;; Maybe some sort-by-... could be added:
2847     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2848     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2849     (gnus-summary-mark-as-expirable
2850      "delete" nil
2851      :visible (gnus-check-backend-function 'request-expire-articles
2852                                            gnus-newsgroup-name))
2853     (gnus-summary-mark-as-spam
2854      "mail/spam" t
2855      :visible (and (fboundp 'spam-group-ham-contents-p)
2856                    (spam-group-ham-contents-p gnus-newsgroup-name))
2857      :help "Mark as spam")
2858     (gnus-summary-mark-as-read-forward
2859      "mail/not-spam" nil
2860      :visible (and (fboundp 'spam-group-spam-contents-p)
2861                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2862     ;;
2863     (gnus-summary-exit "exit")
2864     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2865     (gnus-info-find-node "help"))
2866   "List of functions for the summary tool bar (GNOME style).
2867
2868 See `gmm-tool-bar-from-list' for the format of the list."
2869   :type '(repeat gmm-tool-bar-item)
2870   :version "23.1" ;; No Gnus
2871   :initialize 'custom-initialize-default
2872   :set 'gnus-summary-tool-bar-update
2873   :group 'gnus-summary)
2874
2875 (defcustom gnus-summary-tool-bar-retro
2876   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2877     (gnus-summary-next-unread-article "gnus/next-ur")
2878     (gnus-summary-post-news "gnus/post")
2879     (gnus-summary-followup-with-original "gnus/fuwo")
2880     (gnus-summary-followup "gnus/followup")
2881     (gnus-summary-reply-with-original "gnus/reply-wo")
2882     (gnus-summary-reply "gnus/reply")
2883     (gnus-summary-caesar-message "gnus/rot13")
2884     (gnus-uu-decode-uu "gnus/uu-decode")
2885     (gnus-summary-save-article-file "gnus/save-aif")
2886     (gnus-summary-save-article "gnus/save-art")
2887     (gnus-uu-post-news "gnus/uu-post")
2888     (gnus-summary-catchup "gnus/catchup")
2889     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2890     (gnus-summary-exit "gnus/exit-summ")
2891     ;; Some new command that may need more suitable icons:
2892     (gnus-summary-print-article "gnus/print" nil :visible nil)
2893     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2894     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2895     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2896     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2897     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2898     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2899     ;;
2900     (gnus-info-find-node "gnus/help" nil :visible nil))
2901   "List of functions for the summary tool bar (retro look).
2902
2903 See `gmm-tool-bar-from-list' for the format of the list."
2904   :type '(repeat gmm-tool-bar-item)
2905   :version "23.1" ;; No Gnus
2906   :initialize 'custom-initialize-default
2907   :set 'gnus-summary-tool-bar-update
2908   :group 'gnus-summary)
2909
2910 (defcustom gnus-summary-tool-bar-zap-list t
2911   "List of icon items from the global tool bar.
2912 These items are not displayed in the Gnus summary mode tool bar.
2913
2914 See `gmm-tool-bar-from-list' for the format of the list."
2915   :type 'gmm-tool-bar-zap-list
2916   :version "23.1" ;; No Gnus
2917   :initialize 'custom-initialize-default
2918   :set 'gnus-summary-tool-bar-update
2919   :group 'gnus-summary)
2920
2921 (defvar image-load-path)
2922 (defvar tool-bar-map)
2923
2924 (defun gnus-summary-make-tool-bar (&optional force)
2925   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2926 When FORCE, rebuild the tool bar."
2927   (when (and (not (featurep 'xemacs))
2928              (boundp 'tool-bar-mode)
2929              tool-bar-mode
2930              (or (not gnus-summary-tool-bar-map) force))
2931     (let* ((load-path
2932             (gmm-image-load-path-for-library "gnus"
2933                                              "mail/save.xpm"
2934                                              nil t))
2935            (image-load-path (cons (car load-path)
2936                                   (when (boundp 'image-load-path)
2937                                     image-load-path)))
2938            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2939                                         gnus-summary-tool-bar-zap-list
2940                                         'gnus-summary-mode-map)))
2941       (when map
2942         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2943         ;; uses it's value.
2944         (setq gnus-summary-tool-bar-map map))))
2945   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2946
2947 (defun gnus-score-set-default (var value)
2948   "A version of set that updates the GNU Emacs menu-bar."
2949   (set var value)
2950   ;; It is the message that forces the active status to be updated.
2951   (message ""))
2952
2953 (defun gnus-make-score-map (type)
2954   "Make a summary score map of type TYPE."
2955   (if t
2956       nil
2957     (let ((headers '(("author" "from" string)
2958                      ("subject" "subject" string)
2959                      ("article body" "body" string)
2960                      ("article head" "head" string)
2961                      ("xref" "xref" string)
2962                      ("extra header" "extra" string)
2963                      ("lines" "lines" number)
2964                      ("followups to author" "followup" string)))
2965           (types '((number ("less than" <)
2966                            ("greater than" >)
2967                            ("equal" =))
2968                    (string ("substring" s)
2969                            ("exact string" e)
2970                            ("fuzzy string" f)
2971                            ("regexp" r))))
2972           (perms '(("temporary" (current-time-string))
2973                    ("permanent" nil)
2974                    ("immediate" now)))
2975           header)
2976       (list
2977        (apply
2978         'nconc
2979         (list
2980          (if (eq type 'lower)
2981              "Lower score"
2982            "Increase score"))
2983         (let (outh)
2984           (while headers
2985             (setq header (car headers))
2986             (setq outh
2987                   (cons
2988                    (apply
2989                     'nconc
2990                     (list (car header))
2991                     (let ((ts (cdr (assoc (nth 2 header) types)))
2992                           outt)
2993                       (while ts
2994                         (setq outt
2995                               (cons
2996                                (apply
2997                                 'nconc
2998                                 (list (caar ts))
2999                                 (let ((ps perms)
3000                                       outp)
3001                                   (while ps
3002                                     (setq outp
3003                                           (cons
3004                                            (vector
3005                                             (caar ps)
3006                                             (list
3007                                              'gnus-summary-score-entry
3008                                              (nth 1 header)
3009                                              (if (or (string= (nth 1 header)
3010                                                               "head")
3011                                                      (string= (nth 1 header)
3012                                                               "body"))
3013                                                  ""
3014                                                (list 'gnus-summary-header
3015                                                      (nth 1 header)))
3016                                              (list 'quote (nth 1 (car ts)))
3017                                              (list 'gnus-score-delta-default
3018                                                    nil)
3019                                              (nth 1 (car ps))
3020                                              t)
3021                                             t)
3022                                            outp))
3023                                     (setq ps (cdr ps)))
3024                                   (list (nreverse outp))))
3025                                outt))
3026                         (setq ts (cdr ts)))
3027                       (list (nreverse outt))))
3028                    outh))
3029             (setq headers (cdr headers)))
3030           (list (nreverse outh))))))))
3031
3032
3033 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3034 (defvar bookmark-make-record-function)
3035 \f
3036
3037 (defun gnus-summary-mode (&optional group)
3038   "Major mode for reading articles.
3039
3040 All normal editing commands are switched off.
3041 \\<gnus-summary-mode-map>
3042 Each line in this buffer represents one article.  To read an
3043 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3044 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3045 respectively.
3046
3047 You can also post articles and send mail from this buffer.  To
3048 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3049 of an article, type `\\[gnus-summary-reply]'.
3050
3051 There are approx. one gazillion commands you can execute in this
3052 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3053
3054 The following commands are available:
3055
3056 \\{gnus-summary-mode-map}"
3057   (interactive)
3058   (kill-all-local-variables)
3059   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3060     (gnus-summary-make-local-variables))
3061   (gnus-summary-make-local-variables)
3062   (setq gnus-newsgroup-name group)
3063   (when (gnus-visual-p 'summary-menu 'menu)
3064     (gnus-summary-make-menu-bar)
3065     (gnus-summary-make-tool-bar))
3066   (gnus-make-thread-indent-array)
3067   (gnus-simplify-mode-line)
3068   (setq major-mode 'gnus-summary-mode)
3069   (setq mode-name "Summary")
3070   (use-local-map gnus-summary-mode-map)
3071   (buffer-disable-undo)
3072   (setq buffer-read-only t              ;Disable modification
3073         show-trailing-whitespace nil)
3074   (setq truncate-lines t)
3075   (add-to-invisibility-spec '(gnus-sum . t))
3076   (gnus-summary-set-display-table)
3077   (gnus-set-default-directory)
3078   (make-local-variable 'gnus-summary-line-format)
3079   (make-local-variable 'gnus-summary-line-format-spec)
3080   (make-local-variable 'gnus-summary-dummy-line-format)
3081   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3082   (make-local-variable 'gnus-summary-mark-positions)
3083   (gnus-make-local-hook 'pre-command-hook)
3084   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3085   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3086   (turn-on-gnus-mailing-list-mode)
3087   (mm-enable-multibyte)
3088   (set (make-local-variable 'bookmark-make-record-function)
3089        'gnus-summary-bookmark-make-record)
3090   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3091   (gnus-update-summary-mark-positions))
3092
3093 (defun gnus-summary-make-local-variables ()
3094   "Make all the local summary buffer variables."
3095   (let (global)
3096     (dolist (local gnus-summary-local-variables)
3097       (if (consp local)
3098           (progn
3099             (if (eq (cdr local) 'global)
3100                 ;; Copy the global value of the variable.
3101                 (setq global (symbol-value (car local)))
3102               ;; Use the value from the list.
3103               (setq global (eval (cdr local))))
3104             (set (make-local-variable (car local)) global))
3105         ;; Simple nil-valued local variable.
3106         (set (make-local-variable local) nil)))))
3107
3108 (defun gnus-summary-clear-local-variables ()
3109   (let ((locals gnus-summary-local-variables))
3110     (while locals
3111       (if (consp (car locals))
3112           (and (symbolp (caar locals))
3113                (set (caar locals) nil))
3114         (and (symbolp (car locals))
3115              (set (car locals) nil)))
3116       (setq locals (cdr locals)))))
3117
3118 ;; Summary data functions.
3119
3120 (defmacro gnus-data-number (data)
3121   `(car ,data))
3122
3123 (defmacro gnus-data-set-number (data number)
3124   `(setcar ,data ,number))
3125
3126 (defmacro gnus-data-mark (data)
3127   `(nth 1 ,data))
3128
3129 (defmacro gnus-data-set-mark (data mark)
3130   `(setcar (nthcdr 1 ,data) ,mark))
3131
3132 (defmacro gnus-data-pos (data)
3133   `(nth 2 ,data))
3134
3135 (defmacro gnus-data-set-pos (data pos)
3136   `(setcar (nthcdr 2 ,data) ,pos))
3137
3138 (defmacro gnus-data-header (data)
3139   `(nth 3 ,data))
3140
3141 (defmacro gnus-data-set-header (data header)
3142   `(setf (nth 3 ,data) ,header))
3143
3144 (defmacro gnus-data-level (data)
3145   `(nth 4 ,data))
3146
3147 (defmacro gnus-data-unread-p (data)
3148   `(= (nth 1 ,data) gnus-unread-mark))
3149
3150 (defmacro gnus-data-read-p (data)
3151   `(/= (nth 1 ,data) gnus-unread-mark))
3152
3153 (defmacro gnus-data-pseudo-p (data)
3154   `(consp (nth 3 ,data)))
3155
3156 (defmacro gnus-data-find (number)
3157   `(assq ,number gnus-newsgroup-data))
3158
3159 (defmacro gnus-data-find-list (number &optional data)
3160   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3161      (memq (assq ,number bdata)
3162            bdata)))
3163
3164 (defmacro gnus-data-make (number mark pos header level)
3165   `(list ,number ,mark ,pos ,header ,level))
3166
3167 (defun gnus-data-enter (after-article number mark pos header level offset)
3168   (let ((data (gnus-data-find-list after-article)))
3169     (unless data
3170       (error "No such article: %d" after-article))
3171     (setcdr data (cons (gnus-data-make number mark pos header level)
3172                        (cdr data)))
3173     (setq gnus-newsgroup-data-reverse nil)
3174     (gnus-data-update-list (cddr data) offset)))
3175
3176 (defun gnus-data-enter-list (after-article list &optional offset)
3177   (when list
3178     (let ((data (and after-article (gnus-data-find-list after-article)))
3179           (ilist list))
3180       (if (not (or data
3181                    after-article))
3182           (let ((odata gnus-newsgroup-data))
3183             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3184             (when offset
3185               (gnus-data-update-list odata offset)))
3186         ;; Find the last element in the list to be spliced into the main
3187         ;; list.
3188         (setq list (last list))
3189         (if (not data)
3190             (progn
3191               (setcdr list gnus-newsgroup-data)
3192               (setq gnus-newsgroup-data ilist)
3193               (when offset
3194                 (gnus-data-update-list (cdr list) offset)))
3195           (setcdr list (cdr data))
3196           (setcdr data ilist)
3197           (when offset
3198             (gnus-data-update-list (cdr list) offset))))
3199       (setq gnus-newsgroup-data-reverse nil))))
3200
3201 (defun gnus-data-remove (article &optional offset)
3202   (let ((data gnus-newsgroup-data))
3203     (if (= (gnus-data-number (car data)) article)
3204         (progn
3205           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3206                 gnus-newsgroup-data-reverse nil)
3207           (when offset
3208             (gnus-data-update-list gnus-newsgroup-data offset)))
3209       (while (cdr data)
3210         (when (= (gnus-data-number (cadr data)) article)
3211           (setcdr data (cddr data))
3212           (when offset
3213             (gnus-data-update-list (cdr data) offset))
3214           (setq data nil
3215                 gnus-newsgroup-data-reverse nil))
3216         (setq data (cdr data))))))
3217
3218 (defmacro gnus-data-list (backward)
3219   `(if ,backward
3220        (or gnus-newsgroup-data-reverse
3221            (setq gnus-newsgroup-data-reverse
3222                  (reverse gnus-newsgroup-data)))
3223      gnus-newsgroup-data))
3224
3225 (defun gnus-data-update-list (data offset)
3226   "Add OFFSET to the POS of all data entries in DATA."
3227   (setq gnus-newsgroup-data-reverse nil)
3228   (while data
3229     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3230     (setq data (cdr data))))
3231
3232 (defun gnus-summary-article-pseudo-p (article)
3233   "Say whether this article is a pseudo article or not."
3234   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3235
3236 (defmacro gnus-summary-article-sparse-p (article)
3237   "Say whether this article is a sparse article or not."
3238   `(memq ,article gnus-newsgroup-sparse))
3239
3240 (defmacro gnus-summary-article-ancient-p (article)
3241   "Say whether this article is a sparse article or not."
3242   `(memq ,article gnus-newsgroup-ancient))
3243
3244 (defun gnus-article-parent-p (number)
3245   "Say whether this article is a parent or not."
3246   (let ((data (gnus-data-find-list number)))
3247     (and (cdr data)              ; There has to be an article after...
3248          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3249             (gnus-data-level (nth 1 data))))))
3250
3251 (defun gnus-article-children (number)
3252   "Return a list of all children to NUMBER."
3253   (let* ((data (gnus-data-find-list number))
3254          (level (gnus-data-level (car data)))
3255          children)
3256     (setq data (cdr data))
3257     (while (and data
3258                 (= (gnus-data-level (car data)) (1+ level)))
3259       (push (gnus-data-number (car data)) children)
3260       (setq data (cdr data)))
3261     children))
3262
3263 (defmacro gnus-summary-skip-intangible ()
3264   "If the current article is intangible, then jump to a different article."
3265   '(let ((to (get-text-property (point) 'gnus-intangible)))
3266      (and to (gnus-summary-goto-subject to))))
3267
3268 (defmacro gnus-summary-article-intangible-p ()
3269   "Say whether this article is intangible or not."
3270   '(get-text-property (point) 'gnus-intangible))
3271
3272 (defun gnus-article-read-p (article)
3273   "Say whether ARTICLE is read or not."
3274   (not (or (memq article gnus-newsgroup-marked)
3275            (memq article gnus-newsgroup-spam-marked)
3276            (memq article gnus-newsgroup-unreads)
3277            (memq article gnus-newsgroup-unselected)
3278            (memq article gnus-newsgroup-dormant))))
3279
3280 ;; Some summary mode macros.
3281
3282 (defmacro gnus-summary-article-number ()
3283   "The article number of the article on the current line.
3284 If there isn't an article number here, then we return the current
3285 article number."
3286   '(progn
3287      (gnus-summary-skip-intangible)
3288      (or (get-text-property (point) 'gnus-number)
3289          (gnus-summary-last-subject))))
3290
3291 (defmacro gnus-summary-article-header (&optional number)
3292   "Return the header of article NUMBER."
3293   `(gnus-data-header (gnus-data-find
3294                       ,(or number '(gnus-summary-article-number)))))
3295
3296 (defmacro gnus-summary-thread-level (&optional number)
3297   "Return the level of thread that starts with article NUMBER."
3298   `(if (and (eq gnus-summary-make-false-root 'dummy)
3299             (get-text-property (point) 'gnus-intangible))
3300        0
3301      (gnus-data-level (gnus-data-find
3302                        ,(or number '(gnus-summary-article-number))))))
3303
3304 (defmacro gnus-summary-article-mark (&optional number)
3305   "Return the mark of article NUMBER."
3306   `(gnus-data-mark (gnus-data-find
3307                     ,(or number '(gnus-summary-article-number)))))
3308
3309 (defmacro gnus-summary-article-pos (&optional number)
3310   "Return the position of the line of article NUMBER."
3311   `(gnus-data-pos (gnus-data-find
3312                    ,(or number '(gnus-summary-article-number)))))
3313
3314 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3315 (defmacro gnus-summary-article-subject (&optional number)
3316   "Return current subject string or nil if nothing."
3317   `(let ((headers
3318           ,(if number
3319                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3320              '(gnus-data-header (assq (gnus-summary-article-number)
3321                                       gnus-newsgroup-data)))))
3322      (and headers
3323           (vectorp headers)
3324           (mail-header-subject headers))))
3325
3326 (defmacro gnus-summary-article-score (&optional number)
3327   "Return current article score."
3328   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3329                   gnus-newsgroup-scored))
3330        gnus-summary-default-score 0))
3331
3332 (defun gnus-summary-article-children (&optional number)
3333   "Return a list of article numbers that are children of article NUMBER."
3334   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3335          (level (gnus-data-level (car data)))
3336          l children)
3337     (while (and (setq data (cdr data))
3338                 (> (setq l (gnus-data-level (car data))) level))
3339       (and (= (1+ level) l)
3340            (push (gnus-data-number (car data))
3341                  children)))
3342     (nreverse children)))
3343
3344 (defun gnus-summary-article-parent (&optional number)
3345   "Return the article number of the parent of article NUMBER."
3346   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3347                                     (gnus-data-list t)))
3348          (level (gnus-data-level (car data))))
3349     (if (zerop level)
3350         ()                              ; This is a root.
3351       ;; We search until we find an article with a level less than
3352       ;; this one.  That function has to be the parent.
3353       (while (and (setq data (cdr data))
3354                   (not (< (gnus-data-level (car data)) level))))
3355       (and data (gnus-data-number (car data))))))
3356
3357 (defun gnus-unread-mark-p (mark)
3358   "Say whether MARK is the unread mark."
3359   (= mark gnus-unread-mark))
3360
3361 (defun gnus-read-mark-p (mark)
3362   "Say whether MARK is one of the marks that mark as read.
3363 This is all marks except unread, ticked, dormant, and expirable."
3364   (not (or (= mark gnus-unread-mark)
3365            (= mark gnus-ticked-mark)
3366            (= mark gnus-spam-mark)
3367            (= mark gnus-dormant-mark)
3368            (= mark gnus-expirable-mark))))
3369
3370 (defmacro gnus-article-mark (number)
3371   "Return the MARK of article NUMBER.
3372 This macro should only be used when computing the mark the \"first\"
3373 time; i.e., when generating the summary lines.  After that,
3374 `gnus-summary-article-mark' should be used to examine the
3375 marks of articles."
3376   `(cond
3377     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3378     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3379     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3380     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3381     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3382     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3383     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3384     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3385            gnus-ancient-mark))))
3386
3387 ;; Saving hidden threads.
3388
3389 (defmacro gnus-save-hidden-threads (&rest forms)
3390   "Save hidden threads, eval FORMS, and restore the hidden threads."
3391   (let ((config (make-symbol "config")))
3392     `(let ((,config (gnus-hidden-threads-configuration)))
3393        (unwind-protect
3394            (save-excursion
3395              ,@forms)
3396          (gnus-restore-hidden-threads-configuration ,config)))))
3397 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3398 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3399
3400 (defun gnus-data-compute-positions ()
3401   "Compute the positions of all articles."
3402   (setq gnus-newsgroup-data-reverse nil)
3403   (let ((data gnus-newsgroup-data))
3404     (save-excursion
3405       (gnus-save-hidden-threads
3406         (gnus-summary-show-all-threads)
3407         (goto-char (point-min))
3408         (while data
3409           (while (get-text-property (point) 'gnus-intangible)
3410             (forward-line 1))
3411           (gnus-data-set-pos (car data) (+ (point) 3))
3412           (setq data (cdr data))
3413           (forward-line 1))))))
3414
3415 (defun gnus-hidden-threads-configuration ()
3416   "Return the current hidden threads configuration."
3417   (save-excursion
3418     (let (config)
3419       (goto-char (point-min))
3420       (while (not (eobp))
3421         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3422           (push (save-excursion (forward-line 0) (point)) config))
3423         (forward-line 1))
3424       config)))
3425
3426 (defun gnus-restore-hidden-threads-configuration (config)
3427   "Restore hidden threads configuration from CONFIG."
3428   (save-excursion
3429     (let (point (inhibit-read-only t))
3430       (while (setq point (pop config))
3431         (goto-char point)
3432         (gnus-summary-hide-thread)))))
3433
3434 ;; Various summary mode internalish functions.
3435
3436 (defun gnus-mouse-pick-article (e)
3437   (interactive "e")
3438   (mouse-set-point e)
3439   (gnus-summary-next-page nil t))
3440
3441 (defun gnus-summary-set-display-table ()
3442   "Change the display table.
3443 Odd characters have a tendency to mess
3444 up nicely formatted displays - we make all possible glyphs
3445 display only a single character."
3446
3447   ;; We start from the standard display table, if any.
3448   (let ((table (or (copy-sequence standard-display-table)
3449                    (make-display-table)))
3450         (i 32))
3451     ;; Nix out all the control chars...
3452     (while (>= (setq i (1- i)) 0)
3453       (gnus-put-display-table i [??] table))
3454    ;; ... but not newline and cr, of course.  (cr is necessary for the
3455     ;; selective display).
3456     (gnus-put-display-table ?\n nil table)
3457     (gnus-put-display-table ?\r nil table)
3458     ;; We keep TAB as well.
3459     (gnus-put-display-table ?\t nil table)
3460     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3461     ;; Emacs 23 (unicode), that are not set already.
3462     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3463                  160
3464                256)))
3465       (while (>= (setq i (1- i)) 127)
3466         ;; Only modify if the entry is nil.
3467         (unless (gnus-get-display-table i table)
3468           (gnus-put-display-table i [??] table))))
3469     (setq buffer-display-table table)))
3470
3471 (defun gnus-summary-set-article-display-arrow (pos)
3472   "Update the overlay arrow to point to line at position POS."
3473   (when gnus-summary-display-arrow
3474     (make-local-variable 'overlay-arrow-position)
3475     (make-local-variable 'overlay-arrow-string)
3476     (save-excursion
3477       (goto-char pos)
3478       (beginning-of-line)
3479       (unless overlay-arrow-position
3480         (setq overlay-arrow-position (make-marker)))
3481       (setq overlay-arrow-string "=>"
3482             overlay-arrow-position (set-marker overlay-arrow-position
3483                                                (point)
3484                                                (current-buffer))))))
3485
3486 (defun gnus-summary-setup-buffer (group)
3487   "Initialize summary buffer."
3488   (let ((buffer (gnus-summary-buffer-name group))
3489         (dead-name (concat "*Dead Summary "
3490                            (gnus-group-decoded-name group) "*")))
3491     ;; If a dead summary buffer exists, we kill it.
3492     (when (gnus-buffer-live-p dead-name)
3493       (gnus-kill-buffer dead-name))
3494     (if (get-buffer buffer)
3495         (progn
3496           (set-buffer buffer)
3497           (setq gnus-summary-buffer (current-buffer))
3498           (not gnus-newsgroup-prepared))
3499       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3500       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3501       (gnus-summary-mode group)
3502       (when gnus-carpal
3503         (gnus-carpal-setup-buffer 'summary))
3504       (when (gnus-group-quit-config group)
3505         (set (make-local-variable 'gnus-single-article-buffer) nil))
3506       (make-local-variable 'gnus-article-buffer)
3507       (make-local-variable 'gnus-article-current)
3508       (make-local-variable 'gnus-original-article-buffer)
3509       (setq gnus-newsgroup-name group)
3510       ;; Set any local variables in the group parameters.
3511       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3512       t)))
3513
3514 (defun gnus-set-global-variables ()
3515   "Set the global equivalents of the buffer-local variables.
3516 They are set to the latest values they had.  These reflect the summary
3517 buffer that was in action when the last article was fetched."
3518   (when (eq major-mode 'gnus-summary-mode)
3519     (setq gnus-summary-buffer (current-buffer))
3520     (let ((name gnus-newsgroup-name)
3521           (marked gnus-newsgroup-marked)
3522           (spam gnus-newsgroup-spam-marked)
3523           (unread gnus-newsgroup-unreads)
3524           (headers gnus-current-headers)
3525           (data gnus-newsgroup-data)
3526           (summary gnus-summary-buffer)
3527           (article-buffer gnus-article-buffer)
3528           (original gnus-original-article-buffer)
3529           (gac gnus-article-current)
3530           (reffed gnus-reffed-article-number)
3531           (score-file gnus-current-score-file)
3532           (default-charset gnus-newsgroup-charset)
3533           vlist)
3534       (let ((locals gnus-newsgroup-variables))
3535         (while locals
3536           (if (consp (car locals))
3537               (push (eval (caar locals)) vlist)
3538             (push (eval (car locals)) vlist))
3539           (setq locals (cdr locals)))
3540         (setq vlist (nreverse vlist)))
3541       (with-current-buffer gnus-group-buffer
3542         (setq gnus-newsgroup-name name
3543               gnus-newsgroup-marked marked
3544               gnus-newsgroup-spam-marked spam
3545               gnus-newsgroup-unreads unread
3546               gnus-current-headers headers
3547               gnus-newsgroup-data data
3548               gnus-article-current gac
3549               gnus-summary-buffer summary
3550               gnus-article-buffer article-buffer
3551               gnus-original-article-buffer original
3552               gnus-reffed-article-number reffed
3553               gnus-current-score-file score-file
3554               gnus-newsgroup-charset default-charset)
3555         (let ((locals gnus-newsgroup-variables))
3556           (while locals
3557             (if (consp (car locals))
3558                 (set (caar locals) (pop vlist))
3559               (set (car locals) (pop vlist)))
3560             (setq locals (cdr locals))))
3561         ;; The article buffer also has local variables.
3562         (when (gnus-buffer-live-p gnus-article-buffer)
3563           (set-buffer gnus-article-buffer)
3564           (setq gnus-summary-buffer summary))))))
3565
3566 (defun gnus-summary-article-unread-p (article)
3567   "Say whether ARTICLE is unread or not."
3568   (memq article gnus-newsgroup-unreads))
3569
3570 (defun gnus-summary-first-article-p (&optional article)
3571   "Return whether ARTICLE is the first article in the buffer."
3572   (if (not (setq article (or article (gnus-summary-article-number))))
3573       nil
3574     (eq article (caar gnus-newsgroup-data))))
3575
3576 (defun gnus-summary-last-article-p (&optional article)
3577   "Return whether ARTICLE is the last article in the buffer."
3578   (if (not (setq article (or article (gnus-summary-article-number))))
3579       ;; All non-existent numbers are the last article.  :-)
3580       t
3581     (not (cdr (gnus-data-find-list article)))))
3582
3583 (defun gnus-make-thread-indent-array (&optional n)
3584   (when (or n
3585             (progn (setq n 200) nil)
3586             (null gnus-thread-indent-array)
3587             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3588     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3589           gnus-thread-indent-array-level gnus-thread-indent-level)
3590     (while (>= n 0)
3591       (aset gnus-thread-indent-array n
3592             (make-string (* n gnus-thread-indent-level) ? ))
3593       (setq n (1- n)))))
3594
3595 (defun gnus-update-summary-mark-positions ()
3596   "Compute where the summary marks are to go."
3597   (save-excursion
3598     (when (gnus-buffer-exists-p gnus-summary-buffer)
3599       (set-buffer gnus-summary-buffer))
3600     (let ((spec gnus-summary-line-format-spec)
3601           pos)
3602       (save-excursion
3603         (gnus-set-work-buffer)
3604         (let ((gnus-tmp-unread ?Z)
3605               (gnus-replied-mark ?Z)
3606               (gnus-score-below-mark ?Z)
3607               (gnus-score-over-mark ?Z)
3608               (gnus-undownloaded-mark ?Z)
3609               (gnus-summary-line-format-spec spec)
3610               (gnus-newsgroup-downloadable '(0))
3611               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3612               case-fold-search ignores)
3613           ;; Here, all marks are bound to Z.
3614           (gnus-summary-insert-line header
3615                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3616           (goto-char (point-min))
3617           ;; Memorize the positions of the same characters as dummy marks.
3618           (while (re-search-forward "[A-D]" nil t)
3619             (push (point) ignores))
3620           (erase-buffer)
3621           ;; We use A-D as dummy marks in order to know column positions
3622           ;; where marks should be inserted.
3623           (setq gnus-tmp-unread ?A
3624                 gnus-replied-mark ?B
3625                 gnus-score-below-mark ?C
3626                 gnus-score-over-mark ?C
3627                 gnus-undownloaded-mark ?D)
3628           (gnus-summary-insert-line header
3629                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3630           ;; Ignore characters which aren't dummy marks.
3631           (dolist (p ignores)
3632             (delete-region (goto-char (1- p)) p)
3633             (insert ?Z))
3634           (goto-char (point-min))
3635           (setq pos (list (cons 'unread
3636                                 (and (search-forward "A" nil t)
3637                                      (- (point) (point-min) 1)))))
3638           (goto-char (point-min))
3639           (push (cons 'replied (and (search-forward "B" nil t)
3640                                     (- (point) (point-min) 1)))
3641                 pos)
3642           (goto-char (point-min))
3643           (push (cons 'score (and (search-forward "C" nil t)
3644                                   (- (point) (point-min) 1)))
3645                 pos)
3646           (goto-char (point-min))
3647           (push (cons 'download (and (search-forward "D" nil t)
3648                                      (- (point) (point-min) 1)))
3649                 pos)))
3650       (setq gnus-summary-mark-positions pos))))
3651
3652 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3653   "Insert a dummy root in the summary buffer."
3654   (beginning-of-line)
3655   (gnus-add-text-properties
3656    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3657    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3658
3659 (defun gnus-summary-extract-address-component (from)
3660   (or (car (funcall gnus-extract-address-components from))
3661       from))
3662
3663 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3664   (let ((mail-parse-charset gnus-newsgroup-charset)
3665         (ignored-from-addresses (gnus-ignored-from-addresses))
3666         ; Is it really necessary to do this next part for each summary line?
3667         ; Luckily, doesn't seem to slow things down much.
3668         (mail-parse-ignored-charsets
3669          (with-current-buffer gnus-summary-buffer
3670            gnus-newsgroup-ignored-charsets)))
3671     (or
3672      (and ignored-from-addresses
3673           (string-match ignored-from-addresses gnus-tmp-from)
3674           (let ((extra-headers (mail-header-extra header))
3675                 to
3676                 newsgroups)
3677             (cond
3678              ((setq to (cdr (assq 'To extra-headers)))
3679               (concat gnus-summary-to-prefix
3680                       (inline
3681                         (gnus-summary-extract-address-component
3682                          (funcall gnus-decode-encoded-address-function to)))))
3683              ((setq newsgroups
3684                     (or
3685                      (cdr (assq 'Newsgroups extra-headers))
3686                      (and
3687                       (memq 'Newsgroups gnus-extra-headers)
3688                       (eq (car (gnus-find-method-for-group
3689                                 gnus-newsgroup-name)) 'nntp)
3690                       (gnus-group-real-name gnus-newsgroup-name))))
3691               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3692      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3693
3694 (defun gnus-summary-insert-line (gnus-tmp-header
3695                                  gnus-tmp-level gnus-tmp-current
3696                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3697                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3698                                  &optional gnus-tmp-dummy gnus-tmp-score
3699                                  gnus-tmp-process)
3700   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3701       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3702                                           gnus-tmp-level)))
3703   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3704          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3705          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3706          (gnus-tmp-score-char
3707           (if (or (null gnus-summary-default-score)
3708                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3709                       gnus-summary-zcore-fuzz))
3710               ?                         ;Whitespace
3711             (if (< gnus-tmp-score gnus-summary-default-score)
3712                 gnus-score-below-mark gnus-score-over-mark)))
3713          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3714          (gnus-tmp-replied
3715           (cond (gnus-tmp-process gnus-process-mark)
3716                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3717                  gnus-cached-mark)
3718                 (gnus-tmp-replied gnus-replied-mark)
3719                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3720                  gnus-forwarded-mark)
3721                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3722                  gnus-saved-mark)
3723                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3724                  gnus-recent-mark)
3725                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3726                  gnus-unseen-mark)
3727                 (t gnus-no-mark)))
3728          (gnus-tmp-downloaded
3729           (cond (undownloaded
3730                  gnus-undownloaded-mark)
3731                 (gnus-newsgroup-agentized
3732                  gnus-downloaded-mark)
3733                 (t
3734                  gnus-no-mark)))
3735          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3736          (gnus-tmp-name
3737           (cond
3738            ((string-match "<[^>]+> *$" gnus-tmp-from)
3739             (let ((beg (match-beginning 0)))
3740               (or (and (string-match "^\".+\"" gnus-tmp-from)
3741                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3742                   (substring gnus-tmp-from 0 beg))))
3743            ((string-match "(.+)" gnus-tmp-from)
3744             (substring gnus-tmp-from
3745                        (1+ (match-beginning 0)) (1- (match-end 0))))
3746            (t gnus-tmp-from)))
3747          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3748          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3749          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3750          (inhibit-read-only t))
3751     (when (string= gnus-tmp-name "")
3752       (setq gnus-tmp-name gnus-tmp-from))
3753     (unless (numberp gnus-tmp-lines)
3754       (setq gnus-tmp-lines -1))
3755     (if (= gnus-tmp-lines -1)
3756         (setq gnus-tmp-lines "?")
3757       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3758     (condition-case ()
3759         (gnus-put-text-property
3760          (point)
3761          (progn (eval gnus-summary-line-format-spec) (point))
3762          'gnus-number gnus-tmp-number)
3763       (error (gnus-message 5 "Error updating the summary line")))
3764     (when (gnus-visual-p 'summary-highlight 'highlight)
3765       (forward-line -1)
3766       (gnus-summary-highlight-line)
3767       (gnus-run-hooks 'gnus-summary-update-hook)
3768       (forward-line 1))))
3769
3770 (defun gnus-summary-update-line (&optional dont-update)
3771   "Update summary line after change."
3772   (when (and gnus-summary-default-score
3773              (not gnus-summary-inhibit-highlight))
3774     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3775            (article (gnus-summary-article-number))
3776            (score (gnus-summary-article-score article)))
3777       (unless dont-update
3778         (if (and gnus-summary-mark-below
3779                  (< (gnus-summary-article-score)
3780                     gnus-summary-mark-below))
3781             ;; This article has a low score, so we mark it as read.
3782             (when (memq article gnus-newsgroup-unreads)
3783               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3784           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3785             ;; This article was previously marked as read on account
3786             ;; of a low score, but now it has risen, so we mark it as
3787             ;; unread.
3788             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3789         (gnus-summary-update-mark
3790          (if (or (null gnus-summary-default-score)
3791                  (<= (abs (- score gnus-summary-default-score))
3792                      gnus-summary-zcore-fuzz))
3793              ?                          ;Whitespace
3794            (if (< score gnus-summary-default-score)
3795                gnus-score-below-mark gnus-score-over-mark))
3796          'score))
3797       ;; Do visual highlighting.
3798       (when (gnus-visual-p 'summary-highlight 'highlight)
3799         (gnus-summary-highlight-line)
3800         (gnus-run-hooks 'gnus-summary-update-hook)))))
3801
3802 (defvar gnus-tmp-new-adopts nil)
3803
3804 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3805   "Return the number of articles in THREAD.
3806 This may be 0 in some cases -- if none of the articles in
3807 the thread are to be displayed."
3808   (let* ((number
3809          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3810           (cond
3811            ((not (listp thread))
3812             1)
3813            ((and (consp thread) (cdr thread))
3814             (apply
3815              '+ 1 (mapcar
3816                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3817            ((null thread)
3818             1)
3819            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3820             1)
3821            (t 0))))
3822     (when (and level (zerop level) gnus-tmp-new-adopts)
3823       (incf number
3824             (apply '+ (mapcar
3825                        'gnus-summary-number-of-articles-in-thread
3826                        gnus-tmp-new-adopts))))
3827     (if char
3828         (if (> number 1) gnus-not-empty-thread-mark
3829           gnus-empty-thread-mark)
3830       number)))
3831
3832 (defsubst gnus-summary-line-message-size (head)
3833   "Return pretty-printed version of message size.
3834 This function is intended to be used in
3835 `gnus-summary-line-format-alist'."
3836   (let ((c (or (mail-header-chars head) -1)))
3837     (cond ((< c 0) "n/a")               ; chars not available
3838           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3839           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3840           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3841           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3842
3843
3844 (defun gnus-summary-set-local-parameters (group)
3845   "Go through the local params of GROUP and set all variable specs in that list."
3846   (let ((vars '(quit-config)))          ; Ignore quit-config.
3847     (dolist (elem (gnus-group-find-parameter group))
3848       (and (consp elem)                 ; Has to be a cons.
3849            (consp (cdr elem))           ; The cdr has to be a list.
3850            (symbolp (car elem))         ; Has to be a symbol in there.
3851            (not (memq (car elem) vars))
3852            (ignore-errors
3853              (push (car elem) vars)
3854              ;; Variables like `gnus-show-threads' that are globally
3855              ;; bound, if used as group parameters, need to get to be
3856              ;; buffer-local, whereas just parameters like `gcc-self',
3857              ;; `timestamp', etc. should not be bound as variables.
3858              (if (boundp (car elem))
3859                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3860                (eval (nth 1 elem))))))))
3861
3862 (defun gnus-summary-read-group (group &optional show-all no-article
3863                                       kill-buffer no-display backward
3864                                       select-articles)
3865   "Start reading news in newsgroup GROUP.
3866 If SHOW-ALL is non-nil, already read articles are also listed.
3867 If NO-ARTICLE is non-nil, no article is selected initially.
3868 If NO-DISPLAY, don't generate a summary buffer."
3869   (let (result)
3870     (while (and group
3871                 (null (setq result
3872                             (let ((gnus-auto-select-next nil))
3873                               (or (gnus-summary-read-group-1
3874                                    group show-all no-article
3875                                    kill-buffer no-display
3876                                    select-articles)
3877                                   (setq show-all nil
3878                                         select-articles nil)))))
3879                 (eq gnus-auto-select-next 'quietly))
3880       (set-buffer gnus-group-buffer)
3881       ;; The entry function called above goes to the next
3882       ;; group automatically, so we go two groups back
3883       ;; if we are searching for the previous group.
3884       (when backward
3885         (gnus-group-prev-unread-group 2))
3886       (if (not (equal group (gnus-group-group-name)))
3887           (setq group (gnus-group-group-name))
3888         (setq group nil)))
3889     result))
3890
3891 (defun gnus-summary-read-group-1 (group show-all no-article
3892                                         kill-buffer no-display
3893                                         &optional select-articles)
3894   ;; Killed foreign groups can't be entered.
3895   ;;  (when (and (not (gnus-group-native-p group))
3896   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3897   ;;    (error "Dead non-native groups can't be entered"))
3898   (gnus-message 5 "Retrieving newsgroup: %s..."
3899                 (gnus-group-decoded-name group))
3900   (let* ((new-group (gnus-summary-setup-buffer group))
3901          (quit-config (gnus-group-quit-config group))
3902          (did-select (and new-group (gnus-select-newsgroup
3903                                      group show-all select-articles))))
3904     (cond
3905      ;; This summary buffer exists already, so we just select it.
3906      ((not new-group)
3907       (gnus-set-global-variables)
3908       (when kill-buffer
3909         (gnus-kill-or-deaden-summary kill-buffer))
3910       (gnus-configure-windows 'summary 'force)
3911       (gnus-set-mode-line 'summary)
3912       (gnus-summary-position-point)
3913       (message "")
3914       t)
3915      ;; We couldn't select this group.
3916      ((null did-select)
3917       (when (and (eq major-mode 'gnus-summary-mode)
3918                  (not (equal (current-buffer) kill-buffer)))
3919         (kill-buffer (current-buffer))
3920         (if (not quit-config)
3921             (progn
3922               ;; Update the info -- marks might need to be removed,
3923               ;; for instance.
3924               (gnus-summary-update-info)
3925               (set-buffer gnus-group-buffer)
3926               (gnus-group-jump-to-group group)
3927               (gnus-group-next-unread-group 1))
3928           (gnus-handle-ephemeral-exit quit-config)))
3929       (let ((grpinfo (gnus-get-info group)))
3930         (if (null (gnus-info-read grpinfo))
3931             (gnus-message 3 "Group %s contains no messages"
3932                           (gnus-group-decoded-name group))
3933           (gnus-message 3 "Can't select group")))
3934       nil)
3935      ;; The user did a `C-g' while prompting for number of articles,
3936      ;; so we exit this group.
3937      ((eq did-select 'quit)
3938       (and (eq major-mode 'gnus-summary-mode)
3939            (not (equal (current-buffer) kill-buffer))
3940            (kill-buffer (current-buffer)))
3941       (when kill-buffer
3942         (gnus-kill-or-deaden-summary kill-buffer))
3943       (if (not quit-config)
3944           (progn
3945             (set-buffer gnus-group-buffer)
3946             (gnus-group-jump-to-group group)
3947             (gnus-configure-windows 'group 'force))
3948         (gnus-handle-ephemeral-exit quit-config))
3949       ;; Finally signal the quit.
3950       (signal 'quit nil))
3951      ;; The group was successfully selected.
3952      (t
3953       (gnus-set-global-variables)
3954       ;; Save the active value in effect when the group was entered.
3955       (setq gnus-newsgroup-active
3956             (gnus-copy-sequence
3957              (gnus-active gnus-newsgroup-name)))
3958       ;; You can change the summary buffer in some way with this hook.
3959       (gnus-run-hooks 'gnus-select-group-hook)
3960       (when (memq 'summary (gnus-update-format-specifications
3961                             nil 'summary 'summary-mode 'summary-dummy))
3962         ;; The format specification for the summary line was updated,
3963         ;; so we need to update the mark positions as well.
3964         (gnus-update-summary-mark-positions))
3965       ;; Do score processing.
3966       (when gnus-use-scoring
3967         (gnus-possibly-score-headers))
3968       ;; Check whether to fill in the gaps in the threads.
3969       (when gnus-build-sparse-threads
3970         (gnus-build-sparse-threads))
3971       ;; Find the initial limit.
3972       (if show-all
3973           (let ((gnus-newsgroup-dormant nil))
3974             (gnus-summary-initial-limit show-all))
3975         (gnus-summary-initial-limit show-all))
3976       ;; Generate the summary buffer.
3977       (unless no-display
3978         (gnus-summary-prepare))
3979       (when gnus-use-trees
3980         (gnus-tree-open group)
3981         (setq gnus-summary-highlight-line-function
3982               'gnus-tree-highlight-article))
3983       ;; If the summary buffer is empty, but there are some low-scored
3984       ;; articles or some excluded dormants, we include these in the
3985       ;; buffer.
3986       (when (and (zerop (buffer-size))
3987                  (not no-display))
3988         (cond (gnus-newsgroup-dormant
3989                (gnus-summary-limit-include-dormant))
3990               ((and gnus-newsgroup-scored show-all)
3991                (gnus-summary-limit-include-expunged t))))
3992       ;; Function `gnus-apply-kill-file' must be called in this hook.
3993       (gnus-run-hooks 'gnus-apply-kill-hook)
3994       (if (and (zerop (buffer-size))
3995                (not no-display))
3996           (progn
3997             ;; This newsgroup is empty.
3998             (gnus-summary-catchup-and-exit nil t)
3999             (gnus-message 6 "No unread news")
4000             (when kill-buffer
4001               (gnus-kill-or-deaden-summary kill-buffer))
4002             ;; Return nil from this function.
4003             nil)
4004         ;; Hide conversation thread subtrees.  We cannot do this in
4005         ;; gnus-summary-prepare-hook since kill processing may not
4006         ;; work with hidden articles.
4007         (gnus-summary-maybe-hide-threads)
4008         (when kill-buffer
4009           (gnus-kill-or-deaden-summary kill-buffer))
4010         (gnus-summary-auto-select-subject)
4011         ;; Show first unread article if requested.
4012         (if (and (not no-article)
4013                  (not no-display)
4014                  gnus-newsgroup-unreads
4015                  gnus-auto-select-first)
4016             (progn
4017               (gnus-configure-windows 'summary)
4018               (let ((art (gnus-summary-article-number)))
4019                 (unless (and (not gnus-plugged)
4020                              (or (memq art gnus-newsgroup-undownloaded)
4021                                  (memq art gnus-newsgroup-downloadable)))
4022                   (gnus-summary-goto-article art))))
4023           ;; Don't select any articles.
4024           (gnus-summary-position-point)
4025           (gnus-configure-windows 'summary 'force)
4026           (gnus-set-mode-line 'summary))
4027         (when (and gnus-auto-center-group
4028                    (get-buffer-window gnus-group-buffer t))
4029           ;; Gotta use windows, because recenter does weird stuff if
4030           ;; the current buffer ain't the displayed window.
4031           (let ((owin (selected-window)))
4032             (select-window (get-buffer-window gnus-group-buffer t))
4033             (when (gnus-group-goto-group group)
4034               (recenter))
4035             (select-window owin)))
4036         ;; Mark this buffer as "prepared".
4037         (setq gnus-newsgroup-prepared t)
4038         (gnus-run-hooks 'gnus-summary-prepared-hook)
4039         (unless (gnus-ephemeral-group-p group)
4040           (gnus-group-update-group group))
4041         t)))))
4042
4043 (defun gnus-summary-auto-select-subject ()
4044   "Select the subject line on initial group entry."
4045   (goto-char (point-min))
4046   (cond
4047    ((eq gnus-auto-select-subject 'best)
4048     (gnus-summary-best-unread-subject))
4049    ((eq gnus-auto-select-subject 'unread)
4050     (gnus-summary-first-unread-subject))
4051    ((eq gnus-auto-select-subject 'unseen)
4052     (gnus-summary-first-unseen-subject))
4053    ((eq gnus-auto-select-subject 'unseen-or-unread)
4054     (gnus-summary-first-unseen-or-unread-subject))
4055    ((eq gnus-auto-select-subject 'first)
4056     ;; Do nothing.
4057     )
4058    ((functionp gnus-auto-select-subject)
4059     (funcall gnus-auto-select-subject))))
4060
4061 (defun gnus-summary-prepare ()
4062   "Generate the summary buffer."
4063   (interactive)
4064   (let ((inhibit-read-only t))
4065     (erase-buffer)
4066     (setq gnus-newsgroup-data nil
4067           gnus-newsgroup-data-reverse nil)
4068     (gnus-run-hooks 'gnus-summary-generate-hook)
4069     ;; Generate the buffer, either with threads or without.
4070     (when gnus-newsgroup-headers
4071       (gnus-summary-prepare-threads
4072        (if gnus-show-threads
4073            (gnus-sort-gathered-threads
4074             (funcall gnus-summary-thread-gathering-function
4075                      (gnus-sort-threads
4076                       (gnus-cut-threads (gnus-make-threads)))))
4077          ;; Unthreaded display.
4078          (gnus-sort-articles gnus-newsgroup-headers))))
4079     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4080     ;; Call hooks for modifying summary buffer.
4081     (goto-char (point-min))
4082     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4083
4084 (defsubst gnus-general-simplify-subject (subject)
4085   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4086   (setq subject
4087         (cond
4088          ;; Truncate the subject.
4089          (gnus-simplify-subject-functions
4090           (gnus-map-function gnus-simplify-subject-functions subject))
4091          ((numberp gnus-summary-gather-subject-limit)
4092           (setq subject (gnus-simplify-subject-re subject))
4093           (if (> (length subject) gnus-summary-gather-subject-limit)
4094               (substring subject 0 gnus-summary-gather-subject-limit)
4095             subject))
4096          ;; Fuzzily simplify it.
4097          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4098           (gnus-simplify-subject-fuzzy subject))
4099          ;; Just remove the leading "Re:".
4100          (t
4101           (gnus-simplify-subject-re subject))))
4102
4103   (if (and gnus-summary-gather-exclude-subject
4104            (string-match gnus-summary-gather-exclude-subject subject))
4105       nil                         ; This article shouldn't be gathered
4106     subject))
4107
4108 (defun gnus-summary-simplify-subject-query ()
4109   "Query where the respool algorithm would put this article."
4110   (interactive)
4111   (gnus-summary-select-article)
4112   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4113
4114 (defun gnus-gather-threads-by-subject (threads)
4115   "Gather threads by looking at Subject headers."
4116   (if (not gnus-summary-make-false-root)
4117       threads
4118     (let ((hashtb (gnus-make-hashtable 1024))
4119           (prev threads)
4120           (result threads)
4121           subject hthread whole-subject)
4122       (while threads
4123         (setq subject (gnus-general-simplify-subject
4124                        (setq whole-subject (mail-header-subject
4125                                             (caar threads)))))
4126         (when subject
4127           (if (setq hthread (gnus-gethash subject hashtb))
4128               (progn
4129                 ;; We enter a dummy root into the thread, if we
4130                 ;; haven't done that already.
4131                 (unless (stringp (caar hthread))
4132                   (setcar hthread (list whole-subject (car hthread))))
4133                 ;; We add this new gathered thread to this gathered
4134                 ;; thread.
4135                 (setcdr (car hthread)
4136                         (nconc (cdar hthread) (list (car threads))))
4137                 ;; Remove it from the list of threads.
4138                 (setcdr prev (cdr threads))
4139                 (setq threads prev))
4140             ;; Enter this thread into the hash table.
4141             (gnus-sethash subject
4142                           (if gnus-summary-make-false-root-always
4143                               (progn
4144                                 ;; If you want a dummy root above all
4145                                 ;; threads...
4146                                 (setcar threads (list whole-subject
4147                                                       (car threads)))
4148                                 threads)
4149                             threads)
4150                           hashtb)))
4151         (setq prev threads)
4152         (setq threads (cdr threads)))
4153       result)))
4154
4155 (defun gnus-gather-threads-by-references (threads)
4156   "Gather threads by looking at References headers."
4157   (let ((idhashtb (gnus-make-hashtable 1024))
4158         (thhashtb (gnus-make-hashtable 1024))
4159         (prev threads)
4160         (result threads)
4161         ids references id gthread gid entered ref)
4162     (while threads
4163       (when (setq references (mail-header-references (caar threads)))
4164         (setq id (mail-header-id (caar threads))
4165               ids (inline (gnus-split-references references))
4166               entered nil)
4167         (while (setq ref (pop ids))
4168           (setq ids (delete ref ids))
4169           (if (not (setq gid (gnus-gethash ref idhashtb)))
4170               (progn
4171                 (gnus-sethash ref id idhashtb)
4172                 (gnus-sethash id threads thhashtb))
4173             (setq gthread (gnus-gethash gid thhashtb))
4174             (unless entered
4175               ;; We enter a dummy root into the thread, if we
4176               ;; haven't done that already.
4177               (unless (stringp (caar gthread))
4178                 (setcar gthread (list (mail-header-subject (caar gthread))
4179                                       (car gthread))))
4180               ;; We add this new gathered thread to this gathered
4181               ;; thread.
4182               (setcdr (car gthread)
4183                       (nconc (cdar gthread) (list (car threads)))))
4184             ;; Add it into the thread hash table.
4185             (gnus-sethash id gthread thhashtb)
4186             (setq entered t)
4187             ;; Remove it from the list of threads.
4188             (setcdr prev (cdr threads))
4189             (setq threads prev))))
4190       (setq prev threads)
4191       (setq threads (cdr threads)))
4192     result))
4193
4194 (defun gnus-sort-gathered-threads (threads)
4195   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4196   (let ((result threads))
4197     (while threads
4198       (when (stringp (caar threads))
4199         (setcdr (car threads)
4200                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4201       (setq threads (cdr threads)))
4202     result))
4203
4204 (defun gnus-thread-loop-p (root thread)
4205   "Say whether ROOT is in THREAD."
4206   (let ((stack (list thread))
4207         (infloop 0)
4208         th)
4209     (while (setq thread (pop stack))
4210       (setq th (cdr thread))
4211       (while (and th
4212                   (not (eq (caar th) root)))
4213         (pop th))
4214       (if th
4215           ;; We have found a loop.
4216           (let (ref-dep)
4217             (setcdr thread (delq (car th) (cdr thread)))
4218             (if (boundp (setq ref-dep (intern "none"
4219                                               gnus-newsgroup-dependencies)))
4220                 (setcdr (symbol-value ref-dep)
4221                         (nconc (cdr (symbol-value ref-dep))
4222                                (list (car th))))
4223               (set ref-dep (list nil (car th))))
4224             (setq infloop 1
4225                   stack nil))
4226         ;; Push all the subthreads onto the stack.
4227         (push (cdr thread) stack)))
4228     infloop))
4229
4230 (defun gnus-make-threads ()
4231   "Go through the dependency hashtb and find the roots.  Return all threads."
4232   (let (threads)
4233     (while (catch 'infloop
4234              (mapatoms
4235               (lambda (refs)
4236                 ;; Deal with self-referencing References loops.
4237                 (when (and (car (symbol-value refs))
4238                            (not (zerop
4239                                  (apply
4240                                   '+
4241                                   (mapcar
4242                                    (lambda (thread)
4243                                      (gnus-thread-loop-p
4244                                       (car (symbol-value refs)) thread))
4245                                    (cdr (symbol-value refs)))))))
4246                   (setq threads nil)
4247                   (throw 'infloop t))
4248                 (unless (car (symbol-value refs))
4249                   ;; These threads do not refer back to any other
4250                   ;; articles, so they're roots.
4251                   (setq threads (append (cdr (symbol-value refs)) threads))))
4252               gnus-newsgroup-dependencies)))
4253     threads))
4254
4255 ;; Build the thread tree.
4256 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4257   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4258
4259 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4260 if it was already present.
4261
4262 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4263 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4264 Message-IDs will be renamed to a unique Message-ID before being
4265 entered.
4266
4267 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4268   (let* ((id (mail-header-id header))
4269          (id-dep (and id (intern id dependencies)))
4270          parent-id ref ref-dep ref-header replaced)
4271     ;; Enter this `header' in the `dependencies' table.
4272     (cond
4273      ((not id-dep)
4274       (setq header nil))
4275      ;; The first two cases do the normal part: enter a new `header'
4276      ;; in the `dependencies' table.
4277      ((not (boundp id-dep))
4278       (set id-dep (list header)))
4279      ((null (car (symbol-value id-dep)))
4280       (setcar (symbol-value id-dep) header))
4281
4282      ;; From here the `header' was already present in the
4283      ;; `dependencies' table.
4284      (force-new
4285       ;; Overrides an existing entry;
4286       ;; just set the header part of the entry.
4287       (setcar (symbol-value id-dep) header)
4288       (setq replaced t))
4289
4290      ;; Renames the existing `header' to a unique Message-ID.
4291      ((not gnus-summary-ignore-duplicates)
4292       ;; An article with this Message-ID has already been seen.
4293       ;; We rename the Message-ID.
4294       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4295            (list header))
4296       (mail-header-set-id header id))
4297
4298      ;; The last case ignores an existing entry, except it adds any
4299      ;; additional Xrefs (in case the two articles came from different
4300      ;; servers.
4301      ;; Also sets `header' to `nil' meaning that the `dependencies'
4302      ;; table was *not* modified.
4303      (t
4304       (mail-header-set-xref
4305        (car (symbol-value id-dep))
4306        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4307                    "")
4308                (or (mail-header-xref header) "")))
4309       (setq header nil)))
4310
4311     (when (and header (not replaced))
4312       ;; First check that we are not creating a References loop.
4313       (setq parent-id (gnus-parent-id (mail-header-references header)))
4314       (setq ref parent-id)
4315       (while (and ref
4316                   (setq ref-dep (intern-soft ref dependencies))
4317                   (boundp ref-dep)
4318                   (setq ref-header (car (symbol-value ref-dep))))
4319         (if (string= id ref)
4320             ;; Yuk!  This is a reference loop.  Make the article be a
4321             ;; root article.
4322             (progn
4323               (mail-header-set-references (car (symbol-value id-dep)) "none")
4324               (setq ref nil)
4325               (setq parent-id nil))
4326           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4327       (setq ref-dep (intern (or parent-id "none") dependencies))
4328       (if (boundp ref-dep)
4329           (setcdr (symbol-value ref-dep)
4330                   (nconc (cdr (symbol-value ref-dep))
4331                          (list (symbol-value id-dep))))
4332         (set ref-dep (list nil (symbol-value id-dep)))))
4333     header))
4334
4335 (defun gnus-extract-message-id-from-in-reply-to (string)
4336   (if (string-match "<[^>]+>" string)
4337       (substring string (match-beginning 0) (match-end 0))
4338     nil))
4339
4340 (defun gnus-build-sparse-threads ()
4341   (let ((headers gnus-newsgroup-headers)
4342         (mail-parse-charset gnus-newsgroup-charset)
4343         (gnus-summary-ignore-duplicates t)
4344         header references generation relations
4345         subject child end new-child date)
4346     ;; First we create an alist of generations/relations, where
4347     ;; generations is how much we trust the relation, and the relation
4348     ;; is parent/child.
4349     (gnus-message 7 "Making sparse threads...")
4350     (save-excursion
4351       (nnheader-set-temp-buffer " *gnus sparse threads*")
4352       (while (setq header (pop headers))
4353         (when (and (setq references (mail-header-references header))
4354                    (not (string= references "")))
4355           (insert references)
4356           (setq child (mail-header-id header)
4357                 subject (mail-header-subject header)
4358                 date (mail-header-date header)
4359                 generation 0)
4360           (while (search-backward ">" nil t)
4361             (setq end (1+ (point)))
4362             (when (search-backward "<" nil t)
4363               (setq new-child (buffer-substring (point) end))
4364               (push (list (incf generation)
4365                           child (setq child new-child)
4366                           subject date)
4367                     relations)))
4368           (when child
4369             (push (list (1+ generation) child nil subject) relations))
4370           (erase-buffer)))
4371       (kill-buffer (current-buffer)))
4372     ;; Sort over trustworthiness.
4373     (dolist (relation (sort relations 'car-less-than-car))
4374       (when (gnus-dependencies-add-header
4375              (make-full-mail-header
4376               gnus-reffed-article-number
4377               (nth 3 relation) "" (or (nth 4 relation) "")
4378               (nth 1 relation)
4379               (or (nth 2 relation) "") 0 0 "")
4380              gnus-newsgroup-dependencies nil)
4381         (push gnus-reffed-article-number gnus-newsgroup-limit)
4382         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4383         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4384               gnus-newsgroup-reads)
4385         (decf gnus-reffed-article-number)))
4386     (gnus-message 7 "Making sparse threads...done")))
4387
4388 (defun gnus-build-old-threads ()
4389   ;; Look at all the articles that refer back to old articles, and
4390   ;; fetch the headers for the articles that aren't there.  This will
4391   ;; build complete threads - if the roots haven't been expired by the
4392   ;; server, that is.
4393   (let ((mail-parse-charset gnus-newsgroup-charset)
4394         id heads)
4395     (mapatoms
4396      (lambda (refs)
4397        (when (not (car (symbol-value refs)))
4398          (setq heads (cdr (symbol-value refs)))
4399          (while heads
4400            (if (memq (mail-header-number (caar heads))
4401                      gnus-newsgroup-dormant)
4402                (setq heads (cdr heads))
4403              (setq id (symbol-name refs))
4404              (while (and (setq id (gnus-build-get-header id))
4405                          (not (car (gnus-id-to-thread id)))))
4406              (setq heads nil)))))
4407      gnus-newsgroup-dependencies)))
4408
4409 (defsubst gnus-remove-odd-characters (string)
4410   "Translate STRING into something that doesn't contain weird characters."
4411   (mm-subst-char-in-string
4412    ?\r ?\-
4413    (mm-subst-char-in-string ?\n ?\- string t) t))
4414
4415 ;; This function has to be called with point after the article number
4416 ;; on the beginning of the line.
4417 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4418   (let ((eol (point-at-eol))
4419         (buffer (current-buffer))
4420         header references in-reply-to)
4421
4422     ;; overview: [num subject from date id refs chars lines misc]
4423     (unwind-protect
4424         (let (x)
4425           (narrow-to-region (point) eol)
4426           (unless (eobp)
4427             (forward-char))
4428
4429           (setq header
4430                 (make-full-mail-header
4431                  number                 ; number
4432                  (condition-case ()     ; subject
4433                      (gnus-remove-odd-characters
4434                       (funcall gnus-decode-encoded-word-function
4435                                (setq x (nnheader-nov-field))))
4436                    (error x))
4437                  (condition-case ()     ; from
4438                      (gnus-remove-odd-characters
4439                       (funcall gnus-decode-encoded-address-function
4440                                (setq x (nnheader-nov-field))))
4441                    (error x))
4442                  (nnheader-nov-field)   ; date
4443                  (nnheader-nov-read-message-id number)  ; id
4444                  (setq references (nnheader-nov-field)) ; refs
4445                  (nnheader-nov-read-integer) ; chars
4446                  (nnheader-nov-read-integer) ; lines
4447                  (unless (eobp)
4448                    (if (looking-at "Xref: ")
4449                        (goto-char (match-end 0)))
4450                    (nnheader-nov-field)) ; Xref
4451                  (nnheader-nov-parse-extra)))) ; extra
4452
4453       (widen))
4454
4455     (when (and (string= references "")
4456                (setq in-reply-to (mail-header-extra header))
4457                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4458       (mail-header-set-references
4459        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4460
4461     (when gnus-alter-header-function
4462       (funcall gnus-alter-header-function header))
4463     (gnus-dependencies-add-header header dependencies force-new)))
4464
4465 (defun gnus-build-get-header (id)
4466   "Look through the buffer of NOV lines and find the header to ID.
4467 Enter this line into the dependencies hash table, and return
4468 the id of the parent article (if any)."
4469   (let ((deps gnus-newsgroup-dependencies)
4470         found header)
4471     (prog1
4472         (with-current-buffer nntp-server-buffer
4473           (let ((case-fold-search nil))
4474             (goto-char (point-min))
4475             (while (and (not found)
4476                         (search-forward id nil t))
4477               (beginning-of-line)
4478               (setq found (looking-at
4479                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4480                                    (regexp-quote id))))
4481               (or found (beginning-of-line 2)))
4482             (when found
4483               (beginning-of-line)
4484               (and
4485                (setq header (gnus-nov-parse-line
4486                              (read (current-buffer)) deps))
4487                (gnus-parent-id (mail-header-references header))))))
4488       (when header
4489         (let ((number (mail-header-number header)))
4490           (push number gnus-newsgroup-limit)
4491           (push header gnus-newsgroup-headers)
4492           (if (memq number gnus-newsgroup-unselected)
4493               (progn
4494                 (setq gnus-newsgroup-unreads
4495                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4496                                                number))
4497                 (setq gnus-newsgroup-unselected
4498                       (delq number gnus-newsgroup-unselected)))
4499             (push number gnus-newsgroup-ancient)))))))
4500
4501 (defun gnus-build-all-threads ()
4502   "Read all the headers."
4503   (let ((gnus-summary-ignore-duplicates t)
4504         (mail-parse-charset gnus-newsgroup-charset)
4505         (dependencies gnus-newsgroup-dependencies)
4506         header article)
4507     (with-current-buffer nntp-server-buffer
4508       (let ((case-fold-search nil))
4509         (goto-char (point-min))
4510         (while (not (eobp))
4511           (ignore-errors
4512             (setq article (read (current-buffer))
4513                   header (gnus-nov-parse-line article dependencies)))
4514           (when header
4515             (with-current-buffer gnus-summary-buffer
4516               (push header gnus-newsgroup-headers)
4517               (if (memq (setq article (mail-header-number header))
4518                         gnus-newsgroup-unselected)
4519                   (progn
4520                     (setq gnus-newsgroup-unreads
4521                           (gnus-add-to-sorted-list
4522                            gnus-newsgroup-unreads article))
4523                     (setq gnus-newsgroup-unselected
4524                           (delq article gnus-newsgroup-unselected)))
4525                 (push article gnus-newsgroup-ancient)))
4526             (forward-line 1)))))))
4527
4528 (defun gnus-summary-update-article-line (article header)
4529   "Update the line for ARTICLE using HEADER."
4530   (let* ((id (mail-header-id header))
4531          (thread (gnus-id-to-thread id)))
4532     (unless thread
4533       (error "Article in no thread"))
4534     ;; Update the thread.
4535     (setcar thread header)
4536     (gnus-summary-goto-subject article)
4537     (let* ((datal (gnus-data-find-list article))
4538            (data (car datal))
4539            (inhibit-read-only t)
4540            (level (gnus-summary-thread-level)))
4541       (gnus-delete-line)
4542       (let ((inserted (- (point)
4543                          (progn
4544                            (gnus-summary-insert-line
4545                             header level nil
4546                             (memq article gnus-newsgroup-undownloaded)
4547                             (gnus-article-mark article)
4548                             (memq article gnus-newsgroup-replied)
4549                             (memq article gnus-newsgroup-expirable)
4550                             ;; Only insert the Subject string when it's different
4551                             ;; from the previous Subject string.
4552                             (if (and
4553                                  gnus-show-threads
4554                                  (gnus-subject-equal
4555                                   (condition-case ()
4556                                       (mail-header-subject
4557                                        (gnus-data-header
4558                                         (cadr
4559                                          (gnus-data-find-list
4560                                           article
4561                                           (gnus-data-list t)))))
4562                                     ;; Error on the side of excessive subjects.
4563                                     (error ""))
4564                                   (mail-header-subject header)))
4565                                 ""
4566                               (mail-header-subject header))
4567                             nil (cdr (assq article gnus-newsgroup-scored))
4568                             (memq article gnus-newsgroup-processable))
4569                            (point)))))
4570         (when (cdr datal)
4571           (gnus-data-update-list
4572            (cdr datal)
4573            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4574
4575 (defun gnus-summary-update-article (article &optional iheader)
4576   "Update ARTICLE in the summary buffer."
4577   (set-buffer gnus-summary-buffer)
4578   (let* ((header (gnus-summary-article-header article))
4579          (id (mail-header-id header))
4580          (data (gnus-data-find article))
4581          (thread (gnus-id-to-thread id))
4582          (references (mail-header-references header))
4583          (parent
4584           (gnus-id-to-thread
4585            (or (gnus-parent-id
4586                 (when (and references
4587                            (not (equal "" references)))
4588                   references))
4589                "none")))
4590          (inhibit-read-only t)
4591          (old (car thread)))
4592     (when thread
4593       (unless iheader
4594         (setcar thread nil)
4595         (when parent
4596           (delq thread parent)))
4597       (if (gnus-summary-insert-subject id header)
4598           ;; Set the (possibly) new article number in the data structure.
4599           (gnus-data-set-number data (gnus-id-to-article id))
4600         (setcar thread old)
4601         nil))))
4602
4603 (defun gnus-rebuild-thread (id &optional line)
4604   "Rebuild the thread containing ID.
4605 If LINE, insert the rebuilt thread starting on line LINE."
4606   (let ((inhibit-read-only t)
4607         old-pos current thread data)
4608     (if (not gnus-show-threads)
4609         (setq thread (list (car (gnus-id-to-thread id))))
4610       ;; Get the thread this article is part of.
4611       (setq thread (gnus-remove-thread id)))
4612     (setq old-pos (point-at-bol))
4613     (setq current (save-excursion
4614                     (and (re-search-backward "[\r\n]" nil t)
4615                          (gnus-summary-article-number))))
4616     ;; If this is a gathered thread, we have to go some re-gathering.
4617     (when (stringp (car thread))
4618       (let ((subject (car thread))
4619             roots thr)
4620         (setq thread (cdr thread))
4621         (while thread
4622           (unless (memq (setq thr (gnus-id-to-thread
4623                                    (gnus-root-id
4624                                     (mail-header-id (caar thread)))))
4625                         roots)
4626             (push thr roots))
4627           (setq thread (cdr thread)))
4628         ;; We now have all (unique) roots.
4629         (if (= (length roots) 1)
4630             ;; All the loose roots are now one solid root.
4631             (setq thread (car roots))
4632           (setq thread (cons subject (gnus-sort-threads roots))))))
4633     (let (threads)
4634       ;; We then insert this thread into the summary buffer.
4635       (when line
4636         (goto-char (point-min))
4637         (forward-line (1- line)))
4638       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4639         (if gnus-show-threads
4640             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4641           (gnus-summary-prepare-unthreaded thread))
4642         (setq data (nreverse gnus-newsgroup-data))
4643         (setq threads gnus-newsgroup-threads))
4644       ;; We splice the new data into the data structure.
4645       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4646       ;;!!! then we want to insert at the beginning of the buffer.
4647       ;;!!! That happens to be true with Gnus now, but that may
4648       ;;!!! change in the future.  Perhaps.
4649       (gnus-data-enter-list
4650        (if line nil current) data (- (point) old-pos))
4651       (setq gnus-newsgroup-threads
4652             (nconc threads gnus-newsgroup-threads))
4653       (gnus-data-compute-positions))))
4654
4655 (defun gnus-number-to-header (number)
4656   "Return the header for article NUMBER."
4657   (let ((headers gnus-newsgroup-headers))
4658     (while (and headers
4659                 (not (= number (mail-header-number (car headers)))))
4660       (pop headers))
4661     (when headers
4662       (car headers))))
4663
4664 (defun gnus-parent-headers (in-headers &optional generation)
4665   "Return the headers of the GENERATIONeth parent of HEADERS."
4666   (unless generation
4667     (setq generation 1))
4668   (let ((parent t)
4669         (headers in-headers)
4670         references)
4671     (while (and parent
4672                 (not (zerop generation))
4673                 (setq references (mail-header-references headers)))
4674       (setq headers (if (and references
4675                              (setq parent (gnus-parent-id references)))
4676                         (car (gnus-id-to-thread parent))
4677                       nil))
4678       (decf generation))
4679     (and (not (eq headers in-headers))
4680          headers)))
4681
4682 (defun gnus-id-to-thread (id)
4683   "Return the (sub-)thread where ID appears."
4684   (gnus-gethash id gnus-newsgroup-dependencies))
4685
4686 (defun gnus-id-to-article (id)
4687   "Return the article number of ID."
4688   (let ((thread (gnus-id-to-thread id)))
4689     (when (and thread
4690                (car thread))
4691       (mail-header-number (car thread)))))
4692
4693 (defun gnus-id-to-header (id)
4694   "Return the article headers of ID."
4695   (car (gnus-id-to-thread id)))
4696
4697 (defun gnus-article-displayed-root-p (article)
4698   "Say whether ARTICLE is a root(ish) article."
4699   (let ((level (gnus-summary-thread-level article))
4700         (refs (mail-header-references  (gnus-summary-article-header article)))
4701         particle)
4702     (cond
4703      ((null level) nil)
4704      ((zerop level) t)
4705      ((null refs) t)
4706      ((null (gnus-parent-id refs)) t)
4707      ((and (= 1 level)
4708            (null (setq particle (gnus-id-to-article
4709                                  (gnus-parent-id refs))))
4710            (null (gnus-summary-thread-level particle)))))))
4711
4712 (defun gnus-root-id (id)
4713   "Return the id of the root of the thread where ID appears."
4714   (let (last-id prev)
4715     (while (and id (setq prev (car (gnus-id-to-thread id))))
4716       (setq last-id id
4717             id (gnus-parent-id (mail-header-references prev))))
4718     last-id))
4719
4720 (defun gnus-articles-in-thread (thread)
4721   "Return the list of articles in THREAD."
4722   (cons (mail-header-number (car thread))
4723         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4724
4725 (defun gnus-remove-thread (id &optional dont-remove)
4726   "Remove the thread that has ID in it."
4727   (let (headers thread last-id)
4728     ;; First go up in this thread until we find the root.
4729     (setq last-id (gnus-root-id id)
4730           headers (message-flatten-list (gnus-id-to-thread last-id)))
4731     ;; We have now found the real root of this thread.  It might have
4732     ;; been gathered into some loose thread, so we have to search
4733     ;; through the threads to find the thread we wanted.
4734     (let ((threads gnus-newsgroup-threads)
4735           sub)
4736       (while threads
4737         (setq sub (car threads))
4738         (if (stringp (car sub))
4739             ;; This is a gathered thread, so we look at the roots
4740             ;; below it to find whether this article is in this
4741             ;; gathered root.
4742             (progn
4743               (setq sub (cdr sub))
4744               (while sub
4745                 (when (member (caar sub) headers)
4746                   (setq thread (car threads)
4747                         threads nil
4748                         sub nil))
4749                 (setq sub (cdr sub))))
4750           ;; It's an ordinary thread, so we check it.
4751           (when (eq (car sub) (car headers))
4752             (setq thread sub
4753                   threads nil)))
4754         (setq threads (cdr threads)))
4755       ;; If this article is in no thread, then it's a root.
4756       (if thread
4757           (unless dont-remove
4758             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4759         (setq thread (gnus-id-to-thread last-id)))
4760       (when thread
4761         (prog1
4762             thread                      ; We return this thread.
4763           (unless dont-remove
4764             (if (stringp (car thread))
4765                 (progn
4766                   ;; If we use dummy roots, then we have to remove the
4767                   ;; dummy root as well.
4768                   (when (eq gnus-summary-make-false-root 'dummy)
4769                     ;; We go to the dummy root by going to
4770                     ;; the first sub-"thread", and then one line up.
4771                     (gnus-summary-goto-article
4772                      (mail-header-number (caadr thread)))
4773                     (forward-line -1)
4774                     (gnus-delete-line)
4775                     (gnus-data-compute-positions))
4776                   (setq thread (cdr thread))
4777                   (while thread
4778                     (gnus-remove-thread-1 (car thread))
4779                     (setq thread (cdr thread))))
4780               (gnus-remove-thread-1 thread))))))))
4781
4782 (defun gnus-remove-thread-1 (thread)
4783   "Remove the thread THREAD recursively."
4784   (let ((number (mail-header-number (pop thread)))
4785         d)
4786     (setq thread (reverse thread))
4787     (while thread
4788       (gnus-remove-thread-1 (pop thread)))
4789     (when (setq d (gnus-data-find number))
4790       (goto-char (gnus-data-pos d))
4791       (gnus-summary-show-thread)
4792       (gnus-data-remove
4793        number
4794        (- (point-at-bol)
4795           (prog1
4796               (1+ (point-at-eol))
4797             (gnus-delete-line)))))))
4798
4799 (defun gnus-sort-threads-recursive (threads func)
4800   (sort (mapcar (lambda (thread)
4801                   (cons (car thread)
4802                         (and (cdr thread)
4803                              (gnus-sort-threads-recursive (cdr thread) func))))
4804                 threads) func))
4805
4806 (defun gnus-sort-threads-loop (threads func)
4807   (let* ((superthread (cons nil threads))
4808          (stack (list (cons superthread threads)))
4809          remaining-threads thread)
4810     (while stack
4811       (setq remaining-threads (cdr (car stack)))
4812       (if remaining-threads
4813           (progn (setq thread (car remaining-threads))
4814                  (setcdr (car stack) (cdr remaining-threads))
4815                  (if (cdr thread)
4816                      (push (cons thread (cdr thread)) stack)))
4817         (setq thread (caar stack))
4818         (setcdr thread (sort (cdr thread) func))
4819         (pop stack)))
4820     (cdr superthread)))
4821
4822 (defun gnus-sort-threads (threads)
4823   "Sort THREADS."
4824   (if (not gnus-thread-sort-functions)
4825       threads
4826     (gnus-message 8 "Sorting threads...")
4827     (prog1
4828         (condition-case nil
4829             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4830               (gnus-sort-threads-recursive
4831                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4832           ;; Even after binding max-lisp-eval-depth, the recursive
4833           ;; sorter might fail for very long threads.  In that case,
4834           ;; try using a (less well-tested) non-recursive sorter.
4835           (error (gnus-message 9 "Sorting threads with loop...")
4836                  (gnus-sort-threads-loop
4837                   threads (gnus-make-sort-function
4838                            gnus-thread-sort-functions))))
4839       (gnus-message 8 "Sorting threads...done"))))
4840
4841 (defun gnus-sort-articles (articles)
4842   "Sort ARTICLES."
4843   (when gnus-article-sort-functions
4844     (gnus-message 7 "Sorting articles...")
4845     (prog1
4846         (setq gnus-newsgroup-headers
4847               (sort articles (gnus-make-sort-function
4848                               gnus-article-sort-functions)))
4849       (gnus-message 7 "Sorting articles...done"))))
4850
4851 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4852 (defmacro gnus-thread-header (thread)
4853   "Return header of first article in THREAD.
4854 Note that THREAD must never, ever be anything else than a variable -
4855 using some other form will lead to serious barfage."
4856   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4857   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4858   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4859         (vector thread) 2))
4860
4861 (defsubst gnus-article-sort-by-number (h1 h2)
4862   "Sort articles by article number."
4863   (< (mail-header-number h1)
4864      (mail-header-number h2)))
4865
4866 (defun gnus-thread-sort-by-number (h1 h2)
4867   "Sort threads by root article number."
4868   (gnus-article-sort-by-number
4869    (gnus-thread-header h1) (gnus-thread-header h2)))
4870
4871 (defsubst gnus-article-sort-by-random (h1 h2)
4872   "Sort articles randomly."
4873   (zerop (random 2)))
4874
4875 (defun gnus-thread-sort-by-random (h1 h2)
4876   "Sort threads randomly."
4877   (gnus-article-sort-by-random
4878    (gnus-thread-header h1) (gnus-thread-header h2)))
4879
4880 (defsubst gnus-article-sort-by-lines (h1 h2)
4881   "Sort articles by article Lines header."
4882   (< (mail-header-lines h1)
4883      (mail-header-lines h2)))
4884
4885 (defun gnus-thread-sort-by-lines (h1 h2)
4886   "Sort threads by root article Lines header."
4887   (gnus-article-sort-by-lines
4888    (gnus-thread-header h1) (gnus-thread-header h2)))
4889
4890 (defsubst gnus-article-sort-by-chars (h1 h2)
4891   "Sort articles by octet length."
4892   (< (mail-header-chars h1)
4893      (mail-header-chars h2)))
4894
4895 (defun gnus-thread-sort-by-chars (h1 h2)
4896   "Sort threads by root article octet length."
4897   (gnus-article-sort-by-chars
4898    (gnus-thread-header h1) (gnus-thread-header h2)))
4899
4900 (defsubst gnus-article-sort-by-author (h1 h2)
4901   "Sort articles by root author."
4902   (gnus-string<
4903    (let ((extract (funcall
4904                    gnus-extract-address-components
4905                    (mail-header-from h1))))
4906      (or (car extract) (cadr extract) ""))
4907    (let ((extract (funcall
4908                    gnus-extract-address-components
4909                    (mail-header-from h2))))
4910      (or (car extract) (cadr extract) ""))))
4911
4912 (defun gnus-thread-sort-by-author (h1 h2)
4913   "Sort threads by root author."
4914   (gnus-article-sort-by-author
4915    (gnus-thread-header h1)  (gnus-thread-header h2)))
4916
4917 (defsubst gnus-article-sort-by-recipient (h1 h2)
4918   "Sort articles by recipient."
4919   (gnus-string<
4920    (let ((extract (funcall
4921                    gnus-extract-address-components
4922                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4923      (or (car extract) (cadr extract)))
4924    (let ((extract (funcall
4925                    gnus-extract-address-components
4926                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4927      (or (car extract) (cadr extract)))))
4928
4929 (defun gnus-thread-sort-by-recipient (h1 h2)
4930   "Sort threads by root recipient."
4931   (gnus-article-sort-by-recipient
4932    (gnus-thread-header h1) (gnus-thread-header h2)))
4933
4934 (defsubst gnus-article-sort-by-subject (h1 h2)
4935   "Sort articles by root subject."
4936   (gnus-string<
4937    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4938    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4939
4940 (defun gnus-thread-sort-by-subject (h1 h2)
4941   "Sort threads by root subject."
4942   (gnus-article-sort-by-subject
4943    (gnus-thread-header h1) (gnus-thread-header h2)))
4944
4945 (defsubst gnus-article-sort-by-date (h1 h2)
4946   "Sort articles by root article date."
4947   (time-less-p
4948    (gnus-date-get-time (mail-header-date h1))
4949    (gnus-date-get-time (mail-header-date h2))))
4950
4951 (defun gnus-thread-sort-by-date (h1 h2)
4952   "Sort threads by root article date."
4953   (gnus-article-sort-by-date
4954    (gnus-thread-header h1) (gnus-thread-header h2)))
4955
4956 (defsubst gnus-article-sort-by-score (h1 h2)
4957   "Sort articles by root article score.
4958 Unscored articles will be counted as having a score of zero."
4959   (> (or (cdr (assq (mail-header-number h1)
4960                     gnus-newsgroup-scored))
4961          gnus-summary-default-score 0)
4962      (or (cdr (assq (mail-header-number h2)
4963                     gnus-newsgroup-scored))
4964          gnus-summary-default-score 0)))
4965
4966 (defun gnus-thread-sort-by-score (h1 h2)
4967   "Sort threads by root article score."
4968   (gnus-article-sort-by-score
4969    (gnus-thread-header h1) (gnus-thread-header h2)))
4970
4971 (defun gnus-thread-sort-by-total-score (h1 h2)
4972   "Sort threads by the sum of all scores in the thread.
4973 Unscored articles will be counted as having a score of zero."
4974   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4975
4976 (defun gnus-thread-total-score (thread)
4977   ;; This function find the total score of THREAD.
4978   (cond
4979    ((null thread)
4980     0)
4981    ((consp thread)
4982     (if (stringp (car thread))
4983         (apply gnus-thread-score-function 0
4984                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4985       (gnus-thread-total-score-1 thread)))
4986    (t
4987     (gnus-thread-total-score-1 (list thread)))))
4988
4989 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4990   "Sort threads such that the thread with the most recently arrived article comes first."
4991   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4992
4993 (defun gnus-thread-highest-number (thread)
4994   "Return the highest article number in THREAD."
4995   (apply 'max (mapcar (lambda (header)
4996                         (mail-header-number header))
4997                       (message-flatten-list thread))))
4998
4999 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5000   "Sort threads such that the thread with the most recently dated article comes first."
5001   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5002
5003 ; Since this is called not only to sort the top-level threads, but
5004 ; also in recursive sorts to order the articles within a thread, each
5005 ; article will be processed many times.  Thus it speeds things up
5006 ; quite a bit to use gnus-date-get-time, which caches the time value.
5007 (defun gnus-thread-latest-date (thread)
5008   "Return the highest article date in THREAD."
5009   (apply 'max
5010          (mapcar (lambda (header) (gnus-float-time
5011                                    (gnus-date-get-time
5012                                     (mail-header-date header))))
5013                  (message-flatten-list thread))))
5014
5015 (defun gnus-thread-total-score-1 (root)
5016   ;; This function find the total score of the thread below ROOT.
5017   (setq root (car root))
5018   (apply gnus-thread-score-function
5019          (or (append
5020               (mapcar 'gnus-thread-total-score
5021                       (cdr (gnus-id-to-thread (mail-header-id root))))
5022               (when (> (mail-header-number root) 0)
5023                 (list (or (cdr (assq (mail-header-number root)
5024                                      gnus-newsgroup-scored))
5025                           gnus-summary-default-score 0))))
5026              (list gnus-summary-default-score)
5027              '(0))))
5028
5029 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5030 (defvar gnus-tmp-prev-subject nil)
5031 (defvar gnus-tmp-false-parent nil)
5032 (defvar gnus-tmp-root-expunged nil)
5033 (defvar gnus-tmp-dummy-line nil)
5034
5035 (defun gnus-extra-header (type &optional header)
5036   "Return the extra header of TYPE."
5037   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5038       ""))
5039
5040 (defvar gnus-tmp-thread-tree-header-string "")
5041
5042 (defcustom gnus-sum-thread-tree-root "> "
5043   "With %B spec, used for the 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-false-root "> "
5050   "With %B spec, used for a false root of a thread.
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-single-indent ""
5057   "With %B spec, used for a thread with just one message.
5058 If nil, use subject instead."
5059   :version "22.1"
5060   :type '(radio (const :format "%v  " nil) string)
5061   :group 'gnus-thread)
5062
5063 (defcustom gnus-sum-thread-tree-vertical "| "
5064   "With %B spec, used for drawing a vertical line."
5065   :version "22.1"
5066   :type 'string
5067   :group 'gnus-thread)
5068
5069 (defcustom gnus-sum-thread-tree-indent "  "
5070   "With %B spec, used for indenting."
5071   :version "22.1"
5072   :type 'string
5073   :group 'gnus-thread)
5074
5075 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5076   "With %B spec, used for a leaf with brothers."
5077   :version "22.1"
5078   :type 'string
5079   :group 'gnus-thread)
5080
5081 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5082   "With %B spec, used for a leaf without brothers."
5083   :version "22.1"
5084   :type 'string
5085   :group 'gnus-thread)
5086
5087 (defcustom gnus-summary-display-while-building nil
5088   "If non-nil, show and update the summary buffer as it's being built.
5089 If the value is t, update the buffer after every line is inserted.  If
5090 the value is an integer (N), update the display every N lines."
5091   :version "22.1"
5092   :group 'gnus-thread
5093   :type '(choice (const :tag "off" nil)
5094                  number
5095                  (const :tag "frequently" t)))
5096
5097 (defun gnus-summary-prepare-threads (threads)
5098   "Prepare summary buffer from THREADS and indentation LEVEL.
5099 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5100 or a straight list of headers."
5101   (gnus-message 7 "Generating summary...")
5102
5103   (setq gnus-newsgroup-threads threads)
5104   (beginning-of-line)
5105
5106   (let ((gnus-tmp-level 0)
5107         (default-score (or gnus-summary-default-score 0))
5108         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5109         (building-line-count gnus-summary-display-while-building)
5110         (building-count (integerp gnus-summary-display-while-building))
5111         thread number subject stack state gnus-tmp-gathered beg-match
5112         new-roots gnus-tmp-new-adopts thread-end simp-subject
5113         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5114         gnus-tmp-replied gnus-tmp-subject-or-nil
5115         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5116         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5117         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5118         tree-stack)
5119
5120     (setq gnus-tmp-prev-subject nil
5121           gnus-tmp-thread-tree-header-string "")
5122
5123     (if (vectorp (car threads))
5124         ;; If this is a straight (sic) list of headers, then a
5125         ;; threaded summary display isn't required, so we just create
5126         ;; an unthreaded one.
5127         (gnus-summary-prepare-unthreaded threads)
5128
5129       ;; Do the threaded display.
5130
5131       (if gnus-summary-display-while-building
5132           (switch-to-buffer (buffer-name)))
5133       (while (or threads stack gnus-tmp-new-adopts new-roots)
5134
5135         (if (and (= gnus-tmp-level 0)
5136                  (or (not stack)
5137                      (= (caar stack) 0))
5138                  (not gnus-tmp-false-parent)
5139                  (or gnus-tmp-new-adopts new-roots))
5140             (if gnus-tmp-new-adopts
5141                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5142                       thread (list (car gnus-tmp-new-adopts))
5143                       gnus-tmp-header (caar thread)
5144                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5145               (when new-roots
5146                 (setq thread (list (car new-roots))
5147                       gnus-tmp-header (caar thread)
5148                       new-roots (cdr new-roots))))
5149
5150           (if threads
5151               ;; If there are some threads, we do them before the
5152               ;; threads on the stack.
5153               (setq thread threads
5154                     gnus-tmp-header (caar thread))
5155             ;; There were no current threads, so we pop something off
5156             ;; the stack.
5157             (setq state (car stack)
5158                   gnus-tmp-level (car state)
5159                   tree-stack (cadr state)
5160                   thread (caddr state)
5161                   stack (cdr stack)
5162                   gnus-tmp-header (caar thread))))
5163
5164         (setq gnus-tmp-false-parent nil)
5165         (setq gnus-tmp-root-expunged nil)
5166         (setq thread-end nil)
5167
5168         (if (stringp gnus-tmp-header)
5169             ;; The header is a dummy root.
5170             (cond
5171              ((eq gnus-summary-make-false-root 'adopt)
5172               ;; We let the first article adopt the rest.
5173               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5174                                                (cddar thread)))
5175               (setq gnus-tmp-gathered
5176                     (nconc (mapcar
5177                             (lambda (h) (mail-header-number (car h)))
5178                             (cddar thread))
5179                            gnus-tmp-gathered))
5180               (setq thread (cons (list (caar thread)
5181                                        (cadar thread))
5182                                  (cdr thread)))
5183               (setq gnus-tmp-level -1
5184                     gnus-tmp-false-parent t))
5185              ((eq gnus-summary-make-false-root 'empty)
5186               ;; We print adopted articles with empty subject fields.
5187               (setq gnus-tmp-gathered
5188                     (nconc (mapcar
5189                             (lambda (h) (mail-header-number (car h)))
5190                             (cddar thread))
5191                            gnus-tmp-gathered))
5192               (setq gnus-tmp-level -1))
5193              ((eq gnus-summary-make-false-root 'dummy)
5194               ;; We remember that we probably want to output a dummy
5195               ;; root.
5196               (setq gnus-tmp-dummy-line gnus-tmp-header)
5197               (setq gnus-tmp-prev-subject gnus-tmp-header))
5198              (t
5199               ;; We do not make a root for the gathered
5200               ;; sub-threads at all.
5201               (setq gnus-tmp-level -1)))
5202
5203           (setq number (mail-header-number gnus-tmp-header)
5204                 subject (mail-header-subject gnus-tmp-header)
5205                 simp-subject (gnus-simplify-subject-fully subject))
5206
5207           (cond
5208            ;; If the thread has changed subject, we might want to make
5209            ;; this subthread into a root.
5210            ((and (null gnus-thread-ignore-subject)
5211                  (not (zerop gnus-tmp-level))
5212                  gnus-tmp-prev-subject
5213                  (not (string= gnus-tmp-prev-subject simp-subject)))
5214             (setq new-roots (nconc new-roots (list (car thread)))
5215                   thread-end t
5216                   gnus-tmp-header nil))
5217            ;; If the article lies outside the current limit,
5218            ;; then we do not display it.
5219            ((not (memq number gnus-newsgroup-limit))
5220             (setq gnus-tmp-gathered
5221                   (nconc (mapcar
5222                           (lambda (h) (mail-header-number (car h)))
5223                           (cdar thread))
5224                          gnus-tmp-gathered))
5225             (setq gnus-tmp-new-adopts (if (cdar thread)
5226                                           (append gnus-tmp-new-adopts
5227                                                   (cdar thread))
5228                                         gnus-tmp-new-adopts)
5229                   thread-end t
5230                   gnus-tmp-header nil)
5231             (when (zerop gnus-tmp-level)
5232               (setq gnus-tmp-root-expunged t)))
5233            ;; Perhaps this article is to be marked as read?
5234            ((and gnus-summary-mark-below
5235                  (< (or (cdr (assq number gnus-newsgroup-scored))
5236                         default-score)
5237                     gnus-summary-mark-below)
5238                  ;; Don't touch sparse articles.
5239                  (not (gnus-summary-article-sparse-p number))
5240                  (not (gnus-summary-article-ancient-p number)))
5241             (setq gnus-newsgroup-unreads
5242                   (delq number gnus-newsgroup-unreads))
5243             (if gnus-newsgroup-auto-expire
5244                 (setq gnus-newsgroup-expirable
5245                       (gnus-add-to-sorted-list
5246                        gnus-newsgroup-expirable number))
5247               (push (cons number gnus-low-score-mark)
5248                     gnus-newsgroup-reads))))
5249
5250           (when gnus-tmp-header
5251             ;; We may have an old dummy line to output before this
5252             ;; article.
5253             (when (and gnus-tmp-dummy-line
5254                        (gnus-subject-equal
5255                         gnus-tmp-dummy-line
5256                         (mail-header-subject gnus-tmp-header)))
5257               (gnus-summary-insert-dummy-line
5258                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5259               (setq gnus-tmp-dummy-line nil))
5260
5261             ;; Compute the mark.
5262             (setq gnus-tmp-unread (gnus-article-mark number))
5263
5264             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5265                                   gnus-tmp-header gnus-tmp-level)
5266                   gnus-newsgroup-data)
5267
5268             ;; Actually insert the line.
5269             (setq
5270              gnus-tmp-subject-or-nil
5271              (cond
5272               ((and gnus-thread-ignore-subject
5273                     gnus-tmp-prev-subject
5274                     (not (string= gnus-tmp-prev-subject simp-subject)))
5275                subject)
5276               ((zerop gnus-tmp-level)
5277                (if (and (eq gnus-summary-make-false-root 'empty)
5278                         (memq number gnus-tmp-gathered)
5279                         gnus-tmp-prev-subject
5280                         (string= gnus-tmp-prev-subject simp-subject))
5281                    gnus-summary-same-subject
5282                  subject))
5283               (t gnus-summary-same-subject)))
5284             (if (and (eq gnus-summary-make-false-root 'adopt)
5285                      (= gnus-tmp-level 1)
5286                      (memq number gnus-tmp-gathered))
5287                 (setq gnus-tmp-opening-bracket ?\<
5288                       gnus-tmp-closing-bracket ?\>)
5289               (setq gnus-tmp-opening-bracket ?\[
5290                     gnus-tmp-closing-bracket ?\]))
5291             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5292                 (gnus-make-thread-indent-array
5293                  (max (* 2 (length gnus-thread-indent-array))
5294                       gnus-tmp-level)))
5295             (setq
5296              gnus-tmp-indentation
5297              (aref gnus-thread-indent-array gnus-tmp-level)
5298              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5299              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5300                                 gnus-summary-default-score 0)
5301              gnus-tmp-score-char
5302              (if (or (null gnus-summary-default-score)
5303                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5304                          gnus-summary-zcore-fuzz))
5305                  ?                      ;Whitespace
5306                (if (< gnus-tmp-score gnus-summary-default-score)
5307                    gnus-score-below-mark gnus-score-over-mark))
5308              gnus-tmp-replied
5309              (cond ((memq number gnus-newsgroup-processable)
5310                     gnus-process-mark)
5311                    ((memq number gnus-newsgroup-cached)
5312                     gnus-cached-mark)
5313                    ((memq number gnus-newsgroup-replied)
5314                     gnus-replied-mark)
5315                    ((memq number gnus-newsgroup-forwarded)
5316                     gnus-forwarded-mark)
5317                    ((memq number gnus-newsgroup-saved)
5318                     gnus-saved-mark)
5319                    ((memq number gnus-newsgroup-recent)
5320                     gnus-recent-mark)
5321                    ((memq number gnus-newsgroup-unseen)
5322                     gnus-unseen-mark)
5323                    (t gnus-no-mark))
5324              gnus-tmp-downloaded
5325              (cond ((memq number gnus-newsgroup-undownloaded)
5326                     gnus-undownloaded-mark)
5327                    (gnus-newsgroup-agentized
5328                     gnus-downloaded-mark)
5329                    (t
5330                     gnus-no-mark))
5331              gnus-tmp-from (mail-header-from gnus-tmp-header)
5332              gnus-tmp-name
5333              (cond
5334               ((string-match "<[^>]+> *$" gnus-tmp-from)
5335                (setq beg-match (match-beginning 0))
5336                (or (and (string-match "^\".+\"" gnus-tmp-from)
5337                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5338                    (substring gnus-tmp-from 0 beg-match)))
5339               ((string-match "(.+)" gnus-tmp-from)
5340                (substring gnus-tmp-from
5341                           (1+ (match-beginning 0)) (1- (match-end 0))))
5342               (t gnus-tmp-from))
5343
5344              ;; Do the %B string
5345              gnus-tmp-thread-tree-header-string
5346              (cond
5347               ((not gnus-show-threads) "")
5348               ((zerop gnus-tmp-level)
5349                (cond ((cdar thread)
5350                       (or gnus-sum-thread-tree-root subject))
5351                      (gnus-tmp-new-adopts
5352                       (or gnus-sum-thread-tree-false-root subject))
5353                      (t
5354                       (or gnus-sum-thread-tree-single-indent subject))))
5355               (t
5356                (concat (apply 'concat
5357                               (mapcar (lambda (item)
5358                                         (if (= item 1)
5359                                             gnus-sum-thread-tree-vertical
5360                                           gnus-sum-thread-tree-indent))
5361                                       (cdr (reverse tree-stack))))
5362                        (if (nth 1 thread)
5363                            gnus-sum-thread-tree-leaf-with-other
5364                          gnus-sum-thread-tree-single-leaf)))))
5365             (when (string= gnus-tmp-name "")
5366               (setq gnus-tmp-name gnus-tmp-from))
5367             (unless (numberp gnus-tmp-lines)
5368               (setq gnus-tmp-lines -1))
5369             (if (= gnus-tmp-lines -1)
5370                 (setq gnus-tmp-lines "?")
5371               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5372             (gnus-put-text-property
5373              (point)
5374              (progn (eval gnus-summary-line-format-spec) (point))
5375              'gnus-number number)
5376             (when gnus-visual-p
5377               (forward-line -1)
5378               (gnus-summary-highlight-line)
5379               (when gnus-summary-update-hook
5380                 (gnus-run-hooks 'gnus-summary-update-hook))
5381               (forward-line 1))
5382
5383             (setq gnus-tmp-prev-subject simp-subject)))
5384
5385         (when (nth 1 thread)
5386           (push (list (max 0 gnus-tmp-level)
5387                       (copy-sequence tree-stack)
5388                       (nthcdr 1 thread))
5389                 stack))
5390         (push (if (nth 1 thread) 1 0) tree-stack)
5391         (incf gnus-tmp-level)
5392         (setq threads (if thread-end nil (cdar thread)))
5393         (if gnus-summary-display-while-building
5394             (if building-count
5395                 (progn
5396                   ;; use a set frequency
5397                   (setq building-line-count (1- building-line-count))
5398                   (when (= building-line-count 0)
5399                     (sit-for 0)
5400                     (setq building-line-count
5401                           gnus-summary-display-while-building)))
5402               ;; always
5403               (sit-for 0)))
5404         (unless threads
5405           (setq gnus-tmp-level 0)))))
5406   (gnus-message 7 "Generating summary...done"))
5407
5408 (defun gnus-summary-prepare-unthreaded (headers)
5409   "Generate an unthreaded summary buffer based on HEADERS."
5410   (let (header number mark)
5411
5412     (beginning-of-line)
5413
5414     (while headers
5415       ;; We may have to root out some bad articles...
5416       (when (memq (setq number (mail-header-number
5417                                 (setq header (pop headers))))
5418                   gnus-newsgroup-limit)
5419         ;; Mark article as read when it has a low score.
5420         (when (and gnus-summary-mark-below
5421                    (< (or (cdr (assq number gnus-newsgroup-scored))
5422                           gnus-summary-default-score 0)
5423                       gnus-summary-mark-below)
5424                    (not (gnus-summary-article-ancient-p number)))
5425           (setq gnus-newsgroup-unreads
5426                 (delq number gnus-newsgroup-unreads))
5427           (if gnus-newsgroup-auto-expire
5428               (push number gnus-newsgroup-expirable)
5429             (push (cons number gnus-low-score-mark)
5430                   gnus-newsgroup-reads)))
5431
5432         (setq mark (gnus-article-mark number))
5433         (push (gnus-data-make number mark (1+ (point)) header 0)
5434               gnus-newsgroup-data)
5435         (gnus-summary-insert-line
5436          header 0 number
5437          (memq number gnus-newsgroup-undownloaded)
5438          mark (memq number gnus-newsgroup-replied)
5439          (memq number gnus-newsgroup-expirable)
5440          (mail-header-subject header) nil
5441          (cdr (assq number gnus-newsgroup-scored))
5442          (memq number gnus-newsgroup-processable))))))
5443
5444 (defun gnus-summary-remove-list-identifiers ()
5445   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5446   (let ((regexp (if (consp gnus-list-identifiers)
5447                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5448                   gnus-list-identifiers))
5449         changed subject)
5450     (when regexp
5451       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5452       (dolist (header gnus-newsgroup-headers)
5453         (setq subject (mail-header-subject header)
5454               changed nil)
5455         (while (string-match regexp subject)
5456           (setq subject
5457                 (concat (substring subject 0 (match-beginning 1))
5458                         (substring subject (match-end 0)))
5459                 changed t))
5460         (when changed
5461           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5462             (setq subject
5463                   (concat (substring subject 0 (match-beginning 1))
5464                           (substring subject (match-end 1)))))
5465           (mail-header-set-subject header subject))))))
5466
5467 (defun gnus-fetch-headers (articles)
5468   "Fetch headers of ARTICLES."
5469   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5470     (gnus-message 5 "Fetching headers for %s..." name)
5471     (prog1
5472         (if (eq 'nov
5473                 (setq gnus-headers-retrieved-by
5474                       (gnus-retrieve-headers
5475                        articles gnus-newsgroup-name
5476                        ;; We might want to fetch old headers, but
5477                        ;; not if there is only 1 article.
5478                        (and (or (and
5479                                  (not (eq gnus-fetch-old-headers 'some))
5480                                  (not (numberp gnus-fetch-old-headers)))
5481                                 (> (length articles) 1))
5482                             gnus-fetch-old-headers))))
5483             (gnus-get-newsgroup-headers-xover
5484              articles nil nil gnus-newsgroup-name t)
5485           (gnus-get-newsgroup-headers))
5486       (gnus-message 5 "Fetching headers for %s...done" name))))
5487
5488 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5489   "Select newsgroup GROUP.
5490 If READ-ALL is non-nil, all articles in the group are selected.
5491 If SELECT-ARTICLES, only select those articles from GROUP."
5492   (let* ((entry (gnus-group-entry group))
5493          ;;!!! Dirty hack; should be removed.
5494          (gnus-summary-ignore-duplicates
5495           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5496               t
5497             gnus-summary-ignore-duplicates))
5498          (info (nth 2 entry))
5499          charset articles fetched-articles cached)
5500
5501     (unless (gnus-check-server
5502              (set (make-local-variable 'gnus-current-select-method)
5503                   (gnus-find-method-for-group group)))
5504       (error "Couldn't open server"))
5505     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5506
5507     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5508         (gnus-activate-group group)     ; Or we can activate it...
5509         (progn                          ; Or we bug out.
5510           (when (equal major-mode 'gnus-summary-mode)
5511             (gnus-kill-buffer (current-buffer)))
5512           (error
5513            "Couldn't activate group %s: %s"
5514            (mm-decode-coding-string group charset)
5515            (mm-decode-coding-string (gnus-status-message group) charset))))
5516
5517     (unless (gnus-request-group group t)
5518       (when (equal major-mode 'gnus-summary-mode)
5519         (gnus-kill-buffer (current-buffer)))
5520       (error "Couldn't request group %s: %s"
5521              (mm-decode-coding-string group charset)
5522              (mm-decode-coding-string (gnus-status-message group) charset)))
5523
5524     (when gnus-agent
5525       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5526
5527       (setq gnus-summary-use-undownloaded-faces
5528             (gnus-agent-find-parameter
5529              group
5530              'agent-enable-undownloaded-faces)))
5531
5532     (setq gnus-newsgroup-name group
5533           gnus-newsgroup-unselected nil
5534           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5535
5536     (let ((display (gnus-group-find-parameter group 'display)))
5537       (setq gnus-newsgroup-display
5538             (cond
5539              ((not (zerop (or (car-safe read-all) 0)))
5540               ;; The user entered the group with C-u SPC/RET, let's show
5541               ;; all articles.
5542               'gnus-not-ignore)
5543              ((eq display 'all)
5544               'gnus-not-ignore)
5545              ((arrayp display)
5546               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5547              ((numberp display)
5548               ;; The following is probably the "correct" solution, but
5549               ;; it makes Gnus fetch all headers and then limit the
5550               ;; articles (which is slow), so instead we hack the
5551               ;; select-articles parameter instead. -- Simon Josefsson
5552               ;; <jas@kth.se>
5553               ;;
5554               ;; (gnus-byte-compile
5555               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5556               ;;                         display)))))
5557               (setq select-articles
5558                     (gnus-uncompress-range
5559                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5560                              (if (> tmp 0)
5561                                  tmp
5562                                1))
5563                            (cdr (gnus-active group)))))
5564               nil)
5565              (t
5566               nil))))
5567
5568     (gnus-summary-setup-default-charset)
5569
5570     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5571     (when (gnus-virtual-group-p group)
5572       (setq cached gnus-newsgroup-cached))
5573
5574     (setq gnus-newsgroup-unreads
5575           (gnus-sorted-ndifference
5576            (gnus-sorted-ndifference gnus-newsgroup-unreads
5577                                     gnus-newsgroup-marked)
5578            gnus-newsgroup-dormant))
5579
5580     (setq gnus-newsgroup-processable nil)
5581
5582     (gnus-update-read-articles group gnus-newsgroup-unreads)
5583
5584     ;; Adjust and set lists of article marks.
5585     (when info
5586       (gnus-adjust-marked-articles info))
5587     (if (setq articles select-articles)
5588         (setq gnus-newsgroup-unselected
5589               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5590       (setq articles (gnus-articles-to-read group read-all)))
5591
5592     (cond
5593      ((null articles)
5594       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5595       'quit)
5596      ((eq articles 0) nil)
5597      (t
5598       ;; Init the dependencies hash table.
5599       (setq gnus-newsgroup-dependencies
5600             (gnus-make-hashtable (length articles)))
5601       (gnus-set-global-variables)
5602       ;; Retrieve the headers and read them in.
5603
5604       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5605
5606       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5607       (when cached
5608         (setq gnus-newsgroup-cached cached))
5609
5610       ;; Suppress duplicates?
5611       (when gnus-suppress-duplicates
5612         (gnus-dup-suppress-articles))
5613
5614       ;; Set the initial limit.
5615       (setq gnus-newsgroup-limit (copy-sequence articles))
5616       ;; Remove canceled articles from the list of unread articles.
5617       (setq fetched-articles
5618             (mapcar (lambda (headers) (mail-header-number headers))
5619                     gnus-newsgroup-headers))
5620       (setq gnus-newsgroup-articles fetched-articles)
5621       (setq gnus-newsgroup-unreads
5622             (gnus-sorted-nintersection
5623              gnus-newsgroup-unreads fetched-articles))
5624       (gnus-compute-unseen-list)
5625
5626       ;; Removed marked articles that do not exist.
5627       (gnus-update-missing-marks
5628        (gnus-sorted-difference articles fetched-articles))
5629       ;; We might want to build some more threads first.
5630       (when (and gnus-fetch-old-headers
5631                  (eq gnus-headers-retrieved-by 'nov))
5632         (if (eq gnus-fetch-old-headers 'invisible)
5633             (gnus-build-all-threads)
5634           (gnus-build-old-threads)))
5635       ;; Let the Gnus agent mark articles as read.
5636       (when gnus-agent
5637         (gnus-agent-get-undownloaded-list))
5638       ;; Remove list identifiers from subject
5639       (when gnus-list-identifiers
5640         (gnus-summary-remove-list-identifiers))
5641       ;; Check whether auto-expire is to be done in this group.
5642       (setq gnus-newsgroup-auto-expire
5643             (gnus-group-auto-expirable-p group))
5644       ;; Set up the article buffer now, if necessary.
5645       (unless (and gnus-single-article-buffer
5646                    (equal gnus-article-buffer "*Article*"))
5647         (gnus-article-setup-buffer))
5648       ;; First and last article in this newsgroup.
5649       (when gnus-newsgroup-headers
5650         (setq gnus-newsgroup-begin
5651               (mail-header-number (car gnus-newsgroup-headers))
5652               gnus-newsgroup-end
5653               (mail-header-number
5654                (gnus-last-element gnus-newsgroup-headers))))
5655       ;; GROUP is successfully selected.
5656       (or gnus-newsgroup-headers t)))))
5657
5658 (defun gnus-compute-unseen-list ()
5659   ;; The `seen' marks are treated specially.
5660   (if (not gnus-newsgroup-seen)
5661       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5662     (setq gnus-newsgroup-unseen
5663           (gnus-inverse-list-range-intersection
5664            gnus-newsgroup-articles gnus-newsgroup-seen))))
5665
5666 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5667
5668 (defun gnus-summary-display-make-predicate (display)
5669   (require 'gnus-agent)
5670   (when (= (length display) 1)
5671     (setq display (car display)))
5672   (unless gnus-summary-display-cache
5673     (dolist (elem (append '((unread . unread)
5674                             (read . read)
5675                             (unseen . unseen))
5676                           gnus-article-mark-lists))
5677       (push (cons (cdr elem)
5678                   (gnus-byte-compile
5679                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5680             gnus-summary-display-cache)))
5681   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5682         (gnus-category-predicate-cache gnus-summary-display-cache))
5683     (gnus-get-predicate display)))
5684
5685 ;; Uses the dynamically bound `number' variable.
5686 (defvar number)
5687 (defun gnus-article-marked-p (type &optional article)
5688   (let ((article (or article number)))
5689     (cond
5690      ((eq type 'tick)
5691       (memq article gnus-newsgroup-marked))
5692      ((eq type 'spam)
5693       (memq article gnus-newsgroup-spam-marked))
5694      ((eq type 'unsend)
5695       (memq article gnus-newsgroup-unsendable))
5696      ((eq type 'undownload)
5697       (memq article gnus-newsgroup-undownloaded))
5698      ((eq type 'download)
5699       (memq article gnus-newsgroup-downloadable))
5700      ((eq type 'unread)
5701       (memq article gnus-newsgroup-unreads))
5702      ((eq type 'read)
5703       (memq article gnus-newsgroup-reads))
5704      ((eq type 'dormant)
5705       (memq article gnus-newsgroup-dormant) )
5706      ((eq type 'expire)
5707       (memq article gnus-newsgroup-expirable))
5708      ((eq type 'reply)
5709       (memq article gnus-newsgroup-replied))
5710      ((eq type 'killed)
5711       (memq article gnus-newsgroup-killed))
5712      ((eq type 'bookmark)
5713       (assq article gnus-newsgroup-bookmarks))
5714      ((eq type 'score)
5715       (assq article gnus-newsgroup-scored))
5716      ((eq type 'save)
5717       (memq article gnus-newsgroup-saved))
5718      ((eq type 'cache)
5719       (memq article gnus-newsgroup-cached))
5720      ((eq type 'forward)
5721       (memq article gnus-newsgroup-forwarded))
5722      ((eq type 'seen)
5723       (not (memq article gnus-newsgroup-unseen)))
5724      ((eq type 'recent)
5725       (memq article gnus-newsgroup-recent))
5726      (t t))))
5727
5728 (defun gnus-articles-to-read (group &optional read-all)
5729   "Find out what articles the user wants to read."
5730   (let* ((articles
5731           ;; Select all articles if `read-all' is non-nil, or if there
5732           ;; are no unread articles.
5733           (if (or read-all
5734                   (and (zerop (length gnus-newsgroup-marked))
5735                        (zerop (length gnus-newsgroup-unreads)))
5736                   ;; Fetch all if the predicate is non-nil.
5737                   gnus-newsgroup-display)
5738               ;; We want to select the headers for all the articles in
5739               ;; the group, so we select either all the active
5740               ;; articles in the group, or (if that's nil), the
5741               ;; articles in the cache.
5742               (or
5743                (if gnus-newsgroup-maximum-articles
5744                    (let ((active (gnus-active group)))
5745                      (gnus-uncompress-range
5746                       (cons (max (car active)
5747                                  (- (cdr active)
5748                                     gnus-newsgroup-maximum-articles
5749                                     -1))
5750                             (cdr active))))
5751                  (gnus-uncompress-range (gnus-active group)))
5752                (gnus-cache-articles-in-group group))
5753             ;; Select only the "normal" subset of articles.
5754             (gnus-sorted-nunion
5755              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5756              gnus-newsgroup-unreads)))
5757          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5758          (scored (length scored-list))
5759          (number (length articles))
5760          (marked (+ (length gnus-newsgroup-marked)
5761                     (length gnus-newsgroup-dormant)))
5762          (select
5763           (cond
5764            ((numberp read-all)
5765             read-all)
5766            ((numberp gnus-newsgroup-display)
5767             gnus-newsgroup-display)
5768            (t
5769             (condition-case ()
5770                 (cond
5771                  ((and (or (<= scored marked) (= scored number))
5772                        (numberp gnus-large-newsgroup)
5773                        (> number gnus-large-newsgroup))
5774                   (let* ((cursor-in-echo-area nil)
5775                          (initial (gnus-parameter-large-newsgroup-initial
5776                                    gnus-newsgroup-name))
5777                          (input
5778                           (read-string
5779                            (format
5780                             "How many articles from %s (%s %d): "
5781                             (gnus-group-decoded-name gnus-newsgroup-name)
5782                             (if initial "max" "default")
5783                             number)
5784                            (if initial
5785                                (cons (number-to-string initial)
5786                                      0)))))
5787                     (if (string-match "^[ \t]*$" input) number input)))
5788                  ((and (> scored marked) (< scored number)
5789                        (> (- scored number) 20))
5790                   (let ((input
5791                          (read-string
5792                           (format "%s %s (%d scored, %d total): "
5793                                   "How many articles from"
5794                                   (gnus-group-decoded-name group)
5795                                   scored number))))
5796                     (if (string-match "^[ \t]*$" input)
5797                         number input)))
5798                  (t number))
5799               (quit
5800                (message "Quit getting the articles to read")
5801                nil))))))
5802     (setq select (if (stringp select) (string-to-number select) select))
5803     (if (or (null select) (zerop select))
5804         select
5805       (if (and (not (zerop scored)) (<= (abs select) scored))
5806           (progn
5807             (setq articles (sort scored-list '<))
5808             (setq number (length articles)))
5809         (setq articles (copy-sequence articles)))
5810
5811       (when (< (abs select) number)
5812         (if (< select 0)
5813             ;; Select the N oldest articles.
5814             (setcdr (nthcdr (1- (abs select)) articles) nil)
5815           ;; Select the N most recent articles.
5816           (setq articles (nthcdr (- number select) articles))))
5817       (setq gnus-newsgroup-unselected
5818             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5819       (when gnus-alter-articles-to-read-function
5820         (setq articles
5821               (sort
5822                (funcall gnus-alter-articles-to-read-function
5823                         gnus-newsgroup-name articles)
5824                '<)))
5825       articles)))
5826
5827 (defun gnus-killed-articles (killed articles)
5828   (let (out)
5829     (while articles
5830       (when (inline (gnus-member-of-range (car articles) killed))
5831         (push (car articles) out))
5832       (setq articles (cdr articles)))
5833     out))
5834
5835 (defun gnus-uncompress-marks (marks)
5836   "Uncompress the mark ranges in MARKS."
5837   (let ((uncompressed '(score bookmark))
5838         out)
5839     (while marks
5840       (if (memq (caar marks) uncompressed)
5841           (push (car marks) out)
5842         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5843       (setq marks (cdr marks)))
5844     out))
5845
5846 (defun gnus-article-mark-to-type (mark)
5847   "Return the type of MARK."
5848   (or (cadr (assq mark gnus-article-special-mark-lists))
5849       'list))
5850
5851 (defun gnus-article-unpropagatable-p (mark)
5852   "Return whether MARK should be propagated to back end."
5853   (memq mark gnus-article-unpropagated-mark-lists))
5854
5855 (defun gnus-adjust-marked-articles (info)
5856   "Set all article lists and remove all marks that are no longer valid."
5857   (let* ((marked-lists (gnus-info-marks info))
5858          (active (gnus-active (gnus-info-group info)))
5859          (min (car active))
5860          (max (cdr active))
5861          (types gnus-article-mark-lists)
5862          marks var articles article mark mark-type
5863          bgn end)
5864     ;; Hack to avoid adjusting marks for imap.
5865     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5866               'nnimap)
5867       (setq min 1))
5868
5869     (dolist (marks marked-lists)
5870       (setq mark (car marks)
5871             mark-type (gnus-article-mark-to-type mark)
5872             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5873
5874       ;; We set the variable according to the type of the marks list,
5875       ;; and then adjust the marks to a subset of the active articles.
5876       (cond
5877        ;; Adjust "simple" lists - compressed yet unsorted
5878        ((eq mark-type 'list)
5879         ;; Simultaneously uncompress and clip to active range
5880         ;; See gnus-uncompress-range for a description of possible marks
5881         (let (l lh)
5882           (if (not (cadr marks))
5883               (set var nil)
5884             (setq articles (if (numberp (cddr marks))
5885                                (list (cdr marks))
5886                              (cdr marks))
5887                   lh (cons nil nil)
5888                   l lh)
5889
5890             (while (setq article (pop articles))
5891               (cond ((consp article)
5892                      (setq bgn (max (car article) min)
5893                            end (min (cdr article) max))
5894                      (while (<= bgn end)
5895                        (setq l (setcdr l (cons bgn nil))
5896                              bgn (1+ bgn))))
5897                     ((and (<= min article)
5898                           (>= max article))
5899                      (setq l (setcdr l (cons article nil))))))
5900             (set var (cdr lh)))))
5901        ;; Adjust assocs.
5902        ((eq mark-type 'tuple)
5903         (set var (setq articles (cdr marks)))
5904         (when (not (listp (cdr (symbol-value var))))
5905           (set var (list (symbol-value var))))
5906         (when (not (listp (cdr articles)))
5907           (setq articles (list articles)))
5908         (while articles
5909           (when (or (not (consp (setq article (pop articles))))
5910                     (< (car article) min)
5911                     (> (car article) max))
5912             (set var (delq article (symbol-value var))))))
5913        ;; Adjust ranges (sloppily).
5914        ((eq mark-type 'range)
5915         (cond
5916          ((eq mark 'seen)
5917           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5918           ;; It should be (seen (NUM1 . NUM2)).
5919           (when (numberp (cddr marks))
5920             (setcdr marks (list (cdr marks))))
5921           (setq articles (cdr marks))
5922           (while (and articles
5923                       (or (and (consp (car articles))
5924                                (> min (cdar articles)))
5925                           (and (numberp (car articles))
5926                                (> min (car articles)))))
5927             (pop articles))
5928           (set var articles))))))))
5929
5930 (defun gnus-update-missing-marks (missing)
5931   "Go through the list of MISSING articles and remove them from the mark lists."
5932   (when missing
5933     (let (var m)
5934       ;; Go through all types.
5935       (dolist (elem gnus-article-mark-lists)
5936         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5937           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5938           (when (symbol-value var)
5939             ;; This list has articles.  So we delete all missing
5940             ;; articles from it.
5941             (setq m missing)
5942             (while m
5943               (set var (delq (pop m) (symbol-value var))))))))))
5944
5945 (defun gnus-update-marks ()
5946   "Enter the various lists of marked articles into the newsgroup info list."
5947   (let ((types gnus-article-mark-lists)
5948         (info (gnus-get-info gnus-newsgroup-name))
5949         type list newmarked symbol delta-marks)
5950     (when info
5951       ;; Add all marks lists to the list of marks lists.
5952       (while (setq type (pop types))
5953         (setq list (symbol-value
5954                     (setq symbol
5955                           (intern (format "gnus-newsgroup-%s" (car type))))))
5956
5957         (when list
5958           ;; Get rid of the entries of the articles that have the
5959           ;; default score.
5960           (when (and (eq (cdr type) 'score)
5961                      gnus-save-score
5962                      list)
5963             (let* ((arts list)
5964                    (prev (cons nil list))
5965                    (all prev))
5966               (while arts
5967                 (if (or (not (consp (car arts)))
5968                         (= (cdar arts) gnus-summary-default-score))
5969                     (setcdr prev (cdr arts))
5970                   (setq prev arts))
5971                 (setq arts (cdr arts)))
5972               (setq list (cdr all)))))
5973
5974         (when (eq (cdr type) 'seen)
5975           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5976
5977         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5978           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5979
5980         (when (and (gnus-check-backend-function
5981                     'request-set-mark gnus-newsgroup-name)
5982                    (not (gnus-article-unpropagatable-p (cdr type))))
5983           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5984                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5985                  (add (gnus-remove-from-range
5986                        (gnus-copy-sequence list) old)))
5987             (when add
5988               (push (list add 'add (list (cdr type))) delta-marks))
5989             (when del
5990               ;; Don't delete marks from outside the active range.  This
5991               ;; shouldn't happen, but is a sanity check.
5992               (setq del (gnus-sorted-range-intersection
5993                          (gnus-active gnus-newsgroup-name) del))
5994               (push (list del 'del (list (cdr type))) delta-marks))))
5995
5996         (when list
5997           (push (cons (cdr type) list) newmarked)))
5998
5999       (when delta-marks
6000         (unless (gnus-check-group gnus-newsgroup-name)
6001           (error "Can't open server for %s" gnus-newsgroup-name))
6002         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6003
6004       ;; Enter these new marks into the info of the group.
6005       (if (nthcdr 3 info)
6006           (setcar (nthcdr 3 info) newmarked)
6007         ;; Add the marks lists to the end of the info.
6008         (when newmarked
6009           (setcdr (nthcdr 2 info) (list newmarked))))
6010
6011       ;; Cut off the end of the info if there's nothing else there.
6012       (let ((i 5))
6013         (while (and (> i 2)
6014                     (not (nth i info)))
6015           (when (nthcdr (decf i) info)
6016             (setcdr (nthcdr i info) nil)))))))
6017
6018 (defun gnus-set-mode-line (where)
6019   "Set the mode line of the article or summary buffers.
6020 If WHERE is `summary', the summary mode line format will be used."
6021   ;; Is this mode line one we keep updated?
6022   (when (and (memq where gnus-updated-mode-lines)
6023              (symbol-value
6024               (intern (format "gnus-%s-mode-line-format-spec" where))))
6025     (let (mode-string)
6026       ;; We evaluate this in the summary buffer since these
6027       ;; variables are buffer-local to that buffer.
6028       (with-current-buffer gnus-summary-buffer
6029         ;; We bind all these variables that are used in the `eval' form
6030         ;; below.
6031         (let* ((mformat (symbol-value
6032                          (intern
6033                           (format "gnus-%s-mode-line-format-spec" where))))
6034                (gnus-tmp-group-name (gnus-mode-string-quote
6035                                      (gnus-group-decoded-name
6036                                       gnus-newsgroup-name)))
6037                (gnus-tmp-article-number (or gnus-current-article 0))
6038                (gnus-tmp-unread gnus-newsgroup-unreads)
6039                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6040                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6041                (gnus-tmp-unread-and-unselected
6042                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6043                             (zerop gnus-tmp-unselected))
6044                        "")
6045                       ((zerop gnus-tmp-unselected)
6046                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6047                       (t (format "{%d(+%d) more}"
6048                                  gnus-tmp-unread-and-unticked
6049                                  gnus-tmp-unselected))))
6050                (gnus-tmp-subject
6051                 (if (and gnus-current-headers
6052                          (vectorp gnus-current-headers))
6053                     (gnus-mode-string-quote
6054                      (mail-header-subject gnus-current-headers))
6055                   ""))
6056                bufname-length max-len
6057                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6058           (setq mode-string (eval mformat))
6059           (setq bufname-length (if (string-match "%b" mode-string)
6060                                    (- (length
6061                                        (buffer-name
6062                                         (if (eq where 'summary)
6063                                             nil
6064                                           (get-buffer gnus-article-buffer))))
6065                                       2)
6066                                  0))
6067           (setq max-len (max 4 (if gnus-mode-non-string-length
6068                                    (- (window-width)
6069                                       gnus-mode-non-string-length
6070                                       bufname-length)
6071                                  (length mode-string))))
6072           ;; We might have to chop a bit of the string off...
6073           (when (> (length mode-string) max-len)
6074             (setq mode-string
6075                   (concat (truncate-string-to-width mode-string (- max-len 3))
6076                           "...")))))
6077       ;; Update the mode line.
6078       (setq mode-line-buffer-identification
6079             (gnus-mode-line-buffer-identification (list mode-string)))
6080       (set-buffer-modified-p t))))
6081
6082 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6083   "Go through the HEADERS list and add all Xrefs to a hash table.
6084 The resulting hash table is returned, or nil if no Xrefs were found."
6085   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6086          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6087          (xref-hashtb (gnus-make-hashtable))
6088          start group entry number xrefs header)
6089     (while headers
6090       (setq header (pop headers))
6091       (when (and (setq xrefs (mail-header-xref header))
6092                  (not (memq (setq number (mail-header-number header))
6093                             unreads)))
6094         (setq start 0)
6095         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6096           (setq start (match-end 0))
6097           (setq group (if prefix
6098                           (concat prefix (substring xrefs (match-beginning 1)
6099                                                     (match-end 1)))
6100                         (substring xrefs (match-beginning 1) (match-end 1))))
6101           (setq number
6102                 (string-to-number (substring xrefs (match-beginning 2)
6103                                           (match-end 2))))
6104           (if (setq entry (gnus-gethash group xref-hashtb))
6105               (setcdr entry (cons number (cdr entry)))
6106             (gnus-sethash group (cons number nil) xref-hashtb)))))
6107     (and start xref-hashtb)))
6108
6109 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6110   "Look through all the headers and mark the Xrefs as read."
6111   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6112         name info xref-hashtb idlist method nth4)
6113     (with-current-buffer gnus-group-buffer
6114       (when (setq xref-hashtb
6115                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6116         (mapatoms
6117          (lambda (group)
6118            (unless (string= from-newsgroup (setq name (symbol-name group)))
6119              (setq idlist (symbol-value group))
6120              ;; Dead groups are not updated.
6121              (and (prog1
6122                       (setq info (gnus-get-info name))
6123                     (when (stringp (setq nth4 (gnus-info-method info)))
6124                       (setq nth4 (gnus-server-to-method nth4))))
6125                   ;; Only do the xrefs if the group has the same
6126                   ;; select method as the group we have just read.
6127                   (or (gnus-methods-equal-p
6128                        nth4 (gnus-find-method-for-group from-newsgroup))
6129                       virtual
6130                       (equal nth4 (setq method (gnus-find-method-for-group
6131                                                 from-newsgroup)))
6132                       (and (equal (car nth4) (car method))
6133                            (equal (nth 1 nth4) (nth 1 method))))
6134                   gnus-use-cross-reference
6135                   (or (not (eq gnus-use-cross-reference t))
6136                       virtual
6137                       ;; Only do cross-references on subscribed
6138                       ;; groups, if that is what is wanted.
6139                       (<= (gnus-info-level info) gnus-level-subscribed))
6140                   (gnus-group-make-articles-read name idlist))))
6141          xref-hashtb)))))
6142
6143 (defun gnus-compute-read-articles (group articles)
6144   (let* ((entry (gnus-group-entry group))
6145          (info (nth 2 entry))
6146          (active (gnus-active group))
6147          ninfo)
6148     (when entry
6149       ;; First peel off all invalid article numbers.
6150       (when active
6151         (let ((ids articles)
6152               id first)
6153           (while (setq id (pop ids))
6154             (when (and first (> id (cdr active)))
6155               ;; We'll end up in this situation in one particular
6156               ;; obscure situation.  If you re-scan a group and get
6157               ;; a new article that is cross-posted to a different
6158               ;; group that has not been re-scanned, you might get
6159               ;; crossposted article that has a higher number than
6160               ;; Gnus believes possible.  So we re-activate this
6161               ;; group as well.  This might mean doing the
6162               ;; crossposting thingy will *increase* the number
6163               ;; of articles in some groups.  Tsk, tsk.
6164               (setq active (or (gnus-activate-group group) active)))
6165             (when (or (> id (cdr active))
6166                       (< id (car active)))
6167               (setq articles (delq id articles))))))
6168       ;; If the read list is nil, we init it.
6169       (if (and active
6170                (null (gnus-info-read info))
6171                (> (car active) 1))
6172           (setq ninfo (cons 1 (1- (car active))))
6173         (setq ninfo (gnus-info-read info)))
6174       ;; Then we add the read articles to the range.
6175       (gnus-add-to-range
6176        ninfo (setq articles (sort articles '<))))))
6177
6178 (defun gnus-group-make-articles-read (group articles)
6179   "Update the info of GROUP to say that ARTICLES are read."
6180   (let* ((num 0)
6181          (entry (gnus-group-entry group))
6182          (info (nth 2 entry))
6183          (active (gnus-active group))
6184          range)
6185     (when entry
6186       (setq range (gnus-compute-read-articles group articles))
6187       (with-current-buffer gnus-group-buffer
6188         (gnus-undo-register
6189           `(progn
6190              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6191              (gnus-info-set-read ',info ',(gnus-info-read info))
6192              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6193              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6194              (gnus-group-update-group ,group t))))
6195       ;; Add the read articles to the range.
6196       (gnus-info-set-read info range)
6197       (gnus-request-set-mark group (list (list range 'add '(read))))
6198       ;; Then we have to re-compute how many unread
6199       ;; articles there are in this group.
6200       (when active
6201         (cond
6202          ((not range)
6203           (setq num (- (1+ (cdr active)) (car active))))
6204          ((not (listp (cdr range)))
6205           (setq num (- (cdr active) (- (1+ (cdr range))
6206                                        (car range)))))
6207          (t
6208           (while range
6209             (if (numberp (car range))
6210                 (setq num (1+ num))
6211               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6212             (setq range (cdr range)))
6213           (setq num (- (cdr active) num))))
6214         ;; Update the number of unread articles.
6215         (setcar entry num)
6216         ;; Update the group buffer.
6217         (unless (gnus-ephemeral-group-p group)
6218           (gnus-group-update-group group t))))))
6219
6220 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6221   (let ((cur nntp-server-buffer)
6222         (dependencies
6223          (or dependencies
6224              (with-current-buffer gnus-summary-buffer
6225                gnus-newsgroup-dependencies)))
6226         headers id end ref number
6227         (mail-parse-charset gnus-newsgroup-charset)
6228         (mail-parse-ignored-charsets
6229          (save-current-buffer (condition-case nil
6230                                   (set-buffer gnus-summary-buffer)
6231                                 (error))
6232                               gnus-newsgroup-ignored-charsets)))
6233     (with-current-buffer nntp-server-buffer
6234       ;; Translate all TAB characters into SPACE characters.
6235       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6236       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6237       (ietf-drums-unfold-fws)
6238       (gnus-run-hooks 'gnus-parse-headers-hook)
6239       (let ((case-fold-search t)
6240             in-reply-to header p lines chars)
6241         (goto-char (point-min))
6242         ;; Search to the beginning of the next header.  Error messages
6243         ;; do not begin with 2 or 3.
6244         (while (re-search-forward "^[23][0-9]+ " nil t)
6245           (setq id nil
6246                 ref nil)
6247           ;; This implementation of this function, with nine
6248           ;; search-forwards instead of the one re-search-forward and
6249           ;; a case (which basically was the old function) is actually
6250           ;; about twice as fast, even though it looks messier.  You
6251           ;; can't have everything, I guess.  Speed and elegance
6252           ;; doesn't always go hand in hand.
6253           (setq
6254            header
6255            (vector
6256             ;; Number.
6257             (prog1
6258                 (setq number (read cur))
6259               (end-of-line)
6260               (setq p (point))
6261               (narrow-to-region (point)
6262                                 (or (and (search-forward "\n.\n" nil t)
6263                                          (- (point) 2))
6264                                     (point))))
6265             ;; Subject.
6266             (progn
6267               (goto-char p)
6268               (if (search-forward "\nsubject:" nil t)
6269                   (funcall gnus-decode-encoded-word-function
6270                            (nnheader-header-value))
6271                 "(none)"))
6272             ;; From.
6273             (progn
6274               (goto-char p)
6275               (if (search-forward "\nfrom:" nil t)
6276                   (funcall gnus-decode-encoded-address-function
6277                            (nnheader-header-value))
6278                 "(nobody)"))
6279             ;; Date.
6280             (progn
6281               (goto-char p)
6282               (if (search-forward "\ndate:" nil t)
6283                   (nnheader-header-value) ""))
6284             ;; Message-ID.
6285             (progn
6286               (goto-char p)
6287               (setq id (if (re-search-forward
6288                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6289                            ;; We do it this way to make sure the Message-ID
6290                            ;; is (somewhat) syntactically valid.
6291                            (buffer-substring (match-beginning 1)
6292                                              (match-end 1))
6293                          ;; If there was no message-id, we just fake one
6294                          ;; to make subsequent routines simpler.
6295                          (nnheader-generate-fake-message-id number))))
6296             ;; References.
6297             (progn
6298               (goto-char p)
6299               (if (search-forward "\nreferences:" nil t)
6300                   (progn
6301                     (setq end (point))
6302                     (prog1
6303                         (nnheader-header-value)
6304                       (setq ref
6305                             (buffer-substring
6306                              (progn
6307                                (end-of-line)
6308                                (search-backward ">" end t)
6309                                (1+ (point)))
6310                              (progn
6311                                (search-backward "<" end t)
6312                                (point))))))
6313                 ;; Get the references from the in-reply-to header if there
6314                 ;; were no references and the in-reply-to header looks
6315                 ;; promising.
6316                 (if (and (search-forward "\nin-reply-to:" nil t)
6317                          (setq in-reply-to (nnheader-header-value))
6318                          (string-match "<[^>]+>" in-reply-to))
6319                     (let (ref2)
6320                       (setq ref (substring in-reply-to (match-beginning 0)
6321                                            (match-end 0)))
6322                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6323                         (setq ref2 (substring in-reply-to (match-beginning 0)
6324                                               (match-end 0)))
6325                         (when (> (length ref2) (length ref))
6326                           (setq ref ref2)))
6327                       ref)
6328                   (setq ref nil))))
6329             ;; Chars.
6330             (progn
6331               (goto-char p)
6332               (if (search-forward "\nchars: " nil t)
6333                   (if (numberp (setq chars (ignore-errors (read cur))))
6334                       chars -1)
6335                 -1))
6336             ;; Lines.
6337             (progn
6338               (goto-char p)
6339               (if (search-forward "\nlines: " nil t)
6340                   (if (numberp (setq lines (ignore-errors (read cur))))
6341                       lines -1)
6342                 -1))
6343             ;; Xref.
6344             (progn
6345               (goto-char p)
6346               (and (search-forward "\nxref:" nil t)
6347                    (nnheader-header-value)))
6348             ;; Extra.
6349             (when gnus-extra-headers
6350               (let ((extra gnus-extra-headers)
6351                     out)
6352                 (while extra
6353                   (goto-char p)
6354                   (when (search-forward
6355                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6356                     (push (cons (car extra) (nnheader-header-value))
6357                           out))
6358                   (pop extra))
6359                 out))))
6360           (when (equal id ref)
6361             (setq ref nil))
6362
6363           (when gnus-alter-header-function
6364             (funcall gnus-alter-header-function header)
6365             (setq id (mail-header-id header)
6366                   ref (gnus-parent-id (mail-header-references header))))
6367
6368           (when (setq header
6369                       (gnus-dependencies-add-header
6370                        header dependencies force-new))
6371             (push header headers))
6372           (goto-char (point-max))
6373           (widen))
6374         (nreverse headers)))))
6375
6376 ;; Goes through the xover lines and returns a list of vectors
6377 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6378                                                   force-new dependencies
6379                                                   group also-fetch-heads)
6380   "Parse the news overview data in the server buffer.
6381 Return a list of headers that match SEQUENCE (see
6382 `nntp-retrieve-headers')."
6383   ;; Get the Xref when the users reads the articles since most/some
6384   ;; NNTP servers do not include Xrefs when using XOVER.
6385   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6386   (let ((mail-parse-charset gnus-newsgroup-charset)
6387         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6388         (cur nntp-server-buffer)
6389         (dependencies (or dependencies gnus-newsgroup-dependencies))
6390         (allp (cond
6391                ((eq gnus-read-all-available-headers t)
6392                 t)
6393                ((and (stringp gnus-read-all-available-headers)
6394                      group)
6395                 (string-match gnus-read-all-available-headers group))
6396                (t
6397                 nil)))
6398         number headers header)
6399     (with-current-buffer nntp-server-buffer
6400       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6401       ;; Allow the user to mangle the headers before parsing them.
6402       (gnus-run-hooks 'gnus-parse-headers-hook)
6403       (goto-char (point-min))
6404       (gnus-parse-without-error
6405         (while (and (or sequence allp)
6406                     (not (eobp)))
6407           (setq number (read cur))
6408           (when (not allp)
6409             (while (and sequence
6410                         (< (car sequence) number))
6411               (setq sequence (cdr sequence))))
6412           (when (and (or allp
6413                          (and sequence
6414                               (eq number (car sequence))))
6415                      (progn
6416                        (setq sequence (cdr sequence))
6417                        (setq header (inline
6418                                       (gnus-nov-parse-line
6419                                        number dependencies force-new)))))
6420             (push header headers))
6421           (forward-line 1)))
6422       ;; A common bug in inn is that if you have posted an article and
6423       ;; then retrieves the active file, it will answer correctly --
6424       ;; the new article is included.  However, a NOV entry for the
6425       ;; article may not have been generated yet, so this may fail.
6426       ;; We work around this problem by retrieving the last few
6427       ;; headers using HEAD.
6428       (if (or (not also-fetch-heads)
6429               (not sequence))
6430           ;; We (probably) got all the headers.
6431           (nreverse headers)
6432         (let ((gnus-nov-is-evil t))
6433           (nconc
6434            (nreverse headers)
6435            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6436              (gnus-get-newsgroup-headers))))))))
6437
6438 (defun gnus-article-get-xrefs ()
6439   "Fill in the Xref value in `gnus-current-headers', if necessary.
6440 This is meant to be called in `gnus-article-internal-prepare-hook'."
6441   (let ((headers (with-current-buffer gnus-summary-buffer
6442                    gnus-current-headers)))
6443     (or (not gnus-use-cross-reference)
6444         (not headers)
6445         (and (mail-header-xref headers)
6446              (not (string= (mail-header-xref headers) "")))
6447         (let ((case-fold-search t)
6448               xref)
6449           (save-restriction
6450             (nnheader-narrow-to-headers)
6451             (goto-char (point-min))
6452             (when (or (and (not (eobp))
6453                            (eq (downcase (char-after)) ?x)
6454                            (looking-at "Xref:"))
6455                       (search-forward "\nXref:" nil t))
6456               (goto-char (1+ (match-end 0)))
6457               (setq xref (buffer-substring (point) (point-at-eol)))
6458               (mail-header-set-xref headers xref)))))))
6459
6460 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6461   "Find article ID and insert the summary line for that article.
6462 OLD-HEADER can either be a header or a line number to insert
6463 the subject line on."
6464   (let* ((line (and (numberp old-header) old-header))
6465          (old-header (and (vectorp old-header) old-header))
6466          (header (cond ((and old-header use-old-header)
6467                         old-header)
6468                        ((and (numberp id)
6469                              (gnus-number-to-header id))
6470                         (gnus-number-to-header id))
6471                        (t
6472                         (gnus-read-header id))))
6473          (number (and (numberp id) id))
6474          d)
6475     (when header
6476       ;; Rebuild the thread that this article is part of and go to the
6477       ;; article we have fetched.
6478       (when (and (not gnus-show-threads)
6479                  old-header)
6480         (when (and number
6481                    (setq d (gnus-data-find (mail-header-number old-header))))
6482           (goto-char (gnus-data-pos d))
6483           (gnus-data-remove
6484            number
6485            (- (point-at-bol)
6486               (prog1
6487                   (1+ (point-at-eol))
6488                 (gnus-delete-line))))))
6489       ;; Remove list identifiers from subject.
6490       (when gnus-list-identifiers
6491         (let ((gnus-newsgroup-headers (list header)))
6492           (gnus-summary-remove-list-identifiers)))
6493       (when old-header
6494         (mail-header-set-number header (mail-header-number old-header)))
6495       (setq gnus-newsgroup-sparse
6496             (delq (setq number (mail-header-number header))
6497                   gnus-newsgroup-sparse))
6498       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6499       (push number gnus-newsgroup-limit)
6500       (gnus-rebuild-thread (mail-header-id header) line)
6501       (gnus-summary-goto-subject number nil t))
6502     (when (and (numberp number)
6503                (> number 0))
6504       ;; We have to update the boundaries even if we can't fetch the
6505       ;; article if ID is a number -- so that the next `P' or `N'
6506       ;; command will fetch the previous (or next) article even
6507       ;; if the one we tried to fetch this time has been canceled.
6508       (when (> number gnus-newsgroup-end)
6509         (setq gnus-newsgroup-end number))
6510       (when (< number gnus-newsgroup-begin)
6511         (setq gnus-newsgroup-begin number))
6512       (setq gnus-newsgroup-unselected
6513             (delq number gnus-newsgroup-unselected)))
6514     ;; Report back a success?
6515     (and header (mail-header-number header))))
6516
6517 ;;; Process/prefix in the summary buffer
6518
6519 (defun gnus-summary-work-articles (n)
6520   "Return a list of articles to be worked upon.
6521 The prefix argument, the list of process marked articles, and the
6522 current article will be taken into consideration."
6523   (with-current-buffer gnus-summary-buffer
6524     (cond
6525      (n
6526       ;; A numerical prefix has been given.
6527       (setq n (prefix-numeric-value n))
6528       (let ((backward (< n 0))
6529             (n (abs (prefix-numeric-value n)))
6530             articles article)
6531         (save-excursion
6532           (while
6533               (and (> n 0)
6534                    (push (setq article (gnus-summary-article-number))
6535                          articles)
6536                    (if backward
6537                        (gnus-summary-find-prev nil article)
6538                      (gnus-summary-find-next nil article)))
6539             (decf n)))
6540         (nreverse articles)))
6541      ((and (gnus-region-active-p) (mark))
6542       (message "region active")
6543       ;; Work on the region between point and mark.
6544       (let ((max (max (point) (mark)))
6545             articles article)
6546         (save-excursion
6547           (goto-char (min (point) (mark)))
6548           (while
6549               (and
6550                (push (setq article (gnus-summary-article-number)) articles)
6551                (gnus-summary-find-next nil article)
6552                (< (point) max)))
6553           (nreverse articles))))
6554      (gnus-newsgroup-processable
6555       ;; There are process-marked articles present.
6556       ;; Save current state.
6557       (gnus-summary-save-process-mark)
6558       ;; Return the list.
6559       (reverse gnus-newsgroup-processable))
6560      (t
6561       ;; Just return the current article.
6562       (list (gnus-summary-article-number))))))
6563
6564 (defmacro gnus-summary-iterate (arg &rest forms)
6565   "Iterate over the process/prefixed articles and do FORMS.
6566 ARG is the interactive prefix given to the command.  FORMS will be
6567 executed with point over the summary line of the articles."
6568   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6569     `(let ((,articles (gnus-summary-work-articles ,arg)))
6570        (while ,articles
6571          (gnus-summary-goto-subject (car ,articles))
6572          ,@forms
6573          (pop ,articles)))))
6574
6575 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6576 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6577
6578 (defun gnus-summary-save-process-mark ()
6579   "Push the current set of process marked articles on the stack."
6580   (interactive)
6581   (push (copy-sequence gnus-newsgroup-processable)
6582         gnus-newsgroup-process-stack))
6583
6584 (defun gnus-summary-kill-process-mark ()
6585   "Push the current set of process marked articles on the stack and unmark."
6586   (interactive)
6587   (gnus-summary-save-process-mark)
6588   (gnus-summary-unmark-all-processable))
6589
6590 (defun gnus-summary-yank-process-mark ()
6591   "Pop the last process mark state off the stack and restore it."
6592   (interactive)
6593   (unless gnus-newsgroup-process-stack
6594     (error "Empty mark stack"))
6595   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6596
6597 (defun gnus-summary-process-mark-set (set)
6598   "Make SET into the current process marked articles."
6599   (gnus-summary-unmark-all-processable)
6600   (mapc 'gnus-summary-set-process-mark set))
6601
6602 ;;; Searching and stuff
6603
6604 (defun gnus-summary-search-group (&optional backward use-level)
6605   "Search for next unread newsgroup.
6606 If optional argument BACKWARD is non-nil, search backward instead."
6607   (with-current-buffer gnus-group-buffer
6608     (when (gnus-group-search-forward
6609            backward nil (if use-level (gnus-group-group-level) nil))
6610       (gnus-group-group-name))))
6611
6612 (defun gnus-summary-best-group (&optional exclude-group)
6613   "Find the name of the best unread group.
6614 If EXCLUDE-GROUP, do not go to this group."
6615   (with-current-buffer gnus-group-buffer
6616     (save-excursion
6617       (gnus-group-best-unread-group exclude-group))))
6618
6619 (defun gnus-summary-find-next (&optional unread article backward)
6620   (if backward
6621       (gnus-summary-find-prev unread article)
6622     (let* ((dummy (gnus-summary-article-intangible-p))
6623            (article (or article (gnus-summary-article-number)))
6624            (data (gnus-data-find-list article))
6625            result)
6626       (when (and (not dummy)
6627                  (or (not gnus-summary-check-current)
6628                      (not unread)
6629                      (not (gnus-data-unread-p (car data)))))
6630         (setq data (cdr data)))
6631       (when (setq result
6632                   (if unread
6633                       (progn
6634                         (while data
6635                           (unless (memq (gnus-data-number (car data))
6636                                         (cond
6637                                          ((eq gnus-auto-goto-ignores
6638                                               'always-undownloaded)
6639                                           gnus-newsgroup-undownloaded)
6640                                          (gnus-plugged
6641                                           nil)
6642                                          ((eq gnus-auto-goto-ignores
6643                                               'unfetched)
6644                                           gnus-newsgroup-unfetched)
6645                                          ((eq gnus-auto-goto-ignores
6646                                               'undownloaded)
6647                                           gnus-newsgroup-undownloaded)))
6648                             (when (gnus-data-unread-p (car data))
6649                               (setq result (car data)
6650                                     data nil)))
6651                           (setq data (cdr data)))
6652                         result)
6653                     (car data)))
6654         (goto-char (gnus-data-pos result))
6655         (gnus-data-number result)))))
6656
6657 (defun gnus-summary-find-prev (&optional unread article)
6658   (let* ((eobp (eobp))
6659          (article (or article (gnus-summary-article-number)))
6660          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6661          result)
6662     (when (and (not eobp)
6663                (or (not gnus-summary-check-current)
6664                    (not unread)
6665                    (not (gnus-data-unread-p (car data)))))
6666       (setq data (cdr data)))
6667     (when (setq result
6668                 (if unread
6669                     (progn
6670                       (while data
6671                         (unless (memq (gnus-data-number (car data))
6672                                       (cond
6673                                        ((eq gnus-auto-goto-ignores
6674                                             'always-undownloaded)
6675                                         gnus-newsgroup-undownloaded)
6676                                        (gnus-plugged
6677                                         nil)
6678                                        ((eq gnus-auto-goto-ignores
6679                                             'unfetched)
6680                                         gnus-newsgroup-unfetched)
6681                                        ((eq gnus-auto-goto-ignores
6682                                             'undownloaded)
6683                                         gnus-newsgroup-undownloaded)))
6684                           (when (gnus-data-unread-p (car data))
6685                             (setq result (car data)
6686                                   data nil)))
6687                         (setq data (cdr data)))
6688                       result)
6689                   (car data)))
6690       (goto-char (gnus-data-pos result))
6691       (gnus-data-number result))))
6692
6693 (defun gnus-summary-find-subject (subject &optional unread backward article)
6694   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6695          (article (or article (gnus-summary-article-number)))
6696          (articles (gnus-data-list backward))
6697          (arts (gnus-data-find-list article articles))
6698          result)
6699     (when (or (not gnus-summary-check-current)
6700               (not unread)
6701               (not (gnus-data-unread-p (car arts))))
6702       (setq arts (cdr arts)))
6703     (while arts
6704       (and (or (not unread)
6705                (gnus-data-unread-p (car arts)))
6706            (vectorp (gnus-data-header (car arts)))
6707            (gnus-subject-equal
6708             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6709            (setq result (car arts)
6710                  arts nil))
6711       (setq arts (cdr arts)))
6712     (and result
6713          (goto-char (gnus-data-pos result))
6714          (gnus-data-number result))))
6715
6716 (defun gnus-summary-search-forward (&optional unread subject backward)
6717   "Search forward for an article.
6718 If UNREAD, look for unread articles.  If SUBJECT, look for
6719 articles with that subject.  If BACKWARD, search backward instead."
6720   (cond (subject (gnus-summary-find-subject subject unread backward))
6721         (backward (gnus-summary-find-prev unread))
6722         (t (gnus-summary-find-next unread))))
6723
6724 (defun gnus-recenter (&optional n)
6725   "Center point in window and redisplay frame.
6726 Also do horizontal recentering."
6727   (interactive "P")
6728   (when (and gnus-auto-center-summary
6729              (not (eq gnus-auto-center-summary 'vertical)))
6730     (gnus-horizontal-recenter))
6731   (if (fboundp 'recenter-top-bottom)
6732       (recenter-top-bottom n)
6733     (recenter n)))
6734
6735 (put 'gnus-recenter 'isearch-scroll t)
6736
6737 (defun gnus-forward-line-ignore-invisible (n)
6738   "Move N lines forward (backward if N is negative).
6739 Like forward-line, but skip over (and don't count) invisible lines."
6740   (let (done)
6741     (while (and (> n 0) (not done))
6742       ;; If the following character is currently invisible,
6743       ;; skip all characters with that same `invisible' property value.
6744       (while (gnus-invisible-p (point))
6745         (goto-char (gnus-next-char-property-change (point))))
6746       (forward-line 1)
6747       (if (eobp)
6748           (setq done t)
6749         (setq n (1- n))))
6750     (while (and (< n 0) (not done))
6751       (forward-line -1)
6752       (if (bobp) (setq done t)
6753         (setq n (1+ n))
6754         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6755           (goto-char (gnus-previous-char-property-change (point))))))))
6756
6757 (defun gnus-summary-recenter ()
6758   "Center point in the summary window.
6759 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6760 displayed, no centering will be performed."
6761   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6762   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6763   (interactive)
6764   ;; The user has to want it.
6765   (when gnus-auto-center-summary
6766     (let* ((top (cond ((< (window-height) 4) 0)
6767                       ((< (window-height) 7) 1)
6768                       (t (if (numberp gnus-auto-center-summary)
6769                              gnus-auto-center-summary
6770                            (/ (1- (window-height)) 2)))))
6771            (height (1- (window-height)))
6772            (bottom (save-excursion
6773                      (goto-char (point-max))
6774                      (gnus-forward-line-ignore-invisible (- height))
6775                      (point)))
6776            (window (get-buffer-window (current-buffer))))
6777       (when (get-buffer-window gnus-article-buffer)
6778         ;; Only do recentering when the article buffer is displayed,
6779         ;; Set the window start to either `bottom', which is the biggest
6780         ;; possible valid number, or the second line from the top,
6781         ;; whichever is the least.
6782         (let ((top-pos (save-excursion
6783                          (gnus-forward-line-ignore-invisible (- top))
6784                          (point))))
6785           (if (> bottom top-pos)
6786               ;; Keep the second line from the top visible
6787               (set-window-start window top-pos)
6788             ;; Try to keep the bottom line visible; if it's partially
6789             ;; obscured, either scroll one more line to make it fully
6790             ;; visible, or revert to using TOP-POS.
6791             (save-excursion
6792               (goto-char (point-max))
6793               (gnus-forward-line-ignore-invisible -1)
6794               (let ((last-line-start (point)))
6795                 (goto-char bottom)
6796                 (set-window-start window (point) t)
6797                 (when (not (pos-visible-in-window-p last-line-start window))
6798                   (gnus-forward-line-ignore-invisible 1)
6799                   (set-window-start window (min (point) top-pos) t)))))))
6800       ;; Do horizontal recentering while we're at it.
6801       (when (and (get-buffer-window (current-buffer) t)
6802                  (not (eq gnus-auto-center-summary 'vertical)))
6803         (let ((selected (selected-window)))
6804           (select-window (get-buffer-window (current-buffer) t))
6805           (gnus-summary-position-point)
6806           (gnus-horizontal-recenter)
6807           (select-window selected))))))
6808
6809 (defun gnus-summary-jump-to-group (newsgroup)
6810   "Move point to NEWSGROUP in group mode buffer."
6811   ;; Keep update point of group mode buffer if visible.
6812   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6813       (save-window-excursion
6814         ;; Take care of tree window mode.
6815         (when (get-buffer-window gnus-group-buffer)
6816           (pop-to-buffer gnus-group-buffer))
6817         (gnus-group-jump-to-group newsgroup))
6818     (save-excursion
6819       ;; Take care of tree window mode.
6820       (if (get-buffer-window gnus-group-buffer 0)
6821           (pop-to-buffer gnus-group-buffer)
6822         (set-buffer gnus-group-buffer))
6823       (gnus-group-jump-to-group newsgroup))))
6824
6825 ;; This function returns a list of article numbers based on the
6826 ;; difference between the ranges of read articles in this group and
6827 ;; the range of active articles.
6828 (defun gnus-list-of-unread-articles (group)
6829   (let* ((read (gnus-info-read (gnus-get-info group)))
6830          (active (or (gnus-active group) (gnus-activate-group group)))
6831          (last (or (cdr active)
6832                    (error "Group %s couldn't be activated " group)))
6833          (bottom (if gnus-newsgroup-maximum-articles
6834                      (max (car active)
6835                           (- last gnus-newsgroup-maximum-articles -1))
6836                    (car active)))
6837          first nlast unread)
6838     ;; If none are read, then all are unread.
6839     (if (not read)
6840         (setq first bottom)
6841       ;; If the range of read articles is a single range, then the
6842       ;; first unread article is the article after the last read
6843       ;; article.  Sounds logical, doesn't it?
6844       (if (and (not (listp (cdr read)))
6845                (or (< (car read) bottom)
6846                    (progn (setq read (list read))
6847                           nil)))
6848           (setq first (max bottom (1+ (cdr read))))
6849         ;; `read' is a list of ranges.
6850         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6851                                   (caar read)))
6852                   1)
6853           (setq first bottom))
6854         (while read
6855           (when first
6856             (while (< first nlast)
6857               (setq unread (cons first unread)
6858                     first (1+ first))))
6859           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6860           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6861           (setq read (cdr read)))))
6862     ;; And add the last unread articles.
6863     (while (<= first last)
6864       (setq unread (cons first unread)
6865             first (1+ first)))
6866     ;; Return the list of unread articles.
6867     (delq 0 (nreverse unread))))
6868
6869 (defun gnus-list-of-read-articles (group)
6870   "Return a list of unread, unticked and non-dormant articles."
6871   (let* ((info (gnus-get-info group))
6872          (marked (gnus-info-marks info))
6873          (active (gnus-active group)))
6874     (and info active
6875          (gnus-list-range-difference
6876           (gnus-list-range-difference
6877            (gnus-sorted-complement
6878             (gnus-uncompress-range
6879              (if gnus-newsgroup-maximum-articles
6880                  (cons (max (car active)
6881                             (- (cdr active)
6882                                gnus-newsgroup-maximum-articles
6883                                -1))
6884                        (cdr active))
6885                active))
6886             (gnus-list-of-unread-articles group))
6887            (cdr (assq 'dormant marked)))
6888           (cdr (assq 'tick marked))))))
6889
6890 ;; This function returns a sequence of article numbers based on the
6891 ;; difference between the ranges of read articles in this group and
6892 ;; the range of active articles.
6893 (defun gnus-sequence-of-unread-articles (group)
6894   (let* ((read (gnus-info-read (gnus-get-info group)))
6895          (active (or (gnus-active group) (gnus-activate-group group)))
6896          (last (cdr active))
6897          (bottom (if gnus-newsgroup-maximum-articles
6898                      (max (car active)
6899                           (- last gnus-newsgroup-maximum-articles -1))
6900                    (car active)))
6901          first nlast unread)
6902     ;; If none are read, then all are unread.
6903     (if (not read)
6904         (setq first bottom)
6905       ;; If the range of read articles is a single range, then the
6906       ;; first unread article is the article after the last read
6907       ;; article.  Sounds logical, doesn't it?
6908       (if (and (not (listp (cdr read)))
6909                (or (< (car read) bottom)
6910                    (progn (setq read (list read))
6911                           nil)))
6912           (setq first (max bottom (1+ (cdr read))))
6913         ;; `read' is a list of ranges.
6914         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6915                                   (caar read)))
6916                   1)
6917           (setq first bottom))
6918         (while read
6919           (when first
6920             (push (cons first nlast) unread))
6921           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6922           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6923           (setq read (cdr read)))))
6924     ;; And add the last unread articles.
6925     (cond ((not (and first last))
6926            nil)
6927           ((< first last)
6928            (push (cons first last) unread))
6929           ((= first last)
6930            (push first unread)))
6931     ;; Return the sequence of unread articles.
6932     (delq 0 (nreverse unread))))
6933
6934 ;; Various summary commands
6935
6936 (defun gnus-summary-select-article-buffer ()
6937   "Reconfigure windows to show article buffer."
6938   (interactive)
6939   (if (not (gnus-buffer-live-p gnus-article-buffer))
6940       (error "There is no article buffer for this summary buffer")
6941     (gnus-configure-windows 'article)
6942     (select-window (get-buffer-window gnus-article-buffer))))
6943
6944 (defun gnus-summary-universal-argument (arg)
6945   "Perform any operation on all articles that are process/prefixed."
6946   (interactive "P")
6947   (let ((articles (gnus-summary-work-articles arg))
6948         func article)
6949     (if (eq
6950          (setq
6951           func
6952           (key-binding
6953            (read-key-sequence
6954             (substitute-command-keys
6955              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6956          'undefined)
6957         (gnus-error 1 "Undefined key")
6958       (save-excursion
6959         (while articles
6960           (gnus-summary-goto-subject (setq article (pop articles)))
6961           (let (gnus-newsgroup-processable)
6962             (command-execute func))
6963           (gnus-summary-remove-process-mark article)))))
6964   (gnus-summary-position-point))
6965
6966 (defun gnus-summary-toggle-truncation (&optional arg)
6967   "Toggle truncation of summary lines.
6968 With ARG, turn line truncation on if ARG is positive."
6969   (interactive "P")
6970   (setq truncate-lines
6971         (if (null arg) (not truncate-lines)
6972           (> (prefix-numeric-value arg) 0)))
6973   (redraw-display))
6974
6975 (defun gnus-summary-find-for-reselect ()
6976   "Return the number of an article to stay on across a reselect.
6977 The current article is considered, then following articles, then previous
6978 articles.  An article is sought which is not cancelled and isn't a temporary
6979 insertion from another group.  If there's no such then return a dummy 0."
6980   (let (found)
6981     (dolist (rev '(nil t))
6982       (unless found      ; don't demand the reverse list if we don't need it
6983         (let ((data (gnus-data-find-list
6984                      (gnus-summary-article-number) (gnus-data-list rev))))
6985           (while (and data (not found))
6986             (if (and (< 0 (gnus-data-number (car data)))
6987                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6988                 (setq found (gnus-data-number (car data))))
6989             (setq data (cdr data))))))
6990     (or found 0)))
6991
6992 (defun gnus-summary-reselect-current-group (&optional all rescan)
6993   "Exit and then reselect the current newsgroup.
6994 The prefix argument ALL means to select all articles."
6995   (interactive "P")
6996   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6997     (error "Ephemeral groups can't be reselected"))
6998   (let ((current-subject (gnus-summary-find-for-reselect))
6999         (group gnus-newsgroup-name))
7000     (setq gnus-newsgroup-begin nil)
7001     (gnus-summary-exit nil 'leave-hidden)
7002     ;; We have to adjust the point of group mode buffer because
7003     ;; point was moved to the next unread newsgroup by exiting.
7004     (gnus-summary-jump-to-group group)
7005     (when rescan
7006       (save-excursion
7007         (gnus-group-get-new-news-this-group 1)))
7008     (gnus-group-read-group all t)
7009     (gnus-summary-goto-subject current-subject nil t)))
7010
7011 (defun gnus-summary-rescan-group (&optional all)
7012   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7013   (interactive "P")
7014   (gnus-summary-reselect-current-group all t))
7015
7016 (defun gnus-summary-update-info (&optional non-destructive)
7017   (save-excursion
7018     (let ((group gnus-newsgroup-name))
7019       (when group
7020         (when gnus-newsgroup-kill-headers
7021           (setq gnus-newsgroup-killed
7022                 (gnus-compress-sequence
7023                  (gnus-sorted-union
7024                   (gnus-list-range-intersection
7025                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7026                   gnus-newsgroup-unreads)
7027                  t)))
7028         (unless (listp (cdr gnus-newsgroup-killed))
7029           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7030         (let ((headers gnus-newsgroup-headers))
7031           ;; Set the new ranges of read articles.
7032           (with-current-buffer gnus-group-buffer
7033             (gnus-undo-force-boundary))
7034           (gnus-update-read-articles
7035            group (gnus-sorted-union
7036                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7037           ;; Set the current article marks.
7038           (let ((gnus-newsgroup-scored
7039                  (if (and (not gnus-save-score)
7040                           (not non-destructive))
7041                      nil
7042                    gnus-newsgroup-scored)))
7043             (save-excursion
7044               (gnus-update-marks)))
7045           ;; Do the cross-ref thing.
7046           (when gnus-use-cross-reference
7047             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7048           ;; Do not switch windows but change the buffer to work.
7049           (set-buffer gnus-group-buffer)
7050           (unless (gnus-ephemeral-group-p group)
7051             (gnus-group-update-group group)))))))
7052
7053 (defun gnus-summary-save-newsrc (&optional force)
7054   "Save the current number of read/marked articles in the dribble buffer.
7055 The dribble buffer will then be saved.
7056 If FORCE (the prefix), also save the .newsrc file(s)."
7057   (interactive "P")
7058   (gnus-summary-update-info t)
7059   (if force
7060       (gnus-save-newsrc-file)
7061     (gnus-dribble-save)))
7062
7063 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7064
7065 (defun gnus-summary-exit (&optional temporary leave-hidden)
7066   "Exit reading current newsgroup, and then return to group selection mode.
7067 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7068   (interactive)
7069   (gnus-set-global-variables)
7070   (when (gnus-buffer-live-p gnus-article-buffer)
7071     (with-current-buffer gnus-article-buffer
7072       (mm-destroy-parts gnus-article-mime-handles)
7073       ;; Set it to nil for safety reason.
7074       (setq gnus-article-mime-handle-alist nil)
7075       (setq gnus-article-mime-handles nil)))
7076   (gnus-kill-save-kill-buffer)
7077   (gnus-async-halt-prefetch)
7078   (let* ((group gnus-newsgroup-name)
7079          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7080          (gnus-group-is-exiting-p t)
7081          (mode major-mode)
7082          (group-point nil)
7083          (buf (current-buffer)))
7084     (unless quit-config
7085       ;; Do adaptive scoring, and possibly save score files.
7086       (when gnus-newsgroup-adaptive
7087         (gnus-score-adaptive))
7088       (when gnus-use-scoring
7089         (gnus-score-save)))
7090     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7091     ;; If we have several article buffers, we kill them at exit.
7092     (unless gnus-single-article-buffer
7093       (when (gnus-buffer-live-p gnus-article-buffer)
7094         (with-current-buffer gnus-article-buffer
7095           ;; Don't kill sticky article buffers
7096           (unless (eq major-mode 'gnus-sticky-article-mode)
7097             (gnus-kill-buffer gnus-article-buffer)
7098             (setq gnus-article-current nil))))
7099       (gnus-kill-buffer gnus-original-article-buffer))
7100     (when gnus-use-cache
7101       (gnus-cache-possibly-remove-articles)
7102       (gnus-cache-save-buffers))
7103     (gnus-async-prefetch-remove-group group)
7104     (when gnus-suppress-duplicates
7105       (gnus-dup-enter-articles))
7106     (when gnus-use-trees
7107       (gnus-tree-close group))
7108     (when gnus-use-cache
7109       (gnus-cache-write-active))
7110     ;; Remove entries for this group.
7111     (nnmail-purge-split-history (gnus-group-real-name group))
7112     ;; Make all changes in this group permanent.
7113     (unless quit-config
7114       (gnus-run-hooks 'gnus-exit-group-hook)
7115       (gnus-summary-update-info))
7116     (gnus-close-group group)
7117     ;; Make sure where we were, and go to next newsgroup.
7118     (set-buffer gnus-group-buffer)
7119     (unless quit-config
7120       (gnus-group-jump-to-group group))
7121     (gnus-run-hooks 'gnus-summary-exit-hook)
7122     (unless (or quit-config
7123                 (not gnus-summary-next-group-on-exit)
7124                 ;; If this group has disappeared from the summary
7125                 ;; buffer, don't skip forwards.
7126                 (not (string= group (gnus-group-group-name))))
7127       (gnus-group-next-unread-group 1))
7128     (setq group-point (point))
7129     (if temporary
7130         nil                             ;Nothing to do.
7131       (set-buffer buf)
7132       (if (not gnus-kill-summary-on-exit)
7133           (progn
7134             (gnus-deaden-summary)
7135             (setq mode nil))
7136         ;; We set all buffer-local variables to nil.  It is unclear why
7137         ;; this is needed, but if we don't, buffer-local variables are
7138         ;; not garbage-collected, it seems.  This would the lead to en
7139         ;; ever-growing Emacs.
7140         (gnus-summary-clear-local-variables)
7141         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7142           (gnus-summary-clear-local-variables))
7143         (when (get-buffer gnus-article-buffer)
7144           (bury-buffer gnus-article-buffer))
7145         ;; Return to group mode buffer.
7146         (when (eq mode 'gnus-summary-mode)
7147           (gnus-kill-buffer buf)))
7148       (setq gnus-current-select-method gnus-select-method)
7149       (set-buffer gnus-group-buffer)
7150       (if quit-config
7151           (gnus-handle-ephemeral-exit quit-config)
7152         (goto-char group-point)
7153         ;; If gnus-group-buffer is already displayed, make sure we also move
7154         ;; the cursor in the window that displays it.
7155         (let ((win (get-buffer-window (current-buffer) 0)))
7156           (if win (set-window-point win (point))))
7157         (unless leave-hidden
7158           (gnus-configure-windows 'group 'force)))
7159       ;; Clear the current group name.
7160       (unless quit-config
7161         (setq gnus-newsgroup-name nil)))))
7162
7163 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7164 (defun gnus-summary-exit-no-update (&optional no-questions)
7165   "Quit reading current newsgroup without updating read article info."
7166   (interactive)
7167   (let* ((group gnus-newsgroup-name)
7168          (gnus-group-is-exiting-p t)
7169          (gnus-group-is-exiting-without-update-p t)
7170          (quit-config (gnus-group-quit-config group)))
7171     (when (or no-questions
7172               gnus-expert-user
7173               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7174       (gnus-async-halt-prefetch)
7175       (run-hooks 'gnus-summary-prepare-exit-hook)
7176       (when (gnus-buffer-live-p gnus-article-buffer)
7177         (with-current-buffer gnus-article-buffer
7178           (mm-destroy-parts gnus-article-mime-handles)
7179           ;; Set it to nil for safety reason.
7180           (setq gnus-article-mime-handle-alist nil)
7181           (setq gnus-article-mime-handles nil)))
7182       ;; If we have several article buffers, we kill them at exit.
7183       (unless gnus-single-article-buffer
7184         (gnus-kill-buffer gnus-article-buffer)
7185         (gnus-kill-buffer gnus-original-article-buffer)
7186         (setq gnus-article-current nil))
7187       (if (not gnus-kill-summary-on-exit)
7188           (gnus-deaden-summary)
7189         (gnus-close-group group)
7190         (gnus-summary-clear-local-variables)
7191         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7192           (gnus-summary-clear-local-variables))
7193         (gnus-kill-buffer gnus-summary-buffer))
7194       (unless gnus-single-article-buffer
7195         (setq gnus-article-current nil))
7196       (when gnus-use-trees
7197         (gnus-tree-close group))
7198       (gnus-async-prefetch-remove-group group)
7199       (when (get-buffer gnus-article-buffer)
7200         (bury-buffer gnus-article-buffer))
7201       ;; Return to the group buffer.
7202       (gnus-configure-windows 'group 'force)
7203       ;; Clear the current group name.
7204       (setq gnus-newsgroup-name nil)
7205       (unless (gnus-ephemeral-group-p group)
7206         (gnus-group-update-group group))
7207       (when (equal (gnus-group-group-name) group)
7208         (gnus-group-next-unread-group 1))
7209       (when quit-config
7210         (gnus-handle-ephemeral-exit quit-config)))))
7211
7212 (defun gnus-handle-ephemeral-exit (quit-config)
7213   "Handle movement when leaving an ephemeral group.
7214 The state which existed when entering the ephemeral is reset."
7215   (if (not (buffer-name (car quit-config)))
7216       (gnus-configure-windows 'group 'force)
7217     (set-buffer (car quit-config))
7218     (cond ((eq major-mode 'gnus-summary-mode)
7219            (gnus-set-global-variables))
7220           ((eq major-mode 'gnus-article-mode)
7221            (save-current-buffer
7222              ;; The `gnus-summary-buffer' variable may point
7223              ;; to the old summary buffer when using a single
7224              ;; article buffer.
7225              (unless (gnus-buffer-live-p gnus-summary-buffer)
7226                (set-buffer gnus-group-buffer))
7227              (set-buffer gnus-summary-buffer)
7228              (gnus-set-global-variables))))
7229     (if (or (eq (cdr quit-config) 'article)
7230             (eq (cdr quit-config) 'pick))
7231         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7232             (gnus-configure-windows 'pick 'force)
7233           (gnus-configure-windows (cdr quit-config) 'force))
7234       (gnus-configure-windows (cdr quit-config) 'force))
7235     (when (eq major-mode 'gnus-summary-mode)
7236       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7237                                                      next-unread-noselect))
7238           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7239                                   'next-noselect)
7240                               (gnus-summary-next-subject 1 nil t))
7241                              ((eq gnus-auto-select-on-ephemeral-exit
7242                                   'next-unread-noselect)
7243                               (gnus-summary-next-subject 1 t t))))
7244             ;; Hide the article buffer which displays the article different
7245             ;; from the one that the cursor points to in the summary buffer.
7246             (gnus-configure-windows 'summary 'force))
7247         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7248                (gnus-summary-next-subject 1))
7249               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7250                (gnus-summary-next-subject 1 t))))
7251       (gnus-summary-recenter)
7252       (gnus-summary-position-point))))
7253
7254 ;;; Dead summaries.
7255
7256 (defvar gnus-dead-summary-mode-map
7257   (let ((map (make-keymap)))
7258     (suppress-keymap map)
7259     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7260     (dolist (key '("\C-d" "\r" "\177" [delete]))
7261       (define-key map key 'gnus-summary-wake-up-the-dead))
7262     (dolist (key '("q" "Q"))
7263       (define-key map key 'bury-buffer))
7264     map))
7265
7266 (define-minor-mode gnus-dead-summary-mode
7267   "Minor mode for Gnus summary buffers."
7268   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7269   (unless (derived-mode-p 'gnus-summary-mode)
7270     (setq gnus-dead-summary-mode nil)))
7271
7272 (defun gnus-deaden-summary ()
7273   "Make the current summary buffer into a dead summary buffer."
7274   ;; Kill any previous dead summary buffer.
7275   (when (and gnus-dead-summary
7276              (buffer-name gnus-dead-summary))
7277     (with-current-buffer gnus-dead-summary
7278       (when gnus-dead-summary-mode
7279         (kill-buffer (current-buffer)))))
7280   ;; Make this the current dead summary.
7281   (setq gnus-dead-summary (current-buffer))
7282   (gnus-dead-summary-mode 1)
7283   (let ((name (buffer-name)))
7284     (when (string-match "Summary" name)
7285       (rename-buffer
7286        (concat (substring name 0 (match-beginning 0)) "Dead "
7287                (substring name (match-beginning 0)))
7288        t)
7289       (bury-buffer))))
7290
7291 (defun gnus-kill-or-deaden-summary (buffer)
7292   "Kill or deaden the summary BUFFER."
7293   (save-excursion
7294     (when (and (buffer-name buffer)
7295                (not gnus-single-article-buffer))
7296       (with-current-buffer buffer
7297         (gnus-kill-buffer gnus-article-buffer)
7298         (gnus-kill-buffer gnus-original-article-buffer)))
7299     (cond
7300      ;; Kill the buffer.
7301      (gnus-kill-summary-on-exit
7302       (when (and gnus-use-trees
7303                  (gnus-buffer-exists-p buffer))
7304         (with-current-buffer buffer
7305           (gnus-tree-close gnus-newsgroup-name)))
7306       (gnus-kill-buffer buffer))
7307      ;; Deaden the buffer.
7308      ((gnus-buffer-exists-p buffer)
7309       (with-current-buffer buffer
7310         (gnus-deaden-summary))))))
7311
7312 (defun gnus-summary-wake-up-the-dead (&rest args)
7313   "Wake up the dead summary buffer."
7314   (interactive)
7315   (gnus-dead-summary-mode -1)
7316   (let ((name (buffer-name)))
7317     (when (string-match "Dead " name)
7318       (rename-buffer
7319        (concat (substring name 0 (match-beginning 0))
7320                (substring name (match-end 0)))
7321        t)))
7322   (gnus-message 3 "This dead summary is now alive again"))
7323
7324 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7325 (defun gnus-summary-describe-group (&optional force)
7326   "Describe the current newsgroup."
7327   (interactive "P")
7328   (gnus-group-describe-group force gnus-newsgroup-name))
7329
7330 (defun gnus-summary-describe-briefly ()
7331   "Describe summary mode commands briefly."
7332   (interactive)
7333   (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")))
7334
7335 ;; Walking around group mode buffer from summary mode.
7336
7337 (defun gnus-summary-next-group (&optional no-article target-group backward)
7338   "Exit current newsgroup and then select next unread newsgroup.
7339 If prefix argument NO-ARTICLE is non-nil, no article is selected
7340 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7341 previous group instead."
7342   (interactive "P")
7343   ;; Stop pre-fetching.
7344   (gnus-async-halt-prefetch)
7345   (let ((current-group gnus-newsgroup-name)
7346         (current-buffer (current-buffer))
7347         entered)
7348     ;; First we semi-exit this group to update Xrefs and all variables.
7349     ;; We can't do a real exit, because the window conf must remain
7350     ;; the same in case the user is prompted for info, and we don't
7351     ;; want the window conf to change before that...
7352     (gnus-summary-exit t)
7353     (while (not entered)
7354       ;; Then we find what group we are supposed to enter.
7355       (set-buffer gnus-group-buffer)
7356       (gnus-group-jump-to-group current-group)
7357       (setq target-group
7358             (or target-group
7359                 (if (eq gnus-keep-same-level 'best)
7360                     (gnus-summary-best-group gnus-newsgroup-name)
7361                   (gnus-summary-search-group backward gnus-keep-same-level))))
7362       (if (not target-group)
7363           ;; There are no further groups, so we return to the group
7364           ;; buffer.
7365           (progn
7366             (gnus-message 5 "Returning to the group buffer")
7367             (setq entered t)
7368             (when (gnus-buffer-live-p current-buffer)
7369               (set-buffer current-buffer)
7370               (gnus-summary-exit))
7371             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7372         ;; We try to enter the target group.
7373         (gnus-group-jump-to-group target-group)
7374         (let ((unreads (gnus-group-group-unread)))
7375           (if (and (or (eq t unreads)
7376                        (and unreads (not (zerop unreads))))
7377                    (gnus-summary-read-group
7378                     target-group nil no-article
7379                     (and (buffer-name current-buffer) current-buffer)
7380                     nil backward))
7381               (setq entered t)
7382             (setq current-group target-group
7383                   target-group nil)))))))
7384
7385 (defun gnus-summary-prev-group (&optional no-article)
7386   "Exit current newsgroup and then select previous unread newsgroup.
7387 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7388   (interactive "P")
7389   (gnus-summary-next-group no-article nil t))
7390
7391 ;; Walking around summary lines.
7392
7393 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7394   "Go to the first subject satisfying any non-nil constraint.
7395 If UNREAD is non-nil, the article should be unread.
7396 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7397 If UNSEEN is non-nil, the article should be unseen as well as unread.
7398 Returns the article selected or nil if there are no matching articles."
7399   (interactive "P")
7400   (cond
7401    ;; Empty summary.
7402    ((null gnus-newsgroup-data)
7403     (gnus-message 3 "No articles in the group")
7404     nil)
7405    ;; Pick the first article.
7406    ((not (or unread undownloaded unseen))
7407     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7408     (gnus-data-number (car gnus-newsgroup-data)))
7409    ;; Find the first unread article.
7410    (t
7411     (let ((data gnus-newsgroup-data))
7412       (while (and data
7413                   (let ((num (gnus-data-number (car data))))
7414                     (or (memq num gnus-newsgroup-unfetched)
7415                         (not (or (and unread
7416                                       (memq num gnus-newsgroup-unreads))
7417                                  (and undownloaded
7418                                       (memq num gnus-newsgroup-undownloaded))
7419                                  (and unseen
7420                                       (memq num gnus-newsgroup-unseen)
7421                                       (memq num gnus-newsgroup-unreads)))))))
7422         (setq data (cdr data)))
7423       (prog1
7424           (if data
7425               (progn
7426                 (goto-char (gnus-data-pos (car data)))
7427                 (gnus-data-number (car data)))
7428             (gnus-message 3 "No more%s articles"
7429                           (let* ((r (when unread " unread"))
7430                                  (d (when undownloaded " undownloaded"))
7431                                  (s (when unseen " unseen"))
7432                                  (l (delq nil (list r d s))))
7433                             (cond ((= 3 (length l))
7434                                    (concat r "," d ", or" s))
7435                                   ((= 2 (length l))
7436                                    (concat (car l) ", or" (cadr l)))
7437                                   ((= 1 (length l))
7438                                    (car l))
7439                                   (t
7440                                    ""))))
7441             nil
7442             )
7443         (gnus-summary-position-point))))))
7444
7445 (defun gnus-summary-next-subject (n &optional unread dont-display)
7446   "Go to next N'th summary line.
7447 If N is negative, go to the previous N'th subject line.
7448 If UNREAD is non-nil, only unread articles are selected.
7449 The difference between N and the actual number of steps taken is
7450 returned."
7451   (interactive "p")
7452   (let ((backward (< n 0))
7453         (n (abs n)))
7454     (while (and (> n 0)
7455                 (if backward
7456                     (gnus-summary-find-prev unread)
7457                   (gnus-summary-find-next unread)))
7458       (unless (zerop (setq n (1- n)))
7459         (gnus-summary-show-thread)))
7460     (when (/= 0 n)
7461       (gnus-message 7 "No more%s articles"
7462                     (if unread " unread" "")))
7463     (unless dont-display
7464       (gnus-summary-recenter)
7465       (gnus-summary-position-point))
7466     n))
7467
7468 (defun gnus-summary-next-unread-subject (n)
7469   "Go to next N'th unread summary line."
7470   (interactive "p")
7471   (gnus-summary-next-subject n t))
7472
7473 (defun gnus-summary-prev-subject (n &optional unread)
7474   "Go to previous N'th summary line.
7475 If optional argument UNREAD is non-nil, only unread article is selected."
7476   (interactive "p")
7477   (gnus-summary-next-subject (- n) unread))
7478
7479 (defun gnus-summary-prev-unread-subject (n)
7480   "Go to previous N'th unread summary line."
7481   (interactive "p")
7482   (gnus-summary-next-subject (- n) t))
7483
7484 (defun gnus-summary-goto-subjects (articles)
7485   "Insert the subject header for ARTICLES in the current buffer."
7486   (save-excursion
7487     (dolist (article articles)
7488       (gnus-summary-goto-subject article t)))
7489   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7490   (gnus-summary-position-point))
7491
7492 (defun gnus-summary-goto-subject (article &optional force silent)
7493   "Go to the subject line of ARTICLE.
7494 If FORCE, also allow jumping to articles not currently shown."
7495   (interactive "nArticle number: ")
7496   (unless (numberp article)
7497     (error "Article %s is not a number" article))
7498   (let ((b (point))
7499         (data (gnus-data-find article)))
7500     ;; We read in the article if we have to.
7501     (and (not data)
7502          force
7503          (gnus-summary-insert-subject
7504           article
7505           (if (or (numberp force) (vectorp force)) force)
7506           t)
7507          (setq data (gnus-data-find article)))
7508     (goto-char b)
7509     (if (not data)
7510         (progn
7511           (unless silent
7512             (gnus-message 3 "Can't find article %d" article))
7513           nil)
7514       (let ((pt (gnus-data-pos data)))
7515         (goto-char pt)
7516         (gnus-summary-set-article-display-arrow pt))
7517       (gnus-summary-position-point)
7518       article)))
7519
7520 ;; Walking around summary lines with displaying articles.
7521
7522 (defun gnus-summary-expand-window (&optional arg)
7523   "Make the summary buffer take up the entire Emacs frame.
7524 Given a prefix, will force an `article' buffer configuration."
7525   (interactive "P")
7526   (if arg
7527       (gnus-configure-windows 'article 'force)
7528     (gnus-configure-windows 'summary 'force)))
7529
7530 (defun gnus-summary-display-article (article &optional all-header)
7531   "Display ARTICLE in article buffer."
7532   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7533                (with-current-buffer gnus-article-buffer
7534                  (eq major-mode 'gnus-article-mode)))
7535     (gnus-article-setup-buffer))
7536   (gnus-set-global-variables)
7537   (with-current-buffer gnus-article-buffer
7538     (setq gnus-article-charset gnus-newsgroup-charset)
7539     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7540     (mm-enable-multibyte))
7541   (if (null article)
7542       nil
7543     (prog1
7544         (if gnus-summary-display-article-function
7545             (funcall gnus-summary-display-article-function article all-header)
7546           (gnus-article-prepare article all-header))
7547       (gnus-run-hooks 'gnus-select-article-hook)
7548       (when (and gnus-current-article
7549                  (not (zerop gnus-current-article)))
7550         (gnus-summary-goto-subject gnus-current-article))
7551       (gnus-summary-recenter)
7552       (when (and gnus-use-trees gnus-show-threads)
7553         (gnus-possibly-generate-tree article)
7554         (gnus-highlight-selected-tree article))
7555       ;; Successfully display article.
7556       (gnus-article-set-window-start
7557        (cdr (assq article gnus-newsgroup-bookmarks))))))
7558
7559 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7560   "Select the current article.
7561 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7562 non-nil, the article will be re-fetched even if it already present in
7563 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7564 be displayed."
7565   ;; Make sure we are in the summary buffer to work around bbdb bug.
7566   (unless (eq major-mode 'gnus-summary-mode)
7567     (set-buffer gnus-summary-buffer))
7568   (let ((article (or article (gnus-summary-article-number)))
7569         (all-headers (not (not all-headers))) ;Must be t or nil.
7570         gnus-summary-display-article-function)
7571     (and (not pseudo)
7572          (gnus-summary-article-pseudo-p article)
7573          (error "This is a pseudo-article"))
7574     (with-current-buffer gnus-summary-buffer
7575       (if (or (and gnus-single-article-buffer
7576                    (or (null gnus-current-article)
7577                        (null gnus-article-current)
7578                        (null (get-buffer gnus-article-buffer))
7579                        (not (eq article (cdr gnus-article-current)))
7580                        (not (equal (car gnus-article-current)
7581                                    gnus-newsgroup-name))))
7582               (and (not gnus-single-article-buffer)
7583                    (or (null gnus-current-article)
7584                        (not (eq gnus-current-article article))))
7585               force)
7586           ;; The requested article is different from the current article.
7587           (progn
7588             (gnus-summary-display-article article all-headers)
7589             (when (gnus-buffer-live-p gnus-article-buffer)
7590               (with-current-buffer gnus-article-buffer
7591                 (if (not gnus-article-decoded-p) ;; a local variable
7592                     (mm-disable-multibyte))))
7593             (gnus-article-set-window-start
7594              (cdr (assq article gnus-newsgroup-bookmarks)))
7595             article)
7596         'old))))
7597
7598 (defun gnus-summary-force-verify-and-decrypt ()
7599   "Display buttons for signed/encrypted parts and verify/decrypt them."
7600   (interactive)
7601   (let ((mm-verify-option 'known)
7602         (mm-decrypt-option 'known)
7603         (gnus-article-emulate-mime t)
7604         (gnus-buttonized-mime-types (append (list "multipart/signed"
7605                                                   "multipart/encrypted")
7606                                             gnus-buttonized-mime-types)))
7607     (gnus-summary-select-article nil 'force)))
7608
7609 (defun gnus-summary-set-current-mark (&optional current-mark)
7610   "Obsolete function."
7611   nil)
7612
7613 (defun gnus-summary-next-article (&optional unread subject backward push)
7614   "Select the next article.
7615 If UNREAD, only unread articles are selected.
7616 If SUBJECT, only articles with SUBJECT are selected.
7617 If BACKWARD, the previous article is selected instead of the next."
7618   (interactive "P")
7619   ;; Make sure we are in the summary buffer.
7620   (unless (eq major-mode 'gnus-summary-mode)
7621     (set-buffer gnus-summary-buffer))
7622   (cond
7623    ;; Is there such an article?
7624    ((and (gnus-summary-search-forward unread subject backward)
7625          (or (gnus-summary-display-article (gnus-summary-article-number))
7626              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7627     (gnus-summary-position-point))
7628    ;; If not, we try the first unread, if that is wanted.
7629    ((and subject
7630          gnus-auto-select-same
7631          (gnus-summary-first-unread-article))
7632     (gnus-summary-position-point)
7633     (gnus-message 6 "Wrapped"))
7634    ;; Try to get next/previous article not displayed in this group.
7635    ((and gnus-auto-extend-newsgroup
7636          (not unread) (not subject))
7637     (gnus-summary-goto-article
7638      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7639      nil (count-lines (point-min) (point))))
7640    ;; Go to next/previous group.
7641    (t
7642     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7643       (gnus-summary-jump-to-group gnus-newsgroup-name))
7644     (let ((cmd (if (featurep 'xemacs)
7645                    last-command-char
7646                  last-command-event))
7647           (point
7648            (with-current-buffer gnus-group-buffer
7649              (point)))
7650           (group
7651            (if (eq gnus-keep-same-level 'best)
7652                (gnus-summary-best-group gnus-newsgroup-name)
7653              (gnus-summary-search-group backward gnus-keep-same-level))))
7654       ;; For some reason, the group window gets selected.  We change
7655       ;; it back.
7656       (select-window (get-buffer-window (current-buffer)))
7657       ;; Select next unread newsgroup automagically.
7658       (cond
7659        ((or (not gnus-auto-select-next)
7660             (not cmd))
7661         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7662        ((or (eq gnus-auto-select-next 'quietly)
7663             (and (eq gnus-auto-select-next 'slightly-quietly)
7664                  push)
7665             (and (eq gnus-auto-select-next 'almost-quietly)
7666                  (gnus-summary-last-article-p)))
7667         ;; Select quietly.
7668         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7669             (gnus-summary-exit)
7670           (gnus-message 7 "No more%s articles (%s)..."
7671                         (if unread " unread" "")
7672                         (if group (concat "selecting " group)
7673                           "exiting"))
7674           (gnus-summary-next-group nil group backward)))
7675        (t
7676         (when (gnus-key-press-event-p last-input-event)
7677           (gnus-summary-walk-group-buffer
7678            gnus-newsgroup-name cmd unread backward point))))))))
7679
7680 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7681   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7682                       (?\C-p (gnus-group-prev-unread-group 1))))
7683         (cursor-in-echo-area t)
7684         keve key group ended prompt)
7685     (with-current-buffer gnus-group-buffer
7686       (goto-char start)
7687       (setq group
7688             (if (eq gnus-keep-same-level 'best)
7689                 (gnus-summary-best-group gnus-newsgroup-name)
7690               (gnus-summary-search-group backward gnus-keep-same-level))))
7691     (while (not ended)
7692       (setq prompt
7693             (format
7694              "No more%s articles%s " (if unread " unread" "")
7695              (if (and group
7696                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7697                  (format " (Type %s for %s [%s])"
7698                          (single-key-description cmd)
7699                          (gnus-group-decoded-name group)
7700                          (gnus-group-unread group))
7701                (format " (Type %s to exit %s)"
7702                        (single-key-description cmd)
7703                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7704       ;; Confirm auto selection.
7705       (setq key (car (setq keve (gnus-read-event-char prompt)))
7706             ended t)
7707       (cond
7708        ((assq key keystrokes)
7709         (let ((obuf (current-buffer)))
7710           (switch-to-buffer gnus-group-buffer)
7711           (when group
7712             (gnus-group-jump-to-group group))
7713           (eval (cadr (assq key keystrokes)))
7714           (setq group (gnus-group-group-name))
7715           (switch-to-buffer obuf))
7716         (setq ended nil))
7717        ((equal key cmd)
7718         (if (or (not group)
7719                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7720             (gnus-summary-exit)
7721           (gnus-summary-next-group nil group backward)))
7722        (t
7723         (push (cdr keve) unread-command-events))))))
7724
7725 (defun gnus-summary-next-unread-article ()
7726   "Select unread article after current one."
7727   (interactive)
7728   (gnus-summary-next-article
7729    (or (not (eq gnus-summary-goto-unread 'never))
7730        (gnus-summary-last-article-p (gnus-summary-article-number)))
7731    (and gnus-auto-select-same
7732         (gnus-summary-article-subject))))
7733
7734 (defun gnus-summary-prev-article (&optional unread subject)
7735   "Select the article before the current one.
7736 If UNREAD is non-nil, only unread articles are selected."
7737   (interactive "P")
7738   (gnus-summary-next-article unread subject t))
7739
7740 (defun gnus-summary-prev-unread-article ()
7741   "Select unread article before current one."
7742   (interactive)
7743   (gnus-summary-prev-article
7744    (or (not (eq gnus-summary-goto-unread 'never))
7745        (gnus-summary-first-article-p (gnus-summary-article-number)))
7746    (and gnus-auto-select-same
7747         (gnus-summary-article-subject))))
7748
7749 (defun gnus-summary-next-page (&optional lines circular stop)
7750   "Show next page of the selected article.
7751 If at the end of the current article, select the next article.
7752 LINES says how many lines should be scrolled up.
7753
7754 If CIRCULAR is non-nil, go to the start of the article instead of
7755 selecting the next article when reaching the end of the current
7756 article.
7757
7758 If STOP is non-nil, just stop when reaching the end of the message.
7759
7760 Also see the variable `gnus-article-skip-boring'."
7761   (interactive "P")
7762   (setq gnus-summary-buffer (current-buffer))
7763   (gnus-set-global-variables)
7764   (let ((article (gnus-summary-article-number))
7765         (article-window (get-buffer-window gnus-article-buffer t))
7766         endp)
7767     ;; If the buffer is empty, we have no article.
7768     (unless article
7769       (error "No article to select"))
7770     (gnus-configure-windows 'article)
7771     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7772         (if (and (eq gnus-summary-goto-unread 'never)
7773                  (not (gnus-summary-last-article-p article)))
7774             (gnus-summary-next-article)
7775           (gnus-summary-next-unread-article))
7776       (if (or (null gnus-current-article)
7777               (null gnus-article-current)
7778               (/= article (cdr gnus-article-current))
7779               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7780           ;; Selected subject is different from current article's.
7781           (gnus-summary-display-article article)
7782         (when article-window
7783           (gnus-eval-in-buffer-window gnus-article-buffer
7784             (setq endp (or (gnus-article-next-page lines)
7785                            (gnus-article-only-boring-p))))
7786           (when endp
7787             (cond ((or stop gnus-summary-stop-at-end-of-message)
7788                    (gnus-message 3 "End of message"))
7789                   (circular
7790                    (gnus-summary-beginning-of-article))
7791                   (lines
7792                    (gnus-message 3 "End of message"))
7793                   ((null lines)
7794                    (if (and (eq gnus-summary-goto-unread 'never)
7795                             (not (gnus-summary-last-article-p article)))
7796                        (gnus-summary-next-article)
7797                      (gnus-summary-next-unread-article))))))))
7798     (gnus-summary-recenter)
7799     (gnus-summary-position-point)))
7800
7801 (defun gnus-summary-prev-page (&optional lines move)
7802   "Show previous page of selected article.
7803 Argument LINES specifies lines to be scrolled down.
7804 If MOVE, move to the previous unread article if point is at
7805 the beginning of the buffer."
7806   (interactive "P")
7807   (let ((article (gnus-summary-article-number))
7808         (article-window (get-buffer-window gnus-article-buffer t))
7809         endp)
7810     (gnus-configure-windows 'article)
7811     (if (or (null gnus-current-article)
7812             (null gnus-article-current)
7813             (/= article (cdr gnus-article-current))
7814             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7815         ;; Selected subject is different from current article's.
7816         (gnus-summary-display-article article)
7817       (gnus-summary-recenter)
7818       (when article-window
7819         (gnus-eval-in-buffer-window gnus-article-buffer
7820           (setq endp (gnus-article-prev-page lines)))
7821         (when (and move endp)
7822           (cond (lines
7823                  (gnus-message 3 "Beginning of message"))
7824                 ((null lines)
7825                  (if (and (eq gnus-summary-goto-unread 'never)
7826                           (not (gnus-summary-first-article-p article)))
7827                      (gnus-summary-prev-article)
7828                    (gnus-summary-prev-unread-article))))))))
7829   (gnus-summary-position-point))
7830
7831 (defun gnus-summary-prev-page-or-article (&optional lines)
7832   "Show previous page of selected article.
7833 Argument LINES specifies lines to be scrolled down.
7834 If at the beginning of the article, go to the next article."
7835   (interactive "P")
7836   (gnus-summary-prev-page lines t))
7837
7838 (defun gnus-summary-scroll-up (lines)
7839   "Scroll up (or down) one line current article.
7840 Argument LINES specifies lines to be scrolled up (or down if negative)."
7841   (interactive "p")
7842   (gnus-configure-windows 'article)
7843   (gnus-summary-show-thread)
7844   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7845     (gnus-eval-in-buffer-window gnus-article-buffer
7846       (cond ((> lines 0)
7847              (when (gnus-article-next-page lines)
7848                (gnus-message 3 "End of message")))
7849             ((< lines 0)
7850              (gnus-article-prev-page (- lines))))))
7851   (gnus-summary-recenter)
7852   (gnus-summary-position-point))
7853
7854 (defun gnus-summary-scroll-down (lines)
7855   "Scroll down (or up) one line current article.
7856 Argument LINES specifies lines to be scrolled down (or up if negative)."
7857   (interactive "p")
7858   (gnus-summary-scroll-up (- lines)))
7859
7860 (defun gnus-summary-next-same-subject ()
7861   "Select next article which has the same subject as current one."
7862   (interactive)
7863   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7864
7865 (defun gnus-summary-prev-same-subject ()
7866   "Select previous article which has the same subject as current one."
7867   (interactive)
7868   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7869
7870 (defun gnus-summary-next-unread-same-subject ()
7871   "Select next unread article which has the same subject as current one."
7872   (interactive)
7873   (gnus-summary-next-article t (gnus-summary-article-subject)))
7874
7875 (defun gnus-summary-prev-unread-same-subject ()
7876   "Select previous unread article which has the same subject as current one."
7877   (interactive)
7878   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7879
7880 (defun gnus-summary-first-unread-article ()
7881   "Select 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-display-article (gnus-summary-article-number)))
7889     (gnus-summary-position-point)))
7890
7891 (defun gnus-summary-first-unread-subject ()
7892   "Place the point on the subject line of the first unread article.
7893 Return nil if there are no unread articles."
7894   (interactive)
7895   (prog1
7896       (when (gnus-summary-first-subject t)
7897         (gnus-summary-show-thread)
7898         (gnus-summary-first-subject t))
7899     (gnus-summary-position-point)))
7900
7901 (defun gnus-summary-first-unseen-subject ()
7902   "Place the point on the subject line of the first unseen article.
7903 Return nil if there are no unseen articles."
7904   (interactive)
7905   (prog1
7906       (when (gnus-summary-first-subject nil nil t)
7907         (gnus-summary-show-thread)
7908         (gnus-summary-first-subject nil nil t))
7909     (gnus-summary-position-point)))
7910
7911 (defun gnus-summary-first-unseen-or-unread-subject ()
7912   "Place the point on the subject line of the first unseen and unread article.
7913 If all article have been seen, on the subject line of the first unread
7914 article."
7915   (interactive)
7916   (prog1
7917       (unless (when (gnus-summary-first-subject nil nil t)
7918                 (gnus-summary-show-thread)
7919                 (gnus-summary-first-subject nil nil t))
7920         (when (gnus-summary-first-subject t)
7921           (gnus-summary-show-thread)
7922           (gnus-summary-first-subject t)))
7923     (gnus-summary-position-point)))
7924
7925 (defun gnus-summary-first-article ()
7926   "Select the first article.
7927 Return nil if there are no articles."
7928   (interactive)
7929   (prog1
7930       (when (gnus-summary-first-subject)
7931         (gnus-summary-show-thread)
7932         (gnus-summary-first-subject)
7933         (gnus-summary-display-article (gnus-summary-article-number)))
7934     (gnus-summary-position-point)))
7935
7936 (defun gnus-summary-best-unread-article (&optional arg)
7937   "Select the unread article with the highest score.
7938 If given a prefix argument, select the next unread article that has a
7939 score higher than the default score."
7940   (interactive "P")
7941   (let ((article (if arg
7942                      (gnus-summary-better-unread-subject)
7943                    (gnus-summary-best-unread-subject))))
7944     (if article
7945         (gnus-summary-goto-article article)
7946       (error "No unread articles"))))
7947
7948 (defun gnus-summary-best-unread-subject ()
7949   "Select the unread subject with the highest score."
7950   (interactive)
7951   (let ((best -1000000)
7952         (data gnus-newsgroup-data)
7953         article score)
7954     (while data
7955       (and (gnus-data-unread-p (car data))
7956            (> (setq score
7957                     (gnus-summary-article-score (gnus-data-number (car data))))
7958               best)
7959            (setq best score
7960                  article (gnus-data-number (car data))))
7961       (setq data (cdr data)))
7962     (when article
7963       (gnus-summary-goto-subject article))
7964     (gnus-summary-position-point)
7965     article))
7966
7967 (defun gnus-summary-better-unread-subject ()
7968   "Select the first unread subject that has a score over the default score."
7969   (interactive)
7970   (let ((data gnus-newsgroup-data)
7971         article score)
7972     (while (and (setq article (gnus-data-number (car data)))
7973                 (or (gnus-data-read-p (car data))
7974                     (not (> (gnus-summary-article-score article)
7975                             gnus-summary-default-score))))
7976       (setq data (cdr data)))
7977     (when article
7978       (gnus-summary-goto-subject article))
7979     (gnus-summary-position-point)
7980     article))
7981
7982 (defun gnus-summary-last-subject ()
7983   "Go to the last displayed subject line in the group."
7984   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7985     (when article
7986       (gnus-summary-goto-subject article))))
7987
7988 (defun gnus-summary-goto-article (article &optional all-headers force)
7989   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7990 If ALL-HEADERS is non-nil, no header lines are hidden.
7991 If FORCE, go to the article even if it isn't displayed.  If FORCE
7992 is a number, it is the line the article is to be displayed on."
7993   (interactive
7994    (list
7995     (completing-read
7996      "Article number or Message-ID: "
7997      (mapcar (lambda (number) (list (int-to-string number)))
7998              gnus-newsgroup-limit))
7999     current-prefix-arg
8000     t))
8001   (prog1
8002       (if (and (stringp article)
8003                (string-match "@\\|%40" article))
8004           (gnus-summary-refer-article article)
8005         (when (stringp article)
8006           (setq article (string-to-number article)))
8007         (if (gnus-summary-goto-subject article force)
8008             (gnus-summary-display-article article all-headers)
8009           (gnus-message 4 "Couldn't go to article %s" article) nil))
8010     (gnus-summary-position-point)))
8011
8012 (defun gnus-summary-goto-last-article ()
8013   "Go to the previously read article."
8014   (interactive)
8015   (prog1
8016       (when gnus-last-article
8017         (gnus-summary-goto-article gnus-last-article nil t))
8018     (gnus-summary-position-point)))
8019
8020 (defun gnus-summary-pop-article (number)
8021   "Pop one article off the history and go to the previous.
8022 NUMBER articles will be popped off."
8023   (interactive "p")
8024   (let (to)
8025     (setq gnus-newsgroup-history
8026           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8027     (if to
8028         (gnus-summary-goto-article (car to) nil t)
8029       (error "Article history empty")))
8030   (gnus-summary-position-point))
8031
8032 ;; Summary commands and functions for limiting the summary buffer.
8033
8034 (defun gnus-summary-limit-to-articles (n)
8035   "Limit the summary buffer to the next N articles.
8036 If not given a prefix, use the process marked articles instead."
8037   (interactive "P")
8038   (prog1
8039       (let ((articles (gnus-summary-work-articles n)))
8040         (setq gnus-newsgroup-processable nil)
8041         (gnus-summary-limit articles))
8042     (gnus-summary-position-point)))
8043
8044 (defun gnus-summary-pop-limit (&optional total)
8045   "Restore the previous limit.
8046 If given a prefix, remove all limits."
8047   (interactive "P")
8048   (when total
8049     (setq gnus-newsgroup-limits
8050           (list (mapcar (lambda (h) (mail-header-number h))
8051                         gnus-newsgroup-headers))))
8052   (unless gnus-newsgroup-limits
8053     (error "No limit to pop"))
8054   (prog1
8055       (gnus-summary-limit nil 'pop)
8056     (gnus-summary-position-point)))
8057
8058 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8059   "Limit the summary buffer to articles that have subjects that match a regexp.
8060 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8061   (interactive
8062    (list (read-string (if current-prefix-arg
8063                           "Exclude subject (regexp): "
8064                         "Limit to subject (regexp): "))
8065          nil current-prefix-arg))
8066   (unless header
8067     (setq header "subject"))
8068   (when (not (equal "" subject))
8069     (prog1
8070         (let ((articles (gnus-summary-find-matching
8071                          (or header "subject") subject 'all nil nil
8072                          not-matching)))
8073           (unless articles
8074             (error "Found no matches for \"%s\"" subject))
8075           (gnus-summary-limit articles))
8076       (gnus-summary-position-point))))
8077
8078 (defun gnus-summary-limit-to-author (from &optional not-matching)
8079   "Limit the summary buffer to articles that have authors that match a regexp.
8080 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8081   (interactive
8082    (list (read-string (if current-prefix-arg
8083                           "Exclude author (regexp): "
8084                         "Limit to author (regexp): "))
8085          current-prefix-arg))
8086   (gnus-summary-limit-to-subject from "from" not-matching))
8087
8088 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8089   "Limit the summary buffer to articles with the given RECIPIENT.
8090
8091 If NOT-MATCHING, exclude RECIPIENT.
8092
8093 To and Cc headers are checked.  You need to include them in
8094 `nnmail-extra-headers'."
8095   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8096   (interactive
8097    (list (read-string (format "%s recipient (regexp): "
8098                               (if current-prefix-arg "Exclude" "Limit to")))
8099          current-prefix-arg))
8100   (when (not (equal "" recipient))
8101     (prog1 (let* ((to
8102                    (if (memq 'To nnmail-extra-headers)
8103                        (gnus-summary-find-matching
8104                         (cons 'extra 'To) recipient 'all nil nil
8105                         not-matching)
8106                      (gnus-message
8107                       1 "`To' isn't present in `nnmail-extra-headers'")
8108                      (sit-for 1)
8109                      nil))
8110                   (cc
8111                    (if (memq 'Cc nnmail-extra-headers)
8112                        (gnus-summary-find-matching
8113                         (cons 'extra 'Cc) recipient 'all nil nil
8114                         not-matching)
8115                      (gnus-message
8116                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8117                      (sit-for 1)
8118                      nil))
8119                   (articles
8120                    (if not-matching
8121                        ;; We need the numbers that are in both lists:
8122                        (mapcar (lambda (a)
8123                                  (and (memq a to) a))
8124                                cc)
8125                      (nconc to cc))))
8126              (unless articles
8127                (error "Found no matches for \"%s\"" recipient))
8128              (gnus-summary-limit articles))
8129       (gnus-summary-position-point))))
8130
8131 (defun gnus-summary-limit-to-address (address &optional not-matching)
8132   "Limit the summary buffer to articles with the given ADDRESS.
8133
8134 If NOT-MATCHING, exclude ADDRESS.
8135
8136 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8137 in `nnmail-extra-headers'."
8138   (interactive
8139    (list (read-string (format "%s address (regexp): "
8140                               (if current-prefix-arg "Exclude" "Limit to")))
8141          current-prefix-arg))
8142   (when (not (equal "" address))
8143     (prog1 (let* ((to
8144                    (if (memq 'To nnmail-extra-headers)
8145                        (gnus-summary-find-matching
8146                         (cons 'extra 'To) address 'all nil nil
8147                         not-matching)
8148                      (gnus-message
8149                       1 "`To' isn't present in `nnmail-extra-headers'")
8150                      (sit-for 1)
8151                      t))
8152                   (cc
8153                    (if (memq 'Cc nnmail-extra-headers)
8154                        (gnus-summary-find-matching
8155                         (cons 'extra 'Cc) address 'all nil nil
8156                         not-matching)
8157                      (gnus-message
8158                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8159                      (sit-for 1)
8160                      t))
8161                   (from
8162                    (gnus-summary-find-matching "from" address
8163                                                'all nil nil not-matching))
8164                   (articles
8165                    (if not-matching
8166                        ;; We need the numbers that are in all lists:
8167                        (if (eq cc t)
8168                            (if (eq to t)
8169                                from
8170                              (mapcar (lambda (a) (car (memq a from))) to))
8171                          (if (eq to t)
8172                              (mapcar (lambda (a) (car (memq a from))) cc)
8173                            (mapcar (lambda (a) (car (memq a from)))
8174                                    (mapcar (lambda (a) (car (memq a to)))
8175                                            cc))))
8176                      (nconc (if (eq to t) nil to)
8177                             (if (eq cc t) nil cc)
8178                             from))))
8179              (unless articles
8180                (error "Found no matches for \"%s\"" address))
8181              (gnus-summary-limit articles))
8182       (gnus-summary-position-point))))
8183
8184 (defun gnus-summary-limit-strange-charsets-predicate (header)
8185   (when (fboundp 'char-charset)
8186     (let ((string (concat (mail-header-subject header)
8187                           (mail-header-from header)))
8188           charset found)
8189       (dotimes (i (1- (length string)))
8190         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8191         (when (string-match "unicode\\|big\\|japanese" charset)
8192           (setq found t)))
8193       found)))
8194
8195 (defun gnus-summary-limit-to-predicate (predicate)
8196   "Limit to articles where PREDICATE returns non-nil.
8197 PREDICATE will be called with the header structures of the
8198 articles."
8199   (let ((articles nil)
8200         (case-fold-search t))
8201     (dolist (header gnus-newsgroup-headers)
8202       (when (funcall predicate header)
8203         (push (mail-header-number header) articles)))
8204     (gnus-summary-limit (nreverse articles))))
8205
8206 (defun gnus-summary-limit-to-age (age &optional younger-p)
8207   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8208 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8209 articles that are younger than AGE days."
8210   (interactive
8211    (let ((younger current-prefix-arg)
8212          (days-got nil)
8213          days)
8214      (while (not days-got)
8215        (setq days (if younger
8216                       (read-string "Limit to articles younger than (in days, older when negative): ")
8217                     (read-string
8218                      "Limit to articles older than (in days, younger when negative): ")))
8219        (when (> (length days) 0)
8220          (setq days (read days)))
8221        (if (numberp days)
8222            (progn
8223              (setq days-got t)
8224              (when (< days 0)
8225                (setq younger (not younger))
8226                (setq days (* days -1))))
8227          (message "Please enter a number.")
8228          (sleep-for 1)))
8229      (list days younger)))
8230   (prog1
8231       (let ((data gnus-newsgroup-data)
8232             (cutoff (days-to-time age))
8233             articles d date is-younger)
8234         (while (setq d (pop data))
8235           (when (and (vectorp (gnus-data-header d))
8236                      (setq date (mail-header-date (gnus-data-header d))))
8237             (setq is-younger (time-less-p
8238                               (time-since (gnus-date-get-time date))
8239                               cutoff))
8240             (when (if younger-p
8241                       is-younger
8242                     (not is-younger))
8243               (push (gnus-data-number d) articles))))
8244         (gnus-summary-limit (nreverse articles)))
8245     (gnus-summary-position-point)))
8246
8247 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8248   "Limit the summary buffer to articles that match an 'extra' header."
8249   (interactive
8250    (let ((header
8251           (intern
8252            (gnus-completing-read-with-default
8253             (symbol-name (car gnus-extra-headers))
8254             (if current-prefix-arg
8255                 "Exclude extra header"
8256               "Limit extra header")
8257             (mapcar (lambda (x)
8258                       (cons (symbol-name x) x))
8259                     gnus-extra-headers)
8260             nil
8261             t))))
8262      (list header
8263            (read-string (format "%s header %s (regexp): "
8264                                 (if current-prefix-arg "Exclude" "Limit to")
8265                                 header))
8266            current-prefix-arg)))
8267   (when (not (equal "" regexp))
8268     (prog1
8269         (let ((articles (gnus-summary-find-matching
8270                          (cons 'extra header) regexp 'all nil nil
8271                          not-matching)))
8272           (unless articles
8273             (error "Found no matches for \"%s\"" regexp))
8274           (gnus-summary-limit articles))
8275       (gnus-summary-position-point))))
8276
8277 (defun gnus-summary-limit-to-display-predicate ()
8278   "Limit the summary buffer to the predicated in the `display' group parameter."
8279   (interactive)
8280   (unless gnus-newsgroup-display
8281     (error "There is no `display' group parameter"))
8282   (let (articles)
8283     (dolist (number gnus-newsgroup-articles)
8284       (when (funcall gnus-newsgroup-display)
8285         (push number articles)))
8286     (gnus-summary-limit articles))
8287   (gnus-summary-position-point))
8288
8289 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8290 (make-obsolete
8291  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread "Emacs 20.4")
8292
8293 (defun gnus-summary-limit-to-unread (&optional all)
8294   "Limit the summary buffer to articles that are not marked as read.
8295 If ALL is non-nil, limit strictly to unread articles."
8296   (interactive "P")
8297   (if all
8298       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8299     (gnus-summary-limit-to-marks
8300      ;; Concat all the marks that say that an article is read and have
8301      ;; those removed.
8302      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8303            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8304            gnus-low-score-mark gnus-expirable-mark
8305            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8306            gnus-duplicate-mark)
8307      'reverse)))
8308
8309 (defun gnus-summary-limit-to-headers (match &optional reverse)
8310   "Limit the summary buffer to articles that have headers that match MATCH.
8311 If REVERSE (the prefix), limit to articles that don't match."
8312   (interactive "sMatch headers (regexp): \nP")
8313   (gnus-summary-limit-to-bodies match reverse t))
8314
8315 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8316   "Limit the summary buffer to articles that have bodies that match MATCH.
8317 If REVERSE (the prefix), limit to articles that don't match."
8318   (interactive "sMatch body (regexp): \nP")
8319   (let ((articles nil)
8320         (gnus-select-article-hook nil)  ;Disable hook.
8321         (gnus-article-prepare-hook nil)
8322         (gnus-use-article-prefetch nil)
8323         (gnus-keep-backlog nil)
8324         (gnus-break-pages nil)
8325         (gnus-summary-display-arrow nil)
8326         (gnus-updated-mode-lines nil)
8327         (gnus-auto-center-summary nil)
8328         (gnus-display-mime-function nil))
8329     (dolist (data gnus-newsgroup-data)
8330       (let (gnus-mark-article-hook)
8331         (gnus-summary-select-article t t nil (gnus-data-number data)))
8332       (with-current-buffer gnus-article-buffer
8333         (article-goto-body)
8334         (let* ((case-fold-search t)
8335                (found (if headersp
8336                           (re-search-backward match nil t)
8337                         (re-search-forward match nil t))))
8338           (when (or (and found
8339                          (not reverse))
8340                     (and (not found)
8341                          reverse))
8342             (push (gnus-data-number data) articles)))))
8343     (if (not articles)
8344         (message "No messages matched")
8345       (gnus-summary-limit articles)))
8346   (gnus-summary-position-point))
8347
8348 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8349   "Limit the summary buffer to articles that aren't part on any thread.
8350 If THREADSP (the prefix), limit to articles that are in threads."
8351   (interactive "P")
8352   (let ((articles nil)
8353         thread-articles
8354         threads)
8355     (dolist (thread gnus-newsgroup-threads)
8356       (if (stringp (car thread))
8357           (dolist (thread (cdr thread))
8358             (push thread threads))
8359         (push thread threads)))
8360     (dolist (thread threads)
8361       (setq thread-articles (gnus-articles-in-thread thread))
8362       (when (or (and threadsp
8363                      (> (length thread-articles) 1))
8364                 (and (not threadsp)
8365                      (= (length thread-articles) 1)))
8366         (setq articles (nconc thread-articles articles))))
8367     (if (not articles)
8368         (message "No messages matched")
8369       (gnus-summary-limit articles))
8370     (gnus-summary-position-point)))
8371
8372 (defun gnus-summary-limit-to-replied (&optional unreplied)
8373   "Limit the summary buffer to replied articles.
8374 If UNREPLIED (the prefix), limit to unreplied articles."
8375   (interactive "P")
8376   (if unreplied
8377       (gnus-summary-limit
8378        (gnus-set-difference gnus-newsgroup-articles
8379         gnus-newsgroup-replied))
8380     (gnus-summary-limit gnus-newsgroup-replied))
8381   (gnus-summary-position-point))
8382
8383 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8384 (make-obsolete 'gnus-summary-delete-marked-with
8385                'gnus-summary-limit-exclude-marks "Emacs 20.4")
8386
8387 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8388   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8389 If REVERSE, limit the summary buffer to articles that are marked
8390 with MARKS.  MARKS can either be a string of marks or a list of marks.
8391 Returns how many articles were removed."
8392   (interactive "sMarks: ")
8393   (gnus-summary-limit-to-marks marks t))
8394
8395 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8396   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8397 If REVERSE (the prefix), limit the summary buffer to articles that are
8398 not marked with MARKS.  MARKS can either be a string of marks or a
8399 list of marks.
8400 Returns how many articles were removed."
8401   (interactive "sMarks: \nP")
8402   (prog1
8403       (let ((data gnus-newsgroup-data)
8404             (marks (if (listp marks) marks
8405                      (append marks nil))) ; Transform to list.
8406             articles)
8407         (while data
8408           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8409                   (memq (gnus-data-mark (car data)) marks))
8410             (push (gnus-data-number (car data)) articles))
8411           (setq data (cdr data)))
8412         (gnus-summary-limit articles))
8413     (gnus-summary-position-point)))
8414
8415 (defun gnus-summary-limit-to-score (score)
8416   "Limit to articles with score at or above SCORE."
8417   (interactive "NLimit to articles with score of at least: ")
8418   (let ((data gnus-newsgroup-data)
8419         articles)
8420     (while data
8421       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8422                 score)
8423         (push (gnus-data-number (car data)) articles))
8424       (setq data (cdr data)))
8425     (prog1
8426         (gnus-summary-limit articles)
8427       (gnus-summary-position-point))))
8428
8429 (defun gnus-summary-limit-to-unseen ()
8430   "Limit to unseen articles."
8431   (interactive)
8432   (prog1
8433       (gnus-summary-limit gnus-newsgroup-unseen)
8434     (gnus-summary-position-point)))
8435
8436 (defun gnus-summary-limit-include-thread (id)
8437   "Display all the hidden articles that is in the thread with ID in it.
8438 When called interactively, ID is the Message-ID of the current
8439 article."
8440   (interactive (list (mail-header-id (gnus-summary-article-header))))
8441   (let ((articles (gnus-articles-in-thread
8442                    (gnus-id-to-thread (gnus-root-id id)))))
8443     (prog1
8444         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8445       (gnus-summary-limit-include-matching-articles
8446        "subject"
8447        (regexp-quote (gnus-simplify-subject-re
8448                       (mail-header-subject (gnus-id-to-header id)))))
8449       (gnus-summary-position-point))))
8450
8451 (defun gnus-summary-limit-include-matching-articles (header regexp)
8452   "Display all the hidden articles that have HEADERs that match REGEXP."
8453   (interactive (list (read-string "Match on header: ")
8454                      (read-string "Regexp: ")))
8455   (let ((articles (gnus-find-matching-articles header regexp)))
8456     (prog1
8457         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8458       (gnus-summary-position-point))))
8459
8460 (defun gnus-summary-insert-dormant-articles ()
8461   "Insert all the dormant articles for this group into the current buffer."
8462   (interactive)
8463   (let ((gnus-verbose (max 6 gnus-verbose)))
8464     (if (not gnus-newsgroup-dormant)
8465         (gnus-message 3 "No dormant articles for this group")
8466       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8467
8468 (defun gnus-summary-insert-ticked-articles ()
8469   "Insert ticked articles for this group into the current buffer."
8470   (interactive)
8471   (let ((gnus-verbose (max 6 gnus-verbose)))
8472     (if (not gnus-newsgroup-marked)
8473         (gnus-message 3 "No ticked articles for this group")
8474       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8475
8476 (defun gnus-summary-limit-include-dormant ()
8477   "Display all the hidden articles that are marked as dormant.
8478 Note that this command only works on a subset of the articles currently
8479 fetched for this group."
8480   (interactive)
8481   (unless gnus-newsgroup-dormant
8482     (error "There are no dormant articles in this group"))
8483   (prog1
8484       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8485     (gnus-summary-position-point)))
8486
8487 (defun gnus-summary-limit-exclude-dormant ()
8488   "Hide all dormant articles."
8489   (interactive)
8490   (prog1
8491       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8492     (gnus-summary-position-point)))
8493
8494 (defun gnus-summary-limit-exclude-childless-dormant ()
8495   "Hide all dormant articles that have no children."
8496   (interactive)
8497   (let ((data (gnus-data-list t))
8498         articles d children)
8499     ;; Find all articles that are either not dormant or have
8500     ;; children.
8501     (while (setq d (pop data))
8502       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8503                 (and (setq children
8504                            (gnus-article-children (gnus-data-number d)))
8505                      (let (found)
8506                        (while children
8507                          (when (memq (car children) articles)
8508                            (setq children nil
8509                                  found t))
8510                          (pop children))
8511                        found)))
8512         (push (gnus-data-number d) articles)))
8513     ;; Do the limiting.
8514     (prog1
8515         (gnus-summary-limit articles)
8516       (gnus-summary-position-point))))
8517
8518 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8519   "Mark all unread excluded articles as read.
8520 If ALL, mark even excluded ticked and dormants as read."
8521   (interactive "P")
8522   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8523   (let ((articles (gnus-sorted-ndifference
8524                    (sort
8525                     (mapcar (lambda (h) (mail-header-number h))
8526                             gnus-newsgroup-headers)
8527                     '<)
8528                    gnus-newsgroup-limit))
8529         article)
8530     (setq gnus-newsgroup-unreads
8531           (gnus-sorted-intersection gnus-newsgroup-unreads
8532                                     gnus-newsgroup-limit))
8533     (if all
8534         (setq gnus-newsgroup-dormant nil
8535               gnus-newsgroup-marked nil
8536               gnus-newsgroup-reads
8537               (nconc
8538                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8539                gnus-newsgroup-reads))
8540       (while (setq article (pop articles))
8541         (unless (or (memq article gnus-newsgroup-dormant)
8542                     (memq article gnus-newsgroup-marked))
8543           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8544
8545 (defun gnus-summary-limit (articles &optional pop)
8546   (if pop
8547       ;; We pop the previous limit off the stack and use that.
8548       (setq articles (car gnus-newsgroup-limits)
8549             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8550     ;; We use the new limit, so we push the old limit on the stack.
8551     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8552   ;; Set the limit.
8553   (setq gnus-newsgroup-limit articles)
8554   (let ((total (length gnus-newsgroup-data))
8555         (data (gnus-data-find-list (gnus-summary-article-number)))
8556         (gnus-summary-mark-below nil)   ; Inhibit this.
8557         found)
8558     ;; This will do all the work of generating the new summary buffer
8559     ;; according to the new limit.
8560     (gnus-summary-prepare)
8561     ;; Hide any threads, possibly.
8562     (gnus-summary-maybe-hide-threads)
8563     ;; Try to return to the article you were at, or one in the
8564     ;; neighborhood.
8565     (when data
8566       ;; We try to find some article after the current one.
8567       (while data
8568         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8569           (setq data nil
8570                 found t))
8571         (setq data (cdr data))))
8572     (unless found
8573       ;; If there is no data, that means that we were after the last
8574       ;; article.  The same goes when we can't find any articles
8575       ;; after the current one.
8576       (goto-char (point-max))
8577       (gnus-summary-find-prev))
8578     (gnus-set-mode-line 'summary)
8579     ;; We return how many articles were removed from the summary
8580     ;; buffer as a result of the new limit.
8581     (- total (length gnus-newsgroup-data))))
8582
8583 (defsubst gnus-invisible-cut-children (threads)
8584   (let ((num 0))
8585     (while threads
8586       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8587         (incf num))
8588       (pop threads))
8589     (< num 2)))
8590
8591 (defsubst gnus-cut-thread (thread)
8592   "Go forwards in the thread until we find an article that we want to display."
8593   (when (or (eq gnus-fetch-old-headers 'some)
8594             (eq gnus-fetch-old-headers 'invisible)
8595             (numberp gnus-fetch-old-headers)
8596             (eq gnus-build-sparse-threads 'some)
8597             (eq gnus-build-sparse-threads 'more))
8598     ;; Deal with old-fetched headers and sparse threads.
8599     (while (and
8600             thread
8601             (or
8602              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8603              (gnus-summary-article-ancient-p
8604               (mail-header-number (car thread))))
8605             (if (or (<= (length (cdr thread)) 1)
8606                     (eq gnus-fetch-old-headers 'invisible))
8607                 (setq gnus-newsgroup-limit
8608                       (delq (mail-header-number (car thread))
8609                             gnus-newsgroup-limit)
8610                       thread (cadr thread))
8611               (when (gnus-invisible-cut-children (cdr thread))
8612                 (let ((th (cdr thread)))
8613                   (while th
8614                     (if (memq (mail-header-number (caar th))
8615                               gnus-newsgroup-limit)
8616                         (setq thread (car th)
8617                               th nil)
8618                       (setq th (cdr th))))))))))
8619   thread)
8620
8621 (defun gnus-cut-threads (threads)
8622   "Cut off all uninteresting articles from the beginning of THREADS."
8623   (when (or (eq gnus-fetch-old-headers 'some)
8624             (eq gnus-fetch-old-headers 'invisible)
8625             (numberp gnus-fetch-old-headers)
8626             (eq gnus-build-sparse-threads 'some)
8627             (eq gnus-build-sparse-threads 'more))
8628     (let ((th threads))
8629       (while th
8630         (setcar th (gnus-cut-thread (car th)))
8631         (setq th (cdr th)))))
8632   ;; Remove nixed out threads.
8633   (delq nil threads))
8634
8635 (defun gnus-summary-initial-limit (&optional show-if-empty)
8636   "Figure out what the initial limit is supposed to be on group entry.
8637 This entails weeding out unwanted dormants, low-scored articles,
8638 fetch-old-headers verbiage, and so on."
8639   ;; Most groups have nothing to remove.
8640   (unless (or gnus-inhibit-limiting
8641               (and (null gnus-newsgroup-dormant)
8642                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8643                    (not (eq gnus-fetch-old-headers 'some))
8644                    (not (numberp gnus-fetch-old-headers))
8645                    (not (eq gnus-fetch-old-headers 'invisible))
8646                    (null gnus-summary-expunge-below)
8647                    (not (eq gnus-build-sparse-threads 'some))
8648                    (not (eq gnus-build-sparse-threads 'more))
8649                    (null gnus-thread-expunge-below)))
8650     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8651     (setq gnus-newsgroup-limit nil)
8652     (mapatoms
8653      (lambda (node)
8654        (unless (car (symbol-value node))
8655          ;; These threads have no parents -- they are roots.
8656          (let ((nodes (cdr (symbol-value node)))
8657                thread)
8658            (while nodes
8659              (if (and gnus-thread-expunge-below
8660                       (< (gnus-thread-total-score (car nodes))
8661                          gnus-thread-expunge-below))
8662                  (gnus-expunge-thread (pop nodes))
8663                (setq thread (pop nodes))
8664                (gnus-summary-limit-children thread))))))
8665      gnus-newsgroup-dependencies)
8666     ;; If this limitation resulted in an empty group, we might
8667     ;; pop the previous limit and use it instead.
8668     (when (and (not gnus-newsgroup-limit)
8669                show-if-empty)
8670       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8671     gnus-newsgroup-limit))
8672
8673 (defun gnus-summary-limit-children (thread)
8674   "Return 1 if this subthread is visible and 0 if it is not."
8675   ;; First we get the number of visible children to this thread.  This
8676   ;; is done by recursing down the thread using this function, so this
8677   ;; will really go down to a leaf article first, before slowly
8678   ;; working its way up towards the root.
8679   (when thread
8680     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8681            (children
8682            (if (cdr thread)
8683                (apply '+ (mapcar 'gnus-summary-limit-children
8684                                  (cdr thread)))
8685              0))
8686           (number (mail-header-number (car thread)))
8687           score)
8688       (if (and
8689            (not (memq number gnus-newsgroup-marked))
8690            (or
8691             ;; If this article is dormant and has absolutely no visible
8692             ;; children, then this article isn't visible.
8693             (and (memq number gnus-newsgroup-dormant)
8694                  (zerop children))
8695             ;; If this is "fetch-old-headered" and there is no
8696             ;; visible children, then we don't want this article.
8697             (and (or (eq gnus-fetch-old-headers 'some)
8698                      (numberp gnus-fetch-old-headers))
8699                  (gnus-summary-article-ancient-p number)
8700                  (zerop children))
8701             ;; If this is "fetch-old-headered" and `invisible', then
8702             ;; we don't want this article.
8703             (and (eq gnus-fetch-old-headers 'invisible)
8704                  (gnus-summary-article-ancient-p number))
8705             ;; If this is a sparsely inserted article with no children,
8706             ;; we don't want it.
8707             (and (eq gnus-build-sparse-threads 'some)
8708                  (gnus-summary-article-sparse-p number)
8709                  (zerop children))
8710             ;; If we use expunging, and this article is really
8711             ;; low-scored, then we don't want this article.
8712             (when (and gnus-summary-expunge-below
8713                        (< (setq score
8714                                 (or (cdr (assq number gnus-newsgroup-scored))
8715                                     gnus-summary-default-score))
8716                           gnus-summary-expunge-below))
8717               ;; We increase the expunge-tally here, but that has
8718               ;; nothing to do with the limits, really.
8719               (incf gnus-newsgroup-expunged-tally)
8720               ;; We also mark as read here, if that's wanted.
8721               (when (and gnus-summary-mark-below
8722                          (< score gnus-summary-mark-below))
8723                 (setq gnus-newsgroup-unreads
8724                       (delq number gnus-newsgroup-unreads))
8725                 (if gnus-newsgroup-auto-expire
8726                     (push number gnus-newsgroup-expirable)
8727                   (push (cons number gnus-low-score-mark)
8728                         gnus-newsgroup-reads)))
8729               t)
8730             ;; Do the `display' group parameter.
8731             (and gnus-newsgroup-display
8732                  (not (funcall gnus-newsgroup-display)))))
8733           ;; Nope, invisible article.
8734           0
8735         ;; Ok, this article is to be visible, so we add it to the limit
8736         ;; and return 1.
8737         (push number gnus-newsgroup-limit)
8738         1))))
8739
8740 (defun gnus-expunge-thread (thread)
8741   "Mark all articles in THREAD as read."
8742   (let* ((number (mail-header-number (car thread))))
8743     (incf gnus-newsgroup-expunged-tally)
8744     ;; We also mark as read here, if that's wanted.
8745     (setq gnus-newsgroup-unreads
8746           (delq number gnus-newsgroup-unreads))
8747     (if gnus-newsgroup-auto-expire
8748         (push number gnus-newsgroup-expirable)
8749       (push (cons number gnus-low-score-mark)
8750             gnus-newsgroup-reads)))
8751   ;; Go recursively through all subthreads.
8752   (mapcar 'gnus-expunge-thread (cdr thread)))
8753
8754 ;; Summary article oriented commands
8755
8756 (defun gnus-summary-refer-parent-article (n)
8757   "Refer parent article N times.
8758 If N is negative, go to ancestor -N instead.
8759 The difference between N and the number of articles fetched is returned."
8760   (interactive "p")
8761   (let ((skip 1)
8762         error header ref)
8763     (when (not (natnump n))
8764       (setq skip (abs n)
8765             n 1))
8766     (while (and (> n 0)
8767                 (not error))
8768       (setq header (gnus-summary-article-header))
8769       (if (and (eq (mail-header-number header)
8770                    (cdr gnus-article-current))
8771                (equal gnus-newsgroup-name
8772                       (car gnus-article-current)))
8773           ;; If we try to find the parent of the currently
8774           ;; displayed article, then we take a look at the actual
8775           ;; References header, since this is slightly more
8776           ;; reliable than the References field we got from the
8777           ;; server.
8778           (with-current-buffer gnus-original-article-buffer
8779             (nnheader-narrow-to-headers)
8780             (unless (setq ref (message-fetch-field "references"))
8781               (when (setq ref (message-fetch-field "in-reply-to"))
8782                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8783             (widen))
8784         (setq ref
8785               ;; It's not the current article, so we take a bet on
8786               ;; the value we got from the server.
8787               (mail-header-references header)))
8788       (if (and ref
8789                (not (equal ref "")))
8790           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8791             (gnus-message 1 "Couldn't find parent"))
8792         (gnus-message 1 "No references in article %d"
8793                       (gnus-summary-article-number))
8794         (setq error t))
8795       (decf n))
8796     (gnus-summary-position-point)
8797     n))
8798
8799 (defun gnus-summary-refer-references ()
8800   "Fetch all articles mentioned in the References header.
8801 Return the number of articles fetched."
8802   (interactive)
8803   (let ((ref (mail-header-references (gnus-summary-article-header)))
8804         (current (gnus-summary-article-number))
8805         (n 0))
8806     (if (or (not ref)
8807             (equal ref ""))
8808         (error "No References in the current article")
8809       ;; For each Message-ID in the References header...
8810       (while (string-match "<[^>]*>" ref)
8811         (incf n)
8812         ;; ... fetch that article.
8813         (gnus-summary-refer-article
8814          (prog1 (match-string 0 ref)
8815            (setq ref (substring ref (match-end 0))))))
8816       (gnus-summary-goto-subject current)
8817       (gnus-summary-position-point)
8818       n)))
8819
8820 (defun gnus-summary-refer-thread (&optional limit)
8821   "Fetch all articles in the current thread.
8822 If LIMIT (the numerical prefix), fetch that many old headers instead
8823 of what's specified by the `gnus-refer-thread-limit' variable."
8824   (interactive "P")
8825   (let ((id (mail-header-id (gnus-summary-article-header)))
8826         (limit (if limit (prefix-numeric-value limit)
8827                  gnus-refer-thread-limit)))
8828     (unless (eq gnus-fetch-old-headers 'invisible)
8829       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8830       ;; Retrieve the headers and read them in.
8831       (if (eq (if (numberp limit)
8832                   (gnus-retrieve-headers
8833                    (list (min
8834                           (+ (mail-header-number
8835                               (gnus-summary-article-header))
8836                              limit)
8837                           gnus-newsgroup-end))
8838                    gnus-newsgroup-name (* limit 2))
8839                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8840                 ;; headers.
8841                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8842                                        gnus-newsgroup-name limit))
8843               'nov)
8844           (gnus-build-all-threads)
8845         (error "Can't fetch thread from back ends that don't support NOV"))
8846       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8847     (gnus-summary-limit-include-thread id)))
8848
8849 (defun gnus-summary-refer-article (message-id)
8850   "Fetch an article specified by MESSAGE-ID."
8851   (interactive "sMessage-ID: ")
8852   (when (and (stringp message-id)
8853              (not (zerop (length message-id))))
8854     (setq message-id (gnus-replace-in-string message-id " " ""))
8855     ;; Construct the correct Message-ID if necessary.
8856     ;; Suggested by tale@pawl.rpi.edu.
8857     (unless (string-match "^<" message-id)
8858       (setq message-id (concat "<" message-id)))
8859     (unless (string-match ">$" message-id)
8860       (setq message-id (concat message-id ">")))
8861     ;; People often post MIDs from URLs, so unhex it:
8862     (unless (string-match "@" message-id)
8863       (setq message-id (gnus-url-unhex-string message-id)))
8864     (let* ((header (gnus-id-to-header message-id))
8865            (sparse (and header
8866                         (gnus-summary-article-sparse-p
8867                          (mail-header-number header))
8868                         (memq (mail-header-number header)
8869                               gnus-newsgroup-limit)))
8870            number)
8871       (cond
8872        ;; If the article is present in the buffer we just go to it.
8873        ((and header
8874              (or (not (gnus-summary-article-sparse-p
8875                        (mail-header-number header)))
8876                  sparse))
8877         (prog1
8878             (gnus-summary-goto-article
8879              (mail-header-number header) nil t)
8880           (when sparse
8881             (gnus-summary-update-article (mail-header-number header)))))
8882        (t
8883         ;; We fetch the article.
8884         (catch 'found
8885           (dolist (gnus-override-method (gnus-refer-article-methods))
8886             (when (and (gnus-check-server gnus-override-method)
8887                        ;; Fetch the header,
8888                        (setq number (gnus-summary-insert-subject message-id)))
8889               ;; and display the article.
8890               (gnus-summary-select-article nil nil nil number)
8891               (throw 'found t)))
8892           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8893
8894 (defun gnus-refer-article-methods ()
8895   "Return a list of referable methods."
8896   (cond
8897    ;; No method, so we default to current and native.
8898    ((null gnus-refer-article-method)
8899     (list gnus-current-select-method gnus-select-method))
8900    ;; Current.
8901    ((eq 'current gnus-refer-article-method)
8902     (list gnus-current-select-method))
8903    ;; List of select methods.
8904    ((not (and (symbolp (car gnus-refer-article-method))
8905               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8906     (let (out)
8907       (dolist (method gnus-refer-article-method)
8908         (push (if (eq 'current method)
8909                   gnus-current-select-method
8910                 method)
8911               out))
8912       (nreverse out)))
8913    ;; One single select method.
8914    (t
8915     (list gnus-refer-article-method))))
8916
8917 (defun gnus-summary-edit-parameters ()
8918   "Edit the group parameters of the current group."
8919   (interactive)
8920   (gnus-group-edit-group gnus-newsgroup-name 'params))
8921
8922 (defun gnus-summary-customize-parameters ()
8923   "Customize the group parameters of the current group."
8924   (interactive)
8925   (gnus-group-customize gnus-newsgroup-name))
8926
8927 (defun gnus-summary-enter-digest-group (&optional force)
8928   "Enter an nndoc group based on the current article.
8929 If FORCE, force a digest interpretation.  If not, try
8930 to guess what the document format is."
8931   (interactive "P")
8932   (let ((conf gnus-current-window-configuration))
8933     (save-window-excursion
8934       (save-excursion
8935         (let (gnus-article-prepare-hook
8936               gnus-display-mime-function
8937               gnus-break-pages)
8938           (gnus-summary-select-article))))
8939     (setq gnus-current-window-configuration conf)
8940     (let* ((name (format "%s-%d"
8941                          (gnus-group-prefixed-name
8942                           gnus-newsgroup-name (list 'nndoc ""))
8943                          (with-current-buffer gnus-summary-buffer
8944                            gnus-current-article)))
8945            (ogroup gnus-newsgroup-name)
8946            (params (append (gnus-info-params (gnus-get-info ogroup))
8947                            (list (cons 'to-group ogroup))
8948                            (list (cons 'parent-group ogroup))
8949                            (list (cons 'save-article-group ogroup))))
8950            (case-fold-search t)
8951            (buf (current-buffer))
8952            dig to-address)
8953       (with-current-buffer gnus-original-article-buffer
8954         ;; Have the digest group inherit the main mail address of
8955         ;; the parent article.
8956         (when (setq to-address (or (gnus-fetch-field "reply-to")
8957                                    (gnus-fetch-field "from")))
8958           (setq params
8959                 (append
8960                  (list (cons 'to-address
8961                              (funcall gnus-decode-encoded-address-function
8962                                       to-address))))))
8963         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8964         (insert-buffer-substring gnus-original-article-buffer)
8965         ;; Remove lines that may lead nndoc to misinterpret the
8966         ;; document type.
8967         (narrow-to-region
8968          (goto-char (point-min))
8969          (or (search-forward "\n\n" nil t) (point)))
8970         (goto-char (point-min))
8971         (delete-matching-lines "^Path:\\|^From ")
8972         (widen))
8973       (unwind-protect
8974           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8975                     (gnus-newsgroup-ephemeral-ignored-charsets
8976                      gnus-newsgroup-ignored-charsets))
8977                 (gnus-group-read-ephemeral-group
8978                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8979                               (nndoc-article-type
8980                                ,(if force 'mbox 'guess)))
8981                  t nil nil nil
8982                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8983                                                         "ADAPT")))))
8984               ;; Make all postings to this group go to the parent group.
8985               (nconc (gnus-info-params (gnus-get-info name))
8986                      params)
8987             ;; Couldn't select this doc group.
8988             (switch-to-buffer buf)
8989             (gnus-set-global-variables)
8990             (gnus-configure-windows 'summary)
8991             (gnus-message 3 "Article couldn't be entered?"))
8992         (kill-buffer dig)))))
8993
8994 (defun gnus-summary-read-document (n)
8995   "Open a new group based on the current article(s).
8996 This will allow you to read digests and other similar
8997 documents as newsgroups.
8998 Obeys the standard process/prefix convention."
8999   (interactive "P")
9000   (let* ((ogroup gnus-newsgroup-name)
9001          (params (append (gnus-info-params (gnus-get-info ogroup))
9002                          (list (cons 'to-group ogroup))))
9003          group egroup groups vgroup)
9004     (dolist (article (gnus-summary-work-articles n))
9005       (setq group (format "%s-%d" gnus-newsgroup-name article))
9006       (gnus-summary-remove-process-mark article)
9007       (when (gnus-summary-display-article article)
9008         (save-excursion ;;What for?
9009           (with-temp-buffer
9010             (insert-buffer-substring gnus-original-article-buffer)
9011             ;; Remove some headers that may lead nndoc to make
9012             ;; the wrong guess.
9013             (message-narrow-to-head)
9014             (goto-char (point-min))
9015             (delete-matching-lines "^Path:\\|^From ")
9016             (widen)
9017             (if (setq egroup
9018                       (gnus-group-read-ephemeral-group
9019                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9020                                      (nndoc-article-type guess))
9021                        t nil t))
9022                 (progn
9023                   ;; Make all postings to this group go to the parent group.
9024                   (nconc (gnus-info-params (gnus-get-info egroup))
9025                          params)
9026                   (push egroup groups))
9027               ;; Couldn't select this doc group.
9028               (gnus-error 3 "Article couldn't be entered"))))))
9029     ;; Now we have selected all the documents.
9030     (cond
9031      ((not groups)
9032       (error "None of the articles could be interpreted as documents"))
9033      ((gnus-group-read-ephemeral-group
9034        (setq vgroup (format
9035                      "nnvirtual:%s-%s" gnus-newsgroup-name
9036                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9037        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9038        t
9039        (cons (current-buffer) 'summary)))
9040      (t
9041       (error "Couldn't select virtual nndoc group")))))
9042
9043 (defun gnus-summary-isearch-article (&optional regexp-p)
9044   "Do incremental search forward on the current article.
9045 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9046   (interactive "P")
9047   (gnus-summary-select-article)
9048   (gnus-configure-windows 'article)
9049   (gnus-eval-in-buffer-window gnus-article-buffer
9050     (save-restriction
9051       (widen)
9052       (isearch-forward regexp-p))))
9053
9054 (defun gnus-summary-repeat-search-article-forward ()
9055   "Repeat the previous search forwards."
9056   (interactive)
9057   (unless gnus-last-search-regexp
9058     (error "No previous search"))
9059   (gnus-summary-search-article-forward gnus-last-search-regexp))
9060
9061 (defun gnus-summary-repeat-search-article-backward ()
9062   "Repeat the previous search backwards."
9063   (interactive)
9064   (unless gnus-last-search-regexp
9065     (error "No previous search"))
9066   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9067
9068 (defun gnus-summary-search-article-forward (regexp &optional backward)
9069   "Search for an article containing REGEXP forward.
9070 If BACKWARD, search backward instead."
9071   (interactive
9072    (list (read-string
9073           (format "Search article %s (regexp%s): "
9074                   (if current-prefix-arg "backward" "forward")
9075                   (if gnus-last-search-regexp
9076                       (concat ", default " gnus-last-search-regexp)
9077                     "")))
9078          current-prefix-arg))
9079   (if (string-equal regexp "")
9080       (setq regexp (or gnus-last-search-regexp ""))
9081     (setq gnus-last-search-regexp regexp)
9082     (setq gnus-article-before-search gnus-current-article))
9083   ;; Intentionally set gnus-last-article.
9084   (setq gnus-last-article gnus-article-before-search)
9085   (let ((gnus-last-article gnus-last-article))
9086     (if (gnus-summary-search-article regexp backward)
9087         (gnus-summary-show-thread)
9088       (signal 'search-failed (list regexp)))))
9089
9090 (defun gnus-summary-search-article-backward (regexp)
9091   "Search for an article containing REGEXP backward."
9092   (interactive
9093    (list (read-string
9094           (format "Search article backward (regexp%s): "
9095                   (if gnus-last-search-regexp
9096                       (concat ", default " gnus-last-search-regexp)
9097                     "")))))
9098   (gnus-summary-search-article-forward regexp 'backward))
9099
9100 (defun gnus-summary-search-article (regexp &optional backward)
9101   "Search for an article containing REGEXP.
9102 Optional argument BACKWARD means do search for backward.
9103 `gnus-select-article-hook' is not called during the search."
9104   ;; We have to require this here to make sure that the following
9105   ;; dynamic binding isn't shadowed by autoloading.
9106   (require 'gnus-async)
9107   (require 'gnus-art)
9108   (let ((gnus-select-article-hook nil)  ;Disable hook.
9109         (gnus-article-prepare-hook nil)
9110         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9111         (gnus-use-article-prefetch nil)
9112         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9113         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9114         (gnus-visual nil)
9115         (gnus-keep-backlog nil)
9116         (gnus-break-pages nil)
9117         (gnus-summary-display-arrow nil)
9118         (gnus-updated-mode-lines nil)
9119         (gnus-auto-center-summary nil)
9120         (sum (current-buffer))
9121         (gnus-display-mime-function nil)
9122         (found nil)
9123         point)
9124     (gnus-save-hidden-threads
9125       (gnus-summary-select-article)
9126       (set-buffer gnus-article-buffer)
9127       (goto-char (window-point (get-buffer-window (current-buffer))))
9128       (when backward
9129         (forward-line -1))
9130       (while (not found)
9131         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9132         (if (if backward
9133                 (re-search-backward regexp nil t)
9134               (re-search-forward regexp nil t))
9135             ;; We found the regexp.
9136             (progn
9137               (setq found 'found)
9138               (beginning-of-line)
9139               (set-window-start
9140                (get-buffer-window (current-buffer))
9141                (point))
9142               (forward-line 1)
9143               (set-window-point
9144                (get-buffer-window (current-buffer))
9145                (point))
9146               (set-buffer sum)
9147               (setq point (point)))
9148           ;; We didn't find it, so we go to the next article.
9149           (set-buffer sum)
9150           (setq found 'not)
9151           (while (eq found 'not)
9152             (if (not (if backward (gnus-summary-find-prev)
9153                        (gnus-summary-find-next)))
9154                 ;; No more articles.
9155                 (setq found t)
9156               ;; Select the next article and adjust point.
9157               (unless (gnus-summary-article-sparse-p
9158                        (gnus-summary-article-number))
9159                 (setq found nil)
9160                 (gnus-summary-select-article)
9161                 (set-buffer gnus-article-buffer)
9162                 (widen)
9163                 (goto-char (if backward (point-max) (point-min))))))))
9164       (gnus-message 7 ""))
9165     ;; Return whether we found the regexp.
9166     (when (eq found 'found)
9167       (goto-char point)
9168       (gnus-summary-show-thread)
9169       (gnus-summary-goto-subject gnus-current-article)
9170       (gnus-summary-position-point)
9171       t)))
9172
9173 (defun gnus-find-matching-articles (header regexp)
9174   "Return a list of all articles that match REGEXP on HEADER.
9175 This search includes all articles in the current group that Gnus has
9176 fetched headers for, whether they are displayed or not."
9177   (let ((articles nil)
9178         ;; Can't eta-reduce because it's a macro.
9179         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9180         (case-fold-search t))
9181     (dolist (header gnus-newsgroup-headers)
9182       (when (string-match regexp (funcall func header))
9183         (push (mail-header-number header) articles)))
9184     (nreverse articles)))
9185
9186 (defun gnus-summary-find-matching (header regexp &optional backward unread
9187                                           not-case-fold not-matching)
9188   "Return a list of all articles that match REGEXP on HEADER.
9189 The search stars on the current article and goes forwards unless
9190 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9191 If UNREAD is non-nil, only unread articles will
9192 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9193 in the comparisons. If NOT-MATCHING, return a list of all articles that
9194 not match REGEXP on HEADER."
9195   (let ((case-fold-search (not not-case-fold))
9196         articles d func)
9197     (if (consp header)
9198         (if (eq (car header) 'extra)
9199             (setq func
9200                   `(lambda (h)
9201                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9202                          "")))
9203           (error "%s is an invalid header" header))
9204       (unless (fboundp (intern (concat "mail-header-" header)))
9205         (error "%s is not a valid header" header))
9206       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9207     (dolist (d (if (eq backward 'all)
9208                    gnus-newsgroup-data
9209                  (gnus-data-find-list
9210                   (gnus-summary-article-number)
9211                   (gnus-data-list backward))))
9212       (when (and (or (not unread)       ; We want all articles...
9213                      (gnus-data-unread-p d)) ; Or just unreads.
9214                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9215                  (if not-matching
9216                      (not (string-match
9217                            regexp
9218                            (funcall func (gnus-data-header d))))
9219                    (string-match regexp
9220                                  (funcall func (gnus-data-header d)))))
9221         (push (gnus-data-number d) articles))) ; Success!
9222     (nreverse articles)))
9223
9224 (defun gnus-summary-execute-command (header regexp command &optional backward)
9225   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9226 If HEADER is an empty string (or nil), the match is done on the entire
9227 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9228   (interactive
9229    (list (let ((completion-ignore-case t))
9230            (completing-read
9231             "Header name: "
9232             (mapcar (lambda (header) (list (format "%s" header)))
9233                     (append
9234                      '("Number" "Subject" "From" "Lines" "Date"
9235                        "Message-ID" "Xref" "References" "Body")
9236                      gnus-extra-headers))
9237             nil 'require-match))
9238          (read-string "Regexp: ")
9239          (read-key-sequence "Command: ")
9240          current-prefix-arg))
9241   (when (equal header "Body")
9242     (setq header ""))
9243   ;; Hidden thread subtrees must be searched as well.
9244   (gnus-summary-show-all-threads)
9245   ;; We don't want to change current point nor window configuration.
9246   (save-excursion
9247     (save-window-excursion
9248       (let (gnus-visual
9249             gnus-treat-strip-trailing-blank-lines
9250             gnus-treat-strip-leading-blank-lines
9251             gnus-treat-strip-multiple-blank-lines
9252             gnus-treat-hide-boring-headers
9253             gnus-treat-fold-newsgroups
9254             gnus-article-prepare-hook)
9255         (gnus-message 6 "Executing %s..." (key-description command))
9256         ;; We'd like to execute COMMAND interactively so as to give arguments.
9257         (gnus-execute header regexp
9258                       `(call-interactively ',(key-binding command))
9259                       backward)
9260         (gnus-message 6 "Executing %s...done" (key-description command))))))
9261
9262 (defun gnus-summary-beginning-of-article ()
9263   "Scroll the article back to the beginning."
9264   (interactive)
9265   (gnus-summary-select-article)
9266   (gnus-configure-windows 'article)
9267   (gnus-eval-in-buffer-window gnus-article-buffer
9268     (widen)
9269     (goto-char (point-min))
9270     (when gnus-break-pages
9271       (gnus-narrow-to-page))))
9272
9273 (defun gnus-summary-end-of-article ()
9274   "Scroll to the end of the article."
9275   (interactive)
9276   (gnus-summary-select-article)
9277   (gnus-configure-windows 'article)
9278   (gnus-eval-in-buffer-window gnus-article-buffer
9279     (widen)
9280     (goto-char (point-max))
9281     (recenter -3)
9282     (when gnus-break-pages
9283       (gnus-narrow-to-page))))
9284
9285 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9286   "Truncate to LEN and quote all \"(\"'s in STRING."
9287   (gnus-replace-in-string (if (and len (> (length string) len))
9288                               (substring string 0 len)
9289                             string)
9290                           "[()]" "\\\\\\&"))
9291
9292 (defun gnus-summary-print-article (&optional filename n)
9293   "Generate and print a PostScript image of the process-marked (mail) articles.
9294
9295 If used interactively, print the current article if none are
9296 process-marked.  With prefix arg, prompt the user for the name of the
9297 file to save in.
9298
9299 When used from Lisp, accept two optional args FILENAME and N.  N means
9300 to print the next N articles.  If N is negative, print the N previous
9301 articles.  If N is nil and articles have been marked with the process
9302 mark, print these instead.
9303
9304 If the optional first argument FILENAME is nil, send the image to the
9305 printer.  If FILENAME is a string, save the PostScript image in a file with
9306 that name.  If FILENAME is a number, prompt the user for the name of the file
9307 to save in."
9308   (interactive (list (ps-print-preprint current-prefix-arg)))
9309   (dolist (article (gnus-summary-work-articles n))
9310     (gnus-summary-select-article nil nil 'pseudo article)
9311     (gnus-eval-in-buffer-window gnus-article-buffer
9312       (gnus-print-buffer))
9313     (gnus-summary-remove-process-mark article))
9314   (ps-despool filename))
9315
9316 (defun gnus-print-buffer ()
9317   (let ((buffer (generate-new-buffer " *print*")))
9318     (unwind-protect
9319         (progn
9320           (copy-to-buffer buffer (point-min) (point-max))
9321           (set-buffer buffer)
9322           (gnus-remove-text-with-property 'gnus-decoration)
9323           (when (gnus-visual-p 'article-highlight 'highlight)
9324             ;; Copy-to-buffer doesn't copy overlay.  So redo
9325             ;; highlight.
9326             (let ((gnus-article-buffer buffer))
9327               (gnus-article-highlight-citation t)
9328               (gnus-article-highlight-signature)
9329               (gnus-article-emphasize)
9330               (gnus-article-delete-invisible-text)))
9331           (let ((ps-left-header
9332                  (list
9333                   (concat "("
9334                           (gnus-summary-print-truncate-and-quote
9335                            (mail-header-subject gnus-current-headers)
9336                            66) ")")
9337                   (concat "("
9338                           (gnus-summary-print-truncate-and-quote
9339                            (mail-header-from gnus-current-headers)
9340                            45) ")")))
9341                 (ps-right-header
9342                  (list
9343                   "/pagenumberstring load"
9344                   (concat "("
9345                           (mail-header-date gnus-current-headers) ")"))))
9346             (gnus-run-hooks 'gnus-ps-print-hook)
9347             (save-excursion
9348               (if ps-print-color-p
9349                   (ps-spool-buffer-with-faces)
9350                 (ps-spool-buffer)))))
9351       (kill-buffer buffer))))
9352
9353 (defun gnus-summary-show-complete-article ()
9354   "Show a complete version of the current article.
9355 This is only useful if you're looking at a partial version of the
9356 article currently."
9357   (interactive)
9358   (let ((gnus-keep-backlog nil)
9359         (gnus-use-cache nil)
9360         (gnus-agent nil)
9361         (gnus-fetch-partial-articles nil))
9362     (gnus-flush-original-article-buffer)
9363     (gnus-summary-show-article)))
9364
9365 (defun gnus-summary-show-article (&optional arg)
9366   "Force redisplaying of the current article.
9367 If ARG (the prefix) is a number, show the article with the charset
9368 defined in `gnus-summary-show-article-charset-alist', or the charset
9369 input.
9370 If ARG (the prefix) is non-nil and not a number, show the raw article
9371 without any article massaging functions being run.  Normally, the key
9372 strokes are `C-u g'."
9373   (interactive "P")
9374   (cond
9375    ((numberp arg)
9376     (gnus-summary-show-article t)
9377     (let ((gnus-newsgroup-charset
9378            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9379                (mm-read-coding-system
9380                 "View as charset: " ;; actually it is coding system.
9381                 (with-current-buffer gnus-article-buffer
9382                   (mm-detect-coding-region (point) (point-max))))))
9383           (gnus-newsgroup-ignored-charsets 'gnus-all))
9384       (gnus-summary-select-article nil 'force)
9385       (let ((deps gnus-newsgroup-dependencies)
9386             head header lines)
9387         (with-current-buffer gnus-original-article-buffer
9388           (save-restriction
9389             (message-narrow-to-head)
9390             (setq head (buffer-string))
9391             (goto-char (point-min))
9392             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9393               (goto-char (point-max))
9394               (widen)
9395               (setq lines (1- (count-lines (point) (point-max))))))
9396           (with-temp-buffer
9397             (insert (format "211 %d Article retrieved.\n"
9398                             (cdr gnus-article-current)))
9399             (insert head)
9400             (if lines (insert (format "Lines: %d\n" lines)))
9401             (insert ".\n")
9402             (let ((nntp-server-buffer (current-buffer)))
9403               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9404         (gnus-data-set-header
9405          (gnus-data-find (cdr gnus-article-current))
9406          header)
9407         (gnus-summary-update-article-line
9408          (cdr gnus-article-current) header)
9409         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9410           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9411    ((not arg)
9412     ;; Select the article the normal way.
9413     (gnus-summary-select-article nil 'force))
9414    (t
9415     ;; We have to require this here to make sure that the following
9416     ;; dynamic binding isn't shadowed by autoloading.
9417     (require 'gnus-async)
9418     (require 'gnus-art)
9419     ;; Bind the article treatment functions to nil.
9420     (let ((gnus-have-all-headers t)
9421           gnus-article-prepare-hook
9422           gnus-article-decode-hook
9423           gnus-display-mime-function
9424           gnus-break-pages)
9425       ;; Destroy any MIME parts.
9426       (when (gnus-buffer-live-p gnus-article-buffer)
9427         (with-current-buffer gnus-article-buffer
9428           (mm-destroy-parts gnus-article-mime-handles)
9429           ;; Set it to nil for safety reason.
9430           (setq gnus-article-mime-handle-alist nil)
9431           (setq gnus-article-mime-handles nil)))
9432       (gnus-summary-select-article nil 'force))))
9433   (gnus-summary-goto-subject gnus-current-article)
9434   (gnus-summary-position-point))
9435
9436 (defun gnus-summary-show-raw-article ()
9437   "Show the raw article without any article massaging functions being run."
9438   (interactive)
9439   (gnus-summary-show-article t))
9440
9441 (defun gnus-summary-verbose-headers (&optional arg)
9442   "Toggle permanent full header display.
9443 If ARG is a positive number, turn header display on.
9444 If ARG is a negative number, turn header display off."
9445   (interactive "P")
9446   (setq gnus-show-all-headers
9447         (cond ((or (not (numberp arg))
9448                    (zerop arg))
9449                (not gnus-show-all-headers))
9450               ((natnump arg)
9451                t)))
9452   (gnus-summary-show-article))
9453
9454 (defun gnus-summary-toggle-header (&optional arg)
9455   "Show the headers if they are hidden, or hide them if they are shown.
9456 If ARG is a positive number, show the entire header.
9457 If ARG is a negative number, hide the unwanted header lines."
9458   (interactive "P")
9459   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9460                      (get-buffer-window gnus-article-buffer t))))
9461     (with-current-buffer gnus-article-buffer
9462       (widen)
9463       (article-narrow-to-head)
9464       (let* ((inhibit-read-only t)
9465              (inhibit-point-motion-hooks t)
9466              (hidden (if (numberp arg)
9467                          (>= arg 0)
9468                        (or (not (looking-at "[^ \t\n]+:"))
9469                            (gnus-article-hidden-text-p 'headers))))
9470              s e)
9471         (delete-region (point-min) (point-max))
9472         (with-current-buffer gnus-original-article-buffer
9473           (goto-char (setq s (point-min)))
9474           (setq e (if (search-forward "\n\n" nil t)
9475                       (1- (point))
9476                     (point-max))))
9477         (insert-buffer-substring gnus-original-article-buffer s e)
9478         (run-hooks 'gnus-article-decode-hook)
9479         (if hidden
9480             (let ((gnus-treat-hide-headers nil)
9481                   (gnus-treat-hide-boring-headers nil))
9482               (gnus-delete-wash-type 'headers)
9483               (gnus-treat-article 'head))
9484           (gnus-treat-article 'head))
9485         (widen)
9486         (if window
9487             (set-window-start window (goto-char (point-min))))
9488         (if gnus-break-pages
9489             (gnus-narrow-to-page)
9490           (when (gnus-visual-p 'page-marker)
9491             (let ((inhibit-read-only t))
9492               (gnus-remove-text-with-property 'gnus-prev)
9493               (gnus-remove-text-with-property 'gnus-next))))
9494         (gnus-set-mode-line 'article)))))
9495
9496 (defun gnus-summary-show-all-headers ()
9497   "Make all header lines visible."
9498   (interactive)
9499   (gnus-summary-toggle-header 1))
9500
9501 (defun gnus-summary-caesar-message (&optional arg)
9502   "Caesar rotate the current article by 13.
9503 With a non-numerical prefix, also rotate headers.  A numerical
9504 prefix specifies how many places to rotate each letter forward."
9505   (interactive "P")
9506   (gnus-summary-select-article)
9507   (let ((mail-header-separator ""))
9508     (gnus-eval-in-buffer-window gnus-article-buffer
9509       (save-restriction
9510         (widen)
9511         (let ((start (window-start))
9512               (inhibit-read-only t))
9513           (if (equal arg '(4))
9514               (message-caesar-buffer-body nil t)
9515             (message-caesar-buffer-body arg))
9516           (set-window-start (get-buffer-window (current-buffer)) start)))))
9517   ;; Create buttons and stuff...
9518   (gnus-treat-article nil))
9519
9520 (declare-function idna-to-unicode "ext:idna" (str))
9521
9522 (defun gnus-summary-idna-message (&optional arg)
9523   "Decode IDNA encoded domain names in the current articles.
9524 IDNA encoded domain names looks like `xn--bar'.  If a string
9525 remain unencoded after running this function, it is likely an
9526 invalid IDNA string (`xn--bar' is invalid).
9527
9528 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9529 installed for this command to work."
9530   (interactive "P")
9531   (if (not (and (condition-case nil (require 'idna)
9532                   (file-error))
9533                 (mm-coding-system-p 'utf-8)
9534                 (executable-find (symbol-value 'idna-program))))
9535       (gnus-message
9536        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9537     (gnus-summary-select-article)
9538     (let ((mail-header-separator ""))
9539       (gnus-eval-in-buffer-window gnus-article-buffer
9540         (save-restriction
9541           (widen)
9542           (let ((start (window-start))
9543                 buffer-read-only)
9544             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9545               (replace-match (idna-to-unicode (match-string 1))))
9546             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9547
9548 (defun gnus-summary-morse-message (&optional arg)
9549   "Morse decode the current article."
9550   (interactive "P")
9551   (gnus-summary-select-article)
9552   (let ((mail-header-separator ""))
9553     (gnus-eval-in-buffer-window gnus-article-buffer
9554       (save-excursion
9555         (save-restriction
9556           (widen)
9557           (let ((pos (window-start))
9558                 (inhibit-read-only t))
9559             (goto-char (point-min))
9560             (when (message-goto-body)
9561               (gnus-narrow-to-body))
9562             (goto-char (point-min))
9563             (while (search-forward "·" (point-max) t)
9564               (replace-match "."))
9565             (unmorse-region (point-min) (point-max))
9566             (widen)
9567             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9568
9569 (defun gnus-summary-stop-page-breaking ()
9570   "Stop page breaking in the current article."
9571   (interactive)
9572   (gnus-summary-select-article)
9573   (gnus-eval-in-buffer-window gnus-article-buffer
9574     (widen)
9575     (when (gnus-visual-p 'page-marker)
9576       (let ((inhibit-read-only t))
9577         (gnus-remove-text-with-property 'gnus-prev)
9578         (gnus-remove-text-with-property 'gnus-next))
9579       (setq gnus-page-broken nil))))
9580
9581 (defun gnus-summary-move-article (&optional n to-newsgroup
9582                                             select-method action)
9583   "Move the current article to a different newsgroup.
9584 If N is a positive number, move the N next articles.
9585 If N is a negative number, move the N previous articles.
9586 If N is nil and any articles have been marked with the process mark,
9587 move those articles instead.
9588 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9589 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9590 re-spool using this method.
9591
9592 When called interactively with TO-NEWSGROUP being nil, the value of
9593 the variable `gnus-move-split-methods' is used for finding a default
9594 for the target newsgroup.
9595
9596 For this function to work, both the current newsgroup and the
9597 newsgroup that you want to move to have to support the `request-move'
9598 and `request-accept' functions.
9599
9600 ACTION can be either `move' (the default), `crosspost' or `copy'."
9601   (interactive "P")
9602   (unless action
9603     (setq action 'move))
9604   ;; Check whether the source group supports the required functions.
9605   (cond ((and (eq action 'move)
9606               (not (gnus-check-backend-function
9607                     'request-move-article gnus-newsgroup-name)))
9608          (error "The current group does not support article moving"))
9609         ((and (eq action 'crosspost)
9610               (not (gnus-check-backend-function
9611                     'request-replace-article gnus-newsgroup-name)))
9612          (error "The current group does not support article editing")))
9613   (let ((articles (gnus-summary-work-articles n))
9614         (prefix (if (gnus-check-backend-function
9615                      'request-move-article gnus-newsgroup-name)
9616                     (funcall gnus-move-group-prefix-function
9617                              gnus-newsgroup-name)
9618                   ""))
9619         (names '((move "Move" "Moving")
9620                  (copy "Copy" "Copying")
9621                  (crosspost "Crosspost" "Crossposting")))
9622         (copy-buf (save-excursion
9623                     (nnheader-set-temp-buffer " *copy article*")))
9624         art-group to-method new-xref article to-groups
9625         articles-to-update-marks encoded)
9626     (unless (assq action names)
9627       (error "Unknown action %s" action))
9628     ;; Read the newsgroup name.
9629     (when (and (not to-newsgroup)
9630                (not select-method))
9631       (if (and gnus-move-split-methods
9632                (not
9633                 (and (memq gnus-current-article articles)
9634                      (gnus-buffer-live-p gnus-original-article-buffer))))
9635           ;; When `gnus-move-split-methods' is non-nil, we have to
9636           ;; select an article to give `gnus-read-move-group-name' an
9637           ;; opportunity to suggest an appropriate default.  However,
9638           ;; we needn't render or mark the article.
9639           (let ((gnus-display-mime-function nil)
9640                 (gnus-article-prepare-hook nil)
9641                 (gnus-mark-article-hook nil))
9642             (gnus-summary-select-article nil nil nil (car articles))))
9643       (setq to-newsgroup (gnus-read-move-group-name
9644                           (cadr (assq action names))
9645                           (symbol-value
9646                            (intern (format "gnus-current-%s-group" action)))
9647                           articles prefix)
9648             encoded to-newsgroup
9649             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9650       (set (intern (format "gnus-current-%s-group" action))
9651            (mm-decode-coding-string
9652             to-newsgroup
9653             (gnus-group-name-charset to-method to-newsgroup))))
9654     (unless to-method
9655       (setq to-method (or select-method
9656                           (gnus-server-to-method
9657                            (gnus-group-method to-newsgroup)))))
9658     (setq to-newsgroup
9659           (or encoded
9660               (and to-newsgroup
9661                    (mm-encode-coding-string
9662                     to-newsgroup
9663                     (gnus-group-name-charset to-method to-newsgroup)))))
9664     ;; Check the method we are to move this article to...
9665     (unless (gnus-check-backend-function
9666              'request-accept-article (car to-method))
9667       (error "%s does not support article copying" (car to-method)))
9668     (unless (gnus-check-server to-method)
9669       (error "Can't open server %s" (car to-method)))
9670     (gnus-message 6 "%s to %s: %s..."
9671                   (caddr (assq action names))
9672                   (or (car select-method)
9673                       (gnus-group-decoded-name to-newsgroup))
9674                   articles)
9675     (while articles
9676       (setq article (pop articles))
9677       (setq
9678        art-group
9679        (cond
9680         ;; Move the article.
9681         ((eq action 'move)
9682          ;; Remove this article from future suppression.
9683          (gnus-dup-unsuppress-article article)
9684          (let* ((from-method (gnus-find-method-for-group
9685                               gnus-newsgroup-name))
9686                 (to-method (or select-method
9687                                (gnus-find-method-for-group to-newsgroup)))
9688                 (move-is-internal (gnus-server-equal from-method to-method)))
9689            (gnus-request-move-article
9690             article                     ; Article to move
9691             gnus-newsgroup-name         ; From newsgroup
9692             (nth 1 (gnus-find-method-for-group
9693                     gnus-newsgroup-name)) ; Server
9694             (list 'gnus-request-accept-article
9695                   to-newsgroup (list 'quote select-method)
9696                   (not articles) t)     ; Accept form
9697             (not articles)              ; Only save nov last time
9698             (and move-is-internal
9699                  to-newsgroup           ; Not respooling
9700                  (gnus-group-real-name to-newsgroup))))) ; Is this move internal?
9701         ;; Copy the article.
9702         ((eq action 'copy)
9703          (with-current-buffer copy-buf
9704            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9705              (save-restriction
9706                (nnheader-narrow-to-headers)
9707                (dolist (hdr gnus-copy-article-ignored-headers)
9708                  (message-remove-header hdr t)))
9709              (gnus-request-accept-article
9710               to-newsgroup select-method (not articles) t))))
9711         ;; Crosspost the article.
9712         ((eq action 'crosspost)
9713          (let ((xref (message-tokenize-header
9714                       (mail-header-xref (gnus-summary-article-header article))
9715                       " ")))
9716            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9717                                   ":" (number-to-string article)))
9718            (unless xref
9719              (setq xref (list (system-name))))
9720            (setq new-xref
9721                  (concat
9722                   (mapconcat 'identity
9723                              (delete "Xref:" (delete new-xref xref))
9724                              " ")
9725                   " " new-xref))
9726            (with-current-buffer copy-buf
9727              ;; First put the article in the destination group.
9728              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9729              (when (consp (setq art-group
9730                                 (gnus-request-accept-article
9731                                  to-newsgroup select-method (not articles) t)))
9732                (setq new-xref (concat new-xref " " (car art-group)
9733                                       ":"
9734                                       (number-to-string (cdr art-group))))
9735                ;; Now we have the new Xrefs header, so we insert
9736                ;; it and replace the new article.
9737                (nnheader-replace-header "Xref" new-xref)
9738                (gnus-request-replace-article
9739                 (cdr art-group) to-newsgroup (current-buffer) t)
9740                art-group))))))
9741       (cond
9742        ((not art-group)
9743         (gnus-message 1 "Couldn't %s article %s: %s"
9744                       (cadr (assq action names)) article
9745                       (nnheader-get-report (car to-method))))
9746        ((eq art-group 'junk)
9747         (when (eq action 'move)
9748           (gnus-summary-mark-article article gnus-canceled-mark)
9749           (gnus-message 4 "Deleted article %s" article)
9750           ;; run the delete hook
9751           (run-hook-with-args 'gnus-summary-article-delete-hook
9752                               action
9753                               (gnus-data-header
9754                                (assoc article (gnus-data-list nil)))
9755                               gnus-newsgroup-name nil
9756                               select-method)))
9757        (t
9758         (let* ((pto-group (gnus-group-prefixed-name
9759                            (car art-group) to-method))
9760                (info (gnus-get-info pto-group))
9761                (to-group (gnus-info-group info))
9762                to-marks)
9763           ;; Update the group that has been moved to.
9764           (when (and info
9765                      (memq action '(move copy)))
9766             (unless (member to-group to-groups)
9767               (push to-group to-groups))
9768
9769             (unless (memq article gnus-newsgroup-unreads)
9770               (push 'read to-marks)
9771               (gnus-info-set-read
9772                info (gnus-add-to-range (gnus-info-read info)
9773                                        (list (cdr art-group)))))
9774
9775             ;; See whether the article is to be put in the cache.
9776             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9777                    (marks (if expirable
9778                               gnus-article-mark-lists
9779                             (delete '(expirable . expire)
9780                                     (copy-sequence gnus-article-mark-lists))))
9781                    (to-article (cdr art-group)))
9782
9783               ;; Enter the article into the cache in the new group,
9784               ;; if that is required.
9785               (when gnus-use-cache
9786                 (gnus-cache-possibly-enter-article
9787                  to-group to-article
9788                  (memq article gnus-newsgroup-marked)
9789                  (memq article gnus-newsgroup-dormant)
9790                  (memq article gnus-newsgroup-unreads)))
9791
9792               (when gnus-preserve-marks
9793                 ;; Copy any marks over to the new group.
9794                 (when (and (equal to-group gnus-newsgroup-name)
9795                            (not (memq article gnus-newsgroup-unreads)))
9796                   ;; Mark this article as read in this group.
9797                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9798                   (setcdr (gnus-active to-group) to-article)
9799                   (setcdr gnus-newsgroup-active to-article))
9800
9801                 (while marks
9802                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9803                     (when (memq article (symbol-value
9804                                          (intern (format "gnus-newsgroup-%s"
9805                                                          (caar marks)))))
9806                       (push (cdar marks) to-marks)
9807                       ;; If the other group is the same as this group,
9808                       ;; then we have to add the mark to the list.
9809                       (when (equal to-group gnus-newsgroup-name)
9810                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9811                              (cons to-article
9812                                    (symbol-value
9813                                     (intern (format "gnus-newsgroup-%s"
9814                                                     (caar marks)))))))
9815                       ;; Copy the marks to other group.
9816                       (gnus-add-marked-articles
9817                        to-group (cdar marks) (list to-article) info)))
9818                   (setq marks (cdr marks)))
9819
9820                 (when (and expirable
9821                            gnus-mark-copied-or-moved-articles-as-expirable
9822                            (not (memq 'expire to-marks)))
9823                   ;; Mark this article as expirable.
9824                   (push 'expire to-marks)
9825                   (when (equal to-group gnus-newsgroup-name)
9826                     (push to-article gnus-newsgroup-expirable))
9827                   ;; Copy the expirable mark to other group.
9828                   (gnus-add-marked-articles
9829                    to-group 'expire (list to-article) info))
9830
9831                 (when to-marks
9832                   (gnus-request-set-mark
9833                    to-group (list (list (list to-article) 'add to-marks)))))
9834
9835               (gnus-dribble-enter
9836                (concat "(gnus-group-set-info '"
9837                        (gnus-prin1-to-string (gnus-get-info to-group))
9838                        ")"))))
9839
9840           ;; Update the Xref header in this article to point to
9841           ;; the new crossposted article we have just created.
9842           (when (eq action 'crosspost)
9843             (with-current-buffer copy-buf
9844               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9845               (nnheader-replace-header "Xref" new-xref)
9846               (gnus-request-replace-article
9847                article gnus-newsgroup-name (current-buffer) t)))
9848
9849           ;; run the move/copy/crosspost/respool hook
9850           (run-hook-with-args 'gnus-summary-article-move-hook
9851                               action
9852                               (gnus-data-header
9853                                (assoc article (gnus-data-list nil)))
9854                               gnus-newsgroup-name
9855                               to-newsgroup
9856                               select-method))
9857
9858         ;;;!!!Why is this necessary?
9859         (set-buffer gnus-summary-buffer)
9860
9861         (when (eq action 'move)
9862           (save-excursion
9863             (gnus-summary-goto-subject article)
9864             (gnus-summary-mark-article article gnus-canceled-mark)))))
9865       (push article articles-to-update-marks))
9866
9867     (save-excursion
9868       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9869     ;; Re-activate all groups that have been moved to.
9870     (with-current-buffer gnus-group-buffer
9871       (let ((gnus-group-marked to-groups))
9872         (gnus-group-get-new-news-this-group nil t)))
9873
9874     (gnus-kill-buffer copy-buf)
9875     (gnus-summary-position-point)
9876     (gnus-set-mode-line 'summary)))
9877
9878 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9879   "Copy the current article to some other group.
9880 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9881 When called interactively, if TO-NEWSGROUP is nil, use the value of
9882 the variable `gnus-move-split-methods' for finding a default target
9883 newsgroup.
9884 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9885 re-spool using this method."
9886   (interactive "P")
9887   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9888
9889 (defun gnus-summary-crosspost-article (&optional n)
9890   "Crosspost the current article to some other group."
9891   (interactive "P")
9892   (gnus-summary-move-article n nil nil 'crosspost))
9893
9894 (defcustom gnus-summary-respool-default-method nil
9895   "Default method type for respooling an article.
9896 If nil, use to the current newsgroup method."
9897   :type 'symbol
9898   :group 'gnus-summary-mail)
9899
9900 (defun gnus-summary-respool-article (&optional n method)
9901   "Respool the current article.
9902 The article will be squeezed through the mail spooling process again,
9903 which means that it will be put in some mail newsgroup or other
9904 depending on `nnmail-split-methods'.
9905 If N is a positive number, respool the N next articles.
9906 If N is a negative number, respool the N previous articles.
9907 If N is nil and any articles have been marked with the process mark,
9908 respool those articles instead.
9909
9910 Respooling can be done both from mail groups and \"real\" newsgroups.
9911 In the former case, the articles in question will be moved from the
9912 current group into whatever groups they are destined to.  In the
9913 latter case, they will be copied into the relevant groups."
9914   (interactive
9915    (list current-prefix-arg
9916          (let* ((methods (gnus-methods-using 'respool))
9917                 (methname
9918                  (symbol-name (or gnus-summary-respool-default-method
9919                                   (car (gnus-find-method-for-group
9920                                         gnus-newsgroup-name)))))
9921                 (method
9922                  (gnus-completing-read-with-default
9923                   methname "Backend to use when respooling"
9924                   methods nil t nil 'gnus-mail-method-history))
9925                 ms)
9926            (cond
9927             ((zerop (length (setq ms (gnus-servers-using-backend
9928                                       (intern method)))))
9929              (list (intern method) ""))
9930             ((= 1 (length ms))
9931              (car ms))
9932             (t
9933              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9934                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9935                            ms-alist))))))))
9936   (unless method
9937     (error "No method given for respooling"))
9938   (if (assoc (symbol-name
9939               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9940              (gnus-methods-using 'respool))
9941       (gnus-summary-move-article n nil method)
9942     (gnus-summary-copy-article n nil method)))
9943
9944 (defun gnus-summary-import-article (file &optional edit)
9945   "Import an arbitrary file into a mail newsgroup."
9946   (interactive "fImport file: \nP")
9947   (let ((group gnus-newsgroup-name)
9948         (now (current-time))
9949         atts lines group-art)
9950     (unless (gnus-check-backend-function 'request-accept-article group)
9951       (error "%s does not support article importing" group))
9952     (or (file-readable-p file)
9953         (not (file-regular-p file))
9954         (error "Can't read %s" file))
9955     (with-current-buffer (gnus-get-buffer-create " *import file*")
9956       (erase-buffer)
9957       (nnheader-insert-file-contents file)
9958       (goto-char (point-min))
9959       (if (nnheader-article-p)
9960           (save-restriction
9961             (goto-char (point-min))
9962             (search-forward "\n\n" nil t)
9963             (narrow-to-region (point-min) (1- (point)))
9964             (goto-char (point-min))
9965             (unless (re-search-forward "^date:" nil t)
9966               (goto-char (point-max))
9967               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9968        ;; This doesn't look like an article, so we fudge some headers.
9969         (setq atts (file-attributes file)
9970               lines (count-lines (point-min) (point-max)))
9971         (insert "From: " (read-string "From: ") "\n"
9972                 "Subject: " (read-string "Subject: ") "\n"
9973                 "Date: " (message-make-date (nth 5 atts)) "\n"
9974                 "Message-ID: " (message-make-message-id) "\n"
9975                 "Lines: " (int-to-string lines) "\n"
9976                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9977       (setq group-art (gnus-request-accept-article group nil t))
9978       (kill-buffer (current-buffer)))
9979     (setq gnus-newsgroup-active (gnus-activate-group group))
9980     (forward-line 1)
9981     (gnus-summary-goto-article (cdr group-art) nil t)
9982     (when edit
9983       (gnus-summary-edit-article))))
9984
9985 (defun gnus-summary-create-article ()
9986   "Create an article in a mail newsgroup."
9987   (interactive)
9988   (let ((group gnus-newsgroup-name)
9989         (now (current-time))
9990         group-art)
9991     (unless (gnus-check-backend-function 'request-accept-article group)
9992       (error "%s does not support article importing" group))
9993     (with-current-buffer (gnus-get-buffer-create " *import file*")
9994       (erase-buffer)
9995       (goto-char (point-min))
9996       ;; This doesn't look like an article, so we fudge some headers.
9997       (insert "From: " (read-string "From: ") "\n"
9998               "Subject: " (read-string "Subject: ") "\n"
9999               "Date: " (message-make-date now) "\n"
10000               "Message-ID: " (message-make-message-id) "\n")
10001       (setq group-art (gnus-request-accept-article group nil t))
10002       (kill-buffer (current-buffer)))
10003     (setq gnus-newsgroup-active (gnus-activate-group group))
10004     (forward-line 1)
10005     (gnus-summary-goto-article (cdr group-art) nil t)
10006     (gnus-summary-edit-article)))
10007
10008 (defun gnus-summary-article-posted-p ()
10009   "Say whether the current (mail) article is available from news as well.
10010 This will be the case if the article has both been mailed and posted."
10011   (interactive)
10012   (let ((id (mail-header-references (gnus-summary-article-header)))
10013         (gnus-override-method (car (gnus-refer-article-methods))))
10014     (if (gnus-request-head id "")
10015         (gnus-message 2 "The current message was found on %s"
10016                       gnus-override-method)
10017       (gnus-message 2 "The current message couldn't be found on %s"
10018                     gnus-override-method)
10019       nil)))
10020
10021 (defun gnus-summary-expire-articles (&optional now)
10022   "Expire all articles that are marked as expirable in the current group."
10023   (interactive)
10024   (when (and (not gnus-group-is-exiting-without-update-p)
10025              (gnus-check-backend-function
10026               'request-expire-articles gnus-newsgroup-name))
10027     ;; This backend supports expiry.
10028     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10029            (expirable (if total
10030                           (progn
10031                             ;; We need to update the info for
10032                             ;; this group for `gnus-list-of-read-articles'
10033                             ;; to give us the right answer.
10034                             (gnus-run-hooks 'gnus-exit-group-hook)
10035                             (gnus-summary-update-info)
10036                             (gnus-list-of-read-articles gnus-newsgroup-name))
10037                         (setq gnus-newsgroup-expirable
10038                               (sort gnus-newsgroup-expirable '<))))
10039            (expiry-wait (if now 'immediate
10040                           (gnus-group-find-parameter
10041                            gnus-newsgroup-name 'expiry-wait)))
10042            (nnmail-expiry-target
10043             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10044                 nnmail-expiry-target))
10045            es)
10046       (when expirable
10047         ;; There are expirable articles in this group, so we run them
10048         ;; through the expiry process.
10049         (gnus-message 6 "Expiring articles...")
10050         (unless (gnus-check-group gnus-newsgroup-name)
10051           (error "Can't open server for %s" gnus-newsgroup-name))
10052         ;; The list of articles that weren't expired is returned.
10053         (save-excursion
10054           (if expiry-wait
10055               (let ((nnmail-expiry-wait-function nil)
10056                     (nnmail-expiry-wait expiry-wait))
10057                 (setq es (gnus-request-expire-articles
10058                           expirable gnus-newsgroup-name)))
10059             (setq es (gnus-request-expire-articles
10060                       expirable gnus-newsgroup-name)))
10061           (unless total
10062             (setq gnus-newsgroup-expirable es))
10063           ;; We go through the old list of expirable, and mark all
10064           ;; really expired articles as nonexistent.
10065           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10066             (let ((gnus-use-cache nil))
10067               (dolist (article expirable)
10068                 (when (and (not (memq article es))
10069                            (gnus-data-find article))
10070                   (gnus-summary-mark-article article gnus-canceled-mark)
10071                   (run-hook-with-args 'gnus-summary-article-expire-hook
10072                                       'delete
10073                                       (gnus-data-header
10074                                        (assoc article (gnus-data-list nil)))
10075                                       gnus-newsgroup-name
10076                                       nil
10077                                       nil))))))
10078         (gnus-message 6 "Expiring articles...done")))))
10079
10080 (defun gnus-summary-expire-articles-now ()
10081   "Expunge all expirable articles in the current group.
10082 This means that *all* articles that are marked as expirable will be
10083 deleted forever, right now."
10084   (interactive)
10085   (or gnus-expert-user
10086       (gnus-yes-or-no-p
10087        "Are you really, really sure you want to delete all expirable messages? ")
10088       (error "Phew!"))
10089   (gnus-summary-expire-articles t))
10090
10091 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10092 (defun gnus-summary-delete-article (&optional n)
10093   "Delete the N next (mail) articles.
10094 This command actually deletes articles.  This is not a marking
10095 command.  The article will disappear forever from your life, never to
10096 return.
10097
10098 If N is negative, delete backwards.
10099 If N is nil and articles have been marked with the process mark,
10100 delete these instead.
10101
10102 If `gnus-novice-user' is non-nil you will be asked for
10103 confirmation before the articles are deleted."
10104   (interactive "P")
10105   (unless (gnus-check-backend-function 'request-expire-articles
10106                                        gnus-newsgroup-name)
10107     (error "The current newsgroup does not support article deletion"))
10108   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10109     (error "Couldn't open server"))
10110   ;; Compute the list of articles to delete.
10111   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10112         (nnmail-expiry-target 'delete)
10113         not-deleted)
10114     (if (and gnus-novice-user
10115              (not (gnus-yes-or-no-p
10116                    (format "Do you really want to delete %s forever? "
10117                            (if (> (length articles) 1)
10118                                (format "these %s articles" (length articles))
10119                              "this article")))))
10120         ()
10121       ;; Delete the articles.
10122       (setq not-deleted (gnus-request-expire-articles
10123                          articles gnus-newsgroup-name 'force))
10124       (save-excursion
10125         (while articles
10126           (gnus-summary-remove-process-mark (car articles))
10127           ;; The backend might not have been able to delete the article
10128           ;; after all.
10129           (unless (memq (car articles) not-deleted)
10130             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10131           (let* ((article (car articles))
10132                  (ghead  (gnus-data-header
10133                           (assoc article (gnus-data-list nil)))))
10134             (run-hook-with-args 'gnus-summary-article-delete-hook
10135                                 'delete ghead gnus-newsgroup-name nil
10136                                 nil))
10137           (setq articles (cdr articles))))
10138       (when not-deleted
10139         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10140     (gnus-summary-position-point)
10141     (gnus-set-mode-line 'summary)
10142     not-deleted))
10143
10144 (defun gnus-summary-edit-article (&optional arg)
10145   "Edit the current article.
10146 This will have permanent effect only in mail groups.
10147 If ARG is nil, edit the decoded articles.
10148 If ARG is 1, edit the raw articles.
10149 If ARG is 2, edit the raw articles even in read-only groups.
10150 If ARG is 3, edit the articles with the current handles.
10151 Otherwise, allow editing of articles even in read-only
10152 groups."
10153   (interactive "P")
10154   (let (force raw current-handles)
10155     (cond
10156      ((null arg))
10157      ((eq arg 1)
10158       (setq raw t))
10159      ((eq arg 2)
10160       (setq raw t
10161             force t))
10162      ((eq arg 3)
10163       (setq current-handles
10164             (and (gnus-buffer-live-p gnus-article-buffer)
10165                  (with-current-buffer gnus-article-buffer
10166                    (prog1
10167                        gnus-article-mime-handles
10168                      (setq gnus-article-mime-handles nil))))))
10169      (t
10170       (setq force t)))
10171     (when (and raw (not force)
10172                (member gnus-newsgroup-name '("nndraft:delayed"
10173                                              "nndraft:drafts"
10174                                              "nndraft:queue")))
10175       (error "Can't edit the raw article in group %s"
10176              gnus-newsgroup-name))
10177     (with-current-buffer gnus-summary-buffer
10178       (let ((mail-parse-charset gnus-newsgroup-charset)
10179             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10180         (gnus-set-global-variables)
10181         (when (and (not force)
10182                    (gnus-group-read-only-p))
10183           (error "The current newsgroup does not support article editing"))
10184         (gnus-summary-show-article t)
10185         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10186           (with-current-buffer gnus-article-buffer
10187             (mm-enable-multibyte)))
10188         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10189             (setq raw t))
10190         (gnus-article-edit-article
10191          (if raw 'ignore
10192            `(lambda ()
10193               (let ((mbl mml-buffer-list))
10194                 (setq mml-buffer-list nil)
10195                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10196                   (mime-to-mml ,'current-handles))
10197                 (let ((mbl1 mml-buffer-list))
10198                   (setq mml-buffer-list mbl)
10199                   (set (make-local-variable 'mml-buffer-list) mbl1))
10200                 (gnus-make-local-hook 'kill-buffer-hook)
10201                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10202          `(lambda (no-highlight)
10203             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10204                   (message-options message-options)
10205                   (message-options-set-recipient)
10206                   (mail-parse-ignored-charsets
10207                    ',gnus-newsgroup-ignored-charsets)
10208                   (rfc2047-header-encoding-alist
10209                    ',(let ((charset (gnus-group-name-charset
10210                                      (gnus-find-method-for-group
10211                                       gnus-newsgroup-name)
10212                                      gnus-newsgroup-name)))
10213                        (append (list (cons "Newsgroups" charset)
10214                                      (cons "Followup-To" charset)
10215                                      (cons "Xref" charset))
10216                                rfc2047-header-encoding-alist))))
10217               ,(if (not raw) '(progn
10218                                 (mml-to-mime)
10219                                 (mml-destroy-buffers)
10220                                 (remove-hook 'kill-buffer-hook
10221                                              'mml-destroy-buffers t)
10222                                 (kill-local-variable 'mml-buffer-list)))
10223               (gnus-summary-edit-article-done
10224                ,(or (mail-header-references gnus-current-headers) "")
10225                ,(gnus-group-read-only-p)
10226                ,gnus-summary-buffer no-highlight))))))))
10227
10228 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10229
10230 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10231                                                  no-highlight)
10232   "Make edits to the current article permanent."
10233   (interactive)
10234   (save-excursion
10235    ;; The buffer restriction contains the entire article if it exists.
10236     (when (article-goto-body)
10237       (let ((lines (count-lines (point) (point-max)))
10238             (length (- (point-max) (point)))
10239             (case-fold-search t)
10240             (body (copy-marker (point))))
10241         (goto-char (point-min))
10242         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10243           (delete-region (match-beginning 1) (match-end 1))
10244           (insert (number-to-string length)))
10245         (goto-char (point-min))
10246         (when (re-search-forward
10247                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10248           (delete-region (match-beginning 1) (match-end 1))
10249           (insert (number-to-string length)))
10250         (goto-char (point-min))
10251         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10252           (delete-region (match-beginning 1) (match-end 1))
10253           (insert (number-to-string lines))))))
10254   ;; Replace the article.
10255   (let ((buf (current-buffer)))
10256     (with-temp-buffer
10257       (insert-buffer-substring buf)
10258
10259       (if (and (not read-only)
10260                (not (gnus-request-replace-article
10261                      (cdr gnus-article-current) (car gnus-article-current)
10262                      (current-buffer) t)))
10263           (error "Couldn't replace article")
10264         ;; Update the summary buffer.
10265         (if (and references
10266                  (equal (message-tokenize-header references " ")
10267                         (message-tokenize-header
10268                          (or (message-fetch-field "references") "") " ")))
10269             ;; We only have to update this line.
10270             (save-excursion
10271               (save-restriction
10272                 (message-narrow-to-head)
10273                 (let ((head (buffer-substring-no-properties
10274                              (point-min) (point-max)))
10275                       header)
10276                   (with-temp-buffer
10277                     (insert (format "211 %d Article retrieved.\n"
10278                                     (cdr gnus-article-current)))
10279                     (insert head)
10280                     (insert ".\n")
10281                     (let ((nntp-server-buffer (current-buffer)))
10282                       (setq header (car (gnus-get-newsgroup-headers
10283                                          nil t))))
10284                     (with-current-buffer gnus-summary-buffer
10285                       (gnus-data-set-header
10286                        (gnus-data-find (cdr gnus-article-current))
10287                        header)
10288                       (gnus-summary-update-article-line
10289                        (cdr gnus-article-current) header)
10290                       (if (gnus-summary-goto-subject
10291                            (cdr gnus-article-current) nil t)
10292                           (gnus-summary-update-secondary-mark
10293                            (cdr gnus-article-current))))))))
10294           ;; Update threads.
10295           (set-buffer (or buffer gnus-summary-buffer))
10296           (gnus-summary-update-article (cdr gnus-article-current))
10297           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10298               (gnus-summary-update-secondary-mark
10299                (cdr gnus-article-current))))
10300         ;; Prettify the article buffer again.
10301         (unless no-highlight
10302           (with-current-buffer gnus-article-buffer
10303             ;;;!!! Fix this -- article should be rehighlighted.
10304             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10305             (set-buffer gnus-original-article-buffer)
10306             (gnus-request-article
10307              (cdr gnus-article-current)
10308              (car gnus-article-current) (current-buffer))))
10309         ;; Prettify the summary buffer line.
10310         (when (gnus-visual-p 'summary-highlight 'highlight)
10311           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10312
10313 (defun gnus-summary-edit-wash (key)
10314   "Perform editing command KEY in the article buffer."
10315   (interactive
10316    (list
10317     (progn
10318       (message "%s" (concat (this-command-keys) "- "))
10319       (read-char))))
10320   (message "")
10321   (gnus-summary-edit-article)
10322   (execute-kbd-macro (concat (this-command-keys) key))
10323   (gnus-article-edit-done))
10324
10325 ;;; Respooling
10326
10327 (defun gnus-summary-respool-query (&optional silent trace)
10328   "Query where the respool algorithm would put this article."
10329   (interactive)
10330   (let (gnus-mark-article-hook)
10331     (gnus-summary-select-article)
10332     (with-current-buffer gnus-original-article-buffer
10333       (let ((groups (nnmail-article-group 'identity trace)))
10334         (unless silent
10335           (if groups
10336               (message "This message would go to %s"
10337                        (mapconcat 'car groups ", "))
10338             (message "This message would go to no groups"))
10339           groups)))))
10340
10341 (defun gnus-summary-respool-trace ()
10342   "Trace where the respool algorithm would put this article.
10343 Display a buffer showing all fancy splitting patterns which matched."
10344   (interactive)
10345   (gnus-summary-respool-query nil t))
10346
10347 ;; Summary marking commands.
10348
10349 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10350   "Mark articles which has the same subject as read, and then select the next.
10351 If UNMARK is positive, remove any kind of mark.
10352 If UNMARK is negative, tick articles."
10353   (interactive "P")
10354   (when unmark
10355     (setq unmark (prefix-numeric-value unmark)))
10356   (let ((count
10357          (gnus-summary-mark-same-subject
10358           (gnus-summary-article-subject) unmark)))
10359     ;; Select next unread article.  If auto-select-same mode, should
10360     ;; select the first unread article.
10361     (gnus-summary-next-article t (and gnus-auto-select-same
10362                                       (gnus-summary-article-subject)))
10363     (gnus-message 7 "%d article%s marked as %s"
10364                   count (if (= count 1) " is" "s are")
10365                   (if unmark "unread" "read"))))
10366
10367 (defun gnus-summary-kill-same-subject (&optional unmark)
10368   "Mark articles which has the same subject as read.
10369 If UNMARK is positive, remove any kind of mark.
10370 If UNMARK is negative, tick articles."
10371   (interactive "P")
10372   (when unmark
10373     (setq unmark (prefix-numeric-value unmark)))
10374   (let ((count
10375          (gnus-summary-mark-same-subject
10376           (gnus-summary-article-subject) unmark)))
10377     ;; If marked as read, go to next unread subject.
10378     (when (null unmark)
10379       ;; Go to next unread subject.
10380       (gnus-summary-next-subject 1 t))
10381     (gnus-message 7 "%d articles are marked as %s"
10382                   count (if unmark "unread" "read"))))
10383
10384 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10385   "Mark articles with same SUBJECT as read, and return marked number.
10386 If optional argument UNMARK is positive, remove any kinds of marks.
10387 If optional argument UNMARK is negative, mark articles as unread instead."
10388   (let ((count 1))
10389     (save-excursion
10390       (cond
10391        ((null unmark)                   ; Mark as read.
10392         (while (and
10393                 (progn
10394                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10395                   (gnus-summary-show-thread) t)
10396                 (gnus-summary-find-subject subject))
10397           (setq count (1+ count))))
10398        ((> unmark 0)                    ; Tick.
10399         (while (and
10400                 (progn
10401                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10402                   (gnus-summary-show-thread) t)
10403                 (gnus-summary-find-subject subject))
10404           (setq count (1+ count))))
10405        (t                               ; Mark as unread.
10406         (while (and
10407                 (progn
10408                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10409                   (gnus-summary-show-thread) t)
10410                 (gnus-summary-find-subject subject))
10411           (setq count (1+ count)))))
10412       (gnus-set-mode-line 'summary)
10413       ;; Return the number of marked articles.
10414       count)))
10415
10416 (defun gnus-summary-mark-as-processable (n &optional unmark)
10417   "Set the process mark on the next N articles.
10418 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10419 the process mark instead.  The difference between N and the actual
10420 number of articles marked is returned."
10421   (interactive "P")
10422   (if (and (null n) (gnus-region-active-p))
10423       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10424     (setq n (prefix-numeric-value n))
10425     (let ((backward (< n 0))
10426           (n (abs n)))
10427       (while (and
10428               (> n 0)
10429               (if unmark
10430                   (gnus-summary-remove-process-mark
10431                    (gnus-summary-article-number))
10432                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10433               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10434         (setq n (1- n)))
10435       (when (/= 0 n)
10436         (gnus-message 7 "No more articles"))
10437       (gnus-summary-recenter)
10438       (gnus-summary-position-point)
10439       n)))
10440
10441 (defun gnus-summary-unmark-as-processable (n)
10442   "Remove the process mark from the next N articles.
10443 If N is negative, unmark backward instead.  The difference between N and
10444 the actual number of articles unmarked is returned."
10445   (interactive "P")
10446   (gnus-summary-mark-as-processable n t))
10447
10448 (defun gnus-summary-unmark-all-processable ()
10449   "Remove the process mark from all articles."
10450   (interactive)
10451   (save-excursion
10452     (while gnus-newsgroup-processable
10453       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10454   (gnus-summary-position-point))
10455
10456 (defun gnus-summary-add-mark (article type)
10457   "Mark ARTICLE with a mark of TYPE."
10458   (let ((vtype (car (assq type gnus-article-mark-lists)))
10459         var)
10460     (if (not vtype)
10461         (error "No such mark type: %s" type)
10462       (setq var (intern (format "gnus-newsgroup-%s" type)))
10463       (set var (cons article (symbol-value var)))
10464       (if (memq type '(processable cached replied forwarded recent saved))
10465           (gnus-summary-update-secondary-mark article)
10466         ;;; !!! This is bogus.  We should find out what primary
10467         ;;; !!! mark we want to set.
10468         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10469
10470 (defun gnus-summary-mark-as-expirable (n)
10471   "Mark N articles forward as expirable.
10472 If N is negative, mark backward instead.  The difference between N and
10473 the actual number of articles marked is returned."
10474   (interactive "p")
10475   (gnus-summary-mark-forward n gnus-expirable-mark))
10476
10477 (defun gnus-summary-mark-as-spam (n)
10478   "Mark N articles forward as spam.
10479 If N is negative, mark backward instead.  The difference between N and
10480 the actual number of articles marked is returned."
10481   (interactive "p")
10482   (gnus-summary-mark-forward n gnus-spam-mark))
10483
10484 (defun gnus-summary-mark-article-as-replied (article)
10485   "Mark ARTICLE as replied to and update the summary line.
10486 ARTICLE can also be a list of articles."
10487   (interactive (list (gnus-summary-article-number)))
10488   (let ((articles (if (listp article) article (list article))))
10489     (dolist (article articles)
10490       (unless (numberp article)
10491         (error "%s is not a number" article))
10492       (push article gnus-newsgroup-replied)
10493       (let ((inhibit-read-only t))
10494         (when (gnus-summary-goto-subject article nil t)
10495           (gnus-summary-update-secondary-mark article))))))
10496
10497 (defun gnus-summary-mark-article-as-forwarded (article)
10498   "Mark ARTICLE as forwarded and update the summary line.
10499 ARTICLE can also be a list of articles."
10500   (let ((articles (if (listp article) article (list article))))
10501     (dolist (article articles)
10502       (push article gnus-newsgroup-forwarded)
10503       (let ((inhibit-read-only t))
10504         (when (gnus-summary-goto-subject article nil t)
10505           (gnus-summary-update-secondary-mark article))))))
10506
10507 (defun gnus-summary-set-bookmark (article)
10508   "Set a bookmark in current article."
10509   (interactive (list (gnus-summary-article-number)))
10510   (when (or (not (get-buffer gnus-article-buffer))
10511             (not gnus-current-article)
10512             (not gnus-article-current)
10513             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10514     (error "No current article selected"))
10515   ;; Remove old bookmark, if one exists.
10516   (gnus-pull article gnus-newsgroup-bookmarks)
10517   ;; Set the new bookmark, which is on the form
10518   ;; (article-number . line-number-in-body).
10519   (push
10520    (cons article
10521          (with-current-buffer gnus-article-buffer
10522            (count-lines
10523             (min (point)
10524                  (save-excursion
10525                    (article-goto-body)
10526                    (point)))
10527             (point))))
10528    gnus-newsgroup-bookmarks)
10529   (gnus-message 6 "A bookmark has been added to the current article."))
10530
10531 (defun gnus-summary-remove-bookmark (article)
10532   "Remove the bookmark from the current article."
10533   (interactive (list (gnus-summary-article-number)))
10534   ;; Remove old bookmark, if one exists.
10535   (if (not (assq article gnus-newsgroup-bookmarks))
10536       (gnus-message 6 "No bookmark in current article.")
10537     (gnus-pull article gnus-newsgroup-bookmarks)
10538     (gnus-message 6 "Removed bookmark.")))
10539
10540 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10541 (defun gnus-summary-mark-as-dormant (n)
10542   "Mark N articles forward as dormant.
10543 If N is negative, mark backward instead.  The difference between N and
10544 the actual number of articles marked is returned."
10545   (interactive "p")
10546   (gnus-summary-mark-forward n gnus-dormant-mark))
10547
10548 (defun gnus-summary-set-process-mark (article)
10549   "Set the process mark on ARTICLE and update the summary line."
10550   (setq gnus-newsgroup-processable
10551         (cons article
10552               (delq article gnus-newsgroup-processable)))
10553   (when (gnus-summary-goto-subject article)
10554     (gnus-summary-show-thread)
10555     (gnus-summary-goto-subject article)
10556     (gnus-summary-update-secondary-mark article)))
10557
10558 (defun gnus-summary-remove-process-mark (&rest articles)
10559   "Remove the process mark from ARTICLES and update the summary line."
10560   (dolist (article articles)
10561     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10562     (when (gnus-summary-goto-subject article)
10563       (gnus-summary-show-thread)
10564       (gnus-summary-goto-subject article)
10565       (gnus-summary-update-secondary-mark article)))
10566   t)
10567
10568 (defun gnus-summary-set-saved-mark (article)
10569   "Set the process mark on ARTICLE and update the summary line."
10570   (push article gnus-newsgroup-saved)
10571   (when (gnus-summary-goto-subject article)
10572     (gnus-summary-update-secondary-mark article)))
10573
10574 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10575   "Mark N articles as read forwards.
10576 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10577 The difference between N and the actual number of articles marked is
10578 returned.
10579 If NO-EXPIRE, auto-expiry will be inhibited."
10580   (interactive "p")
10581   (gnus-summary-show-thread)
10582   (let ((backward (< n 0))
10583         (gnus-summary-goto-unread
10584          (and gnus-summary-goto-unread
10585               (not (eq gnus-summary-goto-unread 'never))
10586               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10587                                     gnus-ticked-mark gnus-dormant-mark)))))
10588         (n (abs n))
10589         (mark (or mark gnus-del-mark)))
10590     (while (and (> n 0)
10591                 (gnus-summary-mark-article nil mark no-expire)
10592                 (zerop (gnus-summary-next-subject
10593                         (if backward -1 1)
10594                         (and gnus-summary-goto-unread
10595                              (not (eq gnus-summary-goto-unread 'never)))
10596                         t)))
10597       (setq n (1- n)))
10598     (when (/= 0 n)
10599       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10600     (gnus-summary-recenter)
10601     (gnus-summary-position-point)
10602     (gnus-set-mode-line 'summary)
10603     n))
10604
10605 (defun gnus-summary-mark-article-as-read (mark)
10606   "Mark the current article quickly as read with MARK."
10607   (let ((article (gnus-summary-article-number)))
10608     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10609     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10610     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10611     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10612     (push (cons article mark) gnus-newsgroup-reads)
10613     ;; Possibly remove from cache, if that is used.
10614     (when gnus-use-cache
10615       (gnus-cache-enter-remove-article article))
10616     ;; Allow the backend to change the mark.
10617     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10618     ;; Check for auto-expiry.
10619     (when (and gnus-newsgroup-auto-expire
10620                (memq mark gnus-auto-expirable-marks))
10621       (setq mark gnus-expirable-mark)
10622       ;; Let the backend know about the mark change.
10623       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10624       (push article gnus-newsgroup-expirable))
10625     ;; Set the mark in the buffer.
10626     (gnus-summary-update-mark mark 'unread)
10627     t))
10628
10629 (defun gnus-summary-mark-article-as-unread (mark)
10630   "Mark the current article quickly as unread with MARK."
10631   (let* ((article (gnus-summary-article-number))
10632          (old-mark (gnus-summary-article-mark article)))
10633     ;; Allow the backend to change the mark.
10634     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10635     (if (eq mark old-mark)
10636         t
10637       (if (<= article 0)
10638           (progn
10639             (gnus-error 1 "Can't mark negative article numbers")
10640             nil)
10641         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10642         (setq gnus-newsgroup-spam-marked
10643               (delq article gnus-newsgroup-spam-marked))
10644         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10645         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10646         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10647         (cond ((= mark gnus-ticked-mark)
10648                (setq gnus-newsgroup-marked
10649                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10650                                               article)))
10651               ((= mark gnus-spam-mark)
10652                (setq gnus-newsgroup-spam-marked
10653                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10654                                               article)))
10655               ((= mark gnus-dormant-mark)
10656                (setq gnus-newsgroup-dormant
10657                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10658                                               article)))
10659               (t
10660                (setq gnus-newsgroup-unreads
10661                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10662                                               article))))
10663         (gnus-pull article gnus-newsgroup-reads)
10664
10665         ;; See whether the article is to be put in the cache.
10666         (and gnus-use-cache
10667              (vectorp (gnus-summary-article-header article))
10668              (save-excursion
10669                (gnus-cache-possibly-enter-article
10670                 gnus-newsgroup-name article
10671                 (= mark gnus-ticked-mark)
10672                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10673
10674         ;; Fix the mark.
10675         (gnus-summary-update-mark mark 'unread)
10676         t))))
10677
10678 (defun gnus-summary-mark-article (&optional article mark no-expire)
10679   "Mark ARTICLE with MARK.  MARK can be any character.
10680 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10681 `??' (dormant) and `?E' (expirable).
10682 If MARK is nil, then the default character `?r' is used.
10683 If ARTICLE is nil, then the article on the current line will be
10684 marked.
10685 If NO-EXPIRE, auto-expiry will be inhibited."
10686   ;; The mark might be a string.
10687   (when (stringp mark)
10688     (setq mark (aref mark 0)))
10689   ;; If no mark is given, then we check auto-expiring.
10690   (when (null mark)
10691     (setq mark gnus-del-mark))
10692   (when (and (not no-expire)
10693              gnus-newsgroup-auto-expire
10694              (memq mark gnus-auto-expirable-marks))
10695     (setq mark gnus-expirable-mark))
10696   (let ((article (or article (gnus-summary-article-number)))
10697         (old-mark (gnus-summary-article-mark article)))
10698     ;; Allow the backend to change the mark.
10699     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10700     (if (eq mark old-mark)
10701         t
10702       (unless article
10703         (error "No article on current line"))
10704       (if (not (if (or (= mark gnus-unread-mark)
10705                        (= mark gnus-ticked-mark)
10706                        (= mark gnus-spam-mark)
10707                        (= mark gnus-dormant-mark))
10708                    (gnus-mark-article-as-unread article mark)
10709                  (gnus-mark-article-as-read article mark)))
10710           t
10711         ;; See whether the article is to be put in the cache.
10712         (and gnus-use-cache
10713              (not (= mark gnus-canceled-mark))
10714              (vectorp (gnus-summary-article-header article))
10715              (save-excursion
10716                (gnus-cache-possibly-enter-article
10717                 gnus-newsgroup-name article
10718                 (= mark gnus-ticked-mark)
10719                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10720
10721         (when (gnus-summary-goto-subject article nil t)
10722           (let ((inhibit-read-only t))
10723             (gnus-summary-show-thread)
10724             ;; Fix the mark.
10725             (gnus-summary-update-mark mark 'unread)
10726             t))))))
10727
10728 (defun gnus-summary-update-secondary-mark (article)
10729   "Update the secondary (read, process, cache) mark."
10730   (gnus-summary-update-mark
10731    (cond ((memq article gnus-newsgroup-processable)
10732           gnus-process-mark)
10733          ((memq article gnus-newsgroup-cached)
10734           gnus-cached-mark)
10735          ((memq article gnus-newsgroup-replied)
10736           gnus-replied-mark)
10737          ((memq article gnus-newsgroup-forwarded)
10738           gnus-forwarded-mark)
10739          ((memq article gnus-newsgroup-saved)
10740           gnus-saved-mark)
10741          ((memq article gnus-newsgroup-recent)
10742           gnus-recent-mark)
10743          ((memq article gnus-newsgroup-unseen)
10744           gnus-unseen-mark)
10745          (t gnus-no-mark))
10746    'replied)
10747   (when (gnus-visual-p 'summary-highlight 'highlight)
10748     (gnus-summary-highlight-line)
10749     (gnus-run-hooks 'gnus-summary-update-hook))
10750   t)
10751
10752 (defun gnus-summary-update-download-mark (article)
10753   "Update the download mark."
10754   (gnus-summary-update-mark
10755    (cond ((memq article gnus-newsgroup-undownloaded)
10756           gnus-undownloaded-mark)
10757          (gnus-newsgroup-agentized
10758           gnus-downloaded-mark)
10759          (t
10760           gnus-no-mark))
10761    'download)
10762   (gnus-summary-update-line t)
10763   t)
10764
10765 (defun gnus-summary-update-mark (mark type)
10766   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10767         (inhibit-read-only t))
10768     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10769     (when forward
10770       (when (looking-at "\r")
10771         (incf forward))
10772       (when (<= (+ forward (point)) (point-max))
10773         ;; Go to the right position on the line.
10774         (goto-char (+ forward (point)))
10775         ;; Replace the old mark with the new mark.
10776         (let ((to-insert
10777                (mm-subst-char-in-string
10778                 (char-after) mark
10779                 (buffer-substring (point) (1+ (point))))))
10780           (delete-region (point) (1+ (point)))
10781           (insert to-insert))
10782         ;; Optionally update the marks by some user rule.
10783         (when (eq type 'unread)
10784           (gnus-data-set-mark
10785            (gnus-data-find (gnus-summary-article-number)) mark)
10786           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10787
10788 (defun gnus-mark-article-as-read (article &optional mark)
10789   "Enter ARTICLE in the pertinent lists and remove it from others."
10790   ;; Make the article expirable.
10791   (let ((mark (or mark gnus-del-mark)))
10792     (setq gnus-newsgroup-expirable
10793           (if (= mark gnus-expirable-mark)
10794               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10795             (delq article gnus-newsgroup-expirable)))
10796     ;; Remove from unread and marked lists.
10797     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10798     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10799     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10800     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10801     (push (cons article mark) gnus-newsgroup-reads)
10802     ;; Possibly remove from cache, if that is used.
10803     (when gnus-use-cache
10804       (gnus-cache-enter-remove-article article))
10805     t))
10806
10807 (defun gnus-mark-article-as-unread (article &optional mark)
10808   "Enter ARTICLE in the pertinent lists and remove it from others."
10809   (let ((mark (or mark gnus-ticked-mark)))
10810     (if (<= article 0)
10811         (progn
10812           (gnus-error 1 "Can't mark negative article numbers")
10813           nil)
10814       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10815             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10816             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10817             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10818             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10819
10820       ;; Unsuppress duplicates?
10821       (when gnus-suppress-duplicates
10822         (gnus-dup-unsuppress-article article))
10823
10824       (cond ((= mark gnus-ticked-mark)
10825              (setq gnus-newsgroup-marked
10826                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10827             ((= mark gnus-spam-mark)
10828              (setq gnus-newsgroup-spam-marked
10829                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10830                                             article)))
10831             ((= mark gnus-dormant-mark)
10832              (setq gnus-newsgroup-dormant
10833                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10834             (t
10835              (setq gnus-newsgroup-unreads
10836                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10837       (gnus-pull article gnus-newsgroup-reads)
10838       t)))
10839
10840 (defalias 'gnus-summary-mark-as-unread-forward
10841   'gnus-summary-tick-article-forward)
10842 (make-obsolete 'gnus-summary-mark-as-unread-forward
10843                'gnus-summary-tick-article-forward "Emacs 20.4")
10844 (defun gnus-summary-tick-article-forward (n)
10845   "Tick N articles forwards.
10846 If N is negative, tick backwards instead.
10847 The difference between N and the number of articles ticked is returned."
10848   (interactive "p")
10849   (gnus-summary-mark-forward n gnus-ticked-mark))
10850
10851 (defalias 'gnus-summary-mark-as-unread-backward
10852   'gnus-summary-tick-article-backward)
10853 (make-obsolete 'gnus-summary-mark-as-unread-backward
10854                'gnus-summary-tick-article-backward "Emacs 20.4")
10855 (defun gnus-summary-tick-article-backward (n)
10856   "Tick N articles backwards.
10857 The difference between N and the number of articles ticked is returned."
10858   (interactive "p")
10859   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10860
10861 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10862 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article "Emacs 20.4")
10863 (defun gnus-summary-tick-article (&optional article clear-mark)
10864   "Mark current article as unread.
10865 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10866 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10867   (interactive)
10868   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10869                                        gnus-ticked-mark)))
10870
10871 (defun gnus-summary-mark-as-read-forward (n)
10872   "Mark N articles as read forwards.
10873 If N is negative, mark backwards instead.
10874 The difference between N and the actual number of articles marked is
10875 returned."
10876   (interactive "p")
10877   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10878
10879 (defun gnus-summary-mark-as-read-backward (n)
10880   "Mark the N articles as read backwards.
10881 The difference between N and the actual number of articles marked is
10882 returned."
10883   (interactive "p")
10884   (gnus-summary-mark-forward
10885    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10886
10887 (defun gnus-summary-mark-as-read (&optional article mark)
10888   "Mark current article as read.
10889 ARTICLE specifies the article to be marked as read.
10890 MARK specifies a string to be inserted at the beginning of the line."
10891   (gnus-summary-mark-article article mark))
10892
10893 (defun gnus-summary-clear-mark-forward (n)
10894   "Clear marks from N articles forward.
10895 If N is negative, clear backward instead.
10896 The difference between N and the number of marks cleared is returned."
10897   (interactive "p")
10898   (gnus-summary-mark-forward n gnus-unread-mark))
10899
10900 (defun gnus-summary-clear-mark-backward (n)
10901   "Clear marks from N articles backward.
10902 The difference between N and the number of marks cleared is returned."
10903   (interactive "p")
10904   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10905
10906 (defun gnus-summary-mark-unread-as-read ()
10907   "Intended to be used by `gnus-mark-article-hook'."
10908   (when (memq gnus-current-article gnus-newsgroup-unreads)
10909     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10910
10911 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10912   "Intended to be used by `gnus-mark-article-hook'."
10913   (let ((mark (gnus-summary-article-mark)))
10914     (when (or (gnus-unread-mark-p mark)
10915               (gnus-read-mark-p mark))
10916       (gnus-summary-mark-article gnus-current-article
10917                                  (or new-mark gnus-read-mark)))))
10918
10919 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10920   "Intended to be used by `gnus-mark-article-hook'."
10921   (let ((mark (gnus-summary-article-mark)))
10922     (when (or (gnus-unread-mark-p mark)
10923               (gnus-read-mark-p mark))
10924       (gnus-summary-mark-article (gnus-summary-article-number)
10925                                  (or new-mark gnus-read-mark)))))
10926
10927 (defun gnus-summary-mark-unread-as-ticked ()
10928   "Intended to be used by `gnus-mark-article-hook'."
10929   (when (memq gnus-current-article gnus-newsgroup-unreads)
10930     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10931
10932 (defun gnus-summary-mark-region-as-read (point mark all)
10933   "Mark all unread articles between point and mark as read.
10934 If given a prefix, mark all articles between point and mark as read,
10935 even ticked and dormant ones."
10936   (interactive "r\nP")
10937   (save-excursion
10938     (let (article)
10939       (goto-char point)
10940       (beginning-of-line)
10941       (while (and
10942               (< (point) mark)
10943               (progn
10944                 (when (or all
10945                           (memq (setq article (gnus-summary-article-number))
10946                                 gnus-newsgroup-unreads))
10947                   (gnus-summary-mark-article article gnus-del-mark))
10948                 t)
10949               (gnus-summary-find-next))))))
10950
10951 (defun gnus-summary-mark-below (score mark)
10952   "Mark articles with score less than SCORE with MARK."
10953   (interactive "P\ncMark: ")
10954   (setq score (if score
10955                   (prefix-numeric-value score)
10956                 (or gnus-summary-default-score 0)))
10957   (with-current-buffer gnus-summary-buffer
10958     (goto-char (point-min))
10959     (while
10960         (progn
10961           (and (< (gnus-summary-article-score) score)
10962                (gnus-summary-mark-article nil mark))
10963           (gnus-summary-find-next)))))
10964
10965 (defun gnus-summary-kill-below (&optional score)
10966   "Mark articles with score below SCORE as read."
10967   (interactive "P")
10968   (gnus-summary-mark-below score gnus-killed-mark))
10969
10970 (defun gnus-summary-clear-above (&optional score)
10971   "Clear all marks from articles with score above SCORE."
10972   (interactive "P")
10973   (gnus-summary-mark-above score gnus-unread-mark))
10974
10975 (defun gnus-summary-tick-above (&optional score)
10976   "Tick all articles with score above SCORE."
10977   (interactive "P")
10978   (gnus-summary-mark-above score gnus-ticked-mark))
10979
10980 (defun gnus-summary-mark-above (score mark)
10981   "Mark articles with score over SCORE with MARK."
10982   (interactive "P\ncMark: ")
10983   (setq score (if score
10984                   (prefix-numeric-value score)
10985                 (or gnus-summary-default-score 0)))
10986   (with-current-buffer gnus-summary-buffer
10987     (goto-char (point-min))
10988     (while (and (progn
10989                   (when (> (gnus-summary-article-score) score)
10990                     (gnus-summary-mark-article nil mark))
10991                   t)
10992                 (gnus-summary-find-next)))))
10993
10994 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10995 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10996 (defun gnus-summary-limit-include-expunged (&optional no-error)
10997   "Display all the hidden articles that were expunged for low scores."
10998   (interactive)
10999   (let ((inhibit-read-only t))
11000     (let ((scored gnus-newsgroup-scored)
11001           headers h)
11002       (while scored
11003         (unless (gnus-summary-article-header (caar scored))
11004           (and (setq h (gnus-number-to-header (caar scored)))
11005                (< (cdar scored) gnus-summary-expunge-below)
11006                (push h headers)))
11007         (setq scored (cdr scored)))
11008       (if (not headers)
11009           (when (not no-error)
11010             (error "No expunged articles hidden"))
11011         (goto-char (point-min))
11012         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11013         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11014         (dolist (x headers)
11015           (push (mail-header-number x) gnus-newsgroup-limit))
11016         (gnus-summary-prepare-unthreaded (nreverse headers))
11017         (goto-char (point-min))
11018         (gnus-summary-position-point)
11019         t))))
11020
11021 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11022   "Mark all unread articles in this newsgroup as read.
11023 If prefix argument ALL is non-nil, ticked and dormant articles will
11024 also be marked as read.
11025 If QUIETLY is non-nil, no questions will be asked.
11026
11027 If TO-HERE is non-nil, it should be a point in the buffer.  All
11028 articles before (after, if REVERSE is set) this point will be marked
11029 as read.
11030
11031 Note that this function will only catch up the unread article
11032 in the current summary buffer limitation.
11033
11034 The number of articles marked as read is returned."
11035   (interactive "P")
11036   (prog1
11037       (save-excursion
11038         (when (or quietly
11039                   (not gnus-interactive-catchup) ;Without confirmation?
11040                   gnus-expert-user
11041                   (gnus-y-or-n-p
11042                    (if all
11043                        "Mark absolutely all articles as read? "
11044                      "Mark all unread articles as read? ")))
11045           (if (and not-mark
11046                    (not gnus-newsgroup-adaptive)
11047                    (not gnus-newsgroup-auto-expire)
11048                    (not gnus-suppress-duplicates)
11049                    (or (not gnus-use-cache)
11050                        (eq gnus-use-cache 'passive)))
11051               (progn
11052                 (when all
11053                   (setq gnus-newsgroup-marked nil
11054                         gnus-newsgroup-spam-marked nil
11055                         gnus-newsgroup-dormant nil))
11056                 (setq gnus-newsgroup-unreads
11057                       (gnus-sorted-nunion
11058                        (gnus-sorted-intersection gnus-newsgroup-unreads
11059                                                  gnus-newsgroup-downloadable)
11060                        (gnus-sorted-difference gnus-newsgroup-unfetched
11061                                                gnus-newsgroup-cached))))
11062             ;; We actually mark all articles as canceled, which we
11063             ;; have to do when using auto-expiry or adaptive scoring.
11064             (gnus-summary-show-all-threads)
11065             (if (and to-here reverse)
11066                 (progn
11067                   (goto-char to-here)
11068                   (gnus-summary-mark-current-read-and-unread-as-read
11069                    gnus-catchup-mark)
11070                   (while (gnus-summary-find-next (not all))
11071                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11072               (when (gnus-summary-first-subject (not all))
11073                 (while (and
11074                         (if to-here (< (point) to-here) t)
11075                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11076                         (gnus-summary-find-next (not all))))))
11077             (gnus-set-mode-line 'summary))
11078           t))
11079     (gnus-summary-position-point)))
11080
11081 (defun gnus-summary-catchup-to-here (&optional all)
11082   "Mark all unticked articles before the current one as read.
11083 If ALL is non-nil, also mark ticked and dormant articles as read."
11084   (interactive "P")
11085   (save-excursion
11086     (gnus-save-hidden-threads
11087       (let ((beg (point)))
11088         ;; We check that there are unread articles.
11089         (when (or all (gnus-summary-find-prev))
11090           (gnus-summary-catchup all t beg)))))
11091   (gnus-summary-position-point))
11092
11093 (defun gnus-summary-catchup-from-here (&optional all)
11094   "Mark all unticked articles after (and including) the current one as read.
11095 If ALL is non-nil, also mark ticked and dormant articles as read."
11096   (interactive "P")
11097   (save-excursion
11098     (gnus-save-hidden-threads
11099       (let ((beg (point)))
11100         ;; We check that there are unread articles.
11101         (when (or all (gnus-summary-find-next))
11102           (gnus-summary-catchup all t beg nil t)))))
11103   (gnus-summary-position-point))
11104
11105 (defun gnus-summary-catchup-all (&optional quietly)
11106   "Mark all articles in this newsgroup as read.
11107 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11108 instead, which marks only unread articles as read."
11109   (interactive "P")
11110   (gnus-summary-catchup t quietly))
11111
11112 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11113   "Mark all unread articles in this group as read, then exit.
11114 If prefix argument ALL is non-nil, all articles are marked as read.
11115 If QUIETLY is non-nil, no questions will be asked."
11116   (interactive "P")
11117   (when (gnus-summary-catchup all quietly nil 'fast)
11118     ;; Select next newsgroup or exit.
11119     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11120              (eq gnus-auto-select-next 'quietly))
11121         (gnus-summary-next-group nil)
11122       (gnus-summary-exit))))
11123
11124 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11125   "Mark all articles in this newsgroup as read, and then exit.
11126 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11127 instead, which marks only unread articles as read."
11128   (interactive "P")
11129   (gnus-summary-catchup-and-exit t quietly))
11130
11131 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11132   "Mark all articles in this group as read and select the next group.
11133 If given a prefix, mark all articles, unread as well as ticked, as
11134 read."
11135   (interactive "P")
11136   (save-excursion
11137     (gnus-summary-catchup all))
11138   (gnus-summary-next-group))
11139
11140 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11141   "Mark all articles in this group as read and select the previous group.
11142 If given a prefix, mark all articles, unread as well as ticked, as
11143 read."
11144   (interactive "P")
11145   (save-excursion
11146     (gnus-summary-catchup all))
11147   (gnus-summary-next-group nil nil t))
11148
11149 ;;;
11150 ;;; with article
11151 ;;;
11152
11153 (defmacro gnus-with-article (article &rest forms)
11154   "Select ARTICLE and perform FORMS in the original article buffer.
11155 Then replace the article with the result."
11156   `(progn
11157      ;; We don't want the article to be marked as read.
11158      (let (gnus-mark-article-hook)
11159        (gnus-summary-select-article t t nil ,article))
11160      (set-buffer gnus-original-article-buffer)
11161      ,@forms
11162      (if (not (gnus-check-backend-function
11163                'request-replace-article (car gnus-article-current)))
11164          (gnus-message 5 "Read-only group; not replacing")
11165        (unless (gnus-request-replace-article
11166                 ,article (car gnus-article-current)
11167                 (current-buffer) t)
11168          (error "Couldn't replace article")))
11169      ;; The cache and backlog have to be flushed somewhat.
11170      (when gnus-keep-backlog
11171        (gnus-backlog-remove-article
11172         (car gnus-article-current) (cdr gnus-article-current)))
11173      (when gnus-use-cache
11174        (gnus-cache-update-article
11175         (car gnus-article-current) (cdr gnus-article-current)))))
11176
11177 (put 'gnus-with-article 'lisp-indent-function 1)
11178 (put 'gnus-with-article 'edebug-form-spec '(form body))
11179
11180 ;; Thread-based commands.
11181
11182 (defun gnus-summary-articles-in-thread (&optional article)
11183   "Return a list of all articles in the current thread.
11184 If ARTICLE is non-nil, return all articles in the thread that starts
11185 with that article."
11186   (let* ((article (or article (gnus-summary-article-number)))
11187          (data (gnus-data-find-list article))
11188          (top-level (gnus-data-level (car data)))
11189          (top-subject
11190           (cond ((null gnus-thread-operation-ignore-subject)
11191                  (gnus-simplify-subject-re
11192                   (mail-header-subject (gnus-data-header (car data)))))
11193                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11194                  (gnus-simplify-subject-fuzzy
11195                   (mail-header-subject (gnus-data-header (car data)))))
11196                 (t nil)))
11197          (end-point (save-excursion
11198                       (if (gnus-summary-go-to-next-thread)
11199                           (point) (point-max))))
11200          articles)
11201     (while (and data
11202                 (< (gnus-data-pos (car data)) end-point))
11203       (when (or (not top-subject)
11204                 (string= top-subject
11205                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11206                              (gnus-simplify-subject-fuzzy
11207                               (mail-header-subject
11208                                (gnus-data-header (car data))))
11209                            (gnus-simplify-subject-re
11210                             (mail-header-subject
11211                              (gnus-data-header (car data)))))))
11212         (push (gnus-data-number (car data)) articles))
11213       (unless (and (setq data (cdr data))
11214                    (> (gnus-data-level (car data)) top-level))
11215         (setq data nil)))
11216     ;; Return the list of articles.
11217     (nreverse articles)))
11218
11219 (defun gnus-summary-rethread-current ()
11220   "Rethread the thread the current article is part of."
11221   (interactive)
11222   (let* ((gnus-show-threads t)
11223          (article (gnus-summary-article-number))
11224          (id (mail-header-id (gnus-summary-article-header)))
11225          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11226     (unless id
11227       (error "No article on the current line"))
11228     (gnus-rebuild-thread id)
11229     (gnus-summary-goto-subject article)))
11230
11231 (defun gnus-summary-reparent-thread ()
11232   "Make the current article child of the marked (or previous) article.
11233
11234 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11235 is non-nil or the Subject: of both articles are the same."
11236   (interactive)
11237   (unless (not (gnus-group-read-only-p))
11238     (error "The current newsgroup does not support article editing"))
11239   (unless (<= (length gnus-newsgroup-processable) 1)
11240     (error "No more than one article may be marked"))
11241   (let ((child (gnus-summary-article-number))
11242         ;; First grab the marked article, otherwise one line up.
11243         (parent (if (not (null gnus-newsgroup-processable))
11244                     (car gnus-newsgroup-processable)
11245                   (save-excursion
11246                     (if (eq (forward-line -1) 0)
11247                         (gnus-summary-article-number)
11248                       (error "Beginning of summary buffer"))))))
11249     (gnus-summary-reparent-children parent (list child))))
11250
11251 (defun gnus-summary-reparent-children (parent children)
11252   "Make PARENT the parent of CHILDREN.
11253 When called interactively, PARENT is the current article and CHILDREN
11254 are the process-marked articles."
11255   (interactive
11256    (list (gnus-summary-article-number)
11257          (gnus-summary-work-articles nil)))
11258   (dolist (child children)
11259     (save-window-excursion
11260       (let ((gnus-article-buffer " *reparent*"))
11261         (unless (not (eq parent child))
11262           (error "An article may not be self-referential"))
11263         (let ((message-id (mail-header-id
11264                            (gnus-summary-article-header parent))))
11265           (unless (and message-id (not (equal message-id "")))
11266             (error "No message-id in desired parent"))
11267           (gnus-with-article child
11268             (save-restriction
11269               (goto-char (point-min))
11270               (message-narrow-to-head)
11271               (if (re-search-forward "^References: " nil t)
11272                   (progn
11273                     (re-search-forward "^[^ \t]" nil t)
11274                     (forward-line -1)
11275                     (end-of-line)
11276                     (insert " " message-id))
11277                 (insert "References: " message-id "\n"))))
11278           (set-buffer gnus-summary-buffer)
11279           (gnus-summary-unmark-all-processable)
11280           (gnus-summary-update-article child)
11281           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11282             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11283           (gnus-summary-rethread-current)
11284           (gnus-message 3 "Article %d is now the child of article %d"
11285                         child parent))))))
11286
11287 (defun gnus-summary-toggle-threads (&optional arg)
11288   "Toggle showing conversation threads.
11289 If ARG is positive number, turn showing conversation threads on."
11290   (interactive "P")
11291   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11292     (setq gnus-show-threads
11293           (if (null arg) (not gnus-show-threads)
11294             (> (prefix-numeric-value arg) 0)))
11295     (gnus-summary-prepare)
11296     (gnus-summary-goto-subject current)
11297     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11298     (gnus-summary-position-point)))
11299
11300 (eval-and-compile
11301   (if (fboundp 'remove-overlays)
11302       (defalias 'gnus-remove-overlays 'remove-overlays)
11303     (defun gnus-remove-overlays (beg end name val)
11304       "Clear BEG and END of overlays whose property NAME has value VAL.
11305 For compatibility with XEmacs."
11306       (dolist (ov (gnus-overlays-in beg end))
11307         (when (eq (gnus-overlay-get ov name) val)
11308           (gnus-delete-overlay ov))))))
11309
11310 (defun gnus-summary-show-all-threads ()
11311   "Show all threads."
11312   (interactive)
11313   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11314   (gnus-summary-position-point))
11315
11316 (defun gnus-summary-show-thread ()
11317   "Show thread subtrees.
11318 Returns nil if no thread was there to be shown."
11319   (interactive)
11320   (let* ((orig (point))
11321          (end (point-at-eol))
11322          ;; Leave point at bol
11323          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11324          (eoi (when (eq (get-char-property end 'invisible) 'gnus-sum)
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 (boundp group)
11890        (symbol-name group)
11891        (symbol-value group)
11892        (gnus-get-function (gnus-find-method-for-group
11893                            (symbol-name group)) 'request-accept-article t)))
11894
11895 (defun gnus-read-move-group-name (prompt default articles prefix)
11896   "Read a group name."
11897   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11898          (minibuffer-confirm-incomplete nil) ; XEmacs
11899          (prom
11900           (format "%s %s to"
11901                   prompt
11902                   (if (> (length articles) 1)
11903                       (format "these %d articles" (length articles))
11904                     "this article")))
11905          (to-newsgroup
11906           (let (active group)
11907             (when (or (null split-name) (= 1 (length split-name)))
11908               (setq active (gnus-make-hashtable (length gnus-active-hashtb)))
11909               (mapatoms (lambda (symbol)
11910                           (setq group (symbol-name symbol))
11911                           (when (string-match "[^\000-\177]" group)
11912                             (setq group (gnus-group-decoded-name group)))
11913                           (set (intern group active) group))
11914                         gnus-active-hashtb))
11915             (cond
11916              ((null split-name)
11917               (gnus-completing-read-with-default
11918                default prom active 'gnus-valid-move-group-p nil prefix
11919                'gnus-group-history))
11920              ((= 1 (length split-name))
11921               (gnus-completing-read-with-default
11922                (car split-name) prom active 'gnus-valid-move-group-p nil nil
11923                'gnus-group-history))
11924              (t
11925               (gnus-completing-read-with-default
11926                nil prom (mapcar 'list (nreverse split-name)) nil nil nil
11927                'gnus-group-history)))))
11928          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11929          encoded)
11930     (when to-newsgroup
11931       (if (or (string= to-newsgroup "")
11932               (string= to-newsgroup prefix))
11933           (setq to-newsgroup default))
11934       (unless to-newsgroup
11935         (error "No group name entered"))
11936       (setq encoded (mm-encode-coding-string
11937                      to-newsgroup
11938                      (gnus-group-name-charset to-method to-newsgroup)))
11939       (or (gnus-active encoded)
11940           (gnus-activate-group encoded nil nil to-method)
11941           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11942                                      to-newsgroup))
11943               (or (and (gnus-request-create-group encoded to-method)
11944                        (gnus-activate-group encoded nil nil to-method)
11945                        (gnus-subscribe-group encoded))
11946                   (error "Couldn't create group %s" to-newsgroup)))
11947           (error "No such group: %s" to-newsgroup))
11948       encoded)))
11949
11950 (defvar gnus-summary-save-parts-counter)
11951 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11952
11953 (defun gnus-summary-save-parts (type dir n &optional reverse)
11954   "Save parts matching TYPE to DIR.
11955 If REVERSE, save parts that do not match TYPE."
11956   (interactive
11957    (list (read-string "Save parts of type: "
11958                       (or (car gnus-summary-save-parts-type-history)
11959                           gnus-summary-save-parts-default-mime)
11960                       'gnus-summary-save-parts-type-history)
11961          (setq gnus-summary-save-parts-last-directory
11962                (read-file-name "Save to directory: "
11963                                gnus-summary-save-parts-last-directory
11964                                nil t))
11965          current-prefix-arg))
11966   (gnus-summary-iterate n
11967     (let ((gnus-display-mime-function nil)
11968           gnus-article-prepare-hook
11969           gnus-article-decode-hook
11970           gnus-display-mime-function
11971           gnus-break-pages
11972           (gnus-inhibit-treatment t))
11973       (gnus-summary-select-article))
11974     (with-current-buffer gnus-article-buffer
11975       (let ((handles (or gnus-article-mime-handles
11976                          (mm-dissect-buffer nil gnus-article-loose-mime)
11977                          (and gnus-article-emulate-mime
11978                               (mm-uu-dissect))))
11979             (gnus-summary-save-parts-counter 1))
11980         (when handles
11981           (gnus-summary-save-parts-1 type dir handles reverse)
11982           (unless gnus-article-mime-handles ;; Don't destroy this case.
11983             (mm-destroy-parts handles)))))))
11984
11985 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11986   (if (stringp (car handle))
11987       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11988               (cdr handle))
11989     (when (if reverse
11990               (not (string-match type (mm-handle-media-type handle)))
11991             (string-match type (mm-handle-media-type handle)))
11992       (let ((file (expand-file-name
11993                    (gnus-map-function
11994                     mm-file-name-rewrite-functions
11995                     (file-name-nondirectory
11996                      (or
11997                       (mail-content-type-get
11998                        (mm-handle-disposition handle) 'filename)
11999                       (mail-content-type-get
12000                        (mm-handle-type handle) 'name)
12001                       (format "%s.%d.%d" gnus-newsgroup-name
12002                               (cdr gnus-article-current)
12003                               gnus-summary-save-parts-counter))))
12004                    dir)))
12005         (incf gnus-summary-save-parts-counter)
12006         (unless (file-exists-p file)
12007           (mm-save-part-to-file handle file))))))
12008
12009 ;; Summary extract commands
12010
12011 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12012   (let ((inhibit-read-only t)
12013         (article (gnus-summary-article-number))
12014         after-article b e)
12015     (unless (gnus-summary-goto-subject article)
12016       (error "No such article: %d" article))
12017     (gnus-summary-position-point)
12018     ;; If all commands are to be bunched up on one line, we collect
12019     ;; them here.
12020     (unless gnus-view-pseudos-separately
12021       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12022             files action)
12023         (while ps
12024           (setq action (cdr (assq 'action (car ps))))
12025           (setq files (list (cdr (assq 'name (car ps)))))
12026           (while (and ps (cdr ps)
12027                       (string= (or action "1")
12028                                (or (cdr (assq 'action (cadr ps))) "2")))
12029             (push (cdr (assq 'name (cadr ps))) files)
12030             (setcdr ps (cddr ps)))
12031           (when files
12032             (when (not (string-match "%s" action))
12033               (push " " files))
12034             (push " " files)
12035             (when (assq 'execute (car ps))
12036               (setcdr (assq 'execute (car ps))
12037                       (funcall (if (string-match "%s" action)
12038                                    'format 'concat)
12039                                action
12040                                (mapconcat
12041                                 (lambda (f)
12042                                   (if (equal f " ")
12043                                       f
12044                                     (shell-quote-argument f)))
12045                                 files " ")))))
12046           (setq ps (cdr ps)))))
12047     (if (and gnus-view-pseudos (not not-view))
12048         (while pslist
12049           (when (assq 'execute (car pslist))
12050             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12051                                   (eq gnus-view-pseudos 'not-confirm)))
12052           (setq pslist (cdr pslist)))
12053       (save-excursion
12054         (while pslist
12055           (setq after-article (or (cdr (assq 'article (car pslist)))
12056                                   (gnus-summary-article-number)))
12057           (gnus-summary-goto-subject after-article)
12058           (forward-line 1)
12059           (setq b (point))
12060           (insert "    " (file-name-nondirectory
12061                           (cdr (assq 'name (car pslist))))
12062                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12063           (setq e (point))
12064           (forward-line -1)             ; back to `b'
12065           (gnus-add-text-properties
12066            b (1- e) (list 'gnus-number gnus-reffed-article-number
12067                           gnus-mouse-face-prop gnus-mouse-face))
12068           (gnus-data-enter
12069            after-article gnus-reffed-article-number
12070            gnus-unread-mark b (car pslist) 0 (- e b))
12071           (setq gnus-newsgroup-unreads
12072                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12073                                          gnus-reffed-article-number))
12074           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12075           (setq pslist (cdr pslist)))))))
12076
12077 (defun gnus-pseudos< (p1 p2)
12078   (let ((c1 (cdr (assq 'action p1)))
12079         (c2 (cdr (assq 'action p2))))
12080     (and c1 c2 (string< c1 c2))))
12081
12082 (defun gnus-request-pseudo-article (props)
12083   (cond ((assq 'execute props)
12084          (gnus-execute-command (cdr (assq 'execute props)))))
12085   (let ((gnus-current-article (gnus-summary-article-number)))
12086     (gnus-run-hooks 'gnus-mark-article-hook)))
12087
12088 (defun gnus-execute-command (command &optional automatic)
12089   (save-excursion
12090     (gnus-article-setup-buffer)
12091     (set-buffer gnus-article-buffer)
12092     (setq buffer-read-only nil)
12093     (let ((command (if automatic command
12094                      (read-string "Command: " (cons command 0)))))
12095       (erase-buffer)
12096       (insert "$ " command "\n\n")
12097       (if gnus-view-pseudo-asynchronously
12098           (start-process "gnus-execute" (current-buffer) shell-file-name
12099                          shell-command-switch command)
12100         (call-process shell-file-name nil t nil
12101                       shell-command-switch command)))))
12102
12103 ;; Summary kill commands.
12104
12105 (defun gnus-summary-edit-global-kill (article)
12106   "Edit the \"global\" kill file."
12107   (interactive (list (gnus-summary-article-number)))
12108   (gnus-group-edit-global-kill article))
12109
12110 (defun gnus-summary-edit-local-kill ()
12111   "Edit a local kill file applied to the current newsgroup."
12112   (interactive)
12113   (setq gnus-current-headers (gnus-summary-article-header))
12114   (gnus-group-edit-local-kill
12115    (gnus-summary-article-number) gnus-newsgroup-name))
12116
12117 ;;; Header reading.
12118
12119 (defun gnus-read-header (id &optional header)
12120   "Read the headers of article ID and enter them into the Gnus system."
12121   (let ((group gnus-newsgroup-name)
12122         (gnus-override-method
12123          (or
12124           gnus-override-method
12125           (and (gnus-news-group-p gnus-newsgroup-name)
12126                (car (gnus-refer-article-methods)))))
12127         where)
12128     ;; First we check to see whether the header in question is already
12129     ;; fetched.
12130     (if (stringp id)
12131         ;; This is a Message-ID.
12132         (setq header (or header (gnus-id-to-header id)))
12133       ;; This is an article number.
12134       (setq header (or header (gnus-summary-article-header id))))
12135     (if (and header
12136              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12137         ;; We have found the header.
12138         header
12139       ;; We have to really fetch the header to this article.
12140       (with-current-buffer nntp-server-buffer
12141         (when (setq where (gnus-request-head id group))
12142           (nnheader-fold-continuation-lines)
12143           (goto-char (point-max))
12144           (insert ".\n")
12145           (goto-char (point-min))
12146           (insert "211 ")
12147           (princ (cond
12148                   ((numberp id) id)
12149                   ((cdr where) (cdr where))
12150                   (header (mail-header-number header))
12151                   (t gnus-reffed-article-number))
12152                  (current-buffer))
12153           (insert " Article retrieved.\n"))
12154         (if (or (not where)
12155                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12156             ()                          ; Malformed head.
12157           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12158             (when (and (stringp id)
12159                        (or
12160                         (not (string= (gnus-group-real-name group)
12161                                       (car where)))
12162                         (not (gnus-server-equal gnus-override-method
12163                                                 (gnus-group-method group)))))
12164               ;; If we fetched by Message-ID and the article came from
12165               ;; a different group (or server), we fudge some bogus
12166               ;; article numbers for this article.
12167               (mail-header-set-number header gnus-reffed-article-number))
12168             (with-current-buffer gnus-summary-buffer
12169               (decf gnus-reffed-article-number)
12170               (gnus-remove-header (mail-header-number header))
12171               (push header gnus-newsgroup-headers)
12172               (setq gnus-current-headers header)
12173               (push (mail-header-number header) gnus-newsgroup-limit)))
12174           header)))))
12175
12176 (defun gnus-remove-header (number)
12177   "Remove header NUMBER from `gnus-newsgroup-headers'."
12178   (if (and gnus-newsgroup-headers
12179            (= number (mail-header-number (car gnus-newsgroup-headers))))
12180       (pop gnus-newsgroup-headers)
12181     (let ((headers gnus-newsgroup-headers))
12182       (while (and (cdr headers)
12183                   (not (= number (mail-header-number (cadr headers)))))
12184         (pop headers))
12185       (when (cdr headers)
12186         (setcdr headers (cddr headers))))))
12187
12188 ;;;
12189 ;;; summary highlights
12190 ;;;
12191
12192 (defun gnus-highlight-selected-summary ()
12193   "Highlight selected article in summary buffer."
12194   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12195   (when gnus-summary-selected-face
12196     (save-excursion
12197       (let* ((beg (point-at-bol))
12198              (end (point-at-eol))
12199              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12200              (from (if (get-text-property beg gnus-mouse-face-prop)
12201                        beg
12202                      (or (next-single-property-change
12203                           beg gnus-mouse-face-prop nil end)
12204                          beg)))
12205              (to
12206               (if (= from end)
12207                   (- from 2)
12208                 (or (next-single-property-change
12209                      from gnus-mouse-face-prop nil end)
12210                     end))))
12211         ;; If no mouse-face prop on line we will have to = from = end,
12212         ;; so we highlight the entire line instead.
12213         (when (= (+ to 2) from)
12214           (setq from beg)
12215           (setq to end))
12216         (if gnus-newsgroup-selected-overlay
12217             ;; Move old overlay.
12218             (gnus-move-overlay
12219              gnus-newsgroup-selected-overlay from to (current-buffer))
12220           ;; Create new overlay.
12221           (gnus-overlay-put
12222            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12223            'face gnus-summary-selected-face))))))
12224
12225 (defvar gnus-summary-highlight-line-cached nil)
12226 (defvar gnus-summary-highlight-line-trigger nil)
12227
12228 (defun gnus-summary-highlight-line-0 ()
12229   (if (and (eq gnus-summary-highlight-line-trigger
12230                gnus-summary-highlight)
12231            gnus-summary-highlight-line-cached)
12232       gnus-summary-highlight-line-cached
12233     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12234           gnus-summary-highlight-line-cached
12235           (let* ((cond (list 'cond))
12236                  (c cond)
12237                  (list gnus-summary-highlight))
12238             (while list
12239               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12240                               nil))
12241               (setq c (cdr c)
12242                     list (cdr list)))
12243             (gnus-byte-compile (list 'lambda nil cond))))))
12244
12245 (defun gnus-summary-highlight-line ()
12246   "Highlight current line according to `gnus-summary-highlight'."
12247   (let* ((beg (point-at-bol))
12248          (article (or (gnus-summary-article-number) gnus-current-article))
12249          (score (or (cdr (assq article
12250                                gnus-newsgroup-scored))
12251                     gnus-summary-default-score 0))
12252          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12253          (inhibit-read-only t)
12254          (default gnus-summary-default-score)
12255          (default-high gnus-summary-default-high-score)
12256          (default-low gnus-summary-default-low-score)
12257          (uncached (and gnus-summary-use-undownloaded-faces
12258                         (memq article gnus-newsgroup-undownloaded)
12259                         (not (memq article gnus-newsgroup-cached)))))
12260     (let ((face (funcall (gnus-summary-highlight-line-0))))
12261       (unless (eq face (get-text-property beg 'face))
12262         (gnus-put-text-property-excluding-characters-with-faces
12263          beg (point-at-eol) 'face
12264          (setq face (if (boundp face) (symbol-value face) face)))
12265         (when gnus-summary-highlight-line-function
12266           (funcall gnus-summary-highlight-line-function article face))))))
12267
12268 (defun gnus-update-read-articles (group unread &optional compute)
12269   "Update the list of read articles in GROUP.
12270 UNREAD is a sorted list."
12271   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12272         (info (gnus-get-info group))
12273         (prev 1)
12274         read)
12275     (if (or (not info) (not active))
12276         ;; There is no info on this group if it was, in fact,
12277         ;; killed.  Gnus stores no information on killed groups, so
12278         ;; there's nothing to be done.
12279         ;; One could store the information somewhere temporarily,
12280         ;; perhaps...  Hmmm...
12281         ()
12282       ;; Remove any negative articles numbers.
12283       (while (and unread (< (car unread) 0))
12284         (setq unread (cdr unread)))
12285       ;; Remove any expired article numbers
12286       (while (and unread (< (car unread) (car active)))
12287         (setq unread (cdr unread)))
12288       ;; Compute the ranges of read articles by looking at the list of
12289       ;; unread articles.
12290       (while unread
12291         (when (/= (car unread) prev)
12292           (push (if (= prev (1- (car unread))) prev
12293                   (cons prev (1- (car unread))))
12294                 read))
12295         (setq prev (1+ (car unread)))
12296         (setq unread (cdr unread)))
12297       (when (<= prev (cdr active))
12298         (push (cons prev (cdr active)) read))
12299       (setq read (if (> (length read) 1) (nreverse read) read))
12300       (if compute
12301           read
12302         (save-excursion
12303           (let (setmarkundo)
12304             ;; Propagate the read marks to the backend.
12305             (when (and gnus-propagate-marks
12306                        (gnus-check-backend-function 'request-set-mark group))
12307               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12308                     (add (gnus-remove-from-range read (gnus-info-read info))))
12309                 (when (or add del)
12310                   (unless (gnus-check-group group)
12311                     (error "Can't open server for %s" group))
12312                   (gnus-request-set-mark
12313                    group (delq nil (list (if add (list add 'add '(read)))
12314                                          (if del (list del 'del '(read))))))
12315                   (setq setmarkundo
12316                         `(gnus-request-set-mark
12317                           ,group
12318                           ',(delq nil (list
12319                                        (if del (list del 'add '(read)))
12320                                        (if add (list add 'del '(read))))))))))
12321             (set-buffer gnus-group-buffer)
12322             (gnus-undo-register
12323               `(progn
12324                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12325                  (gnus-info-set-read ',info ',(gnus-info-read info))
12326                  (gnus-get-unread-articles-in-group ',info
12327                                                     (gnus-active ,group))
12328                  (gnus-group-update-group ,group t)
12329                  ,setmarkundo))))
12330         ;; Enter this list into the group info.
12331         (gnus-info-set-read info read)
12332         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12333         (gnus-get-unread-articles-in-group info (gnus-active group))
12334         t))))
12335
12336 (defun gnus-offer-save-summaries ()
12337   "Offer to save all active summary buffers."
12338   (let (buffers)
12339     ;; Go through all buffers and find all summaries.
12340     (dolist (buffer (buffer-list))
12341       (when (and (setq buffer (buffer-name buffer))
12342                  (string-match "Summary" buffer)
12343                  (with-current-buffer buffer
12344                    ;; We check that this is, indeed, a summary buffer.
12345                    (and (eq major-mode 'gnus-summary-mode)
12346                         ;; Also make sure this isn't bogus.
12347                         gnus-newsgroup-prepared
12348                         ;; Also make sure that this isn't a
12349                         ;; dead summary buffer.
12350                         (not gnus-dead-summary-mode))))
12351         (push buffer buffers)))
12352     ;; Go through all these summary buffers and offer to save them.
12353     (when buffers
12354       (save-excursion
12355         (map-y-or-n-p
12356          "Update summary buffer %s? "
12357          (lambda (buf)
12358            (switch-to-buffer buf)
12359            (gnus-summary-exit))
12360          buffers)))))
12361
12362 (defun gnus-summary-setup-default-charset ()
12363   "Setup newsgroup default charset."
12364   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12365       (setq gnus-newsgroup-charset nil)
12366     (let* ((ignored-charsets
12367             (or gnus-newsgroup-ephemeral-ignored-charsets
12368                 (append
12369                  (and gnus-newsgroup-name
12370                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12371                  gnus-newsgroup-ignored-charsets))))
12372       (setq gnus-newsgroup-charset
12373             (or gnus-newsgroup-ephemeral-charset
12374                 (and gnus-newsgroup-name
12375                      (gnus-parameter-charset gnus-newsgroup-name))
12376                 gnus-default-charset))
12377       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12378            ignored-charsets))))
12379
12380 ;;;
12381 ;;; Mime Commands
12382 ;;;
12383
12384 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12385   "Display the current article buffer fully MIME-buttonized.
12386 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12387 treated as multipart/mixed."
12388   (interactive "P")
12389   (require 'gnus-art)
12390   (let ((gnus-unbuttonized-mime-types nil)
12391         (gnus-mime-display-multipart-as-mixed show-all-parts))
12392     (gnus-summary-show-article)))
12393
12394 (defun gnus-summary-repair-multipart (article)
12395   "Add a Content-Type header to a multipart article without one."
12396   (interactive (list (gnus-summary-article-number)))
12397   (gnus-with-article article
12398     (message-narrow-to-head)
12399     (message-remove-header "Mime-Version")
12400     (goto-char (point-max))
12401     (insert "Mime-Version: 1.0\n")
12402     (widen)
12403     (when (search-forward "\n--" nil t)
12404       (let ((separator (buffer-substring (point) (point-at-eol))))
12405         (message-narrow-to-head)
12406         (message-remove-header "Content-Type")
12407         (goto-char (point-max))
12408         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12409                         separator))
12410         (widen))))
12411   (let (gnus-mark-article-hook)
12412     (gnus-summary-select-article t t nil article)))
12413
12414 (defun gnus-summary-toggle-display-buttonized ()
12415   "Toggle the buttonizing of the article buffer."
12416   (interactive)
12417   (require 'gnus-art)
12418   (if (setq gnus-inhibit-mime-unbuttonizing
12419             (not gnus-inhibit-mime-unbuttonizing))
12420       (let ((gnus-unbuttonized-mime-types nil))
12421         (gnus-summary-show-article))
12422     (gnus-summary-show-article)))
12423
12424 ;;;
12425 ;;; Generic summary marking commands
12426 ;;;
12427
12428 (defvar gnus-summary-marking-alist
12429   '((read gnus-del-mark "d")
12430     (unread gnus-unread-mark "u")
12431     (ticked gnus-ticked-mark "!")
12432     (dormant gnus-dormant-mark "?")
12433     (expirable gnus-expirable-mark "e"))
12434   "An alist of names/marks/keystrokes.")
12435
12436 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12437 (defvar gnus-summary-mark-map)
12438
12439 (defun gnus-summary-make-all-marking-commands ()
12440   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12441   (dolist (elem gnus-summary-marking-alist)
12442     (apply 'gnus-summary-make-marking-command elem)))
12443
12444 (defun gnus-summary-make-marking-command (name mark keystroke)
12445   (let ((map (make-sparse-keymap)))
12446     (define-key gnus-summary-generic-mark-map keystroke map)
12447     (dolist (lway `((next "next" next nil "n")
12448                     (next-unread "next unread" next t "N")
12449                     (prev "previous" prev nil "p")
12450                     (prev-unread "previous unread" prev t "P")
12451                     (nomove "" nil nil ,keystroke)))
12452       (let ((func (gnus-summary-make-marking-command-1
12453                    mark (car lway) lway name)))
12454         (setq func (eval func))
12455         (define-key map (nth 4 lway) func)))))
12456
12457 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12458   `(defun ,(intern
12459             (format "gnus-summary-put-mark-as-%s%s"
12460                     name (if (eq way 'nomove)
12461                              ""
12462                            (concat "-" (symbol-name way)))))
12463      (n)
12464      ,(format
12465        "Mark the current article as %s%s.
12466 If N, the prefix, then repeat N times.
12467 If N is negative, move in reverse order.
12468 The difference between N and the actual number of articles marked is
12469 returned."
12470        name (cadr lway))
12471      (interactive "p")
12472      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12473
12474 (defun gnus-summary-generic-mark (n mark move unread)
12475   "Mark N articles with MARK."
12476   (unless (eq major-mode 'gnus-summary-mode)
12477     (error "This command can only be used in the summary buffer"))
12478   (gnus-summary-show-thread)
12479   (let ((nummove
12480          (cond
12481           ((eq move 'next) 1)
12482           ((eq move 'prev) -1)
12483           (t 0))))
12484     (if (zerop nummove)
12485         (setq n 1)
12486       (when (< n 0)
12487         (setq n (abs n)
12488               nummove (* -1 nummove))))
12489     (while (and (> n 0)
12490                 (gnus-summary-mark-article nil mark)
12491                 (zerop (gnus-summary-next-subject nummove unread t)))
12492       (setq n (1- n)))
12493     (when (/= 0 n)
12494       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12495     (gnus-summary-recenter)
12496     (gnus-summary-position-point)
12497     (gnus-set-mode-line 'summary)
12498     n))
12499
12500 (defun gnus-summary-insert-articles (articles)
12501   (when (setq articles
12502               (gnus-sorted-difference articles
12503                                       (mapcar (lambda (h)
12504                                                 (mail-header-number h))
12505                                               gnus-newsgroup-headers)))
12506     (setq gnus-newsgroup-headers
12507           (gnus-merge 'list
12508                       gnus-newsgroup-headers
12509                       (gnus-fetch-headers articles)
12510                       'gnus-article-sort-by-number))
12511     ;; Suppress duplicates?
12512     (when gnus-suppress-duplicates
12513       (gnus-dup-suppress-articles))
12514
12515     (if (and gnus-fetch-old-headers
12516              (eq gnus-headers-retrieved-by 'nov))
12517         ;; We might want to build some more threads first.
12518         (if (eq gnus-fetch-old-headers 'invisible)
12519             (gnus-build-all-threads)
12520           (gnus-build-old-threads))
12521       ;; Mark the inserted articles that are unread as unread.
12522       (setq gnus-newsgroup-unreads
12523             (gnus-sorted-nunion
12524              gnus-newsgroup-unreads
12525              (gnus-sorted-nintersection
12526               (gnus-list-of-unread-articles gnus-newsgroup-name)
12527               articles)))
12528       ;; Mark the inserted articles as selected so that the information
12529       ;; of the marks having been changed by a user may be updated when
12530       ;; exiting this group.  See `gnus-summary-update-info'.
12531       (dolist (art articles)
12532         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12533     ;; Let the Gnus agent mark articles as read.
12534     (when gnus-agent
12535       (gnus-agent-get-undownloaded-list))
12536     ;; Remove list identifiers from subject
12537     (when gnus-list-identifiers
12538       (gnus-summary-remove-list-identifiers))
12539     ;; First and last article in this newsgroup.
12540     (when gnus-newsgroup-headers
12541       (setq gnus-newsgroup-begin
12542             (mail-header-number (car gnus-newsgroup-headers))
12543             gnus-newsgroup-end
12544             (mail-header-number
12545              (gnus-last-element gnus-newsgroup-headers))))
12546     (when gnus-use-scoring
12547       (gnus-possibly-score-headers))))
12548
12549 (defun gnus-summary-insert-old-articles (&optional all)
12550   "Insert all old articles in this group.
12551 If ALL is non-nil, already read articles become readable.
12552 If ALL is a number, fetch this number of articles."
12553   (interactive "P")
12554   (prog1
12555       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12556             older len)
12557         (setq older
12558               ;; Some nntp servers lie about their active range.  When
12559               ;; this happens, the active range can be in the millions.
12560               ;; Use a compressed range to avoid creating a huge list.
12561               (gnus-range-difference (list gnus-newsgroup-active) old))
12562         (setq len (gnus-range-length older))
12563         (cond
12564          ((null older) nil)
12565          ((numberp all)
12566           (if (< all len)
12567               (let ((older-range (nreverse older)))
12568                 (setq older nil)
12569
12570                 (while (> all 0)
12571                   (let* ((r (pop older-range))
12572                          (min (if (numberp r) r (car r)))
12573                          (max (if (numberp r) r (cdr r))))
12574                     (while (and (<= min max)
12575                                 (> all 0))
12576                       (push max older)
12577                       (setq all (1- all)
12578                             max (1- max))))))
12579             (setq older (gnus-uncompress-range older))))
12580          (all
12581           (setq older (gnus-uncompress-range older)))
12582          (t
12583           (when (and (numberp gnus-large-newsgroup)
12584                    (> len gnus-large-newsgroup))
12585               (let* ((cursor-in-echo-area nil)
12586                      (initial (gnus-parameter-large-newsgroup-initial
12587                                gnus-newsgroup-name))
12588                      (input
12589                       (read-string
12590                        (format
12591                         "How many articles from %s (%s %d): "
12592                         (gnus-group-decoded-name gnus-newsgroup-name)
12593                         (if initial "max" "default")
12594                         len)
12595                        (if initial
12596                            (cons (number-to-string initial)
12597                                  0)))))
12598                 (unless (string-match "^[ \t]*$" input)
12599                   (setq all (string-to-number input))
12600                   (if (< all len)
12601                       (let ((older-range (nreverse older)))
12602                         (setq older nil)
12603
12604                         (while (> all 0)
12605                           (let* ((r (pop older-range))
12606                                  (min (if (numberp r) r (car r)))
12607                                  (max (if (numberp r) r (cdr r))))
12608                             (while (and (<= min max)
12609                                         (> all 0))
12610                               (push max older)
12611                               (setq all (1- all)
12612                                     max (1- max))))))))))
12613           (setq older (gnus-uncompress-range older))))
12614         (if (not older)
12615             (message "No old news.")
12616           (gnus-summary-insert-articles older)
12617           (gnus-summary-limit (gnus-sorted-nunion old older))))
12618     (gnus-summary-position-point)))
12619
12620 (defun gnus-summary-insert-new-articles ()
12621   "Insert all new articles in this group."
12622   (interactive)
12623   (prog1
12624       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12625             (old-active gnus-newsgroup-active)
12626             (nnmail-fetched-sources (list t))
12627             i new)
12628         (setq gnus-newsgroup-active
12629               (gnus-activate-group gnus-newsgroup-name 'scan))
12630         (setq i (cdr gnus-newsgroup-active))
12631         (while (> i (cdr old-active))
12632           (push i new)
12633           (decf i))
12634         (if (not new)
12635             (message "No gnus is bad news")
12636           (gnus-summary-insert-articles new)
12637           (setq gnus-newsgroup-unreads
12638                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12639           (gnus-summary-limit (gnus-sorted-nunion old new))))
12640     (gnus-summary-position-point)))
12641
12642 ;;; Bookmark support for Gnus.
12643 (declare-function bookmark-make-record-default
12644                   "bookmark" (&optional no-file no-context posn))
12645 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12646 (declare-function bookmark-default-handler "bookmark" (bmk))
12647 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12648 (defvar bookmark-yank-point)
12649 (defvar bookmark-current-buffer)
12650
12651 (defun gnus-summary-bookmark-make-record ()
12652   "Make a bookmark entry for a Gnus summary buffer."
12653   (let (pos buf)
12654     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12655       (save-restriction              ; FIXME is it necessary to widen?
12656         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12657       (setq buf "art") ; We are recording bookmark from article buffer.
12658       (setq bookmark-yank-point (point))
12659       (setq bookmark-current-buffer (current-buffer))
12660       (gnus-article-show-summary))      ; Go back in summary buffer.
12661     ;; We are now recording bookmark from summary buffer.
12662     (unless buf (setq buf "sum"))
12663     (let* ((subject (elt (gnus-summary-article-header) 1))
12664            (grp     (car gnus-article-current))
12665            (art     (cdr gnus-article-current))
12666            (head    (gnus-summary-article-header art))
12667            (id      (mail-header-id head)))
12668       `(,subject
12669         ,@(condition-case nil
12670               (bookmark-make-record-default 'no-file 'no-context pos)
12671             (wrong-number-of-arguments
12672              (bookmark-make-record-default 'point-only)))
12673         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12674         (group . ,grp) (article . ,art)
12675         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12676
12677 ;;;###autoload
12678 (defun gnus-summary-bookmark-jump (bookmark)
12679   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12680 BOOKMARK is a bookmark name or a bookmark record."
12681   (let ((group    (bookmark-prop-get bookmark 'group))
12682         (article  (bookmark-prop-get bookmark 'article))
12683         (id       (bookmark-prop-get bookmark 'message-id))
12684         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12685     (gnus-fetch-group group (list article))
12686     (gnus-summary-insert-cached-articles)
12687     (gnus-summary-goto-article id nil 'force)
12688     ;; FIXME we have to wait article buffer is ready (only large buffer)
12689     ;; Is there a better solution to know that?
12690     ;; If we don't wait `bookmark-default-handler' will have no chance
12691     ;; to set position. However there is no error, just wrong pos.
12692     (sit-for 1)
12693     (when (string= buf "Gnus-art")
12694       (other-window 1))
12695     (bookmark-default-handler
12696      `(""
12697        (buffer . ,(current-buffer))
12698        . ,(bookmark-get-bookmark-record bookmark)))))
12699
12700 (gnus-summary-make-all-marking-commands)
12701
12702 (gnus-ems-redefine)
12703
12704 (provide 'gnus-sum)
12705
12706 (run-hooks 'gnus-sum-load-hook)
12707
12708 ;; Local Variables:
12709 ;; coding: iso-8859-1
12710 ;; End:
12711
12712 ;;; gnus-sum.el ends here