Add Gravatar support
[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   "D" gnus-summary-enter-digest-group
2051   "R" gnus-summary-refer-references
2052   "T" gnus-summary-refer-thread
2053   "g" gnus-summary-show-article
2054   "s" gnus-summary-isearch-article
2055   "P" gnus-summary-print-article
2056   "S" gnus-sticky-article
2057   "M" gnus-mailing-list-insinuate
2058   "t" gnus-article-babel)
2059
2060 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2061   "b" gnus-article-add-buttons
2062   "B" gnus-article-add-buttons-to-head
2063   "o" gnus-article-treat-overstrike
2064   "e" gnus-article-emphasize
2065   "w" gnus-article-fill-cited-article
2066   "Q" gnus-article-fill-long-lines
2067   "L" gnus-article-toggle-truncate-lines
2068   "C" gnus-article-capitalize-sentences
2069   "c" gnus-article-remove-cr
2070   "q" gnus-article-de-quoted-unreadable
2071   "6" gnus-article-de-base64-unreadable
2072   "Z" gnus-article-decode-HZ
2073   "A" gnus-article-treat-ansi-sequences
2074   "h" gnus-article-wash-html
2075   "u" gnus-article-unsplit-urls
2076   "s" gnus-summary-force-verify-and-decrypt
2077   "f" gnus-article-display-x-face
2078   "l" gnus-summary-stop-page-breaking
2079   "r" gnus-summary-caesar-message
2080   "m" gnus-summary-morse-message
2081   "t" gnus-summary-toggle-header
2082   "g" gnus-treat-smiley
2083   "v" gnus-summary-verbose-headers
2084   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2085   "p" gnus-article-verify-x-pgp-sig
2086   "d" gnus-article-treat-dumbquotes
2087   "i" gnus-summary-idna-message)
2088
2089 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2090   ;; mnemonic: deuglif*Y*
2091   "u" gnus-article-outlook-unwrap-lines
2092   "a" gnus-article-outlook-repair-attribution
2093   "c" gnus-article-outlook-rearrange-citation
2094   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2095
2096 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2097   "a" gnus-article-hide
2098   "h" gnus-article-hide-headers
2099   "b" gnus-article-hide-boring-headers
2100   "s" gnus-article-hide-signature
2101   "c" gnus-article-hide-citation
2102   "C" gnus-article-hide-citation-in-followups
2103   "l" gnus-article-hide-list-identifiers
2104   "B" gnus-article-strip-banner
2105   "P" gnus-article-hide-pem
2106   "\C-c" gnus-article-hide-citation-maybe)
2107
2108 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2109   "a" gnus-article-highlight
2110   "h" gnus-article-highlight-headers
2111   "c" gnus-article-highlight-citation
2112   "s" gnus-article-highlight-signature)
2113
2114 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2115   "f" gnus-article-treat-fold-headers
2116   "u" gnus-article-treat-unfold-headers
2117   "n" gnus-article-treat-fold-newsgroups)
2118
2119 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2120   "x" gnus-article-display-x-face
2121   "d" gnus-article-display-face
2122   "s" gnus-treat-smiley
2123   "D" gnus-article-remove-images
2124   "W" gnus-html-show-images
2125   "f" gnus-treat-from-picon
2126   "m" gnus-treat-mail-picon
2127   "n" gnus-treat-newsgroups-picon
2128   "g" gnus-treat-from-gravatar
2129   "h" gnus-treat-mail-gravatar)
2130
2131 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2132   "w" gnus-article-decode-mime-words
2133   "c" gnus-article-decode-charset
2134   "v" gnus-mime-view-all-parts
2135   "b" gnus-article-view-part)
2136
2137 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2138   "z" gnus-article-date-ut
2139   "u" gnus-article-date-ut
2140   "l" gnus-article-date-local
2141   "p" gnus-article-date-english
2142   "e" gnus-article-date-lapsed
2143   "o" gnus-article-date-original
2144   "i" gnus-article-date-iso8601
2145   "s" gnus-article-date-user)
2146
2147 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2148   "t" gnus-article-remove-trailing-blank-lines
2149   "l" gnus-article-strip-leading-blank-lines
2150   "m" gnus-article-strip-multiple-blank-lines
2151   "a" gnus-article-strip-blank-lines
2152   "A" gnus-article-strip-all-blank-lines
2153   "s" gnus-article-strip-leading-space
2154   "e" gnus-article-strip-trailing-space
2155   "w" gnus-article-remove-leading-whitespace)
2156
2157 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2158   "v" gnus-version
2159   "d" gnus-summary-describe-group
2160   "h" gnus-summary-describe-briefly
2161   "i" gnus-info-find-node
2162   "C" gnus-group-fetch-control)
2163
2164 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2165   "e" gnus-summary-expire-articles
2166   "\M-\C-e" gnus-summary-expire-articles-now
2167   "\177" gnus-summary-delete-article
2168   [delete] gnus-summary-delete-article
2169   [backspace] gnus-summary-delete-article
2170   "m" gnus-summary-move-article
2171   "r" gnus-summary-respool-article
2172   "w" gnus-summary-edit-article
2173   "c" gnus-summary-copy-article
2174   "B" gnus-summary-crosspost-article
2175   "q" gnus-summary-respool-query
2176   "t" gnus-summary-respool-trace
2177   "i" gnus-summary-import-article
2178   "I" gnus-summary-create-article
2179   "p" gnus-summary-article-posted-p)
2180
2181 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2182   "o" gnus-summary-save-article
2183   "m" gnus-summary-save-article-mail
2184   "F" gnus-summary-write-article-file
2185   "r" gnus-summary-save-article-rmail
2186   "f" gnus-summary-save-article-file
2187   "b" gnus-summary-save-article-body-file
2188   "B" gnus-summary-write-article-body-file
2189   "h" gnus-summary-save-article-folder
2190   "v" gnus-summary-save-article-vm
2191   "p" gnus-summary-pipe-output
2192   "P" gnus-summary-muttprint)
2193
2194 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2195   "b" gnus-summary-display-buttonized
2196   "m" gnus-summary-repair-multipart
2197   "v" gnus-article-view-part
2198   "o" gnus-article-save-part
2199   "O" gnus-article-save-part-and-strip
2200   "r" gnus-article-replace-part
2201   "d" gnus-article-delete-part
2202   "t" gnus-article-view-part-as-type
2203   "j" gnus-article-jump-to-part
2204   "c" gnus-article-copy-part
2205   "C" gnus-article-view-part-as-charset
2206   "e" gnus-article-view-part-externally
2207   "H" gnus-article-browse-html-article
2208   "E" gnus-article-encrypt-body
2209   "i" gnus-article-inline-part
2210   "|" gnus-article-pipe-part)
2211
2212 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2213   "p" gnus-summary-mark-as-processable
2214   "u" gnus-summary-unmark-as-processable
2215   "U" gnus-summary-unmark-all-processable
2216   "v" gnus-uu-mark-over
2217   "s" gnus-uu-mark-series
2218   "r" gnus-uu-mark-region
2219   "g" gnus-uu-unmark-region
2220   "R" gnus-uu-mark-by-regexp
2221   "G" gnus-uu-unmark-by-regexp
2222   "t" gnus-uu-mark-thread
2223   "T" gnus-uu-unmark-thread
2224   "a" gnus-uu-mark-all
2225   "b" gnus-uu-mark-buffer
2226   "S" gnus-uu-mark-sparse
2227   "k" gnus-summary-kill-process-mark
2228   "y" gnus-summary-yank-process-mark
2229   "w" gnus-summary-save-process-mark
2230   "i" gnus-uu-invert-processable)
2231
2232 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2233   ;;"x" gnus-uu-extract-any
2234   "m" gnus-summary-save-parts
2235   "u" gnus-uu-decode-uu
2236   "U" gnus-uu-decode-uu-and-save
2237   "s" gnus-uu-decode-unshar
2238   "S" gnus-uu-decode-unshar-and-save
2239   "o" gnus-uu-decode-save
2240   "O" gnus-uu-decode-save
2241   "b" gnus-uu-decode-binhex
2242   "B" gnus-uu-decode-binhex
2243   "Y" gnus-uu-decode-yenc
2244   "p" gnus-uu-decode-postscript
2245   "P" gnus-uu-decode-postscript-and-save)
2246
2247 (gnus-define-keys
2248     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2249   "u" gnus-uu-decode-uu-view
2250   "U" gnus-uu-decode-uu-and-save-view
2251   "s" gnus-uu-decode-unshar-view
2252   "S" gnus-uu-decode-unshar-and-save-view
2253   "o" gnus-uu-decode-save-view
2254   "O" gnus-uu-decode-save-view
2255   "b" gnus-uu-decode-binhex-view
2256   "B" gnus-uu-decode-binhex-view
2257   "p" gnus-uu-decode-postscript-view
2258   "P" gnus-uu-decode-postscript-and-save-view)
2259
2260 (defvar gnus-article-post-menu nil)
2261
2262 (defconst gnus-summary-menu-maxlen 20)
2263
2264 (defun gnus-summary-menu-split (menu)
2265   ;; If we have lots of elements, divide them into groups of 20
2266   ;; and make a pane (or submenu) for each one.
2267   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2268       (let ((menu menu) sublists next
2269             (i 1))
2270         (while menu
2271           ;; Pull off the next gnus-summary-menu-maxlen elements
2272           ;; and make them the next element of sublist.
2273           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2274           (if next
2275               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2276                       nil))
2277           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2278                                              (aref (car (last menu)) 0)) menu)
2279                                sublists))
2280           (setq i (1+ i))
2281           (setq menu next))
2282         (nreverse sublists))
2283     ;; Few elements--put them all in one pane.
2284     menu))
2285
2286 (defun gnus-summary-make-menu-bar ()
2287   (gnus-turn-off-edit-menu 'summary)
2288
2289   (unless (boundp 'gnus-summary-misc-menu)
2290
2291     (easy-menu-define
2292       gnus-summary-kill-menu gnus-summary-mode-map ""
2293       (cons
2294        "Score"
2295        (nconc
2296         (list
2297          ["Customize" gnus-score-customize t])
2298         (gnus-make-score-map 'increase)
2299         (gnus-make-score-map 'lower)
2300         '(("Mark"
2301            ["Kill below" gnus-summary-kill-below t]
2302            ["Mark above" gnus-summary-mark-above t]
2303            ["Tick above" gnus-summary-tick-above t]
2304            ["Clear above" gnus-summary-clear-above t])
2305           ["Current score" gnus-summary-current-score t]
2306           ["Set score" gnus-summary-set-score t]
2307           ["Switch current score file..." gnus-score-change-score-file t]
2308           ["Set mark below..." gnus-score-set-mark-below t]
2309           ["Set expunge below..." gnus-score-set-expunge-below t]
2310           ["Edit current score file" gnus-score-edit-current-scores t]
2311           ["Edit score file..." gnus-score-edit-file t]
2312           ["Trace score" gnus-score-find-trace t]
2313           ["Find words" gnus-score-find-favourite-words t]
2314           ["Rescore buffer" gnus-summary-rescore t]
2315           ["Increase score..." gnus-summary-increase-score t]
2316           ["Lower score..." gnus-summary-lower-score t]))))
2317
2318     ;; Define both the Article menu in the summary buffer and the
2319     ;; equivalent Commands menu in the article buffer here for
2320     ;; consistency.
2321     (let ((innards
2322            `(("Hide"
2323               ["All" gnus-article-hide t]
2324               ["Headers" gnus-article-hide-headers t]
2325               ["Signature" gnus-article-hide-signature t]
2326               ["Citation" gnus-article-hide-citation t]
2327               ["List identifiers" gnus-article-hide-list-identifiers t]
2328               ["Banner" gnus-article-strip-banner t]
2329               ["Boring headers" gnus-article-hide-boring-headers t])
2330              ("Highlight"
2331               ["All" gnus-article-highlight t]
2332               ["Headers" gnus-article-highlight-headers t]
2333               ["Signature" gnus-article-highlight-signature t]
2334               ["Citation" gnus-article-highlight-citation t])
2335              ("MIME"
2336               ["Words" gnus-article-decode-mime-words t]
2337               ["Charset" gnus-article-decode-charset t]
2338               ["QP" gnus-article-de-quoted-unreadable t]
2339               ["Base64" gnus-article-de-base64-unreadable t]
2340               ["View MIME buttons" gnus-summary-display-buttonized t]
2341               ["View all" gnus-mime-view-all-parts t]
2342               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2343               ["Encrypt body" gnus-article-encrypt-body
2344                :active (not (gnus-group-read-only-p))
2345                ,@(if (featurep 'xemacs) nil
2346                    '(:help "Encrypt the message body on disk"))]
2347               ["Extract all parts..." gnus-summary-save-parts t]
2348               ("Multipart"
2349                ["Repair multipart" gnus-summary-repair-multipart t]
2350                ["Pipe part..." gnus-article-pipe-part t]
2351                ["Inline part" gnus-article-inline-part t]
2352                ["View part as type..." gnus-article-view-part-as-type t]
2353                ["Encrypt body" gnus-article-encrypt-body
2354                 :active (not (gnus-group-read-only-p))
2355                ,@(if (featurep 'xemacs) nil
2356                    '(:help "Encrypt the message body on disk"))]
2357                ["View part externally" gnus-article-view-part-externally t]
2358                ["View HTML parts in browser" gnus-article-browse-html-article t]
2359                ["View part with charset..." gnus-article-view-part-as-charset t]
2360                ["Copy part" gnus-article-copy-part t]
2361                ["Save part..." gnus-article-save-part t]
2362                ["View part" gnus-article-view-part t]))
2363              ("Date"
2364               ["Local" gnus-article-date-local t]
2365               ["ISO8601" gnus-article-date-iso8601 t]
2366               ["UT" gnus-article-date-ut t]
2367               ["Original" gnus-article-date-original t]
2368               ["Lapsed" gnus-article-date-lapsed t]
2369               ["User-defined" gnus-article-date-user t])
2370              ("Display"
2371               ["Remove images" gnus-article-remove-images t]
2372               ["Toggle smiley" gnus-treat-smiley t]
2373               ["Show X-Face" gnus-article-display-x-face t]
2374               ["Show picons in From" gnus-treat-from-picon t]
2375               ["Show picons in mail headers" gnus-treat-mail-picon t]
2376               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2377               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2378               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2379               ("View as different encoding"
2380                ,@(gnus-summary-menu-split
2381                   (mapcar
2382                    (lambda (cs)
2383                      ;; Since easymenu under Emacs doesn't allow
2384                      ;; lambda forms for menu commands, we should
2385                      ;; provide intern'ed function symbols.
2386                      (let ((command (intern (format "\
2387 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2388                        (fset command
2389                              `(lambda ()
2390                                 (interactive)
2391                                 (let ((gnus-summary-show-article-charset-alist
2392                                        '((1 . ,cs))))
2393                                   (gnus-summary-show-article 1))))
2394                        `[,(symbol-name cs) ,command t]))
2395                    (sort (if (fboundp 'coding-system-list)
2396                              (coding-system-list)
2397                            (mapcar 'car mm-mime-mule-charset-alist))
2398                          'string<)))))
2399              ("Washing"
2400               ("Remove Blanks"
2401                ["Leading" gnus-article-strip-leading-blank-lines t]
2402                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2403                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2404                ["All of the above" gnus-article-strip-blank-lines t]
2405                ["All" gnus-article-strip-all-blank-lines t]
2406                ["Leading space" gnus-article-strip-leading-space t]
2407                ["Trailing space" gnus-article-strip-trailing-space t]
2408                ["Leading space in headers"
2409                 gnus-article-remove-leading-whitespace t])
2410               ["Overstrike" gnus-article-treat-overstrike t]
2411               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2412               ["Emphasis" gnus-article-emphasize t]
2413               ["Word wrap" gnus-article-fill-cited-article t]
2414               ["Fill long lines" gnus-article-fill-long-lines t]
2415               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2416               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2417               ["Remove CR" gnus-article-remove-cr t]
2418               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2419               ["Base64" gnus-article-de-base64-unreadable t]
2420               ["Rot 13" gnus-summary-caesar-message
2421                ,@(if (featurep 'xemacs) '(t)
2422                    '(:help "\"Caesar rotate\" article by 13"))]
2423               ["De-IDNA" gnus-summary-idna-message t]
2424               ["Morse decode" gnus-summary-morse-message t]
2425               ["Unix pipe..." gnus-summary-pipe-message t]
2426               ["Add buttons" gnus-article-add-buttons t]
2427               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2428               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2429               ["Verbose header" gnus-summary-verbose-headers t]
2430               ["Toggle header" gnus-summary-toggle-header t]
2431               ["Unfold headers" gnus-article-treat-unfold-headers t]
2432               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2433               ["Html" gnus-article-wash-html t]
2434               ["Unsplit URLs" gnus-article-unsplit-urls t]
2435               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2436               ["Decode HZ" gnus-article-decode-HZ t]
2437               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2438               ("(Outlook) Deuglify"
2439                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2440                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2441                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2442                ["Full (Outlook) deuglify"
2443                 gnus-article-outlook-deuglify-article t])
2444               )
2445              ("Output"
2446               ["Save in default format..." gnus-summary-save-article
2447                ,@(if (featurep 'xemacs) '(t)
2448                    '(:help "Save article using default method"))]
2449               ["Save in file..." gnus-summary-save-article-file
2450                ,@(if (featurep 'xemacs) '(t)
2451                    '(:help "Save article in file"))]
2452               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2453               ["Save in MH folder..." gnus-summary-save-article-folder t]
2454               ["Save in VM folder..." gnus-summary-save-article-vm t]
2455               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2456               ["Save body in file..." gnus-summary-save-article-body-file t]
2457               ["Pipe through a filter..." gnus-summary-pipe-output t]
2458               ["Print with Muttprint..." gnus-summary-muttprint t]
2459               ["Print" gnus-summary-print-article
2460                ,@(if (featurep 'xemacs) '(t)
2461                    '(:help "Generate and print a PostScript image"))])
2462              ("Copy, move,... (Backend)"
2463               ,@(if (featurep 'xemacs) nil
2464                   '(:help "Copying, moving, expiring articles..."))
2465               ["Respool article..." gnus-summary-respool-article t]
2466               ["Move article..." gnus-summary-move-article
2467                (gnus-check-backend-function
2468                 'request-move-article gnus-newsgroup-name)]
2469               ["Copy article..." gnus-summary-copy-article t]
2470               ["Crosspost article..." gnus-summary-crosspost-article
2471                (gnus-check-backend-function
2472                 'request-replace-article gnus-newsgroup-name)]
2473               ["Import file..." gnus-summary-import-article
2474                (gnus-check-backend-function
2475                 'request-accept-article gnus-newsgroup-name)]
2476               ["Create article..." gnus-summary-create-article
2477                (gnus-check-backend-function
2478                 'request-accept-article gnus-newsgroup-name)]
2479               ["Check if posted" gnus-summary-article-posted-p t]
2480               ["Edit article" gnus-summary-edit-article
2481                (not (gnus-group-read-only-p))]
2482               ["Delete article" gnus-summary-delete-article
2483                (gnus-check-backend-function
2484                 'request-expire-articles gnus-newsgroup-name)]
2485               ["Query respool" gnus-summary-respool-query t]
2486               ["Trace respool" gnus-summary-respool-trace t]
2487               ["Delete expirable articles" gnus-summary-expire-articles-now
2488                (gnus-check-backend-function
2489                 'request-expire-articles gnus-newsgroup-name)])
2490              ("Extract"
2491               ["Uudecode" gnus-uu-decode-uu
2492                ,@(if (featurep 'xemacs) '(t)
2493                    '(:help "Decode uuencoded article(s)"))]
2494               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2495               ["Unshar" gnus-uu-decode-unshar t]
2496               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2497               ["Save" gnus-uu-decode-save t]
2498               ["Binhex" gnus-uu-decode-binhex t]
2499               ["Postscript" gnus-uu-decode-postscript t]
2500               ["All MIME parts" gnus-summary-save-parts t])
2501              ("Cache"
2502               ["Enter article" gnus-cache-enter-article t]
2503               ["Remove article" gnus-cache-remove-article t])
2504              ["Translate" gnus-article-babel t]
2505              ["Select article buffer" gnus-summary-select-article-buffer t]
2506              ["Make article buffer sticky" gnus-sticky-article t]
2507              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2508              ["Isearch article..." gnus-summary-isearch-article t]
2509              ["Beginning of the article" gnus-summary-beginning-of-article t]
2510              ["End of the article" gnus-summary-end-of-article t]
2511              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2512              ["Fetch referenced articles" gnus-summary-refer-references t]
2513              ["Fetch current thread" gnus-summary-refer-thread t]
2514              ["Fetch article with id..." gnus-summary-refer-article t]
2515              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2516              ["Redisplay" gnus-summary-show-article t]
2517              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2518       (easy-menu-define
2519         gnus-summary-article-menu gnus-summary-mode-map ""
2520         (cons "Article" innards))
2521
2522       (if (not (keymapp gnus-summary-article-menu))
2523           (easy-menu-define
2524             gnus-article-commands-menu gnus-article-mode-map ""
2525             (cons "Commands" innards))
2526         ;; in Emacs, don't share menu.
2527         (setq gnus-article-commands-menu
2528               (copy-keymap gnus-summary-article-menu))
2529         (define-key gnus-article-mode-map [menu-bar commands]
2530           (cons "Commands" gnus-article-commands-menu))))
2531
2532     (easy-menu-define
2533       gnus-summary-thread-menu gnus-summary-mode-map ""
2534       '("Threads"
2535         ["Find all messages in thread" gnus-summary-refer-thread t]
2536         ["Toggle threading" gnus-summary-toggle-threads t]
2537         ["Hide threads" gnus-summary-hide-all-threads t]
2538         ["Show threads" gnus-summary-show-all-threads t]
2539         ["Hide thread" gnus-summary-hide-thread t]
2540         ["Show thread" gnus-summary-show-thread t]
2541         ["Go to next thread" gnus-summary-next-thread t]
2542         ["Go to previous thread" gnus-summary-prev-thread t]
2543         ["Go down thread" gnus-summary-down-thread t]
2544         ["Go up thread" gnus-summary-up-thread t]
2545         ["Top of thread" gnus-summary-top-thread t]
2546         ["Mark thread as read" gnus-summary-kill-thread t]
2547         ["Mark thread as expired" gnus-summary-expire-thread t]
2548         ["Lower thread score" gnus-summary-lower-thread t]
2549         ["Raise thread score" gnus-summary-raise-thread t]
2550         ["Rethread current" gnus-summary-rethread-current t]))
2551
2552     (easy-menu-define
2553       gnus-summary-post-menu gnus-summary-mode-map ""
2554       `("Post"
2555         ["Send a message (mail or news)" gnus-summary-post-news
2556          ,@(if (featurep 'xemacs) '(t)
2557              '(:help "Compose a new message (mail or news)"))]
2558         ["Followup" gnus-summary-followup
2559          ,@(if (featurep 'xemacs) '(t)
2560              '(:help "Post followup to this article"))]
2561         ["Followup and yank" gnus-summary-followup-with-original
2562          ,@(if (featurep 'xemacs) '(t)
2563              '(:help "Post followup to this article, quoting its contents"))]
2564         ["Supersede article" gnus-summary-supersede-article t]
2565         ["Cancel article" gnus-summary-cancel-article
2566          ,@(if (featurep 'xemacs) '(t)
2567              '(:help "Cancel an article you posted"))]
2568         ["Reply" gnus-summary-reply t]
2569         ["Reply and yank" gnus-summary-reply-with-original t]
2570         ["Wide reply" gnus-summary-wide-reply t]
2571         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2572          ,@(if (featurep 'xemacs) '(t)
2573              '(:help "Mail a reply, quoting this article"))]
2574         ["Very wide reply" gnus-summary-very-wide-reply t]
2575         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2576          ,@(if (featurep 'xemacs) '(t)
2577              '(:help "Mail a very wide reply, quoting this article"))]
2578         ["Mail forward" gnus-summary-mail-forward t]
2579         ["Post forward" gnus-summary-post-forward t]
2580         ["Digest and mail" gnus-uu-digest-mail-forward t]
2581         ["Digest and post" gnus-uu-digest-post-forward t]
2582         ["Resend message" gnus-summary-resend-message t]
2583         ["Resend message edit" gnus-summary-resend-message-edit t]
2584         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2585         ["Send a mail" gnus-summary-mail-other-window t]
2586         ["Create a local message" gnus-summary-news-other-window t]
2587         ["Uuencode and post" gnus-uu-post-news
2588          ,@(if (featurep 'xemacs) '(t)
2589              '(:help "Post a uuencoded article"))]
2590         ["Followup via news" gnus-summary-followup-to-mail t]
2591         ["Followup via news and yank"
2592          gnus-summary-followup-to-mail-with-original t]
2593         ["Strip signature on reply"
2594          (lambda ()
2595            (interactive)
2596            (if (not (memq message-cite-function
2597                           '(message-cite-original-without-signature
2598                             message-cite-original)))
2599                ;; Stupid workaround for XEmacs not honoring :visible.
2600                (message "Can't toggle this value of `message-cite-function'")
2601              (setq message-cite-function
2602                    (if (eq message-cite-function
2603                            'message-cite-original-without-signature)
2604                        'message-cite-original
2605                      'message-cite-original-without-signature))))
2606          ;; XEmacs barfs on :visible.
2607          ,@(if (featurep 'xemacs) nil
2608              '(:visible (memq message-cite-function
2609                               '(message-cite-original-without-signature
2610                                 message-cite-original))))
2611          :style toggle
2612          :selected (eq message-cite-function
2613                        'message-cite-original-without-signature)
2614          ,@(if (featurep 'xemacs) nil
2615              '(:help "Strip signature from cited article when replying."))]
2616         ;;("Draft"
2617         ;;["Send" gnus-summary-send-draft t]
2618         ;;["Send bounced" gnus-resend-bounced-mail t])
2619         ))
2620
2621     (cond
2622      ((not (keymapp gnus-summary-post-menu))
2623       (setq gnus-article-post-menu gnus-summary-post-menu))
2624      ((not gnus-article-post-menu)
2625       ;; Don't share post menu.
2626       (setq gnus-article-post-menu
2627             (copy-keymap gnus-summary-post-menu))))
2628     (define-key gnus-article-mode-map [menu-bar post]
2629       (cons "Post" gnus-article-post-menu))
2630
2631     (easy-menu-define
2632       gnus-summary-misc-menu gnus-summary-mode-map ""
2633       `("Gnus"
2634         ("Mark Read"
2635          ["Mark as read" gnus-summary-mark-as-read-forward t]
2636          ["Mark same subject and select"
2637           gnus-summary-kill-same-subject-and-select t]
2638          ["Mark same subject" gnus-summary-kill-same-subject t]
2639          ["Catchup" gnus-summary-catchup
2640           ,@(if (featurep 'xemacs) '(t)
2641               '(:help "Mark unread articles in this group as read"))]
2642          ["Catchup all" gnus-summary-catchup-all t]
2643          ["Catchup to here" gnus-summary-catchup-to-here t]
2644          ["Catchup from here" gnus-summary-catchup-from-here t]
2645          ["Catchup region" gnus-summary-mark-region-as-read
2646           (gnus-mark-active-p)]
2647          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2648         ("Mark Various"
2649          ["Tick" gnus-summary-tick-article-forward t]
2650          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2651          ["Remove marks" gnus-summary-clear-mark-forward t]
2652          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2653          ["Set bookmark" gnus-summary-set-bookmark t]
2654          ["Remove bookmark" gnus-summary-remove-bookmark t])
2655         ("Limit to"
2656          ["Marks..." gnus-summary-limit-to-marks t]
2657          ["Subject..." gnus-summary-limit-to-subject t]
2658          ["Author..." gnus-summary-limit-to-author t]
2659          ["Recipient..." gnus-summary-limit-to-recipient t]
2660          ["Address..." gnus-summary-limit-to-address t]
2661          ["Age..." gnus-summary-limit-to-age t]
2662          ["Extra..." gnus-summary-limit-to-extra t]
2663          ["Score..." gnus-summary-limit-to-score t]
2664          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2665          ["Unread" gnus-summary-limit-to-unread t]
2666          ["Unseen" gnus-summary-limit-to-unseen t]
2667          ["Singletons" gnus-summary-limit-to-singletons t]
2668          ["Replied" gnus-summary-limit-to-replied t]
2669          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2670          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2671          ["Pop limit" gnus-summary-pop-limit t]
2672          ["Show dormant" gnus-summary-limit-include-dormant t]
2673          ["Hide childless dormant"
2674           gnus-summary-limit-exclude-childless-dormant t]
2675          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2676          ["Hide marked" gnus-summary-limit-exclude-marks t]
2677          ["Show expunged" gnus-summary-limit-include-expunged t])
2678         ("Process Mark"
2679          ["Set mark" gnus-summary-mark-as-processable t]
2680          ["Remove mark" gnus-summary-unmark-as-processable t]
2681          ["Remove all marks" gnus-summary-unmark-all-processable t]
2682          ["Invert marks" gnus-uu-invert-processable t]
2683          ["Mark above" gnus-uu-mark-over t]
2684          ["Mark series" gnus-uu-mark-series t]
2685          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2686          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2687          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2688          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2689          ["Mark all" gnus-uu-mark-all t]
2690          ["Mark buffer" gnus-uu-mark-buffer t]
2691          ["Mark sparse" gnus-uu-mark-sparse t]
2692          ["Mark thread" gnus-uu-mark-thread t]
2693          ["Unmark thread" gnus-uu-unmark-thread t]
2694          ("Process Mark Sets"
2695           ["Kill" gnus-summary-kill-process-mark t]
2696           ["Yank" gnus-summary-yank-process-mark
2697            gnus-newsgroup-process-stack]
2698           ["Save" gnus-summary-save-process-mark t]
2699           ["Run command on marked..." gnus-summary-universal-argument t]))
2700         ("Registry Marks")
2701         ("Scroll article"
2702          ["Page forward" gnus-summary-next-page
2703           ,@(if (featurep 'xemacs) '(t)
2704               '(:help "Show next page of article"))]
2705          ["Page backward" gnus-summary-prev-page
2706           ,@(if (featurep 'xemacs) '(t)
2707               '(:help "Show previous page of article"))]
2708          ["Line forward" gnus-summary-scroll-up t])
2709         ("Move"
2710          ["Next unread article" gnus-summary-next-unread-article t]
2711          ["Previous unread article" gnus-summary-prev-unread-article t]
2712          ["Next article" gnus-summary-next-article t]
2713          ["Previous article" gnus-summary-prev-article t]
2714          ["Next unread subject" gnus-summary-next-unread-subject t]
2715          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2716          ["Next article same subject" gnus-summary-next-same-subject t]
2717          ["Previous article same subject" gnus-summary-prev-same-subject t]
2718          ["First unread article" gnus-summary-first-unread-article t]
2719          ["Best unread article" gnus-summary-best-unread-article t]
2720          ["Go to subject number..." gnus-summary-goto-subject t]
2721          ["Go to article number..." gnus-summary-goto-article t]
2722          ["Go to the last article" gnus-summary-goto-last-article t]
2723          ["Pop article off history" gnus-summary-pop-article t])
2724         ("Sort"
2725          ["Sort by number" gnus-summary-sort-by-number t]
2726          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2727          ["Sort by author" gnus-summary-sort-by-author t]
2728          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2729          ["Sort by subject" gnus-summary-sort-by-subject t]
2730          ["Sort by date" gnus-summary-sort-by-date t]
2731          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2732          ["Sort by score" gnus-summary-sort-by-score t]
2733          ["Sort by lines" gnus-summary-sort-by-lines t]
2734          ["Sort by characters" gnus-summary-sort-by-chars t]
2735          ["Randomize" gnus-summary-sort-by-random t]
2736          ["Original sort" gnus-summary-sort-by-original t])
2737         ("Help"
2738          ["Describe group" gnus-summary-describe-group t]
2739          ["Fetch control message" gnus-group-fetch-control
2740           ,@(if (featurep 'xemacs) nil
2741               '(:help "Display the archived control message for the current group"))]
2742          ["Read manual" gnus-info-find-node t])
2743         ("Modes"
2744          ["Pick and read" gnus-pick-mode t]
2745          ["Binary" gnus-binary-mode t])
2746         ("Regeneration"
2747          ["Regenerate" gnus-summary-prepare t]
2748          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2749          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2750          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2751          ["Toggle threading" gnus-summary-toggle-threads t])
2752         ["See old articles" gnus-summary-insert-old-articles t]
2753         ["See new articles" gnus-summary-insert-new-articles t]
2754         ["Filter articles..." gnus-summary-execute-command t]
2755         ["Run command on articles..." gnus-summary-universal-argument t]
2756         ["Search articles forward..." gnus-summary-search-article-forward t]
2757         ["Search articles backward..." gnus-summary-search-article-backward t]
2758         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2759         ["Expand window" gnus-summary-expand-window t]
2760         ["Expire expirable articles" gnus-summary-expire-articles
2761          (gnus-check-backend-function
2762           'request-expire-articles gnus-newsgroup-name)]
2763         ["Edit local kill file" gnus-summary-edit-local-kill t]
2764         ["Edit main kill file" gnus-summary-edit-global-kill t]
2765         ["Edit group parameters" gnus-summary-edit-parameters t]
2766         ["Customize group parameters" gnus-summary-customize-parameters t]
2767         ["Send a bug report" gnus-bug t]
2768         ("Exit"
2769          ["Catchup and exit" gnus-summary-catchup-and-exit
2770           ,@(if (featurep 'xemacs) '(t)
2771               '(:help "Mark unread articles in this group as read, then exit"))]
2772          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2773          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2774          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2775          ["Exit group" gnus-summary-exit
2776           ,@(if (featurep 'xemacs) '(t)
2777               '(:help "Exit current group, return to group selection mode"))]
2778          ["Exit group without updating" gnus-summary-exit-no-update t]
2779          ["Exit and goto next group" gnus-summary-next-group t]
2780          ["Exit and goto prev group" gnus-summary-prev-group t]
2781          ["Reselect group" gnus-summary-reselect-current-group t]
2782          ["Rescan group" gnus-summary-rescan-group t]
2783          ["Update dribble" gnus-summary-save-newsrc t])))
2784
2785     (gnus-run-hooks 'gnus-summary-menu-hook)))
2786
2787 (defvar gnus-summary-tool-bar-map nil)
2788
2789 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2790 ;; affect _new_ message buffers.  We might add a function that walks thru all
2791 ;; summary-mode buffers and force the update.
2792 (defun gnus-summary-tool-bar-update (&optional symbol value)
2793   "Update summary mode toolbar.
2794 Setter function for custom variables."
2795   (setq-default gnus-summary-tool-bar-map nil)
2796   (when symbol
2797     ;; When used as ":set" function:
2798     (set-default symbol value))
2799   (when (gnus-buffer-live-p gnus-summary-buffer)
2800     (with-current-buffer gnus-summary-buffer
2801       (gnus-summary-make-tool-bar))))
2802
2803 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2804                                      'gnus-summary-tool-bar-gnome
2805                                    'gnus-summary-tool-bar-retro)
2806   "Specifies the Gnus summary tool bar.
2807
2808 It can be either a list or a symbol refering to a list.  See
2809 `gmm-tool-bar-from-list' for the format of the list.  The
2810 default key map is `gnus-summary-mode-map'.
2811
2812 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2813 `gnus-summary-tool-bar-retro'."
2814   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2815                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2816                  (repeat :tag "User defined list" gmm-tool-bar-item)
2817                  (symbol))
2818   :version "23.1" ;; No Gnus
2819   :initialize 'custom-initialize-default
2820   :set 'gnus-summary-tool-bar-update
2821   :group 'gnus-summary)
2822
2823 (defcustom gnus-summary-tool-bar-gnome
2824   '((gnus-summary-post-news "mail/compose" nil)
2825     (gnus-summary-insert-new-articles "mail/inbox" nil
2826                                       :visible (or (not gnus-agent)
2827                                                    gnus-plugged))
2828     (gnus-summary-reply-with-original "mail/reply")
2829     (gnus-summary-reply "mail/reply" nil :visible nil)
2830     (gnus-summary-followup-with-original "mail/reply-all")
2831     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2832     (gnus-summary-mail-forward "mail/forward")
2833     (gnus-summary-save-article "mail/save")
2834     (gnus-summary-search-article-forward "search" nil :visible nil)
2835     (gnus-summary-print-article "print")
2836     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2837     ;; Some new commands that may need more suitable icons:
2838     (gnus-summary-save-newsrc "save" nil :visible nil)
2839     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2840     (gnus-summary-prev-article "left-arrow")
2841     (gnus-summary-next-article "right-arrow")
2842     (gnus-summary-next-page "next-page")
2843     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2844     ;;
2845     ;; Maybe some sort-by-... could be added:
2846     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2847     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2848     (gnus-summary-mark-as-expirable
2849      "delete" nil
2850      :visible (gnus-check-backend-function 'request-expire-articles
2851                                            gnus-newsgroup-name))
2852     (gnus-summary-mark-as-spam
2853      "mail/spam" t
2854      :visible (and (fboundp 'spam-group-ham-contents-p)
2855                    (spam-group-ham-contents-p gnus-newsgroup-name))
2856      :help "Mark as spam")
2857     (gnus-summary-mark-as-read-forward
2858      "mail/not-spam" nil
2859      :visible (and (fboundp 'spam-group-spam-contents-p)
2860                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2861     ;;
2862     (gnus-summary-exit "exit")
2863     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2864     (gnus-info-find-node "help"))
2865   "List of functions for the summary tool bar (GNOME style).
2866
2867 See `gmm-tool-bar-from-list' for the format of the list."
2868   :type '(repeat gmm-tool-bar-item)
2869   :version "23.1" ;; No Gnus
2870   :initialize 'custom-initialize-default
2871   :set 'gnus-summary-tool-bar-update
2872   :group 'gnus-summary)
2873
2874 (defcustom gnus-summary-tool-bar-retro
2875   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2876     (gnus-summary-next-unread-article "gnus/next-ur")
2877     (gnus-summary-post-news "gnus/post")
2878     (gnus-summary-followup-with-original "gnus/fuwo")
2879     (gnus-summary-followup "gnus/followup")
2880     (gnus-summary-reply-with-original "gnus/reply-wo")
2881     (gnus-summary-reply "gnus/reply")
2882     (gnus-summary-caesar-message "gnus/rot13")
2883     (gnus-uu-decode-uu "gnus/uu-decode")
2884     (gnus-summary-save-article-file "gnus/save-aif")
2885     (gnus-summary-save-article "gnus/save-art")
2886     (gnus-uu-post-news "gnus/uu-post")
2887     (gnus-summary-catchup "gnus/catchup")
2888     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2889     (gnus-summary-exit "gnus/exit-summ")
2890     ;; Some new command that may need more suitable icons:
2891     (gnus-summary-print-article "gnus/print" nil :visible nil)
2892     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2893     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2894     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2895     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2896     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2897     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2898     ;;
2899     (gnus-info-find-node "gnus/help" nil :visible nil))
2900   "List of functions for the summary tool bar (retro look).
2901
2902 See `gmm-tool-bar-from-list' for the format of the list."
2903   :type '(repeat gmm-tool-bar-item)
2904   :version "23.1" ;; No Gnus
2905   :initialize 'custom-initialize-default
2906   :set 'gnus-summary-tool-bar-update
2907   :group 'gnus-summary)
2908
2909 (defcustom gnus-summary-tool-bar-zap-list t
2910   "List of icon items from the global tool bar.
2911 These items are not displayed in the Gnus summary mode tool bar.
2912
2913 See `gmm-tool-bar-from-list' for the format of the list."
2914   :type 'gmm-tool-bar-zap-list
2915   :version "23.1" ;; No Gnus
2916   :initialize 'custom-initialize-default
2917   :set 'gnus-summary-tool-bar-update
2918   :group 'gnus-summary)
2919
2920 (defvar image-load-path)
2921 (defvar tool-bar-map)
2922
2923 (defun gnus-summary-make-tool-bar (&optional force)
2924   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2925 When FORCE, rebuild the tool bar."
2926   (when (and (not (featurep 'xemacs))
2927              (boundp 'tool-bar-mode)
2928              tool-bar-mode
2929              (or (not gnus-summary-tool-bar-map) force))
2930     (let* ((load-path
2931             (gmm-image-load-path-for-library "gnus"
2932                                              "mail/save.xpm"
2933                                              nil t))
2934            (image-load-path (cons (car load-path)
2935                                   (when (boundp 'image-load-path)
2936                                     image-load-path)))
2937            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2938                                         gnus-summary-tool-bar-zap-list
2939                                         'gnus-summary-mode-map)))
2940       (when map
2941         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2942         ;; uses it's value.
2943         (setq gnus-summary-tool-bar-map map))))
2944   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2945
2946 (defun gnus-score-set-default (var value)
2947   "A version of set that updates the GNU Emacs menu-bar."
2948   (set var value)
2949   ;; It is the message that forces the active status to be updated.
2950   (message ""))
2951
2952 (defun gnus-make-score-map (type)
2953   "Make a summary score map of type TYPE."
2954   (if t
2955       nil
2956     (let ((headers '(("author" "from" string)
2957                      ("subject" "subject" string)
2958                      ("article body" "body" string)
2959                      ("article head" "head" string)
2960                      ("xref" "xref" string)
2961                      ("extra header" "extra" string)
2962                      ("lines" "lines" number)
2963                      ("followups to author" "followup" string)))
2964           (types '((number ("less than" <)
2965                            ("greater than" >)
2966                            ("equal" =))
2967                    (string ("substring" s)
2968                            ("exact string" e)
2969                            ("fuzzy string" f)
2970                            ("regexp" r))))
2971           (perms '(("temporary" (current-time-string))
2972                    ("permanent" nil)
2973                    ("immediate" now)))
2974           header)
2975       (list
2976        (apply
2977         'nconc
2978         (list
2979          (if (eq type 'lower)
2980              "Lower score"
2981            "Increase score"))
2982         (let (outh)
2983           (while headers
2984             (setq header (car headers))
2985             (setq outh
2986                   (cons
2987                    (apply
2988                     'nconc
2989                     (list (car header))
2990                     (let ((ts (cdr (assoc (nth 2 header) types)))
2991                           outt)
2992                       (while ts
2993                         (setq outt
2994                               (cons
2995                                (apply
2996                                 'nconc
2997                                 (list (caar ts))
2998                                 (let ((ps perms)
2999                                       outp)
3000                                   (while ps
3001                                     (setq outp
3002                                           (cons
3003                                            (vector
3004                                             (caar ps)
3005                                             (list
3006                                              'gnus-summary-score-entry
3007                                              (nth 1 header)
3008                                              (if (or (string= (nth 1 header)
3009                                                               "head")
3010                                                      (string= (nth 1 header)
3011                                                               "body"))
3012                                                  ""
3013                                                (list 'gnus-summary-header
3014                                                      (nth 1 header)))
3015                                              (list 'quote (nth 1 (car ts)))
3016                                              (list 'gnus-score-delta-default
3017                                                    nil)
3018                                              (nth 1 (car ps))
3019                                              t)
3020                                             t)
3021                                            outp))
3022                                     (setq ps (cdr ps)))
3023                                   (list (nreverse outp))))
3024                                outt))
3025                         (setq ts (cdr ts)))
3026                       (list (nreverse outt))))
3027                    outh))
3028             (setq headers (cdr headers)))
3029           (list (nreverse outh))))))))
3030
3031
3032 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3033 (defvar bookmark-make-record-function)
3034 \f
3035
3036 (defun gnus-summary-mode (&optional group)
3037   "Major mode for reading articles.
3038
3039 All normal editing commands are switched off.
3040 \\<gnus-summary-mode-map>
3041 Each line in this buffer represents one article.  To read an
3042 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3043 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3044 respectively.
3045
3046 You can also post articles and send mail from this buffer.  To
3047 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3048 of an article, type `\\[gnus-summary-reply]'.
3049
3050 There are approx. one gazillion commands you can execute in this
3051 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3052
3053 The following commands are available:
3054
3055 \\{gnus-summary-mode-map}"
3056   (interactive)
3057   (kill-all-local-variables)
3058   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3059     (gnus-summary-make-local-variables))
3060   (gnus-summary-make-local-variables)
3061   (setq gnus-newsgroup-name group)
3062   (when (gnus-visual-p 'summary-menu 'menu)
3063     (gnus-summary-make-menu-bar)
3064     (gnus-summary-make-tool-bar))
3065   (gnus-make-thread-indent-array)
3066   (gnus-simplify-mode-line)
3067   (setq major-mode 'gnus-summary-mode)
3068   (setq mode-name "Summary")
3069   (use-local-map gnus-summary-mode-map)
3070   (buffer-disable-undo)
3071   (setq buffer-read-only t              ;Disable modification
3072         show-trailing-whitespace nil)
3073   (setq truncate-lines t)
3074   (add-to-invisibility-spec '(gnus-sum . t))
3075   (gnus-summary-set-display-table)
3076   (gnus-set-default-directory)
3077   (make-local-variable 'gnus-summary-line-format)
3078   (make-local-variable 'gnus-summary-line-format-spec)
3079   (make-local-variable 'gnus-summary-dummy-line-format)
3080   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3081   (make-local-variable 'gnus-summary-mark-positions)
3082   (gnus-make-local-hook 'pre-command-hook)
3083   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3084   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3085   (turn-on-gnus-mailing-list-mode)
3086   (mm-enable-multibyte)
3087   (set (make-local-variable 'bookmark-make-record-function)
3088        'gnus-summary-bookmark-make-record)
3089   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3090   (gnus-update-summary-mark-positions))
3091
3092 (defun gnus-summary-make-local-variables ()
3093   "Make all the local summary buffer variables."
3094   (let (global)
3095     (dolist (local gnus-summary-local-variables)
3096       (if (consp local)
3097           (progn
3098             (if (eq (cdr local) 'global)
3099                 ;; Copy the global value of the variable.
3100                 (setq global (symbol-value (car local)))
3101               ;; Use the value from the list.
3102               (setq global (eval (cdr local))))
3103             (set (make-local-variable (car local)) global))
3104         ;; Simple nil-valued local variable.
3105         (set (make-local-variable local) nil)))))
3106
3107 (defun gnus-summary-clear-local-variables ()
3108   (let ((locals gnus-summary-local-variables))
3109     (while locals
3110       (if (consp (car locals))
3111           (and (symbolp (caar locals))
3112                (set (caar locals) nil))
3113         (and (symbolp (car locals))
3114              (set (car locals) nil)))
3115       (setq locals (cdr locals)))))
3116
3117 ;; Summary data functions.
3118
3119 (defmacro gnus-data-number (data)
3120   `(car ,data))
3121
3122 (defmacro gnus-data-set-number (data number)
3123   `(setcar ,data ,number))
3124
3125 (defmacro gnus-data-mark (data)
3126   `(nth 1 ,data))
3127
3128 (defmacro gnus-data-set-mark (data mark)
3129   `(setcar (nthcdr 1 ,data) ,mark))
3130
3131 (defmacro gnus-data-pos (data)
3132   `(nth 2 ,data))
3133
3134 (defmacro gnus-data-set-pos (data pos)
3135   `(setcar (nthcdr 2 ,data) ,pos))
3136
3137 (defmacro gnus-data-header (data)
3138   `(nth 3 ,data))
3139
3140 (defmacro gnus-data-set-header (data header)
3141   `(setf (nth 3 ,data) ,header))
3142
3143 (defmacro gnus-data-level (data)
3144   `(nth 4 ,data))
3145
3146 (defmacro gnus-data-unread-p (data)
3147   `(= (nth 1 ,data) gnus-unread-mark))
3148
3149 (defmacro gnus-data-read-p (data)
3150   `(/= (nth 1 ,data) gnus-unread-mark))
3151
3152 (defmacro gnus-data-pseudo-p (data)
3153   `(consp (nth 3 ,data)))
3154
3155 (defmacro gnus-data-find (number)
3156   `(assq ,number gnus-newsgroup-data))
3157
3158 (defmacro gnus-data-find-list (number &optional data)
3159   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3160      (memq (assq ,number bdata)
3161            bdata)))
3162
3163 (defmacro gnus-data-make (number mark pos header level)
3164   `(list ,number ,mark ,pos ,header ,level))
3165
3166 (defun gnus-data-enter (after-article number mark pos header level offset)
3167   (let ((data (gnus-data-find-list after-article)))
3168     (unless data
3169       (error "No such article: %d" after-article))
3170     (setcdr data (cons (gnus-data-make number mark pos header level)
3171                        (cdr data)))
3172     (setq gnus-newsgroup-data-reverse nil)
3173     (gnus-data-update-list (cddr data) offset)))
3174
3175 (defun gnus-data-enter-list (after-article list &optional offset)
3176   (when list
3177     (let ((data (and after-article (gnus-data-find-list after-article)))
3178           (ilist list))
3179       (if (not (or data
3180                    after-article))
3181           (let ((odata gnus-newsgroup-data))
3182             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3183             (when offset
3184               (gnus-data-update-list odata offset)))
3185         ;; Find the last element in the list to be spliced into the main
3186         ;; list.
3187         (setq list (last list))
3188         (if (not data)
3189             (progn
3190               (setcdr list gnus-newsgroup-data)
3191               (setq gnus-newsgroup-data ilist)
3192               (when offset
3193                 (gnus-data-update-list (cdr list) offset)))
3194           (setcdr list (cdr data))
3195           (setcdr data ilist)
3196           (when offset
3197             (gnus-data-update-list (cdr list) offset))))
3198       (setq gnus-newsgroup-data-reverse nil))))
3199
3200 (defun gnus-data-remove (article &optional offset)
3201   (let ((data gnus-newsgroup-data))
3202     (if (= (gnus-data-number (car data)) article)
3203         (progn
3204           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3205                 gnus-newsgroup-data-reverse nil)
3206           (when offset
3207             (gnus-data-update-list gnus-newsgroup-data offset)))
3208       (while (cdr data)
3209         (when (= (gnus-data-number (cadr data)) article)
3210           (setcdr data (cddr data))
3211           (when offset
3212             (gnus-data-update-list (cdr data) offset))
3213           (setq data nil
3214                 gnus-newsgroup-data-reverse nil))
3215         (setq data (cdr data))))))
3216
3217 (defmacro gnus-data-list (backward)
3218   `(if ,backward
3219        (or gnus-newsgroup-data-reverse
3220            (setq gnus-newsgroup-data-reverse
3221                  (reverse gnus-newsgroup-data)))
3222      gnus-newsgroup-data))
3223
3224 (defun gnus-data-update-list (data offset)
3225   "Add OFFSET to the POS of all data entries in DATA."
3226   (setq gnus-newsgroup-data-reverse nil)
3227   (while data
3228     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3229     (setq data (cdr data))))
3230
3231 (defun gnus-summary-article-pseudo-p (article)
3232   "Say whether this article is a pseudo article or not."
3233   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3234
3235 (defmacro gnus-summary-article-sparse-p (article)
3236   "Say whether this article is a sparse article or not."
3237   `(memq ,article gnus-newsgroup-sparse))
3238
3239 (defmacro gnus-summary-article-ancient-p (article)
3240   "Say whether this article is a sparse article or not."
3241   `(memq ,article gnus-newsgroup-ancient))
3242
3243 (defun gnus-article-parent-p (number)
3244   "Say whether this article is a parent or not."
3245   (let ((data (gnus-data-find-list number)))
3246     (and (cdr data)              ; There has to be an article after...
3247          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3248             (gnus-data-level (nth 1 data))))))
3249
3250 (defun gnus-article-children (number)
3251   "Return a list of all children to NUMBER."
3252   (let* ((data (gnus-data-find-list number))
3253          (level (gnus-data-level (car data)))
3254          children)
3255     (setq data (cdr data))
3256     (while (and data
3257                 (= (gnus-data-level (car data)) (1+ level)))
3258       (push (gnus-data-number (car data)) children)
3259       (setq data (cdr data)))
3260     children))
3261
3262 (defmacro gnus-summary-skip-intangible ()
3263   "If the current article is intangible, then jump to a different article."
3264   '(let ((to (get-text-property (point) 'gnus-intangible)))
3265      (and to (gnus-summary-goto-subject to))))
3266
3267 (defmacro gnus-summary-article-intangible-p ()
3268   "Say whether this article is intangible or not."
3269   '(get-text-property (point) 'gnus-intangible))
3270
3271 (defun gnus-article-read-p (article)
3272   "Say whether ARTICLE is read or not."
3273   (not (or (memq article gnus-newsgroup-marked)
3274            (memq article gnus-newsgroup-spam-marked)
3275            (memq article gnus-newsgroup-unreads)
3276            (memq article gnus-newsgroup-unselected)
3277            (memq article gnus-newsgroup-dormant))))
3278
3279 ;; Some summary mode macros.
3280
3281 (defmacro gnus-summary-article-number ()
3282   "The article number of the article on the current line.
3283 If there isn't an article number here, then we return the current
3284 article number."
3285   '(progn
3286      (gnus-summary-skip-intangible)
3287      (or (get-text-property (point) 'gnus-number)
3288          (gnus-summary-last-subject))))
3289
3290 (defmacro gnus-summary-article-header (&optional number)
3291   "Return the header of article NUMBER."
3292   `(gnus-data-header (gnus-data-find
3293                       ,(or number '(gnus-summary-article-number)))))
3294
3295 (defmacro gnus-summary-thread-level (&optional number)
3296   "Return the level of thread that starts with article NUMBER."
3297   `(if (and (eq gnus-summary-make-false-root 'dummy)
3298             (get-text-property (point) 'gnus-intangible))
3299        0
3300      (gnus-data-level (gnus-data-find
3301                        ,(or number '(gnus-summary-article-number))))))
3302
3303 (defmacro gnus-summary-article-mark (&optional number)
3304   "Return the mark of article NUMBER."
3305   `(gnus-data-mark (gnus-data-find
3306                     ,(or number '(gnus-summary-article-number)))))
3307
3308 (defmacro gnus-summary-article-pos (&optional number)
3309   "Return the position of the line of article NUMBER."
3310   `(gnus-data-pos (gnus-data-find
3311                    ,(or number '(gnus-summary-article-number)))))
3312
3313 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3314 (defmacro gnus-summary-article-subject (&optional number)
3315   "Return current subject string or nil if nothing."
3316   `(let ((headers
3317           ,(if number
3318                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3319              '(gnus-data-header (assq (gnus-summary-article-number)
3320                                       gnus-newsgroup-data)))))
3321      (and headers
3322           (vectorp headers)
3323           (mail-header-subject headers))))
3324
3325 (defmacro gnus-summary-article-score (&optional number)
3326   "Return current article score."
3327   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3328                   gnus-newsgroup-scored))
3329        gnus-summary-default-score 0))
3330
3331 (defun gnus-summary-article-children (&optional number)
3332   "Return a list of article numbers that are children of article NUMBER."
3333   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3334          (level (gnus-data-level (car data)))
3335          l children)
3336     (while (and (setq data (cdr data))
3337                 (> (setq l (gnus-data-level (car data))) level))
3338       (and (= (1+ level) l)
3339            (push (gnus-data-number (car data))
3340                  children)))
3341     (nreverse children)))
3342
3343 (defun gnus-summary-article-parent (&optional number)
3344   "Return the article number of the parent of article NUMBER."
3345   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3346                                     (gnus-data-list t)))
3347          (level (gnus-data-level (car data))))
3348     (if (zerop level)
3349         ()                              ; This is a root.
3350       ;; We search until we find an article with a level less than
3351       ;; this one.  That function has to be the parent.
3352       (while (and (setq data (cdr data))
3353                   (not (< (gnus-data-level (car data)) level))))
3354       (and data (gnus-data-number (car data))))))
3355
3356 (defun gnus-unread-mark-p (mark)
3357   "Say whether MARK is the unread mark."
3358   (= mark gnus-unread-mark))
3359
3360 (defun gnus-read-mark-p (mark)
3361   "Say whether MARK is one of the marks that mark as read.
3362 This is all marks except unread, ticked, dormant, and expirable."
3363   (not (or (= mark gnus-unread-mark)
3364            (= mark gnus-ticked-mark)
3365            (= mark gnus-spam-mark)
3366            (= mark gnus-dormant-mark)
3367            (= mark gnus-expirable-mark))))
3368
3369 (defmacro gnus-article-mark (number)
3370   "Return the MARK of article NUMBER.
3371 This macro should only be used when computing the mark the \"first\"
3372 time; i.e., when generating the summary lines.  After that,
3373 `gnus-summary-article-mark' should be used to examine the
3374 marks of articles."
3375   `(cond
3376     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3377     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3378     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3379     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3380     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3381     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3382     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3383     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3384            gnus-ancient-mark))))
3385
3386 ;; Saving hidden threads.
3387
3388 (defmacro gnus-save-hidden-threads (&rest forms)
3389   "Save hidden threads, eval FORMS, and restore the hidden threads."
3390   (let ((config (make-symbol "config")))
3391     `(let ((,config (gnus-hidden-threads-configuration)))
3392        (unwind-protect
3393            (save-excursion
3394              ,@forms)
3395          (gnus-restore-hidden-threads-configuration ,config)))))
3396 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3397 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3398
3399 (defun gnus-data-compute-positions ()
3400   "Compute the positions of all articles."
3401   (setq gnus-newsgroup-data-reverse nil)
3402   (let ((data gnus-newsgroup-data))
3403     (save-excursion
3404       (gnus-save-hidden-threads
3405         (gnus-summary-show-all-threads)
3406         (goto-char (point-min))
3407         (while data
3408           (while (get-text-property (point) 'gnus-intangible)
3409             (forward-line 1))
3410           (gnus-data-set-pos (car data) (+ (point) 3))
3411           (setq data (cdr data))
3412           (forward-line 1))))))
3413
3414 (defun gnus-hidden-threads-configuration ()
3415   "Return the current hidden threads configuration."
3416   (save-excursion
3417     (let (config)
3418       (goto-char (point-min))
3419       (while (not (eobp))
3420         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3421           (push (save-excursion (forward-line 0) (point)) config))
3422         (forward-line 1))
3423       config)))
3424
3425 (defun gnus-restore-hidden-threads-configuration (config)
3426   "Restore hidden threads configuration from CONFIG."
3427   (save-excursion
3428     (let (point (inhibit-read-only t))
3429       (while (setq point (pop config))
3430         (goto-char point)
3431         (gnus-summary-hide-thread)))))
3432
3433 ;; Various summary mode internalish functions.
3434
3435 (defun gnus-mouse-pick-article (e)
3436   (interactive "e")
3437   (mouse-set-point e)
3438   (gnus-summary-next-page nil t))
3439
3440 (defun gnus-summary-set-display-table ()
3441   "Change the display table.
3442 Odd characters have a tendency to mess
3443 up nicely formatted displays - we make all possible glyphs
3444 display only a single character."
3445
3446   ;; We start from the standard display table, if any.
3447   (let ((table (or (copy-sequence standard-display-table)
3448                    (make-display-table)))
3449         (i 32))
3450     ;; Nix out all the control chars...
3451     (while (>= (setq i (1- i)) 0)
3452       (gnus-put-display-table i [??] table))
3453    ;; ... but not newline and cr, of course.  (cr is necessary for the
3454     ;; selective display).
3455     (gnus-put-display-table ?\n nil table)
3456     (gnus-put-display-table ?\r nil table)
3457     ;; We keep TAB as well.
3458     (gnus-put-display-table ?\t nil table)
3459     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3460     ;; Emacs 23 (unicode), that are not set already.
3461     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3462                  160
3463                256)))
3464       (while (>= (setq i (1- i)) 127)
3465         ;; Only modify if the entry is nil.
3466         (unless (gnus-get-display-table i table)
3467           (gnus-put-display-table i [??] table))))
3468     (setq buffer-display-table table)))
3469
3470 (defun gnus-summary-set-article-display-arrow (pos)
3471   "Update the overlay arrow to point to line at position POS."
3472   (when gnus-summary-display-arrow
3473     (make-local-variable 'overlay-arrow-position)
3474     (make-local-variable 'overlay-arrow-string)
3475     (save-excursion
3476       (goto-char pos)
3477       (beginning-of-line)
3478       (unless overlay-arrow-position
3479         (setq overlay-arrow-position (make-marker)))
3480       (setq overlay-arrow-string "=>"
3481             overlay-arrow-position (set-marker overlay-arrow-position
3482                                                (point)
3483                                                (current-buffer))))))
3484
3485 (defun gnus-summary-setup-buffer (group)
3486   "Initialize summary buffer."
3487   (let ((buffer (gnus-summary-buffer-name group))
3488         (dead-name (concat "*Dead Summary "
3489                            (gnus-group-decoded-name group) "*")))
3490     ;; If a dead summary buffer exists, we kill it.
3491     (when (gnus-buffer-live-p dead-name)
3492       (gnus-kill-buffer dead-name))
3493     (if (get-buffer buffer)
3494         (progn
3495           (set-buffer buffer)
3496           (setq gnus-summary-buffer (current-buffer))
3497           (not gnus-newsgroup-prepared))
3498       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3499       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3500       (gnus-summary-mode group)
3501       (when gnus-carpal
3502         (gnus-carpal-setup-buffer 'summary))
3503       (when (gnus-group-quit-config group)
3504         (set (make-local-variable 'gnus-single-article-buffer) nil))
3505       (make-local-variable 'gnus-article-buffer)
3506       (make-local-variable 'gnus-article-current)
3507       (make-local-variable 'gnus-original-article-buffer)
3508       (setq gnus-newsgroup-name group)
3509       ;; Set any local variables in the group parameters.
3510       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3511       t)))
3512
3513 (defun gnus-set-global-variables ()
3514   "Set the global equivalents of the buffer-local variables.
3515 They are set to the latest values they had.  These reflect the summary
3516 buffer that was in action when the last article was fetched."
3517   (when (eq major-mode 'gnus-summary-mode)
3518     (setq gnus-summary-buffer (current-buffer))
3519     (let ((name gnus-newsgroup-name)
3520           (marked gnus-newsgroup-marked)
3521           (spam gnus-newsgroup-spam-marked)
3522           (unread gnus-newsgroup-unreads)
3523           (headers gnus-current-headers)
3524           (data gnus-newsgroup-data)
3525           (summary gnus-summary-buffer)
3526           (article-buffer gnus-article-buffer)
3527           (original gnus-original-article-buffer)
3528           (gac gnus-article-current)
3529           (reffed gnus-reffed-article-number)
3530           (score-file gnus-current-score-file)
3531           (default-charset gnus-newsgroup-charset)
3532           vlist)
3533       (let ((locals gnus-newsgroup-variables))
3534         (while locals
3535           (if (consp (car locals))
3536               (push (eval (caar locals)) vlist)
3537             (push (eval (car locals)) vlist))
3538           (setq locals (cdr locals)))
3539         (setq vlist (nreverse vlist)))
3540       (with-current-buffer gnus-group-buffer
3541         (setq gnus-newsgroup-name name
3542               gnus-newsgroup-marked marked
3543               gnus-newsgroup-spam-marked spam
3544               gnus-newsgroup-unreads unread
3545               gnus-current-headers headers
3546               gnus-newsgroup-data data
3547               gnus-article-current gac
3548               gnus-summary-buffer summary
3549               gnus-article-buffer article-buffer
3550               gnus-original-article-buffer original
3551               gnus-reffed-article-number reffed
3552               gnus-current-score-file score-file
3553               gnus-newsgroup-charset default-charset)
3554         (let ((locals gnus-newsgroup-variables))
3555           (while locals
3556             (if (consp (car locals))
3557                 (set (caar locals) (pop vlist))
3558               (set (car locals) (pop vlist)))
3559             (setq locals (cdr locals))))
3560         ;; The article buffer also has local variables.
3561         (when (gnus-buffer-live-p gnus-article-buffer)
3562           (set-buffer gnus-article-buffer)
3563           (setq gnus-summary-buffer summary))))))
3564
3565 (defun gnus-summary-article-unread-p (article)
3566   "Say whether ARTICLE is unread or not."
3567   (memq article gnus-newsgroup-unreads))
3568
3569 (defun gnus-summary-first-article-p (&optional article)
3570   "Return whether ARTICLE is the first article in the buffer."
3571   (if (not (setq article (or article (gnus-summary-article-number))))
3572       nil
3573     (eq article (caar gnus-newsgroup-data))))
3574
3575 (defun gnus-summary-last-article-p (&optional article)
3576   "Return whether ARTICLE is the last article in the buffer."
3577   (if (not (setq article (or article (gnus-summary-article-number))))
3578       ;; All non-existent numbers are the last article.  :-)
3579       t
3580     (not (cdr (gnus-data-find-list article)))))
3581
3582 (defun gnus-make-thread-indent-array (&optional n)
3583   (when (or n
3584             (progn (setq n 200) nil)
3585             (null gnus-thread-indent-array)
3586             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3587     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3588           gnus-thread-indent-array-level gnus-thread-indent-level)
3589     (while (>= n 0)
3590       (aset gnus-thread-indent-array n
3591             (make-string (* n gnus-thread-indent-level) ? ))
3592       (setq n (1- n)))))
3593
3594 (defun gnus-update-summary-mark-positions ()
3595   "Compute where the summary marks are to go."
3596   (save-excursion
3597     (when (gnus-buffer-exists-p gnus-summary-buffer)
3598       (set-buffer gnus-summary-buffer))
3599     (let ((spec gnus-summary-line-format-spec)
3600           pos)
3601       (save-excursion
3602         (gnus-set-work-buffer)
3603         (let ((gnus-tmp-unread ?Z)
3604               (gnus-replied-mark ?Z)
3605               (gnus-score-below-mark ?Z)
3606               (gnus-score-over-mark ?Z)
3607               (gnus-undownloaded-mark ?Z)
3608               (gnus-summary-line-format-spec spec)
3609               (gnus-newsgroup-downloadable '(0))
3610               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3611               case-fold-search ignores)
3612           ;; Here, all marks are bound to Z.
3613           (gnus-summary-insert-line header
3614                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3615           (goto-char (point-min))
3616           ;; Memorize the positions of the same characters as dummy marks.
3617           (while (re-search-forward "[A-D]" nil t)
3618             (push (point) ignores))
3619           (erase-buffer)
3620           ;; We use A-D as dummy marks in order to know column positions
3621           ;; where marks should be inserted.
3622           (setq gnus-tmp-unread ?A
3623                 gnus-replied-mark ?B
3624                 gnus-score-below-mark ?C
3625                 gnus-score-over-mark ?C
3626                 gnus-undownloaded-mark ?D)
3627           (gnus-summary-insert-line header
3628                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3629           ;; Ignore characters which aren't dummy marks.
3630           (dolist (p ignores)
3631             (delete-region (goto-char (1- p)) p)
3632             (insert ?Z))
3633           (goto-char (point-min))
3634           (setq pos (list (cons 'unread
3635                                 (and (search-forward "A" nil t)
3636                                      (- (point) (point-min) 1)))))
3637           (goto-char (point-min))
3638           (push (cons 'replied (and (search-forward "B" nil t)
3639                                     (- (point) (point-min) 1)))
3640                 pos)
3641           (goto-char (point-min))
3642           (push (cons 'score (and (search-forward "C" nil t)
3643                                   (- (point) (point-min) 1)))
3644                 pos)
3645           (goto-char (point-min))
3646           (push (cons 'download (and (search-forward "D" nil t)
3647                                      (- (point) (point-min) 1)))
3648                 pos)))
3649       (setq gnus-summary-mark-positions pos))))
3650
3651 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3652   "Insert a dummy root in the summary buffer."
3653   (beginning-of-line)
3654   (gnus-add-text-properties
3655    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3656    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3657
3658 (defun gnus-summary-extract-address-component (from)
3659   (or (car (funcall gnus-extract-address-components from))
3660       from))
3661
3662 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3663   (let ((mail-parse-charset gnus-newsgroup-charset)
3664         (ignored-from-addresses (gnus-ignored-from-addresses))
3665         ; Is it really necessary to do this next part for each summary line?
3666         ; Luckily, doesn't seem to slow things down much.
3667         (mail-parse-ignored-charsets
3668          (with-current-buffer gnus-summary-buffer
3669            gnus-newsgroup-ignored-charsets)))
3670     (or
3671      (and ignored-from-addresses
3672           (string-match ignored-from-addresses gnus-tmp-from)
3673           (let ((extra-headers (mail-header-extra header))
3674                 to
3675                 newsgroups)
3676             (cond
3677              ((setq to (cdr (assq 'To extra-headers)))
3678               (concat gnus-summary-to-prefix
3679                       (inline
3680                         (gnus-summary-extract-address-component
3681                          (funcall gnus-decode-encoded-address-function to)))))
3682              ((setq newsgroups
3683                     (or
3684                      (cdr (assq 'Newsgroups extra-headers))
3685                      (and
3686                       (memq 'Newsgroups gnus-extra-headers)
3687                       (eq (car (gnus-find-method-for-group
3688                                 gnus-newsgroup-name)) 'nntp)
3689                       (gnus-group-real-name gnus-newsgroup-name))))
3690               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3691      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3692
3693 (defun gnus-summary-insert-line (gnus-tmp-header
3694                                  gnus-tmp-level gnus-tmp-current
3695                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3696                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3697                                  &optional gnus-tmp-dummy gnus-tmp-score
3698                                  gnus-tmp-process)
3699   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3700       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3701                                           gnus-tmp-level)))
3702   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3703          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3704          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3705          (gnus-tmp-score-char
3706           (if (or (null gnus-summary-default-score)
3707                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3708                       gnus-summary-zcore-fuzz))
3709               ?                         ;Whitespace
3710             (if (< gnus-tmp-score gnus-summary-default-score)
3711                 gnus-score-below-mark gnus-score-over-mark)))
3712          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3713          (gnus-tmp-replied
3714           (cond (gnus-tmp-process gnus-process-mark)
3715                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3716                  gnus-cached-mark)
3717                 (gnus-tmp-replied gnus-replied-mark)
3718                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3719                  gnus-forwarded-mark)
3720                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3721                  gnus-saved-mark)
3722                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3723                  gnus-recent-mark)
3724                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3725                  gnus-unseen-mark)
3726                 (t gnus-no-mark)))
3727          (gnus-tmp-downloaded
3728           (cond (undownloaded
3729                  gnus-undownloaded-mark)
3730                 (gnus-newsgroup-agentized
3731                  gnus-downloaded-mark)
3732                 (t
3733                  gnus-no-mark)))
3734          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3735          (gnus-tmp-name
3736           (cond
3737            ((string-match "<[^>]+> *$" gnus-tmp-from)
3738             (let ((beg (match-beginning 0)))
3739               (or (and (string-match "^\".+\"" gnus-tmp-from)
3740                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3741                   (substring gnus-tmp-from 0 beg))))
3742            ((string-match "(.+)" gnus-tmp-from)
3743             (substring gnus-tmp-from
3744                        (1+ (match-beginning 0)) (1- (match-end 0))))
3745            (t gnus-tmp-from)))
3746          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3747          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3748          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3749          (inhibit-read-only t))
3750     (when (string= gnus-tmp-name "")
3751       (setq gnus-tmp-name gnus-tmp-from))
3752     (unless (numberp gnus-tmp-lines)
3753       (setq gnus-tmp-lines -1))
3754     (if (= gnus-tmp-lines -1)
3755         (setq gnus-tmp-lines "?")
3756       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3757     (condition-case ()
3758         (gnus-put-text-property
3759          (point)
3760          (progn (eval gnus-summary-line-format-spec) (point))
3761          'gnus-number gnus-tmp-number)
3762       (error (gnus-message 5 "Error updating the summary line")))
3763     (when (gnus-visual-p 'summary-highlight 'highlight)
3764       (forward-line -1)
3765       (gnus-summary-highlight-line)
3766       (gnus-run-hooks 'gnus-summary-update-hook)
3767       (forward-line 1))))
3768
3769 (defun gnus-summary-update-line (&optional dont-update)
3770   "Update summary line after change."
3771   (when (and gnus-summary-default-score
3772              (not gnus-summary-inhibit-highlight))
3773     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3774            (article (gnus-summary-article-number))
3775            (score (gnus-summary-article-score article)))
3776       (unless dont-update
3777         (if (and gnus-summary-mark-below
3778                  (< (gnus-summary-article-score)
3779                     gnus-summary-mark-below))
3780             ;; This article has a low score, so we mark it as read.
3781             (when (memq article gnus-newsgroup-unreads)
3782               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3783           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3784             ;; This article was previously marked as read on account
3785             ;; of a low score, but now it has risen, so we mark it as
3786             ;; unread.
3787             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3788         (gnus-summary-update-mark
3789          (if (or (null gnus-summary-default-score)
3790                  (<= (abs (- score gnus-summary-default-score))
3791                      gnus-summary-zcore-fuzz))
3792              ?                          ;Whitespace
3793            (if (< score gnus-summary-default-score)
3794                gnus-score-below-mark gnus-score-over-mark))
3795          'score))
3796       ;; Do visual highlighting.
3797       (when (gnus-visual-p 'summary-highlight 'highlight)
3798         (gnus-summary-highlight-line)
3799         (gnus-run-hooks 'gnus-summary-update-hook)))))
3800
3801 (defvar gnus-tmp-new-adopts nil)
3802
3803 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3804   "Return the number of articles in THREAD.
3805 This may be 0 in some cases -- if none of the articles in
3806 the thread are to be displayed."
3807   (let* ((number
3808          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3809           (cond
3810            ((not (listp thread))
3811             1)
3812            ((and (consp thread) (cdr thread))
3813             (apply
3814              '+ 1 (mapcar
3815                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3816            ((null thread)
3817             1)
3818            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3819             1)
3820            (t 0))))
3821     (when (and level (zerop level) gnus-tmp-new-adopts)
3822       (incf number
3823             (apply '+ (mapcar
3824                        'gnus-summary-number-of-articles-in-thread
3825                        gnus-tmp-new-adopts))))
3826     (if char
3827         (if (> number 1) gnus-not-empty-thread-mark
3828           gnus-empty-thread-mark)
3829       number)))
3830
3831 (defsubst gnus-summary-line-message-size (head)
3832   "Return pretty-printed version of message size.
3833 This function is intended to be used in
3834 `gnus-summary-line-format-alist'."
3835   (let ((c (or (mail-header-chars head) -1)))
3836     (cond ((< c 0) "n/a")               ; chars not available
3837           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3838           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3839           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3840           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3841
3842
3843 (defun gnus-summary-set-local-parameters (group)
3844   "Go through the local params of GROUP and set all variable specs in that list."
3845   (let ((vars '(quit-config)))          ; Ignore quit-config.
3846     (dolist (elem (gnus-group-find-parameter group))
3847       (and (consp elem)                 ; Has to be a cons.
3848            (consp (cdr elem))           ; The cdr has to be a list.
3849            (symbolp (car elem))         ; Has to be a symbol in there.
3850            (not (memq (car elem) vars))
3851            (ignore-errors
3852              (push (car elem) vars)
3853              ;; Variables like `gnus-show-threads' that are globally
3854              ;; bound, if used as group parameters, need to get to be
3855              ;; buffer-local, whereas just parameters like `gcc-self',
3856              ;; `timestamp', etc. should not be bound as variables.
3857              (if (boundp (car elem))
3858                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3859                (eval (nth 1 elem))))))))
3860
3861 (defun gnus-summary-read-group (group &optional show-all no-article
3862                                       kill-buffer no-display backward
3863                                       select-articles)
3864   "Start reading news in newsgroup GROUP.
3865 If SHOW-ALL is non-nil, already read articles are also listed.
3866 If NO-ARTICLE is non-nil, no article is selected initially.
3867 If NO-DISPLAY, don't generate a summary buffer."
3868   (let (result)
3869     (while (and group
3870                 (null (setq result
3871                             (let ((gnus-auto-select-next nil))
3872                               (or (gnus-summary-read-group-1
3873                                    group show-all no-article
3874                                    kill-buffer no-display
3875                                    select-articles)
3876                                   (setq show-all nil
3877                                         select-articles nil)))))
3878                 (eq gnus-auto-select-next 'quietly))
3879       (set-buffer gnus-group-buffer)
3880       ;; The entry function called above goes to the next
3881       ;; group automatically, so we go two groups back
3882       ;; if we are searching for the previous group.
3883       (when backward
3884         (gnus-group-prev-unread-group 2))
3885       (if (not (equal group (gnus-group-group-name)))
3886           (setq group (gnus-group-group-name))
3887         (setq group nil)))
3888     result))
3889
3890 (defun gnus-summary-read-group-1 (group show-all no-article
3891                                         kill-buffer no-display
3892                                         &optional select-articles)
3893   ;; Killed foreign groups can't be entered.
3894   ;;  (when (and (not (gnus-group-native-p group))
3895   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3896   ;;    (error "Dead non-native groups can't be entered"))
3897   (gnus-message 5 "Retrieving newsgroup: %s..."
3898                 (gnus-group-decoded-name group))
3899   (let* ((new-group (gnus-summary-setup-buffer group))
3900          (quit-config (gnus-group-quit-config group))
3901          (did-select (and new-group (gnus-select-newsgroup
3902                                      group show-all select-articles))))
3903     (cond
3904      ;; This summary buffer exists already, so we just select it.
3905      ((not new-group)
3906       (gnus-set-global-variables)
3907       (when kill-buffer
3908         (gnus-kill-or-deaden-summary kill-buffer))
3909       (gnus-configure-windows 'summary 'force)
3910       (gnus-set-mode-line 'summary)
3911       (gnus-summary-position-point)
3912       (message "")
3913       t)
3914      ;; We couldn't select this group.
3915      ((null did-select)
3916       (when (and (eq major-mode 'gnus-summary-mode)
3917                  (not (equal (current-buffer) kill-buffer)))
3918         (kill-buffer (current-buffer))
3919         (if (not quit-config)
3920             (progn
3921               ;; Update the info -- marks might need to be removed,
3922               ;; for instance.
3923               (gnus-summary-update-info)
3924               (set-buffer gnus-group-buffer)
3925               (gnus-group-jump-to-group group)
3926               (gnus-group-next-unread-group 1))
3927           (gnus-handle-ephemeral-exit quit-config)))
3928       (let ((grpinfo (gnus-get-info group)))
3929         (if (null (gnus-info-read grpinfo))
3930             (gnus-message 3 "Group %s contains no messages"
3931                           (gnus-group-decoded-name group))
3932           (gnus-message 3 "Can't select group")))
3933       nil)
3934      ;; The user did a `C-g' while prompting for number of articles,
3935      ;; so we exit this group.
3936      ((eq did-select 'quit)
3937       (and (eq major-mode 'gnus-summary-mode)
3938            (not (equal (current-buffer) kill-buffer))
3939            (kill-buffer (current-buffer)))
3940       (when kill-buffer
3941         (gnus-kill-or-deaden-summary kill-buffer))
3942       (if (not quit-config)
3943           (progn
3944             (set-buffer gnus-group-buffer)
3945             (gnus-group-jump-to-group group)
3946             (gnus-configure-windows 'group 'force))
3947         (gnus-handle-ephemeral-exit quit-config))
3948       ;; Finally signal the quit.
3949       (signal 'quit nil))
3950      ;; The group was successfully selected.
3951      (t
3952       (gnus-set-global-variables)
3953       ;; Save the active value in effect when the group was entered.
3954       (setq gnus-newsgroup-active
3955             (gnus-copy-sequence
3956              (gnus-active gnus-newsgroup-name)))
3957       ;; You can change the summary buffer in some way with this hook.
3958       (gnus-run-hooks 'gnus-select-group-hook)
3959       (when (memq 'summary (gnus-update-format-specifications
3960                             nil 'summary 'summary-mode 'summary-dummy))
3961         ;; The format specification for the summary line was updated,
3962         ;; so we need to update the mark positions as well.
3963         (gnus-update-summary-mark-positions))
3964       ;; Do score processing.
3965       (when gnus-use-scoring
3966         (gnus-possibly-score-headers))
3967       ;; Check whether to fill in the gaps in the threads.
3968       (when gnus-build-sparse-threads
3969         (gnus-build-sparse-threads))
3970       ;; Find the initial limit.
3971       (if show-all
3972           (let ((gnus-newsgroup-dormant nil))
3973             (gnus-summary-initial-limit show-all))
3974         (gnus-summary-initial-limit show-all))
3975       ;; Generate the summary buffer.
3976       (unless no-display
3977         (gnus-summary-prepare))
3978       (when gnus-use-trees
3979         (gnus-tree-open group)
3980         (setq gnus-summary-highlight-line-function
3981               'gnus-tree-highlight-article))
3982       ;; If the summary buffer is empty, but there are some low-scored
3983       ;; articles or some excluded dormants, we include these in the
3984       ;; buffer.
3985       (when (and (zerop (buffer-size))
3986                  (not no-display))
3987         (cond (gnus-newsgroup-dormant
3988                (gnus-summary-limit-include-dormant))
3989               ((and gnus-newsgroup-scored show-all)
3990                (gnus-summary-limit-include-expunged t))))
3991       ;; Function `gnus-apply-kill-file' must be called in this hook.
3992       (gnus-run-hooks 'gnus-apply-kill-hook)
3993       (if (and (zerop (buffer-size))
3994                (not no-display))
3995           (progn
3996             ;; This newsgroup is empty.
3997             (gnus-summary-catchup-and-exit nil t)
3998             (gnus-message 6 "No unread news")
3999             (when kill-buffer
4000               (gnus-kill-or-deaden-summary kill-buffer))
4001             ;; Return nil from this function.
4002             nil)
4003         ;; Hide conversation thread subtrees.  We cannot do this in
4004         ;; gnus-summary-prepare-hook since kill processing may not
4005         ;; work with hidden articles.
4006         (gnus-summary-maybe-hide-threads)
4007         (when kill-buffer
4008           (gnus-kill-or-deaden-summary kill-buffer))
4009         (gnus-summary-auto-select-subject)
4010         ;; Show first unread article if requested.
4011         (if (and (not no-article)
4012                  (not no-display)
4013                  gnus-newsgroup-unreads
4014                  gnus-auto-select-first)
4015             (progn
4016               (gnus-configure-windows 'summary)
4017               (let ((art (gnus-summary-article-number)))
4018                 (unless (and (not gnus-plugged)
4019                              (or (memq art gnus-newsgroup-undownloaded)
4020                                  (memq art gnus-newsgroup-downloadable)))
4021                   (gnus-summary-goto-article art))))
4022           ;; Don't select any articles.
4023           (gnus-summary-position-point)
4024           (gnus-configure-windows 'summary 'force)
4025           (gnus-set-mode-line 'summary))
4026         (when (and gnus-auto-center-group
4027                    (get-buffer-window gnus-group-buffer t))
4028           ;; Gotta use windows, because recenter does weird stuff if
4029           ;; the current buffer ain't the displayed window.
4030           (let ((owin (selected-window)))
4031             (select-window (get-buffer-window gnus-group-buffer t))
4032             (when (gnus-group-goto-group group)
4033               (recenter))
4034             (select-window owin)))
4035         ;; Mark this buffer as "prepared".
4036         (setq gnus-newsgroup-prepared t)
4037         (gnus-run-hooks 'gnus-summary-prepared-hook)
4038         (unless (gnus-ephemeral-group-p group)
4039           (gnus-group-update-group group))
4040         t)))))
4041
4042 (defun gnus-summary-auto-select-subject ()
4043   "Select the subject line on initial group entry."
4044   (goto-char (point-min))
4045   (cond
4046    ((eq gnus-auto-select-subject 'best)
4047     (gnus-summary-best-unread-subject))
4048    ((eq gnus-auto-select-subject 'unread)
4049     (gnus-summary-first-unread-subject))
4050    ((eq gnus-auto-select-subject 'unseen)
4051     (gnus-summary-first-unseen-subject))
4052    ((eq gnus-auto-select-subject 'unseen-or-unread)
4053     (gnus-summary-first-unseen-or-unread-subject))
4054    ((eq gnus-auto-select-subject 'first)
4055     ;; Do nothing.
4056     )
4057    ((functionp gnus-auto-select-subject)
4058     (funcall gnus-auto-select-subject))))
4059
4060 (defun gnus-summary-prepare ()
4061   "Generate the summary buffer."
4062   (interactive)
4063   (let ((inhibit-read-only t))
4064     (erase-buffer)
4065     (setq gnus-newsgroup-data nil
4066           gnus-newsgroup-data-reverse nil)
4067     (gnus-run-hooks 'gnus-summary-generate-hook)
4068     ;; Generate the buffer, either with threads or without.
4069     (when gnus-newsgroup-headers
4070       (gnus-summary-prepare-threads
4071        (if gnus-show-threads
4072            (gnus-sort-gathered-threads
4073             (funcall gnus-summary-thread-gathering-function
4074                      (gnus-sort-threads
4075                       (gnus-cut-threads (gnus-make-threads)))))
4076          ;; Unthreaded display.
4077          (gnus-sort-articles gnus-newsgroup-headers))))
4078     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4079     ;; Call hooks for modifying summary buffer.
4080     (goto-char (point-min))
4081     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4082
4083 (defsubst gnus-general-simplify-subject (subject)
4084   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4085   (setq subject
4086         (cond
4087          ;; Truncate the subject.
4088          (gnus-simplify-subject-functions
4089           (gnus-map-function gnus-simplify-subject-functions subject))
4090          ((numberp gnus-summary-gather-subject-limit)
4091           (setq subject (gnus-simplify-subject-re subject))
4092           (if (> (length subject) gnus-summary-gather-subject-limit)
4093               (substring subject 0 gnus-summary-gather-subject-limit)
4094             subject))
4095          ;; Fuzzily simplify it.
4096          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4097           (gnus-simplify-subject-fuzzy subject))
4098          ;; Just remove the leading "Re:".
4099          (t
4100           (gnus-simplify-subject-re subject))))
4101
4102   (if (and gnus-summary-gather-exclude-subject
4103            (string-match gnus-summary-gather-exclude-subject subject))
4104       nil                         ; This article shouldn't be gathered
4105     subject))
4106
4107 (defun gnus-summary-simplify-subject-query ()
4108   "Query where the respool algorithm would put this article."
4109   (interactive)
4110   (gnus-summary-select-article)
4111   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4112
4113 (defun gnus-gather-threads-by-subject (threads)
4114   "Gather threads by looking at Subject headers."
4115   (if (not gnus-summary-make-false-root)
4116       threads
4117     (let ((hashtb (gnus-make-hashtable 1024))
4118           (prev threads)
4119           (result threads)
4120           subject hthread whole-subject)
4121       (while threads
4122         (setq subject (gnus-general-simplify-subject
4123                        (setq whole-subject (mail-header-subject
4124                                             (caar threads)))))
4125         (when subject
4126           (if (setq hthread (gnus-gethash subject hashtb))
4127               (progn
4128                 ;; We enter a dummy root into the thread, if we
4129                 ;; haven't done that already.
4130                 (unless (stringp (caar hthread))
4131                   (setcar hthread (list whole-subject (car hthread))))
4132                 ;; We add this new gathered thread to this gathered
4133                 ;; thread.
4134                 (setcdr (car hthread)
4135                         (nconc (cdar hthread) (list (car threads))))
4136                 ;; Remove it from the list of threads.
4137                 (setcdr prev (cdr threads))
4138                 (setq threads prev))
4139             ;; Enter this thread into the hash table.
4140             (gnus-sethash subject
4141                           (if gnus-summary-make-false-root-always
4142                               (progn
4143                                 ;; If you want a dummy root above all
4144                                 ;; threads...
4145                                 (setcar threads (list whole-subject
4146                                                       (car threads)))
4147                                 threads)
4148                             threads)
4149                           hashtb)))
4150         (setq prev threads)
4151         (setq threads (cdr threads)))
4152       result)))
4153
4154 (defun gnus-gather-threads-by-references (threads)
4155   "Gather threads by looking at References headers."
4156   (let ((idhashtb (gnus-make-hashtable 1024))
4157         (thhashtb (gnus-make-hashtable 1024))
4158         (prev threads)
4159         (result threads)
4160         ids references id gthread gid entered ref)
4161     (while threads
4162       (when (setq references (mail-header-references (caar threads)))
4163         (setq id (mail-header-id (caar threads))
4164               ids (inline (gnus-split-references references))
4165               entered nil)
4166         (while (setq ref (pop ids))
4167           (setq ids (delete ref ids))
4168           (if (not (setq gid (gnus-gethash ref idhashtb)))
4169               (progn
4170                 (gnus-sethash ref id idhashtb)
4171                 (gnus-sethash id threads thhashtb))
4172             (setq gthread (gnus-gethash gid thhashtb))
4173             (unless entered
4174               ;; We enter a dummy root into the thread, if we
4175               ;; haven't done that already.
4176               (unless (stringp (caar gthread))
4177                 (setcar gthread (list (mail-header-subject (caar gthread))
4178                                       (car gthread))))
4179               ;; We add this new gathered thread to this gathered
4180               ;; thread.
4181               (setcdr (car gthread)
4182                       (nconc (cdar gthread) (list (car threads)))))
4183             ;; Add it into the thread hash table.
4184             (gnus-sethash id gthread thhashtb)
4185             (setq entered t)
4186             ;; Remove it from the list of threads.
4187             (setcdr prev (cdr threads))
4188             (setq threads prev))))
4189       (setq prev threads)
4190       (setq threads (cdr threads)))
4191     result))
4192
4193 (defun gnus-sort-gathered-threads (threads)
4194   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4195   (let ((result threads))
4196     (while threads
4197       (when (stringp (caar threads))
4198         (setcdr (car threads)
4199                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4200       (setq threads (cdr threads)))
4201     result))
4202
4203 (defun gnus-thread-loop-p (root thread)
4204   "Say whether ROOT is in THREAD."
4205   (let ((stack (list thread))
4206         (infloop 0)
4207         th)
4208     (while (setq thread (pop stack))
4209       (setq th (cdr thread))
4210       (while (and th
4211                   (not (eq (caar th) root)))
4212         (pop th))
4213       (if th
4214           ;; We have found a loop.
4215           (let (ref-dep)
4216             (setcdr thread (delq (car th) (cdr thread)))
4217             (if (boundp (setq ref-dep (intern "none"
4218                                               gnus-newsgroup-dependencies)))
4219                 (setcdr (symbol-value ref-dep)
4220                         (nconc (cdr (symbol-value ref-dep))
4221                                (list (car th))))
4222               (set ref-dep (list nil (car th))))
4223             (setq infloop 1
4224                   stack nil))
4225         ;; Push all the subthreads onto the stack.
4226         (push (cdr thread) stack)))
4227     infloop))
4228
4229 (defun gnus-make-threads ()
4230   "Go through the dependency hashtb and find the roots.  Return all threads."
4231   (let (threads)
4232     (while (catch 'infloop
4233              (mapatoms
4234               (lambda (refs)
4235                 ;; Deal with self-referencing References loops.
4236                 (when (and (car (symbol-value refs))
4237                            (not (zerop
4238                                  (apply
4239                                   '+
4240                                   (mapcar
4241                                    (lambda (thread)
4242                                      (gnus-thread-loop-p
4243                                       (car (symbol-value refs)) thread))
4244                                    (cdr (symbol-value refs)))))))
4245                   (setq threads nil)
4246                   (throw 'infloop t))
4247                 (unless (car (symbol-value refs))
4248                   ;; These threads do not refer back to any other
4249                   ;; articles, so they're roots.
4250                   (setq threads (append (cdr (symbol-value refs)) threads))))
4251               gnus-newsgroup-dependencies)))
4252     threads))
4253
4254 ;; Build the thread tree.
4255 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4256   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4257
4258 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4259 if it was already present.
4260
4261 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4262 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4263 Message-IDs will be renamed to a unique Message-ID before being
4264 entered.
4265
4266 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4267   (let* ((id (mail-header-id header))
4268          (id-dep (and id (intern id dependencies)))
4269          parent-id ref ref-dep ref-header replaced)
4270     ;; Enter this `header' in the `dependencies' table.
4271     (cond
4272      ((not id-dep)
4273       (setq header nil))
4274      ;; The first two cases do the normal part: enter a new `header'
4275      ;; in the `dependencies' table.
4276      ((not (boundp id-dep))
4277       (set id-dep (list header)))
4278      ((null (car (symbol-value id-dep)))
4279       (setcar (symbol-value id-dep) header))
4280
4281      ;; From here the `header' was already present in the
4282      ;; `dependencies' table.
4283      (force-new
4284       ;; Overrides an existing entry;
4285       ;; just set the header part of the entry.
4286       (setcar (symbol-value id-dep) header)
4287       (setq replaced t))
4288
4289      ;; Renames the existing `header' to a unique Message-ID.
4290      ((not gnus-summary-ignore-duplicates)
4291       ;; An article with this Message-ID has already been seen.
4292       ;; We rename the Message-ID.
4293       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4294            (list header))
4295       (mail-header-set-id header id))
4296
4297      ;; The last case ignores an existing entry, except it adds any
4298      ;; additional Xrefs (in case the two articles came from different
4299      ;; servers.
4300      ;; Also sets `header' to `nil' meaning that the `dependencies'
4301      ;; table was *not* modified.
4302      (t
4303       (mail-header-set-xref
4304        (car (symbol-value id-dep))
4305        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4306                    "")
4307                (or (mail-header-xref header) "")))
4308       (setq header nil)))
4309
4310     (when (and header (not replaced))
4311       ;; First check that we are not creating a References loop.
4312       (setq parent-id (gnus-parent-id (mail-header-references header)))
4313       (setq ref parent-id)
4314       (while (and ref
4315                   (setq ref-dep (intern-soft ref dependencies))
4316                   (boundp ref-dep)
4317                   (setq ref-header (car (symbol-value ref-dep))))
4318         (if (string= id ref)
4319             ;; Yuk!  This is a reference loop.  Make the article be a
4320             ;; root article.
4321             (progn
4322               (mail-header-set-references (car (symbol-value id-dep)) "none")
4323               (setq ref nil)
4324               (setq parent-id nil))
4325           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4326       (setq ref-dep (intern (or parent-id "none") dependencies))
4327       (if (boundp ref-dep)
4328           (setcdr (symbol-value ref-dep)
4329                   (nconc (cdr (symbol-value ref-dep))
4330                          (list (symbol-value id-dep))))
4331         (set ref-dep (list nil (symbol-value id-dep)))))
4332     header))
4333
4334 (defun gnus-extract-message-id-from-in-reply-to (string)
4335   (if (string-match "<[^>]+>" string)
4336       (substring string (match-beginning 0) (match-end 0))
4337     nil))
4338
4339 (defun gnus-build-sparse-threads ()
4340   (let ((headers gnus-newsgroup-headers)
4341         (mail-parse-charset gnus-newsgroup-charset)
4342         (gnus-summary-ignore-duplicates t)
4343         header references generation relations
4344         subject child end new-child date)
4345     ;; First we create an alist of generations/relations, where
4346     ;; generations is how much we trust the relation, and the relation
4347     ;; is parent/child.
4348     (gnus-message 7 "Making sparse threads...")
4349     (save-excursion
4350       (nnheader-set-temp-buffer " *gnus sparse threads*")
4351       (while (setq header (pop headers))
4352         (when (and (setq references (mail-header-references header))
4353                    (not (string= references "")))
4354           (insert references)
4355           (setq child (mail-header-id header)
4356                 subject (mail-header-subject header)
4357                 date (mail-header-date header)
4358                 generation 0)
4359           (while (search-backward ">" nil t)
4360             (setq end (1+ (point)))
4361             (when (search-backward "<" nil t)
4362               (setq new-child (buffer-substring (point) end))
4363               (push (list (incf generation)
4364                           child (setq child new-child)
4365                           subject date)
4366                     relations)))
4367           (when child
4368             (push (list (1+ generation) child nil subject) relations))
4369           (erase-buffer)))
4370       (kill-buffer (current-buffer)))
4371     ;; Sort over trustworthiness.
4372     (dolist (relation (sort relations 'car-less-than-car))
4373       (when (gnus-dependencies-add-header
4374              (make-full-mail-header
4375               gnus-reffed-article-number
4376               (nth 3 relation) "" (or (nth 4 relation) "")
4377               (nth 1 relation)
4378               (or (nth 2 relation) "") 0 0 "")
4379              gnus-newsgroup-dependencies nil)
4380         (push gnus-reffed-article-number gnus-newsgroup-limit)
4381         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4382         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4383               gnus-newsgroup-reads)
4384         (decf gnus-reffed-article-number)))
4385     (gnus-message 7 "Making sparse threads...done")))
4386
4387 (defun gnus-build-old-threads ()
4388   ;; Look at all the articles that refer back to old articles, and
4389   ;; fetch the headers for the articles that aren't there.  This will
4390   ;; build complete threads - if the roots haven't been expired by the
4391   ;; server, that is.
4392   (let ((mail-parse-charset gnus-newsgroup-charset)
4393         id heads)
4394     (mapatoms
4395      (lambda (refs)
4396        (when (not (car (symbol-value refs)))
4397          (setq heads (cdr (symbol-value refs)))
4398          (while heads
4399            (if (memq (mail-header-number (caar heads))
4400                      gnus-newsgroup-dormant)
4401                (setq heads (cdr heads))
4402              (setq id (symbol-name refs))
4403              (while (and (setq id (gnus-build-get-header id))
4404                          (not (car (gnus-id-to-thread id)))))
4405              (setq heads nil)))))
4406      gnus-newsgroup-dependencies)))
4407
4408 (defsubst gnus-remove-odd-characters (string)
4409   "Translate STRING into something that doesn't contain weird characters."
4410   (mm-subst-char-in-string
4411    ?\r ?\-
4412    (mm-subst-char-in-string ?\n ?\- string t) t))
4413
4414 ;; This function has to be called with point after the article number
4415 ;; on the beginning of the line.
4416 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4417   (let ((eol (point-at-eol))
4418         (buffer (current-buffer))
4419         header references in-reply-to)
4420
4421     ;; overview: [num subject from date id refs chars lines misc]
4422     (unwind-protect
4423         (let (x)
4424           (narrow-to-region (point) eol)
4425           (unless (eobp)
4426             (forward-char))
4427
4428           (setq header
4429                 (make-full-mail-header
4430                  number                 ; number
4431                  (condition-case ()     ; subject
4432                      (gnus-remove-odd-characters
4433                       (funcall gnus-decode-encoded-word-function
4434                                (setq x (nnheader-nov-field))))
4435                    (error x))
4436                  (condition-case ()     ; from
4437                      (gnus-remove-odd-characters
4438                       (funcall gnus-decode-encoded-address-function
4439                                (setq x (nnheader-nov-field))))
4440                    (error x))
4441                  (nnheader-nov-field)   ; date
4442                  (nnheader-nov-read-message-id number)  ; id
4443                  (setq references (nnheader-nov-field)) ; refs
4444                  (nnheader-nov-read-integer) ; chars
4445                  (nnheader-nov-read-integer) ; lines
4446                  (unless (eobp)
4447                    (if (looking-at "Xref: ")
4448                        (goto-char (match-end 0)))
4449                    (nnheader-nov-field)) ; Xref
4450                  (nnheader-nov-parse-extra)))) ; extra
4451
4452       (widen))
4453
4454     (when (and (string= references "")
4455                (setq in-reply-to (mail-header-extra header))
4456                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4457       (mail-header-set-references
4458        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4459
4460     (when gnus-alter-header-function
4461       (funcall gnus-alter-header-function header))
4462     (gnus-dependencies-add-header header dependencies force-new)))
4463
4464 (defun gnus-build-get-header (id)
4465   "Look through the buffer of NOV lines and find the header to ID.
4466 Enter this line into the dependencies hash table, and return
4467 the id of the parent article (if any)."
4468   (let ((deps gnus-newsgroup-dependencies)
4469         found header)
4470     (prog1
4471         (with-current-buffer nntp-server-buffer
4472           (let ((case-fold-search nil))
4473             (goto-char (point-min))
4474             (while (and (not found)
4475                         (search-forward id nil t))
4476               (beginning-of-line)
4477               (setq found (looking-at
4478                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4479                                    (regexp-quote id))))
4480               (or found (beginning-of-line 2)))
4481             (when found
4482               (beginning-of-line)
4483               (and
4484                (setq header (gnus-nov-parse-line
4485                              (read (current-buffer)) deps))
4486                (gnus-parent-id (mail-header-references header))))))
4487       (when header
4488         (let ((number (mail-header-number header)))
4489           (push number gnus-newsgroup-limit)
4490           (push header gnus-newsgroup-headers)
4491           (if (memq number gnus-newsgroup-unselected)
4492               (progn
4493                 (setq gnus-newsgroup-unreads
4494                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4495                                                number))
4496                 (setq gnus-newsgroup-unselected
4497                       (delq number gnus-newsgroup-unselected)))
4498             (push number gnus-newsgroup-ancient)))))))
4499
4500 (defun gnus-build-all-threads ()
4501   "Read all the headers."
4502   (let ((gnus-summary-ignore-duplicates t)
4503         (mail-parse-charset gnus-newsgroup-charset)
4504         (dependencies gnus-newsgroup-dependencies)
4505         header article)
4506     (with-current-buffer nntp-server-buffer
4507       (let ((case-fold-search nil))
4508         (goto-char (point-min))
4509         (while (not (eobp))
4510           (ignore-errors
4511             (setq article (read (current-buffer))
4512                   header (gnus-nov-parse-line article dependencies)))
4513           (when header
4514             (with-current-buffer gnus-summary-buffer
4515               (push header gnus-newsgroup-headers)
4516               (if (memq (setq article (mail-header-number header))
4517                         gnus-newsgroup-unselected)
4518                   (progn
4519                     (setq gnus-newsgroup-unreads
4520                           (gnus-add-to-sorted-list
4521                            gnus-newsgroup-unreads article))
4522                     (setq gnus-newsgroup-unselected
4523                           (delq article gnus-newsgroup-unselected)))
4524                 (push article gnus-newsgroup-ancient)))
4525             (forward-line 1)))))))
4526
4527 (defun gnus-summary-update-article-line (article header)
4528   "Update the line for ARTICLE using HEADER."
4529   (let* ((id (mail-header-id header))
4530          (thread (gnus-id-to-thread id)))
4531     (unless thread
4532       (error "Article in no thread"))
4533     ;; Update the thread.
4534     (setcar thread header)
4535     (gnus-summary-goto-subject article)
4536     (let* ((datal (gnus-data-find-list article))
4537            (data (car datal))
4538            (inhibit-read-only t)
4539            (level (gnus-summary-thread-level)))
4540       (gnus-delete-line)
4541       (let ((inserted (- (point)
4542                          (progn
4543                            (gnus-summary-insert-line
4544                             header level nil
4545                             (memq article gnus-newsgroup-undownloaded)
4546                             (gnus-article-mark article)
4547                             (memq article gnus-newsgroup-replied)
4548                             (memq article gnus-newsgroup-expirable)
4549                             ;; Only insert the Subject string when it's different
4550                             ;; from the previous Subject string.
4551                             (if (and
4552                                  gnus-show-threads
4553                                  (gnus-subject-equal
4554                                   (condition-case ()
4555                                       (mail-header-subject
4556                                        (gnus-data-header
4557                                         (cadr
4558                                          (gnus-data-find-list
4559                                           article
4560                                           (gnus-data-list t)))))
4561                                     ;; Error on the side of excessive subjects.
4562                                     (error ""))
4563                                   (mail-header-subject header)))
4564                                 ""
4565                               (mail-header-subject header))
4566                             nil (cdr (assq article gnus-newsgroup-scored))
4567                             (memq article gnus-newsgroup-processable))
4568                            (point)))))
4569         (when (cdr datal)
4570           (gnus-data-update-list
4571            (cdr datal)
4572            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4573
4574 (defun gnus-summary-update-article (article &optional iheader)
4575   "Update ARTICLE in the summary buffer."
4576   (set-buffer gnus-summary-buffer)
4577   (let* ((header (gnus-summary-article-header article))
4578          (id (mail-header-id header))
4579          (data (gnus-data-find article))
4580          (thread (gnus-id-to-thread id))
4581          (references (mail-header-references header))
4582          (parent
4583           (gnus-id-to-thread
4584            (or (gnus-parent-id
4585                 (when (and references
4586                            (not (equal "" references)))
4587                   references))
4588                "none")))
4589          (inhibit-read-only t)
4590          (old (car thread)))
4591     (when thread
4592       (unless iheader
4593         (setcar thread nil)
4594         (when parent
4595           (delq thread parent)))
4596       (if (gnus-summary-insert-subject id header)
4597           ;; Set the (possibly) new article number in the data structure.
4598           (gnus-data-set-number data (gnus-id-to-article id))
4599         (setcar thread old)
4600         nil))))
4601
4602 (defun gnus-rebuild-thread (id &optional line)
4603   "Rebuild the thread containing ID.
4604 If LINE, insert the rebuilt thread starting on line LINE."
4605   (let ((inhibit-read-only t)
4606         old-pos current thread data)
4607     (if (not gnus-show-threads)
4608         (setq thread (list (car (gnus-id-to-thread id))))
4609       ;; Get the thread this article is part of.
4610       (setq thread (gnus-remove-thread id)))
4611     (setq old-pos (point-at-bol))
4612     (setq current (save-excursion
4613                     (and (re-search-backward "[\r\n]" nil t)
4614                          (gnus-summary-article-number))))
4615     ;; If this is a gathered thread, we have to go some re-gathering.
4616     (when (stringp (car thread))
4617       (let ((subject (car thread))
4618             roots thr)
4619         (setq thread (cdr thread))
4620         (while thread
4621           (unless (memq (setq thr (gnus-id-to-thread
4622                                    (gnus-root-id
4623                                     (mail-header-id (caar thread)))))
4624                         roots)
4625             (push thr roots))
4626           (setq thread (cdr thread)))
4627         ;; We now have all (unique) roots.
4628         (if (= (length roots) 1)
4629             ;; All the loose roots are now one solid root.
4630             (setq thread (car roots))
4631           (setq thread (cons subject (gnus-sort-threads roots))))))
4632     (let (threads)
4633       ;; We then insert this thread into the summary buffer.
4634       (when line
4635         (goto-char (point-min))
4636         (forward-line (1- line)))
4637       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4638         (if gnus-show-threads
4639             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4640           (gnus-summary-prepare-unthreaded thread))
4641         (setq data (nreverse gnus-newsgroup-data))
4642         (setq threads gnus-newsgroup-threads))
4643       ;; We splice the new data into the data structure.
4644       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4645       ;;!!! then we want to insert at the beginning of the buffer.
4646       ;;!!! That happens to be true with Gnus now, but that may
4647       ;;!!! change in the future.  Perhaps.
4648       (gnus-data-enter-list
4649        (if line nil current) data (- (point) old-pos))
4650       (setq gnus-newsgroup-threads
4651             (nconc threads gnus-newsgroup-threads))
4652       (gnus-data-compute-positions))))
4653
4654 (defun gnus-number-to-header (number)
4655   "Return the header for article NUMBER."
4656   (let ((headers gnus-newsgroup-headers))
4657     (while (and headers
4658                 (not (= number (mail-header-number (car headers)))))
4659       (pop headers))
4660     (when headers
4661       (car headers))))
4662
4663 (defun gnus-parent-headers (in-headers &optional generation)
4664   "Return the headers of the GENERATIONeth parent of HEADERS."
4665   (unless generation
4666     (setq generation 1))
4667   (let ((parent t)
4668         (headers in-headers)
4669         references)
4670     (while (and parent
4671                 (not (zerop generation))
4672                 (setq references (mail-header-references headers)))
4673       (setq headers (if (and references
4674                              (setq parent (gnus-parent-id references)))
4675                         (car (gnus-id-to-thread parent))
4676                       nil))
4677       (decf generation))
4678     (and (not (eq headers in-headers))
4679          headers)))
4680
4681 (defun gnus-id-to-thread (id)
4682   "Return the (sub-)thread where ID appears."
4683   (gnus-gethash id gnus-newsgroup-dependencies))
4684
4685 (defun gnus-id-to-article (id)
4686   "Return the article number of ID."
4687   (let ((thread (gnus-id-to-thread id)))
4688     (when (and thread
4689                (car thread))
4690       (mail-header-number (car thread)))))
4691
4692 (defun gnus-id-to-header (id)
4693   "Return the article headers of ID."
4694   (car (gnus-id-to-thread id)))
4695
4696 (defun gnus-article-displayed-root-p (article)
4697   "Say whether ARTICLE is a root(ish) article."
4698   (let ((level (gnus-summary-thread-level article))
4699         (refs (mail-header-references  (gnus-summary-article-header article)))
4700         particle)
4701     (cond
4702      ((null level) nil)
4703      ((zerop level) t)
4704      ((null refs) t)
4705      ((null (gnus-parent-id refs)) t)
4706      ((and (= 1 level)
4707            (null (setq particle (gnus-id-to-article
4708                                  (gnus-parent-id refs))))
4709            (null (gnus-summary-thread-level particle)))))))
4710
4711 (defun gnus-root-id (id)
4712   "Return the id of the root of the thread where ID appears."
4713   (let (last-id prev)
4714     (while (and id (setq prev (car (gnus-id-to-thread id))))
4715       (setq last-id id
4716             id (gnus-parent-id (mail-header-references prev))))
4717     last-id))
4718
4719 (defun gnus-articles-in-thread (thread)
4720   "Return the list of articles in THREAD."
4721   (cons (mail-header-number (car thread))
4722         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4723
4724 (defun gnus-remove-thread (id &optional dont-remove)
4725   "Remove the thread that has ID in it."
4726   (let (headers thread last-id)
4727     ;; First go up in this thread until we find the root.
4728     (setq last-id (gnus-root-id id)
4729           headers (message-flatten-list (gnus-id-to-thread last-id)))
4730     ;; We have now found the real root of this thread.  It might have
4731     ;; been gathered into some loose thread, so we have to search
4732     ;; through the threads to find the thread we wanted.
4733     (let ((threads gnus-newsgroup-threads)
4734           sub)
4735       (while threads
4736         (setq sub (car threads))
4737         (if (stringp (car sub))
4738             ;; This is a gathered thread, so we look at the roots
4739             ;; below it to find whether this article is in this
4740             ;; gathered root.
4741             (progn
4742               (setq sub (cdr sub))
4743               (while sub
4744                 (when (member (caar sub) headers)
4745                   (setq thread (car threads)
4746                         threads nil
4747                         sub nil))
4748                 (setq sub (cdr sub))))
4749           ;; It's an ordinary thread, so we check it.
4750           (when (eq (car sub) (car headers))
4751             (setq thread sub
4752                   threads nil)))
4753         (setq threads (cdr threads)))
4754       ;; If this article is in no thread, then it's a root.
4755       (if thread
4756           (unless dont-remove
4757             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4758         (setq thread (gnus-id-to-thread last-id)))
4759       (when thread
4760         (prog1
4761             thread                      ; We return this thread.
4762           (unless dont-remove
4763             (if (stringp (car thread))
4764                 (progn
4765                   ;; If we use dummy roots, then we have to remove the
4766                   ;; dummy root as well.
4767                   (when (eq gnus-summary-make-false-root 'dummy)
4768                     ;; We go to the dummy root by going to
4769                     ;; the first sub-"thread", and then one line up.
4770                     (gnus-summary-goto-article
4771                      (mail-header-number (caadr thread)))
4772                     (forward-line -1)
4773                     (gnus-delete-line)
4774                     (gnus-data-compute-positions))
4775                   (setq thread (cdr thread))
4776                   (while thread
4777                     (gnus-remove-thread-1 (car thread))
4778                     (setq thread (cdr thread))))
4779               (gnus-remove-thread-1 thread))))))))
4780
4781 (defun gnus-remove-thread-1 (thread)
4782   "Remove the thread THREAD recursively."
4783   (let ((number (mail-header-number (pop thread)))
4784         d)
4785     (setq thread (reverse thread))
4786     (while thread
4787       (gnus-remove-thread-1 (pop thread)))
4788     (when (setq d (gnus-data-find number))
4789       (goto-char (gnus-data-pos d))
4790       (gnus-summary-show-thread)
4791       (gnus-data-remove
4792        number
4793        (- (point-at-bol)
4794           (prog1
4795               (1+ (point-at-eol))
4796             (gnus-delete-line)))))))
4797
4798 (defun gnus-sort-threads-recursive (threads func)
4799   (sort (mapcar (lambda (thread)
4800                   (cons (car thread)
4801                         (and (cdr thread)
4802                              (gnus-sort-threads-recursive (cdr thread) func))))
4803                 threads) func))
4804
4805 (defun gnus-sort-threads-loop (threads func)
4806   (let* ((superthread (cons nil threads))
4807          (stack (list (cons superthread threads)))
4808          remaining-threads thread)
4809     (while stack
4810       (setq remaining-threads (cdr (car stack)))
4811       (if remaining-threads
4812           (progn (setq thread (car remaining-threads))
4813                  (setcdr (car stack) (cdr remaining-threads))
4814                  (if (cdr thread)
4815                      (push (cons thread (cdr thread)) stack)))
4816         (setq thread (caar stack))
4817         (setcdr thread (sort (cdr thread) func))
4818         (pop stack)))
4819     (cdr superthread)))
4820
4821 (defun gnus-sort-threads (threads)
4822   "Sort THREADS."
4823   (if (not gnus-thread-sort-functions)
4824       threads
4825     (gnus-message 8 "Sorting threads...")
4826     (prog1
4827         (condition-case nil
4828             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4829               (gnus-sort-threads-recursive
4830                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4831           ;; Even after binding max-lisp-eval-depth, the recursive
4832           ;; sorter might fail for very long threads.  In that case,
4833           ;; try using a (less well-tested) non-recursive sorter.
4834           (error (gnus-message 9 "Sorting threads with loop...")
4835                  (gnus-sort-threads-loop
4836                   threads (gnus-make-sort-function
4837                            gnus-thread-sort-functions))))
4838       (gnus-message 8 "Sorting threads...done"))))
4839
4840 (defun gnus-sort-articles (articles)
4841   "Sort ARTICLES."
4842   (when gnus-article-sort-functions
4843     (gnus-message 7 "Sorting articles...")
4844     (prog1
4845         (setq gnus-newsgroup-headers
4846               (sort articles (gnus-make-sort-function
4847                               gnus-article-sort-functions)))
4848       (gnus-message 7 "Sorting articles...done"))))
4849
4850 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4851 (defmacro gnus-thread-header (thread)
4852   "Return header of first article in THREAD.
4853 Note that THREAD must never, ever be anything else than a variable -
4854 using some other form will lead to serious barfage."
4855   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4856   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4857   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4858         (vector thread) 2))
4859
4860 (defsubst gnus-article-sort-by-number (h1 h2)
4861   "Sort articles by article number."
4862   (< (mail-header-number h1)
4863      (mail-header-number h2)))
4864
4865 (defun gnus-thread-sort-by-number (h1 h2)
4866   "Sort threads by root article number."
4867   (gnus-article-sort-by-number
4868    (gnus-thread-header h1) (gnus-thread-header h2)))
4869
4870 (defsubst gnus-article-sort-by-random (h1 h2)
4871   "Sort articles randomly."
4872   (zerop (random 2)))
4873
4874 (defun gnus-thread-sort-by-random (h1 h2)
4875   "Sort threads randomly."
4876   (gnus-article-sort-by-random
4877    (gnus-thread-header h1) (gnus-thread-header h2)))
4878
4879 (defsubst gnus-article-sort-by-lines (h1 h2)
4880   "Sort articles by article Lines header."
4881   (< (mail-header-lines h1)
4882      (mail-header-lines h2)))
4883
4884 (defun gnus-thread-sort-by-lines (h1 h2)
4885   "Sort threads by root article Lines header."
4886   (gnus-article-sort-by-lines
4887    (gnus-thread-header h1) (gnus-thread-header h2)))
4888
4889 (defsubst gnus-article-sort-by-chars (h1 h2)
4890   "Sort articles by octet length."
4891   (< (mail-header-chars h1)
4892      (mail-header-chars h2)))
4893
4894 (defun gnus-thread-sort-by-chars (h1 h2)
4895   "Sort threads by root article octet length."
4896   (gnus-article-sort-by-chars
4897    (gnus-thread-header h1) (gnus-thread-header h2)))
4898
4899 (defsubst gnus-article-sort-by-author (h1 h2)
4900   "Sort articles by root author."
4901   (gnus-string<
4902    (let ((extract (funcall
4903                    gnus-extract-address-components
4904                    (mail-header-from h1))))
4905      (or (car extract) (cadr extract) ""))
4906    (let ((extract (funcall
4907                    gnus-extract-address-components
4908                    (mail-header-from h2))))
4909      (or (car extract) (cadr extract) ""))))
4910
4911 (defun gnus-thread-sort-by-author (h1 h2)
4912   "Sort threads by root author."
4913   (gnus-article-sort-by-author
4914    (gnus-thread-header h1)  (gnus-thread-header h2)))
4915
4916 (defsubst gnus-article-sort-by-recipient (h1 h2)
4917   "Sort articles by recipient."
4918   (gnus-string<
4919    (let ((extract (funcall
4920                    gnus-extract-address-components
4921                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4922      (or (car extract) (cadr extract)))
4923    (let ((extract (funcall
4924                    gnus-extract-address-components
4925                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4926      (or (car extract) (cadr extract)))))
4927
4928 (defun gnus-thread-sort-by-recipient (h1 h2)
4929   "Sort threads by root recipient."
4930   (gnus-article-sort-by-recipient
4931    (gnus-thread-header h1) (gnus-thread-header h2)))
4932
4933 (defsubst gnus-article-sort-by-subject (h1 h2)
4934   "Sort articles by root subject."
4935   (gnus-string<
4936    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4937    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4938
4939 (defun gnus-thread-sort-by-subject (h1 h2)
4940   "Sort threads by root subject."
4941   (gnus-article-sort-by-subject
4942    (gnus-thread-header h1) (gnus-thread-header h2)))
4943
4944 (defsubst gnus-article-sort-by-date (h1 h2)
4945   "Sort articles by root article date."
4946   (time-less-p
4947    (gnus-date-get-time (mail-header-date h1))
4948    (gnus-date-get-time (mail-header-date h2))))
4949
4950 (defun gnus-thread-sort-by-date (h1 h2)
4951   "Sort threads by root article date."
4952   (gnus-article-sort-by-date
4953    (gnus-thread-header h1) (gnus-thread-header h2)))
4954
4955 (defsubst gnus-article-sort-by-score (h1 h2)
4956   "Sort articles by root article score.
4957 Unscored articles will be counted as having a score of zero."
4958   (> (or (cdr (assq (mail-header-number h1)
4959                     gnus-newsgroup-scored))
4960          gnus-summary-default-score 0)
4961      (or (cdr (assq (mail-header-number h2)
4962                     gnus-newsgroup-scored))
4963          gnus-summary-default-score 0)))
4964
4965 (defun gnus-thread-sort-by-score (h1 h2)
4966   "Sort threads by root article score."
4967   (gnus-article-sort-by-score
4968    (gnus-thread-header h1) (gnus-thread-header h2)))
4969
4970 (defun gnus-thread-sort-by-total-score (h1 h2)
4971   "Sort threads by the sum of all scores in the thread.
4972 Unscored articles will be counted as having a score of zero."
4973   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4974
4975 (defun gnus-thread-total-score (thread)
4976   ;; This function find the total score of THREAD.
4977   (cond
4978    ((null thread)
4979     0)
4980    ((consp thread)
4981     (if (stringp (car thread))
4982         (apply gnus-thread-score-function 0
4983                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4984       (gnus-thread-total-score-1 thread)))
4985    (t
4986     (gnus-thread-total-score-1 (list thread)))))
4987
4988 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4989   "Sort threads such that the thread with the most recently arrived article comes first."
4990   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4991
4992 (defun gnus-thread-highest-number (thread)
4993   "Return the highest article number in THREAD."
4994   (apply 'max (mapcar (lambda (header)
4995                         (mail-header-number header))
4996                       (message-flatten-list thread))))
4997
4998 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4999   "Sort threads such that the thread with the most recently dated article comes first."
5000   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5001
5002 ; Since this is called not only to sort the top-level threads, but
5003 ; also in recursive sorts to order the articles within a thread, each
5004 ; article will be processed many times.  Thus it speeds things up
5005 ; quite a bit to use gnus-date-get-time, which caches the time value.
5006 (defun gnus-thread-latest-date (thread)
5007   "Return the highest article date in THREAD."
5008   (apply 'max
5009          (mapcar (lambda (header) (gnus-float-time
5010                                    (gnus-date-get-time
5011                                     (mail-header-date header))))
5012                  (message-flatten-list thread))))
5013
5014 (defun gnus-thread-total-score-1 (root)
5015   ;; This function find the total score of the thread below ROOT.
5016   (setq root (car root))
5017   (apply gnus-thread-score-function
5018          (or (append
5019               (mapcar 'gnus-thread-total-score
5020                       (cdr (gnus-id-to-thread (mail-header-id root))))
5021               (when (> (mail-header-number root) 0)
5022                 (list (or (cdr (assq (mail-header-number root)
5023                                      gnus-newsgroup-scored))
5024                           gnus-summary-default-score 0))))
5025              (list gnus-summary-default-score)
5026              '(0))))
5027
5028 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5029 (defvar gnus-tmp-prev-subject nil)
5030 (defvar gnus-tmp-false-parent nil)
5031 (defvar gnus-tmp-root-expunged nil)
5032 (defvar gnus-tmp-dummy-line nil)
5033
5034 (defun gnus-extra-header (type &optional header)
5035   "Return the extra header of TYPE."
5036   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5037       ""))
5038
5039 (defvar gnus-tmp-thread-tree-header-string "")
5040
5041 (defcustom gnus-sum-thread-tree-root "> "
5042   "With %B spec, used for the root of a thread.
5043 If nil, use subject instead."
5044   :version "22.1"
5045   :type '(radio (const :format "%v  " nil) string)
5046   :group 'gnus-thread)
5047
5048 (defcustom gnus-sum-thread-tree-false-root "> "
5049   "With %B spec, used for a false root of a thread.
5050 If nil, use subject instead."
5051   :version "22.1"
5052   :type '(radio (const :format "%v  " nil) string)
5053   :group 'gnus-thread)
5054
5055 (defcustom gnus-sum-thread-tree-single-indent ""
5056   "With %B spec, used for a thread with just one message.
5057 If nil, use subject instead."
5058   :version "22.1"
5059   :type '(radio (const :format "%v  " nil) string)
5060   :group 'gnus-thread)
5061
5062 (defcustom gnus-sum-thread-tree-vertical "| "
5063   "With %B spec, used for drawing a vertical line."
5064   :version "22.1"
5065   :type 'string
5066   :group 'gnus-thread)
5067
5068 (defcustom gnus-sum-thread-tree-indent "  "
5069   "With %B spec, used for indenting."
5070   :version "22.1"
5071   :type 'string
5072   :group 'gnus-thread)
5073
5074 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5075   "With %B spec, used for a leaf with brothers."
5076   :version "22.1"
5077   :type 'string
5078   :group 'gnus-thread)
5079
5080 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5081   "With %B spec, used for a leaf without brothers."
5082   :version "22.1"
5083   :type 'string
5084   :group 'gnus-thread)
5085
5086 (defcustom gnus-summary-display-while-building nil
5087   "If non-nil, show and update the summary buffer as it's being built.
5088 If the value is t, update the buffer after every line is inserted.  If
5089 the value is an integer (N), update the display every N lines."
5090   :version "22.1"
5091   :group 'gnus-thread
5092   :type '(choice (const :tag "off" nil)
5093                  number
5094                  (const :tag "frequently" t)))
5095
5096 (defun gnus-summary-prepare-threads (threads)
5097   "Prepare summary buffer from THREADS and indentation LEVEL.
5098 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5099 or a straight list of headers."
5100   (gnus-message 7 "Generating summary...")
5101
5102   (setq gnus-newsgroup-threads threads)
5103   (beginning-of-line)
5104
5105   (let ((gnus-tmp-level 0)
5106         (default-score (or gnus-summary-default-score 0))
5107         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5108         (building-line-count gnus-summary-display-while-building)
5109         (building-count (integerp gnus-summary-display-while-building))
5110         thread number subject stack state gnus-tmp-gathered beg-match
5111         new-roots gnus-tmp-new-adopts thread-end simp-subject
5112         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5113         gnus-tmp-replied gnus-tmp-subject-or-nil
5114         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5115         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5116         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5117         tree-stack)
5118
5119     (setq gnus-tmp-prev-subject nil
5120           gnus-tmp-thread-tree-header-string "")
5121
5122     (if (vectorp (car threads))
5123         ;; If this is a straight (sic) list of headers, then a
5124         ;; threaded summary display isn't required, so we just create
5125         ;; an unthreaded one.
5126         (gnus-summary-prepare-unthreaded threads)
5127
5128       ;; Do the threaded display.
5129
5130       (if gnus-summary-display-while-building
5131           (switch-to-buffer (buffer-name)))
5132       (while (or threads stack gnus-tmp-new-adopts new-roots)
5133
5134         (if (and (= gnus-tmp-level 0)
5135                  (or (not stack)
5136                      (= (caar stack) 0))
5137                  (not gnus-tmp-false-parent)
5138                  (or gnus-tmp-new-adopts new-roots))
5139             (if gnus-tmp-new-adopts
5140                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5141                       thread (list (car gnus-tmp-new-adopts))
5142                       gnus-tmp-header (caar thread)
5143                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5144               (when new-roots
5145                 (setq thread (list (car new-roots))
5146                       gnus-tmp-header (caar thread)
5147                       new-roots (cdr new-roots))))
5148
5149           (if threads
5150               ;; If there are some threads, we do them before the
5151               ;; threads on the stack.
5152               (setq thread threads
5153                     gnus-tmp-header (caar thread))
5154             ;; There were no current threads, so we pop something off
5155             ;; the stack.
5156             (setq state (car stack)
5157                   gnus-tmp-level (car state)
5158                   tree-stack (cadr state)
5159                   thread (caddr state)
5160                   stack (cdr stack)
5161                   gnus-tmp-header (caar thread))))
5162
5163         (setq gnus-tmp-false-parent nil)
5164         (setq gnus-tmp-root-expunged nil)
5165         (setq thread-end nil)
5166
5167         (if (stringp gnus-tmp-header)
5168             ;; The header is a dummy root.
5169             (cond
5170              ((eq gnus-summary-make-false-root 'adopt)
5171               ;; We let the first article adopt the rest.
5172               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5173                                                (cddar thread)))
5174               (setq gnus-tmp-gathered
5175                     (nconc (mapcar
5176                             (lambda (h) (mail-header-number (car h)))
5177                             (cddar thread))
5178                            gnus-tmp-gathered))
5179               (setq thread (cons (list (caar thread)
5180                                        (cadar thread))
5181                                  (cdr thread)))
5182               (setq gnus-tmp-level -1
5183                     gnus-tmp-false-parent t))
5184              ((eq gnus-summary-make-false-root 'empty)
5185               ;; We print adopted articles with empty subject fields.
5186               (setq gnus-tmp-gathered
5187                     (nconc (mapcar
5188                             (lambda (h) (mail-header-number (car h)))
5189                             (cddar thread))
5190                            gnus-tmp-gathered))
5191               (setq gnus-tmp-level -1))
5192              ((eq gnus-summary-make-false-root 'dummy)
5193               ;; We remember that we probably want to output a dummy
5194               ;; root.
5195               (setq gnus-tmp-dummy-line gnus-tmp-header)
5196               (setq gnus-tmp-prev-subject gnus-tmp-header))
5197              (t
5198               ;; We do not make a root for the gathered
5199               ;; sub-threads at all.
5200               (setq gnus-tmp-level -1)))
5201
5202           (setq number (mail-header-number gnus-tmp-header)
5203                 subject (mail-header-subject gnus-tmp-header)
5204                 simp-subject (gnus-simplify-subject-fully subject))
5205
5206           (cond
5207            ;; If the thread has changed subject, we might want to make
5208            ;; this subthread into a root.
5209            ((and (null gnus-thread-ignore-subject)
5210                  (not (zerop gnus-tmp-level))
5211                  gnus-tmp-prev-subject
5212                  (not (string= gnus-tmp-prev-subject simp-subject)))
5213             (setq new-roots (nconc new-roots (list (car thread)))
5214                   thread-end t
5215                   gnus-tmp-header nil))
5216            ;; If the article lies outside the current limit,
5217            ;; then we do not display it.
5218            ((not (memq number gnus-newsgroup-limit))
5219             (setq gnus-tmp-gathered
5220                   (nconc (mapcar
5221                           (lambda (h) (mail-header-number (car h)))
5222                           (cdar thread))
5223                          gnus-tmp-gathered))
5224             (setq gnus-tmp-new-adopts (if (cdar thread)
5225                                           (append gnus-tmp-new-adopts
5226                                                   (cdar thread))
5227                                         gnus-tmp-new-adopts)
5228                   thread-end t
5229                   gnus-tmp-header nil)
5230             (when (zerop gnus-tmp-level)
5231               (setq gnus-tmp-root-expunged t)))
5232            ;; Perhaps this article is to be marked as read?
5233            ((and gnus-summary-mark-below
5234                  (< (or (cdr (assq number gnus-newsgroup-scored))
5235                         default-score)
5236                     gnus-summary-mark-below)
5237                  ;; Don't touch sparse articles.
5238                  (not (gnus-summary-article-sparse-p number))
5239                  (not (gnus-summary-article-ancient-p number)))
5240             (setq gnus-newsgroup-unreads
5241                   (delq number gnus-newsgroup-unreads))
5242             (if gnus-newsgroup-auto-expire
5243                 (setq gnus-newsgroup-expirable
5244                       (gnus-add-to-sorted-list
5245                        gnus-newsgroup-expirable number))
5246               (push (cons number gnus-low-score-mark)
5247                     gnus-newsgroup-reads))))
5248
5249           (when gnus-tmp-header
5250             ;; We may have an old dummy line to output before this
5251             ;; article.
5252             (when (and gnus-tmp-dummy-line
5253                        (gnus-subject-equal
5254                         gnus-tmp-dummy-line
5255                         (mail-header-subject gnus-tmp-header)))
5256               (gnus-summary-insert-dummy-line
5257                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5258               (setq gnus-tmp-dummy-line nil))
5259
5260             ;; Compute the mark.
5261             (setq gnus-tmp-unread (gnus-article-mark number))
5262
5263             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5264                                   gnus-tmp-header gnus-tmp-level)
5265                   gnus-newsgroup-data)
5266
5267             ;; Actually insert the line.
5268             (setq
5269              gnus-tmp-subject-or-nil
5270              (cond
5271               ((and gnus-thread-ignore-subject
5272                     gnus-tmp-prev-subject
5273                     (not (string= gnus-tmp-prev-subject simp-subject)))
5274                subject)
5275               ((zerop gnus-tmp-level)
5276                (if (and (eq gnus-summary-make-false-root 'empty)
5277                         (memq number gnus-tmp-gathered)
5278                         gnus-tmp-prev-subject
5279                         (string= gnus-tmp-prev-subject simp-subject))
5280                    gnus-summary-same-subject
5281                  subject))
5282               (t gnus-summary-same-subject)))
5283             (if (and (eq gnus-summary-make-false-root 'adopt)
5284                      (= gnus-tmp-level 1)
5285                      (memq number gnus-tmp-gathered))
5286                 (setq gnus-tmp-opening-bracket ?\<
5287                       gnus-tmp-closing-bracket ?\>)
5288               (setq gnus-tmp-opening-bracket ?\[
5289                     gnus-tmp-closing-bracket ?\]))
5290             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5291                 (gnus-make-thread-indent-array
5292                  (max (* 2 (length gnus-thread-indent-array))
5293                       gnus-tmp-level)))
5294             (setq
5295              gnus-tmp-indentation
5296              (aref gnus-thread-indent-array gnus-tmp-level)
5297              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5298              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5299                                 gnus-summary-default-score 0)
5300              gnus-tmp-score-char
5301              (if (or (null gnus-summary-default-score)
5302                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5303                          gnus-summary-zcore-fuzz))
5304                  ?                      ;Whitespace
5305                (if (< gnus-tmp-score gnus-summary-default-score)
5306                    gnus-score-below-mark gnus-score-over-mark))
5307              gnus-tmp-replied
5308              (cond ((memq number gnus-newsgroup-processable)
5309                     gnus-process-mark)
5310                    ((memq number gnus-newsgroup-cached)
5311                     gnus-cached-mark)
5312                    ((memq number gnus-newsgroup-replied)
5313                     gnus-replied-mark)
5314                    ((memq number gnus-newsgroup-forwarded)
5315                     gnus-forwarded-mark)
5316                    ((memq number gnus-newsgroup-saved)
5317                     gnus-saved-mark)
5318                    ((memq number gnus-newsgroup-recent)
5319                     gnus-recent-mark)
5320                    ((memq number gnus-newsgroup-unseen)
5321                     gnus-unseen-mark)
5322                    (t gnus-no-mark))
5323              gnus-tmp-downloaded
5324              (cond ((memq number gnus-newsgroup-undownloaded)
5325                     gnus-undownloaded-mark)
5326                    (gnus-newsgroup-agentized
5327                     gnus-downloaded-mark)
5328                    (t
5329                     gnus-no-mark))
5330              gnus-tmp-from (mail-header-from gnus-tmp-header)
5331              gnus-tmp-name
5332              (cond
5333               ((string-match "<[^>]+> *$" gnus-tmp-from)
5334                (setq beg-match (match-beginning 0))
5335                (or (and (string-match "^\".+\"" gnus-tmp-from)
5336                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5337                    (substring gnus-tmp-from 0 beg-match)))
5338               ((string-match "(.+)" gnus-tmp-from)
5339                (substring gnus-tmp-from
5340                           (1+ (match-beginning 0)) (1- (match-end 0))))
5341               (t gnus-tmp-from))
5342
5343              ;; Do the %B string
5344              gnus-tmp-thread-tree-header-string
5345              (cond
5346               ((not gnus-show-threads) "")
5347               ((zerop gnus-tmp-level)
5348                (cond ((cdar thread)
5349                       (or gnus-sum-thread-tree-root subject))
5350                      (gnus-tmp-new-adopts
5351                       (or gnus-sum-thread-tree-false-root subject))
5352                      (t
5353                       (or gnus-sum-thread-tree-single-indent subject))))
5354               (t
5355                (concat (apply 'concat
5356                               (mapcar (lambda (item)
5357                                         (if (= item 1)
5358                                             gnus-sum-thread-tree-vertical
5359                                           gnus-sum-thread-tree-indent))
5360                                       (cdr (reverse tree-stack))))
5361                        (if (nth 1 thread)
5362                            gnus-sum-thread-tree-leaf-with-other
5363                          gnus-sum-thread-tree-single-leaf)))))
5364             (when (string= gnus-tmp-name "")
5365               (setq gnus-tmp-name gnus-tmp-from))
5366             (unless (numberp gnus-tmp-lines)
5367               (setq gnus-tmp-lines -1))
5368             (if (= gnus-tmp-lines -1)
5369                 (setq gnus-tmp-lines "?")
5370               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5371             (gnus-put-text-property
5372              (point)
5373              (progn (eval gnus-summary-line-format-spec) (point))
5374              'gnus-number number)
5375             (when gnus-visual-p
5376               (forward-line -1)
5377               (gnus-summary-highlight-line)
5378               (when gnus-summary-update-hook
5379                 (gnus-run-hooks 'gnus-summary-update-hook))
5380               (forward-line 1))
5381
5382             (setq gnus-tmp-prev-subject simp-subject)))
5383
5384         (when (nth 1 thread)
5385           (push (list (max 0 gnus-tmp-level)
5386                       (copy-sequence tree-stack)
5387                       (nthcdr 1 thread))
5388                 stack))
5389         (push (if (nth 1 thread) 1 0) tree-stack)
5390         (incf gnus-tmp-level)
5391         (setq threads (if thread-end nil (cdar thread)))
5392         (if gnus-summary-display-while-building
5393             (if building-count
5394                 (progn
5395                   ;; use a set frequency
5396                   (setq building-line-count (1- building-line-count))
5397                   (when (= building-line-count 0)
5398                     (sit-for 0)
5399                     (setq building-line-count
5400                           gnus-summary-display-while-building)))
5401               ;; always
5402               (sit-for 0)))
5403         (unless threads
5404           (setq gnus-tmp-level 0)))))
5405   (gnus-message 7 "Generating summary...done"))
5406
5407 (defun gnus-summary-prepare-unthreaded (headers)
5408   "Generate an unthreaded summary buffer based on HEADERS."
5409   (let (header number mark)
5410
5411     (beginning-of-line)
5412
5413     (while headers
5414       ;; We may have to root out some bad articles...
5415       (when (memq (setq number (mail-header-number
5416                                 (setq header (pop headers))))
5417                   gnus-newsgroup-limit)
5418         ;; Mark article as read when it has a low score.
5419         (when (and gnus-summary-mark-below
5420                    (< (or (cdr (assq number gnus-newsgroup-scored))
5421                           gnus-summary-default-score 0)
5422                       gnus-summary-mark-below)
5423                    (not (gnus-summary-article-ancient-p number)))
5424           (setq gnus-newsgroup-unreads
5425                 (delq number gnus-newsgroup-unreads))
5426           (if gnus-newsgroup-auto-expire
5427               (push number gnus-newsgroup-expirable)
5428             (push (cons number gnus-low-score-mark)
5429                   gnus-newsgroup-reads)))
5430
5431         (setq mark (gnus-article-mark number))
5432         (push (gnus-data-make number mark (1+ (point)) header 0)
5433               gnus-newsgroup-data)
5434         (gnus-summary-insert-line
5435          header 0 number
5436          (memq number gnus-newsgroup-undownloaded)
5437          mark (memq number gnus-newsgroup-replied)
5438          (memq number gnus-newsgroup-expirable)
5439          (mail-header-subject header) nil
5440          (cdr (assq number gnus-newsgroup-scored))
5441          (memq number gnus-newsgroup-processable))))))
5442
5443 (defun gnus-summary-remove-list-identifiers ()
5444   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5445   (let ((regexp (if (consp gnus-list-identifiers)
5446                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5447                   gnus-list-identifiers))
5448         changed subject)
5449     (when regexp
5450       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5451       (dolist (header gnus-newsgroup-headers)
5452         (setq subject (mail-header-subject header)
5453               changed nil)
5454         (while (string-match regexp subject)
5455           (setq subject
5456                 (concat (substring subject 0 (match-beginning 1))
5457                         (substring subject (match-end 0)))
5458                 changed t))
5459         (when changed
5460           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5461             (setq subject
5462                   (concat (substring subject 0 (match-beginning 1))
5463                           (substring subject (match-end 1)))))
5464           (mail-header-set-subject header subject))))))
5465
5466 (defun gnus-fetch-headers (articles)
5467   "Fetch headers of ARTICLES."
5468   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5469     (gnus-message 5 "Fetching headers for %s..." name)
5470     (prog1
5471         (if (eq 'nov
5472                 (setq gnus-headers-retrieved-by
5473                       (gnus-retrieve-headers
5474                        articles gnus-newsgroup-name
5475                        ;; We might want to fetch old headers, but
5476                        ;; not if there is only 1 article.
5477                        (and (or (and
5478                                  (not (eq gnus-fetch-old-headers 'some))
5479                                  (not (numberp gnus-fetch-old-headers)))
5480                                 (> (length articles) 1))
5481                             gnus-fetch-old-headers))))
5482             (gnus-get-newsgroup-headers-xover
5483              articles nil nil gnus-newsgroup-name t)
5484           (gnus-get-newsgroup-headers))
5485       (gnus-message 5 "Fetching headers for %s...done" name))))
5486
5487 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5488   "Select newsgroup GROUP.
5489 If READ-ALL is non-nil, all articles in the group are selected.
5490 If SELECT-ARTICLES, only select those articles from GROUP."
5491   (let* ((entry (gnus-group-entry group))
5492          ;;!!! Dirty hack; should be removed.
5493          (gnus-summary-ignore-duplicates
5494           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5495               t
5496             gnus-summary-ignore-duplicates))
5497          (info (nth 2 entry))
5498          charset articles fetched-articles cached)
5499
5500     (unless (gnus-check-server
5501              (set (make-local-variable 'gnus-current-select-method)
5502                   (gnus-find-method-for-group group)))
5503       (error "Couldn't open server"))
5504     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5505
5506     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5507         (gnus-activate-group group)     ; Or we can activate it...
5508         (progn                          ; Or we bug out.
5509           (when (equal major-mode 'gnus-summary-mode)
5510             (gnus-kill-buffer (current-buffer)))
5511           (error
5512            "Couldn't activate group %s: %s"
5513            (mm-decode-coding-string group charset)
5514            (mm-decode-coding-string (gnus-status-message group) charset))))
5515
5516     (unless (gnus-request-group group t)
5517       (when (equal major-mode 'gnus-summary-mode)
5518         (gnus-kill-buffer (current-buffer)))
5519       (error "Couldn't request group %s: %s"
5520              (mm-decode-coding-string group charset)
5521              (mm-decode-coding-string (gnus-status-message group) charset)))
5522
5523     (when gnus-agent
5524       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5525
5526       (setq gnus-summary-use-undownloaded-faces
5527             (gnus-agent-find-parameter
5528              group
5529              'agent-enable-undownloaded-faces)))
5530
5531     (setq gnus-newsgroup-name group
5532           gnus-newsgroup-unselected nil
5533           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5534
5535     (let ((display (gnus-group-find-parameter group 'display)))
5536       (setq gnus-newsgroup-display
5537             (cond
5538              ((not (zerop (or (car-safe read-all) 0)))
5539               ;; The user entered the group with C-u SPC/RET, let's show
5540               ;; all articles.
5541               'gnus-not-ignore)
5542              ((eq display 'all)
5543               'gnus-not-ignore)
5544              ((arrayp display)
5545               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5546              ((numberp display)
5547               ;; The following is probably the "correct" solution, but
5548               ;; it makes Gnus fetch all headers and then limit the
5549               ;; articles (which is slow), so instead we hack the
5550               ;; select-articles parameter instead. -- Simon Josefsson
5551               ;; <jas@kth.se>
5552               ;;
5553               ;; (gnus-byte-compile
5554               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5555               ;;                         display)))))
5556               (setq select-articles
5557                     (gnus-uncompress-range
5558                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5559                              (if (> tmp 0)
5560                                  tmp
5561                                1))
5562                            (cdr (gnus-active group)))))
5563               nil)
5564              (t
5565               nil))))
5566
5567     (gnus-summary-setup-default-charset)
5568
5569     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5570     (when (gnus-virtual-group-p group)
5571       (setq cached gnus-newsgroup-cached))
5572
5573     (setq gnus-newsgroup-unreads
5574           (gnus-sorted-ndifference
5575            (gnus-sorted-ndifference gnus-newsgroup-unreads
5576                                     gnus-newsgroup-marked)
5577            gnus-newsgroup-dormant))
5578
5579     (setq gnus-newsgroup-processable nil)
5580
5581     (gnus-update-read-articles group gnus-newsgroup-unreads)
5582
5583     ;; Adjust and set lists of article marks.
5584     (when info
5585       (gnus-adjust-marked-articles info))
5586     (if (setq articles select-articles)
5587         (setq gnus-newsgroup-unselected
5588               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5589       (setq articles (gnus-articles-to-read group read-all)))
5590
5591     (cond
5592      ((null articles)
5593       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5594       'quit)
5595      ((eq articles 0) nil)
5596      (t
5597       ;; Init the dependencies hash table.
5598       (setq gnus-newsgroup-dependencies
5599             (gnus-make-hashtable (length articles)))
5600       (gnus-set-global-variables)
5601       ;; Retrieve the headers and read them in.
5602
5603       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5604
5605       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5606       (when cached
5607         (setq gnus-newsgroup-cached cached))
5608
5609       ;; Suppress duplicates?
5610       (when gnus-suppress-duplicates
5611         (gnus-dup-suppress-articles))
5612
5613       ;; Set the initial limit.
5614       (setq gnus-newsgroup-limit (copy-sequence articles))
5615       ;; Remove canceled articles from the list of unread articles.
5616       (setq fetched-articles
5617             (mapcar (lambda (headers) (mail-header-number headers))
5618                     gnus-newsgroup-headers))
5619       (setq gnus-newsgroup-articles fetched-articles)
5620       (setq gnus-newsgroup-unreads
5621             (gnus-sorted-nintersection
5622              gnus-newsgroup-unreads fetched-articles))
5623       (gnus-compute-unseen-list)
5624
5625       ;; Removed marked articles that do not exist.
5626       (gnus-update-missing-marks
5627        (gnus-sorted-difference articles fetched-articles))
5628       ;; We might want to build some more threads first.
5629       (when (and gnus-fetch-old-headers
5630                  (eq gnus-headers-retrieved-by 'nov))
5631         (if (eq gnus-fetch-old-headers 'invisible)
5632             (gnus-build-all-threads)
5633           (gnus-build-old-threads)))
5634       ;; Let the Gnus agent mark articles as read.
5635       (when gnus-agent
5636         (gnus-agent-get-undownloaded-list))
5637       ;; Remove list identifiers from subject
5638       (when gnus-list-identifiers
5639         (gnus-summary-remove-list-identifiers))
5640       ;; Check whether auto-expire is to be done in this group.
5641       (setq gnus-newsgroup-auto-expire
5642             (gnus-group-auto-expirable-p group))
5643       ;; Set up the article buffer now, if necessary.
5644       (unless (and gnus-single-article-buffer
5645                    (equal gnus-article-buffer "*Article*"))
5646         (gnus-article-setup-buffer))
5647       ;; First and last article in this newsgroup.
5648       (when gnus-newsgroup-headers
5649         (setq gnus-newsgroup-begin
5650               (mail-header-number (car gnus-newsgroup-headers))
5651               gnus-newsgroup-end
5652               (mail-header-number
5653                (gnus-last-element gnus-newsgroup-headers))))
5654       ;; GROUP is successfully selected.
5655       (or gnus-newsgroup-headers t)))))
5656
5657 (defun gnus-compute-unseen-list ()
5658   ;; The `seen' marks are treated specially.
5659   (if (not gnus-newsgroup-seen)
5660       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5661     (setq gnus-newsgroup-unseen
5662           (gnus-inverse-list-range-intersection
5663            gnus-newsgroup-articles gnus-newsgroup-seen))))
5664
5665 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5666
5667 (defun gnus-summary-display-make-predicate (display)
5668   (require 'gnus-agent)
5669   (when (= (length display) 1)
5670     (setq display (car display)))
5671   (unless gnus-summary-display-cache
5672     (dolist (elem (append '((unread . unread)
5673                             (read . read)
5674                             (unseen . unseen))
5675                           gnus-article-mark-lists))
5676       (push (cons (cdr elem)
5677                   (gnus-byte-compile
5678                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5679             gnus-summary-display-cache)))
5680   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5681         (gnus-category-predicate-cache gnus-summary-display-cache))
5682     (gnus-get-predicate display)))
5683
5684 ;; Uses the dynamically bound `number' variable.
5685 (defvar number)
5686 (defun gnus-article-marked-p (type &optional article)
5687   (let ((article (or article number)))
5688     (cond
5689      ((eq type 'tick)
5690       (memq article gnus-newsgroup-marked))
5691      ((eq type 'spam)
5692       (memq article gnus-newsgroup-spam-marked))
5693      ((eq type 'unsend)
5694       (memq article gnus-newsgroup-unsendable))
5695      ((eq type 'undownload)
5696       (memq article gnus-newsgroup-undownloaded))
5697      ((eq type 'download)
5698       (memq article gnus-newsgroup-downloadable))
5699      ((eq type 'unread)
5700       (memq article gnus-newsgroup-unreads))
5701      ((eq type 'read)
5702       (memq article gnus-newsgroup-reads))
5703      ((eq type 'dormant)
5704       (memq article gnus-newsgroup-dormant) )
5705      ((eq type 'expire)
5706       (memq article gnus-newsgroup-expirable))
5707      ((eq type 'reply)
5708       (memq article gnus-newsgroup-replied))
5709      ((eq type 'killed)
5710       (memq article gnus-newsgroup-killed))
5711      ((eq type 'bookmark)
5712       (assq article gnus-newsgroup-bookmarks))
5713      ((eq type 'score)
5714       (assq article gnus-newsgroup-scored))
5715      ((eq type 'save)
5716       (memq article gnus-newsgroup-saved))
5717      ((eq type 'cache)
5718       (memq article gnus-newsgroup-cached))
5719      ((eq type 'forward)
5720       (memq article gnus-newsgroup-forwarded))
5721      ((eq type 'seen)
5722       (not (memq article gnus-newsgroup-unseen)))
5723      ((eq type 'recent)
5724       (memq article gnus-newsgroup-recent))
5725      (t t))))
5726
5727 (defun gnus-articles-to-read (group &optional read-all)
5728   "Find out what articles the user wants to read."
5729   (let* ((articles
5730           ;; Select all articles if `read-all' is non-nil, or if there
5731           ;; are no unread articles.
5732           (if (or read-all
5733                   (and (zerop (length gnus-newsgroup-marked))
5734                        (zerop (length gnus-newsgroup-unreads)))
5735                   ;; Fetch all if the predicate is non-nil.
5736                   gnus-newsgroup-display)
5737               ;; We want to select the headers for all the articles in
5738               ;; the group, so we select either all the active
5739               ;; articles in the group, or (if that's nil), the
5740               ;; articles in the cache.
5741               (or
5742                (if gnus-newsgroup-maximum-articles
5743                    (let ((active (gnus-active group)))
5744                      (gnus-uncompress-range
5745                       (cons (max (car active)
5746                                  (- (cdr active)
5747                                     gnus-newsgroup-maximum-articles
5748                                     -1))
5749                             (cdr active))))
5750                  (gnus-uncompress-range (gnus-active group)))
5751                (gnus-cache-articles-in-group group))
5752             ;; Select only the "normal" subset of articles.
5753             (gnus-sorted-nunion
5754              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5755              gnus-newsgroup-unreads)))
5756          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5757          (scored (length scored-list))
5758          (number (length articles))
5759          (marked (+ (length gnus-newsgroup-marked)
5760                     (length gnus-newsgroup-dormant)))
5761          (select
5762           (cond
5763            ((numberp read-all)
5764             read-all)
5765            ((numberp gnus-newsgroup-display)
5766             gnus-newsgroup-display)
5767            (t
5768             (condition-case ()
5769                 (cond
5770                  ((and (or (<= scored marked) (= scored number))
5771                        (numberp gnus-large-newsgroup)
5772                        (> number gnus-large-newsgroup))
5773                   (let* ((cursor-in-echo-area nil)
5774                          (initial (gnus-parameter-large-newsgroup-initial
5775                                    gnus-newsgroup-name))
5776                          (input
5777                           (read-string
5778                            (format
5779                             "How many articles from %s (%s %d): "
5780                             (gnus-group-decoded-name gnus-newsgroup-name)
5781                             (if initial "max" "default")
5782                             number)
5783                            (if initial
5784                                (cons (number-to-string initial)
5785                                      0)))))
5786                     (if (string-match "^[ \t]*$" input) number input)))
5787                  ((and (> scored marked) (< scored number)
5788                        (> (- scored number) 20))
5789                   (let ((input
5790                          (read-string
5791                           (format "%s %s (%d scored, %d total): "
5792                                   "How many articles from"
5793                                   (gnus-group-decoded-name group)
5794                                   scored number))))
5795                     (if (string-match "^[ \t]*$" input)
5796                         number input)))
5797                  (t number))
5798               (quit
5799                (message "Quit getting the articles to read")
5800                nil))))))
5801     (setq select (if (stringp select) (string-to-number select) select))
5802     (if (or (null select) (zerop select))
5803         select
5804       (if (and (not (zerop scored)) (<= (abs select) scored))
5805           (progn
5806             (setq articles (sort scored-list '<))
5807             (setq number (length articles)))
5808         (setq articles (copy-sequence articles)))
5809
5810       (when (< (abs select) number)
5811         (if (< select 0)
5812             ;; Select the N oldest articles.
5813             (setcdr (nthcdr (1- (abs select)) articles) nil)
5814           ;; Select the N most recent articles.
5815           (setq articles (nthcdr (- number select) articles))))
5816       (setq gnus-newsgroup-unselected
5817             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5818       (when gnus-alter-articles-to-read-function
5819         (setq articles
5820               (sort
5821                (funcall gnus-alter-articles-to-read-function
5822                         gnus-newsgroup-name articles)
5823                '<)))
5824       articles)))
5825
5826 (defun gnus-killed-articles (killed articles)
5827   (let (out)
5828     (while articles
5829       (when (inline (gnus-member-of-range (car articles) killed))
5830         (push (car articles) out))
5831       (setq articles (cdr articles)))
5832     out))
5833
5834 (defun gnus-uncompress-marks (marks)
5835   "Uncompress the mark ranges in MARKS."
5836   (let ((uncompressed '(score bookmark))
5837         out)
5838     (while marks
5839       (if (memq (caar marks) uncompressed)
5840           (push (car marks) out)
5841         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5842       (setq marks (cdr marks)))
5843     out))
5844
5845 (defun gnus-article-mark-to-type (mark)
5846   "Return the type of MARK."
5847   (or (cadr (assq mark gnus-article-special-mark-lists))
5848       'list))
5849
5850 (defun gnus-article-unpropagatable-p (mark)
5851   "Return whether MARK should be propagated to back end."
5852   (memq mark gnus-article-unpropagated-mark-lists))
5853
5854 (defun gnus-adjust-marked-articles (info)
5855   "Set all article lists and remove all marks that are no longer valid."
5856   (let* ((marked-lists (gnus-info-marks info))
5857          (active (gnus-active (gnus-info-group info)))
5858          (min (car active))
5859          (max (cdr active))
5860          (types gnus-article-mark-lists)
5861          marks var articles article mark mark-type
5862          bgn end)
5863     ;; Hack to avoid adjusting marks for imap.
5864     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5865               'nnimap)
5866       (setq min 1))
5867
5868     (dolist (marks marked-lists)
5869       (setq mark (car marks)
5870             mark-type (gnus-article-mark-to-type mark)
5871             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5872
5873       ;; We set the variable according to the type of the marks list,
5874       ;; and then adjust the marks to a subset of the active articles.
5875       (cond
5876        ;; Adjust "simple" lists - compressed yet unsorted
5877        ((eq mark-type 'list)
5878         ;; Simultaneously uncompress and clip to active range
5879         ;; See gnus-uncompress-range for a description of possible marks
5880         (let (l lh)
5881           (if (not (cadr marks))
5882               (set var nil)
5883             (setq articles (if (numberp (cddr marks))
5884                                (list (cdr marks))
5885                              (cdr marks))
5886                   lh (cons nil nil)
5887                   l lh)
5888
5889             (while (setq article (pop articles))
5890               (cond ((consp article)
5891                      (setq bgn (max (car article) min)
5892                            end (min (cdr article) max))
5893                      (while (<= bgn end)
5894                        (setq l (setcdr l (cons bgn nil))
5895                              bgn (1+ bgn))))
5896                     ((and (<= min article)
5897                           (>= max article))
5898                      (setq l (setcdr l (cons article nil))))))
5899             (set var (cdr lh)))))
5900        ;; Adjust assocs.
5901        ((eq mark-type 'tuple)
5902         (set var (setq articles (cdr marks)))
5903         (when (not (listp (cdr (symbol-value var))))
5904           (set var (list (symbol-value var))))
5905         (when (not (listp (cdr articles)))
5906           (setq articles (list articles)))
5907         (while articles
5908           (when (or (not (consp (setq article (pop articles))))
5909                     (< (car article) min)
5910                     (> (car article) max))
5911             (set var (delq article (symbol-value var))))))
5912        ;; Adjust ranges (sloppily).
5913        ((eq mark-type 'range)
5914         (cond
5915          ((eq mark 'seen)
5916           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5917           ;; It should be (seen (NUM1 . NUM2)).
5918           (when (numberp (cddr marks))
5919             (setcdr marks (list (cdr marks))))
5920           (setq articles (cdr marks))
5921           (while (and articles
5922                       (or (and (consp (car articles))
5923                                (> min (cdar articles)))
5924                           (and (numberp (car articles))
5925                                (> min (car articles)))))
5926             (pop articles))
5927           (set var articles))))))))
5928
5929 (defun gnus-update-missing-marks (missing)
5930   "Go through the list of MISSING articles and remove them from the mark lists."
5931   (when missing
5932     (let (var m)
5933       ;; Go through all types.
5934       (dolist (elem gnus-article-mark-lists)
5935         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5936           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5937           (when (symbol-value var)
5938             ;; This list has articles.  So we delete all missing
5939             ;; articles from it.
5940             (setq m missing)
5941             (while m
5942               (set var (delq (pop m) (symbol-value var))))))))))
5943
5944 (defun gnus-update-marks ()
5945   "Enter the various lists of marked articles into the newsgroup info list."
5946   (let ((types gnus-article-mark-lists)
5947         (info (gnus-get-info gnus-newsgroup-name))
5948         type list newmarked symbol delta-marks)
5949     (when info
5950       ;; Add all marks lists to the list of marks lists.
5951       (while (setq type (pop types))
5952         (setq list (symbol-value
5953                     (setq symbol
5954                           (intern (format "gnus-newsgroup-%s" (car type))))))
5955
5956         (when list
5957           ;; Get rid of the entries of the articles that have the
5958           ;; default score.
5959           (when (and (eq (cdr type) 'score)
5960                      gnus-save-score
5961                      list)
5962             (let* ((arts list)
5963                    (prev (cons nil list))
5964                    (all prev))
5965               (while arts
5966                 (if (or (not (consp (car arts)))
5967                         (= (cdar arts) gnus-summary-default-score))
5968                     (setcdr prev (cdr arts))
5969                   (setq prev arts))
5970                 (setq arts (cdr arts)))
5971               (setq list (cdr all)))))
5972
5973         (when (eq (cdr type) 'seen)
5974           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5975
5976         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5977           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5978
5979         (when (and (gnus-check-backend-function
5980                     'request-set-mark gnus-newsgroup-name)
5981                    (not (gnus-article-unpropagatable-p (cdr type))))
5982           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5983                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5984                  (add (gnus-remove-from-range
5985                        (gnus-copy-sequence list) old)))
5986             (when add
5987               (push (list add 'add (list (cdr type))) delta-marks))
5988             (when del
5989               ;; Don't delete marks from outside the active range.  This
5990               ;; shouldn't happen, but is a sanity check.
5991               (setq del (gnus-sorted-range-intersection
5992                          (gnus-active gnus-newsgroup-name) del))
5993               (push (list del 'del (list (cdr type))) delta-marks))))
5994
5995         (when list
5996           (push (cons (cdr type) list) newmarked)))
5997
5998       (when delta-marks
5999         (unless (gnus-check-group gnus-newsgroup-name)
6000           (error "Can't open server for %s" gnus-newsgroup-name))
6001         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6002
6003       ;; Enter these new marks into the info of the group.
6004       (if (nthcdr 3 info)
6005           (setcar (nthcdr 3 info) newmarked)
6006         ;; Add the marks lists to the end of the info.
6007         (when newmarked
6008           (setcdr (nthcdr 2 info) (list newmarked))))
6009
6010       ;; Cut off the end of the info if there's nothing else there.
6011       (let ((i 5))
6012         (while (and (> i 2)
6013                     (not (nth i info)))
6014           (when (nthcdr (decf i) info)
6015             (setcdr (nthcdr i info) nil)))))))
6016
6017 (defun gnus-set-mode-line (where)
6018   "Set the mode line of the article or summary buffers.
6019 If WHERE is `summary', the summary mode line format will be used."
6020   ;; Is this mode line one we keep updated?
6021   (when (and (memq where gnus-updated-mode-lines)
6022              (symbol-value
6023               (intern (format "gnus-%s-mode-line-format-spec" where))))
6024     (let (mode-string)
6025       ;; We evaluate this in the summary buffer since these
6026       ;; variables are buffer-local to that buffer.
6027       (with-current-buffer gnus-summary-buffer
6028         ;; We bind all these variables that are used in the `eval' form
6029         ;; below.
6030         (let* ((mformat (symbol-value
6031                          (intern
6032                           (format "gnus-%s-mode-line-format-spec" where))))
6033                (gnus-tmp-group-name (gnus-mode-string-quote
6034                                      (gnus-group-decoded-name
6035                                       gnus-newsgroup-name)))
6036                (gnus-tmp-article-number (or gnus-current-article 0))
6037                (gnus-tmp-unread gnus-newsgroup-unreads)
6038                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6039                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6040                (gnus-tmp-unread-and-unselected
6041                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6042                             (zerop gnus-tmp-unselected))
6043                        "")
6044                       ((zerop gnus-tmp-unselected)
6045                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6046                       (t (format "{%d(+%d) more}"
6047                                  gnus-tmp-unread-and-unticked
6048                                  gnus-tmp-unselected))))
6049                (gnus-tmp-subject
6050                 (if (and gnus-current-headers
6051                          (vectorp gnus-current-headers))
6052                     (gnus-mode-string-quote
6053                      (mail-header-subject gnus-current-headers))
6054                   ""))
6055                bufname-length max-len
6056                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6057           (setq mode-string (eval mformat))
6058           (setq bufname-length (if (string-match "%b" mode-string)
6059                                    (- (length
6060                                        (buffer-name
6061                                         (if (eq where 'summary)
6062                                             nil
6063                                           (get-buffer gnus-article-buffer))))
6064                                       2)
6065                                  0))
6066           (setq max-len (max 4 (if gnus-mode-non-string-length
6067                                    (- (window-width)
6068                                       gnus-mode-non-string-length
6069                                       bufname-length)
6070                                  (length mode-string))))
6071           ;; We might have to chop a bit of the string off...
6072           (when (> (length mode-string) max-len)
6073             (setq mode-string
6074                   (concat (truncate-string-to-width mode-string (- max-len 3))
6075                           "...")))))
6076       ;; Update the mode line.
6077       (setq mode-line-buffer-identification
6078             (gnus-mode-line-buffer-identification (list mode-string)))
6079       (set-buffer-modified-p t))))
6080
6081 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6082   "Go through the HEADERS list and add all Xrefs to a hash table.
6083 The resulting hash table is returned, or nil if no Xrefs were found."
6084   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6085          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6086          (xref-hashtb (gnus-make-hashtable))
6087          start group entry number xrefs header)
6088     (while headers
6089       (setq header (pop headers))
6090       (when (and (setq xrefs (mail-header-xref header))
6091                  (not (memq (setq number (mail-header-number header))
6092                             unreads)))
6093         (setq start 0)
6094         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6095           (setq start (match-end 0))
6096           (setq group (if prefix
6097                           (concat prefix (substring xrefs (match-beginning 1)
6098                                                     (match-end 1)))
6099                         (substring xrefs (match-beginning 1) (match-end 1))))
6100           (setq number
6101                 (string-to-number (substring xrefs (match-beginning 2)
6102                                           (match-end 2))))
6103           (if (setq entry (gnus-gethash group xref-hashtb))
6104               (setcdr entry (cons number (cdr entry)))
6105             (gnus-sethash group (cons number nil) xref-hashtb)))))
6106     (and start xref-hashtb)))
6107
6108 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6109   "Look through all the headers and mark the Xrefs as read."
6110   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6111         name info xref-hashtb idlist method nth4)
6112     (with-current-buffer gnus-group-buffer
6113       (when (setq xref-hashtb
6114                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6115         (mapatoms
6116          (lambda (group)
6117            (unless (string= from-newsgroup (setq name (symbol-name group)))
6118              (setq idlist (symbol-value group))
6119              ;; Dead groups are not updated.
6120              (and (prog1
6121                       (setq info (gnus-get-info name))
6122                     (when (stringp (setq nth4 (gnus-info-method info)))
6123                       (setq nth4 (gnus-server-to-method nth4))))
6124                   ;; Only do the xrefs if the group has the same
6125                   ;; select method as the group we have just read.
6126                   (or (gnus-methods-equal-p
6127                        nth4 (gnus-find-method-for-group from-newsgroup))
6128                       virtual
6129                       (equal nth4 (setq method (gnus-find-method-for-group
6130                                                 from-newsgroup)))
6131                       (and (equal (car nth4) (car method))
6132                            (equal (nth 1 nth4) (nth 1 method))))
6133                   gnus-use-cross-reference
6134                   (or (not (eq gnus-use-cross-reference t))
6135                       virtual
6136                       ;; Only do cross-references on subscribed
6137                       ;; groups, if that is what is wanted.
6138                       (<= (gnus-info-level info) gnus-level-subscribed))
6139                   (gnus-group-make-articles-read name idlist))))
6140          xref-hashtb)))))
6141
6142 (defun gnus-compute-read-articles (group articles)
6143   (let* ((entry (gnus-group-entry group))
6144          (info (nth 2 entry))
6145          (active (gnus-active group))
6146          ninfo)
6147     (when entry
6148       ;; First peel off all invalid article numbers.
6149       (when active
6150         (let ((ids articles)
6151               id first)
6152           (while (setq id (pop ids))
6153             (when (and first (> id (cdr active)))
6154               ;; We'll end up in this situation in one particular
6155               ;; obscure situation.  If you re-scan a group and get
6156               ;; a new article that is cross-posted to a different
6157               ;; group that has not been re-scanned, you might get
6158               ;; crossposted article that has a higher number than
6159               ;; Gnus believes possible.  So we re-activate this
6160               ;; group as well.  This might mean doing the
6161               ;; crossposting thingy will *increase* the number
6162               ;; of articles in some groups.  Tsk, tsk.
6163               (setq active (or (gnus-activate-group group) active)))
6164             (when (or (> id (cdr active))
6165                       (< id (car active)))
6166               (setq articles (delq id articles))))))
6167       ;; If the read list is nil, we init it.
6168       (if (and active
6169                (null (gnus-info-read info))
6170                (> (car active) 1))
6171           (setq ninfo (cons 1 (1- (car active))))
6172         (setq ninfo (gnus-info-read info)))
6173       ;; Then we add the read articles to the range.
6174       (gnus-add-to-range
6175        ninfo (setq articles (sort articles '<))))))
6176
6177 (defun gnus-group-make-articles-read (group articles)
6178   "Update the info of GROUP to say that ARTICLES are read."
6179   (let* ((num 0)
6180          (entry (gnus-group-entry group))
6181          (info (nth 2 entry))
6182          (active (gnus-active group))
6183          range)
6184     (when entry
6185       (setq range (gnus-compute-read-articles group articles))
6186       (with-current-buffer gnus-group-buffer
6187         (gnus-undo-register
6188           `(progn
6189              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6190              (gnus-info-set-read ',info ',(gnus-info-read info))
6191              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6192              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6193              (gnus-group-update-group ,group t))))
6194       ;; Add the read articles to the range.
6195       (gnus-info-set-read info range)
6196       (gnus-request-set-mark group (list (list range 'add '(read))))
6197       ;; Then we have to re-compute how many unread
6198       ;; articles there are in this group.
6199       (when active
6200         (cond
6201          ((not range)
6202           (setq num (- (1+ (cdr active)) (car active))))
6203          ((not (listp (cdr range)))
6204           (setq num (- (cdr active) (- (1+ (cdr range))
6205                                        (car range)))))
6206          (t
6207           (while range
6208             (if (numberp (car range))
6209                 (setq num (1+ num))
6210               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6211             (setq range (cdr range)))
6212           (setq num (- (cdr active) num))))
6213         ;; Update the number of unread articles.
6214         (setcar entry num)
6215         ;; Update the group buffer.
6216         (unless (gnus-ephemeral-group-p group)
6217           (gnus-group-update-group group t))))))
6218
6219 (defvar gnus-newsgroup-none-id 0)
6220
6221 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6222   (let ((cur nntp-server-buffer)
6223         (dependencies
6224          (or dependencies
6225              (with-current-buffer gnus-summary-buffer
6226                gnus-newsgroup-dependencies)))
6227         headers id end ref number
6228         (mail-parse-charset gnus-newsgroup-charset)
6229         (mail-parse-ignored-charsets
6230          (save-current-buffer (condition-case nil
6231                                   (set-buffer gnus-summary-buffer)
6232                                 (error))
6233                               gnus-newsgroup-ignored-charsets)))
6234     (with-current-buffer nntp-server-buffer
6235       ;; Translate all TAB characters into SPACE characters.
6236       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6237       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6238       (ietf-drums-unfold-fws)
6239       (gnus-run-hooks 'gnus-parse-headers-hook)
6240       (let ((case-fold-search t)
6241             in-reply-to header p lines chars)
6242         (goto-char (point-min))
6243         ;; Search to the beginning of the next header.  Error messages
6244         ;; do not begin with 2 or 3.
6245         (while (re-search-forward "^[23][0-9]+ " nil t)
6246           (setq id nil
6247                 ref nil)
6248           ;; This implementation of this function, with nine
6249           ;; search-forwards instead of the one re-search-forward and
6250           ;; a case (which basically was the old function) is actually
6251           ;; about twice as fast, even though it looks messier.  You
6252           ;; can't have everything, I guess.  Speed and elegance
6253           ;; doesn't always go hand in hand.
6254           (setq
6255            header
6256            (vector
6257             ;; Number.
6258             (prog1
6259                 (setq number (read cur))
6260               (end-of-line)
6261               (setq p (point))
6262               (narrow-to-region (point)
6263                                 (or (and (search-forward "\n.\n" nil t)
6264                                          (- (point) 2))
6265                                     (point))))
6266             ;; Subject.
6267             (progn
6268               (goto-char p)
6269               (if (search-forward "\nsubject:" nil t)
6270                   (funcall gnus-decode-encoded-word-function
6271                            (nnheader-header-value))
6272                 "(none)"))
6273             ;; From.
6274             (progn
6275               (goto-char p)
6276               (if (search-forward "\nfrom:" nil t)
6277                   (funcall gnus-decode-encoded-address-function
6278                            (nnheader-header-value))
6279                 "(nobody)"))
6280             ;; Date.
6281             (progn
6282               (goto-char p)
6283               (if (search-forward "\ndate:" nil t)
6284                   (nnheader-header-value) ""))
6285             ;; Message-ID.
6286             (progn
6287               (goto-char p)
6288               (setq id (if (re-search-forward
6289                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6290                            ;; We do it this way to make sure the Message-ID
6291                            ;; is (somewhat) syntactically valid.
6292                            (buffer-substring (match-beginning 1)
6293                                              (match-end 1))
6294                          ;; If there was no message-id, we just fake one
6295                          ;; to make subsequent routines simpler.
6296                          (nnheader-generate-fake-message-id number))))
6297             ;; References.
6298             (progn
6299               (goto-char p)
6300               (if (search-forward "\nreferences:" nil t)
6301                   (progn
6302                     (setq end (point))
6303                     (prog1
6304                         (nnheader-header-value)
6305                       (setq ref
6306                             (buffer-substring
6307                              (progn
6308                                (end-of-line)
6309                                (search-backward ">" end t)
6310                                (1+ (point)))
6311                              (progn
6312                                (search-backward "<" end t)
6313                                (point))))))
6314                 ;; Get the references from the in-reply-to header if there
6315                 ;; were no references and the in-reply-to header looks
6316                 ;; promising.
6317                 (if (and (search-forward "\nin-reply-to:" nil t)
6318                          (setq in-reply-to (nnheader-header-value))
6319                          (string-match "<[^>]+>" in-reply-to))
6320                     (let (ref2)
6321                       (setq ref (substring in-reply-to (match-beginning 0)
6322                                            (match-end 0)))
6323                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6324                         (setq ref2 (substring in-reply-to (match-beginning 0)
6325                                               (match-end 0)))
6326                         (when (> (length ref2) (length ref))
6327                           (setq ref ref2)))
6328                       ref)
6329                   (setq ref nil))))
6330             ;; Chars.
6331             (progn
6332               (goto-char p)
6333               (if (search-forward "\nchars: " nil t)
6334                   (if (numberp (setq chars (ignore-errors (read cur))))
6335                       chars -1)
6336                 -1))
6337             ;; Lines.
6338             (progn
6339               (goto-char p)
6340               (if (search-forward "\nlines: " nil t)
6341                   (if (numberp (setq lines (ignore-errors (read cur))))
6342                       lines -1)
6343                 -1))
6344             ;; Xref.
6345             (progn
6346               (goto-char p)
6347               (and (search-forward "\nxref:" nil t)
6348                    (nnheader-header-value)))
6349             ;; Extra.
6350             (when gnus-extra-headers
6351               (let ((extra gnus-extra-headers)
6352                     out)
6353                 (while extra
6354                   (goto-char p)
6355                   (when (search-forward
6356                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6357                     (push (cons (car extra) (nnheader-header-value))
6358                           out))
6359                   (pop extra))
6360                 out))))
6361           (when (equal id ref)
6362             (setq ref nil))
6363
6364           (when gnus-alter-header-function
6365             (funcall gnus-alter-header-function header)
6366             (setq id (mail-header-id header)
6367                   ref (gnus-parent-id (mail-header-references header))))
6368
6369           (when (setq header
6370                       (gnus-dependencies-add-header
6371                        header dependencies force-new))
6372             (push header headers))
6373           (goto-char (point-max))
6374           (widen))
6375         (nreverse headers)))))
6376
6377 ;; Goes through the xover lines and returns a list of vectors
6378 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6379                                                   force-new dependencies
6380                                                   group also-fetch-heads)
6381   "Parse the news overview data in the server buffer.
6382 Return a list of headers that match SEQUENCE (see
6383 `nntp-retrieve-headers')."
6384   ;; Get the Xref when the users reads the articles since most/some
6385   ;; NNTP servers do not include Xrefs when using XOVER.
6386   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6387   (let ((mail-parse-charset gnus-newsgroup-charset)
6388         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6389         (cur nntp-server-buffer)
6390         (dependencies (or dependencies gnus-newsgroup-dependencies))
6391         (allp (cond
6392                ((eq gnus-read-all-available-headers t)
6393                 t)
6394                ((and (stringp gnus-read-all-available-headers)
6395                      group)
6396                 (string-match gnus-read-all-available-headers group))
6397                (t
6398                 nil)))
6399         number headers header)
6400     (with-current-buffer nntp-server-buffer
6401       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6402       ;; Allow the user to mangle the headers before parsing them.
6403       (gnus-run-hooks 'gnus-parse-headers-hook)
6404       (goto-char (point-min))
6405       (gnus-parse-without-error
6406         (while (and (or sequence allp)
6407                     (not (eobp)))
6408           (setq number (read cur))
6409           (when (not allp)
6410             (while (and sequence
6411                         (< (car sequence) number))
6412               (setq sequence (cdr sequence))))
6413           (when (and (or allp
6414                          (and sequence
6415                               (eq number (car sequence))))
6416                      (progn
6417                        (setq sequence (cdr sequence))
6418                        (setq header (inline
6419                                       (gnus-nov-parse-line
6420                                        number dependencies force-new)))))
6421             (push header headers))
6422           (forward-line 1)))
6423       ;; A common bug in inn is that if you have posted an article and
6424       ;; then retrieves the active file, it will answer correctly --
6425       ;; the new article is included.  However, a NOV entry for the
6426       ;; article may not have been generated yet, so this may fail.
6427       ;; We work around this problem by retrieving the last few
6428       ;; headers using HEAD.
6429       (if (or (not also-fetch-heads)
6430               (not sequence))
6431           ;; We (probably) got all the headers.
6432           (nreverse headers)
6433         (let ((gnus-nov-is-evil t))
6434           (nconc
6435            (nreverse headers)
6436            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6437              (gnus-get-newsgroup-headers))))))))
6438
6439 (defun gnus-article-get-xrefs ()
6440   "Fill in the Xref value in `gnus-current-headers', if necessary.
6441 This is meant to be called in `gnus-article-internal-prepare-hook'."
6442   (let ((headers (with-current-buffer gnus-summary-buffer
6443                    gnus-current-headers)))
6444     (or (not gnus-use-cross-reference)
6445         (not headers)
6446         (and (mail-header-xref headers)
6447              (not (string= (mail-header-xref headers) "")))
6448         (let ((case-fold-search t)
6449               xref)
6450           (save-restriction
6451             (nnheader-narrow-to-headers)
6452             (goto-char (point-min))
6453             (when (or (and (not (eobp))
6454                            (eq (downcase (char-after)) ?x)
6455                            (looking-at "Xref:"))
6456                       (search-forward "\nXref:" nil t))
6457               (goto-char (1+ (match-end 0)))
6458               (setq xref (buffer-substring (point) (point-at-eol)))
6459               (mail-header-set-xref headers xref)))))))
6460
6461 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6462   "Find article ID and insert the summary line for that article.
6463 OLD-HEADER can either be a header or a line number to insert
6464 the subject line on."
6465   (let* ((line (and (numberp old-header) old-header))
6466          (old-header (and (vectorp old-header) old-header))
6467          (header (cond ((and old-header use-old-header)
6468                         old-header)
6469                        ((and (numberp id)
6470                              (gnus-number-to-header id))
6471                         (gnus-number-to-header id))
6472                        (t
6473                         (gnus-read-header id))))
6474          (number (and (numberp id) id))
6475          d)
6476     (when header
6477       ;; Rebuild the thread that this article is part of and go to the
6478       ;; article we have fetched.
6479       (when (and (not gnus-show-threads)
6480                  old-header)
6481         (when (and number
6482                    (setq d (gnus-data-find (mail-header-number old-header))))
6483           (goto-char (gnus-data-pos d))
6484           (gnus-data-remove
6485            number
6486            (- (point-at-bol)
6487               (prog1
6488                   (1+ (point-at-eol))
6489                 (gnus-delete-line))))))
6490       ;; Remove list identifiers from subject.
6491       (when gnus-list-identifiers
6492         (let ((gnus-newsgroup-headers (list header)))
6493           (gnus-summary-remove-list-identifiers)))
6494       (when old-header
6495         (mail-header-set-number header (mail-header-number old-header)))
6496       (setq gnus-newsgroup-sparse
6497             (delq (setq number (mail-header-number header))
6498                   gnus-newsgroup-sparse))
6499       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6500       (push number gnus-newsgroup-limit)
6501       (gnus-rebuild-thread (mail-header-id header) line)
6502       (gnus-summary-goto-subject number nil t))
6503     (when (and (numberp number)
6504                (> number 0))
6505       ;; We have to update the boundaries even if we can't fetch the
6506       ;; article if ID is a number -- so that the next `P' or `N'
6507       ;; command will fetch the previous (or next) article even
6508       ;; if the one we tried to fetch this time has been canceled.
6509       (when (> number gnus-newsgroup-end)
6510         (setq gnus-newsgroup-end number))
6511       (when (< number gnus-newsgroup-begin)
6512         (setq gnus-newsgroup-begin number))
6513       (setq gnus-newsgroup-unselected
6514             (delq number gnus-newsgroup-unselected)))
6515     ;; Report back a success?
6516     (and header (mail-header-number header))))
6517
6518 ;;; Process/prefix in the summary buffer
6519
6520 (defun gnus-summary-work-articles (n)
6521   "Return a list of articles to be worked upon.
6522 The prefix argument, the list of process marked articles, and the
6523 current article will be taken into consideration."
6524   (with-current-buffer gnus-summary-buffer
6525     (cond
6526      (n
6527       ;; A numerical prefix has been given.
6528       (setq n (prefix-numeric-value n))
6529       (let ((backward (< n 0))
6530             (n (abs (prefix-numeric-value n)))
6531             articles article)
6532         (save-excursion
6533           (while
6534               (and (> n 0)
6535                    (push (setq article (gnus-summary-article-number))
6536                          articles)
6537                    (if backward
6538                        (gnus-summary-find-prev nil article)
6539                      (gnus-summary-find-next nil article)))
6540             (decf n)))
6541         (nreverse articles)))
6542      ((and (gnus-region-active-p) (mark))
6543       (message "region active")
6544       ;; Work on the region between point and mark.
6545       (let ((max (max (point) (mark)))
6546             articles article)
6547         (save-excursion
6548           (goto-char (min (point) (mark)))
6549           (while
6550               (and
6551                (push (setq article (gnus-summary-article-number)) articles)
6552                (gnus-summary-find-next nil article)
6553                (< (point) max)))
6554           (nreverse articles))))
6555      (gnus-newsgroup-processable
6556       ;; There are process-marked articles present.
6557       ;; Save current state.
6558       (gnus-summary-save-process-mark)
6559       ;; Return the list.
6560       (reverse gnus-newsgroup-processable))
6561      (t
6562       ;; Just return the current article.
6563       (list (gnus-summary-article-number))))))
6564
6565 (defmacro gnus-summary-iterate (arg &rest forms)
6566   "Iterate over the process/prefixed articles and do FORMS.
6567 ARG is the interactive prefix given to the command.  FORMS will be
6568 executed with point over the summary line of the articles."
6569   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6570     `(let ((,articles (gnus-summary-work-articles ,arg)))
6571        (while ,articles
6572          (gnus-summary-goto-subject (car ,articles))
6573          ,@forms
6574          (pop ,articles)))))
6575
6576 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6577 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6578
6579 (defun gnus-summary-save-process-mark ()
6580   "Push the current set of process marked articles on the stack."
6581   (interactive)
6582   (push (copy-sequence gnus-newsgroup-processable)
6583         gnus-newsgroup-process-stack))
6584
6585 (defun gnus-summary-kill-process-mark ()
6586   "Push the current set of process marked articles on the stack and unmark."
6587   (interactive)
6588   (gnus-summary-save-process-mark)
6589   (gnus-summary-unmark-all-processable))
6590
6591 (defun gnus-summary-yank-process-mark ()
6592   "Pop the last process mark state off the stack and restore it."
6593   (interactive)
6594   (unless gnus-newsgroup-process-stack
6595     (error "Empty mark stack"))
6596   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6597
6598 (defun gnus-summary-process-mark-set (set)
6599   "Make SET into the current process marked articles."
6600   (gnus-summary-unmark-all-processable)
6601   (mapc 'gnus-summary-set-process-mark set))
6602
6603 ;;; Searching and stuff
6604
6605 (defun gnus-summary-search-group (&optional backward use-level)
6606   "Search for next unread newsgroup.
6607 If optional argument BACKWARD is non-nil, search backward instead."
6608   (with-current-buffer gnus-group-buffer
6609     (when (gnus-group-search-forward
6610            backward nil (if use-level (gnus-group-group-level) nil))
6611       (gnus-group-group-name))))
6612
6613 (defun gnus-summary-best-group (&optional exclude-group)
6614   "Find the name of the best unread group.
6615 If EXCLUDE-GROUP, do not go to this group."
6616   (with-current-buffer gnus-group-buffer
6617     (save-excursion
6618       (gnus-group-best-unread-group exclude-group))))
6619
6620 (defun gnus-summary-find-next (&optional unread article backward)
6621   (if backward
6622       (gnus-summary-find-prev unread article)
6623     (let* ((dummy (gnus-summary-article-intangible-p))
6624            (article (or article (gnus-summary-article-number)))
6625            (data (gnus-data-find-list article))
6626            result)
6627       (when (and (not dummy)
6628                  (or (not gnus-summary-check-current)
6629                      (not unread)
6630                      (not (gnus-data-unread-p (car data)))))
6631         (setq data (cdr data)))
6632       (when (setq result
6633                   (if unread
6634                       (progn
6635                         (while data
6636                           (unless (memq (gnus-data-number (car data))
6637                                         (cond
6638                                          ((eq gnus-auto-goto-ignores
6639                                               'always-undownloaded)
6640                                           gnus-newsgroup-undownloaded)
6641                                          (gnus-plugged
6642                                           nil)
6643                                          ((eq gnus-auto-goto-ignores
6644                                               'unfetched)
6645                                           gnus-newsgroup-unfetched)
6646                                          ((eq gnus-auto-goto-ignores
6647                                               'undownloaded)
6648                                           gnus-newsgroup-undownloaded)))
6649                             (when (gnus-data-unread-p (car data))
6650                               (setq result (car data)
6651                                     data nil)))
6652                           (setq data (cdr data)))
6653                         result)
6654                     (car data)))
6655         (goto-char (gnus-data-pos result))
6656         (gnus-data-number result)))))
6657
6658 (defun gnus-summary-find-prev (&optional unread article)
6659   (let* ((eobp (eobp))
6660          (article (or article (gnus-summary-article-number)))
6661          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6662          result)
6663     (when (and (not eobp)
6664                (or (not gnus-summary-check-current)
6665                    (not unread)
6666                    (not (gnus-data-unread-p (car data)))))
6667       (setq data (cdr data)))
6668     (when (setq result
6669                 (if unread
6670                     (progn
6671                       (while data
6672                         (unless (memq (gnus-data-number (car data))
6673                                       (cond
6674                                        ((eq gnus-auto-goto-ignores
6675                                             'always-undownloaded)
6676                                         gnus-newsgroup-undownloaded)
6677                                        (gnus-plugged
6678                                         nil)
6679                                        ((eq gnus-auto-goto-ignores
6680                                             'unfetched)
6681                                         gnus-newsgroup-unfetched)
6682                                        ((eq gnus-auto-goto-ignores
6683                                             'undownloaded)
6684                                         gnus-newsgroup-undownloaded)))
6685                           (when (gnus-data-unread-p (car data))
6686                             (setq result (car data)
6687                                   data nil)))
6688                         (setq data (cdr data)))
6689                       result)
6690                   (car data)))
6691       (goto-char (gnus-data-pos result))
6692       (gnus-data-number result))))
6693
6694 (defun gnus-summary-find-subject (subject &optional unread backward article)
6695   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6696          (article (or article (gnus-summary-article-number)))
6697          (articles (gnus-data-list backward))
6698          (arts (gnus-data-find-list article articles))
6699          result)
6700     (when (or (not gnus-summary-check-current)
6701               (not unread)
6702               (not (gnus-data-unread-p (car arts))))
6703       (setq arts (cdr arts)))
6704     (while arts
6705       (and (or (not unread)
6706                (gnus-data-unread-p (car arts)))
6707            (vectorp (gnus-data-header (car arts)))
6708            (gnus-subject-equal
6709             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6710            (setq result (car arts)
6711                  arts nil))
6712       (setq arts (cdr arts)))
6713     (and result
6714          (goto-char (gnus-data-pos result))
6715          (gnus-data-number result))))
6716
6717 (defun gnus-summary-search-forward (&optional unread subject backward)
6718   "Search forward for an article.
6719 If UNREAD, look for unread articles.  If SUBJECT, look for
6720 articles with that subject.  If BACKWARD, search backward instead."
6721   (cond (subject (gnus-summary-find-subject subject unread backward))
6722         (backward (gnus-summary-find-prev unread))
6723         (t (gnus-summary-find-next unread))))
6724
6725 (defun gnus-recenter (&optional n)
6726   "Center point in window and redisplay frame.
6727 Also do horizontal recentering."
6728   (interactive "P")
6729   (when (and gnus-auto-center-summary
6730              (not (eq gnus-auto-center-summary 'vertical)))
6731     (gnus-horizontal-recenter))
6732   (if (fboundp 'recenter-top-bottom)
6733       (recenter-top-bottom n)
6734     (recenter n)))
6735
6736 (put 'gnus-recenter 'isearch-scroll t)
6737
6738 (defun gnus-forward-line-ignore-invisible (n)
6739   "Move N lines forward (backward if N is negative).
6740 Like forward-line, but skip over (and don't count) invisible lines."
6741   (let (done)
6742     (while (and (> n 0) (not done))
6743       ;; If the following character is currently invisible,
6744       ;; skip all characters with that same `invisible' property value.
6745       (while (gnus-invisible-p (point))
6746         (goto-char (gnus-next-char-property-change (point))))
6747       (forward-line 1)
6748       (if (eobp)
6749           (setq done t)
6750         (setq n (1- n))))
6751     (while (and (< n 0) (not done))
6752       (forward-line -1)
6753       (if (bobp) (setq done t)
6754         (setq n (1+ n))
6755         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6756           (goto-char (gnus-previous-char-property-change (point))))))))
6757
6758 (defun gnus-summary-recenter ()
6759   "Center point in the summary window.
6760 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6761 displayed, no centering will be performed."
6762   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6763   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6764   (interactive)
6765   ;; The user has to want it.
6766   (when gnus-auto-center-summary
6767     (let* ((top (cond ((< (window-height) 4) 0)
6768                       ((< (window-height) 7) 1)
6769                       (t (if (numberp gnus-auto-center-summary)
6770                              gnus-auto-center-summary
6771                            (/ (1- (window-height)) 2)))))
6772            (height (1- (window-height)))
6773            (bottom (save-excursion
6774                      (goto-char (point-max))
6775                      (gnus-forward-line-ignore-invisible (- height))
6776                      (point)))
6777            (window (get-buffer-window (current-buffer))))
6778       (when (get-buffer-window gnus-article-buffer)
6779         ;; Only do recentering when the article buffer is displayed,
6780         ;; Set the window start to either `bottom', which is the biggest
6781         ;; possible valid number, or the second line from the top,
6782         ;; whichever is the least.
6783         (let ((top-pos (save-excursion
6784                          (gnus-forward-line-ignore-invisible (- top))
6785                          (point))))
6786           (if (> bottom top-pos)
6787               ;; Keep the second line from the top visible
6788               (set-window-start window top-pos)
6789             ;; Try to keep the bottom line visible; if it's partially
6790             ;; obscured, either scroll one more line to make it fully
6791             ;; visible, or revert to using TOP-POS.
6792             (save-excursion
6793               (goto-char (point-max))
6794               (gnus-forward-line-ignore-invisible -1)
6795               (let ((last-line-start (point)))
6796                 (goto-char bottom)
6797                 (set-window-start window (point) t)
6798                 (when (not (pos-visible-in-window-p last-line-start window))
6799                   (gnus-forward-line-ignore-invisible 1)
6800                   (set-window-start window (min (point) top-pos) t)))))))
6801       ;; Do horizontal recentering while we're at it.
6802       (when (and (get-buffer-window (current-buffer) t)
6803                  (not (eq gnus-auto-center-summary 'vertical)))
6804         (let ((selected (selected-window)))
6805           (select-window (get-buffer-window (current-buffer) t))
6806           (gnus-summary-position-point)
6807           (gnus-horizontal-recenter)
6808           (select-window selected))))))
6809
6810 (defun gnus-summary-jump-to-group (newsgroup)
6811   "Move point to NEWSGROUP in group mode buffer."
6812   ;; Keep update point of group mode buffer if visible.
6813   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6814       (save-window-excursion
6815         ;; Take care of tree window mode.
6816         (when (get-buffer-window gnus-group-buffer)
6817           (pop-to-buffer gnus-group-buffer))
6818         (gnus-group-jump-to-group newsgroup))
6819     (save-excursion
6820       ;; Take care of tree window mode.
6821       (if (get-buffer-window gnus-group-buffer 0)
6822           (pop-to-buffer gnus-group-buffer)
6823         (set-buffer gnus-group-buffer))
6824       (gnus-group-jump-to-group newsgroup))))
6825
6826 ;; This function returns a list of article numbers based on the
6827 ;; difference between the ranges of read articles in this group and
6828 ;; the range of active articles.
6829 (defun gnus-list-of-unread-articles (group)
6830   (let* ((read (gnus-info-read (gnus-get-info group)))
6831          (active (or (gnus-active group) (gnus-activate-group group)))
6832          (last (or (cdr active)
6833                    (error "Group %s couldn't be activated " group)))
6834          (bottom (if gnus-newsgroup-maximum-articles
6835                      (max (car active)
6836                           (- last gnus-newsgroup-maximum-articles -1))
6837                    (car active)))
6838          first nlast unread)
6839     ;; If none are read, then all are unread.
6840     (if (not read)
6841         (setq first bottom)
6842       ;; If the range of read articles is a single range, then the
6843       ;; first unread article is the article after the last read
6844       ;; article.  Sounds logical, doesn't it?
6845       (if (and (not (listp (cdr read)))
6846                (or (< (car read) bottom)
6847                    (progn (setq read (list read))
6848                           nil)))
6849           (setq first (max bottom (1+ (cdr read))))
6850         ;; `read' is a list of ranges.
6851         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6852                                   (caar read)))
6853                   1)
6854           (setq first bottom))
6855         (while read
6856           (when first
6857             (while (< first nlast)
6858               (setq unread (cons first unread)
6859                     first (1+ first))))
6860           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6861           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6862           (setq read (cdr read)))))
6863     ;; And add the last unread articles.
6864     (while (<= first last)
6865       (setq unread (cons first unread)
6866             first (1+ first)))
6867     ;; Return the list of unread articles.
6868     (delq 0 (nreverse unread))))
6869
6870 (defun gnus-list-of-read-articles (group)
6871   "Return a list of unread, unticked and non-dormant articles."
6872   (let* ((info (gnus-get-info group))
6873          (marked (gnus-info-marks info))
6874          (active (gnus-active group)))
6875     (and info active
6876          (gnus-list-range-difference
6877           (gnus-list-range-difference
6878            (gnus-sorted-complement
6879             (gnus-uncompress-range
6880              (if gnus-newsgroup-maximum-articles
6881                  (cons (max (car active)
6882                             (- (cdr active)
6883                                gnus-newsgroup-maximum-articles
6884                                -1))
6885                        (cdr active))
6886                active))
6887             (gnus-list-of-unread-articles group))
6888            (cdr (assq 'dormant marked)))
6889           (cdr (assq 'tick marked))))))
6890
6891 ;; This function returns a sequence of article numbers based on the
6892 ;; difference between the ranges of read articles in this group and
6893 ;; the range of active articles.
6894 (defun gnus-sequence-of-unread-articles (group)
6895   (let* ((read (gnus-info-read (gnus-get-info group)))
6896          (active (or (gnus-active group) (gnus-activate-group group)))
6897          (last (cdr active))
6898          (bottom (if gnus-newsgroup-maximum-articles
6899                      (max (car active)
6900                           (- last gnus-newsgroup-maximum-articles -1))
6901                    (car active)))
6902          first nlast unread)
6903     ;; If none are read, then all are unread.
6904     (if (not read)
6905         (setq first bottom)
6906       ;; If the range of read articles is a single range, then the
6907       ;; first unread article is the article after the last read
6908       ;; article.  Sounds logical, doesn't it?
6909       (if (and (not (listp (cdr read)))
6910                (or (< (car read) bottom)
6911                    (progn (setq read (list read))
6912                           nil)))
6913           (setq first (max bottom (1+ (cdr read))))
6914         ;; `read' is a list of ranges.
6915         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6916                                   (caar read)))
6917                   1)
6918           (setq first bottom))
6919         (while read
6920           (when first
6921             (push (cons first nlast) unread))
6922           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6923           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6924           (setq read (cdr read)))))
6925     ;; And add the last unread articles.
6926     (cond ((not (and first last))
6927            nil)
6928           ((< first last)
6929            (push (cons first last) unread))
6930           ((= first last)
6931            (push first unread)))
6932     ;; Return the sequence of unread articles.
6933     (delq 0 (nreverse unread))))
6934
6935 ;; Various summary commands
6936
6937 (defun gnus-summary-select-article-buffer ()
6938   "Reconfigure windows to show article buffer."
6939   (interactive)
6940   (if (not (gnus-buffer-live-p gnus-article-buffer))
6941       (error "There is no article buffer for this summary buffer")
6942     (gnus-configure-windows 'article)
6943     (select-window (get-buffer-window gnus-article-buffer))))
6944
6945 (defun gnus-summary-universal-argument (arg)
6946   "Perform any operation on all articles that are process/prefixed."
6947   (interactive "P")
6948   (let ((articles (gnus-summary-work-articles arg))
6949         func article)
6950     (if (eq
6951          (setq
6952           func
6953           (key-binding
6954            (read-key-sequence
6955             (substitute-command-keys
6956              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6957          'undefined)
6958         (gnus-error 1 "Undefined key")
6959       (save-excursion
6960         (while articles
6961           (gnus-summary-goto-subject (setq article (pop articles)))
6962           (let (gnus-newsgroup-processable)
6963             (command-execute func))
6964           (gnus-summary-remove-process-mark article)))))
6965   (gnus-summary-position-point))
6966
6967 (defun gnus-summary-toggle-truncation (&optional arg)
6968   "Toggle truncation of summary lines.
6969 With ARG, turn line truncation on if ARG is positive."
6970   (interactive "P")
6971   (setq truncate-lines
6972         (if (null arg) (not truncate-lines)
6973           (> (prefix-numeric-value arg) 0)))
6974   (redraw-display))
6975
6976 (defun gnus-summary-find-for-reselect ()
6977   "Return the number of an article to stay on across a reselect.
6978 The current article is considered, then following articles, then previous
6979 articles.  An article is sought which is not cancelled and isn't a temporary
6980 insertion from another group.  If there's no such then return a dummy 0."
6981   (let (found)
6982     (dolist (rev '(nil t))
6983       (unless found      ; don't demand the reverse list if we don't need it
6984         (let ((data (gnus-data-find-list
6985                      (gnus-summary-article-number) (gnus-data-list rev))))
6986           (while (and data (not found))
6987             (if (and (< 0 (gnus-data-number (car data)))
6988                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6989                 (setq found (gnus-data-number (car data))))
6990             (setq data (cdr data))))))
6991     (or found 0)))
6992
6993 (defun gnus-summary-reselect-current-group (&optional all rescan)
6994   "Exit and then reselect the current newsgroup.
6995 The prefix argument ALL means to select all articles."
6996   (interactive "P")
6997   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6998     (error "Ephemeral groups can't be reselected"))
6999   (let ((current-subject (gnus-summary-find-for-reselect))
7000         (group gnus-newsgroup-name))
7001     (setq gnus-newsgroup-begin nil)
7002     (gnus-summary-exit nil 'leave-hidden)
7003     ;; We have to adjust the point of group mode buffer because
7004     ;; point was moved to the next unread newsgroup by exiting.
7005     (gnus-summary-jump-to-group group)
7006     (when rescan
7007       (save-excursion
7008         (gnus-group-get-new-news-this-group 1)))
7009     (gnus-group-read-group all t)
7010     (gnus-summary-goto-subject current-subject nil t)))
7011
7012 (defun gnus-summary-rescan-group (&optional all)
7013   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7014   (interactive "P")
7015   (gnus-summary-reselect-current-group all t))
7016
7017 (defun gnus-summary-update-info (&optional non-destructive)
7018   (save-excursion
7019     (let ((group gnus-newsgroup-name))
7020       (when group
7021         (when gnus-newsgroup-kill-headers
7022           (setq gnus-newsgroup-killed
7023                 (gnus-compress-sequence
7024                  (gnus-sorted-union
7025                   (gnus-list-range-intersection
7026                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7027                   gnus-newsgroup-unreads)
7028                  t)))
7029         (unless (listp (cdr gnus-newsgroup-killed))
7030           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7031         (let ((headers gnus-newsgroup-headers))
7032           ;; Set the new ranges of read articles.
7033           (with-current-buffer gnus-group-buffer
7034             (gnus-undo-force-boundary))
7035           (gnus-update-read-articles
7036            group (gnus-sorted-union
7037                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7038           ;; Set the current article marks.
7039           (let ((gnus-newsgroup-scored
7040                  (if (and (not gnus-save-score)
7041                           (not non-destructive))
7042                      nil
7043                    gnus-newsgroup-scored)))
7044             (save-excursion
7045               (gnus-update-marks)))
7046           ;; Do the cross-ref thing.
7047           (when gnus-use-cross-reference
7048             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7049           ;; Do not switch windows but change the buffer to work.
7050           (set-buffer gnus-group-buffer)
7051           (unless (gnus-ephemeral-group-p group)
7052             (gnus-group-update-group group)))))))
7053
7054 (defun gnus-summary-save-newsrc (&optional force)
7055   "Save the current number of read/marked articles in the dribble buffer.
7056 The dribble buffer will then be saved.
7057 If FORCE (the prefix), also save the .newsrc file(s)."
7058   (interactive "P")
7059   (gnus-summary-update-info t)
7060   (if force
7061       (gnus-save-newsrc-file)
7062     (gnus-dribble-save)))
7063
7064 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7065
7066 (defun gnus-summary-exit (&optional temporary leave-hidden)
7067   "Exit reading current newsgroup, and then return to group selection mode.
7068 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7069   (interactive)
7070   (gnus-set-global-variables)
7071   (when (gnus-buffer-live-p gnus-article-buffer)
7072     (with-current-buffer gnus-article-buffer
7073       (mm-destroy-parts gnus-article-mime-handles)
7074       ;; Set it to nil for safety reason.
7075       (setq gnus-article-mime-handle-alist nil)
7076       (setq gnus-article-mime-handles nil)))
7077   (gnus-kill-save-kill-buffer)
7078   (gnus-async-halt-prefetch)
7079   (let* ((group gnus-newsgroup-name)
7080          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7081          (gnus-group-is-exiting-p t)
7082          (mode major-mode)
7083          (group-point nil)
7084          (buf (current-buffer)))
7085     (unless quit-config
7086       ;; Do adaptive scoring, and possibly save score files.
7087       (when gnus-newsgroup-adaptive
7088         (gnus-score-adaptive))
7089       (when gnus-use-scoring
7090         (gnus-score-save)))
7091     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7092     ;; If we have several article buffers, we kill them at exit.
7093     (unless gnus-single-article-buffer
7094       (when (gnus-buffer-live-p gnus-article-buffer)
7095         (with-current-buffer gnus-article-buffer
7096           ;; Don't kill sticky article buffers
7097           (unless (eq major-mode 'gnus-sticky-article-mode)
7098             (gnus-kill-buffer gnus-article-buffer)
7099             (setq gnus-article-current nil))))
7100       (gnus-kill-buffer gnus-original-article-buffer))
7101     (when gnus-use-cache
7102       (gnus-cache-possibly-remove-articles)
7103       (gnus-cache-save-buffers))
7104     (gnus-async-prefetch-remove-group group)
7105     (when gnus-suppress-duplicates
7106       (gnus-dup-enter-articles))
7107     (when gnus-use-trees
7108       (gnus-tree-close group))
7109     (when gnus-use-cache
7110       (gnus-cache-write-active))
7111     ;; Remove entries for this group.
7112     (nnmail-purge-split-history (gnus-group-real-name group))
7113     ;; Make all changes in this group permanent.
7114     (unless quit-config
7115       (gnus-run-hooks 'gnus-exit-group-hook)
7116       (gnus-summary-update-info))
7117     (gnus-close-group group)
7118     ;; Make sure where we were, and go to next newsgroup.
7119     (set-buffer gnus-group-buffer)
7120     (unless quit-config
7121       (gnus-group-jump-to-group group))
7122     (gnus-run-hooks 'gnus-summary-exit-hook)
7123     (unless (or quit-config
7124                 (not gnus-summary-next-group-on-exit)
7125                 ;; If this group has disappeared from the summary
7126                 ;; buffer, don't skip forwards.
7127                 (not (string= group (gnus-group-group-name))))
7128       (gnus-group-next-unread-group 1))
7129     (setq group-point (point))
7130     (if temporary
7131         nil                             ;Nothing to do.
7132       (set-buffer buf)
7133       (if (not gnus-kill-summary-on-exit)
7134           (progn
7135             (gnus-deaden-summary)
7136             (setq mode nil))
7137         ;; We set all buffer-local variables to nil.  It is unclear why
7138         ;; this is needed, but if we don't, buffer-local variables are
7139         ;; not garbage-collected, it seems.  This would the lead to en
7140         ;; ever-growing Emacs.
7141         (gnus-summary-clear-local-variables)
7142         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7143           (gnus-summary-clear-local-variables))
7144         (when (get-buffer gnus-article-buffer)
7145           (bury-buffer gnus-article-buffer))
7146         ;; Return to group mode buffer.
7147         (when (eq mode 'gnus-summary-mode)
7148           (gnus-kill-buffer buf)))
7149       (setq gnus-current-select-method gnus-select-method)
7150       (set-buffer gnus-group-buffer)
7151       (if quit-config
7152           (gnus-handle-ephemeral-exit quit-config)
7153         (goto-char group-point)
7154         ;; If gnus-group-buffer is already displayed, make sure we also move
7155         ;; the cursor in the window that displays it.
7156         (let ((win (get-buffer-window (current-buffer) 0)))
7157           (if win (set-window-point win (point))))
7158         (unless leave-hidden
7159           (gnus-configure-windows 'group 'force)))
7160       ;; Clear the current group name.
7161       (unless quit-config
7162         (setq gnus-newsgroup-name nil)))))
7163
7164 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7165 (defun gnus-summary-exit-no-update (&optional no-questions)
7166   "Quit reading current newsgroup without updating read article info."
7167   (interactive)
7168   (let* ((group gnus-newsgroup-name)
7169          (gnus-group-is-exiting-p t)
7170          (gnus-group-is-exiting-without-update-p t)
7171          (quit-config (gnus-group-quit-config group)))
7172     (when (or no-questions
7173               gnus-expert-user
7174               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7175       (gnus-async-halt-prefetch)
7176       (run-hooks 'gnus-summary-prepare-exit-hook)
7177       (when (gnus-buffer-live-p gnus-article-buffer)
7178         (with-current-buffer gnus-article-buffer
7179           (mm-destroy-parts gnus-article-mime-handles)
7180           ;; Set it to nil for safety reason.
7181           (setq gnus-article-mime-handle-alist nil)
7182           (setq gnus-article-mime-handles nil)))
7183       ;; If we have several article buffers, we kill them at exit.
7184       (unless gnus-single-article-buffer
7185         (gnus-kill-buffer gnus-article-buffer)
7186         (gnus-kill-buffer gnus-original-article-buffer)
7187         (setq gnus-article-current nil))
7188       (if (not gnus-kill-summary-on-exit)
7189           (gnus-deaden-summary)
7190         (gnus-close-group group)
7191         (gnus-summary-clear-local-variables)
7192         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7193           (gnus-summary-clear-local-variables))
7194         (gnus-kill-buffer gnus-summary-buffer))
7195       (unless gnus-single-article-buffer
7196         (setq gnus-article-current nil))
7197       (when gnus-use-trees
7198         (gnus-tree-close group))
7199       (gnus-async-prefetch-remove-group group)
7200       (when (get-buffer gnus-article-buffer)
7201         (bury-buffer gnus-article-buffer))
7202       ;; Return to the group buffer.
7203       (gnus-configure-windows 'group 'force)
7204       ;; Clear the current group name.
7205       (setq gnus-newsgroup-name nil)
7206       (unless (gnus-ephemeral-group-p group)
7207         (gnus-group-update-group group))
7208       (when (equal (gnus-group-group-name) group)
7209         (gnus-group-next-unread-group 1))
7210       (when quit-config
7211         (gnus-handle-ephemeral-exit quit-config)))))
7212
7213 (defun gnus-handle-ephemeral-exit (quit-config)
7214   "Handle movement when leaving an ephemeral group.
7215 The state which existed when entering the ephemeral is reset."
7216   (if (not (buffer-name (car quit-config)))
7217       (gnus-configure-windows 'group 'force)
7218     (set-buffer (car quit-config))
7219     (cond ((eq major-mode 'gnus-summary-mode)
7220            (gnus-set-global-variables))
7221           ((eq major-mode 'gnus-article-mode)
7222            (save-current-buffer
7223              ;; The `gnus-summary-buffer' variable may point
7224              ;; to the old summary buffer when using a single
7225              ;; article buffer.
7226              (unless (gnus-buffer-live-p gnus-summary-buffer)
7227                (set-buffer gnus-group-buffer))
7228              (set-buffer gnus-summary-buffer)
7229              (gnus-set-global-variables))))
7230     (if (or (eq (cdr quit-config) 'article)
7231             (eq (cdr quit-config) 'pick))
7232         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7233             (gnus-configure-windows 'pick 'force)
7234           (gnus-configure-windows (cdr quit-config) 'force))
7235       (gnus-configure-windows (cdr quit-config) 'force))
7236     (when (eq major-mode 'gnus-summary-mode)
7237       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7238                                                      next-unread-noselect))
7239           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7240                                   'next-noselect)
7241                               (gnus-summary-next-subject 1 nil t))
7242                              ((eq gnus-auto-select-on-ephemeral-exit
7243                                   'next-unread-noselect)
7244                               (gnus-summary-next-subject 1 t t))))
7245             ;; Hide the article buffer which displays the article different
7246             ;; from the one that the cursor points to in the summary buffer.
7247             (gnus-configure-windows 'summary 'force))
7248         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7249                (gnus-summary-next-subject 1))
7250               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7251                (gnus-summary-next-subject 1 t))))
7252       (gnus-summary-recenter)
7253       (gnus-summary-position-point))))
7254
7255 ;;; Dead summaries.
7256
7257 (defvar gnus-dead-summary-mode-map
7258   (let ((map (make-keymap)))
7259     (suppress-keymap map)
7260     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7261     (dolist (key '("\C-d" "\r" "\177" [delete]))
7262       (define-key map key 'gnus-summary-wake-up-the-dead))
7263     (dolist (key '("q" "Q"))
7264       (define-key map key 'bury-buffer))
7265     map))
7266
7267 (define-minor-mode gnus-dead-summary-mode
7268   "Minor mode for Gnus summary buffers."
7269   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7270   (unless (derived-mode-p 'gnus-summary-mode)
7271     (setq gnus-dead-summary-mode nil)))
7272
7273 (defun gnus-deaden-summary ()
7274   "Make the current summary buffer into a dead summary buffer."
7275   ;; Kill any previous dead summary buffer.
7276   (when (and gnus-dead-summary
7277              (buffer-name gnus-dead-summary))
7278     (with-current-buffer gnus-dead-summary
7279       (when gnus-dead-summary-mode
7280         (kill-buffer (current-buffer)))))
7281   ;; Make this the current dead summary.
7282   (setq gnus-dead-summary (current-buffer))
7283   (gnus-dead-summary-mode 1)
7284   (let ((name (buffer-name)))
7285     (when (string-match "Summary" name)
7286       (rename-buffer
7287        (concat (substring name 0 (match-beginning 0)) "Dead "
7288                (substring name (match-beginning 0)))
7289        t)
7290       (bury-buffer))))
7291
7292 (defun gnus-kill-or-deaden-summary (buffer)
7293   "Kill or deaden the summary BUFFER."
7294   (save-excursion
7295     (when (and (buffer-name buffer)
7296                (not gnus-single-article-buffer))
7297       (with-current-buffer buffer
7298         (gnus-kill-buffer gnus-article-buffer)
7299         (gnus-kill-buffer gnus-original-article-buffer)))
7300     (cond
7301      ;; Kill the buffer.
7302      (gnus-kill-summary-on-exit
7303       (when (and gnus-use-trees
7304                  (gnus-buffer-exists-p buffer))
7305         (with-current-buffer buffer
7306           (gnus-tree-close gnus-newsgroup-name)))
7307       (gnus-kill-buffer buffer))
7308      ;; Deaden the buffer.
7309      ((gnus-buffer-exists-p buffer)
7310       (with-current-buffer buffer
7311         (gnus-deaden-summary))))))
7312
7313 (defun gnus-summary-wake-up-the-dead (&rest args)
7314   "Wake up the dead summary buffer."
7315   (interactive)
7316   (gnus-dead-summary-mode -1)
7317   (let ((name (buffer-name)))
7318     (when (string-match "Dead " name)
7319       (rename-buffer
7320        (concat (substring name 0 (match-beginning 0))
7321                (substring name (match-end 0)))
7322        t)))
7323   (gnus-message 3 "This dead summary is now alive again"))
7324
7325 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7326 (defun gnus-summary-describe-group (&optional force)
7327   "Describe the current newsgroup."
7328   (interactive "P")
7329   (gnus-group-describe-group force gnus-newsgroup-name))
7330
7331 (defun gnus-summary-describe-briefly ()
7332   "Describe summary mode commands briefly."
7333   (interactive)
7334   (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")))
7335
7336 ;; Walking around group mode buffer from summary mode.
7337
7338 (defun gnus-summary-next-group (&optional no-article target-group backward)
7339   "Exit current newsgroup and then select next unread newsgroup.
7340 If prefix argument NO-ARTICLE is non-nil, no article is selected
7341 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7342 previous group instead."
7343   (interactive "P")
7344   ;; Stop pre-fetching.
7345   (gnus-async-halt-prefetch)
7346   (let ((current-group gnus-newsgroup-name)
7347         (current-buffer (current-buffer))
7348         entered)
7349     ;; First we semi-exit this group to update Xrefs and all variables.
7350     ;; We can't do a real exit, because the window conf must remain
7351     ;; the same in case the user is prompted for info, and we don't
7352     ;; want the window conf to change before that...
7353     (gnus-summary-exit t)
7354     (while (not entered)
7355       ;; Then we find what group we are supposed to enter.
7356       (set-buffer gnus-group-buffer)
7357       (gnus-group-jump-to-group current-group)
7358       (setq target-group
7359             (or target-group
7360                 (if (eq gnus-keep-same-level 'best)
7361                     (gnus-summary-best-group gnus-newsgroup-name)
7362                   (gnus-summary-search-group backward gnus-keep-same-level))))
7363       (if (not target-group)
7364           ;; There are no further groups, so we return to the group
7365           ;; buffer.
7366           (progn
7367             (gnus-message 5 "Returning to the group buffer")
7368             (setq entered t)
7369             (when (gnus-buffer-live-p current-buffer)
7370               (set-buffer current-buffer)
7371               (gnus-summary-exit))
7372             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7373         ;; We try to enter the target group.
7374         (gnus-group-jump-to-group target-group)
7375         (let ((unreads (gnus-group-group-unread)))
7376           (if (and (or (eq t unreads)
7377                        (and unreads (not (zerop unreads))))
7378                    (gnus-summary-read-group
7379                     target-group nil no-article
7380                     (and (buffer-name current-buffer) current-buffer)
7381                     nil backward))
7382               (setq entered t)
7383             (setq current-group target-group
7384                   target-group nil)))))))
7385
7386 (defun gnus-summary-prev-group (&optional no-article)
7387   "Exit current newsgroup and then select previous unread newsgroup.
7388 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7389   (interactive "P")
7390   (gnus-summary-next-group no-article nil t))
7391
7392 ;; Walking around summary lines.
7393
7394 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7395   "Go to the first subject satisfying any non-nil constraint.
7396 If UNREAD is non-nil, the article should be unread.
7397 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7398 If UNSEEN is non-nil, the article should be unseen as well as unread.
7399 Returns the article selected or nil if there are no matching articles."
7400   (interactive "P")
7401   (cond
7402    ;; Empty summary.
7403    ((null gnus-newsgroup-data)
7404     (gnus-message 3 "No articles in the group")
7405     nil)
7406    ;; Pick the first article.
7407    ((not (or unread undownloaded unseen))
7408     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7409     (gnus-data-number (car gnus-newsgroup-data)))
7410    ;; Find the first unread article.
7411    (t
7412     (let ((data gnus-newsgroup-data))
7413       (while (and data
7414                   (let ((num (gnus-data-number (car data))))
7415                     (or (memq num gnus-newsgroup-unfetched)
7416                         (not (or (and unread
7417                                       (memq num gnus-newsgroup-unreads))
7418                                  (and undownloaded
7419                                       (memq num gnus-newsgroup-undownloaded))
7420                                  (and unseen
7421                                       (memq num gnus-newsgroup-unseen)
7422                                       (memq num gnus-newsgroup-unreads)))))))
7423         (setq data (cdr data)))
7424       (prog1
7425           (if data
7426               (progn
7427                 (goto-char (gnus-data-pos (car data)))
7428                 (gnus-data-number (car data)))
7429             (gnus-message 3 "No more%s articles"
7430                           (let* ((r (when unread " unread"))
7431                                  (d (when undownloaded " undownloaded"))
7432                                  (s (when unseen " unseen"))
7433                                  (l (delq nil (list r d s))))
7434                             (cond ((= 3 (length l))
7435                                    (concat r "," d ", or" s))
7436                                   ((= 2 (length l))
7437                                    (concat (car l) ", or" (cadr l)))
7438                                   ((= 1 (length l))
7439                                    (car l))
7440                                   (t
7441                                    ""))))
7442             nil
7443             )
7444         (gnus-summary-position-point))))))
7445
7446 (defun gnus-summary-next-subject (n &optional unread dont-display)
7447   "Go to next N'th summary line.
7448 If N is negative, go to the previous N'th subject line.
7449 If UNREAD is non-nil, only unread articles are selected.
7450 The difference between N and the actual number of steps taken is
7451 returned."
7452   (interactive "p")
7453   (let ((backward (< n 0))
7454         (n (abs n)))
7455     (while (and (> n 0)
7456                 (if backward
7457                     (gnus-summary-find-prev unread)
7458                   (gnus-summary-find-next unread)))
7459       (unless (zerop (setq n (1- n)))
7460         (gnus-summary-show-thread)))
7461     (when (/= 0 n)
7462       (gnus-message 7 "No more%s articles"
7463                     (if unread " unread" "")))
7464     (unless dont-display
7465       (gnus-summary-recenter)
7466       (gnus-summary-position-point))
7467     n))
7468
7469 (defun gnus-summary-next-unread-subject (n)
7470   "Go to next N'th unread summary line."
7471   (interactive "p")
7472   (gnus-summary-next-subject n t))
7473
7474 (defun gnus-summary-prev-subject (n &optional unread)
7475   "Go to previous N'th summary line.
7476 If optional argument UNREAD is non-nil, only unread article is selected."
7477   (interactive "p")
7478   (gnus-summary-next-subject (- n) unread))
7479
7480 (defun gnus-summary-prev-unread-subject (n)
7481   "Go to previous N'th unread summary line."
7482   (interactive "p")
7483   (gnus-summary-next-subject (- n) t))
7484
7485 (defun gnus-summary-goto-subjects (articles)
7486   "Insert the subject header for ARTICLES in the current buffer."
7487   (save-excursion
7488     (dolist (article articles)
7489       (gnus-summary-goto-subject article t)))
7490   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7491   (gnus-summary-position-point))
7492
7493 (defun gnus-summary-goto-subject (article &optional force silent)
7494   "Go to the subject line of ARTICLE.
7495 If FORCE, also allow jumping to articles not currently shown."
7496   (interactive "nArticle number: ")
7497   (unless (numberp article)
7498     (error "Article %s is not a number" article))
7499   (let ((b (point))
7500         (data (gnus-data-find article)))
7501     ;; We read in the article if we have to.
7502     (and (not data)
7503          force
7504          (gnus-summary-insert-subject
7505           article
7506           (if (or (numberp force) (vectorp force)) force)
7507           t)
7508          (setq data (gnus-data-find article)))
7509     (goto-char b)
7510     (if (not data)
7511         (progn
7512           (unless silent
7513             (gnus-message 3 "Can't find article %d" article))
7514           nil)
7515       (let ((pt (gnus-data-pos data)))
7516         (goto-char pt)
7517         (gnus-summary-set-article-display-arrow pt))
7518       (gnus-summary-position-point)
7519       article)))
7520
7521 ;; Walking around summary lines with displaying articles.
7522
7523 (defun gnus-summary-expand-window (&optional arg)
7524   "Make the summary buffer take up the entire Emacs frame.
7525 Given a prefix, will force an `article' buffer configuration."
7526   (interactive "P")
7527   (if arg
7528       (gnus-configure-windows 'article 'force)
7529     (gnus-configure-windows 'summary 'force)))
7530
7531 (defun gnus-summary-display-article (article &optional all-header)
7532   "Display ARTICLE in article buffer."
7533   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7534                (with-current-buffer gnus-article-buffer
7535                  (eq major-mode 'gnus-article-mode)))
7536     (gnus-article-setup-buffer))
7537   (gnus-set-global-variables)
7538   (with-current-buffer gnus-article-buffer
7539     (setq gnus-article-charset gnus-newsgroup-charset)
7540     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7541     (mm-enable-multibyte))
7542   (if (null article)
7543       nil
7544     (prog1
7545         (if gnus-summary-display-article-function
7546             (funcall gnus-summary-display-article-function article all-header)
7547           (gnus-article-prepare article all-header))
7548       (gnus-run-hooks 'gnus-select-article-hook)
7549       (when (and gnus-current-article
7550                  (not (zerop gnus-current-article)))
7551         (gnus-summary-goto-subject gnus-current-article))
7552       (gnus-summary-recenter)
7553       (when (and gnus-use-trees gnus-show-threads)
7554         (gnus-possibly-generate-tree article)
7555         (gnus-highlight-selected-tree article))
7556       ;; Successfully display article.
7557       (gnus-article-set-window-start
7558        (cdr (assq article gnus-newsgroup-bookmarks))))))
7559
7560 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7561   "Select the current article.
7562 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7563 non-nil, the article will be re-fetched even if it already present in
7564 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7565 be displayed."
7566   ;; Make sure we are in the summary buffer to work around bbdb bug.
7567   (unless (eq major-mode 'gnus-summary-mode)
7568     (set-buffer gnus-summary-buffer))
7569   (let ((article (or article (gnus-summary-article-number)))
7570         (all-headers (not (not all-headers))) ;Must be t or nil.
7571         gnus-summary-display-article-function)
7572     (and (not pseudo)
7573          (gnus-summary-article-pseudo-p article)
7574          (error "This is a pseudo-article"))
7575     (with-current-buffer gnus-summary-buffer
7576       (if (or (and gnus-single-article-buffer
7577                    (or (null gnus-current-article)
7578                        (null gnus-article-current)
7579                        (null (get-buffer gnus-article-buffer))
7580                        (not (eq article (cdr gnus-article-current)))
7581                        (not (equal (car gnus-article-current)
7582                                    gnus-newsgroup-name))))
7583               (and (not gnus-single-article-buffer)
7584                    (or (null gnus-current-article)
7585                        (not (eq gnus-current-article article))))
7586               force)
7587           ;; The requested article is different from the current article.
7588           (progn
7589             (gnus-summary-display-article article all-headers)
7590             (when (gnus-buffer-live-p gnus-article-buffer)
7591               (with-current-buffer gnus-article-buffer
7592                 (if (not gnus-article-decoded-p) ;; a local variable
7593                     (mm-disable-multibyte))))
7594             (gnus-article-set-window-start
7595              (cdr (assq article gnus-newsgroup-bookmarks)))
7596             article)
7597         'old))))
7598
7599 (defun gnus-summary-force-verify-and-decrypt ()
7600   "Display buttons for signed/encrypted parts and verify/decrypt them."
7601   (interactive)
7602   (let ((mm-verify-option 'known)
7603         (mm-decrypt-option 'known)
7604         (gnus-article-emulate-mime t)
7605         (gnus-buttonized-mime-types (append (list "multipart/signed"
7606                                                   "multipart/encrypted")
7607                                             gnus-buttonized-mime-types)))
7608     (gnus-summary-select-article nil 'force)))
7609
7610 (defun gnus-summary-set-current-mark (&optional current-mark)
7611   "Obsolete function."
7612   nil)
7613
7614 (defun gnus-summary-next-article (&optional unread subject backward push)
7615   "Select the next article.
7616 If UNREAD, only unread articles are selected.
7617 If SUBJECT, only articles with SUBJECT are selected.
7618 If BACKWARD, the previous article is selected instead of the next."
7619   (interactive "P")
7620   ;; Make sure we are in the summary buffer.
7621   (unless (eq major-mode 'gnus-summary-mode)
7622     (set-buffer gnus-summary-buffer))
7623   (cond
7624    ;; Is there such an article?
7625    ((and (gnus-summary-search-forward unread subject backward)
7626          (or (gnus-summary-display-article (gnus-summary-article-number))
7627              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7628     (gnus-summary-position-point))
7629    ;; If not, we try the first unread, if that is wanted.
7630    ((and subject
7631          gnus-auto-select-same
7632          (gnus-summary-first-unread-article))
7633     (gnus-summary-position-point)
7634     (gnus-message 6 "Wrapped"))
7635    ;; Try to get next/previous article not displayed in this group.
7636    ((and gnus-auto-extend-newsgroup
7637          (not unread) (not subject))
7638     (gnus-summary-goto-article
7639      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7640      nil (count-lines (point-min) (point))))
7641    ;; Go to next/previous group.
7642    (t
7643     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7644       (gnus-summary-jump-to-group gnus-newsgroup-name))
7645     (let ((cmd (if (featurep 'xemacs)
7646                    last-command-char
7647                  last-command-event))
7648           (point
7649            (with-current-buffer gnus-group-buffer
7650              (point)))
7651           (group
7652            (if (eq gnus-keep-same-level 'best)
7653                (gnus-summary-best-group gnus-newsgroup-name)
7654              (gnus-summary-search-group backward gnus-keep-same-level))))
7655       ;; For some reason, the group window gets selected.  We change
7656       ;; it back.
7657       (select-window (get-buffer-window (current-buffer)))
7658       ;; Select next unread newsgroup automagically.
7659       (cond
7660        ((or (not gnus-auto-select-next)
7661             (not cmd))
7662         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7663        ((or (eq gnus-auto-select-next 'quietly)
7664             (and (eq gnus-auto-select-next 'slightly-quietly)
7665                  push)
7666             (and (eq gnus-auto-select-next 'almost-quietly)
7667                  (gnus-summary-last-article-p)))
7668         ;; Select quietly.
7669         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7670             (gnus-summary-exit)
7671           (gnus-message 7 "No more%s articles (%s)..."
7672                         (if unread " unread" "")
7673                         (if group (concat "selecting " group)
7674                           "exiting"))
7675           (gnus-summary-next-group nil group backward)))
7676        (t
7677         (when (gnus-key-press-event-p last-input-event)
7678           (gnus-summary-walk-group-buffer
7679            gnus-newsgroup-name cmd unread backward point))))))))
7680
7681 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7682   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7683                       (?\C-p (gnus-group-prev-unread-group 1))))
7684         (cursor-in-echo-area t)
7685         keve key group ended prompt)
7686     (with-current-buffer gnus-group-buffer
7687       (goto-char start)
7688       (setq group
7689             (if (eq gnus-keep-same-level 'best)
7690                 (gnus-summary-best-group gnus-newsgroup-name)
7691               (gnus-summary-search-group backward gnus-keep-same-level))))
7692     (while (not ended)
7693       (setq prompt
7694             (format
7695              "No more%s articles%s " (if unread " unread" "")
7696              (if (and group
7697                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7698                  (format " (Type %s for %s [%s])"
7699                          (single-key-description cmd)
7700                          (gnus-group-decoded-name group)
7701                          (gnus-group-unread group))
7702                (format " (Type %s to exit %s)"
7703                        (single-key-description cmd)
7704                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7705       ;; Confirm auto selection.
7706       (setq key (car (setq keve (gnus-read-event-char prompt)))
7707             ended t)
7708       (cond
7709        ((assq key keystrokes)
7710         (let ((obuf (current-buffer)))
7711           (switch-to-buffer gnus-group-buffer)
7712           (when group
7713             (gnus-group-jump-to-group group))
7714           (eval (cadr (assq key keystrokes)))
7715           (setq group (gnus-group-group-name))
7716           (switch-to-buffer obuf))
7717         (setq ended nil))
7718        ((equal key cmd)
7719         (if (or (not group)
7720                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7721             (gnus-summary-exit)
7722           (gnus-summary-next-group nil group backward)))
7723        (t
7724         (push (cdr keve) unread-command-events))))))
7725
7726 (defun gnus-summary-next-unread-article ()
7727   "Select unread article after current one."
7728   (interactive)
7729   (gnus-summary-next-article
7730    (or (not (eq gnus-summary-goto-unread 'never))
7731        (gnus-summary-last-article-p (gnus-summary-article-number)))
7732    (and gnus-auto-select-same
7733         (gnus-summary-article-subject))))
7734
7735 (defun gnus-summary-prev-article (&optional unread subject)
7736   "Select the article before the current one.
7737 If UNREAD is non-nil, only unread articles are selected."
7738   (interactive "P")
7739   (gnus-summary-next-article unread subject t))
7740
7741 (defun gnus-summary-prev-unread-article ()
7742   "Select unread article before current one."
7743   (interactive)
7744   (gnus-summary-prev-article
7745    (or (not (eq gnus-summary-goto-unread 'never))
7746        (gnus-summary-first-article-p (gnus-summary-article-number)))
7747    (and gnus-auto-select-same
7748         (gnus-summary-article-subject))))
7749
7750 (defun gnus-summary-next-page (&optional lines circular stop)
7751   "Show next page of the selected article.
7752 If at the end of the current article, select the next article.
7753 LINES says how many lines should be scrolled up.
7754
7755 If CIRCULAR is non-nil, go to the start of the article instead of
7756 selecting the next article when reaching the end of the current
7757 article.
7758
7759 If STOP is non-nil, just stop when reaching the end of the message.
7760
7761 Also see the variable `gnus-article-skip-boring'."
7762   (interactive "P")
7763   (setq gnus-summary-buffer (current-buffer))
7764   (gnus-set-global-variables)
7765   (let ((article (gnus-summary-article-number))
7766         (article-window (get-buffer-window gnus-article-buffer t))
7767         endp)
7768     ;; If the buffer is empty, we have no article.
7769     (unless article
7770       (error "No article to select"))
7771     (gnus-configure-windows 'article)
7772     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7773         (if (and (eq gnus-summary-goto-unread 'never)
7774                  (not (gnus-summary-last-article-p article)))
7775             (gnus-summary-next-article)
7776           (gnus-summary-next-unread-article))
7777       (if (or (null gnus-current-article)
7778               (null gnus-article-current)
7779               (/= article (cdr gnus-article-current))
7780               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7781           ;; Selected subject is different from current article's.
7782           (gnus-summary-display-article article)
7783         (when article-window
7784           (gnus-eval-in-buffer-window gnus-article-buffer
7785             (setq endp (or (gnus-article-next-page lines)
7786                            (gnus-article-only-boring-p))))
7787           (when endp
7788             (cond ((or stop gnus-summary-stop-at-end-of-message)
7789                    (gnus-message 3 "End of message"))
7790                   (circular
7791                    (gnus-summary-beginning-of-article))
7792                   (lines
7793                    (gnus-message 3 "End of message"))
7794                   ((null lines)
7795                    (if (and (eq gnus-summary-goto-unread 'never)
7796                             (not (gnus-summary-last-article-p article)))
7797                        (gnus-summary-next-article)
7798                      (gnus-summary-next-unread-article))))))))
7799     (gnus-summary-recenter)
7800     (gnus-summary-position-point)))
7801
7802 (defun gnus-summary-prev-page (&optional lines move)
7803   "Show previous page of selected article.
7804 Argument LINES specifies lines to be scrolled down.
7805 If MOVE, move to the previous unread article if point is at
7806 the beginning of the buffer."
7807   (interactive "P")
7808   (let ((article (gnus-summary-article-number))
7809         (article-window (get-buffer-window gnus-article-buffer t))
7810         endp)
7811     (gnus-configure-windows 'article)
7812     (if (or (null gnus-current-article)
7813             (null gnus-article-current)
7814             (/= article (cdr gnus-article-current))
7815             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7816         ;; Selected subject is different from current article's.
7817         (gnus-summary-display-article article)
7818       (gnus-summary-recenter)
7819       (when article-window
7820         (gnus-eval-in-buffer-window gnus-article-buffer
7821           (setq endp (gnus-article-prev-page lines)))
7822         (when (and move endp)
7823           (cond (lines
7824                  (gnus-message 3 "Beginning of message"))
7825                 ((null lines)
7826                  (if (and (eq gnus-summary-goto-unread 'never)
7827                           (not (gnus-summary-first-article-p article)))
7828                      (gnus-summary-prev-article)
7829                    (gnus-summary-prev-unread-article))))))))
7830   (gnus-summary-position-point))
7831
7832 (defun gnus-summary-prev-page-or-article (&optional lines)
7833   "Show previous page of selected article.
7834 Argument LINES specifies lines to be scrolled down.
7835 If at the beginning of the article, go to the next article."
7836   (interactive "P")
7837   (gnus-summary-prev-page lines t))
7838
7839 (defun gnus-summary-scroll-up (lines)
7840   "Scroll up (or down) one line current article.
7841 Argument LINES specifies lines to be scrolled up (or down if negative)."
7842   (interactive "p")
7843   (gnus-configure-windows 'article)
7844   (gnus-summary-show-thread)
7845   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7846     (gnus-eval-in-buffer-window gnus-article-buffer
7847       (cond ((> lines 0)
7848              (when (gnus-article-next-page lines)
7849                (gnus-message 3 "End of message")))
7850             ((< lines 0)
7851              (gnus-article-prev-page (- lines))))))
7852   (gnus-summary-recenter)
7853   (gnus-summary-position-point))
7854
7855 (defun gnus-summary-scroll-down (lines)
7856   "Scroll down (or up) one line current article.
7857 Argument LINES specifies lines to be scrolled down (or up if negative)."
7858   (interactive "p")
7859   (gnus-summary-scroll-up (- lines)))
7860
7861 (defun gnus-summary-next-same-subject ()
7862   "Select next article which has the same subject as current one."
7863   (interactive)
7864   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7865
7866 (defun gnus-summary-prev-same-subject ()
7867   "Select previous article which has the same subject as current one."
7868   (interactive)
7869   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7870
7871 (defun gnus-summary-next-unread-same-subject ()
7872   "Select next unread article which has the same subject as current one."
7873   (interactive)
7874   (gnus-summary-next-article t (gnus-summary-article-subject)))
7875
7876 (defun gnus-summary-prev-unread-same-subject ()
7877   "Select previous unread article which has the same subject as current one."
7878   (interactive)
7879   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7880
7881 (defun gnus-summary-first-unread-article ()
7882   "Select the first unread article.
7883 Return nil if there are no unread articles."
7884   (interactive)
7885   (prog1
7886       (when (gnus-summary-first-subject t)
7887         (gnus-summary-show-thread)
7888         (gnus-summary-first-subject t)
7889         (gnus-summary-display-article (gnus-summary-article-number)))
7890     (gnus-summary-position-point)))
7891
7892 (defun gnus-summary-first-unread-subject ()
7893   "Place the point on the subject line of the first unread article.
7894 Return nil if there are no unread articles."
7895   (interactive)
7896   (prog1
7897       (when (gnus-summary-first-subject t)
7898         (gnus-summary-show-thread)
7899         (gnus-summary-first-subject t))
7900     (gnus-summary-position-point)))
7901
7902 (defun gnus-summary-first-unseen-subject ()
7903   "Place the point on the subject line of the first unseen article.
7904 Return nil if there are no unseen articles."
7905   (interactive)
7906   (prog1
7907       (when (gnus-summary-first-subject nil nil t)
7908         (gnus-summary-show-thread)
7909         (gnus-summary-first-subject nil nil t))
7910     (gnus-summary-position-point)))
7911
7912 (defun gnus-summary-first-unseen-or-unread-subject ()
7913   "Place the point on the subject line of the first unseen and unread article.
7914 If all article have been seen, on the subject line of the first unread
7915 article."
7916   (interactive)
7917   (prog1
7918       (unless (when (gnus-summary-first-subject nil nil t)
7919                 (gnus-summary-show-thread)
7920                 (gnus-summary-first-subject nil nil t))
7921         (when (gnus-summary-first-subject t)
7922           (gnus-summary-show-thread)
7923           (gnus-summary-first-subject t)))
7924     (gnus-summary-position-point)))
7925
7926 (defun gnus-summary-first-article ()
7927   "Select the first article.
7928 Return nil if there are no articles."
7929   (interactive)
7930   (prog1
7931       (when (gnus-summary-first-subject)
7932         (gnus-summary-show-thread)
7933         (gnus-summary-first-subject)
7934         (gnus-summary-display-article (gnus-summary-article-number)))
7935     (gnus-summary-position-point)))
7936
7937 (defun gnus-summary-best-unread-article (&optional arg)
7938   "Select the unread article with the highest score.
7939 If given a prefix argument, select the next unread article that has a
7940 score higher than the default score."
7941   (interactive "P")
7942   (let ((article (if arg
7943                      (gnus-summary-better-unread-subject)
7944                    (gnus-summary-best-unread-subject))))
7945     (if article
7946         (gnus-summary-goto-article article)
7947       (error "No unread articles"))))
7948
7949 (defun gnus-summary-best-unread-subject ()
7950   "Select the unread subject with the highest score."
7951   (interactive)
7952   (let ((best -1000000)
7953         (data gnus-newsgroup-data)
7954         article score)
7955     (while data
7956       (and (gnus-data-unread-p (car data))
7957            (> (setq score
7958                     (gnus-summary-article-score (gnus-data-number (car data))))
7959               best)
7960            (setq best score
7961                  article (gnus-data-number (car data))))
7962       (setq data (cdr data)))
7963     (when article
7964       (gnus-summary-goto-subject article))
7965     (gnus-summary-position-point)
7966     article))
7967
7968 (defun gnus-summary-better-unread-subject ()
7969   "Select the first unread subject that has a score over the default score."
7970   (interactive)
7971   (let ((data gnus-newsgroup-data)
7972         article score)
7973     (while (and (setq article (gnus-data-number (car data)))
7974                 (or (gnus-data-read-p (car data))
7975                     (not (> (gnus-summary-article-score article)
7976                             gnus-summary-default-score))))
7977       (setq data (cdr data)))
7978     (when article
7979       (gnus-summary-goto-subject article))
7980     (gnus-summary-position-point)
7981     article))
7982
7983 (defun gnus-summary-last-subject ()
7984   "Go to the last displayed subject line in the group."
7985   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7986     (when article
7987       (gnus-summary-goto-subject article))))
7988
7989 (defun gnus-summary-goto-article (article &optional all-headers force)
7990   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7991 If ALL-HEADERS is non-nil, no header lines are hidden.
7992 If FORCE, go to the article even if it isn't displayed.  If FORCE
7993 is a number, it is the line the article is to be displayed on."
7994   (interactive
7995    (list
7996     (completing-read
7997      "Article number or Message-ID: "
7998      (mapcar (lambda (number) (list (int-to-string number)))
7999              gnus-newsgroup-limit))
8000     current-prefix-arg
8001     t))
8002   (prog1
8003       (if (and (stringp article)
8004                (string-match "@\\|%40" article))
8005           (gnus-summary-refer-article article)
8006         (when (stringp article)
8007           (setq article (string-to-number article)))
8008         (if (gnus-summary-goto-subject article force)
8009             (gnus-summary-display-article article all-headers)
8010           (gnus-message 4 "Couldn't go to article %s" article) nil))
8011     (gnus-summary-position-point)))
8012
8013 (defun gnus-summary-goto-last-article ()
8014   "Go to the previously read article."
8015   (interactive)
8016   (prog1
8017       (when gnus-last-article
8018         (gnus-summary-goto-article gnus-last-article nil t))
8019     (gnus-summary-position-point)))
8020
8021 (defun gnus-summary-pop-article (number)
8022   "Pop one article off the history and go to the previous.
8023 NUMBER articles will be popped off."
8024   (interactive "p")
8025   (let (to)
8026     (setq gnus-newsgroup-history
8027           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8028     (if to
8029         (gnus-summary-goto-article (car to) nil t)
8030       (error "Article history empty")))
8031   (gnus-summary-position-point))
8032
8033 ;; Summary commands and functions for limiting the summary buffer.
8034
8035 (defun gnus-summary-limit-to-articles (n)
8036   "Limit the summary buffer to the next N articles.
8037 If not given a prefix, use the process marked articles instead."
8038   (interactive "P")
8039   (prog1
8040       (let ((articles (gnus-summary-work-articles n)))
8041         (setq gnus-newsgroup-processable nil)
8042         (gnus-summary-limit articles))
8043     (gnus-summary-position-point)))
8044
8045 (defun gnus-summary-pop-limit (&optional total)
8046   "Restore the previous limit.
8047 If given a prefix, remove all limits."
8048   (interactive "P")
8049   (when total
8050     (setq gnus-newsgroup-limits
8051           (list (mapcar (lambda (h) (mail-header-number h))
8052                         gnus-newsgroup-headers))))
8053   (unless gnus-newsgroup-limits
8054     (error "No limit to pop"))
8055   (prog1
8056       (gnus-summary-limit nil 'pop)
8057     (gnus-summary-position-point)))
8058
8059 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8060   "Limit the summary buffer to articles that have subjects that match a regexp.
8061 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8062   (interactive
8063    (list (read-string (if current-prefix-arg
8064                           "Exclude subject (regexp): "
8065                         "Limit to subject (regexp): "))
8066          nil current-prefix-arg))
8067   (unless header
8068     (setq header "subject"))
8069   (when (not (equal "" subject))
8070     (prog1
8071         (let ((articles (gnus-summary-find-matching
8072                          (or header "subject") subject 'all nil nil
8073                          not-matching)))
8074           (unless articles
8075             (error "Found no matches for \"%s\"" subject))
8076           (gnus-summary-limit articles))
8077       (gnus-summary-position-point))))
8078
8079 (defun gnus-summary-limit-to-author (from &optional not-matching)
8080   "Limit the summary buffer to articles that have authors that match a regexp.
8081 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8082   (interactive
8083    (list (read-string (if current-prefix-arg
8084                           "Exclude author (regexp): "
8085                         "Limit to author (regexp): "))
8086          current-prefix-arg))
8087   (gnus-summary-limit-to-subject from "from" not-matching))
8088
8089 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8090   "Limit the summary buffer to articles with the given RECIPIENT.
8091
8092 If NOT-MATCHING, exclude RECIPIENT.
8093
8094 To and Cc headers are checked.  You need to include them in
8095 `nnmail-extra-headers'."
8096   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8097   (interactive
8098    (list (read-string (format "%s recipient (regexp): "
8099                               (if current-prefix-arg "Exclude" "Limit to")))
8100          current-prefix-arg))
8101   (when (not (equal "" recipient))
8102     (prog1 (let* ((to
8103                    (if (memq 'To nnmail-extra-headers)
8104                        (gnus-summary-find-matching
8105                         (cons 'extra 'To) recipient 'all nil nil
8106                         not-matching)
8107                      (gnus-message
8108                       1 "`To' isn't present in `nnmail-extra-headers'")
8109                      (sit-for 1)
8110                      nil))
8111                   (cc
8112                    (if (memq 'Cc nnmail-extra-headers)
8113                        (gnus-summary-find-matching
8114                         (cons 'extra 'Cc) recipient 'all nil nil
8115                         not-matching)
8116                      (gnus-message
8117                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8118                      (sit-for 1)
8119                      nil))
8120                   (articles
8121                    (if not-matching
8122                        ;; We need the numbers that are in both lists:
8123                        (mapcar (lambda (a)
8124                                  (and (memq a to) a))
8125                                cc)
8126                      (nconc to cc))))
8127              (unless articles
8128                (error "Found no matches for \"%s\"" recipient))
8129              (gnus-summary-limit articles))
8130       (gnus-summary-position-point))))
8131
8132 (defun gnus-summary-limit-to-address (address &optional not-matching)
8133   "Limit the summary buffer to articles with the given ADDRESS.
8134
8135 If NOT-MATCHING, exclude ADDRESS.
8136
8137 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8138 in `nnmail-extra-headers'."
8139   (interactive
8140    (list (read-string (format "%s address (regexp): "
8141                               (if current-prefix-arg "Exclude" "Limit to")))
8142          current-prefix-arg))
8143   (when (not (equal "" address))
8144     (prog1 (let* ((to
8145                    (if (memq 'To nnmail-extra-headers)
8146                        (gnus-summary-find-matching
8147                         (cons 'extra 'To) address 'all nil nil
8148                         not-matching)
8149                      (gnus-message
8150                       1 "`To' isn't present in `nnmail-extra-headers'")
8151                      (sit-for 1)
8152                      t))
8153                   (cc
8154                    (if (memq 'Cc nnmail-extra-headers)
8155                        (gnus-summary-find-matching
8156                         (cons 'extra 'Cc) address 'all nil nil
8157                         not-matching)
8158                      (gnus-message
8159                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8160                      (sit-for 1)
8161                      t))
8162                   (from
8163                    (gnus-summary-find-matching "from" address
8164                                                'all nil nil not-matching))
8165                   (articles
8166                    (if not-matching
8167                        ;; We need the numbers that are in all lists:
8168                        (if (eq cc t)
8169                            (if (eq to t)
8170                                from
8171                              (mapcar (lambda (a) (car (memq a from))) to))
8172                          (if (eq to t)
8173                              (mapcar (lambda (a) (car (memq a from))) cc)
8174                            (mapcar (lambda (a) (car (memq a from)))
8175                                    (mapcar (lambda (a) (car (memq a to)))
8176                                            cc))))
8177                      (nconc (if (eq to t) nil to)
8178                             (if (eq cc t) nil cc)
8179                             from))))
8180              (unless articles
8181                (error "Found no matches for \"%s\"" address))
8182              (gnus-summary-limit articles))
8183       (gnus-summary-position-point))))
8184
8185 (defun gnus-summary-limit-strange-charsets-predicate (header)
8186   (when (fboundp 'char-charset)
8187     (let ((string (concat (mail-header-subject header)
8188                           (mail-header-from header)))
8189           charset found)
8190       (dotimes (i (1- (length string)))
8191         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8192         (when (string-match "unicode\\|big\\|japanese" charset)
8193           (setq found t)))
8194       found)))
8195
8196 (defun gnus-summary-limit-to-predicate (predicate)
8197   "Limit to articles where PREDICATE returns non-nil.
8198 PREDICATE will be called with the header structures of the
8199 articles."
8200   (let ((articles nil)
8201         (case-fold-search t))
8202     (dolist (header gnus-newsgroup-headers)
8203       (when (funcall predicate header)
8204         (push (mail-header-number header) articles)))
8205     (gnus-summary-limit (nreverse articles))))
8206
8207 (defun gnus-summary-limit-to-age (age &optional younger-p)
8208   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8209 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8210 articles that are younger than AGE days."
8211   (interactive
8212    (let ((younger current-prefix-arg)
8213          (days-got nil)
8214          days)
8215      (while (not days-got)
8216        (setq days (if younger
8217                       (read-string "Limit to articles younger than (in days, older when negative): ")
8218                     (read-string
8219                      "Limit to articles older than (in days, younger when negative): ")))
8220        (when (> (length days) 0)
8221          (setq days (read days)))
8222        (if (numberp days)
8223            (progn
8224              (setq days-got t)
8225              (when (< days 0)
8226                (setq younger (not younger))
8227                (setq days (* days -1))))
8228          (message "Please enter a number.")
8229          (sleep-for 1)))
8230      (list days younger)))
8231   (prog1
8232       (let ((data gnus-newsgroup-data)
8233             (cutoff (days-to-time age))
8234             articles d date is-younger)
8235         (while (setq d (pop data))
8236           (when (and (vectorp (gnus-data-header d))
8237                      (setq date (mail-header-date (gnus-data-header d))))
8238             (setq is-younger (time-less-p
8239                               (time-since (gnus-date-get-time date))
8240                               cutoff))
8241             (when (if younger-p
8242                       is-younger
8243                     (not is-younger))
8244               (push (gnus-data-number d) articles))))
8245         (gnus-summary-limit (nreverse articles)))
8246     (gnus-summary-position-point)))
8247
8248 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8249   "Limit the summary buffer to articles that match an 'extra' header."
8250   (interactive
8251    (let ((header
8252           (intern
8253            (gnus-completing-read-with-default
8254             (symbol-name (car gnus-extra-headers))
8255             (if current-prefix-arg
8256                 "Exclude extra header"
8257               "Limit extra header")
8258             (mapcar (lambda (x)
8259                       (cons (symbol-name x) x))
8260                     gnus-extra-headers)
8261             nil
8262             t))))
8263      (list header
8264            (read-string (format "%s header %s (regexp): "
8265                                 (if current-prefix-arg "Exclude" "Limit to")
8266                                 header))
8267            current-prefix-arg)))
8268   (when (not (equal "" regexp))
8269     (prog1
8270         (let ((articles (gnus-summary-find-matching
8271                          (cons 'extra header) regexp 'all nil nil
8272                          not-matching)))
8273           (unless articles
8274             (error "Found no matches for \"%s\"" regexp))
8275           (gnus-summary-limit articles))
8276       (gnus-summary-position-point))))
8277
8278 (defun gnus-summary-limit-to-display-predicate ()
8279   "Limit the summary buffer to the predicated in the `display' group parameter."
8280   (interactive)
8281   (unless gnus-newsgroup-display
8282     (error "There is no `display' group parameter"))
8283   (let (articles)
8284     (dolist (number gnus-newsgroup-articles)
8285       (when (funcall gnus-newsgroup-display)
8286         (push number articles)))
8287     (gnus-summary-limit articles))
8288   (gnus-summary-position-point))
8289
8290 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8291 (make-obsolete
8292  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread "Emacs 20.4")
8293
8294 (defun gnus-summary-limit-to-unread (&optional all)
8295   "Limit the summary buffer to articles that are not marked as read.
8296 If ALL is non-nil, limit strictly to unread articles."
8297   (interactive "P")
8298   (if all
8299       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8300     (gnus-summary-limit-to-marks
8301      ;; Concat all the marks that say that an article is read and have
8302      ;; those removed.
8303      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8304            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8305            gnus-low-score-mark gnus-expirable-mark
8306            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8307            gnus-duplicate-mark)
8308      'reverse)))
8309
8310 (defun gnus-summary-limit-to-headers (match &optional reverse)
8311   "Limit the summary buffer to articles that have headers that match MATCH.
8312 If REVERSE (the prefix), limit to articles that don't match."
8313   (interactive "sMatch headers (regexp): \nP")
8314   (gnus-summary-limit-to-bodies match reverse t))
8315
8316 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8317   "Limit the summary buffer to articles that have bodies that match MATCH.
8318 If REVERSE (the prefix), limit to articles that don't match."
8319   (interactive "sMatch body (regexp): \nP")
8320   (let ((articles nil)
8321         (gnus-select-article-hook nil)  ;Disable hook.
8322         (gnus-article-prepare-hook nil)
8323         (gnus-use-article-prefetch nil)
8324         (gnus-keep-backlog nil)
8325         (gnus-break-pages nil)
8326         (gnus-summary-display-arrow nil)
8327         (gnus-updated-mode-lines nil)
8328         (gnus-auto-center-summary nil)
8329         (gnus-display-mime-function nil))
8330     (dolist (data gnus-newsgroup-data)
8331       (let (gnus-mark-article-hook)
8332         (gnus-summary-select-article t t nil (gnus-data-number data)))
8333       (with-current-buffer gnus-article-buffer
8334         (article-goto-body)
8335         (let* ((case-fold-search t)
8336                (found (if headersp
8337                           (re-search-backward match nil t)
8338                         (re-search-forward match nil t))))
8339           (when (or (and found
8340                          (not reverse))
8341                     (and (not found)
8342                          reverse))
8343             (push (gnus-data-number data) articles)))))
8344     (if (not articles)
8345         (message "No messages matched")
8346       (gnus-summary-limit articles)))
8347   (gnus-summary-position-point))
8348
8349 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8350   "Limit the summary buffer to articles that aren't part on any thread.
8351 If THREADSP (the prefix), limit to articles that are in threads."
8352   (interactive "P")
8353   (let ((articles nil)
8354         thread-articles
8355         threads)
8356     (dolist (thread gnus-newsgroup-threads)
8357       (if (stringp (car thread))
8358           (dolist (thread (cdr thread))
8359             (push thread threads))
8360         (push thread threads)))
8361     (dolist (thread threads)
8362       (setq thread-articles (gnus-articles-in-thread thread))
8363       (when (or (and threadsp
8364                      (> (length thread-articles) 1))
8365                 (and (not threadsp)
8366                      (= (length thread-articles) 1)))
8367         (setq articles (nconc thread-articles articles))))
8368     (if (not articles)
8369         (message "No messages matched")
8370       (gnus-summary-limit articles))
8371     (gnus-summary-position-point)))
8372
8373 (defun gnus-summary-limit-to-replied (&optional unreplied)
8374   "Limit the summary buffer to replied articles.
8375 If UNREPLIED (the prefix), limit to unreplied articles."
8376   (interactive "P")
8377   (if unreplied
8378       (gnus-summary-limit
8379        (gnus-set-difference gnus-newsgroup-articles
8380         gnus-newsgroup-replied))
8381     (gnus-summary-limit gnus-newsgroup-replied))
8382   (gnus-summary-position-point))
8383
8384 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8385 (make-obsolete 'gnus-summary-delete-marked-with
8386                'gnus-summary-limit-exclude-marks "Emacs 20.4")
8387
8388 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8389   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8390 If REVERSE, limit the summary buffer to articles that are marked
8391 with MARKS.  MARKS can either be a string of marks or a list of marks.
8392 Returns how many articles were removed."
8393   (interactive "sMarks: ")
8394   (gnus-summary-limit-to-marks marks t))
8395
8396 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8397   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8398 If REVERSE (the prefix), limit the summary buffer to articles that are
8399 not marked with MARKS.  MARKS can either be a string of marks or a
8400 list of marks.
8401 Returns how many articles were removed."
8402   (interactive "sMarks: \nP")
8403   (prog1
8404       (let ((data gnus-newsgroup-data)
8405             (marks (if (listp marks) marks
8406                      (append marks nil))) ; Transform to list.
8407             articles)
8408         (while data
8409           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8410                   (memq (gnus-data-mark (car data)) marks))
8411             (push (gnus-data-number (car data)) articles))
8412           (setq data (cdr data)))
8413         (gnus-summary-limit articles))
8414     (gnus-summary-position-point)))
8415
8416 (defun gnus-summary-limit-to-score (score)
8417   "Limit to articles with score at or above SCORE."
8418   (interactive "NLimit to articles with score of at least: ")
8419   (let ((data gnus-newsgroup-data)
8420         articles)
8421     (while data
8422       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8423                 score)
8424         (push (gnus-data-number (car data)) articles))
8425       (setq data (cdr data)))
8426     (prog1
8427         (gnus-summary-limit articles)
8428       (gnus-summary-position-point))))
8429
8430 (defun gnus-summary-limit-to-unseen ()
8431   "Limit to unseen articles."
8432   (interactive)
8433   (prog1
8434       (gnus-summary-limit gnus-newsgroup-unseen)
8435     (gnus-summary-position-point)))
8436
8437 (defun gnus-summary-limit-include-thread (id)
8438   "Display all the hidden articles that is in the thread with ID in it.
8439 When called interactively, ID is the Message-ID of the current
8440 article."
8441   (interactive (list (mail-header-id (gnus-summary-article-header))))
8442   (let ((articles (gnus-articles-in-thread
8443                    (gnus-id-to-thread (gnus-root-id id)))))
8444     (prog1
8445         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8446       (gnus-summary-limit-include-matching-articles
8447        "subject"
8448        (regexp-quote (gnus-simplify-subject-re
8449                       (mail-header-subject (gnus-id-to-header id)))))
8450       (gnus-summary-position-point))))
8451
8452 (defun gnus-summary-limit-include-matching-articles (header regexp)
8453   "Display all the hidden articles that have HEADERs that match REGEXP."
8454   (interactive (list (read-string "Match on header: ")
8455                      (read-string "Regexp: ")))
8456   (let ((articles (gnus-find-matching-articles header regexp)))
8457     (prog1
8458         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8459       (gnus-summary-position-point))))
8460
8461 (defun gnus-summary-insert-dormant-articles ()
8462   "Insert all the dormant articles for this group into the current buffer."
8463   (interactive)
8464   (let ((gnus-verbose (max 6 gnus-verbose)))
8465     (if (not gnus-newsgroup-dormant)
8466         (gnus-message 3 "No dormant articles for this group")
8467       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8468
8469 (defun gnus-summary-insert-ticked-articles ()
8470   "Insert ticked articles for this group into the current buffer."
8471   (interactive)
8472   (let ((gnus-verbose (max 6 gnus-verbose)))
8473     (if (not gnus-newsgroup-marked)
8474         (gnus-message 3 "No ticked articles for this group")
8475       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8476
8477 (defun gnus-summary-limit-include-dormant ()
8478   "Display all the hidden articles that are marked as dormant.
8479 Note that this command only works on a subset of the articles currently
8480 fetched for this group."
8481   (interactive)
8482   (unless gnus-newsgroup-dormant
8483     (error "There are no dormant articles in this group"))
8484   (prog1
8485       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8486     (gnus-summary-position-point)))
8487
8488 (defun gnus-summary-limit-exclude-dormant ()
8489   "Hide all dormant articles."
8490   (interactive)
8491   (prog1
8492       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8493     (gnus-summary-position-point)))
8494
8495 (defun gnus-summary-limit-exclude-childless-dormant ()
8496   "Hide all dormant articles that have no children."
8497   (interactive)
8498   (let ((data (gnus-data-list t))
8499         articles d children)
8500     ;; Find all articles that are either not dormant or have
8501     ;; children.
8502     (while (setq d (pop data))
8503       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8504                 (and (setq children
8505                            (gnus-article-children (gnus-data-number d)))
8506                      (let (found)
8507                        (while children
8508                          (when (memq (car children) articles)
8509                            (setq children nil
8510                                  found t))
8511                          (pop children))
8512                        found)))
8513         (push (gnus-data-number d) articles)))
8514     ;; Do the limiting.
8515     (prog1
8516         (gnus-summary-limit articles)
8517       (gnus-summary-position-point))))
8518
8519 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8520   "Mark all unread excluded articles as read.
8521 If ALL, mark even excluded ticked and dormants as read."
8522   (interactive "P")
8523   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8524   (let ((articles (gnus-sorted-ndifference
8525                    (sort
8526                     (mapcar (lambda (h) (mail-header-number h))
8527                             gnus-newsgroup-headers)
8528                     '<)
8529                    gnus-newsgroup-limit))
8530         article)
8531     (setq gnus-newsgroup-unreads
8532           (gnus-sorted-intersection gnus-newsgroup-unreads
8533                                     gnus-newsgroup-limit))
8534     (if all
8535         (setq gnus-newsgroup-dormant nil
8536               gnus-newsgroup-marked nil
8537               gnus-newsgroup-reads
8538               (nconc
8539                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8540                gnus-newsgroup-reads))
8541       (while (setq article (pop articles))
8542         (unless (or (memq article gnus-newsgroup-dormant)
8543                     (memq article gnus-newsgroup-marked))
8544           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8545
8546 (defun gnus-summary-limit (articles &optional pop)
8547   (if pop
8548       ;; We pop the previous limit off the stack and use that.
8549       (setq articles (car gnus-newsgroup-limits)
8550             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8551     ;; We use the new limit, so we push the old limit on the stack.
8552     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8553   ;; Set the limit.
8554   (setq gnus-newsgroup-limit articles)
8555   (let ((total (length gnus-newsgroup-data))
8556         (data (gnus-data-find-list (gnus-summary-article-number)))
8557         (gnus-summary-mark-below nil)   ; Inhibit this.
8558         found)
8559     ;; This will do all the work of generating the new summary buffer
8560     ;; according to the new limit.
8561     (gnus-summary-prepare)
8562     ;; Hide any threads, possibly.
8563     (gnus-summary-maybe-hide-threads)
8564     ;; Try to return to the article you were at, or one in the
8565     ;; neighborhood.
8566     (when data
8567       ;; We try to find some article after the current one.
8568       (while data
8569         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8570           (setq data nil
8571                 found t))
8572         (setq data (cdr data))))
8573     (unless found
8574       ;; If there is no data, that means that we were after the last
8575       ;; article.  The same goes when we can't find any articles
8576       ;; after the current one.
8577       (goto-char (point-max))
8578       (gnus-summary-find-prev))
8579     (gnus-set-mode-line 'summary)
8580     ;; We return how many articles were removed from the summary
8581     ;; buffer as a result of the new limit.
8582     (- total (length gnus-newsgroup-data))))
8583
8584 (defsubst gnus-invisible-cut-children (threads)
8585   (let ((num 0))
8586     (while threads
8587       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8588         (incf num))
8589       (pop threads))
8590     (< num 2)))
8591
8592 (defsubst gnus-cut-thread (thread)
8593   "Go forwards in the thread until we find an article that we want to display."
8594   (when (or (eq gnus-fetch-old-headers 'some)
8595             (eq gnus-fetch-old-headers 'invisible)
8596             (numberp gnus-fetch-old-headers)
8597             (eq gnus-build-sparse-threads 'some)
8598             (eq gnus-build-sparse-threads 'more))
8599     ;; Deal with old-fetched headers and sparse threads.
8600     (while (and
8601             thread
8602             (or
8603              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8604              (gnus-summary-article-ancient-p
8605               (mail-header-number (car thread))))
8606             (if (or (<= (length (cdr thread)) 1)
8607                     (eq gnus-fetch-old-headers 'invisible))
8608                 (setq gnus-newsgroup-limit
8609                       (delq (mail-header-number (car thread))
8610                             gnus-newsgroup-limit)
8611                       thread (cadr thread))
8612               (when (gnus-invisible-cut-children (cdr thread))
8613                 (let ((th (cdr thread)))
8614                   (while th
8615                     (if (memq (mail-header-number (caar th))
8616                               gnus-newsgroup-limit)
8617                         (setq thread (car th)
8618                               th nil)
8619                       (setq th (cdr th))))))))))
8620   thread)
8621
8622 (defun gnus-cut-threads (threads)
8623   "Cut off all uninteresting articles from the beginning of THREADS."
8624   (when (or (eq gnus-fetch-old-headers 'some)
8625             (eq gnus-fetch-old-headers 'invisible)
8626             (numberp gnus-fetch-old-headers)
8627             (eq gnus-build-sparse-threads 'some)
8628             (eq gnus-build-sparse-threads 'more))
8629     (let ((th threads))
8630       (while th
8631         (setcar th (gnus-cut-thread (car th)))
8632         (setq th (cdr th)))))
8633   ;; Remove nixed out threads.
8634   (delq nil threads))
8635
8636 (defun gnus-summary-initial-limit (&optional show-if-empty)
8637   "Figure out what the initial limit is supposed to be on group entry.
8638 This entails weeding out unwanted dormants, low-scored articles,
8639 fetch-old-headers verbiage, and so on."
8640   ;; Most groups have nothing to remove.
8641   (unless (or gnus-inhibit-limiting
8642               (and (null gnus-newsgroup-dormant)
8643                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8644                    (not (eq gnus-fetch-old-headers 'some))
8645                    (not (numberp gnus-fetch-old-headers))
8646                    (not (eq gnus-fetch-old-headers 'invisible))
8647                    (null gnus-summary-expunge-below)
8648                    (not (eq gnus-build-sparse-threads 'some))
8649                    (not (eq gnus-build-sparse-threads 'more))
8650                    (null gnus-thread-expunge-below)
8651                    (not gnus-use-nocem)))
8652     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8653     (setq gnus-newsgroup-limit nil)
8654     (mapatoms
8655      (lambda (node)
8656        (unless (car (symbol-value node))
8657          ;; These threads have no parents -- they are roots.
8658          (let ((nodes (cdr (symbol-value node)))
8659                thread)
8660            (while nodes
8661              (if (and gnus-thread-expunge-below
8662                       (< (gnus-thread-total-score (car nodes))
8663                          gnus-thread-expunge-below))
8664                  (gnus-expunge-thread (pop nodes))
8665                (setq thread (pop nodes))
8666                (gnus-summary-limit-children thread))))))
8667      gnus-newsgroup-dependencies)
8668     ;; If this limitation resulted in an empty group, we might
8669     ;; pop the previous limit and use it instead.
8670     (when (and (not gnus-newsgroup-limit)
8671                show-if-empty)
8672       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8673     gnus-newsgroup-limit))
8674
8675 (defun gnus-summary-limit-children (thread)
8676   "Return 1 if this subthread is visible and 0 if it is not."
8677   ;; First we get the number of visible children to this thread.  This
8678   ;; is done by recursing down the thread using this function, so this
8679   ;; will really go down to a leaf article first, before slowly
8680   ;; working its way up towards the root.
8681   (when thread
8682     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8683            (children
8684            (if (cdr thread)
8685                (apply '+ (mapcar 'gnus-summary-limit-children
8686                                  (cdr thread)))
8687              0))
8688           (number (mail-header-number (car thread)))
8689           score)
8690       (if (and
8691            (not (memq number gnus-newsgroup-marked))
8692            (or
8693             ;; If this article is dormant and has absolutely no visible
8694             ;; children, then this article isn't visible.
8695             (and (memq number gnus-newsgroup-dormant)
8696                  (zerop children))
8697             ;; If this is "fetch-old-headered" and there is no
8698             ;; visible children, then we don't want this article.
8699             (and (or (eq gnus-fetch-old-headers 'some)
8700                      (numberp gnus-fetch-old-headers))
8701                  (gnus-summary-article-ancient-p number)
8702                  (zerop children))
8703             ;; If this is "fetch-old-headered" and `invisible', then
8704             ;; we don't want this article.
8705             (and (eq gnus-fetch-old-headers 'invisible)
8706                  (gnus-summary-article-ancient-p number))
8707             ;; If this is a sparsely inserted article with no children,
8708             ;; we don't want it.
8709             (and (eq gnus-build-sparse-threads 'some)
8710                  (gnus-summary-article-sparse-p number)
8711                  (zerop children))
8712             ;; If we use expunging, and this article is really
8713             ;; low-scored, then we don't want this article.
8714             (when (and gnus-summary-expunge-below
8715                        (< (setq score
8716                                 (or (cdr (assq number gnus-newsgroup-scored))
8717                                     gnus-summary-default-score))
8718                           gnus-summary-expunge-below))
8719               ;; We increase the expunge-tally here, but that has
8720               ;; nothing to do with the limits, really.
8721               (incf gnus-newsgroup-expunged-tally)
8722               ;; We also mark as read here, if that's wanted.
8723               (when (and gnus-summary-mark-below
8724                          (< score gnus-summary-mark-below))
8725                 (setq gnus-newsgroup-unreads
8726                       (delq number gnus-newsgroup-unreads))
8727                 (if gnus-newsgroup-auto-expire
8728                     (push number gnus-newsgroup-expirable)
8729                   (push (cons number gnus-low-score-mark)
8730                         gnus-newsgroup-reads)))
8731               t)
8732             ;; Do the `display' group parameter.
8733             (and gnus-newsgroup-display
8734                  (not (funcall gnus-newsgroup-display)))
8735             ;; Check NoCeM things.
8736             (when (and gnus-use-nocem
8737                        (gnus-nocem-unwanted-article-p
8738                         (mail-header-id (car thread))))
8739               (setq gnus-newsgroup-unreads
8740                     (delq number gnus-newsgroup-unreads))
8741               t)))
8742           ;; Nope, invisible article.
8743           0
8744         ;; Ok, this article is to be visible, so we add it to the limit
8745         ;; and return 1.
8746         (push number gnus-newsgroup-limit)
8747         1))))
8748
8749 (defun gnus-expunge-thread (thread)
8750   "Mark all articles in THREAD as read."
8751   (let* ((number (mail-header-number (car thread))))
8752     (incf gnus-newsgroup-expunged-tally)
8753     ;; We also mark as read here, if that's wanted.
8754     (setq gnus-newsgroup-unreads
8755           (delq number gnus-newsgroup-unreads))
8756     (if gnus-newsgroup-auto-expire
8757         (push number gnus-newsgroup-expirable)
8758       (push (cons number gnus-low-score-mark)
8759             gnus-newsgroup-reads)))
8760   ;; Go recursively through all subthreads.
8761   (mapcar 'gnus-expunge-thread (cdr thread)))
8762
8763 ;; Summary article oriented commands
8764
8765 (defun gnus-summary-refer-parent-article (n)
8766   "Refer parent article N times.
8767 If N is negative, go to ancestor -N instead.
8768 The difference between N and the number of articles fetched is returned."
8769   (interactive "p")
8770   (let ((skip 1)
8771         error header ref)
8772     (when (not (natnump n))
8773       (setq skip (abs n)
8774             n 1))
8775     (while (and (> n 0)
8776                 (not error))
8777       (setq header (gnus-summary-article-header))
8778       (if (and (eq (mail-header-number header)
8779                    (cdr gnus-article-current))
8780                (equal gnus-newsgroup-name
8781                       (car gnus-article-current)))
8782           ;; If we try to find the parent of the currently
8783           ;; displayed article, then we take a look at the actual
8784           ;; References header, since this is slightly more
8785           ;; reliable than the References field we got from the
8786           ;; server.
8787           (with-current-buffer gnus-original-article-buffer
8788             (nnheader-narrow-to-headers)
8789             (unless (setq ref (message-fetch-field "references"))
8790               (when (setq ref (message-fetch-field "in-reply-to"))
8791                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8792             (widen))
8793         (setq ref
8794               ;; It's not the current article, so we take a bet on
8795               ;; the value we got from the server.
8796               (mail-header-references header)))
8797       (if (and ref
8798                (not (equal ref "")))
8799           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8800             (gnus-message 1 "Couldn't find parent"))
8801         (gnus-message 1 "No references in article %d"
8802                       (gnus-summary-article-number))
8803         (setq error t))
8804       (decf n))
8805     (gnus-summary-position-point)
8806     n))
8807
8808 (defun gnus-summary-refer-references ()
8809   "Fetch all articles mentioned in the References header.
8810 Return the number of articles fetched."
8811   (interactive)
8812   (let ((ref (mail-header-references (gnus-summary-article-header)))
8813         (current (gnus-summary-article-number))
8814         (n 0))
8815     (if (or (not ref)
8816             (equal ref ""))
8817         (error "No References in the current article")
8818       ;; For each Message-ID in the References header...
8819       (while (string-match "<[^>]*>" ref)
8820         (incf n)
8821         ;; ... fetch that article.
8822         (gnus-summary-refer-article
8823          (prog1 (match-string 0 ref)
8824            (setq ref (substring ref (match-end 0))))))
8825       (gnus-summary-goto-subject current)
8826       (gnus-summary-position-point)
8827       n)))
8828
8829 (defun gnus-summary-refer-thread (&optional limit)
8830   "Fetch all articles in the current thread.
8831 If LIMIT (the numerical prefix), fetch that many old headers instead
8832 of what's specified by the `gnus-refer-thread-limit' variable."
8833   (interactive "P")
8834   (let ((id (mail-header-id (gnus-summary-article-header)))
8835         (limit (if limit (prefix-numeric-value limit)
8836                  gnus-refer-thread-limit)))
8837     (unless (eq gnus-fetch-old-headers 'invisible)
8838       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8839       ;; Retrieve the headers and read them in.
8840       (if (eq (if (numberp limit)
8841                   (gnus-retrieve-headers
8842                    (list (min
8843                           (+ (mail-header-number
8844                               (gnus-summary-article-header))
8845                              limit)
8846                           gnus-newsgroup-end))
8847                    gnus-newsgroup-name (* limit 2))
8848                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8849                 ;; headers.
8850                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8851                                        gnus-newsgroup-name limit))
8852               'nov)
8853           (gnus-build-all-threads)
8854         (error "Can't fetch thread from back ends that don't support NOV"))
8855       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8856     (gnus-summary-limit-include-thread id)))
8857
8858 (defun gnus-summary-refer-article (message-id)
8859   "Fetch an article specified by MESSAGE-ID."
8860   (interactive "sMessage-ID: ")
8861   (when (and (stringp message-id)
8862              (not (zerop (length message-id))))
8863     (setq message-id (gnus-replace-in-string message-id " " ""))
8864     ;; Construct the correct Message-ID if necessary.
8865     ;; Suggested by tale@pawl.rpi.edu.
8866     (unless (string-match "^<" message-id)
8867       (setq message-id (concat "<" message-id)))
8868     (unless (string-match ">$" message-id)
8869       (setq message-id (concat message-id ">")))
8870     ;; People often post MIDs from URLs, so unhex it:
8871     (unless (string-match "@" message-id)
8872       (setq message-id (gnus-url-unhex-string message-id)))
8873     (let* ((header (gnus-id-to-header message-id))
8874            (sparse (and header
8875                         (gnus-summary-article-sparse-p
8876                          (mail-header-number header))
8877                         (memq (mail-header-number header)
8878                               gnus-newsgroup-limit)))
8879            number)
8880       (cond
8881        ;; If the article is present in the buffer we just go to it.
8882        ((and header
8883              (or (not (gnus-summary-article-sparse-p
8884                        (mail-header-number header)))
8885                  sparse))
8886         (prog1
8887             (gnus-summary-goto-article
8888              (mail-header-number header) nil t)
8889           (when sparse
8890             (gnus-summary-update-article (mail-header-number header)))))
8891        (t
8892         ;; We fetch the article.
8893         (catch 'found
8894           (dolist (gnus-override-method (gnus-refer-article-methods))
8895             (when (and (gnus-check-server gnus-override-method)
8896                        ;; Fetch the header,
8897                        (setq number (gnus-summary-insert-subject message-id)))
8898               ;; and display the article.
8899               (gnus-summary-select-article nil nil nil number)
8900               (throw 'found t)))
8901           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8902
8903 (defun gnus-refer-article-methods ()
8904   "Return a list of referable methods."
8905   (cond
8906    ;; No method, so we default to current and native.
8907    ((null gnus-refer-article-method)
8908     (list gnus-current-select-method gnus-select-method))
8909    ;; Current.
8910    ((eq 'current gnus-refer-article-method)
8911     (list gnus-current-select-method))
8912    ;; List of select methods.
8913    ((not (and (symbolp (car gnus-refer-article-method))
8914               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8915     (let (out)
8916       (dolist (method gnus-refer-article-method)
8917         (push (if (eq 'current method)
8918                   gnus-current-select-method
8919                 method)
8920               out))
8921       (nreverse out)))
8922    ;; One single select method.
8923    (t
8924     (list gnus-refer-article-method))))
8925
8926 (defun gnus-summary-edit-parameters ()
8927   "Edit the group parameters of the current group."
8928   (interactive)
8929   (gnus-group-edit-group gnus-newsgroup-name 'params))
8930
8931 (defun gnus-summary-customize-parameters ()
8932   "Customize the group parameters of the current group."
8933   (interactive)
8934   (gnus-group-customize gnus-newsgroup-name))
8935
8936 (defun gnus-summary-enter-digest-group (&optional force)
8937   "Enter an nndoc group based on the current article.
8938 If FORCE, force a digest interpretation.  If not, try
8939 to guess what the document format is."
8940   (interactive "P")
8941   (let ((conf gnus-current-window-configuration))
8942     (save-window-excursion
8943       (save-excursion
8944         (let (gnus-article-prepare-hook
8945               gnus-display-mime-function
8946               gnus-break-pages)
8947           (gnus-summary-select-article))))
8948     (setq gnus-current-window-configuration conf)
8949     (let* ((name (format "%s-%d"
8950                          (gnus-group-prefixed-name
8951                           gnus-newsgroup-name (list 'nndoc ""))
8952                          (with-current-buffer gnus-summary-buffer
8953                            gnus-current-article)))
8954            (ogroup gnus-newsgroup-name)
8955            (params (append (gnus-info-params (gnus-get-info ogroup))
8956                            (list (cons 'to-group ogroup))
8957                            (list (cons 'parent-group ogroup))
8958                            (list (cons 'save-article-group ogroup))))
8959            (case-fold-search t)
8960            (buf (current-buffer))
8961            dig to-address)
8962       (with-current-buffer gnus-original-article-buffer
8963         ;; Have the digest group inherit the main mail address of
8964         ;; the parent article.
8965         (when (setq to-address (or (gnus-fetch-field "reply-to")
8966                                    (gnus-fetch-field "from")))
8967           (setq params
8968                 (append
8969                  (list (cons 'to-address
8970                              (funcall gnus-decode-encoded-address-function
8971                                       to-address))))))
8972         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8973         (insert-buffer-substring gnus-original-article-buffer)
8974         ;; Remove lines that may lead nndoc to misinterpret the
8975         ;; document type.
8976         (narrow-to-region
8977          (goto-char (point-min))
8978          (or (search-forward "\n\n" nil t) (point)))
8979         (goto-char (point-min))
8980         (delete-matching-lines "^Path:\\|^From ")
8981         (widen))
8982       (unwind-protect
8983           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8984                     (gnus-newsgroup-ephemeral-ignored-charsets
8985                      gnus-newsgroup-ignored-charsets))
8986                 (gnus-group-read-ephemeral-group
8987                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8988                               (nndoc-article-type
8989                                ,(if force 'mbox 'guess)))
8990                  t nil nil nil
8991                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8992                                                         "ADAPT")))))
8993               ;; Make all postings to this group go to the parent group.
8994               (nconc (gnus-info-params (gnus-get-info name))
8995                      params)
8996             ;; Couldn't select this doc group.
8997             (switch-to-buffer buf)
8998             (gnus-set-global-variables)
8999             (gnus-configure-windows 'summary)
9000             (gnus-message 3 "Article couldn't be entered?"))
9001         (kill-buffer dig)))))
9002
9003 (defun gnus-summary-read-document (n)
9004   "Open a new group based on the current article(s).
9005 This will allow you to read digests and other similar
9006 documents as newsgroups.
9007 Obeys the standard process/prefix convention."
9008   (interactive "P")
9009   (let* ((ogroup gnus-newsgroup-name)
9010          (params (append (gnus-info-params (gnus-get-info ogroup))
9011                          (list (cons 'to-group ogroup))))
9012          group egroup groups vgroup)
9013     (dolist (article (gnus-summary-work-articles n))
9014       (setq group (format "%s-%d" gnus-newsgroup-name article))
9015       (gnus-summary-remove-process-mark article)
9016       (when (gnus-summary-display-article article)
9017         (save-excursion ;;What for?
9018           (with-temp-buffer
9019             (insert-buffer-substring gnus-original-article-buffer)
9020             ;; Remove some headers that may lead nndoc to make
9021             ;; the wrong guess.
9022             (message-narrow-to-head)
9023             (goto-char (point-min))
9024             (delete-matching-lines "^Path:\\|^From ")
9025             (widen)
9026             (if (setq egroup
9027                       (gnus-group-read-ephemeral-group
9028                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9029                                      (nndoc-article-type guess))
9030                        t nil t))
9031                 (progn
9032                   ;; Make all postings to this group go to the parent group.
9033                   (nconc (gnus-info-params (gnus-get-info egroup))
9034                          params)
9035                   (push egroup groups))
9036               ;; Couldn't select this doc group.
9037               (gnus-error 3 "Article couldn't be entered"))))))
9038     ;; Now we have selected all the documents.
9039     (cond
9040      ((not groups)
9041       (error "None of the articles could be interpreted as documents"))
9042      ((gnus-group-read-ephemeral-group
9043        (setq vgroup (format
9044                      "nnvirtual:%s-%s" gnus-newsgroup-name
9045                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9046        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9047        t
9048        (cons (current-buffer) 'summary)))
9049      (t
9050       (error "Couldn't select virtual nndoc group")))))
9051
9052 (defun gnus-summary-isearch-article (&optional regexp-p)
9053   "Do incremental search forward on the current article.
9054 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9055   (interactive "P")
9056   (gnus-summary-select-article)
9057   (gnus-configure-windows 'article)
9058   (gnus-eval-in-buffer-window gnus-article-buffer
9059     (save-restriction
9060       (widen)
9061       (isearch-forward regexp-p))))
9062
9063 (defun gnus-summary-repeat-search-article-forward ()
9064   "Repeat the previous search forwards."
9065   (interactive)
9066   (unless gnus-last-search-regexp
9067     (error "No previous search"))
9068   (gnus-summary-search-article-forward gnus-last-search-regexp))
9069
9070 (defun gnus-summary-repeat-search-article-backward ()
9071   "Repeat the previous search backwards."
9072   (interactive)
9073   (unless gnus-last-search-regexp
9074     (error "No previous search"))
9075   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9076
9077 (defun gnus-summary-search-article-forward (regexp &optional backward)
9078   "Search for an article containing REGEXP forward.
9079 If BACKWARD, search backward instead."
9080   (interactive
9081    (list (read-string
9082           (format "Search article %s (regexp%s): "
9083                   (if current-prefix-arg "backward" "forward")
9084                   (if gnus-last-search-regexp
9085                       (concat ", default " gnus-last-search-regexp)
9086                     "")))
9087          current-prefix-arg))
9088   (if (string-equal regexp "")
9089       (setq regexp (or gnus-last-search-regexp ""))
9090     (setq gnus-last-search-regexp regexp)
9091     (setq gnus-article-before-search gnus-current-article))
9092   ;; Intentionally set gnus-last-article.
9093   (setq gnus-last-article gnus-article-before-search)
9094   (let ((gnus-last-article gnus-last-article))
9095     (if (gnus-summary-search-article regexp backward)
9096         (gnus-summary-show-thread)
9097       (signal 'search-failed (list regexp)))))
9098
9099 (defun gnus-summary-search-article-backward (regexp)
9100   "Search for an article containing REGEXP backward."
9101   (interactive
9102    (list (read-string
9103           (format "Search article backward (regexp%s): "
9104                   (if gnus-last-search-regexp
9105                       (concat ", default " gnus-last-search-regexp)
9106                     "")))))
9107   (gnus-summary-search-article-forward regexp 'backward))
9108
9109 (defun gnus-summary-search-article (regexp &optional backward)
9110   "Search for an article containing REGEXP.
9111 Optional argument BACKWARD means do search for backward.
9112 `gnus-select-article-hook' is not called during the search."
9113   ;; We have to require this here to make sure that the following
9114   ;; dynamic binding isn't shadowed by autoloading.
9115   (require 'gnus-async)
9116   (require 'gnus-art)
9117   (let ((gnus-select-article-hook nil)  ;Disable hook.
9118         (gnus-article-prepare-hook nil)
9119         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9120         (gnus-use-article-prefetch nil)
9121         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9122         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9123         (gnus-visual nil)
9124         (gnus-keep-backlog nil)
9125         (gnus-break-pages nil)
9126         (gnus-summary-display-arrow nil)
9127         (gnus-updated-mode-lines nil)
9128         (gnus-auto-center-summary nil)
9129         (sum (current-buffer))
9130         (gnus-display-mime-function nil)
9131         (found nil)
9132         point)
9133     (gnus-save-hidden-threads
9134       (gnus-summary-select-article)
9135       (set-buffer gnus-article-buffer)
9136       (goto-char (window-point (get-buffer-window (current-buffer))))
9137       (when backward
9138         (forward-line -1))
9139       (while (not found)
9140         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9141         (if (if backward
9142                 (re-search-backward regexp nil t)
9143               (re-search-forward regexp nil t))
9144             ;; We found the regexp.
9145             (progn
9146               (setq found 'found)
9147               (beginning-of-line)
9148               (set-window-start
9149                (get-buffer-window (current-buffer))
9150                (point))
9151               (forward-line 1)
9152               (set-window-point
9153                (get-buffer-window (current-buffer))
9154                (point))
9155               (set-buffer sum)
9156               (setq point (point)))
9157           ;; We didn't find it, so we go to the next article.
9158           (set-buffer sum)
9159           (setq found 'not)
9160           (while (eq found 'not)
9161             (if (not (if backward (gnus-summary-find-prev)
9162                        (gnus-summary-find-next)))
9163                 ;; No more articles.
9164                 (setq found t)
9165               ;; Select the next article and adjust point.
9166               (unless (gnus-summary-article-sparse-p
9167                        (gnus-summary-article-number))
9168                 (setq found nil)
9169                 (gnus-summary-select-article)
9170                 (set-buffer gnus-article-buffer)
9171                 (widen)
9172                 (goto-char (if backward (point-max) (point-min))))))))
9173       (gnus-message 7 ""))
9174     ;; Return whether we found the regexp.
9175     (when (eq found 'found)
9176       (goto-char point)
9177       (gnus-summary-show-thread)
9178       (gnus-summary-goto-subject gnus-current-article)
9179       (gnus-summary-position-point)
9180       t)))
9181
9182 (defun gnus-find-matching-articles (header regexp)
9183   "Return a list of all articles that match REGEXP on HEADER.
9184 This search includes all articles in the current group that Gnus has
9185 fetched headers for, whether they are displayed or not."
9186   (let ((articles nil)
9187         ;; Can't eta-reduce because it's a macro.
9188         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9189         (case-fold-search t))
9190     (dolist (header gnus-newsgroup-headers)
9191       (when (string-match regexp (funcall func header))
9192         (push (mail-header-number header) articles)))
9193     (nreverse articles)))
9194
9195 (defun gnus-summary-find-matching (header regexp &optional backward unread
9196                                           not-case-fold not-matching)
9197   "Return a list of all articles that match REGEXP on HEADER.
9198 The search stars on the current article and goes forwards unless
9199 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9200 If UNREAD is non-nil, only unread articles will
9201 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9202 in the comparisons. If NOT-MATCHING, return a list of all articles that
9203 not match REGEXP on HEADER."
9204   (let ((case-fold-search (not not-case-fold))
9205         articles d func)
9206     (if (consp header)
9207         (if (eq (car header) 'extra)
9208             (setq func
9209                   `(lambda (h)
9210                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9211                          "")))
9212           (error "%s is an invalid header" header))
9213       (unless (fboundp (intern (concat "mail-header-" header)))
9214         (error "%s is not a valid header" header))
9215       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9216     (dolist (d (if (eq backward 'all)
9217                    gnus-newsgroup-data
9218                  (gnus-data-find-list
9219                   (gnus-summary-article-number)
9220                   (gnus-data-list backward))))
9221       (when (and (or (not unread)       ; We want all articles...
9222                      (gnus-data-unread-p d)) ; Or just unreads.
9223                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9224                  (if not-matching
9225                      (not (string-match
9226                            regexp
9227                            (funcall func (gnus-data-header d))))
9228                    (string-match regexp
9229                                  (funcall func (gnus-data-header d)))))
9230         (push (gnus-data-number d) articles))) ; Success!
9231     (nreverse articles)))
9232
9233 (defun gnus-summary-execute-command (header regexp command &optional backward)
9234   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9235 If HEADER is an empty string (or nil), the match is done on the entire
9236 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9237   (interactive
9238    (list (let ((completion-ignore-case t))
9239            (completing-read
9240             "Header name: "
9241             (mapcar (lambda (header) (list (format "%s" header)))
9242                     (append
9243                      '("Number" "Subject" "From" "Lines" "Date"
9244                        "Message-ID" "Xref" "References" "Body")
9245                      gnus-extra-headers))
9246             nil 'require-match))
9247          (read-string "Regexp: ")
9248          (read-key-sequence "Command: ")
9249          current-prefix-arg))
9250   (when (equal header "Body")
9251     (setq header ""))
9252   ;; Hidden thread subtrees must be searched as well.
9253   (gnus-summary-show-all-threads)
9254   ;; We don't want to change current point nor window configuration.
9255   (save-excursion
9256     (save-window-excursion
9257       (let (gnus-visual
9258             gnus-treat-strip-trailing-blank-lines
9259             gnus-treat-strip-leading-blank-lines
9260             gnus-treat-strip-multiple-blank-lines
9261             gnus-treat-hide-boring-headers
9262             gnus-treat-fold-newsgroups
9263             gnus-article-prepare-hook)
9264         (gnus-message 6 "Executing %s..." (key-description command))
9265         ;; We'd like to execute COMMAND interactively so as to give arguments.
9266         (gnus-execute header regexp
9267                       `(call-interactively ',(key-binding command))
9268                       backward)
9269         (gnus-message 6 "Executing %s...done" (key-description command))))))
9270
9271 (defun gnus-summary-beginning-of-article ()
9272   "Scroll the article back to the beginning."
9273   (interactive)
9274   (gnus-summary-select-article)
9275   (gnus-configure-windows 'article)
9276   (gnus-eval-in-buffer-window gnus-article-buffer
9277     (widen)
9278     (goto-char (point-min))
9279     (when gnus-break-pages
9280       (gnus-narrow-to-page))))
9281
9282 (defun gnus-summary-end-of-article ()
9283   "Scroll to the end of the article."
9284   (interactive)
9285   (gnus-summary-select-article)
9286   (gnus-configure-windows 'article)
9287   (gnus-eval-in-buffer-window gnus-article-buffer
9288     (widen)
9289     (goto-char (point-max))
9290     (recenter -3)
9291     (when gnus-break-pages
9292       (gnus-narrow-to-page))))
9293
9294 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9295   "Truncate to LEN and quote all \"(\"'s in STRING."
9296   (gnus-replace-in-string (if (and len (> (length string) len))
9297                               (substring string 0 len)
9298                             string)
9299                           "[()]" "\\\\\\&"))
9300
9301 (defun gnus-summary-print-article (&optional filename n)
9302   "Generate and print a PostScript image of the process-marked (mail) articles.
9303
9304 If used interactively, print the current article if none are
9305 process-marked.  With prefix arg, prompt the user for the name of the
9306 file to save in.
9307
9308 When used from Lisp, accept two optional args FILENAME and N.  N means
9309 to print the next N articles.  If N is negative, print the N previous
9310 articles.  If N is nil and articles have been marked with the process
9311 mark, print these instead.
9312
9313 If the optional first argument FILENAME is nil, send the image to the
9314 printer.  If FILENAME is a string, save the PostScript image in a file with
9315 that name.  If FILENAME is a number, prompt the user for the name of the file
9316 to save in."
9317   (interactive (list (ps-print-preprint current-prefix-arg)))
9318   (dolist (article (gnus-summary-work-articles n))
9319     (gnus-summary-select-article nil nil 'pseudo article)
9320     (gnus-eval-in-buffer-window gnus-article-buffer
9321       (gnus-print-buffer))
9322     (gnus-summary-remove-process-mark article))
9323   (ps-despool filename))
9324
9325 (defun gnus-print-buffer ()
9326   (let ((buffer (generate-new-buffer " *print*")))
9327     (unwind-protect
9328         (progn
9329           (copy-to-buffer buffer (point-min) (point-max))
9330           (set-buffer buffer)
9331           (gnus-remove-text-with-property 'gnus-decoration)
9332           (when (gnus-visual-p 'article-highlight 'highlight)
9333             ;; Copy-to-buffer doesn't copy overlay.  So redo
9334             ;; highlight.
9335             (let ((gnus-article-buffer buffer))
9336               (gnus-article-highlight-citation t)
9337               (gnus-article-highlight-signature)
9338               (gnus-article-emphasize)
9339               (gnus-article-delete-invisible-text)))
9340           (let ((ps-left-header
9341                  (list
9342                   (concat "("
9343                           (gnus-summary-print-truncate-and-quote
9344                            (mail-header-subject gnus-current-headers)
9345                            66) ")")
9346                   (concat "("
9347                           (gnus-summary-print-truncate-and-quote
9348                            (mail-header-from gnus-current-headers)
9349                            45) ")")))
9350                 (ps-right-header
9351                  (list
9352                   "/pagenumberstring load"
9353                   (concat "("
9354                           (mail-header-date gnus-current-headers) ")"))))
9355             (gnus-run-hooks 'gnus-ps-print-hook)
9356             (save-excursion
9357               (if ps-print-color-p
9358                   (ps-spool-buffer-with-faces)
9359                 (ps-spool-buffer)))))
9360       (kill-buffer buffer))))
9361
9362 (defun gnus-summary-show-article (&optional arg)
9363   "Force redisplaying of the current article.
9364 If ARG (the prefix) is a number, show the article with the charset
9365 defined in `gnus-summary-show-article-charset-alist', or the charset
9366 input.
9367 If ARG (the prefix) is non-nil and not a number, show the raw article
9368 without any article massaging functions being run.  Normally, the key
9369 strokes are `C-u g'."
9370   (interactive "P")
9371   (cond
9372    ((numberp arg)
9373     (gnus-summary-show-article t)
9374     (let ((gnus-newsgroup-charset
9375            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9376                (mm-read-coding-system
9377                 "View as charset: " ;; actually it is coding system.
9378                 (with-current-buffer gnus-article-buffer
9379                   (mm-detect-coding-region (point) (point-max))))))
9380           (gnus-newsgroup-ignored-charsets 'gnus-all))
9381       (gnus-summary-select-article nil 'force)
9382       (let ((deps gnus-newsgroup-dependencies)
9383             head header lines)
9384         (with-current-buffer gnus-original-article-buffer
9385           (save-restriction
9386             (message-narrow-to-head)
9387             (setq head (buffer-string))
9388             (goto-char (point-min))
9389             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9390               (goto-char (point-max))
9391               (widen)
9392               (setq lines (1- (count-lines (point) (point-max))))))
9393           (with-temp-buffer
9394             (insert (format "211 %d Article retrieved.\n"
9395                             (cdr gnus-article-current)))
9396             (insert head)
9397             (if lines (insert (format "Lines: %d\n" lines)))
9398             (insert ".\n")
9399             (let ((nntp-server-buffer (current-buffer)))
9400               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9401         (gnus-data-set-header
9402          (gnus-data-find (cdr gnus-article-current))
9403          header)
9404         (gnus-summary-update-article-line
9405          (cdr gnus-article-current) header)
9406         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9407           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9408    ((not arg)
9409     ;; Select the article the normal way.
9410     (gnus-summary-select-article nil 'force))
9411    (t
9412     ;; We have to require this here to make sure that the following
9413     ;; dynamic binding isn't shadowed by autoloading.
9414     (require 'gnus-async)
9415     (require 'gnus-art)
9416     ;; Bind the article treatment functions to nil.
9417     (let ((gnus-have-all-headers t)
9418           gnus-article-prepare-hook
9419           gnus-article-decode-hook
9420           gnus-display-mime-function
9421           gnus-break-pages)
9422       ;; Destroy any MIME parts.
9423       (when (gnus-buffer-live-p gnus-article-buffer)
9424         (with-current-buffer gnus-article-buffer
9425           (mm-destroy-parts gnus-article-mime-handles)
9426           ;; Set it to nil for safety reason.
9427           (setq gnus-article-mime-handle-alist nil)
9428           (setq gnus-article-mime-handles nil)))
9429       (gnus-summary-select-article nil 'force))))
9430   (gnus-summary-goto-subject gnus-current-article)
9431   (gnus-summary-position-point))
9432
9433 (defun gnus-summary-show-raw-article ()
9434   "Show the raw article without any article massaging functions being run."
9435   (interactive)
9436   (gnus-summary-show-article t))
9437
9438 (defun gnus-summary-verbose-headers (&optional arg)
9439   "Toggle permanent full header display.
9440 If ARG is a positive number, turn header display on.
9441 If ARG is a negative number, turn header display off."
9442   (interactive "P")
9443   (setq gnus-show-all-headers
9444         (cond ((or (not (numberp arg))
9445                    (zerop arg))
9446                (not gnus-show-all-headers))
9447               ((natnump arg)
9448                t)))
9449   (gnus-summary-show-article))
9450
9451 (defun gnus-summary-toggle-header (&optional arg)
9452   "Show the headers if they are hidden, or hide them if they are shown.
9453 If ARG is a positive number, show the entire header.
9454 If ARG is a negative number, hide the unwanted header lines."
9455   (interactive "P")
9456   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9457                      (get-buffer-window gnus-article-buffer t))))
9458     (with-current-buffer gnus-article-buffer
9459       (widen)
9460       (article-narrow-to-head)
9461       (let* ((inhibit-read-only t)
9462              (inhibit-point-motion-hooks t)
9463              (hidden (if (numberp arg)
9464                          (>= arg 0)
9465                        (or (not (looking-at "[^ \t\n]+:"))
9466                            (gnus-article-hidden-text-p 'headers))))
9467              s e)
9468         (delete-region (point-min) (point-max))
9469         (with-current-buffer gnus-original-article-buffer
9470           (goto-char (setq s (point-min)))
9471           (setq e (if (search-forward "\n\n" nil t)
9472                       (1- (point))
9473                     (point-max))))
9474         (insert-buffer-substring gnus-original-article-buffer s e)
9475         (run-hooks 'gnus-article-decode-hook)
9476         (if hidden
9477             (let ((gnus-treat-hide-headers nil)
9478                   (gnus-treat-hide-boring-headers nil))
9479               (gnus-delete-wash-type 'headers)
9480               (gnus-treat-article 'head))
9481           (gnus-treat-article 'head))
9482         (widen)
9483         (if window
9484             (set-window-start window (goto-char (point-min))))
9485         (if gnus-break-pages
9486             (gnus-narrow-to-page)
9487           (when (gnus-visual-p 'page-marker)
9488             (let ((inhibit-read-only t))
9489               (gnus-remove-text-with-property 'gnus-prev)
9490               (gnus-remove-text-with-property 'gnus-next))))
9491         (gnus-set-mode-line 'article)))))
9492
9493 (defun gnus-summary-show-all-headers ()
9494   "Make all header lines visible."
9495   (interactive)
9496   (gnus-summary-toggle-header 1))
9497
9498 (defun gnus-summary-caesar-message (&optional arg)
9499   "Caesar rotate the current article by 13.
9500 With a non-numerical prefix, also rotate headers.  A numerical
9501 prefix specifies how many places to rotate each letter forward."
9502   (interactive "P")
9503   (gnus-summary-select-article)
9504   (let ((mail-header-separator ""))
9505     (gnus-eval-in-buffer-window gnus-article-buffer
9506       (save-restriction
9507         (widen)
9508         (let ((start (window-start))
9509               (inhibit-read-only t))
9510           (if (equal arg '(4))
9511               (message-caesar-buffer-body nil t)
9512             (message-caesar-buffer-body arg))
9513           (set-window-start (get-buffer-window (current-buffer)) start)))))
9514   ;; Create buttons and stuff...
9515   (gnus-treat-article nil))
9516
9517 (declare-function idna-to-unicode "ext:idna" (str))
9518
9519 (defun gnus-summary-idna-message (&optional arg)
9520   "Decode IDNA encoded domain names in the current articles.
9521 IDNA encoded domain names looks like `xn--bar'.  If a string
9522 remain unencoded after running this function, it is likely an
9523 invalid IDNA string (`xn--bar' is invalid).
9524
9525 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9526 installed for this command to work."
9527   (interactive "P")
9528   (if (not (and (condition-case nil (require 'idna)
9529                   (file-error))
9530                 (mm-coding-system-p 'utf-8)
9531                 (executable-find (symbol-value 'idna-program))))
9532       (gnus-message
9533        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9534     (gnus-summary-select-article)
9535     (let ((mail-header-separator ""))
9536       (gnus-eval-in-buffer-window gnus-article-buffer
9537         (save-restriction
9538           (widen)
9539           (let ((start (window-start))
9540                 buffer-read-only)
9541             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9542               (replace-match (idna-to-unicode (match-string 1))))
9543             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9544
9545 (defun gnus-summary-morse-message (&optional arg)
9546   "Morse decode the current article."
9547   (interactive "P")
9548   (gnus-summary-select-article)
9549   (let ((mail-header-separator ""))
9550     (gnus-eval-in-buffer-window gnus-article-buffer
9551       (save-excursion
9552         (save-restriction
9553           (widen)
9554           (let ((pos (window-start))
9555                 (inhibit-read-only t))
9556             (goto-char (point-min))
9557             (when (message-goto-body)
9558               (gnus-narrow-to-body))
9559             (goto-char (point-min))
9560             (while (search-forward "·" (point-max) t)
9561               (replace-match "."))
9562             (unmorse-region (point-min) (point-max))
9563             (widen)
9564             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9565
9566 (defun gnus-summary-stop-page-breaking ()
9567   "Stop page breaking in the current article."
9568   (interactive)
9569   (gnus-summary-select-article)
9570   (gnus-eval-in-buffer-window gnus-article-buffer
9571     (widen)
9572     (when (gnus-visual-p 'page-marker)
9573       (let ((inhibit-read-only t))
9574         (gnus-remove-text-with-property 'gnus-prev)
9575         (gnus-remove-text-with-property 'gnus-next))
9576       (setq gnus-page-broken nil))))
9577
9578 (defun gnus-summary-move-article (&optional n to-newsgroup
9579                                             select-method action)
9580   "Move the current article to a different newsgroup.
9581 If N is a positive number, move the N next articles.
9582 If N is a negative number, move the N previous articles.
9583 If N is nil and any articles have been marked with the process mark,
9584 move those articles instead.
9585 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9586 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9587 re-spool using this method.
9588
9589 When called interactively with TO-NEWSGROUP being nil, the value of
9590 the variable `gnus-move-split-methods' is used for finding a default
9591 for the target newsgroup.
9592
9593 For this function to work, both the current newsgroup and the
9594 newsgroup that you want to move to have to support the `request-move'
9595 and `request-accept' functions.
9596
9597 ACTION can be either `move' (the default), `crosspost' or `copy'."
9598   (interactive "P")
9599   (unless action
9600     (setq action 'move))
9601   ;; Check whether the source group supports the required functions.
9602   (cond ((and (eq action 'move)
9603               (not (gnus-check-backend-function
9604                     'request-move-article gnus-newsgroup-name)))
9605          (error "The current group does not support article moving"))
9606         ((and (eq action 'crosspost)
9607               (not (gnus-check-backend-function
9608                     'request-replace-article gnus-newsgroup-name)))
9609          (error "The current group does not support article editing")))
9610   (let ((articles (gnus-summary-work-articles n))
9611         (prefix (if (gnus-check-backend-function
9612                      'request-move-article gnus-newsgroup-name)
9613                     (funcall gnus-move-group-prefix-function
9614                              gnus-newsgroup-name)
9615                   ""))
9616         (names '((move "Move" "Moving")
9617                  (copy "Copy" "Copying")
9618                  (crosspost "Crosspost" "Crossposting")))
9619         (copy-buf (save-excursion
9620                     (nnheader-set-temp-buffer " *copy article*")))
9621         art-group to-method new-xref article to-groups
9622         articles-to-update-marks encoded)
9623     (unless (assq action names)
9624       (error "Unknown action %s" action))
9625     ;; Read the newsgroup name.
9626     (when (and (not to-newsgroup)
9627                (not select-method))
9628       (if (and gnus-move-split-methods
9629                (not
9630                 (and (memq gnus-current-article articles)
9631                      (gnus-buffer-live-p gnus-original-article-buffer))))
9632           ;; When `gnus-move-split-methods' is non-nil, we have to
9633           ;; select an article to give `gnus-read-move-group-name' an
9634           ;; opportunity to suggest an appropriate default.  However,
9635           ;; we needn't render or mark the article.
9636           (let ((gnus-display-mime-function nil)
9637                 (gnus-article-prepare-hook nil)
9638                 (gnus-mark-article-hook nil))
9639             (gnus-summary-select-article nil nil nil (car articles))))
9640       (setq to-newsgroup (gnus-read-move-group-name
9641                           (cadr (assq action names))
9642                           (symbol-value
9643                            (intern (format "gnus-current-%s-group" action)))
9644                           articles prefix)
9645             encoded to-newsgroup
9646             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9647       (set (intern (format "gnus-current-%s-group" action))
9648            (mm-decode-coding-string
9649             to-newsgroup
9650             (gnus-group-name-charset to-method to-newsgroup))))
9651     (unless to-method
9652       (setq to-method (or select-method
9653                           (gnus-server-to-method
9654                            (gnus-group-method to-newsgroup)))))
9655     (setq to-newsgroup
9656           (or encoded
9657               (and to-newsgroup
9658                    (mm-encode-coding-string
9659                     to-newsgroup
9660                     (gnus-group-name-charset to-method to-newsgroup)))))
9661     ;; Check the method we are to move this article to...
9662     (unless (gnus-check-backend-function
9663              'request-accept-article (car to-method))
9664       (error "%s does not support article copying" (car to-method)))
9665     (unless (gnus-check-server to-method)
9666       (error "Can't open server %s" (car to-method)))
9667     (gnus-message 6 "%s to %s: %s..."
9668                   (caddr (assq action names))
9669                   (or (car select-method)
9670                       (gnus-group-decoded-name to-newsgroup))
9671                   articles)
9672     (while articles
9673       (setq article (pop articles))
9674       (setq
9675        art-group
9676        (cond
9677         ;; Move the article.
9678         ((eq action 'move)
9679          ;; Remove this article from future suppression.
9680          (gnus-dup-unsuppress-article article)
9681          (let* ((from-method (gnus-find-method-for-group
9682                               gnus-newsgroup-name))
9683                 (to-method (or select-method
9684                                (gnus-find-method-for-group to-newsgroup)))
9685                 (move-is-internal (gnus-server-equal from-method to-method)))
9686            (gnus-request-move-article
9687             article                     ; Article to move
9688             gnus-newsgroup-name         ; From newsgroup
9689             (nth 1 (gnus-find-method-for-group
9690                     gnus-newsgroup-name)) ; Server
9691             (list 'gnus-request-accept-article
9692                   to-newsgroup (list 'quote select-method)
9693                   (not articles) t)     ; Accept form
9694             (not articles)              ; Only save nov last time
9695             (and move-is-internal
9696                  to-newsgroup           ; Not respooling
9697                  (gnus-group-real-name to-newsgroup))))) ; Is this move internal?
9698         ;; Copy the article.
9699         ((eq action 'copy)
9700          (with-current-buffer copy-buf
9701            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9702              (save-restriction
9703                (nnheader-narrow-to-headers)
9704                (dolist (hdr gnus-copy-article-ignored-headers)
9705                  (message-remove-header hdr t)))
9706              (gnus-request-accept-article
9707               to-newsgroup select-method (not articles) t))))
9708         ;; Crosspost the article.
9709         ((eq action 'crosspost)
9710          (let ((xref (message-tokenize-header
9711                       (mail-header-xref (gnus-summary-article-header article))
9712                       " ")))
9713            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9714                                   ":" (number-to-string article)))
9715            (unless xref
9716              (setq xref (list (system-name))))
9717            (setq new-xref
9718                  (concat
9719                   (mapconcat 'identity
9720                              (delete "Xref:" (delete new-xref xref))
9721                              " ")
9722                   " " new-xref))
9723            (with-current-buffer copy-buf
9724              ;; First put the article in the destination group.
9725              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9726              (when (consp (setq art-group
9727                                 (gnus-request-accept-article
9728                                  to-newsgroup select-method (not articles) t)))
9729                (setq new-xref (concat new-xref " " (car art-group)
9730                                       ":"
9731                                       (number-to-string (cdr art-group))))
9732                ;; Now we have the new Xrefs header, so we insert
9733                ;; it and replace the new article.
9734                (nnheader-replace-header "Xref" new-xref)
9735                (gnus-request-replace-article
9736                 (cdr art-group) to-newsgroup (current-buffer) t)
9737                art-group))))))
9738       (cond
9739        ((not art-group)
9740         (gnus-message 1 "Couldn't %s article %s: %s"
9741                       (cadr (assq action names)) article
9742                       (nnheader-get-report (car to-method))))
9743        ((eq art-group 'junk)
9744         (when (eq action 'move)
9745           (gnus-summary-mark-article article gnus-canceled-mark)
9746           (gnus-message 4 "Deleted article %s" article)
9747           ;; run the delete hook
9748           (run-hook-with-args 'gnus-summary-article-delete-hook
9749                               action
9750                               (gnus-data-header
9751                                (assoc article (gnus-data-list nil)))
9752                               gnus-newsgroup-name nil
9753                               select-method)))
9754        (t
9755         (let* ((pto-group (gnus-group-prefixed-name
9756                            (car art-group) to-method))
9757                (info (gnus-get-info pto-group))
9758                (to-group (gnus-info-group info))
9759                to-marks)
9760           ;; Update the group that has been moved to.
9761           (when (and info
9762                      (memq action '(move copy)))
9763             (unless (member to-group to-groups)
9764               (push to-group to-groups))
9765
9766             (unless (memq article gnus-newsgroup-unreads)
9767               (push 'read to-marks)
9768               (gnus-info-set-read
9769                info (gnus-add-to-range (gnus-info-read info)
9770                                        (list (cdr art-group)))))
9771
9772             ;; See whether the article is to be put in the cache.
9773             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9774                    (marks (if expirable
9775                               gnus-article-mark-lists
9776                             (delete '(expirable . expire)
9777                                     (copy-sequence gnus-article-mark-lists))))
9778                    (to-article (cdr art-group)))
9779
9780               ;; Enter the article into the cache in the new group,
9781               ;; if that is required.
9782               (when gnus-use-cache
9783                 (gnus-cache-possibly-enter-article
9784                  to-group to-article
9785                  (memq article gnus-newsgroup-marked)
9786                  (memq article gnus-newsgroup-dormant)
9787                  (memq article gnus-newsgroup-unreads)))
9788
9789               (when gnus-preserve-marks
9790                 ;; Copy any marks over to the new group.
9791                 (when (and (equal to-group gnus-newsgroup-name)
9792                            (not (memq article gnus-newsgroup-unreads)))
9793                   ;; Mark this article as read in this group.
9794                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9795                   (setcdr (gnus-active to-group) to-article)
9796                   (setcdr gnus-newsgroup-active to-article))
9797
9798                 (while marks
9799                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9800                     (when (memq article (symbol-value
9801                                          (intern (format "gnus-newsgroup-%s"
9802                                                          (caar marks)))))
9803                       (push (cdar marks) to-marks)
9804                       ;; If the other group is the same as this group,
9805                       ;; then we have to add the mark to the list.
9806                       (when (equal to-group gnus-newsgroup-name)
9807                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9808                              (cons to-article
9809                                    (symbol-value
9810                                     (intern (format "gnus-newsgroup-%s"
9811                                                     (caar marks)))))))
9812                       ;; Copy the marks to other group.
9813                       (gnus-add-marked-articles
9814                        to-group (cdar marks) (list to-article) info)))
9815                   (setq marks (cdr marks)))
9816
9817                 (when (and expirable
9818                            gnus-mark-copied-or-moved-articles-as-expirable
9819                            (not (memq 'expire to-marks)))
9820                   ;; Mark this article as expirable.
9821                   (push 'expire to-marks)
9822                   (when (equal to-group gnus-newsgroup-name)
9823                     (push to-article gnus-newsgroup-expirable))
9824                   ;; Copy the expirable mark to other group.
9825                   (gnus-add-marked-articles
9826                    to-group 'expire (list to-article) info))
9827
9828                 (when to-marks
9829                   (gnus-request-set-mark
9830                    to-group (list (list (list to-article) 'add to-marks)))))
9831
9832               (gnus-dribble-enter
9833                (concat "(gnus-group-set-info '"
9834                        (gnus-prin1-to-string (gnus-get-info to-group))
9835                        ")"))))
9836
9837           ;; Update the Xref header in this article to point to
9838           ;; the new crossposted article we have just created.
9839           (when (eq action 'crosspost)
9840             (with-current-buffer copy-buf
9841               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9842               (nnheader-replace-header "Xref" new-xref)
9843               (gnus-request-replace-article
9844                article gnus-newsgroup-name (current-buffer) t)))
9845
9846           ;; run the move/copy/crosspost/respool hook
9847           (run-hook-with-args 'gnus-summary-article-move-hook
9848                               action
9849                               (gnus-data-header
9850                                (assoc article (gnus-data-list nil)))
9851                               gnus-newsgroup-name
9852                               to-newsgroup
9853                               select-method))
9854
9855         ;;;!!!Why is this necessary?
9856         (set-buffer gnus-summary-buffer)
9857
9858         (when (eq action 'move)
9859           (save-excursion
9860             (gnus-summary-goto-subject article)
9861             (gnus-summary-mark-article article gnus-canceled-mark)))))
9862       (push article articles-to-update-marks))
9863
9864     (save-excursion
9865       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9866     ;; Re-activate all groups that have been moved to.
9867     (with-current-buffer gnus-group-buffer
9868       (let ((gnus-group-marked to-groups))
9869         (gnus-group-get-new-news-this-group nil t)))
9870
9871     (gnus-kill-buffer copy-buf)
9872     (gnus-summary-position-point)
9873     (gnus-set-mode-line 'summary)))
9874
9875 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9876   "Copy the current article to some other group.
9877 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9878 When called interactively, if TO-NEWSGROUP is nil, use the value of
9879 the variable `gnus-move-split-methods' for finding a default target
9880 newsgroup.
9881 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9882 re-spool using this method."
9883   (interactive "P")
9884   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9885
9886 (defun gnus-summary-crosspost-article (&optional n)
9887   "Crosspost the current article to some other group."
9888   (interactive "P")
9889   (gnus-summary-move-article n nil nil 'crosspost))
9890
9891 (defcustom gnus-summary-respool-default-method nil
9892   "Default method type for respooling an article.
9893 If nil, use to the current newsgroup method."
9894   :type 'symbol
9895   :group 'gnus-summary-mail)
9896
9897 (defun gnus-summary-respool-article (&optional n method)
9898   "Respool the current article.
9899 The article will be squeezed through the mail spooling process again,
9900 which means that it will be put in some mail newsgroup or other
9901 depending on `nnmail-split-methods'.
9902 If N is a positive number, respool the N next articles.
9903 If N is a negative number, respool the N previous articles.
9904 If N is nil and any articles have been marked with the process mark,
9905 respool those articles instead.
9906
9907 Respooling can be done both from mail groups and \"real\" newsgroups.
9908 In the former case, the articles in question will be moved from the
9909 current group into whatever groups they are destined to.  In the
9910 latter case, they will be copied into the relevant groups."
9911   (interactive
9912    (list current-prefix-arg
9913          (let* ((methods (gnus-methods-using 'respool))
9914                 (methname
9915                  (symbol-name (or gnus-summary-respool-default-method
9916                                   (car (gnus-find-method-for-group
9917                                         gnus-newsgroup-name)))))
9918                 (method
9919                  (gnus-completing-read-with-default
9920                   methname "Backend to use when respooling"
9921                   methods nil t nil 'gnus-mail-method-history))
9922                 ms)
9923            (cond
9924             ((zerop (length (setq ms (gnus-servers-using-backend
9925                                       (intern method)))))
9926              (list (intern method) ""))
9927             ((= 1 (length ms))
9928              (car ms))
9929             (t
9930              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9931                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9932                            ms-alist))))))))
9933   (unless method
9934     (error "No method given for respooling"))
9935   (if (assoc (symbol-name
9936               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9937              (gnus-methods-using 'respool))
9938       (gnus-summary-move-article n nil method)
9939     (gnus-summary-copy-article n nil method)))
9940
9941 (defun gnus-summary-import-article (file &optional edit)
9942   "Import an arbitrary file into a mail newsgroup."
9943   (interactive "fImport file: \nP")
9944   (let ((group gnus-newsgroup-name)
9945         (now (current-time))
9946         atts lines group-art)
9947     (unless (gnus-check-backend-function 'request-accept-article group)
9948       (error "%s does not support article importing" group))
9949     (or (file-readable-p file)
9950         (not (file-regular-p file))
9951         (error "Can't read %s" file))
9952     (with-current-buffer (gnus-get-buffer-create " *import file*")
9953       (erase-buffer)
9954       (nnheader-insert-file-contents file)
9955       (goto-char (point-min))
9956       (if (nnheader-article-p)
9957           (save-restriction
9958             (goto-char (point-min))
9959             (search-forward "\n\n" nil t)
9960             (narrow-to-region (point-min) (1- (point)))
9961             (goto-char (point-min))
9962             (unless (re-search-forward "^date:" nil t)
9963               (goto-char (point-max))
9964               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9965        ;; This doesn't look like an article, so we fudge some headers.
9966         (setq atts (file-attributes file)
9967               lines (count-lines (point-min) (point-max)))
9968         (insert "From: " (read-string "From: ") "\n"
9969                 "Subject: " (read-string "Subject: ") "\n"
9970                 "Date: " (message-make-date (nth 5 atts)) "\n"
9971                 "Message-ID: " (message-make-message-id) "\n"
9972                 "Lines: " (int-to-string lines) "\n"
9973                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9974       (setq group-art (gnus-request-accept-article group nil t))
9975       (kill-buffer (current-buffer)))
9976     (setq gnus-newsgroup-active (gnus-activate-group group))
9977     (forward-line 1)
9978     (gnus-summary-goto-article (cdr group-art) nil t)
9979     (when edit
9980       (gnus-summary-edit-article))))
9981
9982 (defun gnus-summary-create-article ()
9983   "Create an article in a mail newsgroup."
9984   (interactive)
9985   (let ((group gnus-newsgroup-name)
9986         (now (current-time))
9987         group-art)
9988     (unless (gnus-check-backend-function 'request-accept-article group)
9989       (error "%s does not support article importing" group))
9990     (with-current-buffer (gnus-get-buffer-create " *import file*")
9991       (erase-buffer)
9992       (goto-char (point-min))
9993       ;; This doesn't look like an article, so we fudge some headers.
9994       (insert "From: " (read-string "From: ") "\n"
9995               "Subject: " (read-string "Subject: ") "\n"
9996               "Date: " (message-make-date now) "\n"
9997               "Message-ID: " (message-make-message-id) "\n")
9998       (setq group-art (gnus-request-accept-article group nil t))
9999       (kill-buffer (current-buffer)))
10000     (setq gnus-newsgroup-active (gnus-activate-group group))
10001     (forward-line 1)
10002     (gnus-summary-goto-article (cdr group-art) nil t)
10003     (gnus-summary-edit-article)))
10004
10005 (defun gnus-summary-article-posted-p ()
10006   "Say whether the current (mail) article is available from news as well.
10007 This will be the case if the article has both been mailed and posted."
10008   (interactive)
10009   (let ((id (mail-header-references (gnus-summary-article-header)))
10010         (gnus-override-method (car (gnus-refer-article-methods))))
10011     (if (gnus-request-head id "")
10012         (gnus-message 2 "The current message was found on %s"
10013                       gnus-override-method)
10014       (gnus-message 2 "The current message couldn't be found on %s"
10015                     gnus-override-method)
10016       nil)))
10017
10018 (defun gnus-summary-expire-articles (&optional now)
10019   "Expire all articles that are marked as expirable in the current group."
10020   (interactive)
10021   (when (and (not gnus-group-is-exiting-without-update-p)
10022              (gnus-check-backend-function
10023               'request-expire-articles gnus-newsgroup-name))
10024     ;; This backend supports expiry.
10025     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10026            (expirable (if total
10027                           (progn
10028                             ;; We need to update the info for
10029                             ;; this group for `gnus-list-of-read-articles'
10030                             ;; to give us the right answer.
10031                             (gnus-run-hooks 'gnus-exit-group-hook)
10032                             (gnus-summary-update-info)
10033                             (gnus-list-of-read-articles gnus-newsgroup-name))
10034                         (setq gnus-newsgroup-expirable
10035                               (sort gnus-newsgroup-expirable '<))))
10036            (expiry-wait (if now 'immediate
10037                           (gnus-group-find-parameter
10038                            gnus-newsgroup-name 'expiry-wait)))
10039            (nnmail-expiry-target
10040             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10041                 nnmail-expiry-target))
10042            es)
10043       (when expirable
10044         ;; There are expirable articles in this group, so we run them
10045         ;; through the expiry process.
10046         (gnus-message 6 "Expiring articles...")
10047         (unless (gnus-check-group gnus-newsgroup-name)
10048           (error "Can't open server for %s" gnus-newsgroup-name))
10049         ;; The list of articles that weren't expired is returned.
10050         (save-excursion
10051           (if expiry-wait
10052               (let ((nnmail-expiry-wait-function nil)
10053                     (nnmail-expiry-wait expiry-wait))
10054                 (setq es (gnus-request-expire-articles
10055                           expirable gnus-newsgroup-name)))
10056             (setq es (gnus-request-expire-articles
10057                       expirable gnus-newsgroup-name)))
10058           (unless total
10059             (setq gnus-newsgroup-expirable es))
10060           ;; We go through the old list of expirable, and mark all
10061           ;; really expired articles as nonexistent.
10062           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10063             (let ((gnus-use-cache nil))
10064               (dolist (article expirable)
10065                 (when (and (not (memq article es))
10066                            (gnus-data-find article))
10067                   (gnus-summary-mark-article article gnus-canceled-mark)
10068                   (run-hook-with-args 'gnus-summary-article-expire-hook
10069                                       'delete
10070                                       (gnus-data-header
10071                                        (assoc article (gnus-data-list nil)))
10072                                       gnus-newsgroup-name
10073                                       nil
10074                                       nil))))))
10075         (gnus-message 6 "Expiring articles...done")))))
10076
10077 (defun gnus-summary-expire-articles-now ()
10078   "Expunge all expirable articles in the current group.
10079 This means that *all* articles that are marked as expirable will be
10080 deleted forever, right now."
10081   (interactive)
10082   (or gnus-expert-user
10083       (gnus-yes-or-no-p
10084        "Are you really, really sure you want to delete all expirable messages? ")
10085       (error "Phew!"))
10086   (gnus-summary-expire-articles t))
10087
10088 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10089 (defun gnus-summary-delete-article (&optional n)
10090   "Delete the N next (mail) articles.
10091 This command actually deletes articles.  This is not a marking
10092 command.  The article will disappear forever from your life, never to
10093 return.
10094
10095 If N is negative, delete backwards.
10096 If N is nil and articles have been marked with the process mark,
10097 delete these instead.
10098
10099 If `gnus-novice-user' is non-nil you will be asked for
10100 confirmation before the articles are deleted."
10101   (interactive "P")
10102   (unless (gnus-check-backend-function 'request-expire-articles
10103                                        gnus-newsgroup-name)
10104     (error "The current newsgroup does not support article deletion"))
10105   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10106     (error "Couldn't open server"))
10107   ;; Compute the list of articles to delete.
10108   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10109         (nnmail-expiry-target 'delete)
10110         not-deleted)
10111     (if (and gnus-novice-user
10112              (not (gnus-yes-or-no-p
10113                    (format "Do you really want to delete %s forever? "
10114                            (if (> (length articles) 1)
10115                                (format "these %s articles" (length articles))
10116                              "this article")))))
10117         ()
10118       ;; Delete the articles.
10119       (setq not-deleted (gnus-request-expire-articles
10120                          articles gnus-newsgroup-name 'force))
10121       (save-excursion
10122         (while articles
10123           (gnus-summary-remove-process-mark (car articles))
10124           ;; The backend might not have been able to delete the article
10125           ;; after all.
10126           (unless (memq (car articles) not-deleted)
10127             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10128           (let* ((article (car articles))
10129                  (ghead  (gnus-data-header
10130                           (assoc article (gnus-data-list nil)))))
10131             (run-hook-with-args 'gnus-summary-article-delete-hook
10132                                 'delete ghead gnus-newsgroup-name nil
10133                                 nil))
10134           (setq articles (cdr articles))))
10135       (when not-deleted
10136         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10137     (gnus-summary-position-point)
10138     (gnus-set-mode-line 'summary)
10139     not-deleted))
10140
10141 (defun gnus-summary-edit-article (&optional arg)
10142   "Edit the current article.
10143 This will have permanent effect only in mail groups.
10144 If ARG is nil, edit the decoded articles.
10145 If ARG is 1, edit the raw articles.
10146 If ARG is 2, edit the raw articles even in read-only groups.
10147 If ARG is 3, edit the articles with the current handles.
10148 Otherwise, allow editing of articles even in read-only
10149 groups."
10150   (interactive "P")
10151   (let (force raw current-handles)
10152     (cond
10153      ((null arg))
10154      ((eq arg 1)
10155       (setq raw t))
10156      ((eq arg 2)
10157       (setq raw t
10158             force t))
10159      ((eq arg 3)
10160       (setq current-handles
10161             (and (gnus-buffer-live-p gnus-article-buffer)
10162                  (with-current-buffer gnus-article-buffer
10163                    (prog1
10164                        gnus-article-mime-handles
10165                      (setq gnus-article-mime-handles nil))))))
10166      (t
10167       (setq force t)))
10168     (when (and raw (not force)
10169                (member gnus-newsgroup-name '("nndraft:delayed"
10170                                              "nndraft:drafts"
10171                                              "nndraft:queue")))
10172       (error "Can't edit the raw article in group %s"
10173              gnus-newsgroup-name))
10174     (with-current-buffer gnus-summary-buffer
10175       (let ((mail-parse-charset gnus-newsgroup-charset)
10176             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10177         (gnus-set-global-variables)
10178         (when (and (not force)
10179                    (gnus-group-read-only-p))
10180           (error "The current newsgroup does not support article editing"))
10181         (gnus-summary-show-article t)
10182         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10183           (with-current-buffer gnus-article-buffer
10184             (mm-enable-multibyte)))
10185         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10186             (setq raw t))
10187         (gnus-article-edit-article
10188          (if raw 'ignore
10189            `(lambda ()
10190               (let ((mbl mml-buffer-list))
10191                 (setq mml-buffer-list nil)
10192                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10193                   (mime-to-mml ,'current-handles))
10194                 (let ((mbl1 mml-buffer-list))
10195                   (setq mml-buffer-list mbl)
10196                   (set (make-local-variable 'mml-buffer-list) mbl1))
10197                 (gnus-make-local-hook 'kill-buffer-hook)
10198                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10199          `(lambda (no-highlight)
10200             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10201                   (message-options message-options)
10202                   (message-options-set-recipient)
10203                   (mail-parse-ignored-charsets
10204                    ',gnus-newsgroup-ignored-charsets)
10205                   (rfc2047-header-encoding-alist
10206                    ',(let ((charset (gnus-group-name-charset
10207                                      (gnus-find-method-for-group
10208                                       gnus-newsgroup-name)
10209                                      gnus-newsgroup-name)))
10210                        (append (list (cons "Newsgroups" charset)
10211                                      (cons "Followup-To" charset)
10212                                      (cons "Xref" charset))
10213                                rfc2047-header-encoding-alist))))
10214               ,(if (not raw) '(progn
10215                                 (mml-to-mime)
10216                                 (mml-destroy-buffers)
10217                                 (remove-hook 'kill-buffer-hook
10218                                              'mml-destroy-buffers t)
10219                                 (kill-local-variable 'mml-buffer-list)))
10220               (gnus-summary-edit-article-done
10221                ,(or (mail-header-references gnus-current-headers) "")
10222                ,(gnus-group-read-only-p)
10223                ,gnus-summary-buffer no-highlight))))))))
10224
10225 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10226
10227 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10228                                                  no-highlight)
10229   "Make edits to the current article permanent."
10230   (interactive)
10231   (save-excursion
10232    ;; The buffer restriction contains the entire article if it exists.
10233     (when (article-goto-body)
10234       (let ((lines (count-lines (point) (point-max)))
10235             (length (- (point-max) (point)))
10236             (case-fold-search t)
10237             (body (copy-marker (point))))
10238         (goto-char (point-min))
10239         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10240           (delete-region (match-beginning 1) (match-end 1))
10241           (insert (number-to-string length)))
10242         (goto-char (point-min))
10243         (when (re-search-forward
10244                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10245           (delete-region (match-beginning 1) (match-end 1))
10246           (insert (number-to-string length)))
10247         (goto-char (point-min))
10248         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10249           (delete-region (match-beginning 1) (match-end 1))
10250           (insert (number-to-string lines))))))
10251   ;; Replace the article.
10252   (let ((buf (current-buffer)))
10253     (with-temp-buffer
10254       (insert-buffer-substring buf)
10255
10256       (if (and (not read-only)
10257                (not (gnus-request-replace-article
10258                      (cdr gnus-article-current) (car gnus-article-current)
10259                      (current-buffer) t)))
10260           (error "Couldn't replace article")
10261         ;; Update the summary buffer.
10262         (if (and references
10263                  (equal (message-tokenize-header references " ")
10264                         (message-tokenize-header
10265                          (or (message-fetch-field "references") "") " ")))
10266             ;; We only have to update this line.
10267             (save-excursion
10268               (save-restriction
10269                 (message-narrow-to-head)
10270                 (let ((head (buffer-substring-no-properties
10271                              (point-min) (point-max)))
10272                       header)
10273                   (with-temp-buffer
10274                     (insert (format "211 %d Article retrieved.\n"
10275                                     (cdr gnus-article-current)))
10276                     (insert head)
10277                     (insert ".\n")
10278                     (let ((nntp-server-buffer (current-buffer)))
10279                       (setq header (car (gnus-get-newsgroup-headers
10280                                          nil t))))
10281                     (with-current-buffer gnus-summary-buffer
10282                       (gnus-data-set-header
10283                        (gnus-data-find (cdr gnus-article-current))
10284                        header)
10285                       (gnus-summary-update-article-line
10286                        (cdr gnus-article-current) header)
10287                       (if (gnus-summary-goto-subject
10288                            (cdr gnus-article-current) nil t)
10289                           (gnus-summary-update-secondary-mark
10290                            (cdr gnus-article-current))))))))
10291           ;; Update threads.
10292           (set-buffer (or buffer gnus-summary-buffer))
10293           (gnus-summary-update-article (cdr gnus-article-current))
10294           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10295               (gnus-summary-update-secondary-mark
10296                (cdr gnus-article-current))))
10297         ;; Prettify the article buffer again.
10298         (unless no-highlight
10299           (with-current-buffer gnus-article-buffer
10300             ;;;!!! Fix this -- article should be rehighlighted.
10301             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10302             (set-buffer gnus-original-article-buffer)
10303             (gnus-request-article
10304              (cdr gnus-article-current)
10305              (car gnus-article-current) (current-buffer))))
10306         ;; Prettify the summary buffer line.
10307         (when (gnus-visual-p 'summary-highlight 'highlight)
10308           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10309
10310 (defun gnus-summary-edit-wash (key)
10311   "Perform editing command KEY in the article buffer."
10312   (interactive
10313    (list
10314     (progn
10315       (message "%s" (concat (this-command-keys) "- "))
10316       (read-char))))
10317   (message "")
10318   (gnus-summary-edit-article)
10319   (execute-kbd-macro (concat (this-command-keys) key))
10320   (gnus-article-edit-done))
10321
10322 ;;; Respooling
10323
10324 (defun gnus-summary-respool-query (&optional silent trace)
10325   "Query where the respool algorithm would put this article."
10326   (interactive)
10327   (let (gnus-mark-article-hook)
10328     (gnus-summary-select-article)
10329     (with-current-buffer gnus-original-article-buffer
10330       (let ((groups (nnmail-article-group 'identity trace)))
10331         (unless silent
10332           (if groups
10333               (message "This message would go to %s"
10334                        (mapconcat 'car groups ", "))
10335             (message "This message would go to no groups"))
10336           groups)))))
10337
10338 (defun gnus-summary-respool-trace ()
10339   "Trace where the respool algorithm would put this article.
10340 Display a buffer showing all fancy splitting patterns which matched."
10341   (interactive)
10342   (gnus-summary-respool-query nil t))
10343
10344 ;; Summary marking commands.
10345
10346 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10347   "Mark articles which has the same subject as read, and then select the next.
10348 If UNMARK is positive, remove any kind of mark.
10349 If UNMARK is negative, tick articles."
10350   (interactive "P")
10351   (when unmark
10352     (setq unmark (prefix-numeric-value unmark)))
10353   (let ((count
10354          (gnus-summary-mark-same-subject
10355           (gnus-summary-article-subject) unmark)))
10356     ;; Select next unread article.  If auto-select-same mode, should
10357     ;; select the first unread article.
10358     (gnus-summary-next-article t (and gnus-auto-select-same
10359                                       (gnus-summary-article-subject)))
10360     (gnus-message 7 "%d article%s marked as %s"
10361                   count (if (= count 1) " is" "s are")
10362                   (if unmark "unread" "read"))))
10363
10364 (defun gnus-summary-kill-same-subject (&optional unmark)
10365   "Mark articles which has the same subject as read.
10366 If UNMARK is positive, remove any kind of mark.
10367 If UNMARK is negative, tick articles."
10368   (interactive "P")
10369   (when unmark
10370     (setq unmark (prefix-numeric-value unmark)))
10371   (let ((count
10372          (gnus-summary-mark-same-subject
10373           (gnus-summary-article-subject) unmark)))
10374     ;; If marked as read, go to next unread subject.
10375     (when (null unmark)
10376       ;; Go to next unread subject.
10377       (gnus-summary-next-subject 1 t))
10378     (gnus-message 7 "%d articles are marked as %s"
10379                   count (if unmark "unread" "read"))))
10380
10381 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10382   "Mark articles with same SUBJECT as read, and return marked number.
10383 If optional argument UNMARK is positive, remove any kinds of marks.
10384 If optional argument UNMARK is negative, mark articles as unread instead."
10385   (let ((count 1))
10386     (save-excursion
10387       (cond
10388        ((null unmark)                   ; Mark as read.
10389         (while (and
10390                 (progn
10391                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10392                   (gnus-summary-show-thread) t)
10393                 (gnus-summary-find-subject subject))
10394           (setq count (1+ count))))
10395        ((> unmark 0)                    ; Tick.
10396         (while (and
10397                 (progn
10398                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10399                   (gnus-summary-show-thread) t)
10400                 (gnus-summary-find-subject subject))
10401           (setq count (1+ count))))
10402        (t                               ; Mark as unread.
10403         (while (and
10404                 (progn
10405                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10406                   (gnus-summary-show-thread) t)
10407                 (gnus-summary-find-subject subject))
10408           (setq count (1+ count)))))
10409       (gnus-set-mode-line 'summary)
10410       ;; Return the number of marked articles.
10411       count)))
10412
10413 (defun gnus-summary-mark-as-processable (n &optional unmark)
10414   "Set the process mark on the next N articles.
10415 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10416 the process mark instead.  The difference between N and the actual
10417 number of articles marked is returned."
10418   (interactive "P")
10419   (if (and (null n) (gnus-region-active-p))
10420       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10421     (setq n (prefix-numeric-value n))
10422     (let ((backward (< n 0))
10423           (n (abs n)))
10424       (while (and
10425               (> n 0)
10426               (if unmark
10427                   (gnus-summary-remove-process-mark
10428                    (gnus-summary-article-number))
10429                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10430               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10431         (setq n (1- n)))
10432       (when (/= 0 n)
10433         (gnus-message 7 "No more articles"))
10434       (gnus-summary-recenter)
10435       (gnus-summary-position-point)
10436       n)))
10437
10438 (defun gnus-summary-unmark-as-processable (n)
10439   "Remove the process mark from the next N articles.
10440 If N is negative, unmark backward instead.  The difference between N and
10441 the actual number of articles unmarked is returned."
10442   (interactive "P")
10443   (gnus-summary-mark-as-processable n t))
10444
10445 (defun gnus-summary-unmark-all-processable ()
10446   "Remove the process mark from all articles."
10447   (interactive)
10448   (save-excursion
10449     (while gnus-newsgroup-processable
10450       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10451   (gnus-summary-position-point))
10452
10453 (defun gnus-summary-add-mark (article type)
10454   "Mark ARTICLE with a mark of TYPE."
10455   (let ((vtype (car (assq type gnus-article-mark-lists)))
10456         var)
10457     (if (not vtype)
10458         (error "No such mark type: %s" type)
10459       (setq var (intern (format "gnus-newsgroup-%s" type)))
10460       (set var (cons article (symbol-value var)))
10461       (if (memq type '(processable cached replied forwarded recent saved))
10462           (gnus-summary-update-secondary-mark article)
10463         ;;; !!! This is bogus.  We should find out what primary
10464         ;;; !!! mark we want to set.
10465         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10466
10467 (defun gnus-summary-mark-as-expirable (n)
10468   "Mark N articles forward as expirable.
10469 If N is negative, mark backward instead.  The difference between N and
10470 the actual number of articles marked is returned."
10471   (interactive "p")
10472   (gnus-summary-mark-forward n gnus-expirable-mark))
10473
10474 (defun gnus-summary-mark-as-spam (n)
10475   "Mark N articles forward as spam.
10476 If N is negative, mark backward instead.  The difference between N and
10477 the actual number of articles marked is returned."
10478   (interactive "p")
10479   (gnus-summary-mark-forward n gnus-spam-mark))
10480
10481 (defun gnus-summary-mark-article-as-replied (article)
10482   "Mark ARTICLE as replied to and update the summary line.
10483 ARTICLE can also be a list of articles."
10484   (interactive (list (gnus-summary-article-number)))
10485   (let ((articles (if (listp article) article (list article))))
10486     (dolist (article articles)
10487       (unless (numberp article)
10488         (error "%s is not a number" article))
10489       (push article gnus-newsgroup-replied)
10490       (let ((inhibit-read-only t))
10491         (when (gnus-summary-goto-subject article nil t)
10492           (gnus-summary-update-secondary-mark article))))))
10493
10494 (defun gnus-summary-mark-article-as-forwarded (article)
10495   "Mark ARTICLE as forwarded and update the summary line.
10496 ARTICLE can also be a list of articles."
10497   (let ((articles (if (listp article) article (list article))))
10498     (dolist (article articles)
10499       (push article gnus-newsgroup-forwarded)
10500       (let ((inhibit-read-only t))
10501         (when (gnus-summary-goto-subject article nil t)
10502           (gnus-summary-update-secondary-mark article))))))
10503
10504 (defun gnus-summary-set-bookmark (article)
10505   "Set a bookmark in current article."
10506   (interactive (list (gnus-summary-article-number)))
10507   (when (or (not (get-buffer gnus-article-buffer))
10508             (not gnus-current-article)
10509             (not gnus-article-current)
10510             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10511     (error "No current article selected"))
10512   ;; Remove old bookmark, if one exists.
10513   (gnus-pull article gnus-newsgroup-bookmarks)
10514   ;; Set the new bookmark, which is on the form
10515   ;; (article-number . line-number-in-body).
10516   (push
10517    (cons article
10518          (with-current-buffer gnus-article-buffer
10519            (count-lines
10520             (min (point)
10521                  (save-excursion
10522                    (article-goto-body)
10523                    (point)))
10524             (point))))
10525    gnus-newsgroup-bookmarks)
10526   (gnus-message 6 "A bookmark has been added to the current article."))
10527
10528 (defun gnus-summary-remove-bookmark (article)
10529   "Remove the bookmark from the current article."
10530   (interactive (list (gnus-summary-article-number)))
10531   ;; Remove old bookmark, if one exists.
10532   (if (not (assq article gnus-newsgroup-bookmarks))
10533       (gnus-message 6 "No bookmark in current article.")
10534     (gnus-pull article gnus-newsgroup-bookmarks)
10535     (gnus-message 6 "Removed bookmark.")))
10536
10537 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10538 (defun gnus-summary-mark-as-dormant (n)
10539   "Mark N articles forward as dormant.
10540 If N is negative, mark backward instead.  The difference between N and
10541 the actual number of articles marked is returned."
10542   (interactive "p")
10543   (gnus-summary-mark-forward n gnus-dormant-mark))
10544
10545 (defun gnus-summary-set-process-mark (article)
10546   "Set the process mark on ARTICLE and update the summary line."
10547   (setq gnus-newsgroup-processable
10548         (cons article
10549               (delq article gnus-newsgroup-processable)))
10550   (when (gnus-summary-goto-subject article)
10551     (gnus-summary-show-thread)
10552     (gnus-summary-goto-subject article)
10553     (gnus-summary-update-secondary-mark article)))
10554
10555 (defun gnus-summary-remove-process-mark (&rest articles)
10556   "Remove the process mark from ARTICLES and update the summary line."
10557   (dolist (article articles)
10558     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10559     (when (gnus-summary-goto-subject article)
10560       (gnus-summary-show-thread)
10561       (gnus-summary-goto-subject article)
10562       (gnus-summary-update-secondary-mark article)))
10563   t)
10564
10565 (defun gnus-summary-set-saved-mark (article)
10566   "Set the process mark on ARTICLE and update the summary line."
10567   (push article gnus-newsgroup-saved)
10568   (when (gnus-summary-goto-subject article)
10569     (gnus-summary-update-secondary-mark article)))
10570
10571 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10572   "Mark N articles as read forwards.
10573 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10574 The difference between N and the actual number of articles marked is
10575 returned.
10576 If NO-EXPIRE, auto-expiry will be inhibited."
10577   (interactive "p")
10578   (gnus-summary-show-thread)
10579   (let ((backward (< n 0))
10580         (gnus-summary-goto-unread
10581          (and gnus-summary-goto-unread
10582               (not (eq gnus-summary-goto-unread 'never))
10583               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10584                                     gnus-ticked-mark gnus-dormant-mark)))))
10585         (n (abs n))
10586         (mark (or mark gnus-del-mark)))
10587     (while (and (> n 0)
10588                 (gnus-summary-mark-article nil mark no-expire)
10589                 (zerop (gnus-summary-next-subject
10590                         (if backward -1 1)
10591                         (and gnus-summary-goto-unread
10592                              (not (eq gnus-summary-goto-unread 'never)))
10593                         t)))
10594       (setq n (1- n)))
10595     (when (/= 0 n)
10596       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10597     (gnus-summary-recenter)
10598     (gnus-summary-position-point)
10599     (gnus-set-mode-line 'summary)
10600     n))
10601
10602 (defun gnus-summary-mark-article-as-read (mark)
10603   "Mark the current article quickly as read with MARK."
10604   (let ((article (gnus-summary-article-number)))
10605     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10606     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10607     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10608     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10609     (push (cons article mark) gnus-newsgroup-reads)
10610     ;; Possibly remove from cache, if that is used.
10611     (when gnus-use-cache
10612       (gnus-cache-enter-remove-article article))
10613     ;; Allow the backend to change the mark.
10614     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10615     ;; Check for auto-expiry.
10616     (when (and gnus-newsgroup-auto-expire
10617                (memq mark gnus-auto-expirable-marks))
10618       (setq mark gnus-expirable-mark)
10619       ;; Let the backend know about the mark change.
10620       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10621       (push article gnus-newsgroup-expirable))
10622     ;; Set the mark in the buffer.
10623     (gnus-summary-update-mark mark 'unread)
10624     t))
10625
10626 (defun gnus-summary-mark-article-as-unread (mark)
10627   "Mark the current article quickly as unread with MARK."
10628   (let* ((article (gnus-summary-article-number))
10629          (old-mark (gnus-summary-article-mark article)))
10630     ;; Allow the backend to change the mark.
10631     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10632     (if (eq mark old-mark)
10633         t
10634       (if (<= article 0)
10635           (progn
10636             (gnus-error 1 "Can't mark negative article numbers")
10637             nil)
10638         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10639         (setq gnus-newsgroup-spam-marked
10640               (delq article gnus-newsgroup-spam-marked))
10641         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10642         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10643         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10644         (cond ((= mark gnus-ticked-mark)
10645                (setq gnus-newsgroup-marked
10646                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10647                                               article)))
10648               ((= mark gnus-spam-mark)
10649                (setq gnus-newsgroup-spam-marked
10650                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10651                                               article)))
10652               ((= mark gnus-dormant-mark)
10653                (setq gnus-newsgroup-dormant
10654                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10655                                               article)))
10656               (t
10657                (setq gnus-newsgroup-unreads
10658                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10659                                               article))))
10660         (gnus-pull article gnus-newsgroup-reads)
10661
10662         ;; See whether the article is to be put in the cache.
10663         (and gnus-use-cache
10664              (vectorp (gnus-summary-article-header article))
10665              (save-excursion
10666                (gnus-cache-possibly-enter-article
10667                 gnus-newsgroup-name article
10668                 (= mark gnus-ticked-mark)
10669                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10670
10671         ;; Fix the mark.
10672         (gnus-summary-update-mark mark 'unread)
10673         t))))
10674
10675 (defun gnus-summary-mark-article (&optional article mark no-expire)
10676   "Mark ARTICLE with MARK.  MARK can be any character.
10677 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10678 `??' (dormant) and `?E' (expirable).
10679 If MARK is nil, then the default character `?r' is used.
10680 If ARTICLE is nil, then the article on the current line will be
10681 marked.
10682 If NO-EXPIRE, auto-expiry will be inhibited."
10683   ;; The mark might be a string.
10684   (when (stringp mark)
10685     (setq mark (aref mark 0)))
10686   ;; If no mark is given, then we check auto-expiring.
10687   (when (null mark)
10688     (setq mark gnus-del-mark))
10689   (when (and (not no-expire)
10690              gnus-newsgroup-auto-expire
10691              (memq mark gnus-auto-expirable-marks))
10692     (setq mark gnus-expirable-mark))
10693   (let ((article (or article (gnus-summary-article-number)))
10694         (old-mark (gnus-summary-article-mark article)))
10695     ;; Allow the backend to change the mark.
10696     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10697     (if (eq mark old-mark)
10698         t
10699       (unless article
10700         (error "No article on current line"))
10701       (if (not (if (or (= mark gnus-unread-mark)
10702                        (= mark gnus-ticked-mark)
10703                        (= mark gnus-spam-mark)
10704                        (= mark gnus-dormant-mark))
10705                    (gnus-mark-article-as-unread article mark)
10706                  (gnus-mark-article-as-read article mark)))
10707           t
10708         ;; See whether the article is to be put in the cache.
10709         (and gnus-use-cache
10710              (not (= mark gnus-canceled-mark))
10711              (vectorp (gnus-summary-article-header article))
10712              (save-excursion
10713                (gnus-cache-possibly-enter-article
10714                 gnus-newsgroup-name article
10715                 (= mark gnus-ticked-mark)
10716                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10717
10718         (when (gnus-summary-goto-subject article nil t)
10719           (let ((inhibit-read-only t))
10720             (gnus-summary-show-thread)
10721             ;; Fix the mark.
10722             (gnus-summary-update-mark mark 'unread)
10723             t))))))
10724
10725 (defun gnus-summary-update-secondary-mark (article)
10726   "Update the secondary (read, process, cache) mark."
10727   (gnus-summary-update-mark
10728    (cond ((memq article gnus-newsgroup-processable)
10729           gnus-process-mark)
10730          ((memq article gnus-newsgroup-cached)
10731           gnus-cached-mark)
10732          ((memq article gnus-newsgroup-replied)
10733           gnus-replied-mark)
10734          ((memq article gnus-newsgroup-forwarded)
10735           gnus-forwarded-mark)
10736          ((memq article gnus-newsgroup-saved)
10737           gnus-saved-mark)
10738          ((memq article gnus-newsgroup-recent)
10739           gnus-recent-mark)
10740          ((memq article gnus-newsgroup-unseen)
10741           gnus-unseen-mark)
10742          (t gnus-no-mark))
10743    'replied)
10744   (when (gnus-visual-p 'summary-highlight 'highlight)
10745     (gnus-summary-highlight-line)
10746     (gnus-run-hooks 'gnus-summary-update-hook))
10747   t)
10748
10749 (defun gnus-summary-update-download-mark (article)
10750   "Update the download mark."
10751   (gnus-summary-update-mark
10752    (cond ((memq article gnus-newsgroup-undownloaded)
10753           gnus-undownloaded-mark)
10754          (gnus-newsgroup-agentized
10755           gnus-downloaded-mark)
10756          (t
10757           gnus-no-mark))
10758    'download)
10759   (gnus-summary-update-line t)
10760   t)
10761
10762 (defun gnus-summary-update-mark (mark type)
10763   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10764         (inhibit-read-only t))
10765     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10766     (when forward
10767       (when (looking-at "\r")
10768         (incf forward))
10769       (when (<= (+ forward (point)) (point-max))
10770         ;; Go to the right position on the line.
10771         (goto-char (+ forward (point)))
10772         ;; Replace the old mark with the new mark.
10773         (let ((to-insert
10774                (mm-subst-char-in-string
10775                 (char-after) mark
10776                 (buffer-substring (point) (1+ (point))))))
10777           (delete-region (point) (1+ (point)))
10778           (insert to-insert))
10779         ;; Optionally update the marks by some user rule.
10780         (when (eq type 'unread)
10781           (gnus-data-set-mark
10782            (gnus-data-find (gnus-summary-article-number)) mark)
10783           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10784
10785 (defun gnus-mark-article-as-read (article &optional mark)
10786   "Enter ARTICLE in the pertinent lists and remove it from others."
10787   ;; Make the article expirable.
10788   (let ((mark (or mark gnus-del-mark)))
10789     (setq gnus-newsgroup-expirable
10790           (if (= mark gnus-expirable-mark)
10791               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10792             (delq article gnus-newsgroup-expirable)))
10793     ;; Remove from unread and marked lists.
10794     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10795     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10796     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10797     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10798     (push (cons article mark) gnus-newsgroup-reads)
10799     ;; Possibly remove from cache, if that is used.
10800     (when gnus-use-cache
10801       (gnus-cache-enter-remove-article article))
10802     t))
10803
10804 (defun gnus-mark-article-as-unread (article &optional mark)
10805   "Enter ARTICLE in the pertinent lists and remove it from others."
10806   (let ((mark (or mark gnus-ticked-mark)))
10807     (if (<= article 0)
10808         (progn
10809           (gnus-error 1 "Can't mark negative article numbers")
10810           nil)
10811       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10812             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10813             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10814             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10815             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10816
10817       ;; Unsuppress duplicates?
10818       (when gnus-suppress-duplicates
10819         (gnus-dup-unsuppress-article article))
10820
10821       (cond ((= mark gnus-ticked-mark)
10822              (setq gnus-newsgroup-marked
10823                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10824             ((= mark gnus-spam-mark)
10825              (setq gnus-newsgroup-spam-marked
10826                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10827                                             article)))
10828             ((= mark gnus-dormant-mark)
10829              (setq gnus-newsgroup-dormant
10830                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10831             (t
10832              (setq gnus-newsgroup-unreads
10833                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10834       (gnus-pull article gnus-newsgroup-reads)
10835       t)))
10836
10837 (defalias 'gnus-summary-mark-as-unread-forward
10838   'gnus-summary-tick-article-forward)
10839 (make-obsolete 'gnus-summary-mark-as-unread-forward
10840                'gnus-summary-tick-article-forward "Emacs 20.4")
10841 (defun gnus-summary-tick-article-forward (n)
10842   "Tick N articles forwards.
10843 If N is negative, tick backwards instead.
10844 The difference between N and the number of articles ticked is returned."
10845   (interactive "p")
10846   (gnus-summary-mark-forward n gnus-ticked-mark))
10847
10848 (defalias 'gnus-summary-mark-as-unread-backward
10849   'gnus-summary-tick-article-backward)
10850 (make-obsolete 'gnus-summary-mark-as-unread-backward
10851                'gnus-summary-tick-article-backward "Emacs 20.4")
10852 (defun gnus-summary-tick-article-backward (n)
10853   "Tick N articles backwards.
10854 The difference between N and the number of articles ticked is returned."
10855   (interactive "p")
10856   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10857
10858 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10859 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article "Emacs 20.4")
10860 (defun gnus-summary-tick-article (&optional article clear-mark)
10861   "Mark current article as unread.
10862 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10863 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10864   (interactive)
10865   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10866                                        gnus-ticked-mark)))
10867
10868 (defun gnus-summary-mark-as-read-forward (n)
10869   "Mark N articles as read forwards.
10870 If N is negative, mark backwards instead.
10871 The difference between N and the actual number of articles marked is
10872 returned."
10873   (interactive "p")
10874   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10875
10876 (defun gnus-summary-mark-as-read-backward (n)
10877   "Mark the N articles as read backwards.
10878 The difference between N and the actual number of articles marked is
10879 returned."
10880   (interactive "p")
10881   (gnus-summary-mark-forward
10882    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10883
10884 (defun gnus-summary-mark-as-read (&optional article mark)
10885   "Mark current article as read.
10886 ARTICLE specifies the article to be marked as read.
10887 MARK specifies a string to be inserted at the beginning of the line."
10888   (gnus-summary-mark-article article mark))
10889
10890 (defun gnus-summary-clear-mark-forward (n)
10891   "Clear marks from N articles forward.
10892 If N is negative, clear backward instead.
10893 The difference between N and the number of marks cleared is returned."
10894   (interactive "p")
10895   (gnus-summary-mark-forward n gnus-unread-mark))
10896
10897 (defun gnus-summary-clear-mark-backward (n)
10898   "Clear marks from N articles backward.
10899 The difference between N and the number of marks cleared is returned."
10900   (interactive "p")
10901   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10902
10903 (defun gnus-summary-mark-unread-as-read ()
10904   "Intended to be used by `gnus-mark-article-hook'."
10905   (when (memq gnus-current-article gnus-newsgroup-unreads)
10906     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10907
10908 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10909   "Intended to be used by `gnus-mark-article-hook'."
10910   (let ((mark (gnus-summary-article-mark)))
10911     (when (or (gnus-unread-mark-p mark)
10912               (gnus-read-mark-p mark))
10913       (gnus-summary-mark-article gnus-current-article
10914                                  (or new-mark gnus-read-mark)))))
10915
10916 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10917   "Intended to be used by `gnus-mark-article-hook'."
10918   (let ((mark (gnus-summary-article-mark)))
10919     (when (or (gnus-unread-mark-p mark)
10920               (gnus-read-mark-p mark))
10921       (gnus-summary-mark-article (gnus-summary-article-number)
10922                                  (or new-mark gnus-read-mark)))))
10923
10924 (defun gnus-summary-mark-unread-as-ticked ()
10925   "Intended to be used by `gnus-mark-article-hook'."
10926   (when (memq gnus-current-article gnus-newsgroup-unreads)
10927     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10928
10929 (defun gnus-summary-mark-region-as-read (point mark all)
10930   "Mark all unread articles between point and mark as read.
10931 If given a prefix, mark all articles between point and mark as read,
10932 even ticked and dormant ones."
10933   (interactive "r\nP")
10934   (save-excursion
10935     (let (article)
10936       (goto-char point)
10937       (beginning-of-line)
10938       (while (and
10939               (< (point) mark)
10940               (progn
10941                 (when (or all
10942                           (memq (setq article (gnus-summary-article-number))
10943                                 gnus-newsgroup-unreads))
10944                   (gnus-summary-mark-article article gnus-del-mark))
10945                 t)
10946               (gnus-summary-find-next))))))
10947
10948 (defun gnus-summary-mark-below (score mark)
10949   "Mark articles with score less than SCORE with MARK."
10950   (interactive "P\ncMark: ")
10951   (setq score (if score
10952                   (prefix-numeric-value score)
10953                 (or gnus-summary-default-score 0)))
10954   (with-current-buffer gnus-summary-buffer
10955     (goto-char (point-min))
10956     (while
10957         (progn
10958           (and (< (gnus-summary-article-score) score)
10959                (gnus-summary-mark-article nil mark))
10960           (gnus-summary-find-next)))))
10961
10962 (defun gnus-summary-kill-below (&optional score)
10963   "Mark articles with score below SCORE as read."
10964   (interactive "P")
10965   (gnus-summary-mark-below score gnus-killed-mark))
10966
10967 (defun gnus-summary-clear-above (&optional score)
10968   "Clear all marks from articles with score above SCORE."
10969   (interactive "P")
10970   (gnus-summary-mark-above score gnus-unread-mark))
10971
10972 (defun gnus-summary-tick-above (&optional score)
10973   "Tick all articles with score above SCORE."
10974   (interactive "P")
10975   (gnus-summary-mark-above score gnus-ticked-mark))
10976
10977 (defun gnus-summary-mark-above (score mark)
10978   "Mark articles with score over SCORE with MARK."
10979   (interactive "P\ncMark: ")
10980   (setq score (if score
10981                   (prefix-numeric-value score)
10982                 (or gnus-summary-default-score 0)))
10983   (with-current-buffer gnus-summary-buffer
10984     (goto-char (point-min))
10985     (while (and (progn
10986                   (when (> (gnus-summary-article-score) score)
10987                     (gnus-summary-mark-article nil mark))
10988                   t)
10989                 (gnus-summary-find-next)))))
10990
10991 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10992 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10993 (defun gnus-summary-limit-include-expunged (&optional no-error)
10994   "Display all the hidden articles that were expunged for low scores."
10995   (interactive)
10996   (let ((inhibit-read-only t))
10997     (let ((scored gnus-newsgroup-scored)
10998           headers h)
10999       (while scored
11000         (unless (gnus-summary-article-header (caar scored))
11001           (and (setq h (gnus-number-to-header (caar scored)))
11002                (< (cdar scored) gnus-summary-expunge-below)
11003                (push h headers)))
11004         (setq scored (cdr scored)))
11005       (if (not headers)
11006           (when (not no-error)
11007             (error "No expunged articles hidden"))
11008         (goto-char (point-min))
11009         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11010         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11011         (dolist (x headers)
11012           (push (mail-header-number x) gnus-newsgroup-limit))
11013         (gnus-summary-prepare-unthreaded (nreverse headers))
11014         (goto-char (point-min))
11015         (gnus-summary-position-point)
11016         t))))
11017
11018 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11019   "Mark all unread articles in this newsgroup as read.
11020 If prefix argument ALL is non-nil, ticked and dormant articles will
11021 also be marked as read.
11022 If QUIETLY is non-nil, no questions will be asked.
11023
11024 If TO-HERE is non-nil, it should be a point in the buffer.  All
11025 articles before (after, if REVERSE is set) this point will be marked
11026 as read.
11027
11028 Note that this function will only catch up the unread article
11029 in the current summary buffer limitation.
11030
11031 The number of articles marked as read is returned."
11032   (interactive "P")
11033   (prog1
11034       (save-excursion
11035         (when (or quietly
11036                   (not gnus-interactive-catchup) ;Without confirmation?
11037                   gnus-expert-user
11038                   (gnus-y-or-n-p
11039                    (if all
11040                        "Mark absolutely all articles as read? "
11041                      "Mark all unread articles as read? ")))
11042           (if (and not-mark
11043                    (not gnus-newsgroup-adaptive)
11044                    (not gnus-newsgroup-auto-expire)
11045                    (not gnus-suppress-duplicates)
11046                    (or (not gnus-use-cache)
11047                        (eq gnus-use-cache 'passive)))
11048               (progn
11049                 (when all
11050                   (setq gnus-newsgroup-marked nil
11051                         gnus-newsgroup-spam-marked nil
11052                         gnus-newsgroup-dormant nil))
11053                 (setq gnus-newsgroup-unreads
11054                       (gnus-sorted-nunion
11055                        (gnus-sorted-intersection gnus-newsgroup-unreads
11056                                                  gnus-newsgroup-downloadable)
11057                        (gnus-sorted-difference gnus-newsgroup-unfetched
11058                                                gnus-newsgroup-cached))))
11059             ;; We actually mark all articles as canceled, which we
11060             ;; have to do when using auto-expiry or adaptive scoring.
11061             (gnus-summary-show-all-threads)
11062             (if (and to-here reverse)
11063                 (progn
11064                   (goto-char to-here)
11065                   (gnus-summary-mark-current-read-and-unread-as-read
11066                    gnus-catchup-mark)
11067                   (while (gnus-summary-find-next (not all))
11068                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11069               (when (gnus-summary-first-subject (not all))
11070                 (while (and
11071                         (if to-here (< (point) to-here) t)
11072                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11073                         (gnus-summary-find-next (not all))))))
11074             (gnus-set-mode-line 'summary))
11075           t))
11076     (gnus-summary-position-point)))
11077
11078 (defun gnus-summary-catchup-to-here (&optional all)
11079   "Mark all unticked articles before the current one as read.
11080 If ALL is non-nil, also mark ticked and dormant articles as read."
11081   (interactive "P")
11082   (save-excursion
11083     (gnus-save-hidden-threads
11084       (let ((beg (point)))
11085         ;; We check that there are unread articles.
11086         (when (or all (gnus-summary-find-prev))
11087           (gnus-summary-catchup all t beg)))))
11088   (gnus-summary-position-point))
11089
11090 (defun gnus-summary-catchup-from-here (&optional all)
11091   "Mark all unticked articles after (and including) the current one as read.
11092 If ALL is non-nil, also mark ticked and dormant articles as read."
11093   (interactive "P")
11094   (save-excursion
11095     (gnus-save-hidden-threads
11096       (let ((beg (point)))
11097         ;; We check that there are unread articles.
11098         (when (or all (gnus-summary-find-next))
11099           (gnus-summary-catchup all t beg nil t)))))
11100   (gnus-summary-position-point))
11101
11102 (defun gnus-summary-catchup-all (&optional quietly)
11103   "Mark all articles in this newsgroup as read.
11104 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11105 instead, which marks only unread articles as read."
11106   (interactive "P")
11107   (gnus-summary-catchup t quietly))
11108
11109 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11110   "Mark all unread articles in this group as read, then exit.
11111 If prefix argument ALL is non-nil, all articles are marked as read.
11112 If QUIETLY is non-nil, no questions will be asked."
11113   (interactive "P")
11114   (when (gnus-summary-catchup all quietly nil 'fast)
11115     ;; Select next newsgroup or exit.
11116     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11117              (eq gnus-auto-select-next 'quietly))
11118         (gnus-summary-next-group nil)
11119       (gnus-summary-exit))))
11120
11121 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11122   "Mark all articles in this newsgroup as read, and then exit.
11123 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11124 instead, which marks only unread articles as read."
11125   (interactive "P")
11126   (gnus-summary-catchup-and-exit t quietly))
11127
11128 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11129   "Mark all articles in this group as read and select the next group.
11130 If given a prefix, mark all articles, unread as well as ticked, as
11131 read."
11132   (interactive "P")
11133   (save-excursion
11134     (gnus-summary-catchup all))
11135   (gnus-summary-next-group))
11136
11137 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11138   "Mark all articles in this group as read and select the previous group.
11139 If given a prefix, mark all articles, unread as well as ticked, as
11140 read."
11141   (interactive "P")
11142   (save-excursion
11143     (gnus-summary-catchup all))
11144   (gnus-summary-next-group nil nil t))
11145
11146 ;;;
11147 ;;; with article
11148 ;;;
11149
11150 (defmacro gnus-with-article (article &rest forms)
11151   "Select ARTICLE and perform FORMS in the original article buffer.
11152 Then replace the article with the result."
11153   `(progn
11154      ;; We don't want the article to be marked as read.
11155      (let (gnus-mark-article-hook)
11156        (gnus-summary-select-article t t nil ,article))
11157      (set-buffer gnus-original-article-buffer)
11158      ,@forms
11159      (if (not (gnus-check-backend-function
11160                'request-replace-article (car gnus-article-current)))
11161          (gnus-message 5 "Read-only group; not replacing")
11162        (unless (gnus-request-replace-article
11163                 ,article (car gnus-article-current)
11164                 (current-buffer) t)
11165          (error "Couldn't replace article")))
11166      ;; The cache and backlog have to be flushed somewhat.
11167      (when gnus-keep-backlog
11168        (gnus-backlog-remove-article
11169         (car gnus-article-current) (cdr gnus-article-current)))
11170      (when gnus-use-cache
11171        (gnus-cache-update-article
11172         (car gnus-article-current) (cdr gnus-article-current)))))
11173
11174 (put 'gnus-with-article 'lisp-indent-function 1)
11175 (put 'gnus-with-article 'edebug-form-spec '(form body))
11176
11177 ;; Thread-based commands.
11178
11179 (defun gnus-summary-articles-in-thread (&optional article)
11180   "Return a list of all articles in the current thread.
11181 If ARTICLE is non-nil, return all articles in the thread that starts
11182 with that article."
11183   (let* ((article (or article (gnus-summary-article-number)))
11184          (data (gnus-data-find-list article))
11185          (top-level (gnus-data-level (car data)))
11186          (top-subject
11187           (cond ((null gnus-thread-operation-ignore-subject)
11188                  (gnus-simplify-subject-re
11189                   (mail-header-subject (gnus-data-header (car data)))))
11190                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11191                  (gnus-simplify-subject-fuzzy
11192                   (mail-header-subject (gnus-data-header (car data)))))
11193                 (t nil)))
11194          (end-point (save-excursion
11195                       (if (gnus-summary-go-to-next-thread)
11196                           (point) (point-max))))
11197          articles)
11198     (while (and data
11199                 (< (gnus-data-pos (car data)) end-point))
11200       (when (or (not top-subject)
11201                 (string= top-subject
11202                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11203                              (gnus-simplify-subject-fuzzy
11204                               (mail-header-subject
11205                                (gnus-data-header (car data))))
11206                            (gnus-simplify-subject-re
11207                             (mail-header-subject
11208                              (gnus-data-header (car data)))))))
11209         (push (gnus-data-number (car data)) articles))
11210       (unless (and (setq data (cdr data))
11211                    (> (gnus-data-level (car data)) top-level))
11212         (setq data nil)))
11213     ;; Return the list of articles.
11214     (nreverse articles)))
11215
11216 (defun gnus-summary-rethread-current ()
11217   "Rethread the thread the current article is part of."
11218   (interactive)
11219   (let* ((gnus-show-threads t)
11220          (article (gnus-summary-article-number))
11221          (id (mail-header-id (gnus-summary-article-header)))
11222          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11223     (unless id
11224       (error "No article on the current line"))
11225     (gnus-rebuild-thread id)
11226     (gnus-summary-goto-subject article)))
11227
11228 (defun gnus-summary-reparent-thread ()
11229   "Make the current article child of the marked (or previous) article.
11230
11231 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11232 is non-nil or the Subject: of both articles are the same."
11233   (interactive)
11234   (unless (not (gnus-group-read-only-p))
11235     (error "The current newsgroup does not support article editing"))
11236   (unless (<= (length gnus-newsgroup-processable) 1)
11237     (error "No more than one article may be marked"))
11238   (let ((child (gnus-summary-article-number))
11239         ;; First grab the marked article, otherwise one line up.
11240         (parent (if (not (null gnus-newsgroup-processable))
11241                     (car gnus-newsgroup-processable)
11242                   (save-excursion
11243                     (if (eq (forward-line -1) 0)
11244                         (gnus-summary-article-number)
11245                       (error "Beginning of summary buffer"))))))
11246     (gnus-summary-reparent-children parent (list child))))
11247
11248 (defun gnus-summary-reparent-children (parent children)
11249   "Make PARENT the parent of CHILDREN.
11250 When called interactively, PARENT is the current article and CHILDREN
11251 are the process-marked articles."
11252   (interactive
11253    (list (gnus-summary-article-number)
11254          (gnus-summary-work-articles nil)))
11255   (dolist (child children)
11256     (save-window-excursion
11257       (let ((gnus-article-buffer " *reparent*"))
11258         (unless (not (eq parent child))
11259           (error "An article may not be self-referential"))
11260         (let ((message-id (mail-header-id
11261                            (gnus-summary-article-header parent))))
11262           (unless (and message-id (not (equal message-id "")))
11263             (error "No message-id in desired parent"))
11264           (gnus-with-article child
11265             (save-restriction
11266               (goto-char (point-min))
11267               (message-narrow-to-head)
11268               (if (re-search-forward "^References: " nil t)
11269                   (progn
11270                     (re-search-forward "^[^ \t]" nil t)
11271                     (forward-line -1)
11272                     (end-of-line)
11273                     (insert " " message-id))
11274                 (insert "References: " message-id "\n"))))
11275           (set-buffer gnus-summary-buffer)
11276           (gnus-summary-unmark-all-processable)
11277           (gnus-summary-update-article child)
11278           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11279             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11280           (gnus-summary-rethread-current)
11281           (gnus-message 3 "Article %d is now the child of article %d"
11282                         child parent))))))
11283
11284 (defun gnus-summary-toggle-threads (&optional arg)
11285   "Toggle showing conversation threads.
11286 If ARG is positive number, turn showing conversation threads on."
11287   (interactive "P")
11288   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11289     (setq gnus-show-threads
11290           (if (null arg) (not gnus-show-threads)
11291             (> (prefix-numeric-value arg) 0)))
11292     (gnus-summary-prepare)
11293     (gnus-summary-goto-subject current)
11294     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11295     (gnus-summary-position-point)))
11296
11297 (eval-and-compile
11298   (if (fboundp 'remove-overlays)
11299       (defalias 'gnus-remove-overlays 'remove-overlays)
11300     (defun gnus-remove-overlays (beg end name val)
11301       "Clear BEG and END of overlays whose property NAME has value VAL.
11302 For compatibility with XEmacs."
11303       (dolist (ov (gnus-overlays-in beg end))
11304         (when (eq (gnus-overlay-get ov name) val)
11305           (gnus-delete-overlay ov))))))
11306
11307 (defun gnus-summary-show-all-threads ()
11308   "Show all threads."
11309   (interactive)
11310   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11311   (gnus-summary-position-point))
11312
11313 (defun gnus-summary-show-thread ()
11314   "Show thread subtrees.
11315 Returns nil if no thread was there to be shown."
11316   (interactive)
11317   (let* ((orig (point))
11318          (end (point-at-eol))
11319          ;; Leave point at bol
11320          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11321          (eoi (when (eq (get-char-property end 'invisible) 'gnus-sum)
11322                 (if (fboundp 'next-single-char-property-change)
11323                     (or (next-single-char-property-change end 'invisible)
11324                         (point-max))
11325                   (while (progn
11326                            (end-of-line 2)
11327                            (and (not (eobp))
11328                                 (eq (get-char-property (point) 'invisible)
11329                                     'gnus-sum))))
11330                   (point)))))
11331     (when eoi
11332       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11333       (goto-char orig)
11334       (gnus-summary-position-point)
11335       eoi)))
11336
11337 (defun gnus-summary-maybe-hide-threads ()
11338   "If requested, hide the threads that should be hidden."
11339   (when (and gnus-show-threads
11340              gnus-thread-hide-subtree)
11341     (gnus-summary-hide-all-threads
11342      (if (or (consp gnus-thread-hide-subtree)
11343              (functionp gnus-thread-hide-subtree))
11344          (gnus-make-predicate gnus-thread-hide-subtree)
11345        nil))))
11346
11347 ;;; Hiding predicates.
11348
11349 (defun gnus-article-unread-p (header)
11350   (memq (mail-header-number header) gnus-newsgroup-unreads))
11351
11352 (defun gnus-article-unseen-p (header)
11353   (memq (mail-header-number header) gnus-newsgroup-unseen))
11354
11355 (defun gnus-map-articles (predicate articles)
11356   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11357   (apply 'gnus-or (mapcar predicate
11358                           (mapcar (lambda (number)
11359                                     (gnus-summary-article-header number))
11360                                   articles))))
11361
11362 (defun gnus-summary-hide-all-threads (&optional predicate)
11363   "Hide all thread subtrees.
11364 If PREDICATE is supplied, threads that satisfy this predicate
11365 will not be hidden."
11366   (interactive)
11367   (save-excursion
11368     (goto-char (point-min))
11369     (let ((end nil))
11370       (while (not end)
11371         (when (or (not predicate)
11372                   (gnus-map-articles
11373                    predicate (gnus-summary-article-children)))
11374             (gnus-summary-hide-thread))
11375         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11376   (gnus-summary-position-point))
11377
11378 (defun gnus-summary-hide-thread ()
11379   "Hide thread subtrees.
11380 If PREDICATE is supplied, threads that satisfy this predicate
11381 will not be hidden.
11382 Returns nil if no threads were there to be hidden."
11383   (interactive)
11384   (let ((start (point))
11385         (starteol (line-end-position))
11386         (article (gnus-summary-article-number)))
11387     (goto-char start)
11388     ;; Go forward until either the buffer ends or the subthread ends.
11389     (when (and (not (eobp))
11390                (or (zerop (gnus-summary-next-thread 1 t))
11391                    (goto-char (point-max))))
11392       (if (and (> (point) start)
11393                ;; FIXME: this should actually search for a non-invisible \n.
11394                (search-backward "\n" start t))
11395           (progn
11396             (when (> (point) starteol)
11397               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11398               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11399                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11400                 (gnus-overlay-put ol 'evaporate t)))
11401             (gnus-summary-goto-subject article))
11402         (goto-char start)
11403         nil))))
11404
11405 (defun gnus-summary-go-to-next-thread (&optional previous)
11406   "Go to the same level (or less) next thread.
11407 If PREVIOUS is non-nil, go to previous thread instead.
11408 Return the article number moved to, or nil if moving was impossible."
11409   (let ((level (gnus-summary-thread-level))
11410         (way (if previous -1 1))
11411         (beg (point)))
11412     (forward-line way)
11413     (while (and (not (eobp))
11414                 (< level (gnus-summary-thread-level)))
11415       (forward-line way))
11416     (if (eobp)
11417         (progn
11418           (goto-char beg)
11419           nil)
11420       (setq beg (point))
11421       (prog1
11422           (gnus-summary-article-number)
11423         (goto-char beg)))))
11424
11425 (defun gnus-summary-next-thread (n &optional silent)
11426   "Go to the same level next N'th thread.
11427 If N is negative, search backward instead.
11428 Returns the difference between N and the number of skips actually
11429 done.
11430
11431 If SILENT, don't output messages."
11432   (interactive "p")
11433   (let ((backward (< n 0))
11434         (n (abs n)))
11435     (while (and (> n 0)
11436                 (gnus-summary-go-to-next-thread backward))
11437       (decf n))
11438     (unless silent
11439       (gnus-summary-position-point))
11440     (when (and (not silent) (/= 0 n))
11441       (gnus-message 7 "No more threads"))
11442     n))
11443
11444 (defun gnus-summary-prev-thread (n)
11445   "Go to the same level previous N'th thread.
11446 Returns the difference between N and the number of skips actually
11447 done."
11448   (interactive "p")
11449   (gnus-summary-next-thread (- n)))
11450
11451 (defun gnus-summary-go-down-thread ()
11452   "Go down one level in the current thread."
11453   (let ((children (gnus-summary-article-children)))
11454     (when children
11455       (gnus-summary-goto-subject (car children)))))
11456
11457 (defun gnus-summary-go-up-thread ()
11458   "Go up one level in the current thread."
11459   (let ((parent (gnus-summary-article-parent)))
11460     (when parent
11461       (gnus-summary-goto-subject parent))))
11462
11463 (defun gnus-summary-down-thread (n)
11464   "Go down thread N steps.
11465 If N is negative, go up instead.
11466 Returns the difference between N and how many steps down that were
11467 taken."
11468   (interactive "p")
11469   (let ((up (< n 0))
11470         (n (abs n)))
11471     (while (and (> n 0)
11472                 (if up (gnus-summary-go-up-thread)
11473                   (gnus-summary-go-down-thread)))
11474       (setq n (1- n)))
11475     (gnus-summary-position-point)
11476     (when (/= 0 n)
11477       (gnus-message 7 "Can't go further"))
11478     n))
11479
11480 (defun gnus-summary-up-thread (n)
11481   "Go up thread N steps.
11482 If N is negative, go down instead.
11483 Returns the difference between N and how many steps down that were
11484 taken."
11485   (interactive "p")
11486   (gnus-summary-down-thread (- n)))
11487
11488 (defun gnus-summary-top-thread ()
11489   "Go to the top of the thread."
11490   (interactive)
11491   (while (gnus-summary-go-up-thread))
11492   (gnus-summary-article-number))
11493
11494 (defun gnus-summary-expire-thread ()
11495   "Mark articles under current thread as expired."
11496   (interactive)
11497   (gnus-summary-kill-thread 0))
11498
11499 (defun gnus-summary-kill-thread (&optional unmark)
11500   "Mark articles under current thread as read.
11501 If the prefix argument is positive, remove any kinds of marks.
11502 If the prefix argument is zero, mark thread as expired.
11503 If the prefix argument is negative, tick articles instead."
11504   (interactive "P")
11505   (when unmark
11506     (setq unmark (prefix-numeric-value unmark)))
11507   (let ((articles (gnus-summary-articles-in-thread))
11508         (hide (or (null unmark) (= unmark 0))))
11509     (save-excursion
11510       ;; Expand the thread.
11511       (gnus-summary-show-thread)
11512       ;; Mark all the articles.
11513       (while articles
11514         (gnus-summary-goto-subject (car articles))
11515         (cond ((null unmark)
11516                (gnus-summary-mark-article-as-read gnus-killed-mark))
11517               ((> unmark 0)
11518                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11519               ((= unmark 0)
11520                (gnus-summary-mark-article nil gnus-expirable-mark))
11521               (t
11522                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11523         (setq articles (cdr articles))))
11524     ;; Hide killed subtrees when hide is true.
11525     (and hide
11526          gnus-thread-hide-killed
11527          (gnus-summary-hide-thread))
11528     ;; If hide is t, go to next unread subject.
11529     (when hide
11530       ;; Go to next unread subject.
11531       (gnus-summary-next-subject 1 t)))
11532   (gnus-set-mode-line 'summary))
11533
11534 ;; Summary sorting commands
11535
11536 (defun gnus-summary-sort-by-number (&optional reverse)
11537   "Sort the summary buffer by article number.
11538 Argument REVERSE means reverse order."
11539   (interactive "P")
11540   (gnus-summary-sort 'number reverse))
11541
11542 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11543   "Sort the summary buffer by most recent article number.
11544 Argument REVERSE means reverse order."
11545   (interactive "P")
11546   (gnus-summary-sort 'most-recent-number reverse))
11547
11548 (defun gnus-summary-sort-by-random (&optional reverse)
11549   "Randomize the order in the summary buffer.
11550 Argument REVERSE means to randomize in reverse order."
11551   (interactive "P")
11552   (gnus-summary-sort 'random reverse))
11553
11554 (defun gnus-summary-sort-by-author (&optional reverse)
11555   "Sort the summary buffer by author name alphabetically.
11556 If `case-fold-search' is non-nil, case of letters is ignored.
11557 Argument REVERSE means reverse order."
11558   (interactive "P")
11559   (gnus-summary-sort 'author reverse))
11560
11561 (defun gnus-summary-sort-by-recipient (&optional reverse)
11562   "Sort the summary buffer by recipient name alphabetically.
11563 If `case-fold-search' is non-nil, case of letters is ignored.
11564 Argument REVERSE means reverse order."
11565   (interactive "P")
11566   (gnus-summary-sort 'recipient reverse))
11567
11568 (defun gnus-summary-sort-by-subject (&optional reverse)
11569   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11570 If `case-fold-search' is non-nil, case of letters is ignored.
11571 Argument REVERSE means reverse order."
11572   (interactive "P")
11573   (gnus-summary-sort 'subject reverse))
11574
11575 (defun gnus-summary-sort-by-date (&optional reverse)
11576   "Sort the summary buffer by date.
11577 Argument REVERSE means reverse order."
11578   (interactive "P")
11579   (gnus-summary-sort 'date reverse))
11580
11581 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11582   "Sort the summary buffer by most recent date.
11583 Argument REVERSE means reverse order."
11584   (interactive "P")
11585   (gnus-summary-sort 'most-recent-date reverse))
11586
11587 (defun gnus-summary-sort-by-score (&optional reverse)
11588   "Sort the summary buffer by score.
11589 Argument REVERSE means reverse order."
11590   (interactive "P")
11591   (gnus-summary-sort 'score reverse))
11592
11593 (defun gnus-summary-sort-by-lines (&optional reverse)
11594   "Sort the summary buffer by the number of lines.
11595 Argument REVERSE means reverse order."
11596   (interactive "P")
11597   (gnus-summary-sort 'lines reverse))
11598
11599 (defun gnus-summary-sort-by-chars (&optional reverse)
11600   "Sort the summary buffer by article length.
11601 Argument REVERSE means reverse order."
11602   (interactive "P")
11603   (gnus-summary-sort 'chars reverse))
11604
11605 (defun gnus-summary-sort-by-original (&optional reverse)
11606   "Sort the summary buffer using the default sorting method.
11607 Argument REVERSE means reverse order."
11608   (interactive "P")
11609   (let* ((inhibit-read-only t)
11610          (gnus-summary-prepare-hook nil))
11611     ;; We do the sorting by regenerating the threads.
11612     (gnus-summary-prepare)
11613     ;; Hide subthreads if needed.
11614     (gnus-summary-maybe-hide-threads)))
11615
11616 (defun gnus-summary-sort (predicate reverse)
11617   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11618   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11619          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11620          (gnus-thread-sort-functions
11621           (if (not reverse)
11622               thread
11623             `(lambda (t1 t2)
11624                (,thread t2 t1))))
11625          (gnus-sort-gathered-threads-function
11626           gnus-thread-sort-functions)
11627          (gnus-article-sort-functions
11628           (if (not reverse)
11629               article
11630             `(lambda (t1 t2)
11631                (,article t2 t1))))
11632          (inhibit-read-only t)
11633          (gnus-summary-prepare-hook nil))
11634     ;; We do the sorting by regenerating the threads.
11635     (gnus-summary-prepare)
11636     ;; Hide subthreads if needed.
11637     (gnus-summary-maybe-hide-threads)))
11638
11639 ;; Summary saving commands.
11640
11641 (defun gnus-summary-save-article (&optional n not-saved)
11642   "Save the current article using the default saver function.
11643 If N is a positive number, save the N next articles.
11644 If N is a negative number, save the N previous articles.
11645 If N is nil and any articles have been marked with the process mark,
11646 save those articles instead.
11647 The variable `gnus-default-article-saver' specifies the saver function.
11648
11649 If the optional second argument NOT-SAVED is non-nil, articles saved
11650 will not be marked as saved."
11651   (interactive "P")
11652   (require 'gnus-art)
11653   (let* ((articles (gnus-summary-work-articles n))
11654          (save-buffer (save-excursion
11655                         (nnheader-set-temp-buffer " *Gnus Save*")))
11656          (num (length articles))
11657          ;; Whether to save decoded articles or raw articles.
11658          (decode (when gnus-article-save-coding-system
11659                    (get gnus-default-article-saver :decode)))
11660          ;; When saving many articles in a single file, use the other
11661          ;; function to save articles other than the first one.
11662          (saver2 (get gnus-default-article-saver :function))
11663          (gnus-prompt-before-saving (if saver2
11664                                         t
11665                                       gnus-prompt-before-saving))
11666          (gnus-default-article-saver gnus-default-article-saver)
11667          header file)
11668     (dolist (article articles)
11669       (setq header (gnus-summary-article-header article))
11670       (if (not (vectorp header))
11671           ;; This is a pseudo-article.
11672           (if (assq 'name header)
11673               (gnus-copy-file (cdr (assq 'name header)))
11674             (gnus-message 1 "Article %d is unsaveable" article))
11675         ;; This is a real article.
11676         (save-window-excursion
11677           (gnus-summary-select-article decode decode nil article)
11678           (gnus-summary-goto-subject article))
11679         (with-current-buffer save-buffer
11680           (erase-buffer)
11681           (insert-buffer-substring (if decode
11682                                        gnus-article-buffer
11683                                      gnus-original-article-buffer)))
11684         (setq file (gnus-article-save save-buffer file num))
11685         (gnus-summary-remove-process-mark article)
11686         (unless not-saved
11687           (gnus-summary-set-saved-mark article)))
11688       (when saver2
11689         (setq gnus-default-article-saver saver2
11690               saver2 nil)))
11691     (gnus-kill-buffer save-buffer)
11692     (gnus-summary-position-point)
11693     (gnus-set-mode-line 'summary)
11694     n))
11695
11696 (defun gnus-summary-pipe-output (&optional n sym)
11697   "Pipe the current article to a subprocess.
11698 If N is a positive number, pipe the N next articles.
11699 If N is a negative number, pipe the N previous articles.
11700 If N is nil and any articles have been marked with the process mark,
11701 pipe those articles instead.
11702 The default command to which articles are piped is specified by the
11703 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11704 will be prompted for the command.
11705
11706 The properties `:decode' and `:headers' that are put to the function
11707 symbol `gnus-summary-save-in-pipe' control whether this function
11708 decodes articles and what headers to keep (see the doc string for the
11709 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11710 is neither omitted nor the symbol `r', force including all headers
11711 regardless of the `:headers' property.  If it is the symbol `r',
11712 articles that are not decoded and include all headers will be piped
11713 no matter what the properties `:decode' and `:headers' are."
11714   (interactive (gnus-interactive "P\ny"))
11715   (require 'gnus-art)
11716   (let* ((articles (gnus-summary-work-articles n))
11717          (result-buffer "*Shell Command Output*")
11718          (all-headers (not (memq sym '(nil r))))
11719          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11720          (raw (eq sym 'r))
11721          (headers (get 'gnus-summary-save-in-pipe :headers))
11722          command result)
11723     (unless (numberp (car articles))
11724       (error "No article to pipe"))
11725     (setq command (gnus-read-shell-command
11726                    (concat "Shell command on "
11727                            (if (cdr articles)
11728                                (format "these %d articles" (length articles))
11729                              "this article")
11730                            ": ")
11731                    gnus-summary-pipe-output-default-command))
11732     (when (string-equal command "")
11733       (error "A command is required"))
11734     (when all-headers
11735       (put 'gnus-summary-save-in-pipe :headers nil))
11736     (unwind-protect
11737         (while articles
11738           (gnus-summary-goto-subject (pop articles))
11739           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11740           (when (and (get-buffer result-buffer)
11741                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11742             (setq result (concat result (with-current-buffer result-buffer
11743                                           (buffer-string))))))
11744       (put 'gnus-summary-save-in-pipe :headers headers))
11745     (unless (zerop (length result))
11746       (if (with-current-buffer (get-buffer-create result-buffer)
11747             (erase-buffer)
11748             (insert result)
11749             (prog1
11750                 (and (= (count-lines (point-min) (point)) 1)
11751                      (progn
11752                        (end-of-line 0)
11753                        (<= (current-column)
11754                            (window-width (minibuffer-window)))))
11755               (goto-char (point-min))))
11756           (message "%s" (substring result 0 -1))
11757         (message nil)
11758         (gnus-configure-windows 'pipe)))))
11759
11760 (defun gnus-summary-save-article-mail (&optional arg)
11761   "Append the current article to a Unix mail box file.
11762 If N is a positive number, save the N next articles.
11763 If N is a negative number, save the N previous articles.
11764 If N is nil and any articles have been marked with the process mark,
11765 save those articles instead."
11766   (interactive "P")
11767   (require 'gnus-art)
11768   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11769     (gnus-summary-save-article arg)))
11770
11771 (defun gnus-summary-save-article-rmail (&optional arg)
11772   "Append the current article to an rmail file.
11773 If N is a positive number, save the N next articles.
11774 If N is a negative number, save the N previous articles.
11775 If N is nil and any articles have been marked with the process mark,
11776 save those articles instead."
11777   (interactive "P")
11778   (require 'gnus-art)
11779   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11780     (gnus-summary-save-article arg)))
11781
11782 (defun gnus-summary-save-article-file (&optional arg)
11783   "Append the current article to a file.
11784 If N is a positive number, save the N next articles.
11785 If N is a negative number, save the N previous articles.
11786 If N is nil and any articles have been marked with the process mark,
11787 save those articles instead."
11788   (interactive "P")
11789   (require 'gnus-art)
11790   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11791     (gnus-summary-save-article arg)))
11792
11793 (defun gnus-summary-write-article-file (&optional arg)
11794   "Write the current article to a file, deleting the previous file.
11795 If N is a positive number, save the N next articles.
11796 If N is a negative number, save the N previous articles.
11797 If N is nil and any articles have been marked with the process mark,
11798 save those articles instead."
11799   (interactive "P")
11800   (require 'gnus-art)
11801   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11802     (gnus-summary-save-article arg)))
11803
11804 (defun gnus-summary-save-article-body-file (&optional arg)
11805   "Append the current article body to a file.
11806 If N is a positive number, save the N next articles.
11807 If N is a negative number, save the N previous articles.
11808 If N is nil and any articles have been marked with the process mark,
11809 save those articles instead."
11810   (interactive "P")
11811   (require 'gnus-art)
11812   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11813     (gnus-summary-save-article arg)))
11814
11815 (defun gnus-summary-write-article-body-file (&optional arg)
11816   "Write the current article body to a file, deleting the previous file.
11817 If N is a positive number, save the N next articles.
11818 If N is a negative number, save the N previous articles.
11819 If N is nil and any articles have been marked with the process mark,
11820 save those articles instead."
11821   (interactive "P")
11822   (require 'gnus-art)
11823   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11824     (gnus-summary-save-article arg)))
11825
11826 (defun gnus-summary-muttprint (&optional arg)
11827   "Print the current article using Muttprint.
11828 If N is a positive number, save the N next articles.
11829 If N is a negative number, save the N previous articles.
11830 If N is nil and any articles have been marked with the process mark,
11831 save those articles instead."
11832   (interactive "P")
11833   (require 'gnus-art)
11834   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11835     (gnus-summary-save-article arg t)))
11836
11837 (defun gnus-summary-pipe-message (program)
11838   "Pipe the current article through PROGRAM."
11839   (interactive "sProgram: ")
11840   (gnus-summary-select-article)
11841   (let ((mail-header-separator ""))
11842     (gnus-eval-in-buffer-window gnus-article-buffer
11843       (save-restriction
11844         (widen)
11845         (let ((start (window-start))
11846               (inhibit-read-only t))
11847           (message-pipe-buffer-body program)
11848           (set-window-start (get-buffer-window (current-buffer)) start))))))
11849
11850 (defun gnus-get-split-value (methods)
11851   "Return a value based on the split METHODS."
11852   (let (split-name method result match)
11853     (when methods
11854       (with-current-buffer gnus-original-article-buffer
11855         (save-restriction
11856           (nnheader-narrow-to-headers)
11857           (while (and methods (not split-name))
11858             (goto-char (point-min))
11859             (setq method (pop methods))
11860             (setq match (car method))
11861             (when (cond
11862                    ((stringp match)
11863                     ;; Regular expression.
11864                     (ignore-errors
11865                       (re-search-forward match nil t)))
11866                    ((functionp match)
11867                     ;; Function.
11868                     (save-restriction
11869                       (widen)
11870                       (setq result (funcall match gnus-newsgroup-name))))
11871                    ((consp match)
11872                     ;; Form.
11873                     (save-restriction
11874                       (widen)
11875                       (setq result (eval match)))))
11876               (setq split-name (cdr method))
11877               (cond ((stringp result)
11878                      (push (expand-file-name
11879                             result gnus-article-save-directory)
11880                            split-name))
11881                     ((consp result)
11882                      (setq split-name (append result split-name)))))))))
11883     (nreverse split-name)))
11884
11885 (defun gnus-valid-move-group-p (group)
11886   (and (boundp group)
11887        (symbol-name group)
11888        (symbol-value group)
11889        (gnus-get-function (gnus-find-method-for-group
11890                            (symbol-name group)) 'request-accept-article t)))
11891
11892 (defun gnus-read-move-group-name (prompt default articles prefix)
11893   "Read a group name."
11894   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11895          (minibuffer-confirm-incomplete nil) ; XEmacs
11896          (prom
11897           (format "%s %s to"
11898                   prompt
11899                   (if (> (length articles) 1)
11900                       (format "these %d articles" (length articles))
11901                     "this article")))
11902          (to-newsgroup
11903           (let (active group)
11904             (when (or (null split-name) (= 1 (length split-name)))
11905               (setq active (gnus-make-hashtable (length gnus-active-hashtb)))
11906               (mapatoms (lambda (symbol)
11907                           (setq group (symbol-name symbol))
11908                           (when (string-match "[^\000-\177]" group)
11909                             (setq group (gnus-group-decoded-name group)))
11910                           (set (intern group active) group))
11911                         gnus-active-hashtb))
11912             (cond
11913              ((null split-name)
11914               (gnus-completing-read-with-default
11915                default prom active 'gnus-valid-move-group-p nil prefix
11916                'gnus-group-history))
11917              ((= 1 (length split-name))
11918               (gnus-completing-read-with-default
11919                (car split-name) prom active 'gnus-valid-move-group-p nil nil
11920                'gnus-group-history))
11921              (t
11922               (gnus-completing-read-with-default
11923                nil prom (mapcar 'list (nreverse split-name)) nil nil nil
11924                'gnus-group-history)))))
11925          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11926          encoded)
11927     (when to-newsgroup
11928       (if (or (string= to-newsgroup "")
11929               (string= to-newsgroup prefix))
11930           (setq to-newsgroup default))
11931       (unless to-newsgroup
11932         (error "No group name entered"))
11933       (setq encoded (mm-encode-coding-string
11934                      to-newsgroup
11935                      (gnus-group-name-charset to-method to-newsgroup)))
11936       (or (gnus-active encoded)
11937           (gnus-activate-group encoded nil nil to-method)
11938           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11939                                      to-newsgroup))
11940               (or (and (gnus-request-create-group encoded to-method)
11941                        (gnus-activate-group encoded nil nil to-method)
11942                        (gnus-subscribe-group encoded))
11943                   (error "Couldn't create group %s" to-newsgroup)))
11944           (error "No such group: %s" to-newsgroup))
11945       encoded)))
11946
11947 (defvar gnus-summary-save-parts-counter)
11948 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11949
11950 (defun gnus-summary-save-parts (type dir n &optional reverse)
11951   "Save parts matching TYPE to DIR.
11952 If REVERSE, save parts that do not match TYPE."
11953   (interactive
11954    (list (read-string "Save parts of type: "
11955                       (or (car gnus-summary-save-parts-type-history)
11956                           gnus-summary-save-parts-default-mime)
11957                       'gnus-summary-save-parts-type-history)
11958          (setq gnus-summary-save-parts-last-directory
11959                (read-file-name "Save to directory: "
11960                                gnus-summary-save-parts-last-directory
11961                                nil t))
11962          current-prefix-arg))
11963   (gnus-summary-iterate n
11964     (let ((gnus-display-mime-function nil)
11965           gnus-article-prepare-hook
11966           gnus-article-decode-hook
11967           gnus-display-mime-function
11968           gnus-break-pages
11969           (gnus-inhibit-treatment t))
11970       (gnus-summary-select-article))
11971     (with-current-buffer gnus-article-buffer
11972       (let ((handles (or gnus-article-mime-handles
11973                          (mm-dissect-buffer nil gnus-article-loose-mime)
11974                          (and gnus-article-emulate-mime
11975                               (mm-uu-dissect))))
11976             (gnus-summary-save-parts-counter 1))
11977         (when handles
11978           (gnus-summary-save-parts-1 type dir handles reverse)
11979           (unless gnus-article-mime-handles ;; Don't destroy this case.
11980             (mm-destroy-parts handles)))))))
11981
11982 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11983   (if (stringp (car handle))
11984       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11985               (cdr handle))
11986     (when (if reverse
11987               (not (string-match type (mm-handle-media-type handle)))
11988             (string-match type (mm-handle-media-type handle)))
11989       (let ((file (expand-file-name
11990                    (gnus-map-function
11991                     mm-file-name-rewrite-functions
11992                     (file-name-nondirectory
11993                      (or
11994                       (mail-content-type-get
11995                        (mm-handle-disposition handle) 'filename)
11996                       (mail-content-type-get
11997                        (mm-handle-type handle) 'name)
11998                       (format "%s.%d.%d" gnus-newsgroup-name
11999                               (cdr gnus-article-current)
12000                               gnus-summary-save-parts-counter))))
12001                    dir)))
12002         (incf gnus-summary-save-parts-counter)
12003         (unless (file-exists-p file)
12004           (mm-save-part-to-file handle file))))))
12005
12006 ;; Summary extract commands
12007
12008 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12009   (let ((inhibit-read-only t)
12010         (article (gnus-summary-article-number))
12011         after-article b e)
12012     (unless (gnus-summary-goto-subject article)
12013       (error "No such article: %d" article))
12014     (gnus-summary-position-point)
12015     ;; If all commands are to be bunched up on one line, we collect
12016     ;; them here.
12017     (unless gnus-view-pseudos-separately
12018       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12019             files action)
12020         (while ps
12021           (setq action (cdr (assq 'action (car ps))))
12022           (setq files (list (cdr (assq 'name (car ps)))))
12023           (while (and ps (cdr ps)
12024                       (string= (or action "1")
12025                                (or (cdr (assq 'action (cadr ps))) "2")))
12026             (push (cdr (assq 'name (cadr ps))) files)
12027             (setcdr ps (cddr ps)))
12028           (when files
12029             (when (not (string-match "%s" action))
12030               (push " " files))
12031             (push " " files)
12032             (when (assq 'execute (car ps))
12033               (setcdr (assq 'execute (car ps))
12034                       (funcall (if (string-match "%s" action)
12035                                    'format 'concat)
12036                                action
12037                                (mapconcat
12038                                 (lambda (f)
12039                                   (if (equal f " ")
12040                                       f
12041                                     (shell-quote-argument f)))
12042                                 files " ")))))
12043           (setq ps (cdr ps)))))
12044     (if (and gnus-view-pseudos (not not-view))
12045         (while pslist
12046           (when (assq 'execute (car pslist))
12047             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12048                                   (eq gnus-view-pseudos 'not-confirm)))
12049           (setq pslist (cdr pslist)))
12050       (save-excursion
12051         (while pslist
12052           (setq after-article (or (cdr (assq 'article (car pslist)))
12053                                   (gnus-summary-article-number)))
12054           (gnus-summary-goto-subject after-article)
12055           (forward-line 1)
12056           (setq b (point))
12057           (insert "    " (file-name-nondirectory
12058                           (cdr (assq 'name (car pslist))))
12059                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12060           (setq e (point))
12061           (forward-line -1)             ; back to `b'
12062           (gnus-add-text-properties
12063            b (1- e) (list 'gnus-number gnus-reffed-article-number
12064                           gnus-mouse-face-prop gnus-mouse-face))
12065           (gnus-data-enter
12066            after-article gnus-reffed-article-number
12067            gnus-unread-mark b (car pslist) 0 (- e b))
12068           (setq gnus-newsgroup-unreads
12069                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12070                                          gnus-reffed-article-number))
12071           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12072           (setq pslist (cdr pslist)))))))
12073
12074 (defun gnus-pseudos< (p1 p2)
12075   (let ((c1 (cdr (assq 'action p1)))
12076         (c2 (cdr (assq 'action p2))))
12077     (and c1 c2 (string< c1 c2))))
12078
12079 (defun gnus-request-pseudo-article (props)
12080   (cond ((assq 'execute props)
12081          (gnus-execute-command (cdr (assq 'execute props)))))
12082   (let ((gnus-current-article (gnus-summary-article-number)))
12083     (gnus-run-hooks 'gnus-mark-article-hook)))
12084
12085 (defun gnus-execute-command (command &optional automatic)
12086   (save-excursion
12087     (gnus-article-setup-buffer)
12088     (set-buffer gnus-article-buffer)
12089     (setq buffer-read-only nil)
12090     (let ((command (if automatic command
12091                      (read-string "Command: " (cons command 0)))))
12092       (erase-buffer)
12093       (insert "$ " command "\n\n")
12094       (if gnus-view-pseudo-asynchronously
12095           (start-process "gnus-execute" (current-buffer) shell-file-name
12096                          shell-command-switch command)
12097         (call-process shell-file-name nil t nil
12098                       shell-command-switch command)))))
12099
12100 ;; Summary kill commands.
12101
12102 (defun gnus-summary-edit-global-kill (article)
12103   "Edit the \"global\" kill file."
12104   (interactive (list (gnus-summary-article-number)))
12105   (gnus-group-edit-global-kill article))
12106
12107 (defun gnus-summary-edit-local-kill ()
12108   "Edit a local kill file applied to the current newsgroup."
12109   (interactive)
12110   (setq gnus-current-headers (gnus-summary-article-header))
12111   (gnus-group-edit-local-kill
12112    (gnus-summary-article-number) gnus-newsgroup-name))
12113
12114 ;;; Header reading.
12115
12116 (defun gnus-read-header (id &optional header)
12117   "Read the headers of article ID and enter them into the Gnus system."
12118   (let ((group gnus-newsgroup-name)
12119         (gnus-override-method
12120          (or
12121           gnus-override-method
12122           (and (gnus-news-group-p gnus-newsgroup-name)
12123                (car (gnus-refer-article-methods)))))
12124         where)
12125     ;; First we check to see whether the header in question is already
12126     ;; fetched.
12127     (if (stringp id)
12128         ;; This is a Message-ID.
12129         (setq header (or header (gnus-id-to-header id)))
12130       ;; This is an article number.
12131       (setq header (or header (gnus-summary-article-header id))))
12132     (if (and header
12133              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12134         ;; We have found the header.
12135         header
12136       ;; We have to really fetch the header to this article.
12137       (with-current-buffer nntp-server-buffer
12138         (when (setq where (gnus-request-head id group))
12139           (nnheader-fold-continuation-lines)
12140           (goto-char (point-max))
12141           (insert ".\n")
12142           (goto-char (point-min))
12143           (insert "211 ")
12144           (princ (cond
12145                   ((numberp id) id)
12146                   ((cdr where) (cdr where))
12147                   (header (mail-header-number header))
12148                   (t gnus-reffed-article-number))
12149                  (current-buffer))
12150           (insert " Article retrieved.\n"))
12151         (if (or (not where)
12152                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12153             ()                          ; Malformed head.
12154           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12155             (when (and (stringp id)
12156                        (or
12157                         (not (string= (gnus-group-real-name group)
12158                                       (car where)))
12159                         (not (gnus-server-equal gnus-override-method
12160                                                 (gnus-group-method group)))))
12161               ;; If we fetched by Message-ID and the article came from
12162               ;; a different group (or server), we fudge some bogus
12163               ;; article numbers for this article.
12164               (mail-header-set-number header gnus-reffed-article-number))
12165             (with-current-buffer gnus-summary-buffer
12166               (decf gnus-reffed-article-number)
12167               (gnus-remove-header (mail-header-number header))
12168               (push header gnus-newsgroup-headers)
12169               (setq gnus-current-headers header)
12170               (push (mail-header-number header) gnus-newsgroup-limit)))
12171           header)))))
12172
12173 (defun gnus-remove-header (number)
12174   "Remove header NUMBER from `gnus-newsgroup-headers'."
12175   (if (and gnus-newsgroup-headers
12176            (= number (mail-header-number (car gnus-newsgroup-headers))))
12177       (pop gnus-newsgroup-headers)
12178     (let ((headers gnus-newsgroup-headers))
12179       (while (and (cdr headers)
12180                   (not (= number (mail-header-number (cadr headers)))))
12181         (pop headers))
12182       (when (cdr headers)
12183         (setcdr headers (cddr headers))))))
12184
12185 ;;;
12186 ;;; summary highlights
12187 ;;;
12188
12189 (defun gnus-highlight-selected-summary ()
12190   "Highlight selected article in summary buffer."
12191   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12192   (when gnus-summary-selected-face
12193     (save-excursion
12194       (let* ((beg (point-at-bol))
12195              (end (point-at-eol))
12196              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12197              (from (if (get-text-property beg gnus-mouse-face-prop)
12198                        beg
12199                      (or (next-single-property-change
12200                           beg gnus-mouse-face-prop nil end)
12201                          beg)))
12202              (to
12203               (if (= from end)
12204                   (- from 2)
12205                 (or (next-single-property-change
12206                      from gnus-mouse-face-prop nil end)
12207                     end))))
12208         ;; If no mouse-face prop on line we will have to = from = end,
12209         ;; so we highlight the entire line instead.
12210         (when (= (+ to 2) from)
12211           (setq from beg)
12212           (setq to end))
12213         (if gnus-newsgroup-selected-overlay
12214             ;; Move old overlay.
12215             (gnus-move-overlay
12216              gnus-newsgroup-selected-overlay from to (current-buffer))
12217           ;; Create new overlay.
12218           (gnus-overlay-put
12219            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12220            'face gnus-summary-selected-face))))))
12221
12222 (defvar gnus-summary-highlight-line-cached nil)
12223 (defvar gnus-summary-highlight-line-trigger nil)
12224
12225 (defun gnus-summary-highlight-line-0 ()
12226   (if (and (eq gnus-summary-highlight-line-trigger
12227                gnus-summary-highlight)
12228            gnus-summary-highlight-line-cached)
12229       gnus-summary-highlight-line-cached
12230     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12231           gnus-summary-highlight-line-cached
12232           (let* ((cond (list 'cond))
12233                  (c cond)
12234                  (list gnus-summary-highlight))
12235             (while list
12236               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12237                               nil))
12238               (setq c (cdr c)
12239                     list (cdr list)))
12240             (gnus-byte-compile (list 'lambda nil cond))))))
12241
12242 (defun gnus-summary-highlight-line ()
12243   "Highlight current line according to `gnus-summary-highlight'."
12244   (let* ((beg (point-at-bol))
12245          (article (or (gnus-summary-article-number) gnus-current-article))
12246          (score (or (cdr (assq article
12247                                gnus-newsgroup-scored))
12248                     gnus-summary-default-score 0))
12249          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12250          (inhibit-read-only t)
12251          (default gnus-summary-default-score)
12252          (default-high gnus-summary-default-high-score)
12253          (default-low gnus-summary-default-low-score)
12254          (uncached (and gnus-summary-use-undownloaded-faces
12255                         (memq article gnus-newsgroup-undownloaded)
12256                         (not (memq article gnus-newsgroup-cached)))))
12257     (let ((face (funcall (gnus-summary-highlight-line-0))))
12258       (unless (eq face (get-text-property beg 'face))
12259         (gnus-put-text-property-excluding-characters-with-faces
12260          beg (point-at-eol) 'face
12261          (setq face (if (boundp face) (symbol-value face) face)))
12262         (when gnus-summary-highlight-line-function
12263           (funcall gnus-summary-highlight-line-function article face))))))
12264
12265 (defun gnus-update-read-articles (group unread &optional compute)
12266   "Update the list of read articles in GROUP.
12267 UNREAD is a sorted list."
12268   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12269         (info (gnus-get-info group))
12270         (prev 1)
12271         read)
12272     (if (or (not info) (not active))
12273         ;; There is no info on this group if it was, in fact,
12274         ;; killed.  Gnus stores no information on killed groups, so
12275         ;; there's nothing to be done.
12276         ;; One could store the information somewhere temporarily,
12277         ;; perhaps...  Hmmm...
12278         ()
12279       ;; Remove any negative articles numbers.
12280       (while (and unread (< (car unread) 0))
12281         (setq unread (cdr unread)))
12282       ;; Remove any expired article numbers
12283       (while (and unread (< (car unread) (car active)))
12284         (setq unread (cdr unread)))
12285       ;; Compute the ranges of read articles by looking at the list of
12286       ;; unread articles.
12287       (while unread
12288         (when (/= (car unread) prev)
12289           (push (if (= prev (1- (car unread))) prev
12290                   (cons prev (1- (car unread))))
12291                 read))
12292         (setq prev (1+ (car unread)))
12293         (setq unread (cdr unread)))
12294       (when (<= prev (cdr active))
12295         (push (cons prev (cdr active)) read))
12296       (setq read (if (> (length read) 1) (nreverse read) read))
12297       (if compute
12298           read
12299         (save-excursion
12300           (let (setmarkundo)
12301             ;; Propagate the read marks to the backend.
12302             (when (and gnus-propagate-marks
12303                        (gnus-check-backend-function 'request-set-mark group))
12304               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12305                     (add (gnus-remove-from-range read (gnus-info-read info))))
12306                 (when (or add del)
12307                   (unless (gnus-check-group group)
12308                     (error "Can't open server for %s" group))
12309                   (gnus-request-set-mark
12310                    group (delq nil (list (if add (list add 'add '(read)))
12311                                          (if del (list del 'del '(read))))))
12312                   (setq setmarkundo
12313                         `(gnus-request-set-mark
12314                           ,group
12315                           ',(delq nil (list
12316                                        (if del (list del 'add '(read)))
12317                                        (if add (list add 'del '(read))))))))))
12318             (set-buffer gnus-group-buffer)
12319             (gnus-undo-register
12320               `(progn
12321                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12322                  (gnus-info-set-read ',info ',(gnus-info-read info))
12323                  (gnus-get-unread-articles-in-group ',info
12324                                                     (gnus-active ,group))
12325                  (gnus-group-update-group ,group t)
12326                  ,setmarkundo))))
12327         ;; Enter this list into the group info.
12328         (gnus-info-set-read info read)
12329         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12330         (gnus-get-unread-articles-in-group info (gnus-active group))
12331         t))))
12332
12333 (defun gnus-offer-save-summaries ()
12334   "Offer to save all active summary buffers."
12335   (let (buffers)
12336     ;; Go through all buffers and find all summaries.
12337     (dolist (buffer (buffer-list))
12338       (when (and (setq buffer (buffer-name buffer))
12339                  (string-match "Summary" buffer)
12340                  (with-current-buffer buffer
12341                    ;; We check that this is, indeed, a summary buffer.
12342                    (and (eq major-mode 'gnus-summary-mode)
12343                         ;; Also make sure this isn't bogus.
12344                         gnus-newsgroup-prepared
12345                         ;; Also make sure that this isn't a
12346                         ;; dead summary buffer.
12347                         (not gnus-dead-summary-mode))))
12348         (push buffer buffers)))
12349     ;; Go through all these summary buffers and offer to save them.
12350     (when buffers
12351       (save-excursion
12352         (map-y-or-n-p
12353          "Update summary buffer %s? "
12354          (lambda (buf)
12355            (switch-to-buffer buf)
12356            (gnus-summary-exit))
12357          buffers)))))
12358
12359 (defun gnus-summary-setup-default-charset ()
12360   "Setup newsgroup default charset."
12361   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12362       (setq gnus-newsgroup-charset nil)
12363     (let* ((ignored-charsets
12364             (or gnus-newsgroup-ephemeral-ignored-charsets
12365                 (append
12366                  (and gnus-newsgroup-name
12367                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12368                  gnus-newsgroup-ignored-charsets))))
12369       (setq gnus-newsgroup-charset
12370             (or gnus-newsgroup-ephemeral-charset
12371                 (and gnus-newsgroup-name
12372                      (gnus-parameter-charset gnus-newsgroup-name))
12373                 gnus-default-charset))
12374       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12375            ignored-charsets))))
12376
12377 ;;;
12378 ;;; Mime Commands
12379 ;;;
12380
12381 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12382   "Display the current article buffer fully MIME-buttonized.
12383 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12384 treated as multipart/mixed."
12385   (interactive "P")
12386   (require 'gnus-art)
12387   (let ((gnus-unbuttonized-mime-types nil)
12388         (gnus-mime-display-multipart-as-mixed show-all-parts))
12389     (gnus-summary-show-article)))
12390
12391 (defun gnus-summary-repair-multipart (article)
12392   "Add a Content-Type header to a multipart article without one."
12393   (interactive (list (gnus-summary-article-number)))
12394   (gnus-with-article article
12395     (message-narrow-to-head)
12396     (message-remove-header "Mime-Version")
12397     (goto-char (point-max))
12398     (insert "Mime-Version: 1.0\n")
12399     (widen)
12400     (when (search-forward "\n--" nil t)
12401       (let ((separator (buffer-substring (point) (point-at-eol))))
12402         (message-narrow-to-head)
12403         (message-remove-header "Content-Type")
12404         (goto-char (point-max))
12405         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12406                         separator))
12407         (widen))))
12408   (let (gnus-mark-article-hook)
12409     (gnus-summary-select-article t t nil article)))
12410
12411 (defun gnus-summary-toggle-display-buttonized ()
12412   "Toggle the buttonizing of the article buffer."
12413   (interactive)
12414   (require 'gnus-art)
12415   (if (setq gnus-inhibit-mime-unbuttonizing
12416             (not gnus-inhibit-mime-unbuttonizing))
12417       (let ((gnus-unbuttonized-mime-types nil))
12418         (gnus-summary-show-article))
12419     (gnus-summary-show-article)))
12420
12421 ;;;
12422 ;;; Generic summary marking commands
12423 ;;;
12424
12425 (defvar gnus-summary-marking-alist
12426   '((read gnus-del-mark "d")
12427     (unread gnus-unread-mark "u")
12428     (ticked gnus-ticked-mark "!")
12429     (dormant gnus-dormant-mark "?")
12430     (expirable gnus-expirable-mark "e"))
12431   "An alist of names/marks/keystrokes.")
12432
12433 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12434 (defvar gnus-summary-mark-map)
12435
12436 (defun gnus-summary-make-all-marking-commands ()
12437   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12438   (dolist (elem gnus-summary-marking-alist)
12439     (apply 'gnus-summary-make-marking-command elem)))
12440
12441 (defun gnus-summary-make-marking-command (name mark keystroke)
12442   (let ((map (make-sparse-keymap)))
12443     (define-key gnus-summary-generic-mark-map keystroke map)
12444     (dolist (lway `((next "next" next nil "n")
12445                     (next-unread "next unread" next t "N")
12446                     (prev "previous" prev nil "p")
12447                     (prev-unread "previous unread" prev t "P")
12448                     (nomove "" nil nil ,keystroke)))
12449       (let ((func (gnus-summary-make-marking-command-1
12450                    mark (car lway) lway name)))
12451         (setq func (eval func))
12452         (define-key map (nth 4 lway) func)))))
12453
12454 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12455   `(defun ,(intern
12456             (format "gnus-summary-put-mark-as-%s%s"
12457                     name (if (eq way 'nomove)
12458                              ""
12459                            (concat "-" (symbol-name way)))))
12460      (n)
12461      ,(format
12462        "Mark the current article as %s%s.
12463 If N, the prefix, then repeat N times.
12464 If N is negative, move in reverse order.
12465 The difference between N and the actual number of articles marked is
12466 returned."
12467        name (cadr lway))
12468      (interactive "p")
12469      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12470
12471 (defun gnus-summary-generic-mark (n mark move unread)
12472   "Mark N articles with MARK."
12473   (unless (eq major-mode 'gnus-summary-mode)
12474     (error "This command can only be used in the summary buffer"))
12475   (gnus-summary-show-thread)
12476   (let ((nummove
12477          (cond
12478           ((eq move 'next) 1)
12479           ((eq move 'prev) -1)
12480           (t 0))))
12481     (if (zerop nummove)
12482         (setq n 1)
12483       (when (< n 0)
12484         (setq n (abs n)
12485               nummove (* -1 nummove))))
12486     (while (and (> n 0)
12487                 (gnus-summary-mark-article nil mark)
12488                 (zerop (gnus-summary-next-subject nummove unread t)))
12489       (setq n (1- n)))
12490     (when (/= 0 n)
12491       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12492     (gnus-summary-recenter)
12493     (gnus-summary-position-point)
12494     (gnus-set-mode-line 'summary)
12495     n))
12496
12497 (defun gnus-summary-insert-articles (articles)
12498   (when (setq articles
12499               (gnus-sorted-difference articles
12500                                       (mapcar (lambda (h)
12501                                                 (mail-header-number h))
12502                                               gnus-newsgroup-headers)))
12503     (setq gnus-newsgroup-headers
12504           (gnus-merge 'list
12505                       gnus-newsgroup-headers
12506                       (gnus-fetch-headers articles)
12507                       'gnus-article-sort-by-number))
12508     ;; Suppress duplicates?
12509     (when gnus-suppress-duplicates
12510       (gnus-dup-suppress-articles))
12511
12512     (if (and gnus-fetch-old-headers
12513              (eq gnus-headers-retrieved-by 'nov))
12514         ;; We might want to build some more threads first.
12515         (if (eq gnus-fetch-old-headers 'invisible)
12516             (gnus-build-all-threads)
12517           (gnus-build-old-threads))
12518       ;; Mark the inserted articles that are unread as unread.
12519       (setq gnus-newsgroup-unreads
12520             (gnus-sorted-nunion
12521              gnus-newsgroup-unreads
12522              (gnus-sorted-nintersection
12523               (gnus-list-of-unread-articles gnus-newsgroup-name)
12524               articles)))
12525       ;; Mark the inserted articles as selected so that the information
12526       ;; of the marks having been changed by a user may be updated when
12527       ;; exiting this group.  See `gnus-summary-update-info'.
12528       (dolist (art articles)
12529         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12530     ;; Let the Gnus agent mark articles as read.
12531     (when gnus-agent
12532       (gnus-agent-get-undownloaded-list))
12533     ;; Remove list identifiers from subject
12534     (when gnus-list-identifiers
12535       (gnus-summary-remove-list-identifiers))
12536     ;; First and last article in this newsgroup.
12537     (when gnus-newsgroup-headers
12538       (setq gnus-newsgroup-begin
12539             (mail-header-number (car gnus-newsgroup-headers))
12540             gnus-newsgroup-end
12541             (mail-header-number
12542              (gnus-last-element gnus-newsgroup-headers))))
12543     (when gnus-use-scoring
12544       (gnus-possibly-score-headers))))
12545
12546 (defun gnus-summary-insert-old-articles (&optional all)
12547   "Insert all old articles in this group.
12548 If ALL is non-nil, already read articles become readable.
12549 If ALL is a number, fetch this number of articles."
12550   (interactive "P")
12551   (prog1
12552       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12553             older len)
12554         (setq older
12555               ;; Some nntp servers lie about their active range.  When
12556               ;; this happens, the active range can be in the millions.
12557               ;; Use a compressed range to avoid creating a huge list.
12558               (gnus-range-difference (list gnus-newsgroup-active) old))
12559         (setq len (gnus-range-length older))
12560         (cond
12561          ((null older) nil)
12562          ((numberp all)
12563           (if (< all len)
12564               (let ((older-range (nreverse older)))
12565                 (setq older nil)
12566
12567                 (while (> all 0)
12568                   (let* ((r (pop older-range))
12569                          (min (if (numberp r) r (car r)))
12570                          (max (if (numberp r) r (cdr r))))
12571                     (while (and (<= min max)
12572                                 (> all 0))
12573                       (push max older)
12574                       (setq all (1- all)
12575                             max (1- max))))))
12576             (setq older (gnus-uncompress-range older))))
12577          (all
12578           (setq older (gnus-uncompress-range older)))
12579          (t
12580           (when (and (numberp gnus-large-newsgroup)
12581                    (> len gnus-large-newsgroup))
12582               (let* ((cursor-in-echo-area nil)
12583                      (initial (gnus-parameter-large-newsgroup-initial
12584                                gnus-newsgroup-name))
12585                      (input
12586                       (read-string
12587                        (format
12588                         "How many articles from %s (%s %d): "
12589                         (gnus-group-decoded-name gnus-newsgroup-name)
12590                         (if initial "max" "default")
12591                         len)
12592                        (if initial
12593                            (cons (number-to-string initial)
12594                                  0)))))
12595                 (unless (string-match "^[ \t]*$" input)
12596                   (setq all (string-to-number input))
12597                   (if (< all len)
12598                       (let ((older-range (nreverse older)))
12599                         (setq older nil)
12600
12601                         (while (> all 0)
12602                           (let* ((r (pop older-range))
12603                                  (min (if (numberp r) r (car r)))
12604                                  (max (if (numberp r) r (cdr r))))
12605                             (while (and (<= min max)
12606                                         (> all 0))
12607                               (push max older)
12608                               (setq all (1- all)
12609                                     max (1- max))))))))))
12610           (setq older (gnus-uncompress-range older))))
12611         (if (not older)
12612             (message "No old news.")
12613           (gnus-summary-insert-articles older)
12614           (gnus-summary-limit (gnus-sorted-nunion old older))))
12615     (gnus-summary-position-point)))
12616
12617 (defun gnus-summary-insert-new-articles ()
12618   "Insert all new articles in this group."
12619   (interactive)
12620   (prog1
12621       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12622             (old-active gnus-newsgroup-active)
12623             (nnmail-fetched-sources (list t))
12624             i new)
12625         (setq gnus-newsgroup-active
12626               (gnus-activate-group gnus-newsgroup-name 'scan))
12627         (setq i (cdr gnus-newsgroup-active))
12628         (while (> i (cdr old-active))
12629           (push i new)
12630           (decf i))
12631         (if (not new)
12632             (message "No gnus is bad news")
12633           (gnus-summary-insert-articles new)
12634           (setq gnus-newsgroup-unreads
12635                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12636           (gnus-summary-limit (gnus-sorted-nunion old new))))
12637     (gnus-summary-position-point)))
12638
12639 ;;; Bookmark support for Gnus.
12640 (declare-function bookmark-make-record-default
12641                   "bookmark" (&optional no-file no-context posn))
12642 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12643 (declare-function bookmark-default-handler "bookmark" (bmk))
12644 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12645 (defvar bookmark-yank-point)
12646 (defvar bookmark-current-buffer)
12647
12648 (defun gnus-summary-bookmark-make-record ()
12649   "Make a bookmark entry for a Gnus summary buffer."
12650   (let (pos buf)
12651     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12652       (save-restriction              ; FIXME is it necessary to widen?
12653         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12654       (setq buf "art") ; We are recording bookmark from article buffer.
12655       (setq bookmark-yank-point (point))
12656       (setq bookmark-current-buffer (current-buffer))
12657       (gnus-article-show-summary))      ; Go back in summary buffer.
12658     ;; We are now recording bookmark from summary buffer.
12659     (unless buf (setq buf "sum"))
12660     (let* ((subject (elt (gnus-summary-article-header) 1))
12661            (grp     (car gnus-article-current))
12662            (art     (cdr gnus-article-current))
12663            (head    (gnus-summary-article-header art))
12664            (id      (mail-header-id head)))
12665       `(,subject
12666         ,@(condition-case nil
12667               (bookmark-make-record-default 'no-file 'no-context pos)
12668             (wrong-number-of-arguments
12669              (bookmark-make-record-default 'point-only)))
12670         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12671         (group . ,grp) (article . ,art)
12672         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12673
12674 ;;;###autoload
12675 (defun gnus-summary-bookmark-jump (bookmark)
12676   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12677 BOOKMARK is a bookmark name or a bookmark record."
12678   (let ((group    (bookmark-prop-get bookmark 'group))
12679         (article  (bookmark-prop-get bookmark 'article))
12680         (id       (bookmark-prop-get bookmark 'message-id))
12681         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12682     (gnus-fetch-group group (list article))
12683     (gnus-summary-insert-cached-articles)
12684     (gnus-summary-goto-article id nil 'force)
12685     ;; FIXME we have to wait article buffer is ready (only large buffer)
12686     ;; Is there a better solution to know that?
12687     ;; If we don't wait `bookmark-default-handler' will have no chance
12688     ;; to set position. However there is no error, just wrong pos.
12689     (sit-for 1)
12690     (when (string= buf "Gnus-art")
12691       (other-window 1))
12692     (bookmark-default-handler
12693      `(""
12694        (buffer . ,(current-buffer))
12695        . ,(bookmark-get-bookmark-record bookmark)))))
12696
12697 (gnus-summary-make-all-marking-commands)
12698
12699 (gnus-ems-redefine)
12700
12701 (provide 'gnus-sum)
12702
12703 (run-hooks 'gnus-sum-load-hook)
12704
12705 ;; Local Variables:
12706 ;; coding: iso-8859-1
12707 ;; End:
12708
12709 ;;; gnus-sum.el ends here