(gnus-summary-kill-thread): Use gnus-summary-mark-article instead of
[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-fetch-old-headers nil
80   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
81 If an unread article in the group refers to an older, already
82 read (or just marked as read) article, the old article will not
83 normally be displayed in the Summary buffer.  If this variable is
84 t, Gnus will attempt to grab the headers to the old articles, and
85 thereby build complete threads.  If it has the value `some', all
86 old headers will be fetched but only enough headers to connect
87 otherwise loose threads will be displayed.  This variable can
88 also be a number.  In that case, no more than that number of old
89 headers will be fetched.  If it has the value `invisible', all
90 old headers will be fetched, but none will be displayed.
91
92 The server has to support NOV for any of this to work.
93
94 This feature can seriously impact performance it ignores all
95 locally cached header entries.  Setting it to t for groups for a
96 server that doesn't expire articles (such as news.gmane.org),
97 leads to very slow summary generation."
98   :group 'gnus-thread
99   :type '(choice (const :tag "off" nil)
100                  (const :tag "on" t)
101                  (const some)
102                  (const invisible)
103                  number
104                  (sexp :menu-tag "other" t)))
105
106 (defcustom gnus-refer-thread-limit 500
107   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
108 If t, fetch all the available old headers."
109   :group 'gnus-thread
110   :type '(choice number
111                  (sexp :menu-tag "other" t)))
112
113 (defcustom gnus-summary-make-false-root 'adopt
114   "*nil means that Gnus won't gather loose threads.
115 If the root of a thread has expired or been read in a previous
116 session, the information necessary to build a complete thread has been
117 lost.  Instead of having many small sub-threads from this original thread
118 scattered all over the summary buffer, Gnus can gather them.
119
120 If non-nil, Gnus will try to gather all loose sub-threads from an
121 original thread into one large thread.
122
123 If this variable is non-nil, it should be one of `none', `adopt',
124 `dummy' or `empty'.
125
126 If this variable is `none', Gnus will not make a false root, but just
127 present the sub-threads after another.
128 If this variable is `dummy', Gnus will create a dummy root that will
129 have all the sub-threads as children.
130 If this variable is `adopt', Gnus will make one of the \"children\"
131 the parent and mark all the step-children as such.
132 If this variable is `empty', the \"children\" are printed with empty
133 subject fields.  (Or rather, they will be printed with a string
134 given by the `gnus-summary-same-subject' variable.)"
135   :group 'gnus-thread
136   :type '(choice (const :tag "off" nil)
137                  (const none)
138                  (const dummy)
139                  (const adopt)
140                  (const empty)))
141
142 (defcustom gnus-summary-make-false-root-always nil
143   "Always make a false dummy root."
144   :version "22.1"
145   :group 'gnus-thread
146   :type 'boolean)
147
148 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
149   "*A regexp to match subjects to be excluded from loose thread gathering.
150 As loose thread gathering is done on subjects only, that means that
151 there can be many false gatherings performed.  By rooting out certain
152 common subjects, gathering might become saner."
153   :group 'gnus-thread
154   :type 'regexp)
155
156 (defcustom gnus-summary-gather-subject-limit nil
157   "*Maximum length of subject comparisons when gathering loose threads.
158 Use nil to compare full subjects.  Setting this variable to a low
159 number will help gather threads that have been corrupted by
160 newsreaders chopping off subject lines, but it might also mean that
161 unrelated articles that have subject that happen to begin with the
162 same few characters will be incorrectly gathered.
163
164 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
165 comparing subjects."
166   :group 'gnus-thread
167   :type '(choice (const :tag "off" nil)
168                  (const fuzzy)
169                  (sexp :menu-tag "on" t)))
170
171 (defcustom gnus-simplify-subject-functions nil
172   "List of functions taking a string argument that simplify subjects.
173 The functions are applied recursively.
174
175 Useful functions to put in this list include:
176 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
177 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
178   :group 'gnus-thread
179   :type '(repeat function))
180
181 (defcustom gnus-simplify-ignored-prefixes nil
182   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
183   :group 'gnus-thread
184   :type '(choice (const :tag "off" nil)
185                  regexp))
186
187 (defcustom gnus-build-sparse-threads nil
188   "*If non-nil, fill in the gaps in threads.
189 If `some', only fill in the gaps that are needed to tie loose threads
190 together.  If `more', fill in all leaf nodes that Gnus can find.  If
191 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
192   :group 'gnus-thread
193   :type '(choice (const :tag "off" nil)
194                  (const some)
195                  (const more)
196                  (sexp :menu-tag "all" t)))
197
198 (defcustom gnus-summary-thread-gathering-function
199   'gnus-gather-threads-by-subject
200   "*Function used for gathering loose threads.
201 There are two pre-defined functions: `gnus-gather-threads-by-subject',
202 which only takes Subjects into consideration; and
203 `gnus-gather-threads-by-references', which compared the References
204 headers of the articles to find matches."
205   :group 'gnus-thread
206   :type '(radio (function-item gnus-gather-threads-by-subject)
207                 (function-item gnus-gather-threads-by-references)
208                 (function :tag "other")))
209
210 (defcustom gnus-summary-same-subject ""
211   "*String indicating that the current article has the same subject as the previous.
212 This variable will only be used if the value of
213 `gnus-summary-make-false-root' is `empty'."
214   :group 'gnus-summary-format
215   :type 'string)
216
217 (defcustom gnus-summary-goto-unread t
218   "*If t, many commands will go to the next unread article.
219 This applies to marking commands as well as other commands that
220 \"naturally\" select the next article, like, for instance, `SPC' at
221 the end of an article.
222
223 If nil, the marking commands do NOT go to the next unread article
224 \(they go to the next article instead).  If `never', commands that
225 usually go to the next unread article, will go to the next article,
226 whether it is read or not."
227   :group 'gnus-summary-marks
228   :link '(custom-manual "(gnus)Setting Marks")
229   :type '(choice (const :tag "off" nil)
230                  (const never)
231                  (sexp :menu-tag "on" t)))
232
233 (defcustom gnus-summary-default-score 0
234   "*Default article score level.
235 All scores generated by the score files will be added to this score.
236 If this variable is nil, scoring will be disabled."
237   :group 'gnus-score-default
238   :type '(choice (const :tag "disable")
239                  integer))
240
241 (defcustom gnus-summary-default-high-score 0
242   "*Default threshold for a high scored article.
243 An article will be highlighted as high scored if its score is greater
244 than this score."
245   :version "22.1"
246   :group 'gnus-score-default
247   :type 'integer)
248
249 (defcustom gnus-summary-default-low-score 0
250   "*Default threshold for a low scored article.
251 An article will be highlighted as low scored if its score is smaller
252 than this score."
253   :version "22.1"
254   :group 'gnus-score-default
255   :type 'integer)
256
257 (defcustom gnus-summary-zcore-fuzz 0
258   "*Fuzziness factor for the zcore in the summary buffer.
259 Articles with scores closer than this to `gnus-summary-default-score'
260 will not be marked."
261   :group 'gnus-summary-format
262   :type 'integer)
263
264 (defcustom gnus-simplify-subject-fuzzy-regexp nil
265   "*Strings to be removed when doing fuzzy matches.
266 This can either be a regular expression or list of regular expressions
267 that will be removed from subject strings if fuzzy subject
268 simplification is selected."
269   :group 'gnus-thread
270   :type '(repeat regexp))
271
272 (defcustom gnus-show-threads t
273   "*If non-nil, display threads in summary mode."
274   :group 'gnus-thread
275   :type 'boolean)
276
277 (defcustom gnus-thread-hide-subtree nil
278   "*If non-nil, hide all threads initially.
279 This can be a predicate specifier which says which threads to hide.
280 If threads are hidden, you have to run the command
281 `gnus-summary-show-thread' by hand or select an article."
282   :group 'gnus-thread
283   :type '(radio (sexp :format "Non-nil\n"
284                       :match (lambda (widget value)
285                                (not (or (consp value) (functionp value))))
286                       :value t)
287                 (const nil)
288                 (sexp :tag "Predicate specifier")))
289
290 (defcustom gnus-thread-hide-killed t
291   "*If non-nil, hide killed threads automatically."
292   :group 'gnus-thread
293   :type 'boolean)
294
295 (defcustom gnus-thread-ignore-subject t
296   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
297 If nil, articles that have different subjects from their parents will
298 start separate threads."
299   :group 'gnus-thread
300   :type 'boolean)
301
302 (defcustom gnus-thread-operation-ignore-subject t
303   "*If non-nil, subjects will be ignored when doing thread commands.
304 This affects commands like `gnus-summary-kill-thread' and
305 `gnus-summary-lower-thread'.
306
307 If this variable is nil, articles in the same thread with different
308 subjects will not be included in the operation in question.  If this
309 variable is `fuzzy', only articles that have subjects that are fuzzily
310 equal will be included."
311   :group 'gnus-thread
312   :type '(choice (const :tag "off" nil)
313                  (const fuzzy)
314                  (sexp :tag "on" t)))
315
316 (defcustom gnus-thread-indent-level 4
317   "*Number that says how much each sub-thread should be indented."
318   :group 'gnus-thread
319   :type 'integer)
320
321 (defcustom gnus-auto-extend-newsgroup t
322   "*If non-nil, extend newsgroup forward and backward when requested."
323   :group 'gnus-summary-choose
324   :type 'boolean)
325
326 (defcustom gnus-auto-select-first t
327   "If non-nil, select an article on group entry.
328 An article is selected automatically when entering a group
329 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
330 `gnus-summary-catchup-and-goto-next-group'.
331
332 Which article is selected is controlled by the variable
333 `gnus-auto-select-subject'.
334
335 If you want to prevent automatic selection of articles in some
336 newsgroups, set the variable to nil in `gnus-select-group-hook'."
337   ;; Commands include...
338   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
339   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
340   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
341   :group 'gnus-group-select
342   :type '(choice (const :tag "none" nil)
343                  (sexp :menu-tag "first" t)))
344
345 (defcustom gnus-auto-select-subject 'unread
346   "*Says what subject to place under point when entering a group.
347
348 This variable can either be the symbols `first' (place point on the
349 first subject), `unread' (place point on the subject line of the first
350 unread article), `best' (place point on the subject line of the
351 higest-scored article), `unseen' (place point on the subject line of
352 the first unseen article), `unseen-or-unread' (place point on the subject
353 line of the first unseen article or, if all article have been seen, on the
354 subject line of the first unread article), or a function to be called to
355 place point on some subject line."
356   :version "22.1"
357   :group 'gnus-group-select
358   :type '(choice (const best)
359                  (const unread)
360                  (const first)
361                  (const unseen)
362                  (const unseen-or-unread)))
363
364 (defcustom gnus-auto-select-next t
365   "*If non-nil, offer to go to the next group from the end of the previous.
366 If the value is t and the next newsgroup is empty, Gnus will exit
367 summary mode and go back to group mode.  If the value is neither nil
368 nor t, Gnus will select the following unread newsgroup.  In
369 particular, if the value is the symbol `quietly', the next unread
370 newsgroup will be selected without any confirmation, and if it is
371 `almost-quietly', the next group will be selected without any
372 confirmation if you are located on the last article in the group.
373 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
374 will go to the next group without confirmation."
375   :group 'gnus-summary-maneuvering
376   :type '(choice (const :tag "off" nil)
377                  (const quietly)
378                  (const almost-quietly)
379                  (const slightly-quietly)
380                  (sexp :menu-tag "on" t)))
381
382 (defcustom gnus-auto-select-same nil
383   "*If non-nil, select the next article with the same subject.
384 If there are no more articles with the same subject, go to
385 the first unread article."
386   :group 'gnus-summary-maneuvering
387   :type 'boolean)
388
389 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
390   "What article should be selected after exiting an ephemeral group.
391 Valid values include:
392
393 `next'
394   Select the next article.
395 `next-unread'
396   Select the next unread article.
397 `next-noselect'
398   Move the cursor to the next article.  This is the default.
399 `next-unread-noselect'
400   Move the cursor to the next unread article.
401
402 If it has any other value or there is no next (unread) article, the
403 article selected before entering to the ephemeral group will appear."
404   :version "23.1" ;; No Gnus
405   :group 'gnus-summary-maneuvering
406   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
407                  (const next) (const next-unread)
408                  (const next-noselect) (const next-unread-noselect)
409                  (sexp :tag "other" :value nil)))
410
411 (defcustom gnus-auto-goto-ignores 'unfetched
412   "*Says how to handle unfetched articles when maneuvering.
413
414 This variable can either be the symbols nil (maneuver to any
415 article), `undownloaded' (maneuvering while unplugged ignores articles
416 that have not been fetched), `always-undownloaded' (maneuvering always
417 ignores articles that have not been fetched), `unfetched' (maneuvering
418 ignores articles whose headers have not been fetched).
419
420 NOTE: The list of unfetched articles will always be nil when plugged
421 and, when unplugged, a subset of the undownloaded article list."
422   :version "22.1"
423   :group 'gnus-summary-maneuvering
424   :type '(choice (const :tag "None" nil)
425                  (const :tag "Undownloaded when unplugged" undownloaded)
426                  (const :tag "Undownloaded" always-undownloaded)
427                  (const :tag "Unfetched" unfetched)))
428
429 (defcustom gnus-summary-check-current nil
430   "*If non-nil, consider the current article when moving.
431 The \"unread\" movement commands will stay on the same line if the
432 current article is unread."
433   :group 'gnus-summary-maneuvering
434   :type 'boolean)
435
436 (defcustom gnus-auto-center-summary 2
437   "*If non-nil, always center the current summary buffer.
438 In particular, if `vertical' do only vertical recentering.  If non-nil
439 and non-`vertical', do both horizontal and vertical recentering."
440   :group 'gnus-summary-maneuvering
441   :type '(choice (const :tag "none" nil)
442                  (const vertical)
443                  (integer :tag "height")
444                  (sexp :menu-tag "both" t)))
445
446 (defvar gnus-auto-center-group t
447   "*If non-nil, always center the group buffer.")
448
449 (defcustom gnus-show-all-headers nil
450   "*If non-nil, don't hide any headers."
451   :group 'gnus-article-hiding
452   :group 'gnus-article-headers
453   :type 'boolean)
454
455 (defcustom gnus-summary-ignore-duplicates nil
456   "*If non-nil, ignore articles with identical Message-ID headers."
457   :group 'gnus-summary
458   :type 'boolean)
459
460 (defcustom gnus-single-article-buffer t
461   "*If non-nil, display all articles in the same buffer.
462 If nil, each group will get its own article buffer."
463   :group 'gnus-article-various
464   :type 'boolean)
465
466 (defcustom gnus-break-pages t
467   "*If non-nil, do page breaking on articles.
468 The page delimiter is specified by the `gnus-page-delimiter'
469 variable."
470   :group 'gnus-article-various
471   :type 'boolean)
472
473 (defcustom gnus-move-split-methods nil
474   "*Variable used to suggest where articles are to be moved to.
475 It uses the same syntax as the `gnus-split-methods' variable.
476 However, whereas `gnus-split-methods' specifies file names as targets,
477 this variable specifies group names."
478   :group 'gnus-summary-mail
479   :type '(repeat (choice (list :value (fun) function)
480                          (cons :value ("" "") regexp (repeat string))
481                          (sexp :value nil))))
482
483 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
484   "Function used to compute default prefix for article move/copy/etc prompts.
485 The function should take one argument, a group name, and return a
486 string with the suggested prefix."
487   :group 'gnus-summary-mail
488   :type 'function)
489
490 ;; FIXME: Although the custom type is `character' for the following variables,
491 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
492
493 (defcustom gnus-unread-mark ?           ;Whitespace
494   "*Mark used for unread articles."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-ticked-mark ?!
499   "*Mark used for ticked articles."
500   :group 'gnus-summary-marks
501   :type 'character)
502
503 (defcustom gnus-dormant-mark ??
504   "*Mark used for dormant articles."
505   :group 'gnus-summary-marks
506   :type 'character)
507
508 (defcustom gnus-del-mark ?r
509   "*Mark used for del'd articles."
510   :group 'gnus-summary-marks
511   :type 'character)
512
513 (defcustom gnus-read-mark ?R
514   "*Mark used for read articles."
515   :group 'gnus-summary-marks
516   :type 'character)
517
518 (defcustom gnus-expirable-mark ?E
519   "*Mark used for expirable articles."
520   :group 'gnus-summary-marks
521   :type 'character)
522
523 (defcustom gnus-killed-mark ?K
524   "*Mark used for killed articles."
525   :group 'gnus-summary-marks
526   :type 'character)
527
528 (defcustom gnus-spam-mark ?$
529   "*Mark used for spam articles."
530   :version "22.1"
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-souped-mark ?F
535   "*Mark used for souped articles."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-kill-file-mark ?X
540   "*Mark used for articles killed by kill files."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-low-score-mark ?Y
545   "*Mark used for articles with a low score."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-catchup-mark ?C
550   "*Mark used for articles that are caught up."
551   :group 'gnus-summary-marks
552   :type 'character)
553
554 (defcustom gnus-replied-mark ?A
555   "*Mark used for articles that have been replied to."
556   :group 'gnus-summary-marks
557   :type 'character)
558
559 (defcustom gnus-forwarded-mark ?F
560   "*Mark used for articles that have been forwarded."
561   :version "22.1"
562   :group 'gnus-summary-marks
563   :type 'character)
564
565 (defcustom gnus-recent-mark ?N
566   "*Mark used for articles that are recent."
567   :version "22.1"
568   :group 'gnus-summary-marks
569   :type 'character)
570
571 (defcustom gnus-cached-mark ?*
572   "*Mark used for articles that are in the cache."
573   :group 'gnus-summary-marks
574   :type 'character)
575
576 (defcustom gnus-saved-mark ?S
577   "*Mark used for articles that have been saved."
578   :group 'gnus-summary-marks
579   :type 'character)
580
581 (defcustom gnus-unseen-mark ?.
582   "*Mark used for articles that haven't been seen."
583   :version "22.1"
584   :group 'gnus-summary-marks
585   :type 'character)
586
587 (defcustom gnus-no-mark ?               ;Whitespace
588   "*Mark used for articles that have no other secondary mark."
589   :version "22.1"
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-ancient-mark ?O
594   "*Mark used for ancient articles."
595   :group 'gnus-summary-marks
596   :type 'character)
597
598 (defcustom gnus-sparse-mark ?Q
599   "*Mark used for sparsely reffed articles."
600   :group 'gnus-summary-marks
601   :type 'character)
602
603 (defcustom gnus-canceled-mark ?G
604   "*Mark used for canceled articles."
605   :group 'gnus-summary-marks
606   :type 'character)
607
608 (defcustom gnus-duplicate-mark ?M
609   "*Mark used for duplicate articles."
610   :group 'gnus-summary-marks
611   :type 'character)
612
613 (defcustom gnus-undownloaded-mark ?-
614   "*Mark used for articles that weren't downloaded."
615   :version "22.1"
616   :group 'gnus-summary-marks
617   :type 'character)
618
619 (defcustom gnus-downloaded-mark ?+
620   "*Mark used for articles that were downloaded."
621   :group 'gnus-summary-marks
622   :type 'character)
623
624 (defcustom gnus-downloadable-mark ?%
625   "*Mark used for articles that are to be downloaded."
626   :group 'gnus-summary-marks
627   :type 'character)
628
629 (defcustom gnus-unsendable-mark ?=
630   "*Mark used for articles that won't be sent."
631   :group 'gnus-summary-marks
632   :type 'character)
633
634 (defcustom gnus-score-over-mark ?+
635   "*Score mark used for articles with high scores."
636   :group 'gnus-summary-marks
637   :type 'character)
638
639 (defcustom gnus-score-below-mark ?-
640   "*Score mark used for articles with low scores."
641   :group 'gnus-summary-marks
642   :type 'character)
643
644 (defcustom gnus-empty-thread-mark ?     ;Whitespace
645   "*There is no thread under the article."
646   :group 'gnus-summary-marks
647   :type 'character)
648
649 (defcustom gnus-not-empty-thread-mark ?=
650   "*There is a thread under the article."
651   :group 'gnus-summary-marks
652   :type 'character)
653
654 (defcustom gnus-view-pseudo-asynchronously nil
655   "*If non-nil, Gnus will view pseudo-articles asynchronously."
656   :group 'gnus-extract-view
657   :type 'boolean)
658
659 (defcustom gnus-auto-expirable-marks
660   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
661         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
662         gnus-souped-mark gnus-duplicate-mark)
663   "*The list of marks converted into expiration if a group is auto-expirable."
664   :version "21.1"
665   :group 'gnus-summary
666   :type '(repeat character))
667
668 (defcustom gnus-inhibit-user-auto-expire t
669   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
670   :version "21.1"
671   :group 'gnus-summary
672   :type 'boolean)
673
674 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
675   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
676 If nil, the expirable marks will be unchanged except that the marks
677 will be removed when copying or moving articles to a group that has
678 not turned auto-expire on.  If non-nil, articles that have been read
679 will be marked as expirable when being copied or moved to a group in
680 which auto-expire is turned on."
681   :version "23.2"
682   :type 'boolean
683   :group 'gnus-summary-marks)
684
685 (defcustom gnus-view-pseudos nil
686   "*If `automatic', pseudo-articles will be viewed automatically.
687 If `not-confirm', pseudos will be viewed automatically, and the user
688 will not be asked to confirm the command."
689   :group 'gnus-extract-view
690   :type '(choice (const :tag "off" nil)
691                  (const automatic)
692                  (const not-confirm)))
693
694 (defcustom gnus-view-pseudos-separately t
695   "*If non-nil, one pseudo-article will be created for each file to be viewed.
696 If nil, all files that use the same viewing command will be given as a
697 list of parameters to that command."
698   :group 'gnus-extract-view
699   :type 'boolean)
700
701 (defcustom gnus-insert-pseudo-articles t
702   "*If non-nil, insert pseudo-articles when decoding articles."
703   :group 'gnus-extract-view
704   :type 'boolean)
705
706 (defcustom gnus-summary-dummy-line-format
707   "   %(:                             :%) %S\n"
708   "*The format specification for the dummy roots in the summary buffer.
709 It works along the same lines as a normal formatting string,
710 with some simple extensions.
711
712 %S  The subject
713
714 General format specifiers can also be used.
715 See `(gnus)Formatting Variables'."
716   :link '(custom-manual "(gnus)Formatting Variables")
717   :group 'gnus-threading
718   :type 'string)
719
720 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
721   "*The format specification for the summary mode line.
722 It works along the same lines as a normal formatting string,
723 with some simple extensions:
724
725 %G  Group name
726 %p  Unprefixed group name
727 %A  Current article number
728 %z  Current article score
729 %V  Gnus version
730 %U  Number of unread articles in the group
731 %e  Number of unselected articles in the group
732 %Z  A string with unread/unselected article counts
733 %g  Shortish group name
734 %S  Subject of the current article
735 %u  User-defined spec
736 %s  Current score file name
737 %d  Number of dormant articles
738 %r  Number of articles that have been marked as read in this session
739 %E  Number of articles expunged by the score files"
740   :group 'gnus-summary-format
741   :type 'string)
742
743 (defcustom gnus-list-identifiers nil
744   "Regexp that matches list identifiers to be removed from subject.
745 This can also be a list of regexps."
746   :version "21.1"
747   :group 'gnus-summary-format
748   :group 'gnus-article-hiding
749   :type '(choice (const :tag "none" nil)
750                  (regexp :value ".*")
751                  (repeat :value (".*") regexp)))
752
753 (defcustom gnus-summary-mark-below 0
754   "*Mark all articles with a score below this variable as read.
755 This variable is local to each summary buffer and usually set by the
756 score file."
757   :group 'gnus-score-default
758   :type 'integer)
759
760 (defun gnus-widget-reversible-match (widget value)
761   "Ignoring WIDGET, convert VALUE to internal form.
762 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
763   ;; (debug value)
764   (or (symbolp value)
765       (and (listp value)
766            (eq (length value) 2)
767            (eq (nth 0 value) 'not)
768            (symbolp (nth 1 value)))))
769
770 (defun gnus-widget-reversible-to-internal (widget value)
771   "Ignoring WIDGET, convert VALUE to internal form.
772 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
773 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
774   ;; (debug value)
775   (if (atom value)
776       (list value nil)
777     (list (nth 1 value) t)))
778
779 (defun gnus-widget-reversible-to-external (widget value)
780   "Ignoring WIDGET, convert VALUE to external form.
781 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
782 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
783   ;; (debug value)
784   (if (nth 1 value)
785       (list 'not (nth 0 value))
786     (nth 0 value)))
787
788 (define-widget 'gnus-widget-reversible 'group
789   "A `group' that convert values."
790   :match 'gnus-widget-reversible-match
791   :value-to-internal 'gnus-widget-reversible-to-internal
792   :value-to-external 'gnus-widget-reversible-to-external)
793
794 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
795   "*List of functions used for sorting articles in the summary buffer.
796
797 Each function takes two articles and returns non-nil if the first
798 article should be sorted before the other.  If you use more than one
799 function, the primary sort function should be the last.  You should
800 probably always include `gnus-article-sort-by-number' in the list of
801 sorting functions -- preferably first.  Also note that sorting by date
802 is often much slower than sorting by number, and the sorting order is
803 very similar.  (Sorting by date means sorting by the time the message
804 was sent, sorting by number means sorting by arrival time.)
805
806 Each item can also be a list `(not F)' where F is a function;
807 this reverses the sort order.
808
809 Ready-made functions include `gnus-article-sort-by-number',
810 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
811 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
812 and `gnus-article-sort-by-score'.
813
814 When threading is turned on, the variable `gnus-thread-sort-functions'
815 controls how articles are sorted."
816   :group 'gnus-summary-sort
817   :type '(repeat (gnus-widget-reversible
818                   (choice (function-item gnus-article-sort-by-number)
819                           (function-item gnus-article-sort-by-author)
820                           (function-item gnus-article-sort-by-subject)
821                           (function-item gnus-article-sort-by-date)
822                           (function-item gnus-article-sort-by-score)
823                           (function-item gnus-article-sort-by-random)
824                           (function :tag "other"))
825                   (boolean :tag "Reverse order"))))
826
827
828 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
829   "*List of functions used for sorting threads in the summary buffer.
830 By default, threads are sorted by article number.
831
832 Each function takes two threads and returns non-nil if the first
833 thread should be sorted before the other.  If you use more than one
834 function, the primary sort function should be the last.  You should
835 probably always include `gnus-thread-sort-by-number' in the list of
836 sorting functions -- preferably first.  Also note that sorting by date
837 is often much slower than sorting by number, and the sorting order is
838 very similar.  (Sorting by date means sorting by the time the message
839 was sent, sorting by number means sorting by arrival time.)
840
841 Each list item can also be a list `(not F)' where F is a
842 function; this specifies reversed sort order.
843
844 Ready-made functions include `gnus-thread-sort-by-number',
845 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
846 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
847 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
848 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
849 and `gnus-thread-sort-by-total-score' (see
850 `gnus-thread-score-function').
851
852 When threading is turned off, the variable
853 `gnus-article-sort-functions' controls how articles are sorted."
854   :group 'gnus-summary-sort
855   :type '(repeat
856           (gnus-widget-reversible
857            (choice (function-item gnus-thread-sort-by-number)
858                    (function-item gnus-thread-sort-by-author)
859                    (function-item gnus-thread-sort-by-recipient)
860                    (function-item gnus-thread-sort-by-subject)
861                    (function-item gnus-thread-sort-by-date)
862                    (function-item gnus-thread-sort-by-score)
863                    (function-item gnus-thread-sort-by-most-recent-number)
864                    (function-item gnus-thread-sort-by-most-recent-date)
865                    (function-item gnus-thread-sort-by-random)
866                    (function-item gnus-thread-sort-by-total-score)
867                    (function :tag "other"))
868            (boolean :tag "Reverse order"))))
869
870 (defcustom gnus-thread-score-function '+
871   "*Function used for calculating the total score of a thread.
872
873 The function is called with the scores of the article and each
874 subthread and should then return the score of the thread.
875
876 Some functions you can use are `+', `max', or `min'."
877   :group 'gnus-summary-sort
878   :type 'function)
879
880 (defcustom gnus-summary-expunge-below nil
881   "All articles that have a score less than this variable will be expunged.
882 This variable is local to the summary buffers."
883   :group 'gnus-score-default
884   :type '(choice (const :tag "off" nil)
885                  integer))
886
887 (defcustom gnus-thread-expunge-below nil
888   "All threads that have a total score less than this variable will be expunged.
889 See `gnus-thread-score-function' for en explanation of what a
890 \"thread score\" is.
891
892 This variable is local to the summary buffers."
893   :group 'gnus-threading
894   :group 'gnus-score-default
895   :type '(choice (const :tag "off" nil)
896                  integer))
897
898 (defcustom gnus-summary-mode-hook nil
899   "*A hook for Gnus summary mode.
900 This hook is run before any variables are set in the summary buffer."
901   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
902   :group 'gnus-summary-various
903   :type 'hook)
904
905 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
906 (when (featurep 'xemacs)
907   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
908   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
909   (add-hook 'gnus-summary-mode-hook
910             'gnus-xmas-switch-horizontal-scrollbar-off))
911
912 (defcustom gnus-summary-menu-hook nil
913   "*Hook run after the creation of the summary mode menu."
914   :group 'gnus-summary-visual
915   :type 'hook)
916
917 (defcustom gnus-summary-exit-hook nil
918   "*A hook called on exit from the summary buffer.
919 It will be called with point in the group buffer."
920   :group 'gnus-summary-exit
921   :type 'hook)
922
923 (defcustom gnus-summary-prepare-hook nil
924   "*A hook called after the summary buffer has been generated.
925 If you want to modify the summary buffer, you can use this hook."
926   :group 'gnus-summary-various
927   :type 'hook)
928
929 (defcustom gnus-summary-prepared-hook nil
930   "*A hook called as the last thing after the summary buffer has been generated."
931   :group 'gnus-summary-various
932   :type 'hook)
933
934 (defcustom gnus-summary-generate-hook nil
935   "*A hook run just before generating the summary buffer.
936 This hook is commonly used to customize threading variables and the
937 like."
938   :group 'gnus-summary-various
939   :type 'hook)
940
941 (defcustom gnus-select-group-hook nil
942   "*A hook called when a newsgroup is selected.
943
944 If you'd like to simplify subjects like the
945 `gnus-summary-next-same-subject' command does, you can use the
946 following hook:
947
948  (add-hook gnus-select-group-hook
949            (lambda ()
950              (mapcar (lambda (header)
951                        (mail-header-set-subject
952                         header
953                         (gnus-simplify-subject
954                          (mail-header-subject header) 're-only)))
955                      gnus-newsgroup-headers)))"
956   :group 'gnus-group-select
957   :type 'hook)
958
959 (defcustom gnus-select-article-hook nil
960   "*A hook called when an article is selected."
961   :group 'gnus-summary-choose
962   :options '(gnus-agent-fetch-selected-article)
963   :type 'hook)
964
965 (defcustom gnus-visual-mark-article-hook
966   (list 'gnus-highlight-selected-summary)
967   "*Hook run after selecting an article in the summary buffer.
968 It is meant to be used for highlighting the article in some way.  It
969 is not run if `gnus-visual' is nil."
970   :group 'gnus-summary-visual
971   :type 'hook)
972
973 (defcustom gnus-parse-headers-hook nil
974   "*A hook called before parsing the headers."
975   :group 'gnus-various
976   :type 'hook)
977
978 (defcustom gnus-exit-group-hook nil
979   "*A hook called when exiting summary mode.
980 This hook is not called from the non-updating exit commands like `Q'."
981   :group 'gnus-various
982   :type 'hook)
983
984 (defcustom gnus-summary-update-hook
985   (list 'gnus-summary-highlight-line)
986   "*A hook called when a summary line is changed.
987 The hook will not be called if `gnus-visual' is nil.
988
989 The default function `gnus-summary-highlight-line' will
990 highlight the line according to the `gnus-summary-highlight'
991 variable."
992   :group 'gnus-summary-visual
993   :type 'hook)
994
995 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
996   "*A hook called when an article is selected for the first time.
997 The hook is intended to mark an article as read (or unread)
998 automatically when it is selected."
999   :group 'gnus-summary-choose
1000   :type 'hook)
1001
1002 (defcustom gnus-group-no-more-groups-hook nil
1003   "*A hook run when returning to group mode having no more (unread) groups."
1004   :group 'gnus-group-select
1005   :type 'hook)
1006
1007 (defcustom gnus-ps-print-hook nil
1008   "*A hook run before ps-printing something from Gnus."
1009   :group 'gnus-summary
1010   :type 'hook)
1011
1012 (defcustom gnus-summary-article-move-hook nil
1013   "*A hook called after an article is moved, copied, respooled, or crossposted."
1014   :version "22.1"
1015   :group 'gnus-summary
1016   :type 'hook)
1017
1018 (defcustom gnus-summary-article-delete-hook nil
1019   "*A hook called after an article is deleted."
1020   :version "22.1"
1021   :group 'gnus-summary
1022   :type 'hook)
1023
1024 (defcustom gnus-summary-article-expire-hook nil
1025   "*A hook called after an article is expired."
1026   :version "22.1"
1027   :group 'gnus-summary
1028   :type 'hook)
1029
1030 (defcustom gnus-summary-display-arrow
1031   (and (fboundp 'display-graphic-p)
1032        (display-graphic-p))
1033   "*If non-nil, display an arrow highlighting the current article."
1034   :version "22.1"
1035   :group 'gnus-summary
1036   :type 'boolean)
1037
1038 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1039   "Face used for highlighting the current article in the summary buffer."
1040   :group 'gnus-summary-visual
1041   :type 'face)
1042
1043 (defvar gnus-tmp-downloaded nil)
1044
1045 (defcustom gnus-summary-highlight
1046   '(((eq mark gnus-canceled-mark)
1047      . gnus-summary-cancelled)
1048     ((and uncached (> score default-high))
1049      . gnus-summary-high-undownloaded)
1050     ((and uncached (< score default-low))
1051      . gnus-summary-low-undownloaded)
1052     (uncached
1053      . gnus-summary-normal-undownloaded)
1054     ((and (> score default-high)
1055           (or (eq mark gnus-dormant-mark)
1056               (eq mark gnus-ticked-mark)))
1057      . gnus-summary-high-ticked)
1058     ((and (< score default-low)
1059           (or (eq mark gnus-dormant-mark)
1060               (eq mark gnus-ticked-mark)))
1061      . gnus-summary-low-ticked)
1062     ((or (eq mark gnus-dormant-mark)
1063          (eq mark gnus-ticked-mark))
1064      . gnus-summary-normal-ticked)
1065     ((and (> score default-high) (eq mark gnus-ancient-mark))
1066      . gnus-summary-high-ancient)
1067     ((and (< score default-low) (eq mark gnus-ancient-mark))
1068      . gnus-summary-low-ancient)
1069     ((eq mark gnus-ancient-mark)
1070      . gnus-summary-normal-ancient)
1071     ((and (> score default-high) (eq mark gnus-unread-mark))
1072      . gnus-summary-high-unread)
1073     ((and (< score default-low) (eq mark gnus-unread-mark))
1074      . gnus-summary-low-unread)
1075     ((eq mark gnus-unread-mark)
1076      . gnus-summary-normal-unread)
1077     ((> score default-high)
1078      . gnus-summary-high-read)
1079     ((< score default-low)
1080      . gnus-summary-low-read)
1081     (t
1082      . gnus-summary-normal-read))
1083   "*Controls the highlighting of summary buffer lines.
1084
1085 A list of (FORM . FACE) pairs.  When deciding how a particular
1086 summary line should be displayed, each form is evaluated.  The content
1087 of the face field after the first true form is used.  You can change
1088 how those summary lines are displayed, by editing the face field.
1089
1090 You can use the following variables in the FORM field.
1091
1092 score:        The article's score.
1093 default:      The default article score.
1094 default-high: The default score for high scored articles.
1095 default-low:  The default score for low scored articles.
1096 below:        The score below which articles are automatically marked as read.
1097 mark:         The article's mark.
1098 uncached:     Non-nil if the article is uncached."
1099   :group 'gnus-summary-visual
1100   :type '(repeat (cons (sexp :tag "Form" nil)
1101                        face)))
1102 (put 'gnus-summary-highlight 'risky-local-variable t)
1103
1104 (defcustom gnus-alter-header-function nil
1105   "Function called to allow alteration of article header structures.
1106 The function is called with one parameter, the article header vector,
1107 which it may alter in any way."
1108   :type '(choice (const :tag "None" nil)
1109                  function)
1110   :group 'gnus-summary)
1111
1112 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1113   "Function used to decode a string with encoded words.")
1114
1115 (defvar gnus-decode-encoded-address-function
1116   'mail-decode-encoded-address-string
1117   "Function used to decode addresses with encoded words.")
1118
1119 (defcustom gnus-extra-headers '(To Newsgroups)
1120   "*Extra headers to parse."
1121   :version "21.1"
1122   :group 'gnus-summary
1123   :type '(repeat symbol))
1124
1125 (defcustom gnus-ignored-from-addresses
1126   (and user-mail-address
1127        (not (string= user-mail-address ""))
1128        (regexp-quote user-mail-address))
1129   "*From headers that may be suppressed in favor of To headers.
1130 This can be a regexp or a list of regexps."
1131   :version "21.1"
1132   :group 'gnus-summary
1133   :type '(choice regexp
1134                  (repeat :tag "Regexp List" regexp)))
1135
1136 (defsubst gnus-ignored-from-addresses ()
1137   (gmm-regexp-concat gnus-ignored-from-addresses))
1138
1139 (defcustom gnus-summary-to-prefix "-> "
1140   "*String prefixed to the To field in the summary line when
1141 using `gnus-ignored-from-addresses'."
1142   :version "22.1"
1143   :group 'gnus-summary
1144   :type 'string)
1145
1146 (defcustom gnus-summary-newsgroup-prefix "=> "
1147   "*String prefixed to the Newsgroup field in the summary
1148 line when using `gnus-ignored-from-addresses'."
1149   :version "22.1"
1150   :group 'gnus-summary
1151   :type 'string)
1152
1153 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1154   "List of charsets that should be ignored.
1155 When these charsets are used in the \"charset\" parameter, the
1156 default charset will be used instead."
1157   :version "21.1"
1158   :type '(repeat symbol)
1159   :group 'gnus-charset)
1160
1161 (defcustom gnus-newsgroup-maximum-articles nil
1162   "The maximum number of articles a newsgroup.
1163 If this is a number, old articles in a newsgroup exceeding this number
1164 are silently ignored.  If it is nil, no article is ignored.  Note that
1165 setting this variable to a number might prevent you from reading very
1166 old articles."
1167   :group 'gnus-group-select
1168   :version "22.2"
1169   :type '(choice (const :tag "No limit" nil)
1170                  integer))
1171
1172 (gnus-define-group-parameter
1173  ignored-charsets
1174  :type list
1175  :function-document
1176  "Return the ignored charsets of GROUP."
1177  :variable gnus-group-ignored-charsets-alist
1178  :variable-default
1179  '(("alt\\.chinese\\.text" iso-8859-1))
1180  :variable-document
1181  "Alist of regexps (to match group names) and charsets that should be ignored.
1182 When these charsets are used in the \"charset\" parameter, the
1183 default charset will be used instead."
1184  :variable-group gnus-charset
1185  :variable-type '(repeat (cons (regexp :tag "Group")
1186                                (repeat symbol)))
1187  :parameter-type '(choice :tag "Ignored charsets"
1188                           :value nil
1189                           (repeat (symbol)))
1190  :parameter-document       "\
1191 List of charsets that should be ignored.
1192
1193 When these charsets are used in the \"charset\" parameter, the
1194 default charset will be used instead.")
1195
1196 (defcustom gnus-group-highlight-words-alist nil
1197   "Alist of group regexps and highlight regexps.
1198 This variable uses the same syntax as `gnus-emphasis-alist'."
1199   :version "21.1"
1200   :type '(repeat (cons (regexp :tag "Group")
1201                        (repeat (list (regexp :tag "Highlight regexp")
1202                                      (number :tag "Group for entire word" 0)
1203                                      (number :tag "Group for displayed part" 0)
1204                                      (symbol :tag "Face"
1205                                              gnus-emphasis-highlight-words)))))
1206   :group 'gnus-summary-visual)
1207
1208 (defcustom gnus-summary-show-article-charset-alist
1209   nil
1210   "Alist of number and charset.
1211 The article will be shown with the charset corresponding to the
1212 numbered argument.
1213 For example: ((1 . cn-gb-2312) (2 . big5))."
1214   :version "21.1"
1215   :type '(repeat (cons (number :tag "Argument" 1)
1216                        (symbol :tag "Charset")))
1217   :group 'gnus-charset)
1218
1219 (defcustom gnus-preserve-marks t
1220   "Whether marks are preserved when moving, copying and respooling messages."
1221   :version "21.1"
1222   :type 'boolean
1223   :group 'gnus-summary-marks)
1224
1225 (defcustom gnus-propagate-marks t
1226   "If non-nil, do not propagate marks to the backends."
1227   :version "23.1" ;; No Gnus
1228   :type 'boolean
1229   :group 'gnus-summary-marks)
1230
1231 (defcustom gnus-alter-articles-to-read-function nil
1232   "Function to be called to alter the list of articles to be selected."
1233   :type '(choice (const nil) function)
1234   :group 'gnus-summary)
1235
1236 (defcustom gnus-orphan-score nil
1237   "*All orphans get this score added.  Set in the score file."
1238   :group 'gnus-score-default
1239   :type '(choice (const nil)
1240                  integer))
1241
1242 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1243   "*A regexp to match MIME parts when saving multiple parts of a
1244 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1245 This regexp will be used by default when prompting the user for which
1246 type of files to save."
1247   :group 'gnus-summary
1248   :type 'regexp)
1249
1250 (defcustom gnus-read-all-available-headers nil
1251   "Whether Gnus should parse all headers made available to it.
1252 This is mostly relevant for slow back ends where the user may
1253 wish to widen the summary buffer to include all headers
1254 that were fetched.  Say, for nnultimate groups."
1255   :version "22.1"
1256   :group 'gnus-summary
1257   :type '(choice boolean regexp))
1258
1259 (defcustom gnus-summary-pipe-output-default-command nil
1260   "Command (and optional arguments) used to pipe article to subprocess.
1261 This will be used as the default command if it is non-nil.  The value
1262 will be updated if you modify it when executing the command
1263 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1264   :version "23.1" ;; No Gnus
1265   :group 'gnus-summary
1266   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1267
1268 (defcustom gnus-summary-muttprint-program "muttprint"
1269   "Command (and optional arguments) used to run Muttprint.
1270 The value will be updated if you modify it when executing the command
1271 `gnus-summary-muttprint'."
1272   :version "22.1"
1273   :group 'gnus-summary
1274   :type 'string)
1275
1276 (defcustom gnus-article-loose-mime t
1277   "If non-nil, don't require MIME-Version header.
1278 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1279 supply the MIME-Version header or deliberately strip it from the mail.
1280 If non-nil (the default), Gnus will treat some articles as MIME
1281 even if the MIME-Version header is missing."
1282   :version "22.1"
1283   :type 'boolean
1284   :group 'gnus-article-mime)
1285
1286 (defcustom gnus-article-emulate-mime t
1287   "If non-nil, use MIME emulation for uuencode and the like.
1288 This means that Gnus will search message bodies for text that look
1289 like uuencoded bits, yEncoded bits, and so on, and present that using
1290 the normal Gnus MIME machinery."
1291   :version "22.1"
1292   :type 'boolean
1293   :group 'gnus-article-mime)
1294
1295 ;;; Internal variables
1296
1297 (defvar gnus-summary-display-cache nil)
1298 (defvar gnus-article-mime-handles nil)
1299 (defvar gnus-article-decoded-p nil)
1300 (defvar gnus-article-charset nil)
1301 (defvar gnus-article-ignored-charsets nil)
1302 (defvar gnus-scores-exclude-files nil)
1303 (defvar gnus-page-broken nil)
1304
1305 (defvar gnus-original-article nil)
1306 (defvar gnus-article-internal-prepare-hook nil)
1307 (defvar gnus-newsgroup-process-stack nil)
1308
1309 (defvar gnus-thread-indent-array nil)
1310 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1311 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1312   "Function called to sort the articles within a thread after it has been gathered together.")
1313
1314 (defvar gnus-summary-save-parts-type-history nil)
1315 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1316
1317 ;; Avoid highlighting in kill files.
1318 (defvar gnus-summary-inhibit-highlight nil)
1319 (defvar gnus-newsgroup-selected-overlay nil)
1320 (defvar gnus-inhibit-limiting nil)
1321 (defvar gnus-newsgroup-adaptive-score-file nil)
1322 (defvar gnus-current-score-file nil)
1323 (defvar gnus-current-move-group nil)
1324 (defvar gnus-current-copy-group nil)
1325 (defvar gnus-current-crosspost-group nil)
1326 (defvar gnus-newsgroup-display nil)
1327
1328 (defvar gnus-newsgroup-dependencies nil)
1329 (defvar gnus-newsgroup-adaptive nil)
1330 (defvar gnus-summary-display-article-function nil)
1331 (defvar gnus-summary-highlight-line-function nil
1332   "Function called after highlighting a summary line.")
1333
1334 (defvar gnus-summary-line-format-alist
1335   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1336     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1337     (?s gnus-tmp-subject-or-nil ?s)
1338     (?n gnus-tmp-name ?s)
1339     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1340         ?s)
1341     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1342             gnus-tmp-from) ?s)
1343     (?F gnus-tmp-from ?s)
1344     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1345     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1346     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1347     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1348     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1349     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1350     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1351     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1352     (?L gnus-tmp-lines ?s)
1353     (?O gnus-tmp-downloaded ?c)
1354     (?I gnus-tmp-indentation ?s)
1355     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1356     (?R gnus-tmp-replied ?c)
1357     (?\[ gnus-tmp-opening-bracket ?c)
1358     (?\] gnus-tmp-closing-bracket ?c)
1359     (?\> (make-string gnus-tmp-level ? ) ?s)
1360     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1361     (?i gnus-tmp-score ?d)
1362     (?z gnus-tmp-score-char ?c)
1363     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1364     (?U gnus-tmp-unread ?c)
1365     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1366         ?s)
1367     (?t (gnus-summary-number-of-articles-in-thread
1368          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1369         ?d)
1370     (?e (gnus-summary-number-of-articles-in-thread
1371          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1372         ?c)
1373     (?u gnus-tmp-user-defined ?s)
1374     (?P (gnus-pick-line-number) ?d)
1375     (?B gnus-tmp-thread-tree-header-string ?s)
1376     (user-date (gnus-user-date
1377                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1378   "An alist of format specifications that can appear in summary lines.
1379 These are paired with what variables they correspond with, along with
1380 the type of the variable (string, integer, character, etc).")
1381
1382 (defvar gnus-summary-dummy-line-format-alist
1383   `((?S gnus-tmp-subject ?s)
1384     (?N gnus-tmp-number ?d)
1385     (?u gnus-tmp-user-defined ?s)))
1386
1387 (defvar gnus-summary-mode-line-format-alist
1388   `((?G gnus-tmp-group-name ?s)
1389     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1390     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1391     (?A gnus-tmp-article-number ?d)
1392     (?Z gnus-tmp-unread-and-unselected ?s)
1393     (?V gnus-version ?s)
1394     (?U gnus-tmp-unread-and-unticked ?d)
1395     (?S gnus-tmp-subject ?s)
1396     (?e gnus-tmp-unselected ?d)
1397     (?u gnus-tmp-user-defined ?s)
1398     (?d (length gnus-newsgroup-dormant) ?d)
1399     (?t (length gnus-newsgroup-marked) ?d)
1400     (?h (length gnus-newsgroup-spam-marked) ?d)
1401     (?r (length gnus-newsgroup-reads) ?d)
1402     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1403     (?E gnus-newsgroup-expunged-tally ?d)
1404     (?s (gnus-current-score-file-nondirectory) ?s)))
1405
1406 ;; This is here rather than in gnus-art for compilation reasons.
1407 (defvar gnus-article-mode-line-format-alist
1408   (nconc '((?w (gnus-article-wash-status) ?s)
1409            (?m (gnus-article-mime-part-status) ?s))
1410          gnus-summary-mode-line-format-alist))
1411
1412 (defvar gnus-last-search-regexp nil
1413   "Default regexp for article search command.")
1414
1415 (defvar gnus-last-shell-command nil
1416   "Default shell command on article.")
1417
1418 (defvar gnus-newsgroup-agentized nil
1419   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1420 (defvar gnus-newsgroup-begin nil)
1421 (defvar gnus-newsgroup-end nil)
1422 (defvar gnus-newsgroup-last-rmail nil)
1423 (defvar gnus-newsgroup-last-mail nil)
1424 (defvar gnus-newsgroup-last-folder nil)
1425 (defvar gnus-newsgroup-last-file nil)
1426 (defvar gnus-newsgroup-last-directory nil)
1427 (defvar gnus-newsgroup-auto-expire nil)
1428 (defvar gnus-newsgroup-active nil)
1429
1430 (defvar gnus-newsgroup-data nil)
1431 (defvar gnus-newsgroup-data-reverse nil)
1432 (defvar gnus-newsgroup-limit nil)
1433 (defvar gnus-newsgroup-limits nil)
1434 (defvar gnus-summary-use-undownloaded-faces nil)
1435
1436 (defvar gnus-newsgroup-unreads nil
1437   "Sorted list of unread articles in the current newsgroup.")
1438
1439 (defvar gnus-newsgroup-unselected nil
1440   "Sorted list of unselected unread articles in the current newsgroup.")
1441
1442 (defvar gnus-newsgroup-reads nil
1443   "Alist of read articles and article marks in the current newsgroup.")
1444
1445 (defvar gnus-newsgroup-expunged-tally nil)
1446
1447 (defvar gnus-newsgroup-marked nil
1448   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1449
1450 (defvar gnus-newsgroup-spam-marked nil
1451   "List of ranges of articles that have been marked as spam.")
1452
1453 (defvar gnus-newsgroup-killed nil
1454   "List of ranges of articles that have been through the scoring process.")
1455
1456 (defvar gnus-newsgroup-cached nil
1457   "Sorted list of articles that come from the article cache.")
1458
1459 (defvar gnus-newsgroup-saved nil
1460   "List of articles that have been saved.")
1461
1462 (defvar gnus-newsgroup-kill-headers nil)
1463
1464 (defvar gnus-newsgroup-replied nil
1465   "List of articles that have been replied to in the current newsgroup.")
1466
1467 (defvar gnus-newsgroup-forwarded nil
1468   "List of articles that have been forwarded in the current newsgroup.")
1469
1470 (defvar gnus-newsgroup-recent nil
1471   "List of articles that have are recent in the current newsgroup.")
1472
1473 (defvar gnus-newsgroup-expirable nil
1474   "Sorted list of articles in the current newsgroup that can be expired.")
1475
1476 (defvar gnus-newsgroup-processable nil
1477   "List of articles in the current newsgroup that can be processed.")
1478
1479 (defvar gnus-newsgroup-downloadable nil
1480   "Sorted list of articles in the current newsgroup that can be processed.")
1481
1482 (defvar gnus-newsgroup-unfetched nil
1483   "Sorted list of articles in the current newsgroup whose headers have
1484 not been fetched into the agent.
1485
1486 This list will always be a subset of gnus-newsgroup-undownloaded.")
1487
1488 (defvar gnus-newsgroup-undownloaded nil
1489   "List of articles in the current newsgroup that haven't been downloaded.")
1490
1491 (defvar gnus-newsgroup-unsendable nil
1492   "List of articles in the current newsgroup that won't be sent.")
1493
1494 (defvar gnus-newsgroup-bookmarks nil
1495   "List of articles in the current newsgroup that have bookmarks.")
1496
1497 (defvar gnus-newsgroup-dormant nil
1498   "Sorted list of dormant articles in the current newsgroup.")
1499
1500 (defvar gnus-newsgroup-unseen nil
1501   "List of unseen articles in the current newsgroup.")
1502
1503 (defvar gnus-newsgroup-seen nil
1504   "Range of seen articles in the current newsgroup.")
1505
1506 (defvar gnus-newsgroup-articles nil
1507   "List of articles in the current newsgroup.")
1508
1509 (defvar gnus-newsgroup-scored nil
1510   "List of scored articles in the current newsgroup.")
1511
1512 (defvar gnus-newsgroup-headers nil
1513   "List of article headers in the current newsgroup.")
1514
1515 (defvar gnus-newsgroup-threads nil)
1516
1517 (defvar gnus-newsgroup-prepared nil
1518   "Whether the current group has been prepared properly.")
1519
1520 (defvar gnus-newsgroup-ancient nil
1521   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1522
1523 (defvar gnus-newsgroup-sparse nil)
1524
1525 (defvar gnus-current-article nil)
1526 (defvar gnus-article-current nil)
1527 (defvar gnus-current-headers nil)
1528 (defvar gnus-have-all-headers nil)
1529 (defvar gnus-last-article nil)
1530 (defvar gnus-newsgroup-history nil)
1531 (defvar gnus-newsgroup-charset nil)
1532 (defvar gnus-newsgroup-ephemeral-charset nil)
1533 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1534
1535 (defvar gnus-article-before-search nil)
1536
1537 (defvar gnus-summary-local-variables
1538   '(gnus-newsgroup-name
1539     gnus-newsgroup-begin gnus-newsgroup-end
1540     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1541     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1542     gnus-newsgroup-last-directory
1543     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1544     gnus-newsgroup-unselected gnus-newsgroup-marked
1545     gnus-newsgroup-spam-marked
1546     gnus-newsgroup-reads gnus-newsgroup-saved
1547     gnus-newsgroup-replied gnus-newsgroup-forwarded
1548     gnus-newsgroup-recent
1549     gnus-newsgroup-expirable
1550     gnus-newsgroup-processable gnus-newsgroup-killed
1551     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1552     gnus-newsgroup-unfetched
1553     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1554     gnus-newsgroup-seen gnus-newsgroup-articles
1555     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1556     gnus-newsgroup-headers gnus-newsgroup-threads
1557     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1558     gnus-current-article gnus-current-headers gnus-have-all-headers
1559     gnus-last-article gnus-article-internal-prepare-hook
1560     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1561     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1562     gnus-thread-expunge-below
1563     gnus-score-alist gnus-current-score-file
1564     (gnus-summary-expunge-below . global)
1565     (gnus-summary-mark-below . global)
1566     (gnus-orphan-score . global)
1567     gnus-newsgroup-active gnus-scores-exclude-files
1568     gnus-newsgroup-history gnus-newsgroup-ancient
1569     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1570     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1571     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1572     (gnus-newsgroup-expunged-tally . 0)
1573     gnus-cache-removable-articles gnus-newsgroup-cached
1574     gnus-newsgroup-data gnus-newsgroup-data-reverse
1575     gnus-newsgroup-limit gnus-newsgroup-limits
1576     gnus-newsgroup-charset gnus-newsgroup-display
1577     gnus-summary-use-undownloaded-faces)
1578   "Variables that are buffer-local to the summary buffers.")
1579
1580 (defvar gnus-newsgroup-variables nil
1581   "A list of variables that have separate values in different newsgroups.
1582 A list of newsgroup (summary buffer) local variables, or cons of
1583 variables and their default expressions to be evalled (when the default
1584 values are not nil), that should be made global while the summary buffer
1585 is active.
1586
1587 Note: The default expressions will be evaluated (using function `eval')
1588 before assignment to the local variable rather than just assigned to it.
1589 If the default expression is the symbol `global', that symbol will not
1590 be evaluated but the global value of the local variable will be used
1591 instead.
1592
1593 These variables can be used to set variables in the group parameters
1594 while still allowing them to affect operations done in other buffers.
1595 For example:
1596
1597 \(setq gnus-newsgroup-variables
1598      '(message-use-followup-to
1599        (gnus-visible-headers .
1600          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1601 ")
1602
1603 (eval-when-compile
1604   ;; Bind features so that require will believe that gnus-sum has
1605   ;; already been loaded (avoids infinite recursion)
1606   (let ((features (cons 'gnus-sum features)))
1607     (require 'gnus-art)))
1608
1609 ;; MIME stuff.
1610
1611 (defvar gnus-decode-encoded-word-methods
1612   '(mail-decode-encoded-word-string)
1613   "List of methods used to decode encoded words.
1614
1615 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1616 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1617 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1618 whose names match REGEXP.
1619
1620 For example:
1621 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1622  mail-decode-encoded-word-string
1623  (\"chinese\" . rfc1843-decode-string))")
1624
1625 (defvar gnus-decode-encoded-word-methods-cache nil)
1626
1627 (defun gnus-multi-decode-encoded-word-string (string)
1628   "Apply the functions from `gnus-encoded-word-methods' that match."
1629   (unless (and gnus-decode-encoded-word-methods-cache
1630                (eq gnus-newsgroup-name
1631                    (car gnus-decode-encoded-word-methods-cache)))
1632     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1633     (dolist (method gnus-decode-encoded-word-methods)
1634       (if (symbolp method)
1635           (nconc gnus-decode-encoded-word-methods-cache (list method))
1636         (if (and gnus-newsgroup-name
1637                  (string-match (car method) gnus-newsgroup-name))
1638             (nconc gnus-decode-encoded-word-methods-cache
1639                    (list (cdr method)))))))
1640   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1641     (setq string (funcall method string))))
1642
1643 ;; Subject simplification.
1644
1645 (defun gnus-simplify-whitespace (str)
1646   "Remove excessive whitespace from STR."
1647   ;; Multiple spaces.
1648   (while (string-match "[ \t][ \t]+" str)
1649     (setq str (concat (substring str 0 (match-beginning 0))
1650                         " "
1651                         (substring str (match-end 0)))))
1652   ;; Leading spaces.
1653   (when (string-match "^[ \t]+" str)
1654     (setq str (substring str (match-end 0))))
1655   ;; Trailing spaces.
1656   (when (string-match "[ \t]+$" str)
1657     (setq str (substring str 0 (match-beginning 0))))
1658   str)
1659
1660 (defun gnus-simplify-all-whitespace (str)
1661   "Remove all whitespace from STR."
1662   (while (string-match "[ \t\n]+" str)
1663     (setq str (replace-match "" nil nil str)))
1664   str)
1665
1666 (defsubst gnus-simplify-subject-re (subject)
1667   "Remove \"Re:\" from subject lines."
1668   (if (string-match message-subject-re-regexp subject)
1669       (substring subject (match-end 0))
1670     subject))
1671
1672 (defun gnus-simplify-subject (subject &optional re-only)
1673   "Remove `Re:' and words in parentheses.
1674 If RE-ONLY is non-nil, strip leading `Re:'s only."
1675   (let ((case-fold-search t))           ;Ignore case.
1676     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1677     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1678       (setq subject (substring subject (match-end 0))))
1679     ;; Remove uninteresting prefixes.
1680     (when (and (not re-only)
1681                gnus-simplify-ignored-prefixes
1682                (string-match gnus-simplify-ignored-prefixes subject))
1683       (setq subject (substring subject (match-end 0))))
1684     ;; Remove words in parentheses from end.
1685     (unless re-only
1686       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1687         (setq subject (substring subject 0 (match-beginning 0)))))
1688     ;; Return subject string.
1689     subject))
1690
1691 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1692 ;; all whitespace.
1693 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1694   (goto-char (point-min))
1695   (while (re-search-forward regexp nil t)
1696     (replace-match (or newtext ""))))
1697
1698 (defun gnus-simplify-buffer-fuzzy ()
1699   "Simplify string in the buffer fuzzily.
1700 The string in the accessible portion of the current buffer is simplified.
1701 It is assumed to be a single-line subject.
1702 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1703 matter is removed.  Additional things can be deleted by setting
1704 `gnus-simplify-subject-fuzzy-regexp'."
1705   (let ((case-fold-search t)
1706         (modified-tick))
1707     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1708
1709     (while (not (eq modified-tick (buffer-modified-tick)))
1710       (setq modified-tick (buffer-modified-tick))
1711       (cond
1712        ((listp gnus-simplify-subject-fuzzy-regexp)
1713         (mapc 'gnus-simplify-buffer-fuzzy-step
1714               gnus-simplify-subject-fuzzy-regexp))
1715        (gnus-simplify-subject-fuzzy-regexp
1716         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1717       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1718       (gnus-simplify-buffer-fuzzy-step
1719        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1720       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1721
1722     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1723     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1724     (gnus-simplify-buffer-fuzzy-step " $")
1725     (gnus-simplify-buffer-fuzzy-step "^ +")))
1726
1727 (defun gnus-simplify-subject-fuzzy (subject)
1728   "Simplify a subject string fuzzily.
1729 See `gnus-simplify-buffer-fuzzy' for details."
1730   (save-excursion
1731     (gnus-set-work-buffer)
1732     (let ((case-fold-search t))
1733       ;; Remove uninteresting prefixes.
1734       (when (and gnus-simplify-ignored-prefixes
1735                  (string-match gnus-simplify-ignored-prefixes subject))
1736         (setq subject (substring subject (match-end 0))))
1737       (insert subject)
1738       (inline (gnus-simplify-buffer-fuzzy))
1739       (buffer-string))))
1740
1741 (defsubst gnus-simplify-subject-fully (subject)
1742   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1743   (cond
1744    (gnus-simplify-subject-functions
1745     (gnus-map-function gnus-simplify-subject-functions subject))
1746    ((null gnus-summary-gather-subject-limit)
1747     (gnus-simplify-subject-re subject))
1748    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1749     (gnus-simplify-subject-fuzzy subject))
1750    ((numberp gnus-summary-gather-subject-limit)
1751     (truncate-string-to-width (gnus-simplify-subject-re subject)
1752                               gnus-summary-gather-subject-limit))
1753    (t
1754     subject)))
1755
1756 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1757   "Check whether two subjects are equal.
1758 If optional argument SIMPLE-FIRST is t, first argument is already
1759 simplified."
1760   (cond
1761    ((null simple-first)
1762     (equal (gnus-simplify-subject-fully s1)
1763            (gnus-simplify-subject-fully s2)))
1764    (t
1765     (equal s1
1766            (gnus-simplify-subject-fully s2)))))
1767
1768 (defun gnus-summary-bubble-group ()
1769   "Increase the score of the current group.
1770 This is a handy function to add to `gnus-summary-exit-hook' to
1771 increase the score of each group you read."
1772   (gnus-group-add-score gnus-newsgroup-name))
1773
1774 \f
1775 ;;;
1776 ;;; Gnus summary mode
1777 ;;;
1778
1779 (put 'gnus-summary-mode 'mode-class 'special)
1780
1781 (defvar gnus-article-commands-menu)
1782
1783 ;; Non-orthogonal keys
1784
1785 (gnus-define-keys gnus-summary-mode-map
1786   " " gnus-summary-next-page
1787   "\177" gnus-summary-prev-page
1788   [delete] gnus-summary-prev-page
1789   [backspace] gnus-summary-prev-page
1790   "\r" gnus-summary-scroll-up
1791   "\M-\r" gnus-summary-scroll-down
1792   "n" gnus-summary-next-unread-article
1793   "p" gnus-summary-prev-unread-article
1794   "N" gnus-summary-next-article
1795   "P" gnus-summary-prev-article
1796   "\M-\C-n" gnus-summary-next-same-subject
1797   "\M-\C-p" gnus-summary-prev-same-subject
1798   "\M-n" gnus-summary-next-unread-subject
1799   "\M-p" gnus-summary-prev-unread-subject
1800   "." gnus-summary-first-unread-article
1801   "," gnus-summary-best-unread-article
1802   "\M-s" gnus-summary-search-article-forward
1803   "\M-r" gnus-summary-search-article-backward
1804   "\M-S" gnus-summary-repeat-search-article-forward
1805   "\M-R" gnus-summary-repeat-search-article-backward
1806   "<" gnus-summary-beginning-of-article
1807   ">" gnus-summary-end-of-article
1808   "j" gnus-summary-goto-article
1809   "^" gnus-summary-refer-parent-article
1810   "\M-^" gnus-summary-refer-article
1811   "u" gnus-summary-tick-article-forward
1812   "!" gnus-summary-tick-article-forward
1813   "U" gnus-summary-tick-article-backward
1814   "d" gnus-summary-mark-as-read-forward
1815   "D" gnus-summary-mark-as-read-backward
1816   "E" gnus-summary-mark-as-expirable
1817   "\M-u" gnus-summary-clear-mark-forward
1818   "\M-U" gnus-summary-clear-mark-backward
1819   "k" gnus-summary-kill-same-subject-and-select
1820   "\C-k" gnus-summary-kill-same-subject
1821   "\M-\C-k" gnus-summary-kill-thread
1822   "\M-\C-l" gnus-summary-lower-thread
1823   "e" gnus-summary-edit-article
1824   "#" gnus-summary-mark-as-processable
1825   "\M-#" gnus-summary-unmark-as-processable
1826   "\M-\C-t" gnus-summary-toggle-threads
1827   "\M-\C-s" gnus-summary-show-thread
1828   "\M-\C-h" gnus-summary-hide-thread
1829   "\M-\C-f" gnus-summary-next-thread
1830   "\M-\C-b" gnus-summary-prev-thread
1831   [(meta down)] gnus-summary-next-thread
1832   [(meta up)] gnus-summary-prev-thread
1833   "\M-\C-u" gnus-summary-up-thread
1834   "\M-\C-d" gnus-summary-down-thread
1835   "&" gnus-summary-execute-command
1836   "c" gnus-summary-catchup-and-exit
1837   "\C-w" gnus-summary-mark-region-as-read
1838   "\C-t" gnus-summary-toggle-truncation
1839   "?" gnus-summary-mark-as-dormant
1840   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1841   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1842   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1843   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1844   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1845   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1846   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1847   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1848   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1849   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1850   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1851   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1852   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1853   "=" gnus-summary-expand-window
1854   "\C-x\C-s" gnus-summary-reselect-current-group
1855   "\M-g" gnus-summary-rescan-group
1856   "w" gnus-summary-stop-page-breaking
1857   "\C-c\C-r" gnus-summary-caesar-message
1858   "f" gnus-summary-followup
1859   "F" gnus-summary-followup-with-original
1860   "C" gnus-summary-cancel-article
1861   "r" gnus-summary-reply
1862   "R" gnus-summary-reply-with-original
1863   "\C-c\C-f" gnus-summary-mail-forward
1864   "o" gnus-summary-save-article
1865   "\C-o" gnus-summary-save-article-mail
1866   "|" gnus-summary-pipe-output
1867   "\M-k" gnus-summary-edit-local-kill
1868   "\M-K" gnus-summary-edit-global-kill
1869   ;; "V" gnus-version
1870   "\C-c\C-d" gnus-summary-describe-group
1871   "q" gnus-summary-exit
1872   "Q" gnus-summary-exit-no-update
1873   "\C-c\C-i" gnus-info-find-node
1874   gnus-mouse-2 gnus-mouse-pick-article
1875   [follow-link] mouse-face
1876   "m" gnus-summary-mail-other-window
1877   "a" gnus-summary-post-news
1878   "i" gnus-summary-news-other-window
1879   "x" gnus-summary-limit-to-unread
1880   "s" gnus-summary-isearch-article
1881   "t" gnus-summary-toggle-header
1882   "g" gnus-summary-show-article
1883   "l" gnus-summary-goto-last-article
1884   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1885   "\C-d" gnus-summary-enter-digest-group
1886   "\M-\C-d" gnus-summary-read-document
1887   "\M-\C-e" gnus-summary-edit-parameters
1888   "\M-\C-a" gnus-summary-customize-parameters
1889   "\C-c\C-b" gnus-bug
1890   "*" gnus-cache-enter-article
1891   "\M-*" gnus-cache-remove-article
1892   "\M-&" gnus-summary-universal-argument
1893   "\C-l" gnus-recenter
1894   "I" gnus-summary-increase-score
1895   "L" gnus-summary-lower-score
1896   "\M-i" gnus-symbolic-argument
1897   "h" gnus-summary-select-article-buffer
1898
1899   "b" gnus-article-view-part
1900   "\M-t" gnus-summary-toggle-display-buttonized
1901
1902   "V" gnus-summary-score-map
1903   "X" gnus-uu-extract-map
1904   "S" gnus-summary-send-map)
1905
1906 ;; Sort of orthogonal keymap
1907 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1908   "t" gnus-summary-tick-article-forward
1909   "!" gnus-summary-tick-article-forward
1910   "d" gnus-summary-mark-as-read-forward
1911   "r" gnus-summary-mark-as-read-forward
1912   "c" gnus-summary-clear-mark-forward
1913   " " gnus-summary-clear-mark-forward
1914   "e" gnus-summary-mark-as-expirable
1915   "x" gnus-summary-mark-as-expirable
1916   "?" gnus-summary-mark-as-dormant
1917   "b" gnus-summary-set-bookmark
1918   "B" gnus-summary-remove-bookmark
1919   "#" gnus-summary-mark-as-processable
1920   "\M-#" gnus-summary-unmark-as-processable
1921   "S" gnus-summary-limit-include-expunged
1922   "C" gnus-summary-catchup
1923   "H" gnus-summary-catchup-to-here
1924   "h" gnus-summary-catchup-from-here
1925   "\C-c" gnus-summary-catchup-all
1926   "k" gnus-summary-kill-same-subject-and-select
1927   "K" gnus-summary-kill-same-subject
1928   "P" gnus-uu-mark-map)
1929
1930 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1931   "c" gnus-summary-clear-above
1932   "u" gnus-summary-tick-above
1933   "m" gnus-summary-mark-above
1934   "k" gnus-summary-kill-below)
1935
1936 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1937   "/" gnus-summary-limit-to-subject
1938   "n" gnus-summary-limit-to-articles
1939   "b" gnus-summary-limit-to-bodies
1940   "h" gnus-summary-limit-to-headers
1941   "w" gnus-summary-pop-limit
1942   "s" gnus-summary-limit-to-subject
1943   "a" gnus-summary-limit-to-author
1944   "u" gnus-summary-limit-to-unread
1945   "m" gnus-summary-limit-to-marks
1946   "M" gnus-summary-limit-exclude-marks
1947   "v" gnus-summary-limit-to-score
1948   "*" gnus-summary-limit-include-cached
1949   "D" gnus-summary-limit-include-dormant
1950   "T" gnus-summary-limit-include-thread
1951   "d" gnus-summary-limit-exclude-dormant
1952   "t" gnus-summary-limit-to-age
1953   "." gnus-summary-limit-to-unseen
1954   "x" gnus-summary-limit-to-extra
1955   "p" gnus-summary-limit-to-display-predicate
1956   "E" gnus-summary-limit-include-expunged
1957   "c" gnus-summary-limit-exclude-childless-dormant
1958   "C" gnus-summary-limit-mark-excluded-as-read
1959   "o" gnus-summary-insert-old-articles
1960   "N" gnus-summary-insert-new-articles
1961   "S" gnus-summary-limit-to-singletons
1962   "r" gnus-summary-limit-to-replied
1963   "R" gnus-summary-limit-to-recipient
1964   "A" gnus-summary-limit-to-address)
1965
1966 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1967   "n" gnus-summary-next-unread-article
1968   "p" gnus-summary-prev-unread-article
1969   "N" gnus-summary-next-article
1970   "P" gnus-summary-prev-article
1971   "\C-n" gnus-summary-next-same-subject
1972   "\C-p" gnus-summary-prev-same-subject
1973   "\M-n" gnus-summary-next-unread-subject
1974   "\M-p" gnus-summary-prev-unread-subject
1975   "f" gnus-summary-first-unread-article
1976   "b" gnus-summary-best-unread-article
1977   "j" gnus-summary-goto-article
1978   "g" gnus-summary-goto-subject
1979   "l" gnus-summary-goto-last-article
1980   "o" gnus-summary-pop-article)
1981
1982 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1983   "k" gnus-summary-kill-thread
1984   "E" gnus-summary-expire-thread
1985   "l" gnus-summary-lower-thread
1986   "i" gnus-summary-raise-thread
1987   "T" gnus-summary-toggle-threads
1988   "t" gnus-summary-rethread-current
1989   "^" gnus-summary-reparent-thread
1990   "\M-^" gnus-summary-reparent-children
1991   "s" gnus-summary-show-thread
1992   "S" gnus-summary-show-all-threads
1993   "h" gnus-summary-hide-thread
1994   "H" gnus-summary-hide-all-threads
1995   "n" gnus-summary-next-thread
1996   "p" gnus-summary-prev-thread
1997   "u" gnus-summary-up-thread
1998   "o" gnus-summary-top-thread
1999   "d" gnus-summary-down-thread
2000   "#" gnus-uu-mark-thread
2001   "\M-#" gnus-uu-unmark-thread)
2002
2003 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2004   "g" gnus-summary-prepare
2005   "c" gnus-summary-insert-cached-articles
2006   "d" gnus-summary-insert-dormant-articles
2007   "t" gnus-summary-insert-ticked-articles)
2008
2009 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2010   "c" gnus-summary-catchup-and-exit
2011   "C" gnus-summary-catchup-all-and-exit
2012   "E" gnus-summary-exit-no-update
2013   "Q" gnus-summary-exit
2014   "Z" gnus-summary-exit
2015   "n" gnus-summary-catchup-and-goto-next-group
2016   "p" gnus-summary-catchup-and-goto-prev-group
2017   "R" gnus-summary-reselect-current-group
2018   "G" gnus-summary-rescan-group
2019   "N" gnus-summary-next-group
2020   "s" gnus-summary-save-newsrc
2021   "P" gnus-summary-prev-group)
2022
2023 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2024   " " gnus-summary-next-page
2025   "n" gnus-summary-next-page
2026   "\177" gnus-summary-prev-page
2027   [delete] gnus-summary-prev-page
2028   "p" gnus-summary-prev-page
2029   "\r" gnus-summary-scroll-up
2030   "\M-\r" gnus-summary-scroll-down
2031   "<" gnus-summary-beginning-of-article
2032   ">" gnus-summary-end-of-article
2033   "b" gnus-summary-beginning-of-article
2034   "e" gnus-summary-end-of-article
2035   "^" gnus-summary-refer-parent-article
2036   "r" gnus-summary-refer-parent-article
2037   "D" gnus-summary-enter-digest-group
2038   "R" gnus-summary-refer-references
2039   "T" gnus-summary-refer-thread
2040   "g" gnus-summary-show-article
2041   "s" gnus-summary-isearch-article
2042   "P" gnus-summary-print-article
2043   "S" gnus-sticky-article
2044   "M" gnus-mailing-list-insinuate
2045   "t" gnus-article-babel)
2046
2047 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2048   "b" gnus-article-add-buttons
2049   "B" gnus-article-add-buttons-to-head
2050   "o" gnus-article-treat-overstrike
2051   "e" gnus-article-emphasize
2052   "w" gnus-article-fill-cited-article
2053   "Q" gnus-article-fill-long-lines
2054   "L" gnus-article-toggle-truncate-lines
2055   "C" gnus-article-capitalize-sentences
2056   "c" gnus-article-remove-cr
2057   "q" gnus-article-de-quoted-unreadable
2058   "6" gnus-article-de-base64-unreadable
2059   "Z" gnus-article-decode-HZ
2060   "A" gnus-article-treat-ansi-sequences
2061   "h" gnus-article-wash-html
2062   "u" gnus-article-unsplit-urls
2063   "s" gnus-summary-force-verify-and-decrypt
2064   "f" gnus-article-display-x-face
2065   "l" gnus-summary-stop-page-breaking
2066   "r" gnus-summary-caesar-message
2067   "m" gnus-summary-morse-message
2068   "t" gnus-summary-toggle-header
2069   "g" gnus-treat-smiley
2070   "v" gnus-summary-verbose-headers
2071   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2072   "p" gnus-article-verify-x-pgp-sig
2073   "d" gnus-article-treat-dumbquotes
2074   "i" gnus-summary-idna-message)
2075
2076 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2077   ;; mnemonic: deuglif*Y*
2078   "u" gnus-article-outlook-unwrap-lines
2079   "a" gnus-article-outlook-repair-attribution
2080   "c" gnus-article-outlook-rearrange-citation
2081   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2082
2083 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2084   "a" gnus-article-hide
2085   "h" gnus-article-hide-headers
2086   "b" gnus-article-hide-boring-headers
2087   "s" gnus-article-hide-signature
2088   "c" gnus-article-hide-citation
2089   "C" gnus-article-hide-citation-in-followups
2090   "l" gnus-article-hide-list-identifiers
2091   "B" gnus-article-strip-banner
2092   "P" gnus-article-hide-pem
2093   "\C-c" gnus-article-hide-citation-maybe)
2094
2095 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2096   "a" gnus-article-highlight
2097   "h" gnus-article-highlight-headers
2098   "c" gnus-article-highlight-citation
2099   "s" gnus-article-highlight-signature)
2100
2101 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2102   "f" gnus-article-treat-fold-headers
2103   "u" gnus-article-treat-unfold-headers
2104   "n" gnus-article-treat-fold-newsgroups)
2105
2106 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2107   "x" gnus-article-display-x-face
2108   "d" gnus-article-display-face
2109   "s" gnus-treat-smiley
2110   "D" gnus-article-remove-images
2111   "f" gnus-treat-from-picon
2112   "m" gnus-treat-mail-picon
2113   "n" gnus-treat-newsgroups-picon)
2114
2115 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2116   "w" gnus-article-decode-mime-words
2117   "c" gnus-article-decode-charset
2118   "v" gnus-mime-view-all-parts
2119   "b" gnus-article-view-part)
2120
2121 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2122   "z" gnus-article-date-ut
2123   "u" gnus-article-date-ut
2124   "l" gnus-article-date-local
2125   "p" gnus-article-date-english
2126   "e" gnus-article-date-lapsed
2127   "o" gnus-article-date-original
2128   "i" gnus-article-date-iso8601
2129   "s" gnus-article-date-user)
2130
2131 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2132   "t" gnus-article-remove-trailing-blank-lines
2133   "l" gnus-article-strip-leading-blank-lines
2134   "m" gnus-article-strip-multiple-blank-lines
2135   "a" gnus-article-strip-blank-lines
2136   "A" gnus-article-strip-all-blank-lines
2137   "s" gnus-article-strip-leading-space
2138   "e" gnus-article-strip-trailing-space
2139   "w" gnus-article-remove-leading-whitespace)
2140
2141 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2142   "v" gnus-version
2143   "f" gnus-summary-fetch-faq
2144   "d" gnus-summary-describe-group
2145   "h" gnus-summary-describe-briefly
2146   "i" gnus-info-find-node
2147   "c" gnus-group-fetch-charter
2148   "C" gnus-group-fetch-control)
2149
2150 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2151   "e" gnus-summary-expire-articles
2152   "\M-\C-e" gnus-summary-expire-articles-now
2153   "\177" gnus-summary-delete-article
2154   [delete] gnus-summary-delete-article
2155   [backspace] gnus-summary-delete-article
2156   "m" gnus-summary-move-article
2157   "r" gnus-summary-respool-article
2158   "w" gnus-summary-edit-article
2159   "c" gnus-summary-copy-article
2160   "B" gnus-summary-crosspost-article
2161   "q" gnus-summary-respool-query
2162   "t" gnus-summary-respool-trace
2163   "i" gnus-summary-import-article
2164   "I" gnus-summary-create-article
2165   "p" gnus-summary-article-posted-p)
2166
2167 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2168   "o" gnus-summary-save-article
2169   "m" gnus-summary-save-article-mail
2170   "F" gnus-summary-write-article-file
2171   "r" gnus-summary-save-article-rmail
2172   "f" gnus-summary-save-article-file
2173   "b" gnus-summary-save-article-body-file
2174   "B" gnus-summary-write-article-body-file
2175   "h" gnus-summary-save-article-folder
2176   "v" gnus-summary-save-article-vm
2177   "p" gnus-summary-pipe-output
2178   "P" gnus-summary-muttprint
2179   "s" gnus-soup-add-article)
2180
2181 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2182   "b" gnus-summary-display-buttonized
2183   "m" gnus-summary-repair-multipart
2184   "v" gnus-article-view-part
2185   "o" gnus-article-save-part
2186   "O" gnus-article-save-part-and-strip
2187   "r" gnus-article-replace-part
2188   "d" gnus-article-delete-part
2189   "t" gnus-article-view-part-as-type
2190   "j" gnus-article-jump-to-part
2191   "c" gnus-article-copy-part
2192   "C" gnus-article-view-part-as-charset
2193   "e" gnus-article-view-part-externally
2194   "H" gnus-article-browse-html-article
2195   "E" gnus-article-encrypt-body
2196   "i" gnus-article-inline-part
2197   "|" gnus-article-pipe-part)
2198
2199 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2200   "p" gnus-summary-mark-as-processable
2201   "u" gnus-summary-unmark-as-processable
2202   "U" gnus-summary-unmark-all-processable
2203   "v" gnus-uu-mark-over
2204   "s" gnus-uu-mark-series
2205   "r" gnus-uu-mark-region
2206   "g" gnus-uu-unmark-region
2207   "R" gnus-uu-mark-by-regexp
2208   "G" gnus-uu-unmark-by-regexp
2209   "t" gnus-uu-mark-thread
2210   "T" gnus-uu-unmark-thread
2211   "a" gnus-uu-mark-all
2212   "b" gnus-uu-mark-buffer
2213   "S" gnus-uu-mark-sparse
2214   "k" gnus-summary-kill-process-mark
2215   "y" gnus-summary-yank-process-mark
2216   "w" gnus-summary-save-process-mark
2217   "i" gnus-uu-invert-processable)
2218
2219 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2220   ;;"x" gnus-uu-extract-any
2221   "m" gnus-summary-save-parts
2222   "u" gnus-uu-decode-uu
2223   "U" gnus-uu-decode-uu-and-save
2224   "s" gnus-uu-decode-unshar
2225   "S" gnus-uu-decode-unshar-and-save
2226   "o" gnus-uu-decode-save
2227   "O" gnus-uu-decode-save
2228   "b" gnus-uu-decode-binhex
2229   "B" gnus-uu-decode-binhex
2230   "Y" gnus-uu-decode-yenc
2231   "p" gnus-uu-decode-postscript
2232   "P" gnus-uu-decode-postscript-and-save)
2233
2234 (gnus-define-keys
2235     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2236   "u" gnus-uu-decode-uu-view
2237   "U" gnus-uu-decode-uu-and-save-view
2238   "s" gnus-uu-decode-unshar-view
2239   "S" gnus-uu-decode-unshar-and-save-view
2240   "o" gnus-uu-decode-save-view
2241   "O" gnus-uu-decode-save-view
2242   "b" gnus-uu-decode-binhex-view
2243   "B" gnus-uu-decode-binhex-view
2244   "p" gnus-uu-decode-postscript-view
2245   "P" gnus-uu-decode-postscript-and-save-view)
2246
2247 (defvar gnus-article-post-menu nil)
2248
2249 (defconst gnus-summary-menu-maxlen 20)
2250
2251 (defun gnus-summary-menu-split (menu)
2252   ;; If we have lots of elements, divide them into groups of 20
2253   ;; and make a pane (or submenu) for each one.
2254   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2255       (let ((menu menu) sublists next
2256             (i 1))
2257         (while menu
2258           ;; Pull off the next gnus-summary-menu-maxlen elements
2259           ;; and make them the next element of sublist.
2260           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2261           (if next
2262               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2263                       nil))
2264           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2265                                              (aref (car (last menu)) 0)) menu)
2266                                sublists))
2267           (setq i (1+ i))
2268           (setq menu next))
2269         (nreverse sublists))
2270     ;; Few elements--put them all in one pane.
2271     menu))
2272
2273 (defun gnus-summary-make-menu-bar ()
2274   (gnus-turn-off-edit-menu 'summary)
2275
2276   (unless (boundp 'gnus-summary-misc-menu)
2277
2278     (easy-menu-define
2279       gnus-summary-kill-menu gnus-summary-mode-map ""
2280       (cons
2281        "Score"
2282        (nconc
2283         (list
2284          ["Customize" gnus-score-customize t])
2285         (gnus-make-score-map 'increase)
2286         (gnus-make-score-map 'lower)
2287         '(("Mark"
2288            ["Kill below" gnus-summary-kill-below t]
2289            ["Mark above" gnus-summary-mark-above t]
2290            ["Tick above" gnus-summary-tick-above t]
2291            ["Clear above" gnus-summary-clear-above t])
2292           ["Current score" gnus-summary-current-score t]
2293           ["Set score" gnus-summary-set-score t]
2294           ["Switch current score file..." gnus-score-change-score-file t]
2295           ["Set mark below..." gnus-score-set-mark-below t]
2296           ["Set expunge below..." gnus-score-set-expunge-below t]
2297           ["Edit current score file" gnus-score-edit-current-scores t]
2298           ["Edit score file..." gnus-score-edit-file t]
2299           ["Trace score" gnus-score-find-trace t]
2300           ["Find words" gnus-score-find-favourite-words t]
2301           ["Rescore buffer" gnus-summary-rescore t]
2302           ["Increase score..." gnus-summary-increase-score t]
2303           ["Lower score..." gnus-summary-lower-score t]))))
2304
2305     ;; Define both the Article menu in the summary buffer and the
2306     ;; equivalent Commands menu in the article buffer here for
2307     ;; consistency.
2308     (let ((innards
2309            `(("Hide"
2310               ["All" gnus-article-hide t]
2311               ["Headers" gnus-article-hide-headers t]
2312               ["Signature" gnus-article-hide-signature t]
2313               ["Citation" gnus-article-hide-citation t]
2314               ["List identifiers" gnus-article-hide-list-identifiers t]
2315               ["Banner" gnus-article-strip-banner t]
2316               ["Boring headers" gnus-article-hide-boring-headers t])
2317              ("Highlight"
2318               ["All" gnus-article-highlight t]
2319               ["Headers" gnus-article-highlight-headers t]
2320               ["Signature" gnus-article-highlight-signature t]
2321               ["Citation" gnus-article-highlight-citation t])
2322              ("MIME"
2323               ["Words" gnus-article-decode-mime-words t]
2324               ["Charset" gnus-article-decode-charset t]
2325               ["QP" gnus-article-de-quoted-unreadable t]
2326               ["Base64" gnus-article-de-base64-unreadable t]
2327               ["View MIME buttons" gnus-summary-display-buttonized t]
2328               ["View all" gnus-mime-view-all-parts t]
2329               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2330               ["Encrypt body" gnus-article-encrypt-body
2331                :active (not (gnus-group-read-only-p))
2332                ,@(if (featurep 'xemacs) nil
2333                    '(:help "Encrypt the message body on disk"))]
2334               ["Extract all parts..." gnus-summary-save-parts t]
2335               ("Multipart"
2336                ["Repair multipart" gnus-summary-repair-multipart t]
2337                ["Pipe part..." gnus-article-pipe-part t]
2338                ["Inline part" gnus-article-inline-part t]
2339                ["View part as type..." gnus-article-view-part-as-type t]
2340                ["Encrypt body" gnus-article-encrypt-body
2341                 :active (not (gnus-group-read-only-p))
2342                ,@(if (featurep 'xemacs) nil
2343                    '(:help "Encrypt the message body on disk"))]
2344                ["View part externally" gnus-article-view-part-externally t]
2345                ["View HTML parts in browser" gnus-article-browse-html-article t]
2346                ["View part with charset..." gnus-article-view-part-as-charset t]
2347                ["Copy part" gnus-article-copy-part t]
2348                ["Save part..." gnus-article-save-part t]
2349                ["View part" gnus-article-view-part t]))
2350              ("Date"
2351               ["Local" gnus-article-date-local t]
2352               ["ISO8601" gnus-article-date-iso8601 t]
2353               ["UT" gnus-article-date-ut t]
2354               ["Original" gnus-article-date-original t]
2355               ["Lapsed" gnus-article-date-lapsed t]
2356               ["User-defined" gnus-article-date-user t])
2357              ("Display"
2358               ["Remove images" gnus-article-remove-images t]
2359               ["Toggle smiley" gnus-treat-smiley t]
2360               ["Show X-Face" gnus-article-display-x-face t]
2361               ["Show picons in From" gnus-treat-from-picon t]
2362               ["Show picons in mail headers" gnus-treat-mail-picon t]
2363               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2364               ("View as different encoding"
2365                ,@(gnus-summary-menu-split
2366                   (mapcar
2367                    (lambda (cs)
2368                      ;; Since easymenu under Emacs doesn't allow
2369                      ;; lambda forms for menu commands, we should
2370                      ;; provide intern'ed function symbols.
2371                      (let ((command (intern (format "\
2372 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2373                        (fset command
2374                              `(lambda ()
2375                                 (interactive)
2376                                 (let ((gnus-summary-show-article-charset-alist
2377                                        '((1 . ,cs))))
2378                                   (gnus-summary-show-article 1))))
2379                        `[,(symbol-name cs) ,command t]))
2380                    (sort (if (fboundp 'coding-system-list)
2381                              (coding-system-list)
2382                            (mapcar 'car mm-mime-mule-charset-alist))
2383                          'string<)))))
2384              ("Washing"
2385               ("Remove Blanks"
2386                ["Leading" gnus-article-strip-leading-blank-lines t]
2387                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2388                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2389                ["All of the above" gnus-article-strip-blank-lines t]
2390                ["All" gnus-article-strip-all-blank-lines t]
2391                ["Leading space" gnus-article-strip-leading-space t]
2392                ["Trailing space" gnus-article-strip-trailing-space t]
2393                ["Leading space in headers"
2394                 gnus-article-remove-leading-whitespace t])
2395               ["Overstrike" gnus-article-treat-overstrike t]
2396               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2397               ["Emphasis" gnus-article-emphasize t]
2398               ["Word wrap" gnus-article-fill-cited-article t]
2399               ["Fill long lines" gnus-article-fill-long-lines t]
2400               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2401               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2402               ["Remove CR" gnus-article-remove-cr t]
2403               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2404               ["Base64" gnus-article-de-base64-unreadable t]
2405               ["Rot 13" gnus-summary-caesar-message
2406                ,@(if (featurep 'xemacs) '(t)
2407                    '(:help "\"Caesar rotate\" article by 13"))]
2408               ["De-IDNA" gnus-summary-idna-message t]
2409               ["Morse decode" gnus-summary-morse-message t]
2410               ["Unix pipe..." gnus-summary-pipe-message t]
2411               ["Add buttons" gnus-article-add-buttons t]
2412               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2413               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2414               ["Verbose header" gnus-summary-verbose-headers t]
2415               ["Toggle header" gnus-summary-toggle-header t]
2416               ["Unfold headers" gnus-article-treat-unfold-headers t]
2417               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2418               ["Html" gnus-article-wash-html t]
2419               ["Unsplit URLs" gnus-article-unsplit-urls t]
2420               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2421               ["Decode HZ" gnus-article-decode-HZ t]
2422               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2423               ("(Outlook) Deuglify"
2424                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2425                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2426                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2427                ["Full (Outlook) deuglify"
2428                 gnus-article-outlook-deuglify-article t])
2429               )
2430              ("Output"
2431               ["Save in default format..." gnus-summary-save-article
2432                ,@(if (featurep 'xemacs) '(t)
2433                    '(:help "Save article using default method"))]
2434               ["Save in file..." gnus-summary-save-article-file
2435                ,@(if (featurep 'xemacs) '(t)
2436                    '(:help "Save article in file"))]
2437               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2438               ["Save in MH folder..." gnus-summary-save-article-folder t]
2439               ["Save in VM folder..." gnus-summary-save-article-vm t]
2440               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2441               ["Save body in file..." gnus-summary-save-article-body-file t]
2442               ["Pipe through a filter..." gnus-summary-pipe-output t]
2443               ["Add to SOUP packet" gnus-soup-add-article t]
2444               ["Print with Muttprint..." gnus-summary-muttprint t]
2445               ["Print" gnus-summary-print-article
2446                ,@(if (featurep 'xemacs) '(t)
2447                    '(:help "Generate and print a PostScript image"))])
2448              ("Copy, move,... (Backend)"
2449               ,@(if (featurep 'xemacs) nil
2450                   '(:help "Copying, moving, expiring articles..."))
2451               ["Respool article..." gnus-summary-respool-article t]
2452               ["Move article..." gnus-summary-move-article
2453                (gnus-check-backend-function
2454                 'request-move-article gnus-newsgroup-name)]
2455               ["Copy article..." gnus-summary-copy-article t]
2456               ["Crosspost article..." gnus-summary-crosspost-article
2457                (gnus-check-backend-function
2458                 'request-replace-article gnus-newsgroup-name)]
2459               ["Import file..." gnus-summary-import-article
2460                (gnus-check-backend-function
2461                 'request-accept-article gnus-newsgroup-name)]
2462               ["Create article..." gnus-summary-create-article
2463                (gnus-check-backend-function
2464                 'request-accept-article gnus-newsgroup-name)]
2465               ["Check if posted" gnus-summary-article-posted-p t]
2466               ["Edit article" gnus-summary-edit-article
2467                (not (gnus-group-read-only-p))]
2468               ["Delete article" gnus-summary-delete-article
2469                (gnus-check-backend-function
2470                 'request-expire-articles gnus-newsgroup-name)]
2471               ["Query respool" gnus-summary-respool-query t]
2472               ["Trace respool" gnus-summary-respool-trace t]
2473               ["Delete expirable articles" gnus-summary-expire-articles-now
2474                (gnus-check-backend-function
2475                 'request-expire-articles gnus-newsgroup-name)])
2476              ("Extract"
2477               ["Uudecode" gnus-uu-decode-uu
2478                ,@(if (featurep 'xemacs) '(t)
2479                    '(:help "Decode uuencoded article(s)"))]
2480               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2481               ["Unshar" gnus-uu-decode-unshar t]
2482               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2483               ["Save" gnus-uu-decode-save t]
2484               ["Binhex" gnus-uu-decode-binhex t]
2485               ["Postscript" gnus-uu-decode-postscript t]
2486               ["All MIME parts" gnus-summary-save-parts t])
2487              ("Cache"
2488               ["Enter article" gnus-cache-enter-article t]
2489               ["Remove article" gnus-cache-remove-article t])
2490              ["Translate" gnus-article-babel t]
2491              ["Select article buffer" gnus-summary-select-article-buffer t]
2492              ["Make article buffer sticky" gnus-sticky-article t]
2493              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2494              ["Isearch article..." gnus-summary-isearch-article t]
2495              ["Beginning of the article" gnus-summary-beginning-of-article t]
2496              ["End of the article" gnus-summary-end-of-article t]
2497              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2498              ["Fetch referenced articles" gnus-summary-refer-references t]
2499              ["Fetch current thread" gnus-summary-refer-thread t]
2500              ["Fetch article with id..." gnus-summary-refer-article t]
2501              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2502              ["Redisplay" gnus-summary-show-article t]
2503              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2504       (easy-menu-define
2505         gnus-summary-article-menu gnus-summary-mode-map ""
2506         (cons "Article" innards))
2507
2508       (if (not (keymapp gnus-summary-article-menu))
2509           (easy-menu-define
2510             gnus-article-commands-menu gnus-article-mode-map ""
2511             (cons "Commands" innards))
2512         ;; in Emacs, don't share menu.
2513         (setq gnus-article-commands-menu
2514               (copy-keymap gnus-summary-article-menu))
2515         (define-key gnus-article-mode-map [menu-bar commands]
2516           (cons "Commands" gnus-article-commands-menu))))
2517
2518     (easy-menu-define
2519       gnus-summary-thread-menu gnus-summary-mode-map ""
2520       '("Threads"
2521         ["Find all messages in thread" gnus-summary-refer-thread t]
2522         ["Toggle threading" gnus-summary-toggle-threads t]
2523         ["Hide threads" gnus-summary-hide-all-threads t]
2524         ["Show threads" gnus-summary-show-all-threads t]
2525         ["Hide thread" gnus-summary-hide-thread t]
2526         ["Show thread" gnus-summary-show-thread t]
2527         ["Go to next thread" gnus-summary-next-thread t]
2528         ["Go to previous thread" gnus-summary-prev-thread t]
2529         ["Go down thread" gnus-summary-down-thread t]
2530         ["Go up thread" gnus-summary-up-thread t]
2531         ["Top of thread" gnus-summary-top-thread t]
2532         ["Mark thread as read" gnus-summary-kill-thread t]
2533         ["Mark thread as expired" gnus-summary-expire-thread t]
2534         ["Lower thread score" gnus-summary-lower-thread t]
2535         ["Raise thread score" gnus-summary-raise-thread t]
2536         ["Rethread current" gnus-summary-rethread-current t]))
2537
2538     (easy-menu-define
2539       gnus-summary-post-menu gnus-summary-mode-map ""
2540       `("Post"
2541         ["Send a message (mail or news)" gnus-summary-post-news
2542          ,@(if (featurep 'xemacs) '(t)
2543              '(:help "Compose a new message (mail or news)"))]
2544         ["Followup" gnus-summary-followup
2545          ,@(if (featurep 'xemacs) '(t)
2546              '(:help "Post followup to this article"))]
2547         ["Followup and yank" gnus-summary-followup-with-original
2548          ,@(if (featurep 'xemacs) '(t)
2549              '(:help "Post followup to this article, quoting its contents"))]
2550         ["Supersede article" gnus-summary-supersede-article t]
2551         ["Cancel article" gnus-summary-cancel-article
2552          ,@(if (featurep 'xemacs) '(t)
2553              '(:help "Cancel an article you posted"))]
2554         ["Reply" gnus-summary-reply t]
2555         ["Reply and yank" gnus-summary-reply-with-original t]
2556         ["Wide reply" gnus-summary-wide-reply t]
2557         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2558          ,@(if (featurep 'xemacs) '(t)
2559              '(:help "Mail a reply, quoting this article"))]
2560         ["Very wide reply" gnus-summary-very-wide-reply t]
2561         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2562          ,@(if (featurep 'xemacs) '(t)
2563              '(:help "Mail a very wide reply, quoting this article"))]
2564         ["Mail forward" gnus-summary-mail-forward t]
2565         ["Post forward" gnus-summary-post-forward t]
2566         ["Digest and mail" gnus-uu-digest-mail-forward t]
2567         ["Digest and post" gnus-uu-digest-post-forward t]
2568         ["Resend message" gnus-summary-resend-message t]
2569         ["Resend message edit" gnus-summary-resend-message-edit t]
2570         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2571         ["Send a mail" gnus-summary-mail-other-window t]
2572         ["Create a local message" gnus-summary-news-other-window t]
2573         ["Uuencode and post" gnus-uu-post-news
2574          ,@(if (featurep 'xemacs) '(t)
2575              '(:help "Post a uuencoded article"))]
2576         ["Followup via news" gnus-summary-followup-to-mail t]
2577         ["Followup via news and yank"
2578          gnus-summary-followup-to-mail-with-original t]
2579         ["Strip signature on reply"
2580          (lambda ()
2581            (interactive)
2582            (if (not (memq message-cite-function
2583                           '(message-cite-original-without-signature
2584                             message-cite-original)))
2585                ;; Stupid workaround for XEmacs not honoring :visible.
2586                (message "Can't toggle this value of `message-cite-function'")
2587              (setq message-cite-function
2588                    (if (eq message-cite-function
2589                            'message-cite-original-without-signature)
2590                        'message-cite-original
2591                      'message-cite-original-without-signature))))
2592          ;; XEmacs barfs on :visible.
2593          ,@(if (featurep 'xemacs) nil
2594              '(:visible (memq message-cite-function
2595                               '(message-cite-original-without-signature
2596                                 message-cite-original))))
2597          :style toggle
2598          :selected (eq message-cite-function
2599                        'message-cite-original-without-signature)
2600          ,@(if (featurep 'xemacs) nil
2601              '(:help "Strip signature from cited article when replying."))]
2602         ;;("Draft"
2603         ;;["Send" gnus-summary-send-draft t]
2604         ;;["Send bounced" gnus-resend-bounced-mail t])
2605         ))
2606
2607     (cond
2608      ((not (keymapp gnus-summary-post-menu))
2609       (setq gnus-article-post-menu gnus-summary-post-menu))
2610      ((not gnus-article-post-menu)
2611       ;; Don't share post menu.
2612       (setq gnus-article-post-menu
2613             (copy-keymap gnus-summary-post-menu))))
2614     (define-key gnus-article-mode-map [menu-bar post]
2615       (cons "Post" gnus-article-post-menu))
2616
2617     (easy-menu-define
2618       gnus-summary-misc-menu gnus-summary-mode-map ""
2619       `("Gnus"
2620         ("Mark Read"
2621          ["Mark as read" gnus-summary-mark-as-read-forward t]
2622          ["Mark same subject and select"
2623           gnus-summary-kill-same-subject-and-select t]
2624          ["Mark same subject" gnus-summary-kill-same-subject t]
2625          ["Catchup" gnus-summary-catchup
2626           ,@(if (featurep 'xemacs) '(t)
2627               '(:help "Mark unread articles in this group as read"))]
2628          ["Catchup all" gnus-summary-catchup-all t]
2629          ["Catchup to here" gnus-summary-catchup-to-here t]
2630          ["Catchup from here" gnus-summary-catchup-from-here t]
2631          ["Catchup region" gnus-summary-mark-region-as-read
2632           (gnus-mark-active-p)]
2633          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2634         ("Mark Various"
2635          ["Tick" gnus-summary-tick-article-forward t]
2636          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2637          ["Remove marks" gnus-summary-clear-mark-forward t]
2638          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2639          ["Set bookmark" gnus-summary-set-bookmark t]
2640          ["Remove bookmark" gnus-summary-remove-bookmark t])
2641         ("Limit to"
2642          ["Marks..." gnus-summary-limit-to-marks t]
2643          ["Subject..." gnus-summary-limit-to-subject t]
2644          ["Author..." gnus-summary-limit-to-author t]
2645          ["Recipient..." gnus-summary-limit-to-recipient t]
2646          ["Address..." gnus-summary-limit-to-address t]
2647          ["Age..." gnus-summary-limit-to-age t]
2648          ["Extra..." gnus-summary-limit-to-extra t]
2649          ["Score..." gnus-summary-limit-to-score t]
2650          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2651          ["Unread" gnus-summary-limit-to-unread t]
2652          ["Unseen" gnus-summary-limit-to-unseen t]
2653          ["Singletons" gnus-summary-limit-to-singletons t]
2654          ["Replied" gnus-summary-limit-to-replied t]
2655          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2656          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2657          ["Pop limit" gnus-summary-pop-limit t]
2658          ["Show dormant" gnus-summary-limit-include-dormant t]
2659          ["Hide childless dormant"
2660           gnus-summary-limit-exclude-childless-dormant t]
2661          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2662          ["Hide marked" gnus-summary-limit-exclude-marks t]
2663          ["Show expunged" gnus-summary-limit-include-expunged t])
2664         ("Process Mark"
2665          ["Set mark" gnus-summary-mark-as-processable t]
2666          ["Remove mark" gnus-summary-unmark-as-processable t]
2667          ["Remove all marks" gnus-summary-unmark-all-processable t]
2668          ["Invert marks" gnus-uu-invert-processable t]
2669          ["Mark above" gnus-uu-mark-over t]
2670          ["Mark series" gnus-uu-mark-series t]
2671          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2672          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2673          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2674          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2675          ["Mark all" gnus-uu-mark-all t]
2676          ["Mark buffer" gnus-uu-mark-buffer t]
2677          ["Mark sparse" gnus-uu-mark-sparse t]
2678          ["Mark thread" gnus-uu-mark-thread t]
2679          ["Unmark thread" gnus-uu-unmark-thread t]
2680          ("Process Mark Sets"
2681           ["Kill" gnus-summary-kill-process-mark t]
2682           ["Yank" gnus-summary-yank-process-mark
2683            gnus-newsgroup-process-stack]
2684           ["Save" gnus-summary-save-process-mark t]
2685           ["Run command on marked..." gnus-summary-universal-argument t]))
2686         ("Registry Marks")
2687         ("Scroll article"
2688          ["Page forward" gnus-summary-next-page
2689           ,@(if (featurep 'xemacs) '(t)
2690               '(:help "Show next page of article"))]
2691          ["Page backward" gnus-summary-prev-page
2692           ,@(if (featurep 'xemacs) '(t)
2693               '(:help "Show previous page of article"))]
2694          ["Line forward" gnus-summary-scroll-up t])
2695         ("Move"
2696          ["Next unread article" gnus-summary-next-unread-article t]
2697          ["Previous unread article" gnus-summary-prev-unread-article t]
2698          ["Next article" gnus-summary-next-article t]
2699          ["Previous article" gnus-summary-prev-article t]
2700          ["Next unread subject" gnus-summary-next-unread-subject t]
2701          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2702          ["Next article same subject" gnus-summary-next-same-subject t]
2703          ["Previous article same subject" gnus-summary-prev-same-subject t]
2704          ["First unread article" gnus-summary-first-unread-article t]
2705          ["Best unread article" gnus-summary-best-unread-article t]
2706          ["Go to subject number..." gnus-summary-goto-subject t]
2707          ["Go to article number..." gnus-summary-goto-article t]
2708          ["Go to the last article" gnus-summary-goto-last-article t]
2709          ["Pop article off history" gnus-summary-pop-article t])
2710         ("Sort"
2711          ["Sort by number" gnus-summary-sort-by-number t]
2712          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2713          ["Sort by author" gnus-summary-sort-by-author t]
2714          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2715          ["Sort by subject" gnus-summary-sort-by-subject t]
2716          ["Sort by date" gnus-summary-sort-by-date t]
2717          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2718          ["Sort by score" gnus-summary-sort-by-score t]
2719          ["Sort by lines" gnus-summary-sort-by-lines t]
2720          ["Sort by characters" gnus-summary-sort-by-chars t]
2721          ["Randomize" gnus-summary-sort-by-random t]
2722          ["Original sort" gnus-summary-sort-by-original t])
2723         ("Help"
2724          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2725          ["Describe group" gnus-summary-describe-group t]
2726          ["Fetch charter" gnus-group-fetch-charter
2727           ,@(if (featurep 'xemacs) nil
2728               '(:help "Display the charter of the current group"))]
2729          ["Fetch control message" gnus-group-fetch-control
2730           ,@(if (featurep 'xemacs) nil
2731               '(:help "Display the archived control message for the current group"))]
2732          ["Read manual" gnus-info-find-node t])
2733         ("Modes"
2734          ["Pick and read" gnus-pick-mode t]
2735          ["Binary" gnus-binary-mode t])
2736         ("Regeneration"
2737          ["Regenerate" gnus-summary-prepare t]
2738          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2739          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2740          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2741          ["Toggle threading" gnus-summary-toggle-threads t])
2742         ["See old articles" gnus-summary-insert-old-articles t]
2743         ["See new articles" gnus-summary-insert-new-articles t]
2744         ["Filter articles..." gnus-summary-execute-command t]
2745         ["Run command on articles..." gnus-summary-universal-argument t]
2746         ["Search articles forward..." gnus-summary-search-article-forward t]
2747         ["Search articles backward..." gnus-summary-search-article-backward t]
2748         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2749         ["Expand window" gnus-summary-expand-window t]
2750         ["Expire expirable articles" gnus-summary-expire-articles
2751          (gnus-check-backend-function
2752           'request-expire-articles gnus-newsgroup-name)]
2753         ["Edit local kill file" gnus-summary-edit-local-kill t]
2754         ["Edit main kill file" gnus-summary-edit-global-kill t]
2755         ["Edit group parameters" gnus-summary-edit-parameters t]
2756         ["Customize group parameters" gnus-summary-customize-parameters t]
2757         ["Send a bug report" gnus-bug t]
2758         ("Exit"
2759          ["Catchup and exit" gnus-summary-catchup-and-exit
2760           ,@(if (featurep 'xemacs) '(t)
2761               '(:help "Mark unread articles in this group as read, then exit"))]
2762          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2763          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2764          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2765          ["Exit group" gnus-summary-exit
2766           ,@(if (featurep 'xemacs) '(t)
2767               '(:help "Exit current group, return to group selection mode"))]
2768          ["Exit group without updating" gnus-summary-exit-no-update t]
2769          ["Exit and goto next group" gnus-summary-next-group t]
2770          ["Exit and goto prev group" gnus-summary-prev-group t]
2771          ["Reselect group" gnus-summary-reselect-current-group t]
2772          ["Rescan group" gnus-summary-rescan-group t]
2773          ["Update dribble" gnus-summary-save-newsrc t])))
2774
2775     (gnus-run-hooks 'gnus-summary-menu-hook)))
2776
2777 (defvar gnus-summary-tool-bar-map nil)
2778
2779 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2780 ;; affect _new_ message buffers.  We might add a function that walks thru all
2781 ;; summary-mode buffers and force the update.
2782 (defun gnus-summary-tool-bar-update (&optional symbol value)
2783   "Update summary mode toolbar.
2784 Setter function for custom variables."
2785   (setq-default gnus-summary-tool-bar-map nil)
2786   (when symbol
2787     ;; When used as ":set" function:
2788     (set-default symbol value))
2789   (when (gnus-buffer-live-p gnus-summary-buffer)
2790     (with-current-buffer gnus-summary-buffer
2791       (gnus-summary-make-tool-bar))))
2792
2793 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2794                                      'gnus-summary-tool-bar-gnome
2795                                    'gnus-summary-tool-bar-retro)
2796   "Specifies the Gnus summary tool bar.
2797
2798 It can be either a list or a symbol refering to a list.  See
2799 `gmm-tool-bar-from-list' for the format of the list.  The
2800 default key map is `gnus-summary-mode-map'.
2801
2802 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2803 `gnus-summary-tool-bar-retro'."
2804   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2805                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2806                  (repeat :tag "User defined list" gmm-tool-bar-item)
2807                  (symbol))
2808   :version "23.1" ;; No Gnus
2809   :initialize 'custom-initialize-default
2810   :set 'gnus-summary-tool-bar-update
2811   :group 'gnus-summary)
2812
2813 (defcustom gnus-summary-tool-bar-gnome
2814   '((gnus-summary-post-news "mail/compose" nil)
2815     (gnus-summary-insert-new-articles "mail/inbox" nil
2816                                       :visible (or (not gnus-agent)
2817                                                    gnus-plugged))
2818     (gnus-summary-reply-with-original "mail/reply")
2819     (gnus-summary-reply "mail/reply" nil :visible nil)
2820     (gnus-summary-followup-with-original "mail/reply-all")
2821     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2822     (gnus-summary-mail-forward "mail/forward")
2823     (gnus-summary-save-article "mail/save")
2824     (gnus-summary-search-article-forward "search" nil :visible nil)
2825     (gnus-summary-print-article "print")
2826     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2827     ;; Some new commands that may need more suitable icons:
2828     (gnus-summary-save-newsrc "save" nil :visible nil)
2829     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2830     (gnus-summary-prev-article "left-arrow")
2831     (gnus-summary-next-article "right-arrow")
2832     (gnus-summary-next-page "next-page")
2833     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2834     ;;
2835     ;; Maybe some sort-by-... could be added:
2836     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2837     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2838     (gnus-summary-mark-as-expirable
2839      "delete" nil
2840      :visible (gnus-check-backend-function 'request-expire-articles
2841                                            gnus-newsgroup-name))
2842     (gnus-summary-mark-as-spam
2843      "mail/spam" t
2844      :visible (and (fboundp 'spam-group-ham-contents-p)
2845                    (spam-group-ham-contents-p gnus-newsgroup-name))
2846      :help "Mark as spam")
2847     (gnus-summary-mark-as-read-forward
2848      "mail/not-spam" nil
2849      :visible (and (fboundp 'spam-group-spam-contents-p)
2850                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2851     ;;
2852     (gnus-summary-exit "exit")
2853     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2854     (gnus-info-find-node "help"))
2855   "List of functions for the summary tool bar (GNOME style).
2856
2857 See `gmm-tool-bar-from-list' for the format of the list."
2858   :type '(repeat gmm-tool-bar-item)
2859   :version "23.1" ;; No Gnus
2860   :initialize 'custom-initialize-default
2861   :set 'gnus-summary-tool-bar-update
2862   :group 'gnus-summary)
2863
2864 (defcustom gnus-summary-tool-bar-retro
2865   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2866     (gnus-summary-next-unread-article "gnus/next-ur")
2867     (gnus-summary-post-news "gnus/post")
2868     (gnus-summary-followup-with-original "gnus/fuwo")
2869     (gnus-summary-followup "gnus/followup")
2870     (gnus-summary-reply-with-original "gnus/reply-wo")
2871     (gnus-summary-reply "gnus/reply")
2872     (gnus-summary-caesar-message "gnus/rot13")
2873     (gnus-uu-decode-uu "gnus/uu-decode")
2874     (gnus-summary-save-article-file "gnus/save-aif")
2875     (gnus-summary-save-article "gnus/save-art")
2876     (gnus-uu-post-news "gnus/uu-post")
2877     (gnus-summary-catchup "gnus/catchup")
2878     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2879     (gnus-summary-exit "gnus/exit-summ")
2880     ;; Some new command that may need more suitable icons:
2881     (gnus-summary-print-article "gnus/print" nil :visible nil)
2882     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2883     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2884     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2885     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2886     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2887     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2888     ;;
2889     (gnus-info-find-node "gnus/help" nil :visible nil))
2890   "List of functions for the summary tool bar (retro look).
2891
2892 See `gmm-tool-bar-from-list' for the format of the list."
2893   :type '(repeat gmm-tool-bar-item)
2894   :version "23.1" ;; No Gnus
2895   :initialize 'custom-initialize-default
2896   :set 'gnus-summary-tool-bar-update
2897   :group 'gnus-summary)
2898
2899 (defcustom gnus-summary-tool-bar-zap-list t
2900   "List of icon items from the global tool bar.
2901 These items are not displayed in the Gnus summary mode tool bar.
2902
2903 See `gmm-tool-bar-from-list' for the format of the list."
2904   :type 'gmm-tool-bar-zap-list
2905   :version "23.1" ;; No Gnus
2906   :initialize 'custom-initialize-default
2907   :set 'gnus-summary-tool-bar-update
2908   :group 'gnus-summary)
2909
2910 (defvar image-load-path)
2911 (defvar tool-bar-map)
2912
2913 (defun gnus-summary-make-tool-bar (&optional force)
2914   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2915 When FORCE, rebuild the tool bar."
2916   (when (and (not (featurep 'xemacs))
2917              (boundp 'tool-bar-mode)
2918              tool-bar-mode
2919              (or (not gnus-summary-tool-bar-map) force))
2920     (let* ((load-path
2921             (gmm-image-load-path-for-library "gnus"
2922                                              "mail/save.xpm"
2923                                              nil t))
2924            (image-load-path (cons (car load-path)
2925                                   (when (boundp 'image-load-path)
2926                                     image-load-path)))
2927            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2928                                         gnus-summary-tool-bar-zap-list
2929                                         'gnus-summary-mode-map)))
2930       (when map
2931         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2932         ;; uses it's value.
2933         (setq gnus-summary-tool-bar-map map))))
2934   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2935
2936 (defun gnus-score-set-default (var value)
2937   "A version of set that updates the GNU Emacs menu-bar."
2938   (set var value)
2939   ;; It is the message that forces the active status to be updated.
2940   (message ""))
2941
2942 (defun gnus-make-score-map (type)
2943   "Make a summary score map of type TYPE."
2944   (if t
2945       nil
2946     (let ((headers '(("author" "from" string)
2947                      ("subject" "subject" string)
2948                      ("article body" "body" string)
2949                      ("article head" "head" string)
2950                      ("xref" "xref" string)
2951                      ("extra header" "extra" string)
2952                      ("lines" "lines" number)
2953                      ("followups to author" "followup" string)))
2954           (types '((number ("less than" <)
2955                            ("greater than" >)
2956                            ("equal" =))
2957                    (string ("substring" s)
2958                            ("exact string" e)
2959                            ("fuzzy string" f)
2960                            ("regexp" r))))
2961           (perms '(("temporary" (current-time-string))
2962                    ("permanent" nil)
2963                    ("immediate" now)))
2964           header)
2965       (list
2966        (apply
2967         'nconc
2968         (list
2969          (if (eq type 'lower)
2970              "Lower score"
2971            "Increase score"))
2972         (let (outh)
2973           (while headers
2974             (setq header (car headers))
2975             (setq outh
2976                   (cons
2977                    (apply
2978                     'nconc
2979                     (list (car header))
2980                     (let ((ts (cdr (assoc (nth 2 header) types)))
2981                           outt)
2982                       (while ts
2983                         (setq outt
2984                               (cons
2985                                (apply
2986                                 'nconc
2987                                 (list (caar ts))
2988                                 (let ((ps perms)
2989                                       outp)
2990                                   (while ps
2991                                     (setq outp
2992                                           (cons
2993                                            (vector
2994                                             (caar ps)
2995                                             (list
2996                                              'gnus-summary-score-entry
2997                                              (nth 1 header)
2998                                              (if (or (string= (nth 1 header)
2999                                                               "head")
3000                                                      (string= (nth 1 header)
3001                                                               "body"))
3002                                                  ""
3003                                                (list 'gnus-summary-header
3004                                                      (nth 1 header)))
3005                                              (list 'quote (nth 1 (car ts)))
3006                                              (list 'gnus-score-delta-default
3007                                                    nil)
3008                                              (nth 1 (car ps))
3009                                              t)
3010                                             t)
3011                                            outp))
3012                                     (setq ps (cdr ps)))
3013                                   (list (nreverse outp))))
3014                                outt))
3015                         (setq ts (cdr ts)))
3016                       (list (nreverse outt))))
3017                    outh))
3018             (setq headers (cdr headers)))
3019           (list (nreverse outh))))))))
3020
3021
3022 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3023 (defvar bookmark-make-record-function)
3024 \f
3025
3026 (defun gnus-summary-mode (&optional group)
3027   "Major mode for reading articles.
3028
3029 All normal editing commands are switched off.
3030 \\<gnus-summary-mode-map>
3031 Each line in this buffer represents one article.  To read an
3032 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3033 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3034 respectively.
3035
3036 You can also post articles and send mail from this buffer.  To
3037 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3038 of an article, type `\\[gnus-summary-reply]'.
3039
3040 There are approx. one gazillion commands you can execute in this
3041 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3042
3043 The following commands are available:
3044
3045 \\{gnus-summary-mode-map}"
3046   (interactive)
3047   (kill-all-local-variables)
3048   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3049     (gnus-summary-make-local-variables))
3050   (gnus-summary-make-local-variables)
3051   (setq gnus-newsgroup-name group)
3052   (when (gnus-visual-p 'summary-menu 'menu)
3053     (gnus-summary-make-menu-bar)
3054     (gnus-summary-make-tool-bar))
3055   (gnus-make-thread-indent-array)
3056   (gnus-simplify-mode-line)
3057   (setq major-mode 'gnus-summary-mode)
3058   (setq mode-name "Summary")
3059   (use-local-map gnus-summary-mode-map)
3060   (buffer-disable-undo)
3061   (setq buffer-read-only t              ;Disable modification
3062         show-trailing-whitespace nil)
3063   (setq truncate-lines t)
3064   (add-to-invisibility-spec '(gnus-sum . t))
3065   (gnus-summary-set-display-table)
3066   (gnus-set-default-directory)
3067   (make-local-variable 'gnus-summary-line-format)
3068   (make-local-variable 'gnus-summary-line-format-spec)
3069   (make-local-variable 'gnus-summary-dummy-line-format)
3070   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3071   (make-local-variable 'gnus-summary-mark-positions)
3072   (gnus-make-local-hook 'pre-command-hook)
3073   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3074   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3075   (turn-on-gnus-mailing-list-mode)
3076   (mm-enable-multibyte)
3077   (set (make-local-variable 'bookmark-make-record-function)
3078        'gnus-summary-bookmark-make-record)
3079   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3080   (gnus-update-summary-mark-positions))
3081
3082 (defun gnus-summary-make-local-variables ()
3083   "Make all the local summary buffer variables."
3084   (let (global)
3085     (dolist (local gnus-summary-local-variables)
3086       (if (consp local)
3087           (progn
3088             (if (eq (cdr local) 'global)
3089                 ;; Copy the global value of the variable.
3090                 (setq global (symbol-value (car local)))
3091               ;; Use the value from the list.
3092               (setq global (eval (cdr local))))
3093             (set (make-local-variable (car local)) global))
3094         ;; Simple nil-valued local variable.
3095         (set (make-local-variable local) nil)))))
3096
3097 (defun gnus-summary-clear-local-variables ()
3098   (let ((locals gnus-summary-local-variables))
3099     (while locals
3100       (if (consp (car locals))
3101           (and (symbolp (caar locals))
3102                (set (caar locals) nil))
3103         (and (symbolp (car locals))
3104              (set (car locals) nil)))
3105       (setq locals (cdr locals)))))
3106
3107 ;; Summary data functions.
3108
3109 (defmacro gnus-data-number (data)
3110   `(car ,data))
3111
3112 (defmacro gnus-data-set-number (data number)
3113   `(setcar ,data ,number))
3114
3115 (defmacro gnus-data-mark (data)
3116   `(nth 1 ,data))
3117
3118 (defmacro gnus-data-set-mark (data mark)
3119   `(setcar (nthcdr 1 ,data) ,mark))
3120
3121 (defmacro gnus-data-pos (data)
3122   `(nth 2 ,data))
3123
3124 (defmacro gnus-data-set-pos (data pos)
3125   `(setcar (nthcdr 2 ,data) ,pos))
3126
3127 (defmacro gnus-data-header (data)
3128   `(nth 3 ,data))
3129
3130 (defmacro gnus-data-set-header (data header)
3131   `(setf (nth 3 ,data) ,header))
3132
3133 (defmacro gnus-data-level (data)
3134   `(nth 4 ,data))
3135
3136 (defmacro gnus-data-unread-p (data)
3137   `(= (nth 1 ,data) gnus-unread-mark))
3138
3139 (defmacro gnus-data-read-p (data)
3140   `(/= (nth 1 ,data) gnus-unread-mark))
3141
3142 (defmacro gnus-data-pseudo-p (data)
3143   `(consp (nth 3 ,data)))
3144
3145 (defmacro gnus-data-find (number)
3146   `(assq ,number gnus-newsgroup-data))
3147
3148 (defmacro gnus-data-find-list (number &optional data)
3149   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3150      (memq (assq ,number bdata)
3151            bdata)))
3152
3153 (defmacro gnus-data-make (number mark pos header level)
3154   `(list ,number ,mark ,pos ,header ,level))
3155
3156 (defun gnus-data-enter (after-article number mark pos header level offset)
3157   (let ((data (gnus-data-find-list after-article)))
3158     (unless data
3159       (error "No such article: %d" after-article))
3160     (setcdr data (cons (gnus-data-make number mark pos header level)
3161                        (cdr data)))
3162     (setq gnus-newsgroup-data-reverse nil)
3163     (gnus-data-update-list (cddr data) offset)))
3164
3165 (defun gnus-data-enter-list (after-article list &optional offset)
3166   (when list
3167     (let ((data (and after-article (gnus-data-find-list after-article)))
3168           (ilist list))
3169       (if (not (or data
3170                    after-article))
3171           (let ((odata gnus-newsgroup-data))
3172             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3173             (when offset
3174               (gnus-data-update-list odata offset)))
3175         ;; Find the last element in the list to be spliced into the main
3176         ;; list.
3177         (setq list (last list))
3178         (if (not data)
3179             (progn
3180               (setcdr list gnus-newsgroup-data)
3181               (setq gnus-newsgroup-data ilist)
3182               (when offset
3183                 (gnus-data-update-list (cdr list) offset)))
3184           (setcdr list (cdr data))
3185           (setcdr data ilist)
3186           (when offset
3187             (gnus-data-update-list (cdr list) offset))))
3188       (setq gnus-newsgroup-data-reverse nil))))
3189
3190 (defun gnus-data-remove (article &optional offset)
3191   (let ((data gnus-newsgroup-data))
3192     (if (= (gnus-data-number (car data)) article)
3193         (progn
3194           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3195                 gnus-newsgroup-data-reverse nil)
3196           (when offset
3197             (gnus-data-update-list gnus-newsgroup-data offset)))
3198       (while (cdr data)
3199         (when (= (gnus-data-number (cadr data)) article)
3200           (setcdr data (cddr data))
3201           (when offset
3202             (gnus-data-update-list (cdr data) offset))
3203           (setq data nil
3204                 gnus-newsgroup-data-reverse nil))
3205         (setq data (cdr data))))))
3206
3207 (defmacro gnus-data-list (backward)
3208   `(if ,backward
3209        (or gnus-newsgroup-data-reverse
3210            (setq gnus-newsgroup-data-reverse
3211                  (reverse gnus-newsgroup-data)))
3212      gnus-newsgroup-data))
3213
3214 (defun gnus-data-update-list (data offset)
3215   "Add OFFSET to the POS of all data entries in DATA."
3216   (setq gnus-newsgroup-data-reverse nil)
3217   (while data
3218     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3219     (setq data (cdr data))))
3220
3221 (defun gnus-summary-article-pseudo-p (article)
3222   "Say whether this article is a pseudo article or not."
3223   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3224
3225 (defmacro gnus-summary-article-sparse-p (article)
3226   "Say whether this article is a sparse article or not."
3227   `(memq ,article gnus-newsgroup-sparse))
3228
3229 (defmacro gnus-summary-article-ancient-p (article)
3230   "Say whether this article is a sparse article or not."
3231   `(memq ,article gnus-newsgroup-ancient))
3232
3233 (defun gnus-article-parent-p (number)
3234   "Say whether this article is a parent or not."
3235   (let ((data (gnus-data-find-list number)))
3236     (and (cdr data)              ; There has to be an article after...
3237          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3238             (gnus-data-level (nth 1 data))))))
3239
3240 (defun gnus-article-children (number)
3241   "Return a list of all children to NUMBER."
3242   (let* ((data (gnus-data-find-list number))
3243          (level (gnus-data-level (car data)))
3244          children)
3245     (setq data (cdr data))
3246     (while (and data
3247                 (= (gnus-data-level (car data)) (1+ level)))
3248       (push (gnus-data-number (car data)) children)
3249       (setq data (cdr data)))
3250     children))
3251
3252 (defmacro gnus-summary-skip-intangible ()
3253   "If the current article is intangible, then jump to a different article."
3254   '(let ((to (get-text-property (point) 'gnus-intangible)))
3255      (and to (gnus-summary-goto-subject to))))
3256
3257 (defmacro gnus-summary-article-intangible-p ()
3258   "Say whether this article is intangible or not."
3259   '(get-text-property (point) 'gnus-intangible))
3260
3261 (defun gnus-article-read-p (article)
3262   "Say whether ARTICLE is read or not."
3263   (not (or (memq article gnus-newsgroup-marked)
3264            (memq article gnus-newsgroup-spam-marked)
3265            (memq article gnus-newsgroup-unreads)
3266            (memq article gnus-newsgroup-unselected)
3267            (memq article gnus-newsgroup-dormant))))
3268
3269 ;; Some summary mode macros.
3270
3271 (defmacro gnus-summary-article-number ()
3272   "The article number of the article on the current line.
3273 If there isn't an article number here, then we return the current
3274 article number."
3275   '(progn
3276      (gnus-summary-skip-intangible)
3277      (or (get-text-property (point) 'gnus-number)
3278          (gnus-summary-last-subject))))
3279
3280 (defmacro gnus-summary-article-header (&optional number)
3281   "Return the header of article NUMBER."
3282   `(gnus-data-header (gnus-data-find
3283                       ,(or number '(gnus-summary-article-number)))))
3284
3285 (defmacro gnus-summary-thread-level (&optional number)
3286   "Return the level of thread that starts with article NUMBER."
3287   `(if (and (eq gnus-summary-make-false-root 'dummy)
3288             (get-text-property (point) 'gnus-intangible))
3289        0
3290      (gnus-data-level (gnus-data-find
3291                        ,(or number '(gnus-summary-article-number))))))
3292
3293 (defmacro gnus-summary-article-mark (&optional number)
3294   "Return the mark of article NUMBER."
3295   `(gnus-data-mark (gnus-data-find
3296                     ,(or number '(gnus-summary-article-number)))))
3297
3298 (defmacro gnus-summary-article-pos (&optional number)
3299   "Return the position of the line of article NUMBER."
3300   `(gnus-data-pos (gnus-data-find
3301                    ,(or number '(gnus-summary-article-number)))))
3302
3303 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3304 (defmacro gnus-summary-article-subject (&optional number)
3305   "Return current subject string or nil if nothing."
3306   `(let ((headers
3307           ,(if number
3308                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3309              '(gnus-data-header (assq (gnus-summary-article-number)
3310                                       gnus-newsgroup-data)))))
3311      (and headers
3312           (vectorp headers)
3313           (mail-header-subject headers))))
3314
3315 (defmacro gnus-summary-article-score (&optional number)
3316   "Return current article score."
3317   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3318                   gnus-newsgroup-scored))
3319        gnus-summary-default-score 0))
3320
3321 (defun gnus-summary-article-children (&optional number)
3322   "Return a list of article numbers that are children of article NUMBER."
3323   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3324          (level (gnus-data-level (car data)))
3325          l children)
3326     (while (and (setq data (cdr data))
3327                 (> (setq l (gnus-data-level (car data))) level))
3328       (and (= (1+ level) l)
3329            (push (gnus-data-number (car data))
3330                  children)))
3331     (nreverse children)))
3332
3333 (defun gnus-summary-article-parent (&optional number)
3334   "Return the article number of the parent of article NUMBER."
3335   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3336                                     (gnus-data-list t)))
3337          (level (gnus-data-level (car data))))
3338     (if (zerop level)
3339         ()                              ; This is a root.
3340       ;; We search until we find an article with a level less than
3341       ;; this one.  That function has to be the parent.
3342       (while (and (setq data (cdr data))
3343                   (not (< (gnus-data-level (car data)) level))))
3344       (and data (gnus-data-number (car data))))))
3345
3346 (defun gnus-unread-mark-p (mark)
3347   "Say whether MARK is the unread mark."
3348   (= mark gnus-unread-mark))
3349
3350 (defun gnus-read-mark-p (mark)
3351   "Say whether MARK is one of the marks that mark as read.
3352 This is all marks except unread, ticked, dormant, and expirable."
3353   (not (or (= mark gnus-unread-mark)
3354            (= mark gnus-ticked-mark)
3355            (= mark gnus-spam-mark)
3356            (= mark gnus-dormant-mark)
3357            (= mark gnus-expirable-mark))))
3358
3359 (defmacro gnus-article-mark (number)
3360   "Return the MARK of article NUMBER.
3361 This macro should only be used when computing the mark the \"first\"
3362 time; i.e., when generating the summary lines.  After that,
3363 `gnus-summary-article-mark' should be used to examine the
3364 marks of articles."
3365   `(cond
3366     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3367     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3368     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3369     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3370     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3371     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3372     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3373     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3374            gnus-ancient-mark))))
3375
3376 ;; Saving hidden threads.
3377
3378 (defmacro gnus-save-hidden-threads (&rest forms)
3379   "Save hidden threads, eval FORMS, and restore the hidden threads."
3380   (let ((config (make-symbol "config")))
3381     `(let ((,config (gnus-hidden-threads-configuration)))
3382        (unwind-protect
3383            (save-excursion
3384              ,@forms)
3385          (gnus-restore-hidden-threads-configuration ,config)))))
3386 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3387 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3388
3389 (defun gnus-data-compute-positions ()
3390   "Compute the positions of all articles."
3391   (setq gnus-newsgroup-data-reverse nil)
3392   (let ((data gnus-newsgroup-data))
3393     (save-excursion
3394       (gnus-save-hidden-threads
3395         (gnus-summary-show-all-threads)
3396         (goto-char (point-min))
3397         (while data
3398           (while (get-text-property (point) 'gnus-intangible)
3399             (forward-line 1))
3400           (gnus-data-set-pos (car data) (+ (point) 3))
3401           (setq data (cdr data))
3402           (forward-line 1))))))
3403
3404 (defun gnus-hidden-threads-configuration ()
3405   "Return the current hidden threads configuration."
3406   (save-excursion
3407     (let (config)
3408       (goto-char (point-min))
3409       (while (search-forward "\r" nil t)
3410         (push (1- (point)) config))
3411       config)))
3412
3413 (defun gnus-restore-hidden-threads-configuration (config)
3414   "Restore hidden threads configuration from CONFIG."
3415   (save-excursion
3416     (let (point (inhibit-read-only t))
3417       (while (setq point (pop config))
3418         (when (and (< point (point-max))
3419                    (goto-char point)
3420                    (eq (char-after) ?\n))
3421           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3422
3423 ;; Various summary mode internalish functions.
3424
3425 (defun gnus-mouse-pick-article (e)
3426   (interactive "e")
3427   (mouse-set-point e)
3428   (gnus-summary-next-page nil t))
3429
3430 (defun gnus-summary-set-display-table ()
3431   "Change the display table.
3432 Odd characters have a tendency to mess
3433 up nicely formatted displays - we make all possible glyphs
3434 display only a single character."
3435
3436   ;; We start from the standard display table, if any.
3437   (let ((table (or (copy-sequence standard-display-table)
3438                    (make-display-table)))
3439         (i 32))
3440     ;; Nix out all the control chars...
3441     (while (>= (setq i (1- i)) 0)
3442       (gnus-put-display-table i [??] table))
3443    ;; ... but not newline and cr, of course.  (cr is necessary for the
3444     ;; selective display).
3445     (gnus-put-display-table ?\n nil table)
3446     (gnus-put-display-table ?\r nil table)
3447     ;; We keep TAB as well.
3448     (gnus-put-display-table ?\t nil table)
3449     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3450     ;; Emacs 23 (unicode), that are not set already.
3451     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3452                  160
3453                256)))
3454       (while (>= (setq i (1- i)) 127)
3455         ;; Only modify if the entry is nil.
3456         (unless (gnus-get-display-table i table)
3457           (gnus-put-display-table i [??] table))))
3458     (setq buffer-display-table table)))
3459
3460 (defun gnus-summary-set-article-display-arrow (pos)
3461   "Update the overlay arrow to point to line at position POS."
3462   (when gnus-summary-display-arrow
3463     (make-local-variable 'overlay-arrow-position)
3464     (make-local-variable 'overlay-arrow-string)
3465     (save-excursion
3466       (goto-char pos)
3467       (beginning-of-line)
3468       (unless overlay-arrow-position
3469         (setq overlay-arrow-position (make-marker)))
3470       (setq overlay-arrow-string "=>"
3471             overlay-arrow-position (set-marker overlay-arrow-position
3472                                                (point)
3473                                                (current-buffer))))))
3474
3475 (defun gnus-summary-setup-buffer (group)
3476   "Initialize summary buffer."
3477   (let ((buffer (gnus-summary-buffer-name group))
3478         (dead-name (concat "*Dead Summary "
3479                            (gnus-group-decoded-name group) "*")))
3480     ;; If a dead summary buffer exists, we kill it.
3481     (when (gnus-buffer-live-p dead-name)
3482       (gnus-kill-buffer dead-name))
3483     (if (get-buffer buffer)
3484         (progn
3485           (set-buffer buffer)
3486           (setq gnus-summary-buffer (current-buffer))
3487           (not gnus-newsgroup-prepared))
3488       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3489       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3490       (gnus-summary-mode group)
3491       (when gnus-carpal
3492         (gnus-carpal-setup-buffer 'summary))
3493       (when (gnus-group-quit-config group)
3494         (set (make-local-variable 'gnus-single-article-buffer) nil))
3495       (make-local-variable 'gnus-article-buffer)
3496       (make-local-variable 'gnus-article-current)
3497       (make-local-variable 'gnus-original-article-buffer)
3498       (setq gnus-newsgroup-name group)
3499       ;; Set any local variables in the group parameters.
3500       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3501       t)))
3502
3503 (defun gnus-set-global-variables ()
3504   "Set the global equivalents of the buffer-local variables.
3505 They are set to the latest values they had.  These reflect the summary
3506 buffer that was in action when the last article was fetched."
3507   (when (eq major-mode 'gnus-summary-mode)
3508     (setq gnus-summary-buffer (current-buffer))
3509     (let ((name gnus-newsgroup-name)
3510           (marked gnus-newsgroup-marked)
3511           (spam gnus-newsgroup-spam-marked)
3512           (unread gnus-newsgroup-unreads)
3513           (headers gnus-current-headers)
3514           (data gnus-newsgroup-data)
3515           (summary gnus-summary-buffer)
3516           (article-buffer gnus-article-buffer)
3517           (original gnus-original-article-buffer)
3518           (gac gnus-article-current)
3519           (reffed gnus-reffed-article-number)
3520           (score-file gnus-current-score-file)
3521           (default-charset gnus-newsgroup-charset)
3522           vlist)
3523       (let ((locals gnus-newsgroup-variables))
3524         (while locals
3525           (if (consp (car locals))
3526               (push (eval (caar locals)) vlist)
3527             (push (eval (car locals)) vlist))
3528           (setq locals (cdr locals)))
3529         (setq vlist (nreverse vlist)))
3530       (with-current-buffer gnus-group-buffer
3531         (setq gnus-newsgroup-name name
3532               gnus-newsgroup-marked marked
3533               gnus-newsgroup-spam-marked spam
3534               gnus-newsgroup-unreads unread
3535               gnus-current-headers headers
3536               gnus-newsgroup-data data
3537               gnus-article-current gac
3538               gnus-summary-buffer summary
3539               gnus-article-buffer article-buffer
3540               gnus-original-article-buffer original
3541               gnus-reffed-article-number reffed
3542               gnus-current-score-file score-file
3543               gnus-newsgroup-charset default-charset)
3544         (let ((locals gnus-newsgroup-variables))
3545           (while locals
3546             (if (consp (car locals))
3547                 (set (caar locals) (pop vlist))
3548               (set (car locals) (pop vlist)))
3549             (setq locals (cdr locals))))
3550         ;; The article buffer also has local variables.
3551         (when (gnus-buffer-live-p gnus-article-buffer)
3552           (set-buffer gnus-article-buffer)
3553           (setq gnus-summary-buffer summary))))))
3554
3555 (defun gnus-summary-article-unread-p (article)
3556   "Say whether ARTICLE is unread or not."
3557   (memq article gnus-newsgroup-unreads))
3558
3559 (defun gnus-summary-first-article-p (&optional article)
3560   "Return whether ARTICLE is the first article in the buffer."
3561   (if (not (setq article (or article (gnus-summary-article-number))))
3562       nil
3563     (eq article (caar gnus-newsgroup-data))))
3564
3565 (defun gnus-summary-last-article-p (&optional article)
3566   "Return whether ARTICLE is the last article in the buffer."
3567   (if (not (setq article (or article (gnus-summary-article-number))))
3568       ;; All non-existent numbers are the last article.  :-)
3569       t
3570     (not (cdr (gnus-data-find-list article)))))
3571
3572 (defun gnus-make-thread-indent-array (&optional n)
3573   (when (or n
3574             (progn (setq n 200) nil)
3575             (null gnus-thread-indent-array)
3576             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3577     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3578           gnus-thread-indent-array-level gnus-thread-indent-level)
3579     (while (>= n 0)
3580       (aset gnus-thread-indent-array n
3581             (make-string (* n gnus-thread-indent-level) ? ))
3582       (setq n (1- n)))))
3583
3584 (defun gnus-update-summary-mark-positions ()
3585   "Compute where the summary marks are to go."
3586   (save-excursion
3587     (when (gnus-buffer-exists-p gnus-summary-buffer)
3588       (set-buffer gnus-summary-buffer))
3589     (let ((spec gnus-summary-line-format-spec)
3590           pos)
3591       (save-excursion
3592         (gnus-set-work-buffer)
3593         (let ((gnus-tmp-unread ?Z)
3594               (gnus-replied-mark ?Z)
3595               (gnus-score-below-mark ?Z)
3596               (gnus-score-over-mark ?Z)
3597               (gnus-undownloaded-mark ?Z)
3598               (gnus-summary-line-format-spec spec)
3599               (gnus-newsgroup-downloadable '(0))
3600               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3601               case-fold-search ignores)
3602           ;; Here, all marks are bound to Z.
3603           (gnus-summary-insert-line header
3604                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3605           (goto-char (point-min))
3606           ;; Memorize the positions of the same characters as dummy marks.
3607           (while (re-search-forward "[A-D]" nil t)
3608             (push (point) ignores))
3609           (erase-buffer)
3610           ;; We use A-D as dummy marks in order to know column positions
3611           ;; where marks should be inserted.
3612           (setq gnus-tmp-unread ?A
3613                 gnus-replied-mark ?B
3614                 gnus-score-below-mark ?C
3615                 gnus-score-over-mark ?C
3616                 gnus-undownloaded-mark ?D)
3617           (gnus-summary-insert-line header
3618                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3619           ;; Ignore characters which aren't dummy marks.
3620           (dolist (p ignores)
3621             (delete-region (goto-char (1- p)) p)
3622             (insert ?Z))
3623           (goto-char (point-min))
3624           (setq pos (list (cons 'unread
3625                                 (and (search-forward "A" nil t)
3626                                      (- (point) (point-min) 1)))))
3627           (goto-char (point-min))
3628           (push (cons 'replied (and (search-forward "B" nil t)
3629                                     (- (point) (point-min) 1)))
3630                 pos)
3631           (goto-char (point-min))
3632           (push (cons 'score (and (search-forward "C" nil t)
3633                                   (- (point) (point-min) 1)))
3634                 pos)
3635           (goto-char (point-min))
3636           (push (cons 'download (and (search-forward "D" nil t)
3637                                      (- (point) (point-min) 1)))
3638                 pos)))
3639       (setq gnus-summary-mark-positions pos))))
3640
3641 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3642   "Insert a dummy root in the summary buffer."
3643   (beginning-of-line)
3644   (gnus-add-text-properties
3645    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3646    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3647
3648 (defun gnus-summary-extract-address-component (from)
3649   (or (car (funcall gnus-extract-address-components from))
3650       from))
3651
3652 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3653   (let ((mail-parse-charset gnus-newsgroup-charset)
3654         (ignored-from-addresses (gnus-ignored-from-addresses))
3655         ; Is it really necessary to do this next part for each summary line?
3656         ; Luckily, doesn't seem to slow things down much.
3657         (mail-parse-ignored-charsets
3658          (with-current-buffer gnus-summary-buffer
3659            gnus-newsgroup-ignored-charsets)))
3660     (or
3661      (and ignored-from-addresses
3662           (string-match ignored-from-addresses gnus-tmp-from)
3663           (let ((extra-headers (mail-header-extra header))
3664                 to
3665                 newsgroups)
3666             (cond
3667              ((setq to (cdr (assq 'To extra-headers)))
3668               (concat gnus-summary-to-prefix
3669                       (inline
3670                         (gnus-summary-extract-address-component
3671                          (funcall gnus-decode-encoded-address-function to)))))
3672              ((setq newsgroups
3673                     (or
3674                      (cdr (assq 'Newsgroups extra-headers))
3675                      (and
3676                       (memq 'Newsgroups gnus-extra-headers)
3677                       (eq (car (gnus-find-method-for-group
3678                                 gnus-newsgroup-name)) 'nntp)
3679                       (gnus-group-real-name gnus-newsgroup-name))))
3680               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3681      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3682
3683 (defun gnus-summary-insert-line (gnus-tmp-header
3684                                  gnus-tmp-level gnus-tmp-current
3685                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3686                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3687                                  &optional gnus-tmp-dummy gnus-tmp-score
3688                                  gnus-tmp-process)
3689   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3690       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3691                                           gnus-tmp-level)))
3692   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3693          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3694          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3695          (gnus-tmp-score-char
3696           (if (or (null gnus-summary-default-score)
3697                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3698                       gnus-summary-zcore-fuzz))
3699               ?                         ;Whitespace
3700             (if (< gnus-tmp-score gnus-summary-default-score)
3701                 gnus-score-below-mark gnus-score-over-mark)))
3702          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3703          (gnus-tmp-replied
3704           (cond (gnus-tmp-process gnus-process-mark)
3705                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3706                  gnus-cached-mark)
3707                 (gnus-tmp-replied gnus-replied-mark)
3708                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3709                  gnus-forwarded-mark)
3710                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3711                  gnus-saved-mark)
3712                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3713                  gnus-recent-mark)
3714                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3715                  gnus-unseen-mark)
3716                 (t gnus-no-mark)))
3717          (gnus-tmp-downloaded
3718           (cond (undownloaded
3719                  gnus-undownloaded-mark)
3720                 (gnus-newsgroup-agentized
3721                  gnus-downloaded-mark)
3722                 (t
3723                  gnus-no-mark)))
3724          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3725          (gnus-tmp-name
3726           (cond
3727            ((string-match "<[^>]+> *$" gnus-tmp-from)
3728             (let ((beg (match-beginning 0)))
3729               (or (and (string-match "^\".+\"" gnus-tmp-from)
3730                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3731                   (substring gnus-tmp-from 0 beg))))
3732            ((string-match "(.+)" gnus-tmp-from)
3733             (substring gnus-tmp-from
3734                        (1+ (match-beginning 0)) (1- (match-end 0))))
3735            (t gnus-tmp-from)))
3736          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3737          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3738          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3739          (inhibit-read-only t))
3740     (when (string= gnus-tmp-name "")
3741       (setq gnus-tmp-name gnus-tmp-from))
3742     (unless (numberp gnus-tmp-lines)
3743       (setq gnus-tmp-lines -1))
3744     (if (= gnus-tmp-lines -1)
3745         (setq gnus-tmp-lines "?")
3746       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3747     (condition-case ()
3748         (gnus-put-text-property
3749          (point)
3750          (progn (eval gnus-summary-line-format-spec) (point))
3751          'gnus-number gnus-tmp-number)
3752       (error (gnus-message 5 "Error updating the summary line")))
3753     (when (gnus-visual-p 'summary-highlight 'highlight)
3754       (forward-line -1)
3755       (gnus-run-hooks 'gnus-summary-update-hook)
3756       (forward-line 1))))
3757
3758 (defun gnus-summary-update-line (&optional dont-update)
3759   "Update summary line after change."
3760   (when (and gnus-summary-default-score
3761              (not gnus-summary-inhibit-highlight))
3762     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3763            (article (gnus-summary-article-number))
3764            (score (gnus-summary-article-score article)))
3765       (unless dont-update
3766         (if (and gnus-summary-mark-below
3767                  (< (gnus-summary-article-score)
3768                     gnus-summary-mark-below))
3769             ;; This article has a low score, so we mark it as read.
3770             (when (memq article gnus-newsgroup-unreads)
3771               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3772           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3773             ;; This article was previously marked as read on account
3774             ;; of a low score, but now it has risen, so we mark it as
3775             ;; unread.
3776             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3777         (gnus-summary-update-mark
3778          (if (or (null gnus-summary-default-score)
3779                  (<= (abs (- score gnus-summary-default-score))
3780                      gnus-summary-zcore-fuzz))
3781              ?                          ;Whitespace
3782            (if (< score gnus-summary-default-score)
3783                gnus-score-below-mark gnus-score-over-mark))
3784          'score))
3785       ;; Do visual highlighting.
3786       (when (gnus-visual-p 'summary-highlight 'highlight)
3787         (gnus-run-hooks 'gnus-summary-update-hook)))))
3788
3789 (defvar gnus-tmp-new-adopts nil)
3790
3791 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3792   "Return the number of articles in THREAD.
3793 This may be 0 in some cases -- if none of the articles in
3794 the thread are to be displayed."
3795   (let* ((number
3796          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3797           (cond
3798            ((not (listp thread))
3799             1)
3800            ((and (consp thread) (cdr thread))
3801             (apply
3802              '+ 1 (mapcar
3803                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3804            ((null thread)
3805             1)
3806            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3807             1)
3808            (t 0))))
3809     (when (and level (zerop level) gnus-tmp-new-adopts)
3810       (incf number
3811             (apply '+ (mapcar
3812                        'gnus-summary-number-of-articles-in-thread
3813                        gnus-tmp-new-adopts))))
3814     (if char
3815         (if (> number 1) gnus-not-empty-thread-mark
3816           gnus-empty-thread-mark)
3817       number)))
3818
3819 (defsubst gnus-summary-line-message-size (head)
3820   "Return pretty-printed version of message size.
3821 This function is intended to be used in
3822 `gnus-summary-line-format-alist'."
3823   (let ((c (or (mail-header-chars head) -1)))
3824     (cond ((< c 0) "n/a")               ; chars not available
3825           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3826           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3827           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3828           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3829
3830
3831 (defun gnus-summary-set-local-parameters (group)
3832   "Go through the local params of GROUP and set all variable specs in that list."
3833   (let ((vars '(quit-config)))          ; Ignore quit-config.
3834     (dolist (elem (gnus-group-find-parameter group))
3835       (and (consp elem)                 ; Has to be a cons.
3836            (consp (cdr elem))           ; The cdr has to be a list.
3837            (symbolp (car elem))         ; Has to be a symbol in there.
3838            (not (memq (car elem) vars))
3839            (ignore-errors
3840              (push (car elem) vars)
3841              ;; Variables like `gnus-show-threads' that are globally
3842              ;; bound, if used as group parameters, need to get to be
3843              ;; buffer-local, whereas just parameters like `gcc-self',
3844              ;; `timestamp', etc. should not be bound as variables.
3845              (if (boundp (car elem))
3846                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3847                (eval (nth 1 elem))))))))
3848
3849 (defun gnus-summary-read-group (group &optional show-all no-article
3850                                       kill-buffer no-display backward
3851                                       select-articles)
3852   "Start reading news in newsgroup GROUP.
3853 If SHOW-ALL is non-nil, already read articles are also listed.
3854 If NO-ARTICLE is non-nil, no article is selected initially.
3855 If NO-DISPLAY, don't generate a summary buffer."
3856   (let (result)
3857     (while (and group
3858                 (null (setq result
3859                             (let ((gnus-auto-select-next nil))
3860                               (or (gnus-summary-read-group-1
3861                                    group show-all no-article
3862                                    kill-buffer no-display
3863                                    select-articles)
3864                                   (setq show-all nil
3865                                         select-articles nil)))))
3866                 (eq gnus-auto-select-next 'quietly))
3867       (set-buffer gnus-group-buffer)
3868       ;; The entry function called above goes to the next
3869       ;; group automatically, so we go two groups back
3870       ;; if we are searching for the previous group.
3871       (when backward
3872         (gnus-group-prev-unread-group 2))
3873       (if (not (equal group (gnus-group-group-name)))
3874           (setq group (gnus-group-group-name))
3875         (setq group nil)))
3876     result))
3877
3878 (defun gnus-summary-read-group-1 (group show-all no-article
3879                                         kill-buffer no-display
3880                                         &optional select-articles)
3881   ;; Killed foreign groups can't be entered.
3882   ;;  (when (and (not (gnus-group-native-p group))
3883   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3884   ;;    (error "Dead non-native groups can't be entered"))
3885   (gnus-message 5 "Retrieving newsgroup: %s..."
3886                 (gnus-group-decoded-name group))
3887   (let* ((new-group (gnus-summary-setup-buffer group))
3888          (quit-config (gnus-group-quit-config group))
3889          (did-select (and new-group (gnus-select-newsgroup
3890                                      group show-all select-articles))))
3891     (cond
3892      ;; This summary buffer exists already, so we just select it.
3893      ((not new-group)
3894       (gnus-set-global-variables)
3895       (when kill-buffer
3896         (gnus-kill-or-deaden-summary kill-buffer))
3897       (gnus-configure-windows 'summary 'force)
3898       (gnus-set-mode-line 'summary)
3899       (gnus-summary-position-point)
3900       (message "")
3901       t)
3902      ;; We couldn't select this group.
3903      ((null did-select)
3904       (when (and (eq major-mode 'gnus-summary-mode)
3905                  (not (equal (current-buffer) kill-buffer)))
3906         (kill-buffer (current-buffer))
3907         (if (not quit-config)
3908             (progn
3909               ;; Update the info -- marks might need to be removed,
3910               ;; for instance.
3911               (gnus-summary-update-info)
3912               (set-buffer gnus-group-buffer)
3913               (gnus-group-jump-to-group group)
3914               (gnus-group-next-unread-group 1))
3915           (gnus-handle-ephemeral-exit quit-config)))
3916       (let ((grpinfo (gnus-get-info group)))
3917         (if (null (gnus-info-read grpinfo))
3918             (gnus-message 3 "Group %s contains no messages"
3919                           (gnus-group-decoded-name group))
3920           (gnus-message 3 "Can't select group")))
3921       nil)
3922      ;; The user did a `C-g' while prompting for number of articles,
3923      ;; so we exit this group.
3924      ((eq did-select 'quit)
3925       (and (eq major-mode 'gnus-summary-mode)
3926            (not (equal (current-buffer) kill-buffer))
3927            (kill-buffer (current-buffer)))
3928       (when kill-buffer
3929         (gnus-kill-or-deaden-summary kill-buffer))
3930       (if (not quit-config)
3931           (progn
3932             (set-buffer gnus-group-buffer)
3933             (gnus-group-jump-to-group group)
3934             (gnus-group-next-unread-group 1)
3935             (gnus-configure-windows 'group 'force))
3936         (gnus-handle-ephemeral-exit quit-config))
3937       ;; Finally signal the quit.
3938       (signal 'quit nil))
3939      ;; The group was successfully selected.
3940      (t
3941       (gnus-set-global-variables)
3942       ;; Save the active value in effect when the group was entered.
3943       (setq gnus-newsgroup-active
3944             (gnus-copy-sequence
3945              (gnus-active gnus-newsgroup-name)))
3946       ;; You can change the summary buffer in some way with this hook.
3947       (gnus-run-hooks 'gnus-select-group-hook)
3948       (when (memq 'summary (gnus-update-format-specifications
3949                             nil 'summary 'summary-mode 'summary-dummy))
3950         ;; The format specification for the summary line was updated,
3951         ;; so we need to update the mark positions as well.
3952         (gnus-update-summary-mark-positions))
3953       ;; Do score processing.
3954       (when gnus-use-scoring
3955         (gnus-possibly-score-headers))
3956       ;; Check whether to fill in the gaps in the threads.
3957       (when gnus-build-sparse-threads
3958         (gnus-build-sparse-threads))
3959       ;; Find the initial limit.
3960       (if show-all
3961           (let ((gnus-newsgroup-dormant nil))
3962             (gnus-summary-initial-limit show-all))
3963         (gnus-summary-initial-limit show-all))
3964       ;; Generate the summary buffer.
3965       (unless no-display
3966         (gnus-summary-prepare))
3967       (when gnus-use-trees
3968         (gnus-tree-open group)
3969         (setq gnus-summary-highlight-line-function
3970               'gnus-tree-highlight-article))
3971       ;; If the summary buffer is empty, but there are some low-scored
3972       ;; articles or some excluded dormants, we include these in the
3973       ;; buffer.
3974       (when (and (zerop (buffer-size))
3975                  (not no-display))
3976         (cond (gnus-newsgroup-dormant
3977                (gnus-summary-limit-include-dormant))
3978               ((and gnus-newsgroup-scored show-all)
3979                (gnus-summary-limit-include-expunged t))))
3980       ;; Function `gnus-apply-kill-file' must be called in this hook.
3981       (gnus-run-hooks 'gnus-apply-kill-hook)
3982       (if (and (zerop (buffer-size))
3983                (not no-display))
3984           (progn
3985             ;; This newsgroup is empty.
3986             (gnus-summary-catchup-and-exit nil t)
3987             (gnus-message 6 "No unread news")
3988             (when kill-buffer
3989               (gnus-kill-or-deaden-summary kill-buffer))
3990             ;; Return nil from this function.
3991             nil)
3992         ;; Hide conversation thread subtrees.  We cannot do this in
3993         ;; gnus-summary-prepare-hook since kill processing may not
3994         ;; work with hidden articles.
3995         (gnus-summary-maybe-hide-threads)
3996         (when kill-buffer
3997           (gnus-kill-or-deaden-summary kill-buffer))
3998         (gnus-summary-auto-select-subject)
3999         ;; Show first unread article if requested.
4000         (if (and (not no-article)
4001                  (not no-display)
4002                  gnus-newsgroup-unreads
4003                  gnus-auto-select-first)
4004             (progn
4005               (gnus-configure-windows 'summary)
4006               (let ((art (gnus-summary-article-number)))
4007                 (unless (and (not gnus-plugged)
4008                              (or (memq art gnus-newsgroup-undownloaded)
4009                                  (memq art gnus-newsgroup-downloadable)))
4010                   (gnus-summary-goto-article art))))
4011           ;; Don't select any articles.
4012           (gnus-summary-position-point)
4013           (gnus-configure-windows 'summary 'force)
4014           (gnus-set-mode-line 'summary))
4015         (when (and gnus-auto-center-group
4016                    (get-buffer-window gnus-group-buffer t))
4017           ;; Gotta use windows, because recenter does weird stuff if
4018           ;; the current buffer ain't the displayed window.
4019           (let ((owin (selected-window)))
4020             (select-window (get-buffer-window gnus-group-buffer t))
4021             (when (gnus-group-goto-group group)
4022               (recenter))
4023             (select-window owin)))
4024         ;; Mark this buffer as "prepared".
4025         (setq gnus-newsgroup-prepared t)
4026         (gnus-run-hooks 'gnus-summary-prepared-hook)
4027         (unless (gnus-ephemeral-group-p group)
4028           (gnus-group-update-group group))
4029         t)))))
4030
4031 (defun gnus-summary-auto-select-subject ()
4032   "Select the subject line on initial group entry."
4033   (goto-char (point-min))
4034   (cond
4035    ((eq gnus-auto-select-subject 'best)
4036     (gnus-summary-best-unread-subject))
4037    ((eq gnus-auto-select-subject 'unread)
4038     (gnus-summary-first-unread-subject))
4039    ((eq gnus-auto-select-subject 'unseen)
4040     (gnus-summary-first-unseen-subject))
4041    ((eq gnus-auto-select-subject 'unseen-or-unread)
4042     (gnus-summary-first-unseen-or-unread-subject))
4043    ((eq gnus-auto-select-subject 'first)
4044     ;; Do nothing.
4045     )
4046    ((functionp gnus-auto-select-subject)
4047     (funcall gnus-auto-select-subject))))
4048
4049 (defun gnus-summary-prepare ()
4050   "Generate the summary buffer."
4051   (interactive)
4052   (let ((inhibit-read-only t))
4053     (erase-buffer)
4054     (setq gnus-newsgroup-data nil
4055           gnus-newsgroup-data-reverse nil)
4056     (gnus-run-hooks 'gnus-summary-generate-hook)
4057     ;; Generate the buffer, either with threads or without.
4058     (when gnus-newsgroup-headers
4059       (gnus-summary-prepare-threads
4060        (if gnus-show-threads
4061            (gnus-sort-gathered-threads
4062             (funcall gnus-summary-thread-gathering-function
4063                      (gnus-sort-threads
4064                       (gnus-cut-threads (gnus-make-threads)))))
4065          ;; Unthreaded display.
4066          (gnus-sort-articles gnus-newsgroup-headers))))
4067     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4068     ;; Call hooks for modifying summary buffer.
4069     (goto-char (point-min))
4070     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4071
4072 (defsubst gnus-general-simplify-subject (subject)
4073   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4074   (setq subject
4075         (cond
4076          ;; Truncate the subject.
4077          (gnus-simplify-subject-functions
4078           (gnus-map-function gnus-simplify-subject-functions subject))
4079          ((numberp gnus-summary-gather-subject-limit)
4080           (setq subject (gnus-simplify-subject-re subject))
4081           (if (> (length subject) gnus-summary-gather-subject-limit)
4082               (substring subject 0 gnus-summary-gather-subject-limit)
4083             subject))
4084          ;; Fuzzily simplify it.
4085          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4086           (gnus-simplify-subject-fuzzy subject))
4087          ;; Just remove the leading "Re:".
4088          (t
4089           (gnus-simplify-subject-re subject))))
4090
4091   (if (and gnus-summary-gather-exclude-subject
4092            (string-match gnus-summary-gather-exclude-subject subject))
4093       nil                         ; This article shouldn't be gathered
4094     subject))
4095
4096 (defun gnus-summary-simplify-subject-query ()
4097   "Query where the respool algorithm would put this article."
4098   (interactive)
4099   (gnus-summary-select-article)
4100   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4101
4102 (defun gnus-gather-threads-by-subject (threads)
4103   "Gather threads by looking at Subject headers."
4104   (if (not gnus-summary-make-false-root)
4105       threads
4106     (let ((hashtb (gnus-make-hashtable 1024))
4107           (prev threads)
4108           (result threads)
4109           subject hthread whole-subject)
4110       (while threads
4111         (setq subject (gnus-general-simplify-subject
4112                        (setq whole-subject (mail-header-subject
4113                                             (caar threads)))))
4114         (when subject
4115           (if (setq hthread (gnus-gethash subject hashtb))
4116               (progn
4117                 ;; We enter a dummy root into the thread, if we
4118                 ;; haven't done that already.
4119                 (unless (stringp (caar hthread))
4120                   (setcar hthread (list whole-subject (car hthread))))
4121                 ;; We add this new gathered thread to this gathered
4122                 ;; thread.
4123                 (setcdr (car hthread)
4124                         (nconc (cdar hthread) (list (car threads))))
4125                 ;; Remove it from the list of threads.
4126                 (setcdr prev (cdr threads))
4127                 (setq threads prev))
4128             ;; Enter this thread into the hash table.
4129             (gnus-sethash subject
4130                           (if gnus-summary-make-false-root-always
4131                               (progn
4132                                 ;; If you want a dummy root above all
4133                                 ;; threads...
4134                                 (setcar threads (list whole-subject
4135                                                       (car threads)))
4136                                 threads)
4137                             threads)
4138                           hashtb)))
4139         (setq prev threads)
4140         (setq threads (cdr threads)))
4141       result)))
4142
4143 (defun gnus-gather-threads-by-references (threads)
4144   "Gather threads by looking at References headers."
4145   (let ((idhashtb (gnus-make-hashtable 1024))
4146         (thhashtb (gnus-make-hashtable 1024))
4147         (prev threads)
4148         (result threads)
4149         ids references id gthread gid entered ref)
4150     (while threads
4151       (when (setq references (mail-header-references (caar threads)))
4152         (setq id (mail-header-id (caar threads))
4153               ids (inline (gnus-split-references references))
4154               entered nil)
4155         (while (setq ref (pop ids))
4156           (setq ids (delete ref ids))
4157           (if (not (setq gid (gnus-gethash ref idhashtb)))
4158               (progn
4159                 (gnus-sethash ref id idhashtb)
4160                 (gnus-sethash id threads thhashtb))
4161             (setq gthread (gnus-gethash gid thhashtb))
4162             (unless entered
4163               ;; We enter a dummy root into the thread, if we
4164               ;; haven't done that already.
4165               (unless (stringp (caar gthread))
4166                 (setcar gthread (list (mail-header-subject (caar gthread))
4167                                       (car gthread))))
4168               ;; We add this new gathered thread to this gathered
4169               ;; thread.
4170               (setcdr (car gthread)
4171                       (nconc (cdar gthread) (list (car threads)))))
4172             ;; Add it into the thread hash table.
4173             (gnus-sethash id gthread thhashtb)
4174             (setq entered t)
4175             ;; Remove it from the list of threads.
4176             (setcdr prev (cdr threads))
4177             (setq threads prev))))
4178       (setq prev threads)
4179       (setq threads (cdr threads)))
4180     result))
4181
4182 (defun gnus-sort-gathered-threads (threads)
4183   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4184   (let ((result threads))
4185     (while threads
4186       (when (stringp (caar threads))
4187         (setcdr (car threads)
4188                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4189       (setq threads (cdr threads)))
4190     result))
4191
4192 (defun gnus-thread-loop-p (root thread)
4193   "Say whether ROOT is in THREAD."
4194   (let ((stack (list thread))
4195         (infloop 0)
4196         th)
4197     (while (setq thread (pop stack))
4198       (setq th (cdr thread))
4199       (while (and th
4200                   (not (eq (caar th) root)))
4201         (pop th))
4202       (if th
4203           ;; We have found a loop.
4204           (let (ref-dep)
4205             (setcdr thread (delq (car th) (cdr thread)))
4206             (if (boundp (setq ref-dep (intern "none"
4207                                               gnus-newsgroup-dependencies)))
4208                 (setcdr (symbol-value ref-dep)
4209                         (nconc (cdr (symbol-value ref-dep))
4210                                (list (car th))))
4211               (set ref-dep (list nil (car th))))
4212             (setq infloop 1
4213                   stack nil))
4214         ;; Push all the subthreads onto the stack.
4215         (push (cdr thread) stack)))
4216     infloop))
4217
4218 (defun gnus-make-threads ()
4219   "Go through the dependency hashtb and find the roots.  Return all threads."
4220   (let (threads)
4221     (while (catch 'infloop
4222              (mapatoms
4223               (lambda (refs)
4224                 ;; Deal with self-referencing References loops.
4225                 (when (and (car (symbol-value refs))
4226                            (not (zerop
4227                                  (apply
4228                                   '+
4229                                   (mapcar
4230                                    (lambda (thread)
4231                                      (gnus-thread-loop-p
4232                                       (car (symbol-value refs)) thread))
4233                                    (cdr (symbol-value refs)))))))
4234                   (setq threads nil)
4235                   (throw 'infloop t))
4236                 (unless (car (symbol-value refs))
4237                   ;; These threads do not refer back to any other
4238                   ;; articles, so they're roots.
4239                   (setq threads (append (cdr (symbol-value refs)) threads))))
4240               gnus-newsgroup-dependencies)))
4241     threads))
4242
4243 ;; Build the thread tree.
4244 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4245   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4246
4247 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4248 if it was already present.
4249
4250 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4251 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4252 Message-IDs will be renamed to a unique Message-ID before being
4253 entered.
4254
4255 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4256   (let* ((id (mail-header-id header))
4257          (id-dep (and id (intern id dependencies)))
4258          parent-id ref ref-dep ref-header replaced)
4259     ;; Enter this `header' in the `dependencies' table.
4260     (cond
4261      ((not id-dep)
4262       (setq header nil))
4263      ;; The first two cases do the normal part: enter a new `header'
4264      ;; in the `dependencies' table.
4265      ((not (boundp id-dep))
4266       (set id-dep (list header)))
4267      ((null (car (symbol-value id-dep)))
4268       (setcar (symbol-value id-dep) header))
4269
4270      ;; From here the `header' was already present in the
4271      ;; `dependencies' table.
4272      (force-new
4273       ;; Overrides an existing entry;
4274       ;; just set the header part of the entry.
4275       (setcar (symbol-value id-dep) header)
4276       (setq replaced t))
4277
4278      ;; Renames the existing `header' to a unique Message-ID.
4279      ((not gnus-summary-ignore-duplicates)
4280       ;; An article with this Message-ID has already been seen.
4281       ;; We rename the Message-ID.
4282       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4283            (list header))
4284       (mail-header-set-id header id))
4285
4286      ;; The last case ignores an existing entry, except it adds any
4287      ;; additional Xrefs (in case the two articles came from different
4288      ;; servers.
4289      ;; Also sets `header' to `nil' meaning that the `dependencies'
4290      ;; table was *not* modified.
4291      (t
4292       (mail-header-set-xref
4293        (car (symbol-value id-dep))
4294        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4295                    "")
4296                (or (mail-header-xref header) "")))
4297       (setq header nil)))
4298
4299     (when (and header (not replaced))
4300       ;; First check that we are not creating a References loop.
4301       (setq parent-id (gnus-parent-id (mail-header-references header)))
4302       (setq ref parent-id)
4303       (while (and ref
4304                   (setq ref-dep (intern-soft ref dependencies))
4305                   (boundp ref-dep)
4306                   (setq ref-header (car (symbol-value ref-dep))))
4307         (if (string= id ref)
4308             ;; Yuk!  This is a reference loop.  Make the article be a
4309             ;; root article.
4310             (progn
4311               (mail-header-set-references (car (symbol-value id-dep)) "none")
4312               (setq ref nil)
4313               (setq parent-id nil))
4314           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4315       (setq ref-dep (intern (or parent-id "none") dependencies))
4316       (if (boundp ref-dep)
4317           (setcdr (symbol-value ref-dep)
4318                   (nconc (cdr (symbol-value ref-dep))
4319                          (list (symbol-value id-dep))))
4320         (set ref-dep (list nil (symbol-value id-dep)))))
4321     header))
4322
4323 (defun gnus-extract-message-id-from-in-reply-to (string)
4324   (if (string-match "<[^>]+>" string)
4325       (substring string (match-beginning 0) (match-end 0))
4326     nil))
4327
4328 (defun gnus-build-sparse-threads ()
4329   (let ((headers gnus-newsgroup-headers)
4330         (mail-parse-charset gnus-newsgroup-charset)
4331         (gnus-summary-ignore-duplicates t)
4332         header references generation relations
4333         subject child end new-child date)
4334     ;; First we create an alist of generations/relations, where
4335     ;; generations is how much we trust the relation, and the relation
4336     ;; is parent/child.
4337     (gnus-message 7 "Making sparse threads...")
4338     (save-excursion
4339       (nnheader-set-temp-buffer " *gnus sparse threads*")
4340       (while (setq header (pop headers))
4341         (when (and (setq references (mail-header-references header))
4342                    (not (string= references "")))
4343           (insert references)
4344           (setq child (mail-header-id header)
4345                 subject (mail-header-subject header)
4346                 date (mail-header-date header)
4347                 generation 0)
4348           (while (search-backward ">" nil t)
4349             (setq end (1+ (point)))
4350             (when (search-backward "<" nil t)
4351               (setq new-child (buffer-substring (point) end))
4352               (push (list (incf generation)
4353                           child (setq child new-child)
4354                           subject date)
4355                     relations)))
4356           (when child
4357             (push (list (1+ generation) child nil subject) relations))
4358           (erase-buffer)))
4359       (kill-buffer (current-buffer)))
4360     ;; Sort over trustworthiness.
4361     (dolist (relation (sort relations 'car-less-than-car))
4362       (when (gnus-dependencies-add-header
4363              (make-full-mail-header
4364               gnus-reffed-article-number
4365               (nth 3 relation) "" (or (nth 4 relation) "")
4366               (nth 1 relation)
4367               (or (nth 2 relation) "") 0 0 "")
4368              gnus-newsgroup-dependencies nil)
4369         (push gnus-reffed-article-number gnus-newsgroup-limit)
4370         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4371         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4372               gnus-newsgroup-reads)
4373         (decf gnus-reffed-article-number)))
4374     (gnus-message 7 "Making sparse threads...done")))
4375
4376 (defun gnus-build-old-threads ()
4377   ;; Look at all the articles that refer back to old articles, and
4378   ;; fetch the headers for the articles that aren't there.  This will
4379   ;; build complete threads - if the roots haven't been expired by the
4380   ;; server, that is.
4381   (let ((mail-parse-charset gnus-newsgroup-charset)
4382         id heads)
4383     (mapatoms
4384      (lambda (refs)
4385        (when (not (car (symbol-value refs)))
4386          (setq heads (cdr (symbol-value refs)))
4387          (while heads
4388            (if (memq (mail-header-number (caar heads))
4389                      gnus-newsgroup-dormant)
4390                (setq heads (cdr heads))
4391              (setq id (symbol-name refs))
4392              (while (and (setq id (gnus-build-get-header id))
4393                          (not (car (gnus-id-to-thread id)))))
4394              (setq heads nil)))))
4395      gnus-newsgroup-dependencies)))
4396
4397 (defsubst gnus-remove-odd-characters (string)
4398   "Translate STRING into something that doesn't contain weird characters."
4399   (mm-subst-char-in-string
4400    ?\r ?\-
4401    (mm-subst-char-in-string ?\n ?\- string t) t))
4402
4403 ;; This function has to be called with point after the article number
4404 ;; on the beginning of the line.
4405 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4406   (let ((eol (point-at-eol))
4407         (buffer (current-buffer))
4408         header references in-reply-to)
4409
4410     ;; overview: [num subject from date id refs chars lines misc]
4411     (unwind-protect
4412         (let (x)
4413           (narrow-to-region (point) eol)
4414           (unless (eobp)
4415             (forward-char))
4416
4417           (setq header
4418                 (make-full-mail-header
4419                  number                 ; number
4420                  (condition-case ()     ; subject
4421                      (gnus-remove-odd-characters
4422                       (funcall gnus-decode-encoded-word-function
4423                                (setq x (nnheader-nov-field))))
4424                    (error x))
4425                  (condition-case ()     ; from
4426                      (gnus-remove-odd-characters
4427                       (funcall gnus-decode-encoded-address-function
4428                                (setq x (nnheader-nov-field))))
4429                    (error x))
4430                  (nnheader-nov-field)   ; date
4431                  (nnheader-nov-read-message-id number)  ; id
4432                  (setq references (nnheader-nov-field)) ; refs
4433                  (nnheader-nov-read-integer) ; chars
4434                  (nnheader-nov-read-integer) ; lines
4435                  (unless (eobp)
4436                    (if (looking-at "Xref: ")
4437                        (goto-char (match-end 0)))
4438                    (nnheader-nov-field)) ; Xref
4439                  (nnheader-nov-parse-extra)))) ; extra
4440
4441       (widen))
4442
4443     (when (and (string= references "")
4444                (setq in-reply-to (mail-header-extra header))
4445                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4446       (mail-header-set-references
4447        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4448
4449     (when gnus-alter-header-function
4450       (funcall gnus-alter-header-function header))
4451     (gnus-dependencies-add-header header dependencies force-new)))
4452
4453 (defun gnus-build-get-header (id)
4454   "Look through the buffer of NOV lines and find the header to ID.
4455 Enter this line into the dependencies hash table, and return
4456 the id of the parent article (if any)."
4457   (let ((deps gnus-newsgroup-dependencies)
4458         found header)
4459     (prog1
4460         (with-current-buffer nntp-server-buffer
4461           (let ((case-fold-search nil))
4462             (goto-char (point-min))
4463             (while (and (not found)
4464                         (search-forward id nil t))
4465               (beginning-of-line)
4466               (setq found (looking-at
4467                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4468                                    (regexp-quote id))))
4469               (or found (beginning-of-line 2)))
4470             (when found
4471               (beginning-of-line)
4472               (and
4473                (setq header (gnus-nov-parse-line
4474                              (read (current-buffer)) deps))
4475                (gnus-parent-id (mail-header-references header))))))
4476       (when header
4477         (let ((number (mail-header-number header)))
4478           (push number gnus-newsgroup-limit)
4479           (push header gnus-newsgroup-headers)
4480           (if (memq number gnus-newsgroup-unselected)
4481               (progn
4482                 (setq gnus-newsgroup-unreads
4483                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4484                                                number))
4485                 (setq gnus-newsgroup-unselected
4486                       (delq number gnus-newsgroup-unselected)))
4487             (push number gnus-newsgroup-ancient)))))))
4488
4489 (defun gnus-build-all-threads ()
4490   "Read all the headers."
4491   (let ((gnus-summary-ignore-duplicates t)
4492         (mail-parse-charset gnus-newsgroup-charset)
4493         (dependencies gnus-newsgroup-dependencies)
4494         header article)
4495     (with-current-buffer nntp-server-buffer
4496       (let ((case-fold-search nil))
4497         (goto-char (point-min))
4498         (while (not (eobp))
4499           (ignore-errors
4500             (setq article (read (current-buffer))
4501                   header (gnus-nov-parse-line article dependencies)))
4502           (when header
4503             (with-current-buffer gnus-summary-buffer
4504               (push header gnus-newsgroup-headers)
4505               (if (memq (setq article (mail-header-number header))
4506                         gnus-newsgroup-unselected)
4507                   (progn
4508                     (setq gnus-newsgroup-unreads
4509                           (gnus-add-to-sorted-list
4510                            gnus-newsgroup-unreads article))
4511                     (setq gnus-newsgroup-unselected
4512                           (delq article gnus-newsgroup-unselected)))
4513                 (push article gnus-newsgroup-ancient)))
4514             (forward-line 1)))))))
4515
4516 (defun gnus-summary-update-article-line (article header)
4517   "Update the line for ARTICLE using HEADER."
4518   (let* ((id (mail-header-id header))
4519          (thread (gnus-id-to-thread id)))
4520     (unless thread
4521       (error "Article in no thread"))
4522     ;; Update the thread.
4523     (setcar thread header)
4524     (gnus-summary-goto-subject article)
4525     (let* ((datal (gnus-data-find-list article))
4526            (data (car datal))
4527            (inhibit-read-only t)
4528            (level (gnus-summary-thread-level)))
4529       (gnus-delete-line)
4530       (let ((inserted (- (point)
4531                          (progn
4532                            (gnus-summary-insert-line
4533                             header level nil
4534                             (memq article gnus-newsgroup-undownloaded)
4535                             (gnus-article-mark article)
4536                             (memq article gnus-newsgroup-replied)
4537                             (memq article gnus-newsgroup-expirable)
4538                             ;; Only insert the Subject string when it's different
4539                             ;; from the previous Subject string.
4540                             (if (and
4541                                  gnus-show-threads
4542                                  (gnus-subject-equal
4543                                   (condition-case ()
4544                                       (mail-header-subject
4545                                        (gnus-data-header
4546                                         (cadr
4547                                          (gnus-data-find-list
4548                                           article
4549                                           (gnus-data-list t)))))
4550                                     ;; Error on the side of excessive subjects.
4551                                     (error ""))
4552                                   (mail-header-subject header)))
4553                                 ""
4554                               (mail-header-subject header))
4555                             nil (cdr (assq article gnus-newsgroup-scored))
4556                             (memq article gnus-newsgroup-processable))
4557                            (point)))))
4558         (when (cdr datal)
4559           (gnus-data-update-list
4560            (cdr datal)
4561            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4562
4563 (defun gnus-summary-update-article (article &optional iheader)
4564   "Update ARTICLE in the summary buffer."
4565   (set-buffer gnus-summary-buffer)
4566   (let* ((header (gnus-summary-article-header article))
4567          (id (mail-header-id header))
4568          (data (gnus-data-find article))
4569          (thread (gnus-id-to-thread id))
4570          (references (mail-header-references header))
4571          (parent
4572           (gnus-id-to-thread
4573            (or (gnus-parent-id
4574                 (when (and references
4575                            (not (equal "" references)))
4576                   references))
4577                "none")))
4578          (inhibit-read-only t)
4579          (old (car thread)))
4580     (when thread
4581       (unless iheader
4582         (setcar thread nil)
4583         (when parent
4584           (delq thread parent)))
4585       (if (gnus-summary-insert-subject id header)
4586           ;; Set the (possibly) new article number in the data structure.
4587           (gnus-data-set-number data (gnus-id-to-article id))
4588         (setcar thread old)
4589         nil))))
4590
4591 (defun gnus-rebuild-thread (id &optional line)
4592   "Rebuild the thread containing ID.
4593 If LINE, insert the rebuilt thread starting on line LINE."
4594   (let ((inhibit-read-only t)
4595         old-pos current thread data)
4596     (if (not gnus-show-threads)
4597         (setq thread (list (car (gnus-id-to-thread id))))
4598       ;; Get the thread this article is part of.
4599       (setq thread (gnus-remove-thread id)))
4600     (setq old-pos (point-at-bol))
4601     (setq current (save-excursion
4602                     (and (re-search-backward "[\r\n]" nil t)
4603                          (gnus-summary-article-number))))
4604     ;; If this is a gathered thread, we have to go some re-gathering.
4605     (when (stringp (car thread))
4606       (let ((subject (car thread))
4607             roots thr)
4608         (setq thread (cdr thread))
4609         (while thread
4610           (unless (memq (setq thr (gnus-id-to-thread
4611                                    (gnus-root-id
4612                                     (mail-header-id (caar thread)))))
4613                         roots)
4614             (push thr roots))
4615           (setq thread (cdr thread)))
4616         ;; We now have all (unique) roots.
4617         (if (= (length roots) 1)
4618             ;; All the loose roots are now one solid root.
4619             (setq thread (car roots))
4620           (setq thread (cons subject (gnus-sort-threads roots))))))
4621     (let (threads)
4622       ;; We then insert this thread into the summary buffer.
4623       (when line
4624         (goto-char (point-min))
4625         (forward-line (1- line)))
4626       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4627         (if gnus-show-threads
4628             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4629           (gnus-summary-prepare-unthreaded thread))
4630         (setq data (nreverse gnus-newsgroup-data))
4631         (setq threads gnus-newsgroup-threads))
4632       ;; We splice the new data into the data structure.
4633       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4634       ;;!!! then we want to insert at the beginning of the buffer.
4635       ;;!!! That happens to be true with Gnus now, but that may
4636       ;;!!! change in the future.  Perhaps.
4637       (gnus-data-enter-list
4638        (if line nil current) data (- (point) old-pos))
4639       (setq gnus-newsgroup-threads
4640             (nconc threads gnus-newsgroup-threads))
4641       (gnus-data-compute-positions))))
4642
4643 (defun gnus-number-to-header (number)
4644   "Return the header for article NUMBER."
4645   (let ((headers gnus-newsgroup-headers))
4646     (while (and headers
4647                 (not (= number (mail-header-number (car headers)))))
4648       (pop headers))
4649     (when headers
4650       (car headers))))
4651
4652 (defun gnus-parent-headers (in-headers &optional generation)
4653   "Return the headers of the GENERATIONeth parent of HEADERS."
4654   (unless generation
4655     (setq generation 1))
4656   (let ((parent t)
4657         (headers in-headers)
4658         references)
4659     (while (and parent
4660                 (not (zerop generation))
4661                 (setq references (mail-header-references headers)))
4662       (setq headers (if (and references
4663                              (setq parent (gnus-parent-id references)))
4664                         (car (gnus-id-to-thread parent))
4665                       nil))
4666       (decf generation))
4667     (and (not (eq headers in-headers))
4668          headers)))
4669
4670 (defun gnus-id-to-thread (id)
4671   "Return the (sub-)thread where ID appears."
4672   (gnus-gethash id gnus-newsgroup-dependencies))
4673
4674 (defun gnus-id-to-article (id)
4675   "Return the article number of ID."
4676   (let ((thread (gnus-id-to-thread id)))
4677     (when (and thread
4678                (car thread))
4679       (mail-header-number (car thread)))))
4680
4681 (defun gnus-id-to-header (id)
4682   "Return the article headers of ID."
4683   (car (gnus-id-to-thread id)))
4684
4685 (defun gnus-article-displayed-root-p (article)
4686   "Say whether ARTICLE is a root(ish) article."
4687   (let ((level (gnus-summary-thread-level article))
4688         (refs (mail-header-references  (gnus-summary-article-header article)))
4689         particle)
4690     (cond
4691      ((null level) nil)
4692      ((zerop level) t)
4693      ((null refs) t)
4694      ((null (gnus-parent-id refs)) t)
4695      ((and (= 1 level)
4696            (null (setq particle (gnus-id-to-article
4697                                  (gnus-parent-id refs))))
4698            (null (gnus-summary-thread-level particle)))))))
4699
4700 (defun gnus-root-id (id)
4701   "Return the id of the root of the thread where ID appears."
4702   (let (last-id prev)
4703     (while (and id (setq prev (car (gnus-id-to-thread id))))
4704       (setq last-id id
4705             id (gnus-parent-id (mail-header-references prev))))
4706     last-id))
4707
4708 (defun gnus-articles-in-thread (thread)
4709   "Return the list of articles in THREAD."
4710   (cons (mail-header-number (car thread))
4711         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4712
4713 (defun gnus-remove-thread (id &optional dont-remove)
4714   "Remove the thread that has ID in it."
4715   (let (headers thread last-id)
4716     ;; First go up in this thread until we find the root.
4717     (setq last-id (gnus-root-id id)
4718           headers (message-flatten-list (gnus-id-to-thread last-id)))
4719     ;; We have now found the real root of this thread.  It might have
4720     ;; been gathered into some loose thread, so we have to search
4721     ;; through the threads to find the thread we wanted.
4722     (let ((threads gnus-newsgroup-threads)
4723           sub)
4724       (while threads
4725         (setq sub (car threads))
4726         (if (stringp (car sub))
4727             ;; This is a gathered thread, so we look at the roots
4728             ;; below it to find whether this article is in this
4729             ;; gathered root.
4730             (progn
4731               (setq sub (cdr sub))
4732               (while sub
4733                 (when (member (caar sub) headers)
4734                   (setq thread (car threads)
4735                         threads nil
4736                         sub nil))
4737                 (setq sub (cdr sub))))
4738           ;; It's an ordinary thread, so we check it.
4739           (when (eq (car sub) (car headers))
4740             (setq thread sub
4741                   threads nil)))
4742         (setq threads (cdr threads)))
4743       ;; If this article is in no thread, then it's a root.
4744       (if thread
4745           (unless dont-remove
4746             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4747         (setq thread (gnus-id-to-thread last-id)))
4748       (when thread
4749         (prog1
4750             thread                      ; We return this thread.
4751           (unless dont-remove
4752             (if (stringp (car thread))
4753                 (progn
4754                   ;; If we use dummy roots, then we have to remove the
4755                   ;; dummy root as well.
4756                   (when (eq gnus-summary-make-false-root 'dummy)
4757                     ;; We go to the dummy root by going to
4758                     ;; the first sub-"thread", and then one line up.
4759                     (gnus-summary-goto-article
4760                      (mail-header-number (caadr thread)))
4761                     (forward-line -1)
4762                     (gnus-delete-line)
4763                     (gnus-data-compute-positions))
4764                   (setq thread (cdr thread))
4765                   (while thread
4766                     (gnus-remove-thread-1 (car thread))
4767                     (setq thread (cdr thread))))
4768               (gnus-remove-thread-1 thread))))))))
4769
4770 (defun gnus-remove-thread-1 (thread)
4771   "Remove the thread THREAD recursively."
4772   (let ((number (mail-header-number (pop thread)))
4773         d)
4774     (setq thread (reverse thread))
4775     (while thread
4776       (gnus-remove-thread-1 (pop thread)))
4777     (when (setq d (gnus-data-find number))
4778       (goto-char (gnus-data-pos d))
4779       (gnus-summary-show-thread)
4780       (gnus-data-remove
4781        number
4782        (- (point-at-bol)
4783           (prog1
4784               (1+ (point-at-eol))
4785             (gnus-delete-line)))))))
4786
4787 (defun gnus-sort-threads-recursive (threads func)
4788   (sort (mapcar (lambda (thread)
4789                   (cons (car thread)
4790                         (and (cdr thread)
4791                              (gnus-sort-threads-recursive (cdr thread) func))))
4792                 threads) func))
4793
4794 (defun gnus-sort-threads-loop (threads func)
4795   (let* ((superthread (cons nil threads))
4796          (stack (list (cons superthread threads)))
4797          remaining-threads thread)
4798     (while stack
4799       (setq remaining-threads (cdr (car stack)))
4800       (if remaining-threads
4801           (progn (setq thread (car remaining-threads))
4802                  (setcdr (car stack) (cdr remaining-threads))
4803                  (if (cdr thread)
4804                      (push (cons thread (cdr thread)) stack)))
4805         (setq thread (caar stack))
4806         (setcdr thread (sort (cdr thread) func))
4807         (pop stack)))
4808     (cdr superthread)))
4809
4810 (defun gnus-sort-threads (threads)
4811   "Sort THREADS."
4812   (if (not gnus-thread-sort-functions)
4813       threads
4814     (gnus-message 8 "Sorting threads...")
4815     (prog1
4816         (condition-case nil
4817             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4818               (gnus-sort-threads-recursive
4819                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4820           ;; Even after binding max-lisp-eval-depth, the recursive
4821           ;; sorter might fail for very long threads.  In that case,
4822           ;; try using a (less well-tested) non-recursive sorter.
4823           (error (gnus-sort-threads-loop
4824                   threads (gnus-make-sort-function
4825                            gnus-thread-sort-functions))))
4826       (gnus-message 8 "Sorting threads...done"))))
4827
4828 (defun gnus-sort-articles (articles)
4829   "Sort ARTICLES."
4830   (when gnus-article-sort-functions
4831     (gnus-message 7 "Sorting articles...")
4832     (prog1
4833         (setq gnus-newsgroup-headers
4834               (sort articles (gnus-make-sort-function
4835                               gnus-article-sort-functions)))
4836       (gnus-message 7 "Sorting articles...done"))))
4837
4838 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4839 (defmacro gnus-thread-header (thread)
4840   "Return header of first article in THREAD.
4841 Note that THREAD must never, ever be anything else than a variable -
4842 using some other form will lead to serious barfage."
4843   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4844   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4845   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4846         (vector thread) 2))
4847
4848 (defsubst gnus-article-sort-by-number (h1 h2)
4849   "Sort articles by article number."
4850   (< (mail-header-number h1)
4851      (mail-header-number h2)))
4852
4853 (defun gnus-thread-sort-by-number (h1 h2)
4854   "Sort threads by root article number."
4855   (gnus-article-sort-by-number
4856    (gnus-thread-header h1) (gnus-thread-header h2)))
4857
4858 (defsubst gnus-article-sort-by-random (h1 h2)
4859   "Sort articles randomly."
4860   (zerop (random 2)))
4861
4862 (defun gnus-thread-sort-by-random (h1 h2)
4863   "Sort threads randomly."
4864   (gnus-article-sort-by-random
4865    (gnus-thread-header h1) (gnus-thread-header h2)))
4866
4867 (defsubst gnus-article-sort-by-lines (h1 h2)
4868   "Sort articles by article Lines header."
4869   (< (mail-header-lines h1)
4870      (mail-header-lines h2)))
4871
4872 (defun gnus-thread-sort-by-lines (h1 h2)
4873   "Sort threads by root article Lines header."
4874   (gnus-article-sort-by-lines
4875    (gnus-thread-header h1) (gnus-thread-header h2)))
4876
4877 (defsubst gnus-article-sort-by-chars (h1 h2)
4878   "Sort articles by octet length."
4879   (< (mail-header-chars h1)
4880      (mail-header-chars h2)))
4881
4882 (defun gnus-thread-sort-by-chars (h1 h2)
4883   "Sort threads by root article octet length."
4884   (gnus-article-sort-by-chars
4885    (gnus-thread-header h1) (gnus-thread-header h2)))
4886
4887 (defsubst gnus-article-sort-by-author (h1 h2)
4888   "Sort articles by root author."
4889   (gnus-string<
4890    (let ((extract (funcall
4891                    gnus-extract-address-components
4892                    (mail-header-from h1))))
4893      (or (car extract) (cadr extract) ""))
4894    (let ((extract (funcall
4895                    gnus-extract-address-components
4896                    (mail-header-from h2))))
4897      (or (car extract) (cadr extract) ""))))
4898
4899 (defun gnus-thread-sort-by-author (h1 h2)
4900   "Sort threads by root author."
4901   (gnus-article-sort-by-author
4902    (gnus-thread-header h1)  (gnus-thread-header h2)))
4903
4904 (defsubst gnus-article-sort-by-recipient (h1 h2)
4905   "Sort articles by recipient."
4906   (gnus-string<
4907    (let ((extract (funcall
4908                    gnus-extract-address-components
4909                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4910      (or (car extract) (cadr extract)))
4911    (let ((extract (funcall
4912                    gnus-extract-address-components
4913                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4914      (or (car extract) (cadr extract)))))
4915
4916 (defun gnus-thread-sort-by-recipient (h1 h2)
4917   "Sort threads by root recipient."
4918   (gnus-article-sort-by-recipient
4919    (gnus-thread-header h1) (gnus-thread-header h2)))
4920
4921 (defsubst gnus-article-sort-by-subject (h1 h2)
4922   "Sort articles by root subject."
4923   (gnus-string<
4924    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4925    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4926
4927 (defun gnus-thread-sort-by-subject (h1 h2)
4928   "Sort threads by root subject."
4929   (gnus-article-sort-by-subject
4930    (gnus-thread-header h1) (gnus-thread-header h2)))
4931
4932 (defsubst gnus-article-sort-by-date (h1 h2)
4933   "Sort articles by root article date."
4934   (time-less-p
4935    (gnus-date-get-time (mail-header-date h1))
4936    (gnus-date-get-time (mail-header-date h2))))
4937
4938 (defun gnus-thread-sort-by-date (h1 h2)
4939   "Sort threads by root article date."
4940   (gnus-article-sort-by-date
4941    (gnus-thread-header h1) (gnus-thread-header h2)))
4942
4943 (defsubst gnus-article-sort-by-score (h1 h2)
4944   "Sort articles by root article score.
4945 Unscored articles will be counted as having a score of zero."
4946   (> (or (cdr (assq (mail-header-number h1)
4947                     gnus-newsgroup-scored))
4948          gnus-summary-default-score 0)
4949      (or (cdr (assq (mail-header-number h2)
4950                     gnus-newsgroup-scored))
4951          gnus-summary-default-score 0)))
4952
4953 (defun gnus-thread-sort-by-score (h1 h2)
4954   "Sort threads by root article score."
4955   (gnus-article-sort-by-score
4956    (gnus-thread-header h1) (gnus-thread-header h2)))
4957
4958 (defun gnus-thread-sort-by-total-score (h1 h2)
4959   "Sort threads by the sum of all scores in the thread.
4960 Unscored articles will be counted as having a score of zero."
4961   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4962
4963 (defun gnus-thread-total-score (thread)
4964   ;; This function find the total score of THREAD.
4965   (cond
4966    ((null thread)
4967     0)
4968    ((consp thread)
4969     (if (stringp (car thread))
4970         (apply gnus-thread-score-function 0
4971                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4972       (gnus-thread-total-score-1 thread)))
4973    (t
4974     (gnus-thread-total-score-1 (list thread)))))
4975
4976 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4977   "Sort threads such that the thread with the most recently arrived article comes first."
4978   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4979
4980 (defun gnus-thread-highest-number (thread)
4981   "Return the highest article number in THREAD."
4982   (apply 'max (mapcar (lambda (header)
4983                         (mail-header-number header))
4984                       (message-flatten-list thread))))
4985
4986 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4987   "Sort threads such that the thread with the most recently dated article comes first."
4988   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4989
4990 (defun gnus-thread-latest-date (thread)
4991   "Return the highest article date in THREAD."
4992   (let ((previous-time 0))
4993     (apply 'max
4994            (mapcar
4995             (lambda (header)
4996               (setq previous-time
4997                     (condition-case ()
4998                         (gnus-float-time (mail-header-parse-date
4999                                           (mail-header-date header)))
5000                       (error previous-time))))
5001             (sort
5002              (message-flatten-list thread)
5003              (lambda (h1 h2)
5004                (< (mail-header-number h1)
5005                   (mail-header-number h2))))))))
5006
5007 (defun gnus-thread-total-score-1 (root)
5008   ;; This function find the total score of the thread below ROOT.
5009   (setq root (car root))
5010   (apply gnus-thread-score-function
5011          (or (append
5012               (mapcar 'gnus-thread-total-score
5013                       (cdr (gnus-id-to-thread (mail-header-id root))))
5014               (when (> (mail-header-number root) 0)
5015                 (list (or (cdr (assq (mail-header-number root)
5016                                      gnus-newsgroup-scored))
5017                           gnus-summary-default-score 0))))
5018              (list gnus-summary-default-score)
5019              '(0))))
5020
5021 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5022 (defvar gnus-tmp-prev-subject nil)
5023 (defvar gnus-tmp-false-parent nil)
5024 (defvar gnus-tmp-root-expunged nil)
5025 (defvar gnus-tmp-dummy-line nil)
5026
5027 (defun gnus-extra-header (type &optional header)
5028   "Return the extra header of TYPE."
5029   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5030       ""))
5031
5032 (defvar gnus-tmp-thread-tree-header-string "")
5033
5034 (defcustom gnus-sum-thread-tree-root "> "
5035   "With %B spec, used for the root of a thread.
5036 If nil, use subject instead."
5037   :version "22.1"
5038   :type '(radio (const :format "%v  " nil) string)
5039   :group 'gnus-thread)
5040
5041 (defcustom gnus-sum-thread-tree-false-root "> "
5042   "With %B spec, used for a false 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-single-indent ""
5049   "With %B spec, used for a thread with just one message.
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-vertical "| "
5056   "With %B spec, used for drawing a vertical line."
5057   :version "22.1"
5058   :type 'string
5059   :group 'gnus-thread)
5060
5061 (defcustom gnus-sum-thread-tree-indent "  "
5062   "With %B spec, used for indenting."
5063   :version "22.1"
5064   :type 'string
5065   :group 'gnus-thread)
5066
5067 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5068   "With %B spec, used for a leaf with brothers."
5069   :version "22.1"
5070   :type 'string
5071   :group 'gnus-thread)
5072
5073 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5074   "With %B spec, used for a leaf without brothers."
5075   :version "22.1"
5076   :type 'string
5077   :group 'gnus-thread)
5078
5079 (defcustom gnus-summary-display-while-building nil
5080   "If non-nil, show and update the summary buffer as it's being built.
5081 If the value is t, update the buffer after every line is inserted.  If
5082 the value is an integer (N), update the display every N lines."
5083   :version "22.1"
5084   :group 'gnus-thread
5085   :type '(choice (const :tag "off" nil)
5086                  number
5087                  (const :tag "frequently" t)))
5088
5089 (defun gnus-summary-prepare-threads (threads)
5090   "Prepare summary buffer from THREADS and indentation LEVEL.
5091 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5092 or a straight list of headers."
5093   (gnus-message 7 "Generating summary...")
5094
5095   (setq gnus-newsgroup-threads threads)
5096   (beginning-of-line)
5097
5098   (let ((gnus-tmp-level 0)
5099         (default-score (or gnus-summary-default-score 0))
5100         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5101         (building-line-count gnus-summary-display-while-building)
5102         (building-count (integerp gnus-summary-display-while-building))
5103         thread number subject stack state gnus-tmp-gathered beg-match
5104         new-roots gnus-tmp-new-adopts thread-end simp-subject
5105         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5106         gnus-tmp-replied gnus-tmp-subject-or-nil
5107         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5108         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5109         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5110         tree-stack)
5111
5112     (setq gnus-tmp-prev-subject nil
5113           gnus-tmp-thread-tree-header-string "")
5114
5115     (if (vectorp (car threads))
5116         ;; If this is a straight (sic) list of headers, then a
5117         ;; threaded summary display isn't required, so we just create
5118         ;; an unthreaded one.
5119         (gnus-summary-prepare-unthreaded threads)
5120
5121       ;; Do the threaded display.
5122
5123       (if gnus-summary-display-while-building
5124           (switch-to-buffer (buffer-name)))
5125       (while (or threads stack gnus-tmp-new-adopts new-roots)
5126
5127         (if (and (= gnus-tmp-level 0)
5128                  (or (not stack)
5129                      (= (caar stack) 0))
5130                  (not gnus-tmp-false-parent)
5131                  (or gnus-tmp-new-adopts new-roots))
5132             (if gnus-tmp-new-adopts
5133                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5134                       thread (list (car gnus-tmp-new-adopts))
5135                       gnus-tmp-header (caar thread)
5136                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5137               (when new-roots
5138                 (setq thread (list (car new-roots))
5139                       gnus-tmp-header (caar thread)
5140                       new-roots (cdr new-roots))))
5141
5142           (if threads
5143               ;; If there are some threads, we do them before the
5144               ;; threads on the stack.
5145               (setq thread threads
5146                     gnus-tmp-header (caar thread))
5147             ;; There were no current threads, so we pop something off
5148             ;; the stack.
5149             (setq state (car stack)
5150                   gnus-tmp-level (car state)
5151                   tree-stack (cadr state)
5152                   thread (caddr state)
5153                   stack (cdr stack)
5154                   gnus-tmp-header (caar thread))))
5155
5156         (setq gnus-tmp-false-parent nil)
5157         (setq gnus-tmp-root-expunged nil)
5158         (setq thread-end nil)
5159
5160         (if (stringp gnus-tmp-header)
5161             ;; The header is a dummy root.
5162             (cond
5163              ((eq gnus-summary-make-false-root 'adopt)
5164               ;; We let the first article adopt the rest.
5165               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5166                                                (cddar thread)))
5167               (setq gnus-tmp-gathered
5168                     (nconc (mapcar
5169                             (lambda (h) (mail-header-number (car h)))
5170                             (cddar thread))
5171                            gnus-tmp-gathered))
5172               (setq thread (cons (list (caar thread)
5173                                        (cadar thread))
5174                                  (cdr thread)))
5175               (setq gnus-tmp-level -1
5176                     gnus-tmp-false-parent t))
5177              ((eq gnus-summary-make-false-root 'empty)
5178               ;; We print adopted articles with empty subject fields.
5179               (setq gnus-tmp-gathered
5180                     (nconc (mapcar
5181                             (lambda (h) (mail-header-number (car h)))
5182                             (cddar thread))
5183                            gnus-tmp-gathered))
5184               (setq gnus-tmp-level -1))
5185              ((eq gnus-summary-make-false-root 'dummy)
5186               ;; We remember that we probably want to output a dummy
5187               ;; root.
5188               (setq gnus-tmp-dummy-line gnus-tmp-header)
5189               (setq gnus-tmp-prev-subject gnus-tmp-header))
5190              (t
5191               ;; We do not make a root for the gathered
5192               ;; sub-threads at all.
5193               (setq gnus-tmp-level -1)))
5194
5195           (setq number (mail-header-number gnus-tmp-header)
5196                 subject (mail-header-subject gnus-tmp-header)
5197                 simp-subject (gnus-simplify-subject-fully subject))
5198
5199           (cond
5200            ;; If the thread has changed subject, we might want to make
5201            ;; this subthread into a root.
5202            ((and (null gnus-thread-ignore-subject)
5203                  (not (zerop gnus-tmp-level))
5204                  gnus-tmp-prev-subject
5205                  (not (string= gnus-tmp-prev-subject simp-subject)))
5206             (setq new-roots (nconc new-roots (list (car thread)))
5207                   thread-end t
5208                   gnus-tmp-header nil))
5209            ;; If the article lies outside the current limit,
5210            ;; then we do not display it.
5211            ((not (memq number gnus-newsgroup-limit))
5212             (setq gnus-tmp-gathered
5213                   (nconc (mapcar
5214                           (lambda (h) (mail-header-number (car h)))
5215                           (cdar thread))
5216                          gnus-tmp-gathered))
5217             (setq gnus-tmp-new-adopts (if (cdar thread)
5218                                           (append gnus-tmp-new-adopts
5219                                                   (cdar thread))
5220                                         gnus-tmp-new-adopts)
5221                   thread-end t
5222                   gnus-tmp-header nil)
5223             (when (zerop gnus-tmp-level)
5224               (setq gnus-tmp-root-expunged t)))
5225            ;; Perhaps this article is to be marked as read?
5226            ((and gnus-summary-mark-below
5227                  (< (or (cdr (assq number gnus-newsgroup-scored))
5228                         default-score)
5229                     gnus-summary-mark-below)
5230                  ;; Don't touch sparse articles.
5231                  (not (gnus-summary-article-sparse-p number))
5232                  (not (gnus-summary-article-ancient-p number)))
5233             (setq gnus-newsgroup-unreads
5234                   (delq number gnus-newsgroup-unreads))
5235             (if gnus-newsgroup-auto-expire
5236                 (setq gnus-newsgroup-expirable
5237                       (gnus-add-to-sorted-list
5238                        gnus-newsgroup-expirable number))
5239               (push (cons number gnus-low-score-mark)
5240                     gnus-newsgroup-reads))))
5241
5242           (when gnus-tmp-header
5243             ;; We may have an old dummy line to output before this
5244             ;; article.
5245             (when (and gnus-tmp-dummy-line
5246                        (gnus-subject-equal
5247                         gnus-tmp-dummy-line
5248                         (mail-header-subject gnus-tmp-header)))
5249               (gnus-summary-insert-dummy-line
5250                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5251               (setq gnus-tmp-dummy-line nil))
5252
5253             ;; Compute the mark.
5254             (setq gnus-tmp-unread (gnus-article-mark number))
5255
5256             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5257                                   gnus-tmp-header gnus-tmp-level)
5258                   gnus-newsgroup-data)
5259
5260             ;; Actually insert the line.
5261             (setq
5262              gnus-tmp-subject-or-nil
5263              (cond
5264               ((and gnus-thread-ignore-subject
5265                     gnus-tmp-prev-subject
5266                     (not (string= gnus-tmp-prev-subject simp-subject)))
5267                subject)
5268               ((zerop gnus-tmp-level)
5269                (if (and (eq gnus-summary-make-false-root 'empty)
5270                         (memq number gnus-tmp-gathered)
5271                         gnus-tmp-prev-subject
5272                         (string= gnus-tmp-prev-subject simp-subject))
5273                    gnus-summary-same-subject
5274                  subject))
5275               (t gnus-summary-same-subject)))
5276             (if (and (eq gnus-summary-make-false-root 'adopt)
5277                      (= gnus-tmp-level 1)
5278                      (memq number gnus-tmp-gathered))
5279                 (setq gnus-tmp-opening-bracket ?\<
5280                       gnus-tmp-closing-bracket ?\>)
5281               (setq gnus-tmp-opening-bracket ?\[
5282                     gnus-tmp-closing-bracket ?\]))
5283             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5284                 (gnus-make-thread-indent-array
5285                  (max (* 2 (length gnus-thread-indent-array))
5286                       gnus-tmp-level)))
5287             (setq
5288              gnus-tmp-indentation
5289              (aref gnus-thread-indent-array gnus-tmp-level)
5290              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5291              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5292                                 gnus-summary-default-score 0)
5293              gnus-tmp-score-char
5294              (if (or (null gnus-summary-default-score)
5295                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5296                          gnus-summary-zcore-fuzz))
5297                  ?                      ;Whitespace
5298                (if (< gnus-tmp-score gnus-summary-default-score)
5299                    gnus-score-below-mark gnus-score-over-mark))
5300              gnus-tmp-replied
5301              (cond ((memq number gnus-newsgroup-processable)
5302                     gnus-process-mark)
5303                    ((memq number gnus-newsgroup-cached)
5304                     gnus-cached-mark)
5305                    ((memq number gnus-newsgroup-replied)
5306                     gnus-replied-mark)
5307                    ((memq number gnus-newsgroup-forwarded)
5308                     gnus-forwarded-mark)
5309                    ((memq number gnus-newsgroup-saved)
5310                     gnus-saved-mark)
5311                    ((memq number gnus-newsgroup-recent)
5312                     gnus-recent-mark)
5313                    ((memq number gnus-newsgroup-unseen)
5314                     gnus-unseen-mark)
5315                    (t gnus-no-mark))
5316              gnus-tmp-downloaded
5317              (cond ((memq number gnus-newsgroup-undownloaded)
5318                     gnus-undownloaded-mark)
5319                    (gnus-newsgroup-agentized
5320                     gnus-downloaded-mark)
5321                    (t
5322                     gnus-no-mark))
5323              gnus-tmp-from (mail-header-from gnus-tmp-header)
5324              gnus-tmp-name
5325              (cond
5326               ((string-match "<[^>]+> *$" gnus-tmp-from)
5327                (setq beg-match (match-beginning 0))
5328                (or (and (string-match "^\".+\"" gnus-tmp-from)
5329                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5330                    (substring gnus-tmp-from 0 beg-match)))
5331               ((string-match "(.+)" gnus-tmp-from)
5332                (substring gnus-tmp-from
5333                           (1+ (match-beginning 0)) (1- (match-end 0))))
5334               (t gnus-tmp-from))
5335
5336              ;; Do the %B string
5337              gnus-tmp-thread-tree-header-string
5338              (cond
5339               ((not gnus-show-threads) "")
5340               ((zerop gnus-tmp-level)
5341                (cond ((cdar thread)
5342                       (or gnus-sum-thread-tree-root subject))
5343                      (gnus-tmp-new-adopts
5344                       (or gnus-sum-thread-tree-false-root subject))
5345                      (t
5346                       (or gnus-sum-thread-tree-single-indent subject))))
5347               (t
5348                (concat (apply 'concat
5349                               (mapcar (lambda (item)
5350                                         (if (= item 1)
5351                                             gnus-sum-thread-tree-vertical
5352                                           gnus-sum-thread-tree-indent))
5353                                       (cdr (reverse tree-stack))))
5354                        (if (nth 1 thread)
5355                            gnus-sum-thread-tree-leaf-with-other
5356                          gnus-sum-thread-tree-single-leaf)))))
5357             (when (string= gnus-tmp-name "")
5358               (setq gnus-tmp-name gnus-tmp-from))
5359             (unless (numberp gnus-tmp-lines)
5360               (setq gnus-tmp-lines -1))
5361             (if (= gnus-tmp-lines -1)
5362                 (setq gnus-tmp-lines "?")
5363               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5364               (gnus-put-text-property
5365                (point)
5366                (progn (eval gnus-summary-line-format-spec) (point))
5367                'gnus-number number)
5368               (when gnus-visual-p
5369                 (forward-line -1)
5370                 (gnus-run-hooks 'gnus-summary-update-hook)
5371                 (forward-line 1))
5372
5373               (setq gnus-tmp-prev-subject simp-subject)))
5374
5375         (when (nth 1 thread)
5376           (push (list (max 0 gnus-tmp-level)
5377                       (copy-sequence tree-stack)
5378                       (nthcdr 1 thread))
5379                 stack))
5380         (push (if (nth 1 thread) 1 0) tree-stack)
5381         (incf gnus-tmp-level)
5382         (setq threads (if thread-end nil (cdar thread)))
5383         (if gnus-summary-display-while-building
5384             (if building-count
5385                 (progn
5386                   ;; use a set frequency
5387                   (setq building-line-count (1- building-line-count))
5388                   (when (= building-line-count 0)
5389                     (sit-for 0)
5390                     (setq building-line-count
5391                           gnus-summary-display-while-building)))
5392               ;; always
5393               (sit-for 0)))
5394         (unless threads
5395           (setq gnus-tmp-level 0)))))
5396   (gnus-message 7 "Generating summary...done"))
5397
5398 (defun gnus-summary-prepare-unthreaded (headers)
5399   "Generate an unthreaded summary buffer based on HEADERS."
5400   (let (header number mark)
5401
5402     (beginning-of-line)
5403
5404     (while headers
5405       ;; We may have to root out some bad articles...
5406       (when (memq (setq number (mail-header-number
5407                                 (setq header (pop headers))))
5408                   gnus-newsgroup-limit)
5409         ;; Mark article as read when it has a low score.
5410         (when (and gnus-summary-mark-below
5411                    (< (or (cdr (assq number gnus-newsgroup-scored))
5412                           gnus-summary-default-score 0)
5413                       gnus-summary-mark-below)
5414                    (not (gnus-summary-article-ancient-p number)))
5415           (setq gnus-newsgroup-unreads
5416                 (delq number gnus-newsgroup-unreads))
5417           (if gnus-newsgroup-auto-expire
5418               (push number gnus-newsgroup-expirable)
5419             (push (cons number gnus-low-score-mark)
5420                   gnus-newsgroup-reads)))
5421
5422         (setq mark (gnus-article-mark number))
5423         (push (gnus-data-make number mark (1+ (point)) header 0)
5424               gnus-newsgroup-data)
5425         (gnus-summary-insert-line
5426          header 0 number
5427          (memq number gnus-newsgroup-undownloaded)
5428          mark (memq number gnus-newsgroup-replied)
5429          (memq number gnus-newsgroup-expirable)
5430          (mail-header-subject header) nil
5431          (cdr (assq number gnus-newsgroup-scored))
5432          (memq number gnus-newsgroup-processable))))))
5433
5434 (defun gnus-summary-remove-list-identifiers ()
5435   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5436   (let ((regexp (if (consp gnus-list-identifiers)
5437                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5438                   gnus-list-identifiers))
5439         changed subject)
5440     (when regexp
5441       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5442       (dolist (header gnus-newsgroup-headers)
5443         (setq subject (mail-header-subject header)
5444               changed nil)
5445         (while (string-match regexp subject)
5446           (setq subject
5447                 (concat (substring subject 0 (match-beginning 1))
5448                         (substring subject (match-end 0)))
5449                 changed t))
5450         (when changed
5451           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5452             (setq subject
5453                   (concat (substring subject 0 (match-beginning 1))
5454                           (substring subject (match-end 1)))))
5455           (mail-header-set-subject header subject))))))
5456
5457 (defun gnus-fetch-headers (articles)
5458   "Fetch headers of ARTICLES."
5459   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5460     (gnus-message 5 "Fetching headers for %s..." name)
5461     (prog1
5462         (if (eq 'nov
5463                 (setq gnus-headers-retrieved-by
5464                       (gnus-retrieve-headers
5465                        articles gnus-newsgroup-name
5466                        ;; We might want to fetch old headers, but
5467                        ;; not if there is only 1 article.
5468                        (and (or (and
5469                                  (not (eq gnus-fetch-old-headers 'some))
5470                                  (not (numberp gnus-fetch-old-headers)))
5471                                 (> (length articles) 1))
5472                             gnus-fetch-old-headers))))
5473             (gnus-get-newsgroup-headers-xover
5474              articles nil nil gnus-newsgroup-name t)
5475           (gnus-get-newsgroup-headers))
5476       (gnus-message 5 "Fetching headers for %s...done" name))))
5477
5478 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5479   "Select newsgroup GROUP.
5480 If READ-ALL is non-nil, all articles in the group are selected.
5481 If SELECT-ARTICLES, only select those articles from GROUP."
5482   (let* ((entry (gnus-group-entry group))
5483          ;;!!! Dirty hack; should be removed.
5484          (gnus-summary-ignore-duplicates
5485           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5486               t
5487             gnus-summary-ignore-duplicates))
5488          (info (nth 2 entry))
5489          charset articles fetched-articles cached)
5490
5491     (unless (gnus-check-server
5492              (set (make-local-variable 'gnus-current-select-method)
5493                   (gnus-find-method-for-group group)))
5494       (error "Couldn't open server"))
5495     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5496
5497     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5498         (gnus-activate-group group)     ; Or we can activate it...
5499         (progn                          ; Or we bug out.
5500           (when (equal major-mode 'gnus-summary-mode)
5501             (gnus-kill-buffer (current-buffer)))
5502           (error
5503            "Couldn't activate group %s: %s"
5504            (mm-decode-coding-string group charset)
5505            (mm-decode-coding-string (gnus-status-message group) charset))))
5506
5507     (unless (gnus-request-group group t)
5508         (when (equal major-mode 'gnus-summary-mode)
5509           (gnus-kill-buffer (current-buffer)))
5510         (error "Couldn't request group %s: %s"
5511                (mm-decode-coding-string group charset)
5512                (mm-decode-coding-string (gnus-status-message group) charset)))
5513
5514     (when gnus-agent
5515       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5516
5517       (setq gnus-summary-use-undownloaded-faces
5518             (gnus-agent-find-parameter
5519              group
5520              'agent-enable-undownloaded-faces)))
5521
5522     (setq gnus-newsgroup-name group
5523           gnus-newsgroup-unselected nil
5524           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5525
5526     (let ((display (gnus-group-find-parameter group 'display)))
5527       (setq gnus-newsgroup-display
5528             (cond
5529              ((not (zerop (or (car-safe read-all) 0)))
5530               ;; The user entered the group with C-u SPC/RET, let's show
5531               ;; all articles.
5532               'gnus-not-ignore)
5533              ((eq display 'all)
5534               'gnus-not-ignore)
5535              ((arrayp display)
5536               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5537              ((numberp display)
5538               ;; The following is probably the "correct" solution, but
5539               ;; it makes Gnus fetch all headers and then limit the
5540               ;; articles (which is slow), so instead we hack the
5541               ;; select-articles parameter instead. -- Simon Josefsson
5542               ;; <jas@kth.se>
5543               ;;
5544               ;; (gnus-byte-compile
5545               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5546               ;;                         display)))))
5547               (setq select-articles
5548                     (gnus-uncompress-range
5549                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5550                              (if (> tmp 0)
5551                                  tmp
5552                                1))
5553                            (cdr (gnus-active group)))))
5554               nil)
5555              (t
5556               nil))))
5557
5558     (gnus-summary-setup-default-charset)
5559
5560     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5561     (when (gnus-virtual-group-p group)
5562       (setq cached gnus-newsgroup-cached))
5563
5564     (setq gnus-newsgroup-unreads
5565           (gnus-sorted-ndifference
5566            (gnus-sorted-ndifference gnus-newsgroup-unreads
5567                                     gnus-newsgroup-marked)
5568            gnus-newsgroup-dormant))
5569
5570     (setq gnus-newsgroup-processable nil)
5571
5572     (gnus-update-read-articles group gnus-newsgroup-unreads)
5573
5574     ;; Adjust and set lists of article marks.
5575     (when info
5576       (gnus-adjust-marked-articles info))
5577     (if (setq articles select-articles)
5578         (setq gnus-newsgroup-unselected
5579               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5580       (setq articles (gnus-articles-to-read group read-all)))
5581
5582     (cond
5583      ((null articles)
5584       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5585       'quit)
5586      ((eq articles 0) nil)
5587      (t
5588       ;; Init the dependencies hash table.
5589       (setq gnus-newsgroup-dependencies
5590             (gnus-make-hashtable (length articles)))
5591       (gnus-set-global-variables)
5592       ;; Retrieve the headers and read them in.
5593
5594       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5595
5596       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5597       (when cached
5598         (setq gnus-newsgroup-cached cached))
5599
5600       ;; Suppress duplicates?
5601       (when gnus-suppress-duplicates
5602         (gnus-dup-suppress-articles))
5603
5604       ;; Set the initial limit.
5605       (setq gnus-newsgroup-limit (copy-sequence articles))
5606       ;; Remove canceled articles from the list of unread articles.
5607       (setq fetched-articles
5608             (mapcar (lambda (headers) (mail-header-number headers))
5609                     gnus-newsgroup-headers))
5610       (setq gnus-newsgroup-articles fetched-articles)
5611       (setq gnus-newsgroup-unreads
5612             (gnus-sorted-nintersection
5613              gnus-newsgroup-unreads fetched-articles))
5614       (gnus-compute-unseen-list)
5615
5616       ;; Removed marked articles that do not exist.
5617       (gnus-update-missing-marks
5618        (gnus-sorted-difference articles fetched-articles))
5619       ;; We might want to build some more threads first.
5620       (when (and gnus-fetch-old-headers
5621                  (eq gnus-headers-retrieved-by 'nov))
5622         (if (eq gnus-fetch-old-headers 'invisible)
5623             (gnus-build-all-threads)
5624           (gnus-build-old-threads)))
5625       ;; Let the Gnus agent mark articles as read.
5626       (when gnus-agent
5627         (gnus-agent-get-undownloaded-list))
5628       ;; Remove list identifiers from subject
5629       (when gnus-list-identifiers
5630         (gnus-summary-remove-list-identifiers))
5631       ;; Check whether auto-expire is to be done in this group.
5632       (setq gnus-newsgroup-auto-expire
5633             (gnus-group-auto-expirable-p group))
5634       ;; Set up the article buffer now, if necessary.
5635       (unless (and gnus-single-article-buffer
5636                    (equal gnus-article-buffer "*Article*"))
5637         (gnus-article-setup-buffer))
5638       ;; First and last article in this newsgroup.
5639       (when gnus-newsgroup-headers
5640         (setq gnus-newsgroup-begin
5641               (mail-header-number (car gnus-newsgroup-headers))
5642               gnus-newsgroup-end
5643               (mail-header-number
5644                (gnus-last-element gnus-newsgroup-headers))))
5645       ;; GROUP is successfully selected.
5646       (or gnus-newsgroup-headers t)))))
5647
5648 (defun gnus-compute-unseen-list ()
5649   ;; The `seen' marks are treated specially.
5650   (if (not gnus-newsgroup-seen)
5651       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5652     (setq gnus-newsgroup-unseen
5653           (gnus-inverse-list-range-intersection
5654            gnus-newsgroup-articles gnus-newsgroup-seen))))
5655
5656 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5657
5658 (defun gnus-summary-display-make-predicate (display)
5659   (require 'gnus-agent)
5660   (when (= (length display) 1)
5661     (setq display (car display)))
5662   (unless gnus-summary-display-cache
5663     (dolist (elem (append '((unread . unread)
5664                             (read . read)
5665                             (unseen . unseen))
5666                           gnus-article-mark-lists))
5667       (push (cons (cdr elem)
5668                   (gnus-byte-compile
5669                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5670             gnus-summary-display-cache)))
5671   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5672         (gnus-category-predicate-cache gnus-summary-display-cache))
5673     (gnus-get-predicate display)))
5674
5675 ;; Uses the dynamically bound `number' variable.
5676 (defvar number)
5677 (defun gnus-article-marked-p (type &optional article)
5678   (let ((article (or article number)))
5679     (cond
5680      ((eq type 'tick)
5681       (memq article gnus-newsgroup-marked))
5682      ((eq type 'spam)
5683       (memq article gnus-newsgroup-spam-marked))
5684      ((eq type 'unsend)
5685       (memq article gnus-newsgroup-unsendable))
5686      ((eq type 'undownload)
5687       (memq article gnus-newsgroup-undownloaded))
5688      ((eq type 'download)
5689       (memq article gnus-newsgroup-downloadable))
5690      ((eq type 'unread)
5691       (memq article gnus-newsgroup-unreads))
5692      ((eq type 'read)
5693       (memq article gnus-newsgroup-reads))
5694      ((eq type 'dormant)
5695       (memq article gnus-newsgroup-dormant) )
5696      ((eq type 'expire)
5697       (memq article gnus-newsgroup-expirable))
5698      ((eq type 'reply)
5699       (memq article gnus-newsgroup-replied))
5700      ((eq type 'killed)
5701       (memq article gnus-newsgroup-killed))
5702      ((eq type 'bookmark)
5703       (assq article gnus-newsgroup-bookmarks))
5704      ((eq type 'score)
5705       (assq article gnus-newsgroup-scored))
5706      ((eq type 'save)
5707       (memq article gnus-newsgroup-saved))
5708      ((eq type 'cache)
5709       (memq article gnus-newsgroup-cached))
5710      ((eq type 'forward)
5711       (memq article gnus-newsgroup-forwarded))
5712      ((eq type 'seen)
5713       (not (memq article gnus-newsgroup-unseen)))
5714      ((eq type 'recent)
5715       (memq article gnus-newsgroup-recent))
5716      (t t))))
5717
5718 (defun gnus-articles-to-read (group &optional read-all)
5719   "Find out what articles the user wants to read."
5720   (let* ((articles
5721           ;; Select all articles if `read-all' is non-nil, or if there
5722           ;; are no unread articles.
5723           (if (or read-all
5724                   (and (zerop (length gnus-newsgroup-marked))
5725                        (zerop (length gnus-newsgroup-unreads)))
5726                   ;; Fetch all if the predicate is non-nil.
5727                   gnus-newsgroup-display)
5728               ;; We want to select the headers for all the articles in
5729               ;; the group, so we select either all the active
5730               ;; articles in the group, or (if that's nil), the
5731               ;; articles in the cache.
5732               (or
5733                (if gnus-newsgroup-maximum-articles
5734                    (let ((active (gnus-active group)))
5735                      (gnus-uncompress-range
5736                       (cons (max (car active)
5737                                  (- (cdr active)
5738                                     gnus-newsgroup-maximum-articles
5739                                     -1))
5740                             (cdr active))))
5741                  (gnus-uncompress-range (gnus-active group)))
5742                (gnus-cache-articles-in-group group))
5743             ;; Select only the "normal" subset of articles.
5744             (gnus-sorted-nunion
5745              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5746              gnus-newsgroup-unreads)))
5747          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5748          (scored (length scored-list))
5749          (number (length articles))
5750          (marked (+ (length gnus-newsgroup-marked)
5751                     (length gnus-newsgroup-dormant)))
5752          (select
5753           (cond
5754            ((numberp read-all)
5755             read-all)
5756            ((numberp gnus-newsgroup-display)
5757             gnus-newsgroup-display)
5758            (t
5759             (condition-case ()
5760                 (cond
5761                  ((and (or (<= scored marked) (= scored number))
5762                        (numberp gnus-large-newsgroup)
5763                        (> number gnus-large-newsgroup))
5764                   (let* ((cursor-in-echo-area nil)
5765                          (initial (gnus-parameter-large-newsgroup-initial
5766                                    gnus-newsgroup-name))
5767                          (input
5768                           (read-string
5769                            (format
5770                             "How many articles from %s (%s %d): "
5771                             (gnus-group-decoded-name gnus-newsgroup-name)
5772                             (if initial "max" "default")
5773                             number)
5774                            (if initial
5775                                (cons (number-to-string initial)
5776                                      0)))))
5777                     (if (string-match "^[ \t]*$" input) number input)))
5778                  ((and (> scored marked) (< scored number)
5779                        (> (- scored number) 20))
5780                   (let ((input
5781                          (read-string
5782                           (format "%s %s (%d scored, %d total): "
5783                                   "How many articles from"
5784                                   (gnus-group-decoded-name group)
5785                                   scored number))))
5786                     (if (string-match "^[ \t]*$" input)
5787                         number input)))
5788                  (t number))
5789               (quit
5790                (message "Quit getting the articles to read")
5791                nil))))))
5792     (setq select (if (stringp select) (string-to-number select) select))
5793     (if (or (null select) (zerop select))
5794         select
5795       (if (and (not (zerop scored)) (<= (abs select) scored))
5796           (progn
5797             (setq articles (sort scored-list '<))
5798             (setq number (length articles)))
5799         (setq articles (copy-sequence articles)))
5800
5801       (when (< (abs select) number)
5802         (if (< select 0)
5803             ;; Select the N oldest articles.
5804             (setcdr (nthcdr (1- (abs select)) articles) nil)
5805           ;; Select the N most recent articles.
5806           (setq articles (nthcdr (- number select) articles))))
5807       (setq gnus-newsgroup-unselected
5808             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5809       (when gnus-alter-articles-to-read-function
5810         (setq articles
5811               (sort
5812                (funcall gnus-alter-articles-to-read-function
5813                         gnus-newsgroup-name articles)
5814                '<)))
5815       articles)))
5816
5817 (defun gnus-killed-articles (killed articles)
5818   (let (out)
5819     (while articles
5820       (when (inline (gnus-member-of-range (car articles) killed))
5821         (push (car articles) out))
5822       (setq articles (cdr articles)))
5823     out))
5824
5825 (defun gnus-uncompress-marks (marks)
5826   "Uncompress the mark ranges in MARKS."
5827   (let ((uncompressed '(score bookmark))
5828         out)
5829     (while marks
5830       (if (memq (caar marks) uncompressed)
5831           (push (car marks) out)
5832         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5833       (setq marks (cdr marks)))
5834     out))
5835
5836 (defun gnus-article-mark-to-type (mark)
5837   "Return the type of MARK."
5838   (or (cadr (assq mark gnus-article-special-mark-lists))
5839       'list))
5840
5841 (defun gnus-article-unpropagatable-p (mark)
5842   "Return whether MARK should be propagated to back end."
5843   (memq mark gnus-article-unpropagated-mark-lists))
5844
5845 (defun gnus-adjust-marked-articles (info)
5846   "Set all article lists and remove all marks that are no longer valid."
5847   (let* ((marked-lists (gnus-info-marks info))
5848          (active (gnus-active (gnus-info-group info)))
5849          (min (car active))
5850          (max (cdr active))
5851          (types gnus-article-mark-lists)
5852          marks var articles article mark mark-type
5853          bgn end)
5854
5855     (dolist (marks marked-lists)
5856       (setq mark (car marks)
5857             mark-type (gnus-article-mark-to-type mark)
5858             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5859
5860       ;; We set the variable according to the type of the marks list,
5861       ;; and then adjust the marks to a subset of the active articles.
5862       (cond
5863        ;; Adjust "simple" lists - compressed yet unsorted
5864        ((eq mark-type 'list)
5865         ;; Simultaneously uncompress and clip to active range
5866         ;; See gnus-uncompress-range for a description of possible marks
5867         (let (l lh)
5868           (if (not (cadr marks))
5869               (set var nil)
5870             (setq articles (if (numberp (cddr marks))
5871                                (list (cdr marks))
5872                              (cdr marks))
5873                   lh (cons nil nil)
5874                   l lh)
5875
5876             (while (setq article (pop articles))
5877               (cond ((consp article)
5878                      (setq bgn (max (car article) min)
5879                            end (min (cdr article) max))
5880                      (while (<= bgn end)
5881                        (setq l (setcdr l (cons bgn nil))
5882                              bgn (1+ bgn))))
5883                     ((and (<= min article)
5884                           (>= max article))
5885                      (setq l (setcdr l (cons article nil))))))
5886             (set var (cdr lh)))))
5887        ;; Adjust assocs.
5888        ((eq mark-type 'tuple)
5889         (set var (setq articles (cdr marks)))
5890         (when (not (listp (cdr (symbol-value var))))
5891           (set var (list (symbol-value var))))
5892         (when (not (listp (cdr articles)))
5893           (setq articles (list articles)))
5894         (while articles
5895           (when (or (not (consp (setq article (pop articles))))
5896                     (< (car article) min)
5897                     (> (car article) max))
5898             (set var (delq article (symbol-value var))))))
5899        ;; Adjust ranges (sloppily).
5900        ((eq mark-type 'range)
5901         (cond
5902          ((eq mark 'seen)
5903           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5904           ;; It should be (seen (NUM1 . NUM2)).
5905           (when (numberp (cddr marks))
5906             (setcdr marks (list (cdr marks))))
5907           (setq articles (cdr marks))
5908           (while (and articles
5909                       (or (and (consp (car articles))
5910                                (> min (cdar articles)))
5911                           (and (numberp (car articles))
5912                                (> min (car articles)))))
5913             (pop articles))
5914           (set var articles))))))))
5915
5916 (defun gnus-update-missing-marks (missing)
5917   "Go through the list of MISSING articles and remove them from the mark lists."
5918   (when missing
5919     (let (var m)
5920       ;; Go through all types.
5921       (dolist (elem gnus-article-mark-lists)
5922         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5923           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5924           (when (symbol-value var)
5925             ;; This list has articles.  So we delete all missing
5926             ;; articles from it.
5927             (setq m missing)
5928             (while m
5929               (set var (delq (pop m) (symbol-value var))))))))))
5930
5931 (defun gnus-update-marks ()
5932   "Enter the various lists of marked articles into the newsgroup info list."
5933   (let ((types gnus-article-mark-lists)
5934         (info (gnus-get-info gnus-newsgroup-name))
5935         type list newmarked symbol delta-marks)
5936     (when info
5937       ;; Add all marks lists to the list of marks lists.
5938       (while (setq type (pop types))
5939         (setq list (symbol-value
5940                     (setq symbol
5941                           (intern (format "gnus-newsgroup-%s" (car type))))))
5942
5943         (when list
5944           ;; Get rid of the entries of the articles that have the
5945           ;; default score.
5946           (when (and (eq (cdr type) 'score)
5947                      gnus-save-score
5948                      list)
5949             (let* ((arts list)
5950                    (prev (cons nil list))
5951                    (all prev))
5952               (while arts
5953                 (if (or (not (consp (car arts)))
5954                         (= (cdar arts) gnus-summary-default-score))
5955                     (setcdr prev (cdr arts))
5956                   (setq prev arts))
5957                 (setq arts (cdr arts)))
5958               (setq list (cdr all)))))
5959
5960         (when (eq (cdr type) 'seen)
5961           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5962
5963         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5964           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5965
5966         (when (and (gnus-check-backend-function
5967                     'request-set-mark gnus-newsgroup-name)
5968                    (not (gnus-article-unpropagatable-p (cdr type))))
5969           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5970                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5971                  (add (gnus-remove-from-range
5972                        (gnus-copy-sequence list) old)))
5973             (when add
5974               (push (list add 'add (list (cdr type))) delta-marks))
5975             (when del
5976               (push (list del 'del (list (cdr type))) delta-marks))))
5977
5978         (when list
5979           (push (cons (cdr type) list) newmarked)))
5980
5981       (when delta-marks
5982         (unless (gnus-check-group gnus-newsgroup-name)
5983           (error "Can't open server for %s" gnus-newsgroup-name))
5984         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5985
5986       ;; Enter these new marks into the info of the group.
5987       (if (nthcdr 3 info)
5988           (setcar (nthcdr 3 info) newmarked)
5989         ;; Add the marks lists to the end of the info.
5990         (when newmarked
5991           (setcdr (nthcdr 2 info) (list newmarked))))
5992
5993       ;; Cut off the end of the info if there's nothing else there.
5994       (let ((i 5))
5995         (while (and (> i 2)
5996                     (not (nth i info)))
5997           (when (nthcdr (decf i) info)
5998             (setcdr (nthcdr i info) nil)))))))
5999
6000 (defun gnus-set-mode-line (where)
6001   "Set the mode line of the article or summary buffers.
6002 If WHERE is `summary', the summary mode line format will be used."
6003   ;; Is this mode line one we keep updated?
6004   (when (and (memq where gnus-updated-mode-lines)
6005              (symbol-value
6006               (intern (format "gnus-%s-mode-line-format-spec" where))))
6007     (let (mode-string)
6008       ;; We evaluate this in the summary buffer since these
6009       ;; variables are buffer-local to that buffer.
6010       (with-current-buffer gnus-summary-buffer
6011         ;; We bind all these variables that are used in the `eval' form
6012         ;; below.
6013         (let* ((mformat (symbol-value
6014                          (intern
6015                           (format "gnus-%s-mode-line-format-spec" where))))
6016                (gnus-tmp-group-name (gnus-mode-string-quote
6017                                      (gnus-group-decoded-name
6018                                       gnus-newsgroup-name)))
6019                (gnus-tmp-article-number (or gnus-current-article 0))
6020                (gnus-tmp-unread gnus-newsgroup-unreads)
6021                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6022                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6023                (gnus-tmp-unread-and-unselected
6024                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6025                             (zerop gnus-tmp-unselected))
6026                        "")
6027                       ((zerop gnus-tmp-unselected)
6028                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6029                       (t (format "{%d(+%d) more}"
6030                                  gnus-tmp-unread-and-unticked
6031                                  gnus-tmp-unselected))))
6032                (gnus-tmp-subject
6033                 (if (and gnus-current-headers
6034                          (vectorp gnus-current-headers))
6035                     (gnus-mode-string-quote
6036                      (mail-header-subject gnus-current-headers))
6037                   ""))
6038                bufname-length max-len
6039                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6040           (setq mode-string (eval mformat))
6041           (setq bufname-length (if (string-match "%b" mode-string)
6042                                    (- (length
6043                                        (buffer-name
6044                                         (if (eq where 'summary)
6045                                             nil
6046                                           (get-buffer gnus-article-buffer))))
6047                                       2)
6048                                  0))
6049           (setq max-len (max 4 (if gnus-mode-non-string-length
6050                                    (- (window-width)
6051                                       gnus-mode-non-string-length
6052                                       bufname-length)
6053                                  (length mode-string))))
6054           ;; We might have to chop a bit of the string off...
6055           (when (> (length mode-string) max-len)
6056             (setq mode-string
6057                   (concat (truncate-string-to-width mode-string (- max-len 3))
6058                           "...")))
6059           ;; Pad the mode string a bit.
6060           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
6061       ;; Update the mode line.
6062       (setq mode-line-buffer-identification
6063             (gnus-mode-line-buffer-identification (list mode-string)))
6064       (set-buffer-modified-p t))))
6065
6066 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6067   "Go through the HEADERS list and add all Xrefs to a hash table.
6068 The resulting hash table is returned, or nil if no Xrefs were found."
6069   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6070          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6071          (xref-hashtb (gnus-make-hashtable))
6072          start group entry number xrefs header)
6073     (while headers
6074       (setq header (pop headers))
6075       (when (and (setq xrefs (mail-header-xref header))
6076                  (not (memq (setq number (mail-header-number header))
6077                             unreads)))
6078         (setq start 0)
6079         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6080           (setq start (match-end 0))
6081           (setq group (if prefix
6082                           (concat prefix (substring xrefs (match-beginning 1)
6083                                                     (match-end 1)))
6084                         (substring xrefs (match-beginning 1) (match-end 1))))
6085           (setq number
6086                 (string-to-number (substring xrefs (match-beginning 2)
6087                                           (match-end 2))))
6088           (if (setq entry (gnus-gethash group xref-hashtb))
6089               (setcdr entry (cons number (cdr entry)))
6090             (gnus-sethash group (cons number nil) xref-hashtb)))))
6091     (and start xref-hashtb)))
6092
6093 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6094   "Look through all the headers and mark the Xrefs as read."
6095   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6096         name info xref-hashtb idlist method nth4)
6097     (with-current-buffer gnus-group-buffer
6098       (when (setq xref-hashtb
6099                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6100         (mapatoms
6101          (lambda (group)
6102            (unless (string= from-newsgroup (setq name (symbol-name group)))
6103              (setq idlist (symbol-value group))
6104              ;; Dead groups are not updated.
6105              (and (prog1
6106                       (setq info (gnus-get-info name))
6107                     (when (stringp (setq nth4 (gnus-info-method info)))
6108                       (setq nth4 (gnus-server-to-method nth4))))
6109                   ;; Only do the xrefs if the group has the same
6110                   ;; select method as the group we have just read.
6111                   (or (gnus-methods-equal-p
6112                        nth4 (gnus-find-method-for-group from-newsgroup))
6113                       virtual
6114                       (equal nth4 (setq method (gnus-find-method-for-group
6115                                                 from-newsgroup)))
6116                       (and (equal (car nth4) (car method))
6117                            (equal (nth 1 nth4) (nth 1 method))))
6118                   gnus-use-cross-reference
6119                   (or (not (eq gnus-use-cross-reference t))
6120                       virtual
6121                       ;; Only do cross-references on subscribed
6122                       ;; groups, if that is what is wanted.
6123                       (<= (gnus-info-level info) gnus-level-subscribed))
6124                   (gnus-group-make-articles-read name idlist))))
6125          xref-hashtb)))))
6126
6127 (defun gnus-compute-read-articles (group articles)
6128   (let* ((entry (gnus-group-entry group))
6129          (info (nth 2 entry))
6130          (active (gnus-active group))
6131          ninfo)
6132     (when entry
6133       ;; First peel off all invalid article numbers.
6134       (when active
6135         (let ((ids articles)
6136               id first)
6137           (while (setq id (pop ids))
6138             (when (and first (> id (cdr active)))
6139               ;; We'll end up in this situation in one particular
6140               ;; obscure situation.  If you re-scan a group and get
6141               ;; a new article that is cross-posted to a different
6142               ;; group that has not been re-scanned, you might get
6143               ;; crossposted article that has a higher number than
6144               ;; Gnus believes possible.  So we re-activate this
6145               ;; group as well.  This might mean doing the
6146               ;; crossposting thingy will *increase* the number
6147               ;; of articles in some groups.  Tsk, tsk.
6148               (setq active (or (gnus-activate-group group) active)))
6149             (when (or (> id (cdr active))
6150                       (< id (car active)))
6151               (setq articles (delq id articles))))))
6152       ;; If the read list is nil, we init it.
6153       (if (and active
6154                (null (gnus-info-read info))
6155                (> (car active) 1))
6156           (setq ninfo (cons 1 (1- (car active))))
6157         (setq ninfo (gnus-info-read info)))
6158       ;; Then we add the read articles to the range.
6159       (gnus-add-to-range
6160        ninfo (setq articles (sort articles '<))))))
6161
6162 (defun gnus-group-make-articles-read (group articles)
6163   "Update the info of GROUP to say that ARTICLES are read."
6164   (let* ((num 0)
6165          (entry (gnus-group-entry group))
6166          (info (nth 2 entry))
6167          (active (gnus-active group))
6168          range)
6169     (when entry
6170       (setq range (gnus-compute-read-articles group articles))
6171       (with-current-buffer gnus-group-buffer
6172         (gnus-undo-register
6173           `(progn
6174              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6175              (gnus-info-set-read ',info ',(gnus-info-read info))
6176              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6177              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6178              (gnus-group-update-group ,group t))))
6179       ;; Add the read articles to the range.
6180       (gnus-info-set-read info range)
6181       (gnus-request-set-mark group (list (list range 'add '(read))))
6182       ;; Then we have to re-compute how many unread
6183       ;; articles there are in this group.
6184       (when active
6185         (cond
6186          ((not range)
6187           (setq num (- (1+ (cdr active)) (car active))))
6188          ((not (listp (cdr range)))
6189           (setq num (- (cdr active) (- (1+ (cdr range))
6190                                        (car range)))))
6191          (t
6192           (while range
6193             (if (numberp (car range))
6194                 (setq num (1+ num))
6195               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6196             (setq range (cdr range)))
6197           (setq num (- (cdr active) num))))
6198         ;; Update the number of unread articles.
6199         (setcar entry num)
6200         ;; Update the group buffer.
6201         (unless (gnus-ephemeral-group-p group)
6202           (gnus-group-update-group group t))))))
6203
6204 (defvar gnus-newsgroup-none-id 0)
6205
6206 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6207   (let ((cur nntp-server-buffer)
6208         (dependencies
6209          (or dependencies
6210              (with-current-buffer gnus-summary-buffer
6211                gnus-newsgroup-dependencies)))
6212         headers id end ref number
6213         (mail-parse-charset gnus-newsgroup-charset)
6214         (mail-parse-ignored-charsets
6215          (save-current-buffer (condition-case nil
6216                                   (set-buffer gnus-summary-buffer)
6217                                 (error))
6218                               gnus-newsgroup-ignored-charsets)))
6219     (with-current-buffer nntp-server-buffer
6220       ;; Translate all TAB characters into SPACE characters.
6221       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6222       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6223       (ietf-drums-unfold-fws)
6224       (gnus-run-hooks 'gnus-parse-headers-hook)
6225       (let ((case-fold-search t)
6226             in-reply-to header p lines chars)
6227         (goto-char (point-min))
6228         ;; Search to the beginning of the next header.  Error messages
6229         ;; do not begin with 2 or 3.
6230         (while (re-search-forward "^[23][0-9]+ " nil t)
6231           (setq id nil
6232                 ref nil)
6233           ;; This implementation of this function, with nine
6234           ;; search-forwards instead of the one re-search-forward and
6235           ;; a case (which basically was the old function) is actually
6236           ;; about twice as fast, even though it looks messier.  You
6237           ;; can't have everything, I guess.  Speed and elegance
6238           ;; doesn't always go hand in hand.
6239           (setq
6240            header
6241            (vector
6242             ;; Number.
6243             (prog1
6244                 (setq number (read cur))
6245               (end-of-line)
6246               (setq p (point))
6247               (narrow-to-region (point)
6248                                 (or (and (search-forward "\n.\n" nil t)
6249                                          (- (point) 2))
6250                                     (point))))
6251             ;; Subject.
6252             (progn
6253               (goto-char p)
6254               (if (search-forward "\nsubject:" nil t)
6255                   (funcall gnus-decode-encoded-word-function
6256                            (nnheader-header-value))
6257                 "(none)"))
6258             ;; From.
6259             (progn
6260               (goto-char p)
6261               (if (search-forward "\nfrom:" nil t)
6262                   (funcall gnus-decode-encoded-address-function
6263                            (nnheader-header-value))
6264                 "(nobody)"))
6265             ;; Date.
6266             (progn
6267               (goto-char p)
6268               (if (search-forward "\ndate:" nil t)
6269                   (nnheader-header-value) ""))
6270             ;; Message-ID.
6271             (progn
6272               (goto-char p)
6273               (setq id (if (re-search-forward
6274                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6275                            ;; We do it this way to make sure the Message-ID
6276                            ;; is (somewhat) syntactically valid.
6277                            (buffer-substring (match-beginning 1)
6278                                              (match-end 1))
6279                          ;; If there was no message-id, we just fake one
6280                          ;; to make subsequent routines simpler.
6281                          (nnheader-generate-fake-message-id number))))
6282             ;; References.
6283             (progn
6284               (goto-char p)
6285               (if (search-forward "\nreferences:" nil t)
6286                   (progn
6287                     (setq end (point))
6288                     (prog1
6289                         (nnheader-header-value)
6290                       (setq ref
6291                             (buffer-substring
6292                              (progn
6293                                (end-of-line)
6294                                (search-backward ">" end t)
6295                                (1+ (point)))
6296                              (progn
6297                                (search-backward "<" end t)
6298                                (point))))))
6299                 ;; Get the references from the in-reply-to header if there
6300                 ;; were no references and the in-reply-to header looks
6301                 ;; promising.
6302                 (if (and (search-forward "\nin-reply-to:" nil t)
6303                          (setq in-reply-to (nnheader-header-value))
6304                          (string-match "<[^>]+>" in-reply-to))
6305                     (let (ref2)
6306                       (setq ref (substring in-reply-to (match-beginning 0)
6307                                            (match-end 0)))
6308                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6309                         (setq ref2 (substring in-reply-to (match-beginning 0)
6310                                               (match-end 0)))
6311                         (when (> (length ref2) (length ref))
6312                           (setq ref ref2)))
6313                       ref)
6314                   (setq ref nil))))
6315             ;; Chars.
6316             (progn
6317               (goto-char p)
6318               (if (search-forward "\nchars: " nil t)
6319                   (if (numberp (setq chars (ignore-errors (read cur))))
6320                       chars -1)
6321                 -1))
6322             ;; Lines.
6323             (progn
6324               (goto-char p)
6325               (if (search-forward "\nlines: " nil t)
6326                   (if (numberp (setq lines (ignore-errors (read cur))))
6327                       lines -1)
6328                 -1))
6329             ;; Xref.
6330             (progn
6331               (goto-char p)
6332               (and (search-forward "\nxref:" nil t)
6333                    (nnheader-header-value)))
6334             ;; Extra.
6335             (when gnus-extra-headers
6336               (let ((extra gnus-extra-headers)
6337                     out)
6338                 (while extra
6339                   (goto-char p)
6340                   (when (search-forward
6341                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6342                     (push (cons (car extra) (nnheader-header-value))
6343                           out))
6344                   (pop extra))
6345                 out))))
6346           (when (equal id ref)
6347             (setq ref nil))
6348
6349           (when gnus-alter-header-function
6350             (funcall gnus-alter-header-function header)
6351             (setq id (mail-header-id header)
6352                   ref (gnus-parent-id (mail-header-references header))))
6353
6354           (when (setq header
6355                       (gnus-dependencies-add-header
6356                        header dependencies force-new))
6357             (push header headers))
6358           (goto-char (point-max))
6359           (widen))
6360         (nreverse headers)))))
6361
6362 ;; Goes through the xover lines and returns a list of vectors
6363 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6364                                                   force-new dependencies
6365                                                   group also-fetch-heads)
6366   "Parse the news overview data in the server buffer.
6367 Return a list of headers that match SEQUENCE (see
6368 `nntp-retrieve-headers')."
6369   ;; Get the Xref when the users reads the articles since most/some
6370   ;; NNTP servers do not include Xrefs when using XOVER.
6371   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6372   (let ((mail-parse-charset gnus-newsgroup-charset)
6373         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6374         (cur nntp-server-buffer)
6375         (dependencies (or dependencies gnus-newsgroup-dependencies))
6376         (allp (cond
6377                ((eq gnus-read-all-available-headers t)
6378                 t)
6379                ((and (stringp gnus-read-all-available-headers)
6380                      group)
6381                 (string-match gnus-read-all-available-headers group))
6382                (t
6383                 nil)))
6384         number headers header)
6385     (with-current-buffer nntp-server-buffer
6386       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6387       ;; Allow the user to mangle the headers before parsing them.
6388       (gnus-run-hooks 'gnus-parse-headers-hook)
6389       (goto-char (point-min))
6390       (gnus-parse-without-error
6391         (while (and (or sequence allp)
6392                     (not (eobp)))
6393           (setq number (read cur))
6394           (when (not allp)
6395             (while (and sequence
6396                         (< (car sequence) number))
6397               (setq sequence (cdr sequence))))
6398           (when (and (or allp
6399                          (and sequence
6400                               (eq number (car sequence))))
6401                      (progn
6402                        (setq sequence (cdr sequence))
6403                        (setq header (inline
6404                                       (gnus-nov-parse-line
6405                                        number dependencies force-new)))))
6406             (push header headers))
6407           (forward-line 1)))
6408       ;; A common bug in inn is that if you have posted an article and
6409       ;; then retrieves the active file, it will answer correctly --
6410       ;; the new article is included.  However, a NOV entry for the
6411       ;; article may not have been generated yet, so this may fail.
6412       ;; We work around this problem by retrieving the last few
6413       ;; headers using HEAD.
6414       (if (or (not also-fetch-heads)
6415               (not sequence))
6416           ;; We (probably) got all the headers.
6417           (nreverse headers)
6418         (let ((gnus-nov-is-evil t))
6419           (nconc
6420            (nreverse headers)
6421            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6422              (gnus-get-newsgroup-headers))))))))
6423
6424 (defun gnus-article-get-xrefs ()
6425   "Fill in the Xref value in `gnus-current-headers', if necessary.
6426 This is meant to be called in `gnus-article-internal-prepare-hook'."
6427   (let ((headers (with-current-buffer gnus-summary-buffer
6428                    gnus-current-headers)))
6429     (or (not gnus-use-cross-reference)
6430         (not headers)
6431         (and (mail-header-xref headers)
6432              (not (string= (mail-header-xref headers) "")))
6433         (let ((case-fold-search t)
6434               xref)
6435           (save-restriction
6436             (nnheader-narrow-to-headers)
6437             (goto-char (point-min))
6438             (when (or (and (not (eobp))
6439                            (eq (downcase (char-after)) ?x)
6440                            (looking-at "Xref:"))
6441                       (search-forward "\nXref:" nil t))
6442               (goto-char (1+ (match-end 0)))
6443               (setq xref (buffer-substring (point) (point-at-eol)))
6444               (mail-header-set-xref headers xref)))))))
6445
6446 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6447   "Find article ID and insert the summary line for that article.
6448 OLD-HEADER can either be a header or a line number to insert
6449 the subject line on."
6450   (let* ((line (and (numberp old-header) old-header))
6451          (old-header (and (vectorp old-header) old-header))
6452          (header (cond ((and old-header use-old-header)
6453                         old-header)
6454                        ((and (numberp id)
6455                              (gnus-number-to-header id))
6456                         (gnus-number-to-header id))
6457                        (t
6458                         (gnus-read-header id))))
6459          (number (and (numberp id) id))
6460          d)
6461     (when header
6462       ;; Rebuild the thread that this article is part of and go to the
6463       ;; article we have fetched.
6464       (when (and (not gnus-show-threads)
6465                  old-header)
6466         (when (and number
6467                    (setq d (gnus-data-find (mail-header-number old-header))))
6468           (goto-char (gnus-data-pos d))
6469           (gnus-data-remove
6470            number
6471            (- (point-at-bol)
6472               (prog1
6473                   (1+ (point-at-eol))
6474                 (gnus-delete-line))))))
6475       ;; Remove list identifiers from subject.
6476       (when gnus-list-identifiers
6477         (let ((gnus-newsgroup-headers (list header)))
6478           (gnus-summary-remove-list-identifiers)))
6479       (when old-header
6480         (mail-header-set-number header (mail-header-number old-header)))
6481       (setq gnus-newsgroup-sparse
6482             (delq (setq number (mail-header-number header))
6483                   gnus-newsgroup-sparse))
6484       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6485       (push number gnus-newsgroup-limit)
6486       (gnus-rebuild-thread (mail-header-id header) line)
6487       (gnus-summary-goto-subject number nil t))
6488     (when (and (numberp number)
6489                (> number 0))
6490       ;; We have to update the boundaries even if we can't fetch the
6491       ;; article if ID is a number -- so that the next `P' or `N'
6492       ;; command will fetch the previous (or next) article even
6493       ;; if the one we tried to fetch this time has been canceled.
6494       (when (> number gnus-newsgroup-end)
6495         (setq gnus-newsgroup-end number))
6496       (when (< number gnus-newsgroup-begin)
6497         (setq gnus-newsgroup-begin number))
6498       (setq gnus-newsgroup-unselected
6499             (delq number gnus-newsgroup-unselected)))
6500     ;; Report back a success?
6501     (and header (mail-header-number header))))
6502
6503 ;;; Process/prefix in the summary buffer
6504
6505 (defun gnus-summary-work-articles (n)
6506   "Return a list of articles to be worked upon.
6507 The prefix argument, the list of process marked articles, and the
6508 current article will be taken into consideration."
6509   (with-current-buffer gnus-summary-buffer
6510     (cond
6511      (n
6512       ;; A numerical prefix has been given.
6513       (setq n (prefix-numeric-value n))
6514       (let ((backward (< n 0))
6515             (n (abs (prefix-numeric-value n)))
6516             articles article)
6517         (save-excursion
6518           (while
6519               (and (> n 0)
6520                    (push (setq article (gnus-summary-article-number))
6521                          articles)
6522                    (if backward
6523                        (gnus-summary-find-prev nil article)
6524                      (gnus-summary-find-next nil article)))
6525             (decf n)))
6526         (nreverse articles)))
6527      ((and (gnus-region-active-p) (mark))
6528       (message "region active")
6529       ;; Work on the region between point and mark.
6530       (let ((max (max (point) (mark)))
6531             articles article)
6532         (save-excursion
6533           (goto-char (min (point) (mark)))
6534           (while
6535               (and
6536                (push (setq article (gnus-summary-article-number)) articles)
6537                (gnus-summary-find-next nil article)
6538                (< (point) max)))
6539           (nreverse articles))))
6540      (gnus-newsgroup-processable
6541       ;; There are process-marked articles present.
6542       ;; Save current state.
6543       (gnus-summary-save-process-mark)
6544       ;; Return the list.
6545       (reverse gnus-newsgroup-processable))
6546      (t
6547       ;; Just return the current article.
6548       (list (gnus-summary-article-number))))))
6549
6550 (defmacro gnus-summary-iterate (arg &rest forms)
6551   "Iterate over the process/prefixed articles and do FORMS.
6552 ARG is the interactive prefix given to the command.  FORMS will be
6553 executed with point over the summary line of the articles."
6554   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6555     `(let ((,articles (gnus-summary-work-articles ,arg)))
6556        (while ,articles
6557          (gnus-summary-goto-subject (car ,articles))
6558          ,@forms
6559          (pop ,articles)))))
6560
6561 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6562 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6563
6564 (defun gnus-summary-save-process-mark ()
6565   "Push the current set of process marked articles on the stack."
6566   (interactive)
6567   (push (copy-sequence gnus-newsgroup-processable)
6568         gnus-newsgroup-process-stack))
6569
6570 (defun gnus-summary-kill-process-mark ()
6571   "Push the current set of process marked articles on the stack and unmark."
6572   (interactive)
6573   (gnus-summary-save-process-mark)
6574   (gnus-summary-unmark-all-processable))
6575
6576 (defun gnus-summary-yank-process-mark ()
6577   "Pop the last process mark state off the stack and restore it."
6578   (interactive)
6579   (unless gnus-newsgroup-process-stack
6580     (error "Empty mark stack"))
6581   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6582
6583 (defun gnus-summary-process-mark-set (set)
6584   "Make SET into the current process marked articles."
6585   (gnus-summary-unmark-all-processable)
6586   (mapc 'gnus-summary-set-process-mark set))
6587
6588 ;;; Searching and stuff
6589
6590 (defun gnus-summary-search-group (&optional backward use-level)
6591   "Search for next unread newsgroup.
6592 If optional argument BACKWARD is non-nil, search backward instead."
6593   (with-current-buffer gnus-group-buffer
6594     (when (gnus-group-search-forward
6595            backward nil (if use-level (gnus-group-group-level) nil))
6596       (gnus-group-group-name))))
6597
6598 (defun gnus-summary-best-group (&optional exclude-group)
6599   "Find the name of the best unread group.
6600 If EXCLUDE-GROUP, do not go to this group."
6601   (with-current-buffer gnus-group-buffer
6602     (save-excursion
6603       (gnus-group-best-unread-group exclude-group))))
6604
6605 (defun gnus-summary-find-next (&optional unread article backward)
6606   (if backward
6607       (gnus-summary-find-prev unread article)
6608     (let* ((dummy (gnus-summary-article-intangible-p))
6609            (article (or article (gnus-summary-article-number)))
6610            (data (gnus-data-find-list article))
6611            result)
6612       (when (and (not dummy)
6613                  (or (not gnus-summary-check-current)
6614                      (not unread)
6615                      (not (gnus-data-unread-p (car data)))))
6616         (setq data (cdr data)))
6617       (when (setq result
6618                   (if unread
6619                       (progn
6620                         (while data
6621                           (unless (memq (gnus-data-number (car data))
6622                                         (cond
6623                                          ((eq gnus-auto-goto-ignores
6624                                               'always-undownloaded)
6625                                           gnus-newsgroup-undownloaded)
6626                                          (gnus-plugged
6627                                           nil)
6628                                          ((eq gnus-auto-goto-ignores
6629                                               'unfetched)
6630                                           gnus-newsgroup-unfetched)
6631                                          ((eq gnus-auto-goto-ignores
6632                                               'undownloaded)
6633                                           gnus-newsgroup-undownloaded)))
6634                             (when (gnus-data-unread-p (car data))
6635                               (setq result (car data)
6636                                     data nil)))
6637                           (setq data (cdr data)))
6638                         result)
6639                     (car data)))
6640         (goto-char (gnus-data-pos result))
6641         (gnus-data-number result)))))
6642
6643 (defun gnus-summary-find-prev (&optional unread article)
6644   (let* ((eobp (eobp))
6645          (article (or article (gnus-summary-article-number)))
6646          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6647          result)
6648     (when (and (not eobp)
6649                (or (not gnus-summary-check-current)
6650                    (not unread)
6651                    (not (gnus-data-unread-p (car data)))))
6652       (setq data (cdr data)))
6653     (when (setq result
6654                 (if unread
6655                     (progn
6656                       (while data
6657                         (unless (memq (gnus-data-number (car data))
6658                                       (cond
6659                                        ((eq gnus-auto-goto-ignores
6660                                             'always-undownloaded)
6661                                         gnus-newsgroup-undownloaded)
6662                                        (gnus-plugged
6663                                         nil)
6664                                        ((eq gnus-auto-goto-ignores
6665                                             'unfetched)
6666                                         gnus-newsgroup-unfetched)
6667                                        ((eq gnus-auto-goto-ignores
6668                                             'undownloaded)
6669                                         gnus-newsgroup-undownloaded)))
6670                           (when (gnus-data-unread-p (car data))
6671                             (setq result (car data)
6672                                   data nil)))
6673                         (setq data (cdr data)))
6674                       result)
6675                   (car data)))
6676       (goto-char (gnus-data-pos result))
6677       (gnus-data-number result))))
6678
6679 (defun gnus-summary-find-subject (subject &optional unread backward article)
6680   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6681          (article (or article (gnus-summary-article-number)))
6682          (articles (gnus-data-list backward))
6683          (arts (gnus-data-find-list article articles))
6684          result)
6685     (when (or (not gnus-summary-check-current)
6686               (not unread)
6687               (not (gnus-data-unread-p (car arts))))
6688       (setq arts (cdr arts)))
6689     (while arts
6690       (and (or (not unread)
6691                (gnus-data-unread-p (car arts)))
6692            (vectorp (gnus-data-header (car arts)))
6693            (gnus-subject-equal
6694             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6695            (setq result (car arts)
6696                  arts nil))
6697       (setq arts (cdr arts)))
6698     (and result
6699          (goto-char (gnus-data-pos result))
6700          (gnus-data-number result))))
6701
6702 (defun gnus-summary-search-forward (&optional unread subject backward)
6703   "Search forward for an article.
6704 If UNREAD, look for unread articles.  If SUBJECT, look for
6705 articles with that subject.  If BACKWARD, search backward instead."
6706   (cond (subject (gnus-summary-find-subject subject unread backward))
6707         (backward (gnus-summary-find-prev unread))
6708         (t (gnus-summary-find-next unread))))
6709
6710 (defun gnus-recenter (&optional n)
6711   "Center point in window and redisplay frame.
6712 Also do horizontal recentering."
6713   (interactive "P")
6714   (when (and gnus-auto-center-summary
6715              (not (eq gnus-auto-center-summary 'vertical)))
6716     (gnus-horizontal-recenter))
6717   (if (fboundp 'recenter-top-bottom)
6718       (recenter-top-bottom n)
6719     (recenter n)))
6720
6721 (put 'gnus-recenter 'isearch-scroll t)
6722
6723 (defun gnus-forward-line-ignore-invisible (n)
6724   "Move N lines forward (backward if N is negative).
6725 Like forward-line, but skip over (and don't count) invisible lines."
6726   (let (done)
6727     (while (and (> n 0) (not done))
6728       ;; If the following character is currently invisible,
6729       ;; skip all characters with that same `invisible' property value.
6730       (while (gnus-invisible-p (point))
6731         (goto-char (gnus-next-char-property-change (point))))
6732       (forward-line 1)
6733       (if (eobp)
6734           (setq done t)
6735         (setq n (1- n))))
6736     (while (and (< n 0) (not done))
6737       (forward-line -1)
6738       (if (bobp) (setq done t)
6739         (setq n (1+ n))
6740         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6741           (goto-char (gnus-previous-char-property-change (point))))))))
6742
6743 (defun gnus-summary-recenter ()
6744   "Center point in the summary window.
6745 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6746 displayed, no centering will be performed."
6747   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6748   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6749   (interactive)
6750   ;; The user has to want it.
6751   (when gnus-auto-center-summary
6752     (let* ((top (cond ((< (window-height) 4) 0)
6753                       ((< (window-height) 7) 1)
6754                       (t (if (numberp gnus-auto-center-summary)
6755                              gnus-auto-center-summary
6756                            (/ (1- (window-height)) 2)))))
6757            (height (1- (window-height)))
6758            (bottom (save-excursion
6759                      (goto-char (point-max))
6760                      (gnus-forward-line-ignore-invisible (- height))
6761                      (point)))
6762            (window (get-buffer-window (current-buffer))))
6763       (when (get-buffer-window gnus-article-buffer)
6764         ;; Only do recentering when the article buffer is displayed,
6765         ;; Set the window start to either `bottom', which is the biggest
6766         ;; possible valid number, or the second line from the top,
6767         ;; whichever is the least.
6768         (let ((top-pos (save-excursion
6769                          (gnus-forward-line-ignore-invisible (- top))
6770                          (point))))
6771           (if (> bottom top-pos)
6772               ;; Keep the second line from the top visible
6773               (set-window-start window top-pos)
6774             ;; Try to keep the bottom line visible; if it's partially
6775             ;; obscured, either scroll one more line to make it fully
6776             ;; visible, or revert to using TOP-POS.
6777             (save-excursion
6778               (goto-char (point-max))
6779               (gnus-forward-line-ignore-invisible -1)
6780               (let ((last-line-start (point)))
6781                 (goto-char bottom)
6782                 (set-window-start window (point) t)
6783                 (when (not (pos-visible-in-window-p last-line-start window))
6784                   (gnus-forward-line-ignore-invisible 1)
6785                   (set-window-start window (min (point) top-pos) t)))))))
6786       ;; Do horizontal recentering while we're at it.
6787       (when (and (get-buffer-window (current-buffer) t)
6788                  (not (eq gnus-auto-center-summary 'vertical)))
6789         (let ((selected (selected-window)))
6790           (select-window (get-buffer-window (current-buffer) t))
6791           (gnus-summary-position-point)
6792           (gnus-horizontal-recenter)
6793           (select-window selected))))))
6794
6795 (defun gnus-summary-jump-to-group (newsgroup)
6796   "Move point to NEWSGROUP in group mode buffer."
6797   ;; Keep update point of group mode buffer if visible.
6798   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6799       (save-window-excursion
6800         ;; Take care of tree window mode.
6801         (when (get-buffer-window gnus-group-buffer)
6802           (pop-to-buffer gnus-group-buffer))
6803         (gnus-group-jump-to-group newsgroup))
6804     (save-excursion
6805       ;; Take care of tree window mode.
6806       (if (get-buffer-window gnus-group-buffer 0)
6807           (pop-to-buffer gnus-group-buffer)
6808         (set-buffer gnus-group-buffer))
6809       (gnus-group-jump-to-group newsgroup))))
6810
6811 ;; This function returns a list of article numbers based on the
6812 ;; difference between the ranges of read articles in this group and
6813 ;; the range of active articles.
6814 (defun gnus-list-of-unread-articles (group)
6815   (let* ((read (gnus-info-read (gnus-get-info group)))
6816          (active (or (gnus-active group) (gnus-activate-group group)))
6817          (last (or (cdr active)
6818                    (error "Group %s couldn't be activated " group)))
6819          (bottom (if gnus-newsgroup-maximum-articles
6820                      (max (car active)
6821                           (- last gnus-newsgroup-maximum-articles -1))
6822                    (car active)))
6823          first nlast unread)
6824     ;; If none are read, then all are unread.
6825     (if (not read)
6826         (setq first bottom)
6827       ;; If the range of read articles is a single range, then the
6828       ;; first unread article is the article after the last read
6829       ;; article.  Sounds logical, doesn't it?
6830       (if (and (not (listp (cdr read)))
6831                (or (< (car read) bottom)
6832                    (progn (setq read (list read))
6833                           nil)))
6834           (setq first (max bottom (1+ (cdr read))))
6835         ;; `read' is a list of ranges.
6836         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6837                                   (caar read)))
6838                   1)
6839           (setq first bottom))
6840         (while read
6841           (when first
6842             (while (< first nlast)
6843               (setq unread (cons first unread)
6844                     first (1+ first))))
6845           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6846           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6847           (setq read (cdr read)))))
6848     ;; And add the last unread articles.
6849     (while (<= first last)
6850       (setq unread (cons first unread)
6851             first (1+ first)))
6852     ;; Return the list of unread articles.
6853     (delq 0 (nreverse unread))))
6854
6855 (defun gnus-list-of-read-articles (group)
6856   "Return a list of unread, unticked and non-dormant articles."
6857   (let* ((info (gnus-get-info group))
6858          (marked (gnus-info-marks info))
6859          (active (gnus-active group)))
6860     (and info active
6861          (gnus-list-range-difference
6862           (gnus-list-range-difference
6863            (gnus-sorted-complement
6864             (gnus-uncompress-range
6865              (if gnus-newsgroup-maximum-articles
6866                  (cons (max (car active)
6867                             (- (cdr active)
6868                                gnus-newsgroup-maximum-articles
6869                                -1))
6870                        (cdr active))
6871                active))
6872             (gnus-list-of-unread-articles group))
6873            (cdr (assq 'dormant marked)))
6874           (cdr (assq 'tick marked))))))
6875
6876 ;; This function returns a sequence of article numbers based on the
6877 ;; difference between the ranges of read articles in this group and
6878 ;; the range of active articles.
6879 (defun gnus-sequence-of-unread-articles (group)
6880   (let* ((read (gnus-info-read (gnus-get-info group)))
6881          (active (or (gnus-active group) (gnus-activate-group group)))
6882          (last (cdr active))
6883          (bottom (if gnus-newsgroup-maximum-articles
6884                      (max (car active)
6885                           (- last gnus-newsgroup-maximum-articles -1))
6886                    (car active)))
6887          first nlast unread)
6888     ;; If none are read, then all are unread.
6889     (if (not read)
6890         (setq first bottom)
6891       ;; If the range of read articles is a single range, then the
6892       ;; first unread article is the article after the last read
6893       ;; article.  Sounds logical, doesn't it?
6894       (if (and (not (listp (cdr read)))
6895                (or (< (car read) bottom)
6896                    (progn (setq read (list read))
6897                           nil)))
6898           (setq first (max bottom (1+ (cdr read))))
6899         ;; `read' is a list of ranges.
6900         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6901                                   (caar read)))
6902                   1)
6903           (setq first bottom))
6904         (while read
6905           (when first
6906             (push (cons first nlast) unread))
6907           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6908           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6909           (setq read (cdr read)))))
6910     ;; And add the last unread articles.
6911     (cond ((not (and first last))
6912            nil)
6913           ((< first last)
6914            (push (cons first last) unread))
6915           ((= first last)
6916            (push first unread)))
6917     ;; Return the sequence of unread articles.
6918     (delq 0 (nreverse unread))))
6919
6920 ;; Various summary commands
6921
6922 (defun gnus-summary-select-article-buffer ()
6923   "Reconfigure windows to show article buffer."
6924   (interactive)
6925   (if (not (gnus-buffer-live-p gnus-article-buffer))
6926       (error "There is no article buffer for this summary buffer")
6927     (gnus-configure-windows 'article)
6928     (select-window (get-buffer-window gnus-article-buffer))))
6929
6930 (defun gnus-summary-universal-argument (arg)
6931   "Perform any operation on all articles that are process/prefixed."
6932   (interactive "P")
6933   (let ((articles (gnus-summary-work-articles arg))
6934         func article)
6935     (if (eq
6936          (setq
6937           func
6938           (key-binding
6939            (read-key-sequence
6940             (substitute-command-keys
6941              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6942          'undefined)
6943         (gnus-error 1 "Undefined key")
6944       (save-excursion
6945         (while articles
6946           (gnus-summary-goto-subject (setq article (pop articles)))
6947           (let (gnus-newsgroup-processable)
6948             (command-execute func))
6949           (gnus-summary-remove-process-mark article)))))
6950   (gnus-summary-position-point))
6951
6952 (defun gnus-summary-toggle-truncation (&optional arg)
6953   "Toggle truncation of summary lines.
6954 With ARG, turn line truncation on if ARG is positive."
6955   (interactive "P")
6956   (setq truncate-lines
6957         (if (null arg) (not truncate-lines)
6958           (> (prefix-numeric-value arg) 0)))
6959   (redraw-display))
6960
6961 (defun gnus-summary-find-for-reselect ()
6962   "Return the number of an article to stay on across a reselect.
6963 The current article is considered, then following articles, then previous
6964 articles.  An article is sought which is not cancelled and isn't a temporary
6965 insertion from another group.  If there's no such then return a dummy 0."
6966   (let (found)
6967     (dolist (rev '(nil t))
6968       (unless found      ; don't demand the reverse list if we don't need it
6969         (let ((data (gnus-data-find-list
6970                      (gnus-summary-article-number) (gnus-data-list rev))))
6971           (while (and data (not found))
6972             (if (and (< 0 (gnus-data-number (car data)))
6973                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6974                 (setq found (gnus-data-number (car data))))
6975             (setq data (cdr data))))))
6976     (or found 0)))
6977
6978 (defun gnus-summary-reselect-current-group (&optional all rescan)
6979   "Exit and then reselect the current newsgroup.
6980 The prefix argument ALL means to select all articles."
6981   (interactive "P")
6982   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6983     (error "Ephemeral groups can't be reselected"))
6984   (let ((current-subject (gnus-summary-find-for-reselect))
6985         (group gnus-newsgroup-name))
6986     (setq gnus-newsgroup-begin nil)
6987     (gnus-summary-exit nil 'leave-hidden)
6988     ;; We have to adjust the point of group mode buffer because
6989     ;; point was moved to the next unread newsgroup by exiting.
6990     (gnus-summary-jump-to-group group)
6991     (when rescan
6992       (save-excursion
6993         (gnus-group-get-new-news-this-group 1)))
6994     (gnus-group-read-group all t)
6995     (gnus-summary-goto-subject current-subject nil t)))
6996
6997 (defun gnus-summary-rescan-group (&optional all)
6998   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6999   (interactive "P")
7000   (gnus-summary-reselect-current-group all t))
7001
7002 (defun gnus-summary-update-info (&optional non-destructive)
7003   (save-excursion
7004     (let ((group gnus-newsgroup-name))
7005       (when group
7006         (when gnus-newsgroup-kill-headers
7007           (setq gnus-newsgroup-killed
7008                 (gnus-compress-sequence
7009                  (gnus-sorted-union
7010                   (gnus-list-range-intersection
7011                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7012                   gnus-newsgroup-unreads)
7013                  t)))
7014         (unless (listp (cdr gnus-newsgroup-killed))
7015           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7016         (let ((headers gnus-newsgroup-headers))
7017           ;; Set the new ranges of read articles.
7018           (with-current-buffer gnus-group-buffer
7019             (gnus-undo-force-boundary))
7020           (gnus-update-read-articles
7021            group (gnus-sorted-union
7022                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7023           ;; Set the current article marks.
7024           (let ((gnus-newsgroup-scored
7025                  (if (and (not gnus-save-score)
7026                           (not non-destructive))
7027                      nil
7028                    gnus-newsgroup-scored)))
7029             (save-excursion
7030               (gnus-update-marks)))
7031           ;; Do the cross-ref thing.
7032           (when gnus-use-cross-reference
7033             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7034           ;; Do not switch windows but change the buffer to work.
7035           (set-buffer gnus-group-buffer)
7036           (unless (gnus-ephemeral-group-p group)
7037             (gnus-group-update-group group)))))))
7038
7039 (defun gnus-summary-save-newsrc (&optional force)
7040   "Save the current number of read/marked articles in the dribble buffer.
7041 The dribble buffer will then be saved.
7042 If FORCE (the prefix), also save the .newsrc file(s)."
7043   (interactive "P")
7044   (gnus-summary-update-info t)
7045   (if force
7046       (gnus-save-newsrc-file)
7047     (gnus-dribble-save)))
7048
7049 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7050
7051 (defun gnus-summary-exit (&optional temporary leave-hidden)
7052   "Exit reading current newsgroup, and then return to group selection mode.
7053 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7054   (interactive)
7055   (gnus-set-global-variables)
7056   (when (gnus-buffer-live-p gnus-article-buffer)
7057     (with-current-buffer gnus-article-buffer
7058       (mm-destroy-parts gnus-article-mime-handles)
7059       ;; Set it to nil for safety reason.
7060       (setq gnus-article-mime-handle-alist nil)
7061       (setq gnus-article-mime-handles nil)))
7062   (gnus-kill-save-kill-buffer)
7063   (gnus-async-halt-prefetch)
7064   (let* ((group gnus-newsgroup-name)
7065          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7066          (gnus-group-is-exiting-p t)
7067          (mode major-mode)
7068          (group-point nil)
7069          (buf (current-buffer)))
7070     (unless quit-config
7071       ;; Do adaptive scoring, and possibly save score files.
7072       (when gnus-newsgroup-adaptive
7073         (gnus-score-adaptive))
7074       (when gnus-use-scoring
7075         (gnus-score-save)))
7076     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7077     ;; If we have several article buffers, we kill them at exit.
7078     (unless gnus-single-article-buffer
7079       (when (gnus-buffer-live-p gnus-article-buffer)
7080         (with-current-buffer gnus-article-buffer
7081           ;; Don't kill sticky article buffers
7082           (unless (eq major-mode 'gnus-sticky-article-mode)
7083             (gnus-kill-buffer gnus-article-buffer)
7084             (setq gnus-article-current nil))))
7085       (gnus-kill-buffer gnus-original-article-buffer))
7086     (when gnus-use-cache
7087       (gnus-cache-possibly-remove-articles)
7088       (gnus-cache-save-buffers))
7089     (gnus-async-prefetch-remove-group group)
7090     (when gnus-suppress-duplicates
7091       (gnus-dup-enter-articles))
7092     (when gnus-use-trees
7093       (gnus-tree-close group))
7094     (when gnus-use-cache
7095       (gnus-cache-write-active))
7096     ;; Remove entries for this group.
7097     (nnmail-purge-split-history (gnus-group-real-name group))
7098     ;; Make all changes in this group permanent.
7099     (unless quit-config
7100       (gnus-run-hooks 'gnus-exit-group-hook)
7101       (gnus-summary-update-info))
7102     (gnus-close-group group)
7103     ;; Make sure where we were, and go to next newsgroup.
7104     (set-buffer gnus-group-buffer)
7105     (unless quit-config
7106       (gnus-group-jump-to-group group))
7107     (gnus-run-hooks 'gnus-summary-exit-hook)
7108     (unless (or quit-config
7109                 (not gnus-summary-next-group-on-exit)
7110                 ;; If this group has disappeared from the summary
7111                 ;; buffer, don't skip forwards.
7112                 (not (string= group (gnus-group-group-name))))
7113       (gnus-group-next-unread-group 1))
7114     (setq group-point (point))
7115     (if temporary
7116         nil                             ;Nothing to do.
7117       (set-buffer buf)
7118       (if (not gnus-kill-summary-on-exit)
7119           (progn
7120             (gnus-deaden-summary)
7121             (setq mode nil))
7122         ;; We set all buffer-local variables to nil.  It is unclear why
7123         ;; this is needed, but if we don't, buffer-local variables are
7124         ;; not garbage-collected, it seems.  This would the lead to en
7125         ;; ever-growing Emacs.
7126         (gnus-summary-clear-local-variables)
7127         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7128           (gnus-summary-clear-local-variables))
7129         (when (get-buffer gnus-article-buffer)
7130           (bury-buffer gnus-article-buffer))
7131         ;; Return to group mode buffer.
7132         (when (eq mode 'gnus-summary-mode)
7133           (gnus-kill-buffer buf)))
7134       (setq gnus-current-select-method gnus-select-method)
7135       (set-buffer gnus-group-buffer)
7136       (if quit-config
7137           (gnus-handle-ephemeral-exit quit-config)
7138         (goto-char group-point)
7139         ;; If gnus-group-buffer is already displayed, make sure we also move
7140         ;; the cursor in the window that displays it.
7141         (let ((win (get-buffer-window (current-buffer) 0)))
7142           (if win (set-window-point win (point))))
7143         (unless leave-hidden
7144           (gnus-configure-windows 'group 'force)))
7145       ;; Clear the current group name.
7146       (unless quit-config
7147         (setq gnus-newsgroup-name nil)))))
7148
7149 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7150 (defun gnus-summary-exit-no-update (&optional no-questions)
7151   "Quit reading current newsgroup without updating read article info."
7152   (interactive)
7153   (let* ((group gnus-newsgroup-name)
7154          (gnus-group-is-exiting-p t)
7155          (gnus-group-is-exiting-without-update-p t)
7156          (quit-config (gnus-group-quit-config group)))
7157     (when (or no-questions
7158               gnus-expert-user
7159               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7160       (gnus-async-halt-prefetch)
7161       (run-hooks 'gnus-summary-prepare-exit-hook)
7162       (when (gnus-buffer-live-p gnus-article-buffer)
7163         (with-current-buffer gnus-article-buffer
7164           (mm-destroy-parts gnus-article-mime-handles)
7165           ;; Set it to nil for safety reason.
7166           (setq gnus-article-mime-handle-alist nil)
7167           (setq gnus-article-mime-handles nil)))
7168       ;; If we have several article buffers, we kill them at exit.
7169       (unless gnus-single-article-buffer
7170         (gnus-kill-buffer gnus-article-buffer)
7171         (gnus-kill-buffer gnus-original-article-buffer)
7172         (setq gnus-article-current nil))
7173       (if (not gnus-kill-summary-on-exit)
7174           (gnus-deaden-summary)
7175         (gnus-close-group group)
7176         (gnus-summary-clear-local-variables)
7177         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7178           (gnus-summary-clear-local-variables))
7179         (gnus-kill-buffer gnus-summary-buffer))
7180       (unless gnus-single-article-buffer
7181         (setq gnus-article-current nil))
7182       (when gnus-use-trees
7183         (gnus-tree-close group))
7184       (gnus-async-prefetch-remove-group group)
7185       (when (get-buffer gnus-article-buffer)
7186         (bury-buffer gnus-article-buffer))
7187       ;; Return to the group buffer.
7188       (gnus-configure-windows 'group 'force)
7189       ;; Clear the current group name.
7190       (setq gnus-newsgroup-name nil)
7191       (unless (gnus-ephemeral-group-p group)
7192         (gnus-group-update-group group))
7193       (when (equal (gnus-group-group-name) group)
7194         (gnus-group-next-unread-group 1))
7195       (when quit-config
7196         (gnus-handle-ephemeral-exit quit-config)))))
7197
7198 (defun gnus-handle-ephemeral-exit (quit-config)
7199   "Handle movement when leaving an ephemeral group.
7200 The state which existed when entering the ephemeral is reset."
7201   (if (not (buffer-name (car quit-config)))
7202       (gnus-configure-windows 'group 'force)
7203     (set-buffer (car quit-config))
7204     (cond ((eq major-mode 'gnus-summary-mode)
7205            (gnus-set-global-variables))
7206           ((eq major-mode 'gnus-article-mode)
7207            (save-current-buffer
7208              ;; The `gnus-summary-buffer' variable may point
7209              ;; to the old summary buffer when using a single
7210              ;; article buffer.
7211              (unless (gnus-buffer-live-p gnus-summary-buffer)
7212                (set-buffer gnus-group-buffer))
7213              (set-buffer gnus-summary-buffer)
7214              (gnus-set-global-variables))))
7215     (if (or (eq (cdr quit-config) 'article)
7216             (eq (cdr quit-config) 'pick))
7217         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7218             (gnus-configure-windows 'pick 'force)
7219           (gnus-configure-windows (cdr quit-config) 'force))
7220       (gnus-configure-windows (cdr quit-config) 'force))
7221     (when (eq major-mode 'gnus-summary-mode)
7222       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7223                                                      next-unread-noselect))
7224           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7225                                   'next-noselect)
7226                               (gnus-summary-next-subject 1 nil t))
7227                              ((eq gnus-auto-select-on-ephemeral-exit
7228                                   'next-unread-noselect)
7229                               (gnus-summary-next-subject 1 t t))))
7230             ;; Hide the article buffer which displays the article different
7231             ;; from the one that the cursor points to in the summary buffer.
7232             (gnus-configure-windows 'summary 'force))
7233         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7234                (gnus-summary-next-subject 1))
7235               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7236                (gnus-summary-next-subject 1 t))))
7237       (gnus-summary-recenter)
7238       (gnus-summary-position-point))))
7239
7240 ;;; Dead summaries.
7241
7242 (defvar gnus-dead-summary-mode-map
7243   (let ((map (make-keymap)))
7244     (suppress-keymap map)
7245     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7246     (dolist (key '("\C-d" "\r" "\177" [delete]))
7247       (define-key map key 'gnus-summary-wake-up-the-dead))
7248     (dolist (key '("q" "Q"))
7249       (define-key map key 'bury-buffer))
7250     map))
7251
7252 (define-minor-mode gnus-dead-summary-mode
7253   "Minor mode for Gnus summary buffers."
7254   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7255   (unless (derived-mode-p 'gnus-summary-mode)
7256     (setq gnus-dead-summary-mode nil)))
7257
7258 (defun gnus-deaden-summary ()
7259   "Make the current summary buffer into a dead summary buffer."
7260   ;; Kill any previous dead summary buffer.
7261   (when (and gnus-dead-summary
7262              (buffer-name gnus-dead-summary))
7263     (with-current-buffer gnus-dead-summary
7264       (when gnus-dead-summary-mode
7265         (kill-buffer (current-buffer)))))
7266   ;; Make this the current dead summary.
7267   (setq gnus-dead-summary (current-buffer))
7268   (gnus-dead-summary-mode 1)
7269   (let ((name (buffer-name)))
7270     (when (string-match "Summary" name)
7271       (rename-buffer
7272        (concat (substring name 0 (match-beginning 0)) "Dead "
7273                (substring name (match-beginning 0)))
7274        t)
7275       (bury-buffer))))
7276
7277 (defun gnus-kill-or-deaden-summary (buffer)
7278   "Kill or deaden the summary BUFFER."
7279   (save-excursion
7280     (when (and (buffer-name buffer)
7281                (not gnus-single-article-buffer))
7282       (with-current-buffer buffer
7283         (gnus-kill-buffer gnus-article-buffer)
7284         (gnus-kill-buffer gnus-original-article-buffer)))
7285     (cond
7286      ;; Kill the buffer.
7287      (gnus-kill-summary-on-exit
7288       (when (and gnus-use-trees
7289                  (gnus-buffer-exists-p buffer))
7290         (with-current-buffer buffer
7291           (gnus-tree-close gnus-newsgroup-name)))
7292       (gnus-kill-buffer buffer))
7293      ;; Deaden the buffer.
7294      ((gnus-buffer-exists-p buffer)
7295       (with-current-buffer buffer
7296         (gnus-deaden-summary))))))
7297
7298 (defun gnus-summary-wake-up-the-dead (&rest args)
7299   "Wake up the dead summary buffer."
7300   (interactive)
7301   (gnus-dead-summary-mode -1)
7302   (let ((name (buffer-name)))
7303     (when (string-match "Dead " name)
7304       (rename-buffer
7305        (concat (substring name 0 (match-beginning 0))
7306                (substring name (match-end 0)))
7307        t)))
7308   (gnus-message 3 "This dead summary is now alive again"))
7309
7310 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7311 (defun gnus-summary-fetch-faq (&optional faq-dir)
7312   "Fetch the FAQ for the current group.
7313 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7314 in."
7315   (interactive
7316    (list
7317     (when current-prefix-arg
7318       (completing-read
7319        "FAQ dir: " (and (listp gnus-group-faq-directory)
7320                         (mapcar 'list
7321                                 gnus-group-faq-directory))))))
7322   (let (gnus-faq-buffer)
7323     (when (setq gnus-faq-buffer
7324                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7325       (gnus-configure-windows 'summary-faq))))
7326
7327 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7328 (defun gnus-summary-describe-group (&optional force)
7329   "Describe the current newsgroup."
7330   (interactive "P")
7331   (gnus-group-describe-group force gnus-newsgroup-name))
7332
7333 (defun gnus-summary-describe-briefly ()
7334   "Describe summary mode commands briefly."
7335   (interactive)
7336   (gnus-message 6 (substitute-command-keys "\\<gnus-summary-mode-map>\\[gnus-summary-next-page]:Select  \\[gnus-summary-next-unread-article]:Forward  \\[gnus-summary-prev-unread-article]:Backward  \\[gnus-summary-exit]:Exit  \\[gnus-info-find-node]:Run Info        \\[gnus-summary-describe-briefly]:This help")))
7337
7338 ;; Walking around group mode buffer from summary mode.
7339
7340 (defun gnus-summary-next-group (&optional no-article target-group backward)
7341   "Exit current newsgroup and then select next unread newsgroup.
7342 If prefix argument NO-ARTICLE is non-nil, no article is selected
7343 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7344 previous group instead."
7345   (interactive "P")
7346   ;; Stop pre-fetching.
7347   (gnus-async-halt-prefetch)
7348   (let ((current-group gnus-newsgroup-name)
7349         (current-buffer (current-buffer))
7350         entered)
7351     ;; First we semi-exit this group to update Xrefs and all variables.
7352     ;; We can't do a real exit, because the window conf must remain
7353     ;; the same in case the user is prompted for info, and we don't
7354     ;; want the window conf to change before that...
7355     (gnus-summary-exit t)
7356     (while (not entered)
7357       ;; Then we find what group we are supposed to enter.
7358       (set-buffer gnus-group-buffer)
7359       (gnus-group-jump-to-group current-group)
7360       (setq target-group
7361             (or target-group
7362                 (if (eq gnus-keep-same-level 'best)
7363                     (gnus-summary-best-group gnus-newsgroup-name)
7364                   (gnus-summary-search-group backward gnus-keep-same-level))))
7365       (if (not target-group)
7366           ;; There are no further groups, so we return to the group
7367           ;; buffer.
7368           (progn
7369             (gnus-message 5 "Returning to the group buffer")
7370             (setq entered t)
7371             (when (gnus-buffer-live-p current-buffer)
7372               (set-buffer current-buffer)
7373               (gnus-summary-exit))
7374             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7375         ;; We try to enter the target group.
7376         (gnus-group-jump-to-group target-group)
7377         (let ((unreads (gnus-group-group-unread)))
7378           (if (and (or (eq t unreads)
7379                        (and unreads (not (zerop unreads))))
7380                    (gnus-summary-read-group
7381                     target-group nil no-article
7382                     (and (buffer-name current-buffer) current-buffer)
7383                     nil backward))
7384               (setq entered t)
7385             (setq current-group target-group
7386                   target-group nil)))))))
7387
7388 (defun gnus-summary-prev-group (&optional no-article)
7389   "Exit current newsgroup and then select previous unread newsgroup.
7390 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7391   (interactive "P")
7392   (gnus-summary-next-group no-article nil t))
7393
7394 ;; Walking around summary lines.
7395
7396 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7397   "Go to the first subject satisfying any non-nil constraint.
7398 If UNREAD is non-nil, the article should be unread.
7399 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7400 If UNSEEN is non-nil, the article should be unseen.
7401 Returns the article selected or nil if there are no matching articles."
7402   (interactive "P")
7403   (cond
7404    ;; Empty summary.
7405    ((null gnus-newsgroup-data)
7406     (gnus-message 3 "No articles in the group")
7407     nil)
7408    ;; Pick the first article.
7409    ((not (or unread undownloaded unseen))
7410     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7411     (gnus-data-number (car gnus-newsgroup-data)))
7412    ;; Find the first unread article.
7413    (t
7414     (let ((data gnus-newsgroup-data))
7415       (while (and data
7416                   (let ((num (gnus-data-number (car data))))
7417                     (or (memq num gnus-newsgroup-unfetched)
7418                         (not (or (and unread
7419                                       (memq num gnus-newsgroup-unreads))
7420                                  (and undownloaded
7421                                       (memq num gnus-newsgroup-undownloaded))
7422                                  (and unseen
7423                                       (memq num gnus-newsgroup-unseen)))))))
7424         (setq data (cdr data)))
7425       (prog1
7426           (if data
7427               (progn
7428                 (goto-char (gnus-data-pos (car data)))
7429                 (gnus-data-number (car data)))
7430             (gnus-message 3 "No more%s articles"
7431                           (let* ((r (when unread " unread"))
7432                                  (d (when undownloaded " undownloaded"))
7433                                  (s (when unseen " unseen"))
7434                                  (l (delq nil (list r d s))))
7435                             (cond ((= 3 (length l))
7436                                    (concat r "," d ", or" s))
7437                                   ((= 2 (length l))
7438                                    (concat (car l) ", or" (cadr l)))
7439                                   ((= 1 (length l))
7440                                    (car l))
7441                                   (t
7442                                    ""))))
7443             nil
7444             )
7445         (gnus-summary-position-point))))))
7446
7447 (defun gnus-summary-next-subject (n &optional unread dont-display)
7448   "Go to next N'th summary line.
7449 If N is negative, go to the previous N'th subject line.
7450 If UNREAD is non-nil, only unread articles are selected.
7451 The difference between N and the actual number of steps taken is
7452 returned."
7453   (interactive "p")
7454   (let ((backward (< n 0))
7455         (n (abs n)))
7456     (while (and (> n 0)
7457                 (if backward
7458                     (gnus-summary-find-prev unread)
7459                   (gnus-summary-find-next unread)))
7460       (unless (zerop (setq n (1- n)))
7461         (gnus-summary-show-thread)))
7462     (when (/= 0 n)
7463       (gnus-message 7 "No more%s articles"
7464                     (if unread " unread" "")))
7465     (unless dont-display
7466       (gnus-summary-recenter)
7467       (gnus-summary-position-point))
7468     n))
7469
7470 (defun gnus-summary-next-unread-subject (n)
7471   "Go to next N'th unread summary line."
7472   (interactive "p")
7473   (gnus-summary-next-subject n t))
7474
7475 (defun gnus-summary-prev-subject (n &optional unread)
7476   "Go to previous N'th summary line.
7477 If optional argument UNREAD is non-nil, only unread article is selected."
7478   (interactive "p")
7479   (gnus-summary-next-subject (- n) unread))
7480
7481 (defun gnus-summary-prev-unread-subject (n)
7482   "Go to previous N'th unread summary line."
7483   (interactive "p")
7484   (gnus-summary-next-subject (- n) t))
7485
7486 (defun gnus-summary-goto-subjects (articles)
7487   "Insert the subject header for ARTICLES in the current buffer."
7488   (save-excursion
7489     (dolist (article articles)
7490       (gnus-summary-goto-subject article t)))
7491   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7492   (gnus-summary-position-point))
7493
7494 (defun gnus-summary-goto-subject (article &optional force silent)
7495   "Go to the subject line of ARTICLE.
7496 If FORCE, also allow jumping to articles not currently shown."
7497   (interactive "nArticle number: ")
7498   (unless (numberp article)
7499     (error "Article %s is not a number" article))
7500   (let ((b (point))
7501         (data (gnus-data-find article)))
7502     ;; We read in the article if we have to.
7503     (and (not data)
7504          force
7505          (gnus-summary-insert-subject
7506           article
7507           (if (or (numberp force) (vectorp force)) force)
7508           t)
7509          (setq data (gnus-data-find article)))
7510     (goto-char b)
7511     (if (not data)
7512         (progn
7513           (unless silent
7514             (gnus-message 3 "Can't find article %d" article))
7515           nil)
7516       (let ((pt (gnus-data-pos data)))
7517         (goto-char pt)
7518         (gnus-summary-set-article-display-arrow pt))
7519       (gnus-summary-position-point)
7520       article)))
7521
7522 ;; Walking around summary lines with displaying articles.
7523
7524 (defun gnus-summary-expand-window (&optional arg)
7525   "Make the summary buffer take up the entire Emacs frame.
7526 Given a prefix, will force an `article' buffer configuration."
7527   (interactive "P")
7528   (if arg
7529       (gnus-configure-windows 'article 'force)
7530     (gnus-configure-windows 'summary 'force)))
7531
7532 (defun gnus-summary-display-article (article &optional all-header)
7533   "Display ARTICLE in article buffer."
7534   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7535                (with-current-buffer gnus-article-buffer
7536                  (eq major-mode 'gnus-article-mode)))
7537     (gnus-article-setup-buffer))
7538   (gnus-set-global-variables)
7539   (with-current-buffer gnus-article-buffer
7540     (setq gnus-article-charset gnus-newsgroup-charset)
7541     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7542     (mm-enable-multibyte))
7543   (if (null article)
7544       nil
7545     (prog1
7546         (if gnus-summary-display-article-function
7547             (funcall gnus-summary-display-article-function article all-header)
7548           (gnus-article-prepare article all-header))
7549       (gnus-run-hooks 'gnus-select-article-hook)
7550       (when (and gnus-current-article
7551                  (not (zerop gnus-current-article)))
7552         (gnus-summary-goto-subject gnus-current-article))
7553       (gnus-summary-recenter)
7554       (when (and gnus-use-trees gnus-show-threads)
7555         (gnus-possibly-generate-tree article)
7556         (gnus-highlight-selected-tree article))
7557       ;; Successfully display article.
7558       (gnus-article-set-window-start
7559        (cdr (assq article gnus-newsgroup-bookmarks))))))
7560
7561 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7562   "Select the current article.
7563 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7564 non-nil, the article will be re-fetched even if it already present in
7565 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7566 be displayed."
7567   ;; Make sure we are in the summary buffer to work around bbdb bug.
7568   (unless (eq major-mode 'gnus-summary-mode)
7569     (set-buffer gnus-summary-buffer))
7570   (let ((article (or article (gnus-summary-article-number)))
7571         (all-headers (not (not all-headers))) ;Must be t or nil.
7572         gnus-summary-display-article-function)
7573     (and (not pseudo)
7574          (gnus-summary-article-pseudo-p article)
7575          (error "This is a pseudo-article"))
7576     (with-current-buffer gnus-summary-buffer
7577       (if (or (and gnus-single-article-buffer
7578                    (or (null gnus-current-article)
7579                        (null gnus-article-current)
7580                        (null (get-buffer gnus-article-buffer))
7581                        (not (eq article (cdr gnus-article-current)))
7582                        (not (equal (car gnus-article-current)
7583                                    gnus-newsgroup-name))))
7584               (and (not gnus-single-article-buffer)
7585                    (or (null gnus-current-article)
7586                        (not (eq gnus-current-article article))))
7587               force)
7588           ;; The requested article is different from the current article.
7589           (progn
7590             (gnus-summary-display-article article all-headers)
7591             (when (gnus-buffer-live-p gnus-article-buffer)
7592               (with-current-buffer gnus-article-buffer
7593                 (if (not gnus-article-decoded-p) ;; a local variable
7594                     (mm-disable-multibyte))))
7595             (gnus-article-set-window-start
7596              (cdr (assq article gnus-newsgroup-bookmarks)))
7597             article)
7598         'old))))
7599
7600 (defun gnus-summary-force-verify-and-decrypt ()
7601   "Display buttons for signed/encrypted parts and verify/decrypt them."
7602   (interactive)
7603   (let ((mm-verify-option 'known)
7604         (mm-decrypt-option 'known)
7605         (gnus-article-emulate-mime t)
7606         (gnus-buttonized-mime-types (append (list "multipart/signed"
7607                                                   "multipart/encrypted")
7608                                             gnus-buttonized-mime-types)))
7609     (gnus-summary-select-article nil 'force)))
7610
7611 (defun gnus-summary-set-current-mark (&optional current-mark)
7612   "Obsolete function."
7613   nil)
7614
7615 (defun gnus-summary-next-article (&optional unread subject backward push)
7616   "Select the next article.
7617 If UNREAD, only unread articles are selected.
7618 If SUBJECT, only articles with SUBJECT are selected.
7619 If BACKWARD, the previous article is selected instead of the next."
7620   (interactive "P")
7621   ;; Make sure we are in the summary buffer.
7622   (unless (eq major-mode 'gnus-summary-mode)
7623     (set-buffer gnus-summary-buffer))
7624   (cond
7625    ;; Is there such an article?
7626    ((and (gnus-summary-search-forward unread subject backward)
7627          (or (gnus-summary-display-article (gnus-summary-article-number))
7628              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7629     (gnus-summary-position-point))
7630    ;; If not, we try the first unread, if that is wanted.
7631    ((and subject
7632          gnus-auto-select-same
7633          (gnus-summary-first-unread-article))
7634     (gnus-summary-position-point)
7635     (gnus-message 6 "Wrapped"))
7636    ;; Try to get next/previous article not displayed in this group.
7637    ((and gnus-auto-extend-newsgroup
7638          (not unread) (not subject))
7639     (gnus-summary-goto-article
7640      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7641      nil (count-lines (point-min) (point))))
7642    ;; Go to next/previous group.
7643    (t
7644     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7645       (gnus-summary-jump-to-group gnus-newsgroup-name))
7646     (let ((cmd (if (featurep 'xemacs)
7647                    last-command-char
7648                  last-command-event))
7649           (point
7650            (with-current-buffer gnus-group-buffer
7651              (point)))
7652           (group
7653            (if (eq gnus-keep-same-level 'best)
7654                (gnus-summary-best-group gnus-newsgroup-name)
7655              (gnus-summary-search-group backward gnus-keep-same-level))))
7656       ;; For some reason, the group window gets selected.  We change
7657       ;; it back.
7658       (select-window (get-buffer-window (current-buffer)))
7659       ;; Select next unread newsgroup automagically.
7660       (cond
7661        ((or (not gnus-auto-select-next)
7662             (not cmd))
7663         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7664        ((or (eq gnus-auto-select-next 'quietly)
7665             (and (eq gnus-auto-select-next 'slightly-quietly)
7666                  push)
7667             (and (eq gnus-auto-select-next 'almost-quietly)
7668                  (gnus-summary-last-article-p)))
7669         ;; Select quietly.
7670         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7671             (gnus-summary-exit)
7672           (gnus-message 7 "No more%s articles (%s)..."
7673                         (if unread " unread" "")
7674                         (if group (concat "selecting " group)
7675                           "exiting"))
7676           (gnus-summary-next-group nil group backward)))
7677        (t
7678         (when (gnus-key-press-event-p last-input-event)
7679           (gnus-summary-walk-group-buffer
7680            gnus-newsgroup-name cmd unread backward point))))))))
7681
7682 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7683   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7684                       (?\C-p (gnus-group-prev-unread-group 1))))
7685         (cursor-in-echo-area t)
7686         keve key group ended prompt)
7687     (with-current-buffer gnus-group-buffer
7688       (goto-char start)
7689       (setq group
7690             (if (eq gnus-keep-same-level 'best)
7691                 (gnus-summary-best-group gnus-newsgroup-name)
7692               (gnus-summary-search-group backward gnus-keep-same-level))))
7693     (while (not ended)
7694       (setq prompt
7695             (format
7696              "No more%s articles%s " (if unread " unread" "")
7697              (if (and group
7698                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7699                  (format " (Type %s for %s [%s])"
7700                          (single-key-description cmd)
7701                          (gnus-group-decoded-name group)
7702                          (gnus-group-unread group))
7703                (format " (Type %s to exit %s)"
7704                        (single-key-description cmd)
7705                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7706       ;; Confirm auto selection.
7707       (setq key (car (setq keve (gnus-read-event-char prompt)))
7708             ended t)
7709       (cond
7710        ((assq key keystrokes)
7711         (let ((obuf (current-buffer)))
7712           (switch-to-buffer gnus-group-buffer)
7713           (when group
7714             (gnus-group-jump-to-group group))
7715           (eval (cadr (assq key keystrokes)))
7716           (setq group (gnus-group-group-name))
7717           (switch-to-buffer obuf))
7718         (setq ended nil))
7719        ((equal key cmd)
7720         (if (or (not group)
7721                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7722             (gnus-summary-exit)
7723           (gnus-summary-next-group nil group backward)))
7724        (t
7725         (push (cdr keve) unread-command-events))))))
7726
7727 (defun gnus-summary-next-unread-article ()
7728   "Select unread article after current one."
7729   (interactive)
7730   (gnus-summary-next-article
7731    (or (not (eq gnus-summary-goto-unread 'never))
7732        (gnus-summary-last-article-p (gnus-summary-article-number)))
7733    (and gnus-auto-select-same
7734         (gnus-summary-article-subject))))
7735
7736 (defun gnus-summary-prev-article (&optional unread subject)
7737   "Select the article before the current one.
7738 If UNREAD is non-nil, only unread articles are selected."
7739   (interactive "P")
7740   (gnus-summary-next-article unread subject t))
7741
7742 (defun gnus-summary-prev-unread-article ()
7743   "Select unread article before current one."
7744   (interactive)
7745   (gnus-summary-prev-article
7746    (or (not (eq gnus-summary-goto-unread 'never))
7747        (gnus-summary-first-article-p (gnus-summary-article-number)))
7748    (and gnus-auto-select-same
7749         (gnus-summary-article-subject))))
7750
7751 (defun gnus-summary-next-page (&optional lines circular stop)
7752   "Show next page of the selected article.
7753 If at the end of the current article, select the next article.
7754 LINES says how many lines should be scrolled up.
7755
7756 If CIRCULAR is non-nil, go to the start of the article instead of
7757 selecting the next article when reaching the end of the current
7758 article.
7759
7760 If STOP is non-nil, just stop when reaching the end of the message.
7761
7762 Also see the variable `gnus-article-skip-boring'."
7763   (interactive "P")
7764   (setq gnus-summary-buffer (current-buffer))
7765   (gnus-set-global-variables)
7766   (let ((article (gnus-summary-article-number))
7767         (article-window (get-buffer-window gnus-article-buffer t))
7768         endp)
7769     ;; If the buffer is empty, we have no article.
7770     (unless article
7771       (error "No article to select"))
7772     (gnus-configure-windows 'article)
7773     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7774         (if (and (eq gnus-summary-goto-unread 'never)
7775                  (not (gnus-summary-last-article-p article)))
7776             (gnus-summary-next-article)
7777           (gnus-summary-next-unread-article))
7778       (if (or (null gnus-current-article)
7779               (null gnus-article-current)
7780               (/= article (cdr gnus-article-current))
7781               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7782           ;; Selected subject is different from current article's.
7783           (gnus-summary-display-article article)
7784         (when article-window
7785           (gnus-eval-in-buffer-window gnus-article-buffer
7786             (setq endp (or (gnus-article-next-page lines)
7787                            (gnus-article-only-boring-p))))
7788           (when endp
7789             (cond (stop
7790                    (gnus-message 3 "End of message"))
7791                   (circular
7792                    (gnus-summary-beginning-of-article))
7793                   (lines
7794                    (gnus-message 3 "End of message"))
7795                   ((null lines)
7796                    (if (and (eq gnus-summary-goto-unread 'never)
7797                             (not (gnus-summary-last-article-p article)))
7798                        (gnus-summary-next-article)
7799                      (gnus-summary-next-unread-article))))))))
7800     (gnus-summary-recenter)
7801     (gnus-summary-position-point)))
7802
7803 (defun gnus-summary-prev-page (&optional lines move)
7804   "Show previous page of selected article.
7805 Argument LINES specifies lines to be scrolled down.
7806 If MOVE, move to the previous unread article if point is at
7807 the beginning of the buffer."
7808   (interactive "P")
7809   (let ((article (gnus-summary-article-number))
7810         (article-window (get-buffer-window gnus-article-buffer t))
7811         endp)
7812     (gnus-configure-windows 'article)
7813     (if (or (null gnus-current-article)
7814             (null gnus-article-current)
7815             (/= article (cdr gnus-article-current))
7816             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7817         ;; Selected subject is different from current article's.
7818         (gnus-summary-display-article article)
7819       (gnus-summary-recenter)
7820       (when article-window
7821         (gnus-eval-in-buffer-window gnus-article-buffer
7822           (setq endp (gnus-article-prev-page lines)))
7823         (when (and move endp)
7824           (cond (lines
7825                  (gnus-message 3 "Beginning of message"))
7826                 ((null lines)
7827                  (if (and (eq gnus-summary-goto-unread 'never)
7828                           (not (gnus-summary-first-article-p article)))
7829                      (gnus-summary-prev-article)
7830                    (gnus-summary-prev-unread-article))))))))
7831   (gnus-summary-position-point))
7832
7833 (defun gnus-summary-prev-page-or-article (&optional lines)
7834   "Show previous page of selected article.
7835 Argument LINES specifies lines to be scrolled down.
7836 If at the beginning of the article, go to the next article."
7837   (interactive "P")
7838   (gnus-summary-prev-page lines t))
7839
7840 (defun gnus-summary-scroll-up (lines)
7841   "Scroll up (or down) one line current article.
7842 Argument LINES specifies lines to be scrolled up (or down if negative)."
7843   (interactive "p")
7844   (gnus-configure-windows 'article)
7845   (gnus-summary-show-thread)
7846   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7847     (gnus-eval-in-buffer-window gnus-article-buffer
7848       (cond ((> lines 0)
7849              (when (gnus-article-next-page lines)
7850                (gnus-message 3 "End of message")))
7851             ((< lines 0)
7852              (gnus-article-prev-page (- lines))))))
7853   (gnus-summary-recenter)
7854   (gnus-summary-position-point))
7855
7856 (defun gnus-summary-scroll-down (lines)
7857   "Scroll down (or up) one line current article.
7858 Argument LINES specifies lines to be scrolled down (or up if negative)."
7859   (interactive "p")
7860   (gnus-summary-scroll-up (- lines)))
7861
7862 (defun gnus-summary-next-same-subject ()
7863   "Select next article which has the same subject as current one."
7864   (interactive)
7865   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7866
7867 (defun gnus-summary-prev-same-subject ()
7868   "Select previous article which has the same subject as current one."
7869   (interactive)
7870   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7871
7872 (defun gnus-summary-next-unread-same-subject ()
7873   "Select next unread article which has the same subject as current one."
7874   (interactive)
7875   (gnus-summary-next-article t (gnus-summary-article-subject)))
7876
7877 (defun gnus-summary-prev-unread-same-subject ()
7878   "Select previous unread article which has the same subject as current one."
7879   (interactive)
7880   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7881
7882 (defun gnus-summary-first-unread-article ()
7883   "Select the first unread article.
7884 Return nil if there are no unread articles."
7885   (interactive)
7886   (prog1
7887       (when (gnus-summary-first-subject t)
7888         (gnus-summary-show-thread)
7889         (gnus-summary-first-subject t)
7890         (gnus-summary-display-article (gnus-summary-article-number)))
7891     (gnus-summary-position-point)))
7892
7893 (defun gnus-summary-first-unread-subject ()
7894   "Place the point on the subject line of the first unread article.
7895 Return nil if there are no unread articles."
7896   (interactive)
7897   (prog1
7898       (when (gnus-summary-first-subject t)
7899         (gnus-summary-show-thread)
7900         (gnus-summary-first-subject t))
7901     (gnus-summary-position-point)))
7902
7903 (defun gnus-summary-first-unseen-subject ()
7904   "Place the point on the subject line of the first unseen article.
7905 Return nil if there are no unseen articles."
7906   (interactive)
7907   (prog1
7908       (when (gnus-summary-first-subject nil nil t)
7909         (gnus-summary-show-thread)
7910         (gnus-summary-first-subject nil nil t))
7911     (gnus-summary-position-point)))
7912
7913 (defun gnus-summary-first-unseen-or-unread-subject ()
7914   "Place the point on the subject line of the first unseen article or,
7915 if all article have been seen, on the subject line of the first unread
7916 article."
7917   (interactive)
7918   (prog1
7919       (unless (when (gnus-summary-first-subject nil nil t)
7920                 (gnus-summary-show-thread)
7921                 (gnus-summary-first-subject nil nil t))
7922         (when (gnus-summary-first-subject t)
7923           (gnus-summary-show-thread)
7924           (gnus-summary-first-subject t)))
7925     (gnus-summary-position-point)))
7926
7927 (defun gnus-summary-first-article ()
7928   "Select the first article.
7929 Return nil if there are no articles."
7930   (interactive)
7931   (prog1
7932       (when (gnus-summary-first-subject)
7933         (gnus-summary-show-thread)
7934         (gnus-summary-first-subject)
7935         (gnus-summary-display-article (gnus-summary-article-number)))
7936     (gnus-summary-position-point)))
7937
7938 (defun gnus-summary-best-unread-article (&optional arg)
7939   "Select the unread article with the highest score.
7940 If given a prefix argument, select the next unread article that has a
7941 score higher than the default score."
7942   (interactive "P")
7943   (let ((article (if arg
7944                      (gnus-summary-better-unread-subject)
7945                    (gnus-summary-best-unread-subject))))
7946     (if article
7947         (gnus-summary-goto-article article)
7948       (error "No unread articles"))))
7949
7950 (defun gnus-summary-best-unread-subject ()
7951   "Select the unread subject with the highest score."
7952   (interactive)
7953   (let ((best -1000000)
7954         (data gnus-newsgroup-data)
7955         article score)
7956     (while data
7957       (and (gnus-data-unread-p (car data))
7958            (> (setq score
7959                     (gnus-summary-article-score (gnus-data-number (car data))))
7960               best)
7961            (setq best score
7962                  article (gnus-data-number (car data))))
7963       (setq data (cdr data)))
7964     (when article
7965       (gnus-summary-goto-subject article))
7966     (gnus-summary-position-point)
7967     article))
7968
7969 (defun gnus-summary-better-unread-subject ()
7970   "Select the first unread subject that has a score over the default score."
7971   (interactive)
7972   (let ((data gnus-newsgroup-data)
7973         article score)
7974     (while (and (setq article (gnus-data-number (car data)))
7975                 (or (gnus-data-read-p (car data))
7976                     (not (> (gnus-summary-article-score article)
7977                             gnus-summary-default-score))))
7978       (setq data (cdr data)))
7979     (when article
7980       (gnus-summary-goto-subject article))
7981     (gnus-summary-position-point)
7982     article))
7983
7984 (defun gnus-summary-last-subject ()
7985   "Go to the last displayed subject line in the group."
7986   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7987     (when article
7988       (gnus-summary-goto-subject article))))
7989
7990 (defun gnus-summary-goto-article (article &optional all-headers force)
7991   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7992 If ALL-HEADERS is non-nil, no header lines are hidden.
7993 If FORCE, go to the article even if it isn't displayed.  If FORCE
7994 is a number, it is the line the article is to be displayed on."
7995   (interactive
7996    (list
7997     (completing-read
7998      "Article number or Message-ID: "
7999      (mapcar (lambda (number) (list (int-to-string number)))
8000              gnus-newsgroup-limit))
8001     current-prefix-arg
8002     t))
8003   (prog1
8004       (if (and (stringp article)
8005                (string-match "@\\|%40" article))
8006           (gnus-summary-refer-article article)
8007         (when (stringp article)
8008           (setq article (string-to-number article)))
8009         (if (gnus-summary-goto-subject article force)
8010             (gnus-summary-display-article article all-headers)
8011           (gnus-message 4 "Couldn't go to article %s" article) nil))
8012     (gnus-summary-position-point)))
8013
8014 (defun gnus-summary-goto-last-article ()
8015   "Go to the previously read article."
8016   (interactive)
8017   (prog1
8018       (when gnus-last-article
8019         (gnus-summary-goto-article gnus-last-article nil t))
8020     (gnus-summary-position-point)))
8021
8022 (defun gnus-summary-pop-article (number)
8023   "Pop one article off the history and go to the previous.
8024 NUMBER articles will be popped off."
8025   (interactive "p")
8026   (let (to)
8027     (setq gnus-newsgroup-history
8028           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8029     (if to
8030         (gnus-summary-goto-article (car to) nil t)
8031       (error "Article history empty")))
8032   (gnus-summary-position-point))
8033
8034 ;; Summary commands and functions for limiting the summary buffer.
8035
8036 (defun gnus-summary-limit-to-articles (n)
8037   "Limit the summary buffer to the next N articles.
8038 If not given a prefix, use the process marked articles instead."
8039   (interactive "P")
8040   (prog1
8041       (let ((articles (gnus-summary-work-articles n)))
8042         (setq gnus-newsgroup-processable nil)
8043         (gnus-summary-limit articles))
8044     (gnus-summary-position-point)))
8045
8046 (defun gnus-summary-pop-limit (&optional total)
8047   "Restore the previous limit.
8048 If given a prefix, remove all limits."
8049   (interactive "P")
8050   (when total
8051     (setq gnus-newsgroup-limits
8052           (list (mapcar (lambda (h) (mail-header-number h))
8053                         gnus-newsgroup-headers))))
8054   (unless gnus-newsgroup-limits
8055     (error "No limit to pop"))
8056   (prog1
8057       (gnus-summary-limit nil 'pop)
8058     (gnus-summary-position-point)))
8059
8060 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8061   "Limit the summary buffer to articles that have subjects that match a regexp.
8062 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8063   (interactive
8064    (list (read-string (if current-prefix-arg
8065                           "Exclude subject (regexp): "
8066                         "Limit to subject (regexp): "))
8067          nil current-prefix-arg))
8068   (unless header
8069     (setq header "subject"))
8070   (when (not (equal "" subject))
8071     (prog1
8072         (let ((articles (gnus-summary-find-matching
8073                          (or header "subject") subject 'all nil nil
8074                          not-matching)))
8075           (unless articles
8076             (error "Found no matches for \"%s\"" subject))
8077           (gnus-summary-limit articles))
8078       (gnus-summary-position-point))))
8079
8080 (defun gnus-summary-limit-to-author (from &optional not-matching)
8081   "Limit the summary buffer to articles that have authors that match a regexp.
8082 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8083   (interactive
8084    (list (read-string (if current-prefix-arg
8085                           "Exclude author (regexp): "
8086                         "Limit to author (regexp): "))
8087          current-prefix-arg))
8088   (gnus-summary-limit-to-subject from "from" not-matching))
8089
8090 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8091   "Limit the summary buffer to articles with the given RECIPIENT.
8092
8093 If NOT-MATCHING, exclude RECIPIENT.
8094
8095 To and Cc headers are checked.  You need to include them in
8096 `nnmail-extra-headers'."
8097   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8098   (interactive
8099    (list (read-string (format "%s recipient (regexp): "
8100                               (if current-prefix-arg "Exclude" "Limit to")))
8101          current-prefix-arg))
8102   (when (not (equal "" recipient))
8103     (prog1 (let* ((to
8104                    (if (memq 'To nnmail-extra-headers)
8105                        (gnus-summary-find-matching
8106                         (cons 'extra 'To) recipient 'all nil nil
8107                         not-matching)
8108                      (gnus-message
8109                       1 "`To' isn't present in `nnmail-extra-headers'")
8110                      (sit-for 1)
8111                      nil))
8112                   (cc
8113                    (if (memq 'Cc nnmail-extra-headers)
8114                        (gnus-summary-find-matching
8115                         (cons 'extra 'Cc) recipient 'all nil nil
8116                         not-matching)
8117                      (gnus-message
8118                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8119                      (sit-for 1)
8120                      nil))
8121                   (articles
8122                    (if not-matching
8123                        ;; We need the numbers that are in both lists:
8124                        (mapcar (lambda (a)
8125                                  (and (memq a to) a))
8126                                cc)
8127                      (nconc to cc))))
8128              (unless articles
8129                (error "Found no matches for \"%s\"" recipient))
8130              (gnus-summary-limit articles))
8131       (gnus-summary-position-point))))
8132
8133 (defun gnus-summary-limit-to-address (address &optional not-matching)
8134   "Limit the summary buffer to articles with the given ADDRESS.
8135
8136 If NOT-MATCHING, exclude ADDRESS.
8137
8138 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8139 in `nnmail-extra-headers'."
8140   (interactive
8141    (list (read-string (format "%s address (regexp): "
8142                               (if current-prefix-arg "Exclude" "Limit to")))
8143          current-prefix-arg))
8144   (when (not (equal "" address))
8145     (prog1 (let* ((to
8146                    (if (memq 'To nnmail-extra-headers)
8147                        (gnus-summary-find-matching
8148                         (cons 'extra 'To) address 'all nil nil
8149                         not-matching)
8150                      (gnus-message
8151                       1 "`To' isn't present in `nnmail-extra-headers'")
8152                      (sit-for 1)
8153                      t))
8154                   (cc
8155                    (if (memq 'Cc nnmail-extra-headers)
8156                        (gnus-summary-find-matching
8157                         (cons 'extra 'Cc) address 'all nil nil
8158                         not-matching)
8159                      (gnus-message
8160                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8161                      (sit-for 1)
8162                      t))
8163                   (from
8164                    (gnus-summary-find-matching "from" address
8165                                                'all nil nil not-matching))
8166                   (articles
8167                    (if not-matching
8168                        ;; We need the numbers that are in all lists:
8169                        (if (eq cc t)
8170                            (if (eq to t)
8171                                from
8172                              (mapcar (lambda (a) (car (memq a from))) to))
8173                          (if (eq to t)
8174                              (mapcar (lambda (a) (car (memq a from))) cc)
8175                            (mapcar (lambda (a) (car (memq a from)))
8176                                    (mapcar (lambda (a) (car (memq a to)))
8177                                            cc))))
8178                      (nconc (if (eq to t) nil to)
8179                             (if (eq cc t) nil cc)
8180                             from))))
8181              (unless articles
8182                (error "Found no matches for \"%s\"" address))
8183              (gnus-summary-limit articles))
8184       (gnus-summary-position-point))))
8185
8186 (defun gnus-summary-limit-strange-charsets-predicate (header)
8187   (when (fboundp 'char-charset)
8188     (let ((string (concat (mail-header-subject header)
8189                           (mail-header-from header)))
8190           charset found)
8191       (dotimes (i (1- (length string)))
8192         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8193         (when (string-match "unicode\\|big\\|japanese" charset)
8194           (setq found t)))
8195       found)))
8196
8197 (defun gnus-summary-limit-to-predicate (predicate)
8198   "Limit to articles where PREDICATE returns non-nil.
8199 PREDICATE will be called with the header structures of the
8200 articles."
8201   (let ((articles nil)
8202         (case-fold-search t))
8203     (dolist (header gnus-newsgroup-headers)
8204       (when (funcall predicate header)
8205         (push (mail-header-number header) articles)))
8206     (gnus-summary-limit (nreverse articles))))
8207
8208 (defun gnus-summary-limit-to-age (age &optional younger-p)
8209   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8210 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8211 articles that are younger than AGE days."
8212   (interactive
8213    (let ((younger current-prefix-arg)
8214          (days-got nil)
8215          days)
8216      (while (not days-got)
8217        (setq days (if younger
8218                       (read-string "Limit to articles younger than (in days, older when negative): ")
8219                     (read-string
8220                      "Limit to articles older than (in days, younger when negative): ")))
8221        (when (> (length days) 0)
8222          (setq days (read days)))
8223        (if (numberp days)
8224            (progn
8225              (setq days-got t)
8226              (when (< days 0)
8227                (setq younger (not younger))
8228                (setq days (* days -1))))
8229          (message "Please enter a number.")
8230          (sleep-for 1)))
8231      (list days younger)))
8232   (prog1
8233       (let ((data gnus-newsgroup-data)
8234             (cutoff (days-to-time age))
8235             articles d date is-younger)
8236         (while (setq d (pop data))
8237           (when (and (vectorp (gnus-data-header d))
8238                      (setq date (mail-header-date (gnus-data-header d))))
8239             (setq is-younger (time-less-p
8240                               (time-since (condition-case ()
8241                                               (date-to-time date)
8242                                             (error '(0 0))))
8243                               cutoff))
8244             (when (if younger-p
8245                       is-younger
8246                     (not is-younger))
8247               (push (gnus-data-number d) articles))))
8248         (gnus-summary-limit (nreverse articles)))
8249     (gnus-summary-position-point)))
8250
8251 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8252   "Limit the summary buffer to articles that match an 'extra' header."
8253   (interactive
8254    (let ((header
8255           (intern
8256            (gnus-completing-read-with-default
8257             (symbol-name (car gnus-extra-headers))
8258             (if current-prefix-arg
8259                 "Exclude extra header"
8260               "Limit extra header")
8261             (mapcar (lambda (x)
8262                       (cons (symbol-name x) x))
8263                     gnus-extra-headers)
8264             nil
8265             t))))
8266      (list header
8267            (read-string (format "%s header %s (regexp): "
8268                                 (if current-prefix-arg "Exclude" "Limit to")
8269                                 header))
8270            current-prefix-arg)))
8271   (when (not (equal "" regexp))
8272     (prog1
8273         (let ((articles (gnus-summary-find-matching
8274                          (cons 'extra header) regexp 'all nil nil
8275                          not-matching)))
8276           (unless articles
8277             (error "Found no matches for \"%s\"" regexp))
8278           (gnus-summary-limit articles))
8279       (gnus-summary-position-point))))
8280
8281 (defun gnus-summary-limit-to-display-predicate ()
8282   "Limit the summary buffer to the predicated in the `display' group parameter."
8283   (interactive)
8284   (unless gnus-newsgroup-display
8285     (error "There is no `display' group parameter"))
8286   (let (articles)
8287     (dolist (number gnus-newsgroup-articles)
8288       (when (funcall gnus-newsgroup-display)
8289         (push number articles)))
8290     (gnus-summary-limit articles))
8291   (gnus-summary-position-point))
8292
8293 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8294 (make-obsolete
8295  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread "Emacs 20.4")
8296
8297 (defun gnus-summary-limit-to-unread (&optional all)
8298   "Limit the summary buffer to articles that are not marked as read.
8299 If ALL is non-nil, limit strictly to unread articles."
8300   (interactive "P")
8301   (if all
8302       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8303     (gnus-summary-limit-to-marks
8304      ;; Concat all the marks that say that an article is read and have
8305      ;; those removed.
8306      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8307            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8308            gnus-low-score-mark gnus-expirable-mark
8309            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8310            gnus-duplicate-mark gnus-souped-mark)
8311      'reverse)))
8312
8313 (defun gnus-summary-limit-to-headers (match &optional reverse)
8314   "Limit the summary buffer to articles that have headers that match MATCH.
8315 If REVERSE (the prefix), limit to articles that don't match."
8316   (interactive "sMatch headers (regexp): \nP")
8317   (gnus-summary-limit-to-bodies match reverse t))
8318
8319 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8320   "Limit the summary buffer to articles that have bodies that match MATCH.
8321 If REVERSE (the prefix), limit to articles that don't match."
8322   (interactive "sMatch body (regexp): \nP")
8323   (let ((articles nil)
8324         (gnus-select-article-hook nil)  ;Disable hook.
8325         (gnus-article-prepare-hook nil)
8326         (gnus-use-article-prefetch nil)
8327         (gnus-keep-backlog nil)
8328         (gnus-break-pages nil)
8329         (gnus-summary-display-arrow nil)
8330         (gnus-updated-mode-lines nil)
8331         (gnus-auto-center-summary nil)
8332         (gnus-display-mime-function nil))
8333     (dolist (data gnus-newsgroup-data)
8334       (let (gnus-mark-article-hook)
8335         (gnus-summary-select-article t t nil (gnus-data-number data)))
8336       (with-current-buffer gnus-article-buffer
8337         (article-goto-body)
8338         (let* ((case-fold-search t)
8339                (found (if headersp
8340                           (re-search-backward match nil t)
8341                         (re-search-forward match nil t))))
8342           (when (or (and found
8343                          (not reverse))
8344                     (and (not found)
8345                          reverse))
8346             (push (gnus-data-number data) articles)))))
8347     (if (not articles)
8348         (message "No messages matched")
8349       (gnus-summary-limit articles)))
8350   (gnus-summary-position-point))
8351
8352 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8353   "Limit the summary buffer to articles that aren't part on any thread.
8354 If THREADSP (the prefix), limit to articles that are in threads."
8355   (interactive "P")
8356   (let ((articles nil)
8357         thread-articles
8358         threads)
8359     (dolist (thread gnus-newsgroup-threads)
8360       (if (stringp (car thread))
8361           (dolist (thread (cdr thread))
8362             (push thread threads))
8363         (push thread threads)))
8364     (dolist (thread threads)
8365       (setq thread-articles (gnus-articles-in-thread thread))
8366       (when (or (and threadsp
8367                      (> (length thread-articles) 1))
8368                 (and (not threadsp)
8369                      (= (length thread-articles) 1)))
8370         (setq articles (nconc thread-articles articles))))
8371     (if (not articles)
8372         (message "No messages matched")
8373       (gnus-summary-limit articles))
8374     (gnus-summary-position-point)))
8375
8376 (defun gnus-summary-limit-to-replied (&optional unreplied)
8377   "Limit the summary buffer to replied articles.
8378 If UNREPLIED (the prefix), limit to unreplied articles."
8379   (interactive "P")
8380   (if unreplied
8381       (gnus-summary-limit
8382        (gnus-set-difference gnus-newsgroup-articles
8383         gnus-newsgroup-replied))
8384     (gnus-summary-limit gnus-newsgroup-replied))
8385   (gnus-summary-position-point))
8386
8387 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8388 (make-obsolete 'gnus-summary-delete-marked-with
8389                'gnus-summary-limit-exclude-marks "Emacs 20.4")
8390
8391 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8392   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8393 If REVERSE, limit the summary buffer to articles that are marked
8394 with MARKS.  MARKS can either be a string of marks or a list of marks.
8395 Returns how many articles were removed."
8396   (interactive "sMarks: ")
8397   (gnus-summary-limit-to-marks marks t))
8398
8399 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8400   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8401 If REVERSE (the prefix), limit the summary buffer to articles that are
8402 not marked with MARKS.  MARKS can either be a string of marks or a
8403 list of marks.
8404 Returns how many articles were removed."
8405   (interactive "sMarks: \nP")
8406   (prog1
8407       (let ((data gnus-newsgroup-data)
8408             (marks (if (listp marks) marks
8409                      (append marks nil))) ; Transform to list.
8410             articles)
8411         (while data
8412           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8413                   (memq (gnus-data-mark (car data)) marks))
8414             (push (gnus-data-number (car data)) articles))
8415           (setq data (cdr data)))
8416         (gnus-summary-limit articles))
8417     (gnus-summary-position-point)))
8418
8419 (defun gnus-summary-limit-to-score (score)
8420   "Limit to articles with score at or above SCORE."
8421   (interactive "NLimit to articles with score of at least: ")
8422   (let ((data gnus-newsgroup-data)
8423         articles)
8424     (while data
8425       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8426                 score)
8427         (push (gnus-data-number (car data)) articles))
8428       (setq data (cdr data)))
8429     (prog1
8430         (gnus-summary-limit articles)
8431       (gnus-summary-position-point))))
8432
8433 (defun gnus-summary-limit-to-unseen ()
8434   "Limit to unseen articles."
8435   (interactive)
8436   (prog1
8437       (gnus-summary-limit gnus-newsgroup-unseen)
8438     (gnus-summary-position-point)))
8439
8440 (defun gnus-summary-limit-include-thread (id)
8441   "Display all the hidden articles that is in the thread with ID in it.
8442 When called interactively, ID is the Message-ID of the current
8443 article."
8444   (interactive (list (mail-header-id (gnus-summary-article-header))))
8445   (let ((articles (gnus-articles-in-thread
8446                    (gnus-id-to-thread (gnus-root-id id)))))
8447     (prog1
8448         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8449       (gnus-summary-limit-include-matching-articles
8450        "subject"
8451        (regexp-quote (gnus-simplify-subject-re
8452                       (mail-header-subject (gnus-id-to-header id)))))
8453       (gnus-summary-position-point))))
8454
8455 (defun gnus-summary-limit-include-matching-articles (header regexp)
8456   "Display all the hidden articles that have HEADERs that match REGEXP."
8457   (interactive (list (read-string "Match on header: ")
8458                      (read-string "Regexp: ")))
8459   (let ((articles (gnus-find-matching-articles header regexp)))
8460     (prog1
8461         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8462       (gnus-summary-position-point))))
8463
8464 (defun gnus-summary-insert-dormant-articles ()
8465   "Insert all the dormant articles for this group into the current buffer."
8466   (interactive)
8467   (let ((gnus-verbose (max 6 gnus-verbose)))
8468     (if (not gnus-newsgroup-dormant)
8469         (gnus-message 3 "No dormant articles for this group")
8470       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8471
8472 (defun gnus-summary-insert-ticked-articles ()
8473   "Insert ticked articles for this group into the current buffer."
8474   (interactive)
8475   (let ((gnus-verbose (max 6 gnus-verbose)))
8476     (if (not gnus-newsgroup-marked)
8477         (gnus-message 3 "No ticked articles for this group")
8478       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8479
8480 (defun gnus-summary-limit-include-dormant ()
8481   "Display all the hidden articles that are marked as dormant.
8482 Note that this command only works on a subset of the articles currently
8483 fetched for this group."
8484   (interactive)
8485   (unless gnus-newsgroup-dormant
8486     (error "There are no dormant articles in this group"))
8487   (prog1
8488       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8489     (gnus-summary-position-point)))
8490
8491 (defun gnus-summary-limit-exclude-dormant ()
8492   "Hide all dormant articles."
8493   (interactive)
8494   (prog1
8495       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8496     (gnus-summary-position-point)))
8497
8498 (defun gnus-summary-limit-exclude-childless-dormant ()
8499   "Hide all dormant articles that have no children."
8500   (interactive)
8501   (let ((data (gnus-data-list t))
8502         articles d children)
8503     ;; Find all articles that are either not dormant or have
8504     ;; children.
8505     (while (setq d (pop data))
8506       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8507                 (and (setq children
8508                            (gnus-article-children (gnus-data-number d)))
8509                      (let (found)
8510                        (while children
8511                          (when (memq (car children) articles)
8512                            (setq children nil
8513                                  found t))
8514                          (pop children))
8515                        found)))
8516         (push (gnus-data-number d) articles)))
8517     ;; Do the limiting.
8518     (prog1
8519         (gnus-summary-limit articles)
8520       (gnus-summary-position-point))))
8521
8522 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8523   "Mark all unread excluded articles as read.
8524 If ALL, mark even excluded ticked and dormants as read."
8525   (interactive "P")
8526   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8527   (let ((articles (gnus-sorted-ndifference
8528                    (sort
8529                     (mapcar (lambda (h) (mail-header-number h))
8530                             gnus-newsgroup-headers)
8531                     '<)
8532                    gnus-newsgroup-limit))
8533         article)
8534     (setq gnus-newsgroup-unreads
8535           (gnus-sorted-intersection gnus-newsgroup-unreads
8536                                     gnus-newsgroup-limit))
8537     (if all
8538         (setq gnus-newsgroup-dormant nil
8539               gnus-newsgroup-marked nil
8540               gnus-newsgroup-reads
8541               (nconc
8542                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8543                gnus-newsgroup-reads))
8544       (while (setq article (pop articles))
8545         (unless (or (memq article gnus-newsgroup-dormant)
8546                     (memq article gnus-newsgroup-marked))
8547           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8548
8549 (defun gnus-summary-limit (articles &optional pop)
8550   (if pop
8551       ;; We pop the previous limit off the stack and use that.
8552       (setq articles (car gnus-newsgroup-limits)
8553             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8554     ;; We use the new limit, so we push the old limit on the stack.
8555     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8556   ;; Set the limit.
8557   (setq gnus-newsgroup-limit articles)
8558   (let ((total (length gnus-newsgroup-data))
8559         (data (gnus-data-find-list (gnus-summary-article-number)))
8560         (gnus-summary-mark-below nil)   ; Inhibit this.
8561         found)
8562     ;; This will do all the work of generating the new summary buffer
8563     ;; according to the new limit.
8564     (gnus-summary-prepare)
8565     ;; Hide any threads, possibly.
8566     (gnus-summary-maybe-hide-threads)
8567     ;; Try to return to the article you were at, or one in the
8568     ;; neighborhood.
8569     (when data
8570       ;; We try to find some article after the current one.
8571       (while data
8572         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8573           (setq data nil
8574                 found t))
8575         (setq data (cdr data))))
8576     (unless found
8577       ;; If there is no data, that means that we were after the last
8578       ;; article.  The same goes when we can't find any articles
8579       ;; after the current one.
8580       (goto-char (point-max))
8581       (gnus-summary-find-prev))
8582     (gnus-set-mode-line 'summary)
8583     ;; We return how many articles were removed from the summary
8584     ;; buffer as a result of the new limit.
8585     (- total (length gnus-newsgroup-data))))
8586
8587 (defsubst gnus-invisible-cut-children (threads)
8588   (let ((num 0))
8589     (while threads
8590       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8591         (incf num))
8592       (pop threads))
8593     (< num 2)))
8594
8595 (defsubst gnus-cut-thread (thread)
8596   "Go forwards in the thread until we find an article that we want to display."
8597   (when (or (eq gnus-fetch-old-headers 'some)
8598             (eq gnus-fetch-old-headers 'invisible)
8599             (numberp gnus-fetch-old-headers)
8600             (eq gnus-build-sparse-threads 'some)
8601             (eq gnus-build-sparse-threads 'more))
8602     ;; Deal with old-fetched headers and sparse threads.
8603     (while (and
8604             thread
8605             (or
8606              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8607              (gnus-summary-article-ancient-p
8608               (mail-header-number (car thread))))
8609             (if (or (<= (length (cdr thread)) 1)
8610                     (eq gnus-fetch-old-headers 'invisible))
8611                 (setq gnus-newsgroup-limit
8612                       (delq (mail-header-number (car thread))
8613                             gnus-newsgroup-limit)
8614                       thread (cadr thread))
8615               (when (gnus-invisible-cut-children (cdr thread))
8616                 (let ((th (cdr thread)))
8617                   (while th
8618                     (if (memq (mail-header-number (caar th))
8619                               gnus-newsgroup-limit)
8620                         (setq thread (car th)
8621                               th nil)
8622                       (setq th (cdr th))))))))))
8623   thread)
8624
8625 (defun gnus-cut-threads (threads)
8626   "Cut off all uninteresting articles from the beginning of THREADS."
8627   (when (or (eq gnus-fetch-old-headers 'some)
8628             (eq gnus-fetch-old-headers 'invisible)
8629             (numberp gnus-fetch-old-headers)
8630             (eq gnus-build-sparse-threads 'some)
8631             (eq gnus-build-sparse-threads 'more))
8632     (let ((th threads))
8633       (while th
8634         (setcar th (gnus-cut-thread (car th)))
8635         (setq th (cdr th)))))
8636   ;; Remove nixed out threads.
8637   (delq nil threads))
8638
8639 (defun gnus-summary-initial-limit (&optional show-if-empty)
8640   "Figure out what the initial limit is supposed to be on group entry.
8641 This entails weeding out unwanted dormants, low-scored articles,
8642 fetch-old-headers verbiage, and so on."
8643   ;; Most groups have nothing to remove.
8644   (unless (or gnus-inhibit-limiting
8645               (and (null gnus-newsgroup-dormant)
8646                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8647                    (not (eq gnus-fetch-old-headers 'some))
8648                    (not (numberp gnus-fetch-old-headers))
8649                    (not (eq gnus-fetch-old-headers 'invisible))
8650                    (null gnus-summary-expunge-below)
8651                    (not (eq gnus-build-sparse-threads 'some))
8652                    (not (eq gnus-build-sparse-threads 'more))
8653                    (null gnus-thread-expunge-below)
8654                    (not gnus-use-nocem)))
8655     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8656     (setq gnus-newsgroup-limit nil)
8657     (mapatoms
8658      (lambda (node)
8659        (unless (car (symbol-value node))
8660          ;; These threads have no parents -- they are roots.
8661          (let ((nodes (cdr (symbol-value node)))
8662                thread)
8663            (while nodes
8664              (if (and gnus-thread-expunge-below
8665                       (< (gnus-thread-total-score (car nodes))
8666                          gnus-thread-expunge-below))
8667                  (gnus-expunge-thread (pop nodes))
8668                (setq thread (pop nodes))
8669                (gnus-summary-limit-children thread))))))
8670      gnus-newsgroup-dependencies)
8671     ;; If this limitation resulted in an empty group, we might
8672     ;; pop the previous limit and use it instead.
8673     (when (and (not gnus-newsgroup-limit)
8674                show-if-empty)
8675       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8676     gnus-newsgroup-limit))
8677
8678 (defun gnus-summary-limit-children (thread)
8679   "Return 1 if this subthread is visible and 0 if it is not."
8680   ;; First we get the number of visible children to this thread.  This
8681   ;; is done by recursing down the thread using this function, so this
8682   ;; will really go down to a leaf article first, before slowly
8683   ;; working its way up towards the root.
8684   (when thread
8685     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8686            (children
8687            (if (cdr thread)
8688                (apply '+ (mapcar 'gnus-summary-limit-children
8689                                  (cdr thread)))
8690              0))
8691           (number (mail-header-number (car thread)))
8692           score)
8693       (if (and
8694            (not (memq number gnus-newsgroup-marked))
8695            (or
8696             ;; If this article is dormant and has absolutely no visible
8697             ;; children, then this article isn't visible.
8698             (and (memq number gnus-newsgroup-dormant)
8699                  (zerop children))
8700             ;; If this is "fetch-old-headered" and there is no
8701             ;; visible children, then we don't want this article.
8702             (and (or (eq gnus-fetch-old-headers 'some)
8703                      (numberp gnus-fetch-old-headers))
8704                  (gnus-summary-article-ancient-p number)
8705                  (zerop children))
8706             ;; If this is "fetch-old-headered" and `invisible', then
8707             ;; we don't want this article.
8708             (and (eq gnus-fetch-old-headers 'invisible)
8709                  (gnus-summary-article-ancient-p number))
8710             ;; If this is a sparsely inserted article with no children,
8711             ;; we don't want it.
8712             (and (eq gnus-build-sparse-threads 'some)
8713                  (gnus-summary-article-sparse-p number)
8714                  (zerop children))
8715             ;; If we use expunging, and this article is really
8716             ;; low-scored, then we don't want this article.
8717             (when (and gnus-summary-expunge-below
8718                        (< (setq score
8719                                 (or (cdr (assq number gnus-newsgroup-scored))
8720                                     gnus-summary-default-score))
8721                           gnus-summary-expunge-below))
8722               ;; We increase the expunge-tally here, but that has
8723               ;; nothing to do with the limits, really.
8724               (incf gnus-newsgroup-expunged-tally)
8725               ;; We also mark as read here, if that's wanted.
8726               (when (and gnus-summary-mark-below
8727                          (< score gnus-summary-mark-below))
8728                 (setq gnus-newsgroup-unreads
8729                       (delq number gnus-newsgroup-unreads))
8730                 (if gnus-newsgroup-auto-expire
8731                     (push number gnus-newsgroup-expirable)
8732                   (push (cons number gnus-low-score-mark)
8733                         gnus-newsgroup-reads)))
8734               t)
8735             ;; Do the `display' group parameter.
8736             (and gnus-newsgroup-display
8737                  (not (funcall gnus-newsgroup-display)))
8738             ;; Check NoCeM things.
8739             (when (and gnus-use-nocem
8740                        (gnus-nocem-unwanted-article-p
8741                         (mail-header-id (car thread))))
8742               (setq gnus-newsgroup-unreads
8743                     (delq number gnus-newsgroup-unreads))
8744               t)))
8745           ;; Nope, invisible article.
8746           0
8747         ;; Ok, this article is to be visible, so we add it to the limit
8748         ;; and return 1.
8749         (push number gnus-newsgroup-limit)
8750         1))))
8751
8752 (defun gnus-expunge-thread (thread)
8753   "Mark all articles in THREAD as read."
8754   (let* ((number (mail-header-number (car thread))))
8755     (incf gnus-newsgroup-expunged-tally)
8756     ;; We also mark as read here, if that's wanted.
8757     (setq gnus-newsgroup-unreads
8758           (delq number gnus-newsgroup-unreads))
8759     (if gnus-newsgroup-auto-expire
8760         (push number gnus-newsgroup-expirable)
8761       (push (cons number gnus-low-score-mark)
8762             gnus-newsgroup-reads)))
8763   ;; Go recursively through all subthreads.
8764   (mapcar 'gnus-expunge-thread (cdr thread)))
8765
8766 ;; Summary article oriented commands
8767
8768 (defun gnus-summary-refer-parent-article (n)
8769   "Refer parent article N times.
8770 If N is negative, go to ancestor -N instead.
8771 The difference between N and the number of articles fetched is returned."
8772   (interactive "p")
8773   (let ((skip 1)
8774         error header ref)
8775     (when (not (natnump n))
8776       (setq skip (abs n)
8777             n 1))
8778     (while (and (> n 0)
8779                 (not error))
8780       (setq header (gnus-summary-article-header))
8781       (if (and (eq (mail-header-number header)
8782                    (cdr gnus-article-current))
8783                (equal gnus-newsgroup-name
8784                       (car gnus-article-current)))
8785           ;; If we try to find the parent of the currently
8786           ;; displayed article, then we take a look at the actual
8787           ;; References header, since this is slightly more
8788           ;; reliable than the References field we got from the
8789           ;; server.
8790           (with-current-buffer gnus-original-article-buffer
8791             (nnheader-narrow-to-headers)
8792             (unless (setq ref (message-fetch-field "references"))
8793               (when (setq ref (message-fetch-field "in-reply-to"))
8794                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8795             (widen))
8796         (setq ref
8797               ;; It's not the current article, so we take a bet on
8798               ;; the value we got from the server.
8799               (mail-header-references header)))
8800       (if (and ref
8801                (not (equal ref "")))
8802           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8803             (gnus-message 1 "Couldn't find parent"))
8804         (gnus-message 1 "No references in article %d"
8805                       (gnus-summary-article-number))
8806         (setq error t))
8807       (decf n))
8808     (gnus-summary-position-point)
8809     n))
8810
8811 (defun gnus-summary-refer-references ()
8812   "Fetch all articles mentioned in the References header.
8813 Return the number of articles fetched."
8814   (interactive)
8815   (let ((ref (mail-header-references (gnus-summary-article-header)))
8816         (current (gnus-summary-article-number))
8817         (n 0))
8818     (if (or (not ref)
8819             (equal ref ""))
8820         (error "No References in the current article")
8821       ;; For each Message-ID in the References header...
8822       (while (string-match "<[^>]*>" ref)
8823         (incf n)
8824         ;; ... fetch that article.
8825         (gnus-summary-refer-article
8826          (prog1 (match-string 0 ref)
8827            (setq ref (substring ref (match-end 0))))))
8828       (gnus-summary-goto-subject current)
8829       (gnus-summary-position-point)
8830       n)))
8831
8832 (defun gnus-summary-refer-thread (&optional limit)
8833   "Fetch all articles in the current thread.
8834 If LIMIT (the numerical prefix), fetch that many old headers instead
8835 of what's specified by the `gnus-refer-thread-limit' variable."
8836   (interactive "P")
8837   (let ((id (mail-header-id (gnus-summary-article-header)))
8838         (limit (if limit (prefix-numeric-value limit)
8839                  gnus-refer-thread-limit)))
8840     (unless (eq gnus-fetch-old-headers 'invisible)
8841       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8842       ;; Retrieve the headers and read them in.
8843       (if (eq (if (numberp limit)
8844                   (gnus-retrieve-headers
8845                    (list (min
8846                           (+ (mail-header-number
8847                               (gnus-summary-article-header))
8848                              limit)
8849                           gnus-newsgroup-end))
8850                    gnus-newsgroup-name (* limit 2))
8851                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8852                 ;; headers.
8853                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8854                                        gnus-newsgroup-name limit))
8855               'nov)
8856           (gnus-build-all-threads)
8857         (error "Can't fetch thread from back ends that don't support NOV"))
8858       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8859     (gnus-summary-limit-include-thread id)))
8860
8861 (defun gnus-summary-refer-article (message-id)
8862   "Fetch an article specified by MESSAGE-ID."
8863   (interactive "sMessage-ID: ")
8864   (when (and (stringp message-id)
8865              (not (zerop (length message-id))))
8866     (setq message-id (gnus-replace-in-string message-id " " ""))
8867     ;; Construct the correct Message-ID if necessary.
8868     ;; Suggested by tale@pawl.rpi.edu.
8869     (unless (string-match "^<" message-id)
8870       (setq message-id (concat "<" message-id)))
8871     (unless (string-match ">$" message-id)
8872       (setq message-id (concat message-id ">")))
8873     ;; People often post MIDs from URLs, so unhex it:
8874     (unless (string-match "@" message-id)
8875       (setq message-id (gnus-url-unhex-string message-id)))
8876     (let* ((header (gnus-id-to-header message-id))
8877            (sparse (and header
8878                         (gnus-summary-article-sparse-p
8879                          (mail-header-number header))
8880                         (memq (mail-header-number header)
8881                               gnus-newsgroup-limit)))
8882            number)
8883       (cond
8884        ;; If the article is present in the buffer we just go to it.
8885        ((and header
8886              (or (not (gnus-summary-article-sparse-p
8887                        (mail-header-number header)))
8888                  sparse))
8889         (prog1
8890             (gnus-summary-goto-article
8891              (mail-header-number header) nil t)
8892           (when sparse
8893             (gnus-summary-update-article (mail-header-number header)))))
8894        (t
8895         ;; We fetch the article.
8896         (catch 'found
8897           (dolist (gnus-override-method (gnus-refer-article-methods))
8898             (when (and (gnus-check-server gnus-override-method)
8899                        ;; Fetch the header,
8900                        (setq number (gnus-summary-insert-subject message-id)))
8901               ;; and display the article.
8902               (gnus-summary-select-article nil nil nil number)
8903               (throw 'found t)))
8904           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8905
8906 (defun gnus-refer-article-methods ()
8907   "Return a list of referable methods."
8908   (cond
8909    ;; No method, so we default to current and native.
8910    ((null gnus-refer-article-method)
8911     (list gnus-current-select-method gnus-select-method))
8912    ;; Current.
8913    ((eq 'current gnus-refer-article-method)
8914     (list gnus-current-select-method))
8915    ;; List of select methods.
8916    ((not (and (symbolp (car gnus-refer-article-method))
8917               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8918     (let (out)
8919       (dolist (method gnus-refer-article-method)
8920         (push (if (eq 'current method)
8921                   gnus-current-select-method
8922                 method)
8923               out))
8924       (nreverse out)))
8925    ;; One single select method.
8926    (t
8927     (list gnus-refer-article-method))))
8928
8929 (defun gnus-summary-edit-parameters ()
8930   "Edit the group parameters of the current group."
8931   (interactive)
8932   (gnus-group-edit-group gnus-newsgroup-name 'params))
8933
8934 (defun gnus-summary-customize-parameters ()
8935   "Customize the group parameters of the current group."
8936   (interactive)
8937   (gnus-group-customize gnus-newsgroup-name))
8938
8939 (defun gnus-summary-enter-digest-group (&optional force)
8940   "Enter an nndoc group based on the current article.
8941 If FORCE, force a digest interpretation.  If not, try
8942 to guess what the document format is."
8943   (interactive "P")
8944   (let ((conf gnus-current-window-configuration))
8945     (save-window-excursion
8946       (save-excursion
8947         (let (gnus-article-prepare-hook
8948               gnus-display-mime-function
8949               gnus-break-pages)
8950           (gnus-summary-select-article))))
8951     (setq gnus-current-window-configuration conf)
8952     (let* ((name (format "%s-%d"
8953                          (gnus-group-prefixed-name
8954                           gnus-newsgroup-name (list 'nndoc ""))
8955                          (with-current-buffer gnus-summary-buffer
8956                            gnus-current-article)))
8957            (ogroup gnus-newsgroup-name)
8958            (params (append (gnus-info-params (gnus-get-info ogroup))
8959                            (list (cons 'to-group ogroup))
8960                            (list (cons 'parent-group ogroup))
8961                            (list (cons 'save-article-group ogroup))))
8962            (case-fold-search t)
8963            (buf (current-buffer))
8964            dig to-address)
8965       (with-current-buffer gnus-original-article-buffer
8966         ;; Have the digest group inherit the main mail address of
8967         ;; the parent article.
8968         (when (setq to-address (or (gnus-fetch-field "reply-to")
8969                                    (gnus-fetch-field "from")))
8970           (setq params
8971                 (append
8972                  (list (cons 'to-address
8973                              (funcall gnus-decode-encoded-address-function
8974                                       to-address))))))
8975         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8976         (insert-buffer-substring gnus-original-article-buffer)
8977         ;; Remove lines that may lead nndoc to misinterpret the
8978         ;; document type.
8979         (narrow-to-region
8980          (goto-char (point-min))
8981          (or (search-forward "\n\n" nil t) (point)))
8982         (goto-char (point-min))
8983         (delete-matching-lines "^Path:\\|^From ")
8984         (widen))
8985       (unwind-protect
8986           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8987                     (gnus-newsgroup-ephemeral-ignored-charsets
8988                      gnus-newsgroup-ignored-charsets))
8989                 (gnus-group-read-ephemeral-group
8990                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8991                               (nndoc-article-type
8992                                ,(if force 'mbox 'guess)))
8993                  t nil nil nil
8994                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8995                                                         "ADAPT")))))
8996               ;; Make all postings to this group go to the parent group.
8997               (nconc (gnus-info-params (gnus-get-info name))
8998                      params)
8999             ;; Couldn't select this doc group.
9000             (switch-to-buffer buf)
9001             (gnus-set-global-variables)
9002             (gnus-configure-windows 'summary)
9003             (gnus-message 3 "Article couldn't be entered?"))
9004         (kill-buffer dig)))))
9005
9006 (defun gnus-summary-read-document (n)
9007   "Open a new group based on the current article(s).
9008 This will allow you to read digests and other similar
9009 documents as newsgroups.
9010 Obeys the standard process/prefix convention."
9011   (interactive "P")
9012   (let* ((ogroup gnus-newsgroup-name)
9013          (params (append (gnus-info-params (gnus-get-info ogroup))
9014                          (list (cons 'to-group ogroup))))
9015          group egroup groups vgroup)
9016     (dolist (article (gnus-summary-work-articles n))
9017       (setq group (format "%s-%d" gnus-newsgroup-name article))
9018       (gnus-summary-remove-process-mark article)
9019       (when (gnus-summary-display-article article)
9020         (save-excursion ;;What for?
9021           (with-temp-buffer
9022             (insert-buffer-substring gnus-original-article-buffer)
9023             ;; Remove some headers that may lead nndoc to make
9024             ;; the wrong guess.
9025             (message-narrow-to-head)
9026             (goto-char (point-min))
9027             (delete-matching-lines "^Path:\\|^From ")
9028             (widen)
9029             (if (setq egroup
9030                       (gnus-group-read-ephemeral-group
9031                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9032                                      (nndoc-article-type guess))
9033                        t nil t))
9034                 (progn
9035                   ;; Make all postings to this group go to the parent group.
9036                   (nconc (gnus-info-params (gnus-get-info egroup))
9037                          params)
9038                   (push egroup groups))
9039               ;; Couldn't select this doc group.
9040               (gnus-error 3 "Article couldn't be entered"))))))
9041     ;; Now we have selected all the documents.
9042     (cond
9043      ((not groups)
9044       (error "None of the articles could be interpreted as documents"))
9045      ((gnus-group-read-ephemeral-group
9046        (setq vgroup (format
9047                      "nnvirtual:%s-%s" gnus-newsgroup-name
9048                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9049        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9050        t
9051        (cons (current-buffer) 'summary)))
9052      (t
9053       (error "Couldn't select virtual nndoc group")))))
9054
9055 (defun gnus-summary-isearch-article (&optional regexp-p)
9056   "Do incremental search forward on the current article.
9057 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9058   (interactive "P")
9059   (gnus-summary-select-article)
9060   (gnus-configure-windows 'article)
9061   (gnus-eval-in-buffer-window gnus-article-buffer
9062     (save-restriction
9063       (widen)
9064       (isearch-forward regexp-p))))
9065
9066 (defun gnus-summary-repeat-search-article-forward ()
9067   "Repeat the previous search forwards."
9068   (interactive)
9069   (unless gnus-last-search-regexp
9070     (error "No previous search"))
9071   (gnus-summary-search-article-forward gnus-last-search-regexp))
9072
9073 (defun gnus-summary-repeat-search-article-backward ()
9074   "Repeat the previous search backwards."
9075   (interactive)
9076   (unless gnus-last-search-regexp
9077     (error "No previous search"))
9078   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9079
9080 (defun gnus-summary-search-article-forward (regexp &optional backward)
9081   "Search for an article containing REGEXP forward.
9082 If BACKWARD, search backward instead."
9083   (interactive
9084    (list (read-string
9085           (format "Search article %s (regexp%s): "
9086                   (if current-prefix-arg "backward" "forward")
9087                   (if gnus-last-search-regexp
9088                       (concat ", default " gnus-last-search-regexp)
9089                     "")))
9090          current-prefix-arg))
9091   (if (string-equal regexp "")
9092       (setq regexp (or gnus-last-search-regexp ""))
9093     (setq gnus-last-search-regexp regexp)
9094     (setq gnus-article-before-search gnus-current-article))
9095   ;; Intentionally set gnus-last-article.
9096   (setq gnus-last-article gnus-article-before-search)
9097   (let ((gnus-last-article gnus-last-article))
9098     (if (gnus-summary-search-article regexp backward)
9099         (gnus-summary-show-thread)
9100       (signal 'search-failed (list regexp)))))
9101
9102 (defun gnus-summary-search-article-backward (regexp)
9103   "Search for an article containing REGEXP backward."
9104   (interactive
9105    (list (read-string
9106           (format "Search article backward (regexp%s): "
9107                   (if gnus-last-search-regexp
9108                       (concat ", default " gnus-last-search-regexp)
9109                     "")))))
9110   (gnus-summary-search-article-forward regexp 'backward))
9111
9112 (defun gnus-summary-search-article (regexp &optional backward)
9113   "Search for an article containing REGEXP.
9114 Optional argument BACKWARD means do search for backward.
9115 `gnus-select-article-hook' is not called during the search."
9116   ;; We have to require this here to make sure that the following
9117   ;; dynamic binding isn't shadowed by autoloading.
9118   (require 'gnus-async)
9119   (require 'gnus-art)
9120   (let ((gnus-select-article-hook nil)  ;Disable hook.
9121         (gnus-article-prepare-hook nil)
9122         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9123         (gnus-use-article-prefetch nil)
9124         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9125         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9126         (gnus-visual nil)
9127         (gnus-keep-backlog nil)
9128         (gnus-break-pages nil)
9129         (gnus-summary-display-arrow nil)
9130         (gnus-updated-mode-lines nil)
9131         (gnus-auto-center-summary nil)
9132         (sum (current-buffer))
9133         (gnus-display-mime-function nil)
9134         (found nil)
9135         point)
9136     (gnus-save-hidden-threads
9137       (gnus-summary-select-article)
9138       (set-buffer gnus-article-buffer)
9139       (goto-char (window-point (get-buffer-window (current-buffer))))
9140       (when backward
9141         (forward-line -1))
9142       (while (not found)
9143         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9144         (if (if backward
9145                 (re-search-backward regexp nil t)
9146               (re-search-forward regexp nil t))
9147             ;; We found the regexp.
9148             (progn
9149               (setq found 'found)
9150               (beginning-of-line)
9151               (set-window-start
9152                (get-buffer-window (current-buffer))
9153                (point))
9154               (forward-line 1)
9155               (set-window-point
9156                (get-buffer-window (current-buffer))
9157                (point))
9158               (set-buffer sum)
9159               (setq point (point)))
9160           ;; We didn't find it, so we go to the next article.
9161           (set-buffer sum)
9162           (setq found 'not)
9163           (while (eq found 'not)
9164             (if (not (if backward (gnus-summary-find-prev)
9165                        (gnus-summary-find-next)))
9166                 ;; No more articles.
9167                 (setq found t)
9168               ;; Select the next article and adjust point.
9169               (unless (gnus-summary-article-sparse-p
9170                        (gnus-summary-article-number))
9171                 (setq found nil)
9172                 (gnus-summary-select-article)
9173                 (set-buffer gnus-article-buffer)
9174                 (widen)
9175                 (goto-char (if backward (point-max) (point-min))))))))
9176       (gnus-message 7 ""))
9177     ;; Return whether we found the regexp.
9178     (when (eq found 'found)
9179       (goto-char point)
9180       (gnus-summary-show-thread)
9181       (gnus-summary-goto-subject gnus-current-article)
9182       (gnus-summary-position-point)
9183       t)))
9184
9185 (defun gnus-find-matching-articles (header regexp)
9186   "Return a list of all articles that match REGEXP on HEADER.
9187 This search includes all articles in the current group that Gnus has
9188 fetched headers for, whether they are displayed or not."
9189   (let ((articles nil)
9190         ;; Can't eta-reduce because it's a macro.
9191         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9192         (case-fold-search t))
9193     (dolist (header gnus-newsgroup-headers)
9194       (when (string-match regexp (funcall func header))
9195         (push (mail-header-number header) articles)))
9196     (nreverse articles)))
9197
9198 (defun gnus-summary-find-matching (header regexp &optional backward unread
9199                                           not-case-fold not-matching)
9200   "Return a list of all articles that match REGEXP on HEADER.
9201 The search stars on the current article and goes forwards unless
9202 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9203 If UNREAD is non-nil, only unread articles will
9204 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9205 in the comparisons. If NOT-MATCHING, return a list of all articles that
9206 not match REGEXP on HEADER."
9207   (let ((case-fold-search (not not-case-fold))
9208         articles d func)
9209     (if (consp header)
9210         (if (eq (car header) 'extra)
9211             (setq func
9212                   `(lambda (h)
9213                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9214                          "")))
9215           (error "%s is an invalid header" header))
9216       (unless (fboundp (intern (concat "mail-header-" header)))
9217         (error "%s is not a valid header" header))
9218       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9219     (dolist (d (if (eq backward 'all)
9220                    gnus-newsgroup-data
9221                  (gnus-data-find-list
9222                   (gnus-summary-article-number)
9223                   (gnus-data-list backward))))
9224       (when (and (or (not unread)       ; We want all articles...
9225                      (gnus-data-unread-p d)) ; Or just unreads.
9226                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9227                  (if not-matching
9228                      (not (string-match
9229                            regexp
9230                            (funcall func (gnus-data-header d))))
9231                    (string-match regexp
9232                                  (funcall func (gnus-data-header d)))))
9233         (push (gnus-data-number d) articles))) ; Success!
9234     (nreverse articles)))
9235
9236 (defun gnus-summary-execute-command (header regexp command &optional backward)
9237   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9238 If HEADER is an empty string (or nil), the match is done on the entire
9239 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9240   (interactive
9241    (list (let ((completion-ignore-case t))
9242            (completing-read
9243             "Header name: "
9244             (mapcar (lambda (header) (list (format "%s" header)))
9245                     (append
9246                      '("Number" "Subject" "From" "Lines" "Date"
9247                        "Message-ID" "Xref" "References" "Body")
9248                      gnus-extra-headers))
9249             nil 'require-match))
9250          (read-string "Regexp: ")
9251          (read-key-sequence "Command: ")
9252          current-prefix-arg))
9253   (when (equal header "Body")
9254     (setq header ""))
9255   ;; Hidden thread subtrees must be searched as well.
9256   (gnus-summary-show-all-threads)
9257   ;; We don't want to change current point nor window configuration.
9258   (save-excursion
9259     (save-window-excursion
9260       (let (gnus-visual
9261             gnus-treat-strip-trailing-blank-lines
9262             gnus-treat-strip-leading-blank-lines
9263             gnus-treat-strip-multiple-blank-lines
9264             gnus-treat-hide-boring-headers
9265             gnus-treat-fold-newsgroups
9266             gnus-article-prepare-hook)
9267         (gnus-message 6 "Executing %s..." (key-description command))
9268         ;; We'd like to execute COMMAND interactively so as to give arguments.
9269         (gnus-execute header regexp
9270                       `(call-interactively ',(key-binding command))
9271                       backward)
9272         (gnus-message 6 "Executing %s...done" (key-description command))))))
9273
9274 (defun gnus-summary-beginning-of-article ()
9275   "Scroll the article back to the beginning."
9276   (interactive)
9277   (gnus-summary-select-article)
9278   (gnus-configure-windows 'article)
9279   (gnus-eval-in-buffer-window gnus-article-buffer
9280     (widen)
9281     (goto-char (point-min))
9282     (when gnus-break-pages
9283       (gnus-narrow-to-page))))
9284
9285 (defun gnus-summary-end-of-article ()
9286   "Scroll to the end of the article."
9287   (interactive)
9288   (gnus-summary-select-article)
9289   (gnus-configure-windows 'article)
9290   (gnus-eval-in-buffer-window gnus-article-buffer
9291     (widen)
9292     (goto-char (point-max))
9293     (recenter -3)
9294     (when gnus-break-pages
9295       (gnus-narrow-to-page))))
9296
9297 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9298   "Truncate to LEN and quote all \"(\"'s in STRING."
9299   (gnus-replace-in-string (if (and len (> (length string) len))
9300                               (substring string 0 len)
9301                             string)
9302                           "[()]" "\\\\\\&"))
9303
9304 (defun gnus-summary-print-article (&optional filename n)
9305   "Generate and print a PostScript image of the process-marked (mail) articles.
9306
9307 If used interactively, print the current article if none are
9308 process-marked.  With prefix arg, prompt the user for the name of the
9309 file to save in.
9310
9311 When used from Lisp, accept two optional args FILENAME and N.  N means
9312 to print the next N articles.  If N is negative, print the N previous
9313 articles.  If N is nil and articles have been marked with the process
9314 mark, print these instead.
9315
9316 If the optional first argument FILENAME is nil, send the image to the
9317 printer.  If FILENAME is a string, save the PostScript image in a file with
9318 that name.  If FILENAME is a number, prompt the user for the name of the file
9319 to save in."
9320   (interactive (list (ps-print-preprint current-prefix-arg)))
9321   (dolist (article (gnus-summary-work-articles n))
9322     (gnus-summary-select-article nil nil 'pseudo article)
9323     (gnus-eval-in-buffer-window gnus-article-buffer
9324       (gnus-print-buffer))
9325     (gnus-summary-remove-process-mark article))
9326   (ps-despool filename))
9327
9328 (defun gnus-print-buffer ()
9329   (let ((buffer (generate-new-buffer " *print*")))
9330     (unwind-protect
9331         (progn
9332           (copy-to-buffer buffer (point-min) (point-max))
9333           (set-buffer buffer)
9334           (gnus-remove-text-with-property 'gnus-decoration)
9335           (when (gnus-visual-p 'article-highlight 'highlight)
9336             ;; Copy-to-buffer doesn't copy overlay.  So redo
9337             ;; highlight.
9338             (let ((gnus-article-buffer buffer))
9339               (gnus-article-highlight-citation t)
9340               (gnus-article-highlight-signature)
9341               (gnus-article-emphasize)
9342               (gnus-article-delete-invisible-text)))
9343           (let ((ps-left-header
9344                  (list
9345                   (concat "("
9346                           (gnus-summary-print-truncate-and-quote
9347                            (mail-header-subject gnus-current-headers)
9348                            66) ")")
9349                   (concat "("
9350                           (gnus-summary-print-truncate-and-quote
9351                            (mail-header-from gnus-current-headers)
9352                            45) ")")))
9353                 (ps-right-header
9354                  (list
9355                   "/pagenumberstring load"
9356                   (concat "("
9357                           (mail-header-date gnus-current-headers) ")"))))
9358             (gnus-run-hooks 'gnus-ps-print-hook)
9359             (save-excursion
9360               (if ps-print-color-p
9361                   (ps-spool-buffer-with-faces)
9362                 (ps-spool-buffer)))))
9363       (kill-buffer buffer))))
9364
9365 (defun gnus-summary-show-article (&optional arg)
9366   "Force redisplaying of the current article.
9367 If ARG (the prefix) is a number, show the article with the charset
9368 defined in `gnus-summary-show-article-charset-alist', or the charset
9369 input.
9370 If ARG (the prefix) is non-nil and not a number, show the raw article
9371 without any article massaging functions being run.  Normally, the key
9372 strokes are `C-u g'."
9373   (interactive "P")
9374   (cond
9375    ((numberp arg)
9376     (gnus-summary-show-article t)
9377     (let ((gnus-newsgroup-charset
9378            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9379                (mm-read-coding-system
9380                 "View as charset: " ;; actually it is coding system.
9381                 (with-current-buffer gnus-article-buffer
9382                   (mm-detect-coding-region (point) (point-max))))))
9383           (gnus-newsgroup-ignored-charsets 'gnus-all))
9384       (gnus-summary-select-article nil 'force)
9385       (let ((deps gnus-newsgroup-dependencies)
9386             head header lines)
9387         (with-current-buffer gnus-original-article-buffer
9388           (save-restriction
9389             (message-narrow-to-head)
9390             (setq head (buffer-string))
9391             (goto-char (point-min))
9392             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9393               (goto-char (point-max))
9394               (widen)
9395               (setq lines (1- (count-lines (point) (point-max))))))
9396           (with-temp-buffer
9397             (insert (format "211 %d Article retrieved.\n"
9398                             (cdr gnus-article-current)))
9399             (insert head)
9400             (if lines (insert (format "Lines: %d\n" lines)))
9401             (insert ".\n")
9402             (let ((nntp-server-buffer (current-buffer)))
9403               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9404         (gnus-data-set-header
9405          (gnus-data-find (cdr gnus-article-current))
9406          header)
9407         (gnus-summary-update-article-line
9408          (cdr gnus-article-current) header)
9409         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9410           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9411    ((not arg)
9412     ;; Select the article the normal way.
9413     (gnus-summary-select-article nil 'force))
9414    (t
9415     ;; We have to require this here to make sure that the following
9416     ;; dynamic binding isn't shadowed by autoloading.
9417     (require 'gnus-async)
9418     (require 'gnus-art)
9419     ;; Bind the article treatment functions to nil.
9420     (let ((gnus-have-all-headers t)
9421           gnus-article-prepare-hook
9422           gnus-article-decode-hook
9423           gnus-display-mime-function
9424           gnus-break-pages)
9425       ;; Destroy any MIME parts.
9426       (when (gnus-buffer-live-p gnus-article-buffer)
9427         (with-current-buffer gnus-article-buffer
9428           (mm-destroy-parts gnus-article-mime-handles)
9429           ;; Set it to nil for safety reason.
9430           (setq gnus-article-mime-handle-alist nil)
9431           (setq gnus-article-mime-handles nil)))
9432       (gnus-summary-select-article nil 'force))))
9433   (gnus-summary-goto-subject gnus-current-article)
9434   (gnus-summary-position-point))
9435
9436 (defun gnus-summary-show-raw-article ()
9437   "Show the raw article without any article massaging functions being run."
9438   (interactive)
9439   (gnus-summary-show-article t))
9440
9441 (defun gnus-summary-verbose-headers (&optional arg)
9442   "Toggle permanent full header display.
9443 If ARG is a positive number, turn header display on.
9444 If ARG is a negative number, turn header display off."
9445   (interactive "P")
9446   (setq gnus-show-all-headers
9447         (cond ((or (not (numberp arg))
9448                    (zerop arg))
9449                (not gnus-show-all-headers))
9450               ((natnump arg)
9451                t)))
9452   (gnus-summary-show-article))
9453
9454 (defun gnus-summary-toggle-header (&optional arg)
9455   "Show the headers if they are hidden, or hide them if they are shown.
9456 If ARG is a positive number, show the entire header.
9457 If ARG is a negative number, hide the unwanted header lines."
9458   (interactive "P")
9459   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9460                      (get-buffer-window gnus-article-buffer t))))
9461     (with-current-buffer gnus-article-buffer
9462       (widen)
9463       (article-narrow-to-head)
9464       (let* ((inhibit-read-only t)
9465              (inhibit-point-motion-hooks t)
9466              (hidden (if (numberp arg)
9467                          (>= arg 0)
9468                        (or (not (looking-at "[^ \t\n]+:"))
9469                            (gnus-article-hidden-text-p 'headers))))
9470              s e)
9471         (delete-region (point-min) (point-max))
9472         (with-current-buffer gnus-original-article-buffer
9473           (goto-char (setq s (point-min)))
9474           (setq e (if (search-forward "\n\n" nil t)
9475                       (1- (point))
9476                     (point-max))))
9477         (insert-buffer-substring gnus-original-article-buffer s e)
9478         (run-hooks 'gnus-article-decode-hook)
9479         (if hidden
9480             (let ((gnus-treat-hide-headers nil)
9481                   (gnus-treat-hide-boring-headers nil))
9482               (gnus-delete-wash-type 'headers)
9483               (gnus-treat-article 'head))
9484           (gnus-treat-article 'head))
9485         (widen)
9486         (if window
9487             (set-window-start window (goto-char (point-min))))
9488         (if gnus-break-pages
9489             (gnus-narrow-to-page)
9490           (when (gnus-visual-p 'page-marker)
9491             (let ((inhibit-read-only t))
9492               (gnus-remove-text-with-property 'gnus-prev)
9493               (gnus-remove-text-with-property 'gnus-next))))
9494         (gnus-set-mode-line 'article)))))
9495
9496 (defun gnus-summary-show-all-headers ()
9497   "Make all header lines visible."
9498   (interactive)
9499   (gnus-summary-toggle-header 1))
9500
9501 (defun gnus-summary-caesar-message (&optional arg)
9502   "Caesar rotate the current article by 13.
9503 With a non-numerical prefix, also rotate headers.  A numerical
9504 prefix specifies how many places to rotate each letter forward."
9505   (interactive "P")
9506   (gnus-summary-select-article)
9507   (let ((mail-header-separator ""))
9508     (gnus-eval-in-buffer-window gnus-article-buffer
9509       (save-restriction
9510         (widen)
9511         (let ((start (window-start))
9512               (inhibit-read-only t))
9513           (if (equal arg '(4))
9514               (message-caesar-buffer-body nil t)
9515             (message-caesar-buffer-body arg))
9516           (set-window-start (get-buffer-window (current-buffer)) start)))))
9517   ;; Create buttons and stuff...
9518   (gnus-treat-article nil))
9519
9520 (declare-function idna-to-unicode "ext:idna" (str))
9521
9522 (defun gnus-summary-idna-message (&optional arg)
9523   "Decode IDNA encoded domain names in the current articles.
9524 IDNA encoded domain names looks like `xn--bar'.  If a string
9525 remain unencoded after running this function, it is likely an
9526 invalid IDNA string (`xn--bar' is invalid).
9527
9528 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9529 installed for this command to work."
9530   (interactive "P")
9531   (if (not (and (condition-case nil (require 'idna)
9532                   (file-error))
9533                 (mm-coding-system-p 'utf-8)
9534                 (executable-find (symbol-value 'idna-program))))
9535       (gnus-message
9536        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9537     (gnus-summary-select-article)
9538     (let ((mail-header-separator ""))
9539       (gnus-eval-in-buffer-window gnus-article-buffer
9540         (save-restriction
9541           (widen)
9542           (let ((start (window-start))
9543                 buffer-read-only)
9544             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9545               (replace-match (idna-to-unicode (match-string 1))))
9546             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9547
9548 (defun gnus-summary-morse-message (&optional arg)
9549   "Morse decode the current article."
9550   (interactive "P")
9551   (gnus-summary-select-article)
9552   (let ((mail-header-separator ""))
9553     (gnus-eval-in-buffer-window gnus-article-buffer
9554       (save-excursion
9555         (save-restriction
9556           (widen)
9557           (let ((pos (window-start))
9558                 (inhibit-read-only t))
9559             (goto-char (point-min))
9560             (when (message-goto-body)
9561               (gnus-narrow-to-body))
9562             (goto-char (point-min))
9563             (while (search-forward "·" (point-max) t)
9564               (replace-match "."))
9565             (unmorse-region (point-min) (point-max))
9566             (widen)
9567             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9568
9569 (defun gnus-summary-stop-page-breaking ()
9570   "Stop page breaking in the current article."
9571   (interactive)
9572   (gnus-summary-select-article)
9573   (gnus-eval-in-buffer-window gnus-article-buffer
9574     (widen)
9575     (when (gnus-visual-p 'page-marker)
9576       (let ((inhibit-read-only t))
9577         (gnus-remove-text-with-property 'gnus-prev)
9578         (gnus-remove-text-with-property 'gnus-next))
9579       (setq gnus-page-broken nil))))
9580
9581 (defun gnus-summary-move-article (&optional n to-newsgroup
9582                                             select-method action)
9583   "Move the current article to a different newsgroup.
9584 If N is a positive number, move the N next articles.
9585 If N is a negative number, move the N previous articles.
9586 If N is nil and any articles have been marked with the process mark,
9587 move those articles instead.
9588 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9589 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9590 re-spool using this method.
9591
9592 When called interactively with TO-NEWSGROUP being nil, the value of
9593 the variable `gnus-move-split-methods' is used for finding a default
9594 for the target newsgroup.
9595
9596 For this function to work, both the current newsgroup and the
9597 newsgroup that you want to move to have to support the `request-move'
9598 and `request-accept' functions.
9599
9600 ACTION can be either `move' (the default), `crosspost' or `copy'."
9601   (interactive "P")
9602   (unless action
9603     (setq action 'move))
9604   ;; Check whether the source group supports the required functions.
9605   (cond ((and (eq action 'move)
9606               (not (gnus-check-backend-function
9607                     'request-move-article gnus-newsgroup-name)))
9608          (error "The current group does not support article moving"))
9609         ((and (eq action 'crosspost)
9610               (not (gnus-check-backend-function
9611                     'request-replace-article gnus-newsgroup-name)))
9612          (error "The current group does not support article editing")))
9613   (let ((articles (gnus-summary-work-articles n))
9614         (prefix (if (gnus-check-backend-function
9615                      'request-move-article gnus-newsgroup-name)
9616                     (funcall gnus-move-group-prefix-function
9617                              gnus-newsgroup-name)
9618                   ""))
9619         (names '((move "Move" "Moving")
9620                  (copy "Copy" "Copying")
9621                  (crosspost "Crosspost" "Crossposting")))
9622         (copy-buf (save-excursion
9623                     (nnheader-set-temp-buffer " *copy article*")))
9624         art-group to-method new-xref article to-groups
9625         articles-to-update-marks encoded)
9626     (unless (assq action names)
9627       (error "Unknown action %s" action))
9628     ;; Read the newsgroup name.
9629     (when (and (not to-newsgroup)
9630                (not select-method))
9631       (if (and gnus-move-split-methods
9632                (not
9633                 (and (memq gnus-current-article articles)
9634                      (gnus-buffer-live-p gnus-original-article-buffer))))
9635           ;; When `gnus-move-split-methods' is non-nil, we have to
9636           ;; select an article to give `gnus-read-move-group-name' an
9637           ;; opportunity to suggest an appropriate default.  However,
9638           ;; we needn't render or mark the article.
9639           (let ((gnus-display-mime-function nil)
9640                 (gnus-article-prepare-hook nil)
9641                 (gnus-mark-article-hook nil))
9642             (gnus-summary-select-article nil nil nil (car articles))))
9643       (setq to-newsgroup (gnus-read-move-group-name
9644                           (cadr (assq action names))
9645                           (symbol-value
9646                            (intern (format "gnus-current-%s-group" action)))
9647                           articles prefix)
9648             encoded to-newsgroup
9649             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9650       (set (intern (format "gnus-current-%s-group" action))
9651            (mm-decode-coding-string
9652             to-newsgroup
9653             (gnus-group-name-charset to-method to-newsgroup))))
9654     (unless to-method
9655       (setq to-method (or select-method
9656                           (gnus-server-to-method
9657                            (gnus-group-method to-newsgroup)))))
9658     (setq to-newsgroup
9659           (or encoded
9660               (and to-newsgroup
9661                    (mm-encode-coding-string
9662                     to-newsgroup
9663                     (gnus-group-name-charset to-method to-newsgroup)))))
9664     ;; Check the method we are to move this article to...
9665     (unless (gnus-check-backend-function
9666              'request-accept-article (car to-method))
9667       (error "%s does not support article copying" (car to-method)))
9668     (unless (gnus-check-server to-method)
9669       (error "Can't open server %s" (car to-method)))
9670     (gnus-message 6 "%s to %s: %s..."
9671                   (caddr (assq action names))
9672                   (or (car select-method)
9673                       (gnus-group-decoded-name to-newsgroup))
9674                   articles)
9675     (while articles
9676       (setq article (pop articles))
9677       (setq
9678        art-group
9679        (cond
9680         ;; Move the article.
9681         ((eq action 'move)
9682          ;; Remove this article from future suppression.
9683          (gnus-dup-unsuppress-article article)
9684          (let* ((from-method (gnus-find-method-for-group
9685                               gnus-newsgroup-name))
9686                 (to-method (or select-method
9687                                (gnus-find-method-for-group to-newsgroup)))
9688                 (move-is-internal (gnus-method-equal from-method to-method)))
9689            (gnus-request-move-article
9690             article                     ; Article to move
9691             gnus-newsgroup-name         ; From newsgroup
9692             (nth 1 (gnus-find-method-for-group
9693                     gnus-newsgroup-name)) ; Server
9694             (list 'gnus-request-accept-article
9695                   to-newsgroup (list 'quote select-method)
9696                   (not articles) t)     ; Accept form
9697             (not articles)              ; Only save nov last time
9698             move-is-internal)))         ; is this move internal?
9699         ;; Copy the article.
9700         ((eq action 'copy)
9701          (with-current-buffer copy-buf
9702            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9703              (save-restriction
9704                (nnheader-narrow-to-headers)
9705                (dolist (hdr gnus-copy-article-ignored-headers)
9706                  (message-remove-header hdr t)))
9707              (gnus-request-accept-article
9708               to-newsgroup select-method (not articles) t))))
9709         ;; Crosspost the article.
9710         ((eq action 'crosspost)
9711          (let ((xref (message-tokenize-header
9712                       (mail-header-xref (gnus-summary-article-header article))
9713                       " ")))
9714            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9715                                   ":" (number-to-string article)))
9716            (unless xref
9717              (setq xref (list (system-name))))
9718            (setq new-xref
9719                  (concat
9720                   (mapconcat 'identity
9721                              (delete "Xref:" (delete new-xref xref))
9722                              " ")
9723                   " " new-xref))
9724            (with-current-buffer copy-buf
9725              ;; First put the article in the destination group.
9726              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9727              (when (consp (setq art-group
9728                                 (gnus-request-accept-article
9729                                  to-newsgroup select-method (not articles) t)))
9730                (setq new-xref (concat new-xref " " (car art-group)
9731                                       ":"
9732                                       (number-to-string (cdr art-group))))
9733                ;; Now we have the new Xrefs header, so we insert
9734                ;; it and replace the new article.
9735                (nnheader-replace-header "Xref" new-xref)
9736                (gnus-request-replace-article
9737                 (cdr art-group) to-newsgroup (current-buffer) t)
9738                art-group))))))
9739       (cond
9740        ((not art-group)
9741         (gnus-message 1 "Couldn't %s article %s: %s"
9742                       (cadr (assq action names)) article
9743                       (nnheader-get-report (car to-method))))
9744        ((eq art-group 'junk)
9745         (when (eq action 'move)
9746           (gnus-summary-mark-article article gnus-canceled-mark)
9747           (gnus-message 4 "Deleted article %s" article)
9748           ;; run the delete hook
9749           (run-hook-with-args 'gnus-summary-article-delete-hook
9750                               action
9751                               (gnus-data-header
9752                                (assoc article (gnus-data-list nil)))
9753                               gnus-newsgroup-name nil
9754                               select-method)))
9755        (t
9756         (let* ((pto-group (gnus-group-prefixed-name
9757                            (car art-group) to-method))
9758                (info (gnus-get-info pto-group))
9759                (to-group (gnus-info-group info))
9760                to-marks)
9761           ;; Update the group that has been moved to.
9762           (when (and info
9763                      (memq action '(move copy)))
9764             (unless (member to-group to-groups)
9765               (push to-group to-groups))
9766
9767             (unless (memq article gnus-newsgroup-unreads)
9768               (push 'read to-marks)
9769               (gnus-info-set-read
9770                info (gnus-add-to-range (gnus-info-read info)
9771                                        (list (cdr art-group)))))
9772
9773             ;; See whether the article is to be put in the cache.
9774             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9775                    (marks (if expirable
9776                               gnus-article-mark-lists
9777                             (delete '(expirable . expire)
9778                                     (copy-sequence gnus-article-mark-lists))))
9779                    (to-article (cdr art-group)))
9780
9781               ;; Enter the article into the cache in the new group,
9782               ;; if that is required.
9783               (when gnus-use-cache
9784                 (gnus-cache-possibly-enter-article
9785                  to-group to-article
9786                  (memq article gnus-newsgroup-marked)
9787                  (memq article gnus-newsgroup-dormant)
9788                  (memq article gnus-newsgroup-unreads)))
9789
9790               (when gnus-preserve-marks
9791                 ;; Copy any marks over to the new group.
9792                 (when (and (equal to-group gnus-newsgroup-name)
9793                            (not (memq article gnus-newsgroup-unreads)))
9794                   ;; Mark this article as read in this group.
9795                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9796                   (setcdr (gnus-active to-group) to-article)
9797                   (setcdr gnus-newsgroup-active to-article))
9798
9799                 (while marks
9800                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9801                     (when (memq article (symbol-value
9802                                          (intern (format "gnus-newsgroup-%s"
9803                                                          (caar marks)))))
9804                       (push (cdar marks) to-marks)
9805                       ;; If the other group is the same as this group,
9806                       ;; then we have to add the mark to the list.
9807                       (when (equal to-group gnus-newsgroup-name)
9808                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9809                              (cons to-article
9810                                    (symbol-value
9811                                     (intern (format "gnus-newsgroup-%s"
9812                                                     (caar marks)))))))
9813                       ;; Copy the marks to other group.
9814                       (gnus-add-marked-articles
9815                        to-group (cdar marks) (list to-article) info)))
9816                   (setq marks (cdr marks)))
9817
9818                 (when (and expirable
9819                            gnus-mark-copied-or-moved-articles-as-expirable
9820                            (not (memq 'expire to-marks)))
9821                   ;; Mark this article as expirable.
9822                   (push 'expire to-marks)
9823                   (when (equal to-group gnus-newsgroup-name)
9824                     (push to-article gnus-newsgroup-expirable))
9825                   ;; Copy the expirable mark to other group.
9826                   (gnus-add-marked-articles
9827                    to-group 'expire (list to-article) info))
9828
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         (gnus-summary-goto-subject article)
9859         (when (eq action 'move)
9860           (gnus-summary-mark-article article gnus-canceled-mark))))
9861       (push article articles-to-update-marks))
9862
9863     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9864     ;; Re-activate all groups that have been moved to.
9865     (with-current-buffer gnus-group-buffer
9866       (let ((gnus-group-marked to-groups))
9867         (gnus-group-get-new-news-this-group nil t)))
9868
9869     (gnus-kill-buffer copy-buf)
9870     (gnus-summary-position-point)
9871     (gnus-set-mode-line 'summary)))
9872
9873 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9874   "Copy the current article to some other group.
9875 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9876 When called interactively, if TO-NEWSGROUP is nil, use the value of
9877 the variable `gnus-move-split-methods' for finding a default target
9878 newsgroup.
9879 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9880 re-spool using this method."
9881   (interactive "P")
9882   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9883
9884 (defun gnus-summary-crosspost-article (&optional n)
9885   "Crosspost the current article to some other group."
9886   (interactive "P")
9887   (gnus-summary-move-article n nil nil 'crosspost))
9888
9889 (defcustom gnus-summary-respool-default-method nil
9890   "Default method type for respooling an article.
9891 If nil, use to the current newsgroup method."
9892   :type 'symbol
9893   :group 'gnus-summary-mail)
9894
9895 (defun gnus-summary-respool-article (&optional n method)
9896   "Respool the current article.
9897 The article will be squeezed through the mail spooling process again,
9898 which means that it will be put in some mail newsgroup or other
9899 depending on `nnmail-split-methods'.
9900 If N is a positive number, respool the N next articles.
9901 If N is a negative number, respool the N previous articles.
9902 If N is nil and any articles have been marked with the process mark,
9903 respool those articles instead.
9904
9905 Respooling can be done both from mail groups and \"real\" newsgroups.
9906 In the former case, the articles in question will be moved from the
9907 current group into whatever groups they are destined to.  In the
9908 latter case, they will be copied into the relevant groups."
9909   (interactive
9910    (list current-prefix-arg
9911          (let* ((methods (gnus-methods-using 'respool))
9912                 (methname
9913                  (symbol-name (or gnus-summary-respool-default-method
9914                                   (car (gnus-find-method-for-group
9915                                         gnus-newsgroup-name)))))
9916                 (method
9917                  (gnus-completing-read-with-default
9918                   methname "Backend to use when respooling"
9919                   methods nil t nil 'gnus-mail-method-history))
9920                 ms)
9921            (cond
9922             ((zerop (length (setq ms (gnus-servers-using-backend
9923                                       (intern method)))))
9924              (list (intern method) ""))
9925             ((= 1 (length ms))
9926              (car ms))
9927             (t
9928              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9929                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9930                            ms-alist))))))))
9931   (unless method
9932     (error "No method given for respooling"))
9933   (if (assoc (symbol-name
9934               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9935              (gnus-methods-using 'respool))
9936       (gnus-summary-move-article n nil method)
9937     (gnus-summary-copy-article n nil method)))
9938
9939 (defun gnus-summary-import-article (file &optional edit)
9940   "Import an arbitrary file into a mail newsgroup."
9941   (interactive "fImport file: \nP")
9942   (let ((group gnus-newsgroup-name)
9943         (now (current-time))
9944         atts lines group-art)
9945     (unless (gnus-check-backend-function 'request-accept-article group)
9946       (error "%s does not support article importing" group))
9947     (or (file-readable-p file)
9948         (not (file-regular-p file))
9949         (error "Can't read %s" file))
9950     (with-current-buffer (gnus-get-buffer-create " *import file*")
9951       (erase-buffer)
9952       (nnheader-insert-file-contents file)
9953       (goto-char (point-min))
9954       (if (nnheader-article-p)
9955           (save-restriction
9956             (goto-char (point-min))
9957             (search-forward "\n\n" nil t)
9958             (narrow-to-region (point-min) (1- (point)))
9959             (goto-char (point-min))
9960             (unless (re-search-forward "^date:" nil t)
9961               (goto-char (point-max))
9962               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9963        ;; This doesn't look like an article, so we fudge some headers.
9964         (setq atts (file-attributes file)
9965               lines (count-lines (point-min) (point-max)))
9966         (insert "From: " (read-string "From: ") "\n"
9967                 "Subject: " (read-string "Subject: ") "\n"
9968                 "Date: " (message-make-date (nth 5 atts)) "\n"
9969                 "Message-ID: " (message-make-message-id) "\n"
9970                 "Lines: " (int-to-string lines) "\n"
9971                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9972       (setq group-art (gnus-request-accept-article group nil t))
9973       (kill-buffer (current-buffer)))
9974     (setq gnus-newsgroup-active (gnus-activate-group group))
9975     (forward-line 1)
9976     (gnus-summary-goto-article (cdr group-art) nil t)
9977     (when edit
9978       (gnus-summary-edit-article))))
9979
9980 (defun gnus-summary-create-article ()
9981   "Create an article in a mail newsgroup."
9982   (interactive)
9983   (let ((group gnus-newsgroup-name)
9984         (now (current-time))
9985         group-art)
9986     (unless (gnus-check-backend-function 'request-accept-article group)
9987       (error "%s does not support article importing" group))
9988     (with-current-buffer (gnus-get-buffer-create " *import file*")
9989       (erase-buffer)
9990       (goto-char (point-min))
9991       ;; This doesn't look like an article, so we fudge some headers.
9992       (insert "From: " (read-string "From: ") "\n"
9993               "Subject: " (read-string "Subject: ") "\n"
9994               "Date: " (message-make-date now) "\n"
9995               "Message-ID: " (message-make-message-id) "\n")
9996       (setq group-art (gnus-request-accept-article group nil t))
9997       (kill-buffer (current-buffer)))
9998     (setq gnus-newsgroup-active (gnus-activate-group group))
9999     (forward-line 1)
10000     (gnus-summary-goto-article (cdr group-art) nil t)
10001     (gnus-summary-edit-article)))
10002
10003 (defun gnus-summary-article-posted-p ()
10004   "Say whether the current (mail) article is available from news as well.
10005 This will be the case if the article has both been mailed and posted."
10006   (interactive)
10007   (let ((id (mail-header-references (gnus-summary-article-header)))
10008         (gnus-override-method (car (gnus-refer-article-methods))))
10009     (if (gnus-request-head id "")
10010         (gnus-message 2 "The current message was found on %s"
10011                       gnus-override-method)
10012       (gnus-message 2 "The current message couldn't be found on %s"
10013                     gnus-override-method)
10014       nil)))
10015
10016 (defun gnus-summary-expire-articles (&optional now)
10017   "Expire all articles that are marked as expirable in the current group."
10018   (interactive)
10019   (when (and (not gnus-group-is-exiting-without-update-p)
10020              (gnus-check-backend-function
10021               'request-expire-articles gnus-newsgroup-name))
10022     ;; This backend supports expiry.
10023     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10024            (expirable (if total
10025                           (progn
10026                             ;; We need to update the info for
10027                             ;; this group for `gnus-list-of-read-articles'
10028                             ;; to give us the right answer.
10029                             (gnus-run-hooks 'gnus-exit-group-hook)
10030                             (gnus-summary-update-info)
10031                             (gnus-list-of-read-articles gnus-newsgroup-name))
10032                         (setq gnus-newsgroup-expirable
10033                               (sort gnus-newsgroup-expirable '<))))
10034            (expiry-wait (if now 'immediate
10035                           (gnus-group-find-parameter
10036                            gnus-newsgroup-name 'expiry-wait)))
10037            (nnmail-expiry-target
10038             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10039                 nnmail-expiry-target))
10040            es)
10041       (when expirable
10042         ;; There are expirable articles in this group, so we run them
10043         ;; through the expiry process.
10044         (gnus-message 6 "Expiring articles...")
10045         (unless (gnus-check-group gnus-newsgroup-name)
10046           (error "Can't open server for %s" gnus-newsgroup-name))
10047         ;; The list of articles that weren't expired is returned.
10048         (save-excursion
10049           (if expiry-wait
10050               (let ((nnmail-expiry-wait-function nil)
10051                     (nnmail-expiry-wait expiry-wait))
10052                 (setq es (gnus-request-expire-articles
10053                           expirable gnus-newsgroup-name)))
10054             (setq es (gnus-request-expire-articles
10055                       expirable gnus-newsgroup-name)))
10056           (unless total
10057             (setq gnus-newsgroup-expirable es))
10058           ;; We go through the old list of expirable, and mark all
10059           ;; really expired articles as nonexistent.
10060           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10061             (let ((gnus-use-cache nil))
10062               (dolist (article expirable)
10063                 (when (and (not (memq article es))
10064                            (gnus-data-find article))
10065                   (gnus-summary-mark-article article gnus-canceled-mark)
10066                   (run-hook-with-args 'gnus-summary-article-expire-hook
10067                                       'delete
10068                                       (gnus-data-header
10069                                        (assoc article (gnus-data-list nil)))
10070                                       gnus-newsgroup-name
10071                                       nil
10072                                       nil))))))
10073         (gnus-message 6 "Expiring articles...done")))))
10074
10075 (defun gnus-summary-expire-articles-now ()
10076   "Expunge all expirable articles in the current group.
10077 This means that *all* articles that are marked as expirable will be
10078 deleted forever, right now."
10079   (interactive)
10080   (or gnus-expert-user
10081       (gnus-yes-or-no-p
10082        "Are you really, really sure you want to delete all expirable messages? ")
10083       (error "Phew!"))
10084   (gnus-summary-expire-articles t))
10085
10086 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10087 (defun gnus-summary-delete-article (&optional n)
10088   "Delete the N next (mail) articles.
10089 This command actually deletes articles.  This is not a marking
10090 command.  The article will disappear forever from your life, never to
10091 return.
10092
10093 If N is negative, delete backwards.
10094 If N is nil and articles have been marked with the process mark,
10095 delete these instead.
10096
10097 If `gnus-novice-user' is non-nil you will be asked for
10098 confirmation before the articles are deleted."
10099   (interactive "P")
10100   (unless (gnus-check-backend-function 'request-expire-articles
10101                                        gnus-newsgroup-name)
10102     (error "The current newsgroup does not support article deletion"))
10103   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10104     (error "Couldn't open server"))
10105   ;; Compute the list of articles to delete.
10106   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10107         (nnmail-expiry-target 'delete)
10108         not-deleted)
10109     (if (and gnus-novice-user
10110              (not (gnus-yes-or-no-p
10111                    (format "Do you really want to delete %s forever? "
10112                            (if (> (length articles) 1)
10113                                (format "these %s articles" (length articles))
10114                              "this article")))))
10115         ()
10116       ;; Delete the articles.
10117       (setq not-deleted (gnus-request-expire-articles
10118                          articles gnus-newsgroup-name 'force))
10119       (while articles
10120         (gnus-summary-remove-process-mark (car articles))
10121         ;; The backend might not have been able to delete the article
10122         ;; after all.
10123         (unless (memq (car articles) not-deleted)
10124           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10125         (let* ((article (car articles))
10126                (ghead  (gnus-data-header
10127                                     (assoc article (gnus-data-list nil)))))
10128           (run-hook-with-args 'gnus-summary-article-delete-hook
10129                               'delete ghead gnus-newsgroup-name nil
10130                               nil))
10131         (setq articles (cdr articles)))
10132       (when not-deleted
10133         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10134     (gnus-summary-position-point)
10135     (gnus-set-mode-line 'summary)
10136     not-deleted))
10137
10138 (defun gnus-summary-edit-article (&optional arg)
10139   "Edit the current article.
10140 This will have permanent effect only in mail groups.
10141 If ARG is nil, edit the decoded articles.
10142 If ARG is 1, edit the raw articles.
10143 If ARG is 2, edit the raw articles even in read-only groups.
10144 If ARG is 3, edit the articles with the current handles.
10145 Otherwise, allow editing of articles even in read-only
10146 groups."
10147   (interactive "P")
10148   (let (force raw current-handles)
10149     (cond
10150      ((null arg))
10151      ((eq arg 1)
10152       (setq raw t))
10153      ((eq arg 2)
10154       (setq raw t
10155             force t))
10156      ((eq arg 3)
10157       (setq current-handles
10158             (and (gnus-buffer-live-p gnus-article-buffer)
10159                  (with-current-buffer gnus-article-buffer
10160                    (prog1
10161                        gnus-article-mime-handles
10162                      (setq gnus-article-mime-handles nil))))))
10163      (t
10164       (setq force t)))
10165     (when (and raw (not force)
10166                (member gnus-newsgroup-name '("nndraft:delayed"
10167                                              "nndraft:drafts"
10168                                              "nndraft:queue")))
10169       (error "Can't edit the raw article in group %s"
10170              gnus-newsgroup-name))
10171     (with-current-buffer gnus-summary-buffer
10172       (let ((mail-parse-charset gnus-newsgroup-charset)
10173             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10174         (gnus-set-global-variables)
10175         (when (and (not force)
10176                    (gnus-group-read-only-p))
10177           (error "The current newsgroup does not support article editing"))
10178         (gnus-summary-show-article t)
10179         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10180           (with-current-buffer gnus-article-buffer
10181             (mm-enable-multibyte)))
10182         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10183             (setq raw t))
10184         (gnus-article-edit-article
10185          (if raw 'ignore
10186            `(lambda ()
10187               (let ((mbl mml-buffer-list))
10188                 (setq mml-buffer-list nil)
10189                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10190                   (mime-to-mml ,'current-handles))
10191                 (let ((mbl1 mml-buffer-list))
10192                   (setq mml-buffer-list mbl)
10193                   (set (make-local-variable 'mml-buffer-list) mbl1))
10194                 (gnus-make-local-hook 'kill-buffer-hook)
10195                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10196          `(lambda (no-highlight)
10197             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10198                   (message-options message-options)
10199                   (message-options-set-recipient)
10200                   (mail-parse-ignored-charsets
10201                    ',gnus-newsgroup-ignored-charsets)
10202                   (rfc2047-header-encoding-alist
10203                    ',(let ((charset (gnus-group-name-charset
10204                                      (gnus-find-method-for-group
10205                                       gnus-newsgroup-name)
10206                                      gnus-newsgroup-name)))
10207                        (append (list (cons "Newsgroups" charset)
10208                                      (cons "Followup-To" charset)
10209                                      (cons "Xref" charset))
10210                                rfc2047-header-encoding-alist))))
10211               ,(if (not raw) '(progn
10212                                 (mml-to-mime)
10213                                 (mml-destroy-buffers)
10214                                 (remove-hook 'kill-buffer-hook
10215                                              'mml-destroy-buffers t)
10216                                 (kill-local-variable 'mml-buffer-list)))
10217               (gnus-summary-edit-article-done
10218                ,(or (mail-header-references gnus-current-headers) "")
10219                ,(gnus-group-read-only-p)
10220                ,gnus-summary-buffer no-highlight))))))))
10221
10222 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10223
10224 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10225                                                  no-highlight)
10226   "Make edits to the current article permanent."
10227   (interactive)
10228   (save-excursion
10229    ;; The buffer restriction contains the entire article if it exists.
10230     (when (article-goto-body)
10231       (let ((lines (count-lines (point) (point-max)))
10232             (length (- (point-max) (point)))
10233             (case-fold-search t)
10234             (body (copy-marker (point))))
10235         (goto-char (point-min))
10236         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10237           (delete-region (match-beginning 1) (match-end 1))
10238           (insert (number-to-string length)))
10239         (goto-char (point-min))
10240         (when (re-search-forward
10241                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10242           (delete-region (match-beginning 1) (match-end 1))
10243           (insert (number-to-string length)))
10244         (goto-char (point-min))
10245         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10246           (delete-region (match-beginning 1) (match-end 1))
10247           (insert (number-to-string lines))))))
10248   ;; Replace the article.
10249   (let ((buf (current-buffer)))
10250     (with-temp-buffer
10251       (insert-buffer-substring buf)
10252
10253       (if (and (not read-only)
10254                (not (gnus-request-replace-article
10255                      (cdr gnus-article-current) (car gnus-article-current)
10256                      (current-buffer) t)))
10257           (error "Couldn't replace article")
10258         ;; Update the summary buffer.
10259         (if (and references
10260                  (equal (message-tokenize-header references " ")
10261                         (message-tokenize-header
10262                          (or (message-fetch-field "references") "") " ")))
10263             ;; We only have to update this line.
10264             (save-excursion
10265               (save-restriction
10266                 (message-narrow-to-head)
10267                 (let ((head (buffer-substring-no-properties
10268                              (point-min) (point-max)))
10269                       header)
10270                   (with-temp-buffer
10271                     (insert (format "211 %d Article retrieved.\n"
10272                                     (cdr gnus-article-current)))
10273                     (insert head)
10274                     (insert ".\n")
10275                     (let ((nntp-server-buffer (current-buffer)))
10276                       (setq header (car (gnus-get-newsgroup-headers
10277                                          nil t))))
10278                     (with-current-buffer gnus-summary-buffer
10279                       (gnus-data-set-header
10280                        (gnus-data-find (cdr gnus-article-current))
10281                        header)
10282                       (gnus-summary-update-article-line
10283                        (cdr gnus-article-current) header)
10284                       (if (gnus-summary-goto-subject
10285                            (cdr gnus-article-current) nil t)
10286                           (gnus-summary-update-secondary-mark
10287                            (cdr gnus-article-current))))))))
10288           ;; Update threads.
10289           (set-buffer (or buffer gnus-summary-buffer))
10290           (gnus-summary-update-article (cdr gnus-article-current))
10291           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10292               (gnus-summary-update-secondary-mark
10293                (cdr gnus-article-current))))
10294         ;; Prettify the article buffer again.
10295         (unless no-highlight
10296           (with-current-buffer gnus-article-buffer
10297             ;;;!!! Fix this -- article should be rehighlighted.
10298             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10299             (set-buffer gnus-original-article-buffer)
10300             (gnus-request-article
10301              (cdr gnus-article-current)
10302              (car gnus-article-current) (current-buffer))))
10303         ;; Prettify the summary buffer line.
10304         (when (gnus-visual-p 'summary-highlight 'highlight)
10305           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10306
10307 (defun gnus-summary-edit-wash (key)
10308   "Perform editing command KEY in the article buffer."
10309   (interactive
10310    (list
10311     (progn
10312       (message "%s" (concat (this-command-keys) "- "))
10313       (read-char))))
10314   (message "")
10315   (gnus-summary-edit-article)
10316   (execute-kbd-macro (concat (this-command-keys) key))
10317   (gnus-article-edit-done))
10318
10319 ;;; Respooling
10320
10321 (defun gnus-summary-respool-query (&optional silent trace)
10322   "Query where the respool algorithm would put this article."
10323   (interactive)
10324   (let (gnus-mark-article-hook)
10325     (gnus-summary-select-article)
10326     (with-current-buffer gnus-original-article-buffer
10327       (let ((groups (nnmail-article-group 'identity trace)))
10328         (unless silent
10329           (if groups
10330               (message "This message would go to %s"
10331                        (mapconcat 'car groups ", "))
10332             (message "This message would go to no groups"))
10333           groups)))))
10334
10335 (defun gnus-summary-respool-trace ()
10336   "Trace where the respool algorithm would put this article.
10337 Display a buffer showing all fancy splitting patterns which matched."
10338   (interactive)
10339   (gnus-summary-respool-query nil t))
10340
10341 ;; Summary marking commands.
10342
10343 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10344   "Mark articles which has the same subject as read, and then select the next.
10345 If UNMARK is positive, remove any kind of mark.
10346 If UNMARK is negative, tick articles."
10347   (interactive "P")
10348   (when unmark
10349     (setq unmark (prefix-numeric-value unmark)))
10350   (let ((count
10351          (gnus-summary-mark-same-subject
10352           (gnus-summary-article-subject) unmark)))
10353     ;; Select next unread article.  If auto-select-same mode, should
10354     ;; select the first unread article.
10355     (gnus-summary-next-article t (and gnus-auto-select-same
10356                                       (gnus-summary-article-subject)))
10357     (gnus-message 7 "%d article%s marked as %s"
10358                   count (if (= count 1) " is" "s are")
10359                   (if unmark "unread" "read"))))
10360
10361 (defun gnus-summary-kill-same-subject (&optional unmark)
10362   "Mark articles which has the same subject as read.
10363 If UNMARK is positive, remove any kind of mark.
10364 If UNMARK is negative, tick articles."
10365   (interactive "P")
10366   (when unmark
10367     (setq unmark (prefix-numeric-value unmark)))
10368   (let ((count
10369          (gnus-summary-mark-same-subject
10370           (gnus-summary-article-subject) unmark)))
10371     ;; If marked as read, go to next unread subject.
10372     (when (null unmark)
10373       ;; Go to next unread subject.
10374       (gnus-summary-next-subject 1 t))
10375     (gnus-message 7 "%d articles are marked as %s"
10376                   count (if unmark "unread" "read"))))
10377
10378 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10379   "Mark articles with same SUBJECT as read, and return marked number.
10380 If optional argument UNMARK is positive, remove any kinds of marks.
10381 If optional argument UNMARK is negative, mark articles as unread instead."
10382   (let ((count 1))
10383     (save-excursion
10384       (cond
10385        ((null unmark)                   ; Mark as read.
10386         (while (and
10387                 (progn
10388                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10389                   (gnus-summary-show-thread) t)
10390                 (gnus-summary-find-subject subject))
10391           (setq count (1+ count))))
10392        ((> unmark 0)                    ; Tick.
10393         (while (and
10394                 (progn
10395                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10396                   (gnus-summary-show-thread) t)
10397                 (gnus-summary-find-subject subject))
10398           (setq count (1+ count))))
10399        (t                               ; Mark as unread.
10400         (while (and
10401                 (progn
10402                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10403                   (gnus-summary-show-thread) t)
10404                 (gnus-summary-find-subject subject))
10405           (setq count (1+ count)))))
10406       (gnus-set-mode-line 'summary)
10407       ;; Return the number of marked articles.
10408       count)))
10409
10410 (defun gnus-summary-mark-as-processable (n &optional unmark)
10411   "Set the process mark on the next N articles.
10412 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10413 the process mark instead.  The difference between N and the actual
10414 number of articles marked is returned."
10415   (interactive "P")
10416   (if (and (null n) (gnus-region-active-p))
10417       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10418     (setq n (prefix-numeric-value n))
10419     (let ((backward (< n 0))
10420           (n (abs n)))
10421       (while (and
10422               (> n 0)
10423               (if unmark
10424                   (gnus-summary-remove-process-mark
10425                    (gnus-summary-article-number))
10426                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10427               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10428         (setq n (1- n)))
10429       (when (/= 0 n)
10430         (gnus-message 7 "No more articles"))
10431       (gnus-summary-recenter)
10432       (gnus-summary-position-point)
10433       n)))
10434
10435 (defun gnus-summary-unmark-as-processable (n)
10436   "Remove the process mark from the next N articles.
10437 If N is negative, unmark backward instead.  The difference between N and
10438 the actual number of articles unmarked is returned."
10439   (interactive "P")
10440   (gnus-summary-mark-as-processable n t))
10441
10442 (defun gnus-summary-unmark-all-processable ()
10443   "Remove the process mark from all articles."
10444   (interactive)
10445   (save-excursion
10446     (while gnus-newsgroup-processable
10447       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10448   (gnus-summary-position-point))
10449
10450 (defun gnus-summary-add-mark (article type)
10451   "Mark ARTICLE with a mark of TYPE."
10452   (let ((vtype (car (assq type gnus-article-mark-lists)))
10453         var)
10454     (if (not vtype)
10455         (error "No such mark type: %s" type)
10456       (setq var (intern (format "gnus-newsgroup-%s" type)))
10457       (set var (cons article (symbol-value var)))
10458       (if (memq type '(processable cached replied forwarded recent saved))
10459           (gnus-summary-update-secondary-mark article)
10460         ;;; !!! This is bogus.  We should find out what primary
10461         ;;; !!! mark we want to set.
10462         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10463
10464 (defun gnus-summary-mark-as-expirable (n)
10465   "Mark N articles forward as expirable.
10466 If N is negative, mark backward instead.  The difference between N and
10467 the actual number of articles marked is returned."
10468   (interactive "p")
10469   (gnus-summary-mark-forward n gnus-expirable-mark))
10470
10471 (defun gnus-summary-mark-as-spam (n)
10472   "Mark N articles forward as spam.
10473 If N is negative, mark backward instead.  The difference between N and
10474 the actual number of articles marked is returned."
10475   (interactive "p")
10476   (gnus-summary-mark-forward n gnus-spam-mark))
10477
10478 (defun gnus-summary-mark-article-as-replied (article)
10479   "Mark ARTICLE as replied to and update the summary line.
10480 ARTICLE can also be a list of articles."
10481   (interactive (list (gnus-summary-article-number)))
10482   (let ((articles (if (listp article) article (list article))))
10483     (dolist (article articles)
10484       (unless (numberp article)
10485         (error "%s is not a number" article))
10486       (push article gnus-newsgroup-replied)
10487       (let ((inhibit-read-only t))
10488         (when (gnus-summary-goto-subject article nil t)
10489           (gnus-summary-update-secondary-mark article))))))
10490
10491 (defun gnus-summary-mark-article-as-forwarded (article)
10492   "Mark ARTICLE as forwarded and update the summary line.
10493 ARTICLE can also be a list of articles."
10494   (let ((articles (if (listp article) article (list article))))
10495     (dolist (article articles)
10496       (push article gnus-newsgroup-forwarded)
10497       (let ((inhibit-read-only t))
10498         (when (gnus-summary-goto-subject article nil t)
10499           (gnus-summary-update-secondary-mark article))))))
10500
10501 (defun gnus-summary-set-bookmark (article)
10502   "Set a bookmark in current article."
10503   (interactive (list (gnus-summary-article-number)))
10504   (when (or (not (get-buffer gnus-article-buffer))
10505             (not gnus-current-article)
10506             (not gnus-article-current)
10507             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10508     (error "No current article selected"))
10509   ;; Remove old bookmark, if one exists.
10510   (gnus-pull article gnus-newsgroup-bookmarks)
10511   ;; Set the new bookmark, which is on the form
10512   ;; (article-number . line-number-in-body).
10513   (push
10514    (cons article
10515          (with-current-buffer gnus-article-buffer
10516            (count-lines
10517             (min (point)
10518                  (save-excursion
10519                    (article-goto-body)
10520                    (point)))
10521             (point))))
10522    gnus-newsgroup-bookmarks)
10523   (gnus-message 6 "A bookmark has been added to the current article."))
10524
10525 (defun gnus-summary-remove-bookmark (article)
10526   "Remove the bookmark from the current article."
10527   (interactive (list (gnus-summary-article-number)))
10528   ;; Remove old bookmark, if one exists.
10529   (if (not (assq article gnus-newsgroup-bookmarks))
10530       (gnus-message 6 "No bookmark in current article.")
10531     (gnus-pull article gnus-newsgroup-bookmarks)
10532     (gnus-message 6 "Removed bookmark.")))
10533
10534 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10535 (defun gnus-summary-mark-as-dormant (n)
10536   "Mark N articles forward as dormant.
10537 If N is negative, mark backward instead.  The difference between N and
10538 the actual number of articles marked is returned."
10539   (interactive "p")
10540   (gnus-summary-mark-forward n gnus-dormant-mark))
10541
10542 (defun gnus-summary-set-process-mark (article)
10543   "Set the process mark on ARTICLE and update the summary line."
10544   (setq gnus-newsgroup-processable
10545         (cons article
10546               (delq article gnus-newsgroup-processable)))
10547   (when (gnus-summary-goto-subject article)
10548     (gnus-summary-show-thread)
10549     (gnus-summary-goto-subject article)
10550     (gnus-summary-update-secondary-mark article)))
10551
10552 (defun gnus-summary-remove-process-mark (&rest articles)
10553   "Remove the process mark from ARTICLES and update the summary line."
10554   (dolist (article articles)
10555     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10556     (when (gnus-summary-goto-subject article)
10557       (gnus-summary-show-thread)
10558       (gnus-summary-goto-subject article)
10559       (gnus-summary-update-secondary-mark article)))
10560   t)
10561
10562 (defun gnus-summary-set-saved-mark (article)
10563   "Set the process mark on ARTICLE and update the summary line."
10564   (push article gnus-newsgroup-saved)
10565   (when (gnus-summary-goto-subject article)
10566     (gnus-summary-update-secondary-mark article)))
10567
10568 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10569   "Mark N articles as read forwards.
10570 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10571 The difference between N and the actual number of articles marked is
10572 returned.
10573 If NO-EXPIRE, auto-expiry will be inhibited."
10574   (interactive "p")
10575   (gnus-summary-show-thread)
10576   (let ((backward (< n 0))
10577         (gnus-summary-goto-unread
10578          (and gnus-summary-goto-unread
10579               (not (eq gnus-summary-goto-unread 'never))
10580               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10581                                     gnus-ticked-mark gnus-dormant-mark)))))
10582         (n (abs n))
10583         (mark (or mark gnus-del-mark)))
10584     (while (and (> n 0)
10585                 (gnus-summary-mark-article nil mark no-expire)
10586                 (zerop (gnus-summary-next-subject
10587                         (if backward -1 1)
10588                         (and gnus-summary-goto-unread
10589                              (not (eq gnus-summary-goto-unread 'never)))
10590                         t)))
10591       (setq n (1- n)))
10592     (when (/= 0 n)
10593       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10594     (gnus-summary-recenter)
10595     (gnus-summary-position-point)
10596     (gnus-set-mode-line 'summary)
10597     n))
10598
10599 (defun gnus-summary-mark-article-as-read (mark)
10600   "Mark the current article quickly as read with MARK."
10601   (let ((article (gnus-summary-article-number)))
10602     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10603     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10604     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10605     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10606     (push (cons article mark) gnus-newsgroup-reads)
10607     ;; Possibly remove from cache, if that is used.
10608     (when gnus-use-cache
10609       (gnus-cache-enter-remove-article article))
10610     ;; Allow the backend to change the mark.
10611     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10612     ;; Check for auto-expiry.
10613     (when (and gnus-newsgroup-auto-expire
10614                (memq mark gnus-auto-expirable-marks))
10615       (setq mark gnus-expirable-mark)
10616       ;; Let the backend know about the mark change.
10617       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10618       (push article gnus-newsgroup-expirable))
10619     ;; Set the mark in the buffer.
10620     (gnus-summary-update-mark mark 'unread)
10621     t))
10622
10623 (defun gnus-summary-mark-article-as-unread (mark)
10624   "Mark the current article quickly as unread with MARK."
10625   (let* ((article (gnus-summary-article-number))
10626          (old-mark (gnus-summary-article-mark article)))
10627     ;; Allow the backend to change the mark.
10628     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10629     (if (eq mark old-mark)
10630         t
10631       (if (<= article 0)
10632           (progn
10633             (gnus-error 1 "Can't mark negative article numbers")
10634             nil)
10635         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10636         (setq gnus-newsgroup-spam-marked
10637               (delq article gnus-newsgroup-spam-marked))
10638         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10639         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10640         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10641         (cond ((= mark gnus-ticked-mark)
10642                (setq gnus-newsgroup-marked
10643                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10644                                               article)))
10645               ((= mark gnus-spam-mark)
10646                (setq gnus-newsgroup-spam-marked
10647                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10648                                               article)))
10649               ((= mark gnus-dormant-mark)
10650                (setq gnus-newsgroup-dormant
10651                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10652                                               article)))
10653               (t
10654                (setq gnus-newsgroup-unreads
10655                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10656                                               article))))
10657         (gnus-pull article gnus-newsgroup-reads)
10658
10659         ;; See whether the article is to be put in the cache.
10660         (and gnus-use-cache
10661              (vectorp (gnus-summary-article-header article))
10662              (save-excursion
10663                (gnus-cache-possibly-enter-article
10664                 gnus-newsgroup-name article
10665                 (= mark gnus-ticked-mark)
10666                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10667
10668         ;; Fix the mark.
10669         (gnus-summary-update-mark mark 'unread)
10670         t))))
10671
10672 (defun gnus-summary-mark-article (&optional article mark no-expire)
10673   "Mark ARTICLE with MARK.  MARK can be any character.
10674 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10675 `??' (dormant) and `?E' (expirable).
10676 If MARK is nil, then the default character `?r' is used.
10677 If ARTICLE is nil, then the article on the current line will be
10678 marked.
10679 If NO-EXPIRE, auto-expiry will be inhibited."
10680   ;; The mark might be a string.
10681   (when (stringp mark)
10682     (setq mark (aref mark 0)))
10683   ;; If no mark is given, then we check auto-expiring.
10684   (when (null mark)
10685     (setq mark gnus-del-mark))
10686   (when (and (not no-expire)
10687              gnus-newsgroup-auto-expire
10688              (memq mark gnus-auto-expirable-marks))
10689     (setq mark gnus-expirable-mark))
10690   (let ((article (or article (gnus-summary-article-number)))
10691         (old-mark (gnus-summary-article-mark article)))
10692     ;; Allow the backend to change the mark.
10693     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10694     (if (eq mark old-mark)
10695         t
10696       (unless article
10697         (error "No article on current line"))
10698       (if (not (if (or (= mark gnus-unread-mark)
10699                        (= mark gnus-ticked-mark)
10700                        (= mark gnus-spam-mark)
10701                        (= mark gnus-dormant-mark))
10702                    (gnus-mark-article-as-unread article mark)
10703                  (gnus-mark-article-as-read article mark)))
10704           t
10705         ;; See whether the article is to be put in the cache.
10706         (and gnus-use-cache
10707              (not (= mark gnus-canceled-mark))
10708              (vectorp (gnus-summary-article-header article))
10709              (save-excursion
10710                (gnus-cache-possibly-enter-article
10711                 gnus-newsgroup-name article
10712                 (= mark gnus-ticked-mark)
10713                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10714
10715         (when (gnus-summary-goto-subject article nil t)
10716           (let ((inhibit-read-only t))
10717             (gnus-summary-show-thread)
10718             ;; Fix the mark.
10719             (gnus-summary-update-mark mark 'unread)
10720             t))))))
10721
10722 (defun gnus-summary-update-secondary-mark (article)
10723   "Update the secondary (read, process, cache) mark."
10724   (gnus-summary-update-mark
10725    (cond ((memq article gnus-newsgroup-processable)
10726           gnus-process-mark)
10727          ((memq article gnus-newsgroup-cached)
10728           gnus-cached-mark)
10729          ((memq article gnus-newsgroup-replied)
10730           gnus-replied-mark)
10731          ((memq article gnus-newsgroup-forwarded)
10732           gnus-forwarded-mark)
10733          ((memq article gnus-newsgroup-saved)
10734           gnus-saved-mark)
10735          ((memq article gnus-newsgroup-recent)
10736           gnus-recent-mark)
10737          ((memq article gnus-newsgroup-unseen)
10738           gnus-unseen-mark)
10739          (t gnus-no-mark))
10740    'replied)
10741   (when (gnus-visual-p 'summary-highlight 'highlight)
10742     (gnus-run-hooks 'gnus-summary-update-hook))
10743   t)
10744
10745 (defun gnus-summary-update-download-mark (article)
10746   "Update the download mark."
10747   (gnus-summary-update-mark
10748    (cond ((memq article gnus-newsgroup-undownloaded)
10749           gnus-undownloaded-mark)
10750          (gnus-newsgroup-agentized
10751           gnus-downloaded-mark)
10752          (t
10753           gnus-no-mark))
10754    'download)
10755   (gnus-summary-update-line t)
10756   t)
10757
10758 (defun gnus-summary-update-mark (mark type)
10759   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10760         (inhibit-read-only t))
10761     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10762     (when forward
10763       (when (looking-at "\r")
10764         (incf forward))
10765       (when (<= (+ forward (point)) (point-max))
10766         ;; Go to the right position on the line.
10767         (goto-char (+ forward (point)))
10768         ;; Replace the old mark with the new mark.
10769         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10770         ;; Optionally update the marks by some user rule.
10771         (when (eq type 'unread)
10772           (gnus-data-set-mark
10773            (gnus-data-find (gnus-summary-article-number)) mark)
10774           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10775
10776 (defun gnus-mark-article-as-read (article &optional mark)
10777   "Enter ARTICLE in the pertinent lists and remove it from others."
10778   ;; Make the article expirable.
10779   (let ((mark (or mark gnus-del-mark)))
10780     (setq gnus-newsgroup-expirable
10781           (if (= mark gnus-expirable-mark)
10782               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10783             (delq article gnus-newsgroup-expirable)))
10784     ;; Remove from unread and marked lists.
10785     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10786     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10787     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10788     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10789     (push (cons article mark) gnus-newsgroup-reads)
10790     ;; Possibly remove from cache, if that is used.
10791     (when gnus-use-cache
10792       (gnus-cache-enter-remove-article article))
10793     t))
10794
10795 (defun gnus-mark-article-as-unread (article &optional mark)
10796   "Enter ARTICLE in the pertinent lists and remove it from others."
10797   (let ((mark (or mark gnus-ticked-mark)))
10798     (if (<= article 0)
10799         (progn
10800           (gnus-error 1 "Can't mark negative article numbers")
10801           nil)
10802       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10803             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10804             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10805             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10806             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10807
10808       ;; Unsuppress duplicates?
10809       (when gnus-suppress-duplicates
10810         (gnus-dup-unsuppress-article article))
10811
10812       (cond ((= mark gnus-ticked-mark)
10813              (setq gnus-newsgroup-marked
10814                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10815             ((= mark gnus-spam-mark)
10816              (setq gnus-newsgroup-spam-marked
10817                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10818                                             article)))
10819             ((= mark gnus-dormant-mark)
10820              (setq gnus-newsgroup-dormant
10821                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10822             (t
10823              (setq gnus-newsgroup-unreads
10824                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10825       (gnus-pull article gnus-newsgroup-reads)
10826       t)))
10827
10828 (defalias 'gnus-summary-mark-as-unread-forward
10829   'gnus-summary-tick-article-forward)
10830 (make-obsolete 'gnus-summary-mark-as-unread-forward
10831                'gnus-summary-tick-article-forward "Emacs 20.4")
10832 (defun gnus-summary-tick-article-forward (n)
10833   "Tick N articles forwards.
10834 If N is negative, tick backwards instead.
10835 The difference between N and the number of articles ticked is returned."
10836   (interactive "p")
10837   (gnus-summary-mark-forward n gnus-ticked-mark))
10838
10839 (defalias 'gnus-summary-mark-as-unread-backward
10840   'gnus-summary-tick-article-backward)
10841 (make-obsolete 'gnus-summary-mark-as-unread-backward
10842                'gnus-summary-tick-article-backward "Emacs 20.4")
10843 (defun gnus-summary-tick-article-backward (n)
10844   "Tick N articles backwards.
10845 The difference between N and the number of articles ticked is returned."
10846   (interactive "p")
10847   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10848
10849 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10850 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article "Emacs 20.4")
10851 (defun gnus-summary-tick-article (&optional article clear-mark)
10852   "Mark current article as unread.
10853 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10854 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10855   (interactive)
10856   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10857                                        gnus-ticked-mark)))
10858
10859 (defun gnus-summary-mark-as-read-forward (n)
10860   "Mark N articles as read forwards.
10861 If N is negative, mark backwards instead.
10862 The difference between N and the actual number of articles marked is
10863 returned."
10864   (interactive "p")
10865   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10866
10867 (defun gnus-summary-mark-as-read-backward (n)
10868   "Mark the N articles as read backwards.
10869 The difference between N and the actual number of articles marked is
10870 returned."
10871   (interactive "p")
10872   (gnus-summary-mark-forward
10873    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10874
10875 (defun gnus-summary-mark-as-read (&optional article mark)
10876   "Mark current article as read.
10877 ARTICLE specifies the article to be marked as read.
10878 MARK specifies a string to be inserted at the beginning of the line."
10879   (gnus-summary-mark-article article mark))
10880
10881 (defun gnus-summary-clear-mark-forward (n)
10882   "Clear marks from N articles forward.
10883 If N is negative, clear backward instead.
10884 The difference between N and the number of marks cleared is returned."
10885   (interactive "p")
10886   (gnus-summary-mark-forward n gnus-unread-mark))
10887
10888 (defun gnus-summary-clear-mark-backward (n)
10889   "Clear marks from N articles backward.
10890 The difference between N and the number of marks cleared is returned."
10891   (interactive "p")
10892   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10893
10894 (defun gnus-summary-mark-unread-as-read ()
10895   "Intended to be used by `gnus-mark-article-hook'."
10896   (when (memq gnus-current-article gnus-newsgroup-unreads)
10897     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10898
10899 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10900   "Intended to be used by `gnus-mark-article-hook'."
10901   (let ((mark (gnus-summary-article-mark)))
10902     (when (or (gnus-unread-mark-p mark)
10903               (gnus-read-mark-p mark))
10904       (gnus-summary-mark-article gnus-current-article
10905                                  (or new-mark gnus-read-mark)))))
10906
10907 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10908   "Intended to be used by `gnus-mark-article-hook'."
10909   (let ((mark (gnus-summary-article-mark)))
10910     (when (or (gnus-unread-mark-p mark)
10911               (gnus-read-mark-p mark))
10912       (gnus-summary-mark-article (gnus-summary-article-number)
10913                                  (or new-mark gnus-read-mark)))))
10914
10915 (defun gnus-summary-mark-unread-as-ticked ()
10916   "Intended to be used by `gnus-mark-article-hook'."
10917   (when (memq gnus-current-article gnus-newsgroup-unreads)
10918     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10919
10920 (defun gnus-summary-mark-region-as-read (point mark all)
10921   "Mark all unread articles between point and mark as read.
10922 If given a prefix, mark all articles between point and mark as read,
10923 even ticked and dormant ones."
10924   (interactive "r\nP")
10925   (save-excursion
10926     (let (article)
10927       (goto-char point)
10928       (beginning-of-line)
10929       (while (and
10930               (< (point) mark)
10931               (progn
10932                 (when (or all
10933                           (memq (setq article (gnus-summary-article-number))
10934                                 gnus-newsgroup-unreads))
10935                   (gnus-summary-mark-article article gnus-del-mark))
10936                 t)
10937               (gnus-summary-find-next))))))
10938
10939 (defun gnus-summary-mark-below (score mark)
10940   "Mark articles with score less than SCORE with MARK."
10941   (interactive "P\ncMark: ")
10942   (setq score (if score
10943                   (prefix-numeric-value score)
10944                 (or gnus-summary-default-score 0)))
10945   (with-current-buffer gnus-summary-buffer
10946     (goto-char (point-min))
10947     (while
10948         (progn
10949           (and (< (gnus-summary-article-score) score)
10950                (gnus-summary-mark-article nil mark))
10951           (gnus-summary-find-next)))))
10952
10953 (defun gnus-summary-kill-below (&optional score)
10954   "Mark articles with score below SCORE as read."
10955   (interactive "P")
10956   (gnus-summary-mark-below score gnus-killed-mark))
10957
10958 (defun gnus-summary-clear-above (&optional score)
10959   "Clear all marks from articles with score above SCORE."
10960   (interactive "P")
10961   (gnus-summary-mark-above score gnus-unread-mark))
10962
10963 (defun gnus-summary-tick-above (&optional score)
10964   "Tick all articles with score above SCORE."
10965   (interactive "P")
10966   (gnus-summary-mark-above score gnus-ticked-mark))
10967
10968 (defun gnus-summary-mark-above (score mark)
10969   "Mark articles with score over SCORE with MARK."
10970   (interactive "P\ncMark: ")
10971   (setq score (if score
10972                   (prefix-numeric-value score)
10973                 (or gnus-summary-default-score 0)))
10974   (with-current-buffer gnus-summary-buffer
10975     (goto-char (point-min))
10976     (while (and (progn
10977                   (when (> (gnus-summary-article-score) score)
10978                     (gnus-summary-mark-article nil mark))
10979                   t)
10980                 (gnus-summary-find-next)))))
10981
10982 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10983 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10984 (defun gnus-summary-limit-include-expunged (&optional no-error)
10985   "Display all the hidden articles that were expunged for low scores."
10986   (interactive)
10987   (let ((inhibit-read-only t))
10988     (let ((scored gnus-newsgroup-scored)
10989           headers h)
10990       (while scored
10991         (unless (gnus-summary-article-header (caar scored))
10992           (and (setq h (gnus-number-to-header (caar scored)))
10993                (< (cdar scored) gnus-summary-expunge-below)
10994                (push h headers)))
10995         (setq scored (cdr scored)))
10996       (if (not headers)
10997           (when (not no-error)
10998             (error "No expunged articles hidden"))
10999         (goto-char (point-min))
11000         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11001         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11002         (dolist (x headers)
11003           (push (mail-header-number x) gnus-newsgroup-limit))
11004         (gnus-summary-prepare-unthreaded (nreverse headers))
11005         (goto-char (point-min))
11006         (gnus-summary-position-point)
11007         t))))
11008
11009 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11010   "Mark all unread articles in this newsgroup as read.
11011 If prefix argument ALL is non-nil, ticked and dormant articles will
11012 also be marked as read.
11013 If QUIETLY is non-nil, no questions will be asked.
11014
11015 If TO-HERE is non-nil, it should be a point in the buffer.  All
11016 articles before (after, if REVERSE is set) this point will be marked
11017 as read.
11018
11019 Note that this function will only catch up the unread article
11020 in the current summary buffer limitation.
11021
11022 The number of articles marked as read is returned."
11023   (interactive "P")
11024   (prog1
11025       (save-excursion
11026         (when (or quietly
11027                   (not gnus-interactive-catchup) ;Without confirmation?
11028                   gnus-expert-user
11029                   (gnus-y-or-n-p
11030                    (if all
11031                        "Mark absolutely all articles as read? "
11032                      "Mark all unread articles as read? ")))
11033           (if (and not-mark
11034                    (not gnus-newsgroup-adaptive)
11035                    (not gnus-newsgroup-auto-expire)
11036                    (not gnus-suppress-duplicates)
11037                    (or (not gnus-use-cache)
11038                        (eq gnus-use-cache 'passive)))
11039               (progn
11040                 (when all
11041                   (setq gnus-newsgroup-marked nil
11042                         gnus-newsgroup-spam-marked nil
11043                         gnus-newsgroup-dormant nil))
11044                 (setq gnus-newsgroup-unreads
11045                       (gnus-sorted-nunion
11046                        (gnus-sorted-intersection gnus-newsgroup-unreads
11047                                                  gnus-newsgroup-downloadable)
11048                        (gnus-sorted-difference gnus-newsgroup-unfetched
11049                                                gnus-newsgroup-cached))))
11050             ;; We actually mark all articles as canceled, which we
11051             ;; have to do when using auto-expiry or adaptive scoring.
11052             (gnus-summary-show-all-threads)
11053             (if (and to-here reverse)
11054                 (progn
11055                   (goto-char to-here)
11056                   (gnus-summary-mark-current-read-and-unread-as-read
11057                    gnus-catchup-mark)
11058                   (while (gnus-summary-find-next (not all))
11059                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11060               (when (gnus-summary-first-subject (not all))
11061                 (while (and
11062                         (if to-here (< (point) to-here) t)
11063                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11064                         (gnus-summary-find-next (not all))))))
11065             (gnus-set-mode-line 'summary))
11066           t))
11067     (gnus-summary-position-point)))
11068
11069 (defun gnus-summary-catchup-to-here (&optional all)
11070   "Mark all unticked articles before the current one as read.
11071 If ALL is non-nil, also mark ticked and dormant articles as read."
11072   (interactive "P")
11073   (save-excursion
11074     (gnus-save-hidden-threads
11075       (let ((beg (point)))
11076         ;; We check that there are unread articles.
11077         (when (or all (gnus-summary-find-prev))
11078           (gnus-summary-catchup all t beg)))))
11079   (gnus-summary-position-point))
11080
11081 (defun gnus-summary-catchup-from-here (&optional all)
11082   "Mark all unticked articles after (and including) the current one as read.
11083 If ALL is non-nil, also mark ticked and dormant articles as read."
11084   (interactive "P")
11085   (save-excursion
11086     (gnus-save-hidden-threads
11087       (let ((beg (point)))
11088         ;; We check that there are unread articles.
11089         (when (or all (gnus-summary-find-next))
11090           (gnus-summary-catchup all t beg nil t)))))
11091   (gnus-summary-position-point))
11092
11093 (defun gnus-summary-catchup-all (&optional quietly)
11094   "Mark all articles in this newsgroup as read.
11095 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11096 instead, which marks only unread articles as read."
11097   (interactive "P")
11098   (gnus-summary-catchup t quietly))
11099
11100 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11101   "Mark all unread articles in this group as read, then exit.
11102 If prefix argument ALL is non-nil, all articles are marked as read.
11103 If QUIETLY is non-nil, no questions will be asked."
11104   (interactive "P")
11105   (when (gnus-summary-catchup all quietly nil 'fast)
11106     ;; Select next newsgroup or exit.
11107     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11108              (eq gnus-auto-select-next 'quietly))
11109         (gnus-summary-next-group nil)
11110       (gnus-summary-exit))))
11111
11112 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11113   "Mark all articles in this newsgroup as read, and then exit.
11114 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11115 instead, which marks only unread articles as read."
11116   (interactive "P")
11117   (gnus-summary-catchup-and-exit t quietly))
11118
11119 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11120   "Mark all articles in this group as read and select the next group.
11121 If given a prefix, mark all articles, unread as well as ticked, as
11122 read."
11123   (interactive "P")
11124   (save-excursion
11125     (gnus-summary-catchup all))
11126   (gnus-summary-next-group))
11127
11128 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11129   "Mark all articles in this group as read and select the previous 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 nil nil t))
11136
11137 ;;;
11138 ;;; with article
11139 ;;;
11140
11141 (defmacro gnus-with-article (article &rest forms)
11142   "Select ARTICLE and perform FORMS in the original article buffer.
11143 Then replace the article with the result."
11144   `(progn
11145      ;; We don't want the article to be marked as read.
11146      (let (gnus-mark-article-hook)
11147        (gnus-summary-select-article t t nil ,article))
11148      (set-buffer gnus-original-article-buffer)
11149      ,@forms
11150      (if (not (gnus-check-backend-function
11151                'request-replace-article (car gnus-article-current)))
11152          (gnus-message 5 "Read-only group; not replacing")
11153        (unless (gnus-request-replace-article
11154                 ,article (car gnus-article-current)
11155                 (current-buffer) t)
11156          (error "Couldn't replace article")))
11157      ;; The cache and backlog have to be flushed somewhat.
11158      (when gnus-keep-backlog
11159        (gnus-backlog-remove-article
11160         (car gnus-article-current) (cdr gnus-article-current)))
11161      (when gnus-use-cache
11162        (gnus-cache-update-article
11163         (car gnus-article-current) (cdr gnus-article-current)))))
11164
11165 (put 'gnus-with-article 'lisp-indent-function 1)
11166 (put 'gnus-with-article 'edebug-form-spec '(form body))
11167
11168 ;; Thread-based commands.
11169
11170 (defun gnus-summary-articles-in-thread (&optional article)
11171   "Return a list of all articles in the current thread.
11172 If ARTICLE is non-nil, return all articles in the thread that starts
11173 with that article."
11174   (let* ((article (or article (gnus-summary-article-number)))
11175          (data (gnus-data-find-list article))
11176          (top-level (gnus-data-level (car data)))
11177          (top-subject
11178           (cond ((null gnus-thread-operation-ignore-subject)
11179                  (gnus-simplify-subject-re
11180                   (mail-header-subject (gnus-data-header (car data)))))
11181                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11182                  (gnus-simplify-subject-fuzzy
11183                   (mail-header-subject (gnus-data-header (car data)))))
11184                 (t nil)))
11185          (end-point (save-excursion
11186                       (if (gnus-summary-go-to-next-thread)
11187                           (point) (point-max))))
11188          articles)
11189     (while (and data
11190                 (< (gnus-data-pos (car data)) end-point))
11191       (when (or (not top-subject)
11192                 (string= top-subject
11193                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11194                              (gnus-simplify-subject-fuzzy
11195                               (mail-header-subject
11196                                (gnus-data-header (car data))))
11197                            (gnus-simplify-subject-re
11198                             (mail-header-subject
11199                              (gnus-data-header (car data)))))))
11200         (push (gnus-data-number (car data)) articles))
11201       (unless (and (setq data (cdr data))
11202                    (> (gnus-data-level (car data)) top-level))
11203         (setq data nil)))
11204     ;; Return the list of articles.
11205     (nreverse articles)))
11206
11207 (defun gnus-summary-rethread-current ()
11208   "Rethread the thread the current article is part of."
11209   (interactive)
11210   (let* ((gnus-show-threads t)
11211          (article (gnus-summary-article-number))
11212          (id (mail-header-id (gnus-summary-article-header)))
11213          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11214     (unless id
11215       (error "No article on the current line"))
11216     (gnus-rebuild-thread id)
11217     (gnus-summary-goto-subject article)))
11218
11219 (defun gnus-summary-reparent-thread ()
11220   "Make the current article child of the marked (or previous) article.
11221
11222 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11223 is non-nil or the Subject: of both articles are the same."
11224   (interactive)
11225   (unless (not (gnus-group-read-only-p))
11226     (error "The current newsgroup does not support article editing"))
11227   (unless (<= (length gnus-newsgroup-processable) 1)
11228     (error "No more than one article may be marked"))
11229   (let ((child (gnus-summary-article-number))
11230         ;; First grab the marked article, otherwise one line up.
11231         (parent (if (not (null gnus-newsgroup-processable))
11232                     (car gnus-newsgroup-processable)
11233                   (save-excursion
11234                     (if (eq (forward-line -1) 0)
11235                         (gnus-summary-article-number)
11236                       (error "Beginning of summary buffer"))))))
11237     (gnus-summary-reparent-children parent (list child))))
11238
11239 (defun gnus-summary-reparent-children (parent children)
11240   "Make PARENT the parent of CHILDREN.
11241 When called interactively, PARENT is the current article and CHILDREN
11242 are the process-marked articles."
11243   (interactive
11244    (list (gnus-summary-article-number)
11245          (gnus-summary-work-articles nil)))
11246   (dolist (child children)
11247     (save-window-excursion
11248       (let ((gnus-article-buffer " *reparent*"))
11249         (unless (not (eq parent child))
11250           (error "An article may not be self-referential"))
11251         (let ((message-id (mail-header-id
11252                            (gnus-summary-article-header parent))))
11253           (unless (and message-id (not (equal message-id "")))
11254             (error "No message-id in desired parent"))
11255           (gnus-with-article child
11256             (save-restriction
11257               (goto-char (point-min))
11258               (message-narrow-to-head)
11259               (if (re-search-forward "^References: " nil t)
11260                   (progn
11261                     (re-search-forward "^[^ \t]" nil t)
11262                     (forward-line -1)
11263                     (end-of-line)
11264                     (insert " " message-id))
11265                 (insert "References: " message-id "\n"))))
11266           (set-buffer gnus-summary-buffer)
11267           (gnus-summary-unmark-all-processable)
11268           (gnus-summary-update-article child)
11269           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11270             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11271           (gnus-summary-rethread-current)
11272           (gnus-message 3 "Article %d is now the child of article %d"
11273                         child parent))))))
11274
11275 (defun gnus-summary-toggle-threads (&optional arg)
11276   "Toggle showing conversation threads.
11277 If ARG is positive number, turn showing conversation threads on."
11278   (interactive "P")
11279   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11280     (setq gnus-show-threads
11281           (if (null arg) (not gnus-show-threads)
11282             (> (prefix-numeric-value arg) 0)))
11283     (gnus-summary-prepare)
11284     (gnus-summary-goto-subject current)
11285     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11286     (gnus-summary-position-point)))
11287
11288 (eval-and-compile
11289   (if (fboundp 'remove-overlays)
11290       (defalias 'gnus-remove-overlays 'remove-overlays)
11291     (defun gnus-remove-overlays (beg end name val)
11292       "Clear BEG and END of overlays whose property NAME has value VAL.
11293 For compatibility with Emacs 21 and XEmacs."
11294       (dolist (ov (gnus-overlays-in beg end))
11295         (when (eq (gnus-overlay-get ov name) val)
11296           (gnus-delete-overlay ov))))))
11297
11298 (defun gnus-summary-show-all-threads ()
11299   "Show all threads."
11300   (interactive)
11301   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11302   (gnus-summary-position-point))
11303
11304 (defun gnus-summary-show-thread ()
11305   "Show thread subtrees.
11306 Returns nil if no thread was there to be shown."
11307   (interactive)
11308   (let* ((orig (point))
11309          (end (point-at-eol))
11310          ;; Leave point at bol
11311          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11312          (eoi (when (eq (get-char-property end 'invisible) 'gnus-sum)
11313                 (if (fboundp 'next-single-char-property-change)
11314                     (or (next-single-char-property-change end 'invisible)
11315                         (point-max))
11316                   (while (progn
11317                            (end-of-line 2)
11318                            (and (not (eobp))
11319                                 (eq (get-char-property (point) 'invisible)
11320                                     'gnus-sum))))
11321                   (point)))))
11322     (when eoi
11323       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11324       (goto-char orig)
11325       (gnus-summary-position-point)
11326       eoi)))
11327
11328 (defun gnus-summary-maybe-hide-threads ()
11329   "If requested, hide the threads that should be hidden."
11330   (when (and gnus-show-threads
11331              gnus-thread-hide-subtree)
11332     (gnus-summary-hide-all-threads
11333      (if (or (consp gnus-thread-hide-subtree)
11334              (functionp gnus-thread-hide-subtree))
11335          (gnus-make-predicate gnus-thread-hide-subtree)
11336        nil))))
11337
11338 ;;; Hiding predicates.
11339
11340 (defun gnus-article-unread-p (header)
11341   (memq (mail-header-number header) gnus-newsgroup-unreads))
11342
11343 (defun gnus-article-unseen-p (header)
11344   (memq (mail-header-number header) gnus-newsgroup-unseen))
11345
11346 (defun gnus-map-articles (predicate articles)
11347   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11348   (apply 'gnus-or (mapcar predicate
11349                           (mapcar (lambda (number)
11350                                     (gnus-summary-article-header number))
11351                                   articles))))
11352
11353 (defun gnus-summary-hide-all-threads (&optional predicate)
11354   "Hide all thread subtrees.
11355 If PREDICATE is supplied, threads that satisfy this predicate
11356 will not be hidden."
11357   (interactive)
11358   (save-excursion
11359     (goto-char (point-min))
11360     (let ((end nil))
11361       (while (not end)
11362         (when (or (not predicate)
11363                   (gnus-map-articles
11364                    predicate (gnus-summary-article-children)))
11365             (gnus-summary-hide-thread))
11366         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11367   (gnus-summary-position-point))
11368
11369 (defun gnus-summary-hide-thread ()
11370   "Hide thread subtrees.
11371 If PREDICATE is supplied, threads that satisfy this predicate
11372 will not be hidden.
11373 Returns nil if no threads were there to be hidden."
11374   (interactive)
11375   (let ((start (point))
11376         (starteol (line-end-position))
11377         (article (gnus-summary-article-number)))
11378     (goto-char start)
11379     ;; Go forward until either the buffer ends or the subthread ends.
11380     (when (and (not (eobp))
11381                (or (zerop (gnus-summary-next-thread 1 t))
11382                    (goto-char (point-max))))
11383       (if (and (> (point) start)
11384                ;; FIXME: this should actually search for a non-invisible \n.
11385                (search-backward "\n" start t))
11386           (progn
11387             (when (> (point) starteol)
11388               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11389               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11390                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11391                 (gnus-overlay-put ol 'evaporate t)))
11392             (gnus-summary-goto-subject article))
11393         (goto-char start)
11394         nil))))
11395
11396 (defun gnus-summary-go-to-next-thread (&optional previous)
11397   "Go to the same level (or less) next thread.
11398 If PREVIOUS is non-nil, go to previous thread instead.
11399 Return the article number moved to, or nil if moving was impossible."
11400   (let ((level (gnus-summary-thread-level))
11401         (way (if previous -1 1))
11402         (beg (point)))
11403     (forward-line way)
11404     (while (and (not (eobp))
11405                 (< level (gnus-summary-thread-level)))
11406       (forward-line way))
11407     (if (eobp)
11408         (progn
11409           (goto-char beg)
11410           nil)
11411       (setq beg (point))
11412       (prog1
11413           (gnus-summary-article-number)
11414         (goto-char beg)))))
11415
11416 (defun gnus-summary-next-thread (n &optional silent)
11417   "Go to the same level next N'th thread.
11418 If N is negative, search backward instead.
11419 Returns the difference between N and the number of skips actually
11420 done.
11421
11422 If SILENT, don't output messages."
11423   (interactive "p")
11424   (let ((backward (< n 0))
11425         (n (abs n)))
11426     (while (and (> n 0)
11427                 (gnus-summary-go-to-next-thread backward))
11428       (decf n))
11429     (unless silent
11430       (gnus-summary-position-point))
11431     (when (and (not silent) (/= 0 n))
11432       (gnus-message 7 "No more threads"))
11433     n))
11434
11435 (defun gnus-summary-prev-thread (n)
11436   "Go to the same level previous N'th thread.
11437 Returns the difference between N and the number of skips actually
11438 done."
11439   (interactive "p")
11440   (gnus-summary-next-thread (- n)))
11441
11442 (defun gnus-summary-go-down-thread ()
11443   "Go down one level in the current thread."
11444   (let ((children (gnus-summary-article-children)))
11445     (when children
11446       (gnus-summary-goto-subject (car children)))))
11447
11448 (defun gnus-summary-go-up-thread ()
11449   "Go up one level in the current thread."
11450   (let ((parent (gnus-summary-article-parent)))
11451     (when parent
11452       (gnus-summary-goto-subject parent))))
11453
11454 (defun gnus-summary-down-thread (n)
11455   "Go down thread N steps.
11456 If N is negative, go up instead.
11457 Returns the difference between N and how many steps down that were
11458 taken."
11459   (interactive "p")
11460   (let ((up (< n 0))
11461         (n (abs n)))
11462     (while (and (> n 0)
11463                 (if up (gnus-summary-go-up-thread)
11464                   (gnus-summary-go-down-thread)))
11465       (setq n (1- n)))
11466     (gnus-summary-position-point)
11467     (when (/= 0 n)
11468       (gnus-message 7 "Can't go further"))
11469     n))
11470
11471 (defun gnus-summary-up-thread (n)
11472   "Go up thread N steps.
11473 If N is negative, go down instead.
11474 Returns the difference between N and how many steps down that were
11475 taken."
11476   (interactive "p")
11477   (gnus-summary-down-thread (- n)))
11478
11479 (defun gnus-summary-top-thread ()
11480   "Go to the top of the thread."
11481   (interactive)
11482   (while (gnus-summary-go-up-thread))
11483   (gnus-summary-article-number))
11484
11485 (defun gnus-summary-expire-thread ()
11486   "Mark articles under current thread as expired."
11487   (interactive)
11488   (gnus-summary-kill-thread 0))
11489
11490 (defun gnus-summary-kill-thread (&optional unmark)
11491   "Mark articles under current thread as read.
11492 If the prefix argument is positive, remove any kinds of marks.
11493 If the prefix argument is zero, mark thread as expired.
11494 If the prefix argument is negative, tick articles instead."
11495   (interactive "P")
11496   (when unmark
11497     (setq unmark (prefix-numeric-value unmark)))
11498   (let ((articles (gnus-summary-articles-in-thread))
11499         (hide (or (null unmark) (= unmark 0))))
11500     (save-excursion
11501       ;; Expand the thread.
11502       (gnus-summary-show-thread)
11503       ;; Mark all the articles.
11504       (while articles
11505         (gnus-summary-goto-subject (car articles))
11506         (cond ((null unmark)
11507                (gnus-summary-mark-article-as-read gnus-killed-mark))
11508               ((> unmark 0)
11509                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11510               ((= unmark 0)
11511                (gnus-summary-mark-article nil gnus-expirable-mark))
11512               (t
11513                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11514         (setq articles (cdr articles))))
11515     ;; Hide killed subtrees when hide is true.
11516     (and hide
11517          gnus-thread-hide-killed
11518          (gnus-summary-hide-thread))
11519     ;; If hide is t, go to next unread subject.
11520     (when hide
11521       ;; Go to next unread subject.
11522       (gnus-summary-next-subject 1 t)))
11523   (gnus-set-mode-line 'summary))
11524
11525 ;; Summary sorting commands
11526
11527 (defun gnus-summary-sort-by-number (&optional reverse)
11528   "Sort the summary buffer by article number.
11529 Argument REVERSE means reverse order."
11530   (interactive "P")
11531   (gnus-summary-sort 'number reverse))
11532
11533 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11534   "Sort the summary buffer by most recent article number.
11535 Argument REVERSE means reverse order."
11536   (interactive "P")
11537   (gnus-summary-sort 'most-recent-number reverse))
11538
11539 (defun gnus-summary-sort-by-random (&optional reverse)
11540   "Randomize the order in the summary buffer.
11541 Argument REVERSE means to randomize in reverse order."
11542   (interactive "P")
11543   (gnus-summary-sort 'random reverse))
11544
11545 (defun gnus-summary-sort-by-author (&optional reverse)
11546   "Sort the summary buffer by author name alphabetically.
11547 If `case-fold-search' is non-nil, case of letters is ignored.
11548 Argument REVERSE means reverse order."
11549   (interactive "P")
11550   (gnus-summary-sort 'author reverse))
11551
11552 (defun gnus-summary-sort-by-recipient (&optional reverse)
11553   "Sort the summary buffer by recipient name alphabetically.
11554 If `case-fold-search' is non-nil, case of letters is ignored.
11555 Argument REVERSE means reverse order."
11556   (interactive "P")
11557   (gnus-summary-sort 'recipient reverse))
11558
11559 (defun gnus-summary-sort-by-subject (&optional reverse)
11560   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11561 If `case-fold-search' is non-nil, case of letters is ignored.
11562 Argument REVERSE means reverse order."
11563   (interactive "P")
11564   (gnus-summary-sort 'subject reverse))
11565
11566 (defun gnus-summary-sort-by-date (&optional reverse)
11567   "Sort the summary buffer by date.
11568 Argument REVERSE means reverse order."
11569   (interactive "P")
11570   (gnus-summary-sort 'date reverse))
11571
11572 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11573   "Sort the summary buffer by most recent date.
11574 Argument REVERSE means reverse order."
11575   (interactive "P")
11576   (gnus-summary-sort 'most-recent-date reverse))
11577
11578 (defun gnus-summary-sort-by-score (&optional reverse)
11579   "Sort the summary buffer by score.
11580 Argument REVERSE means reverse order."
11581   (interactive "P")
11582   (gnus-summary-sort 'score reverse))
11583
11584 (defun gnus-summary-sort-by-lines (&optional reverse)
11585   "Sort the summary buffer by the number of lines.
11586 Argument REVERSE means reverse order."
11587   (interactive "P")
11588   (gnus-summary-sort 'lines reverse))
11589
11590 (defun gnus-summary-sort-by-chars (&optional reverse)
11591   "Sort the summary buffer by article length.
11592 Argument REVERSE means reverse order."
11593   (interactive "P")
11594   (gnus-summary-sort 'chars reverse))
11595
11596 (defun gnus-summary-sort-by-original (&optional reverse)
11597   "Sort the summary buffer using the default sorting method.
11598 Argument REVERSE means reverse order."
11599   (interactive "P")
11600   (let* ((inhibit-read-only t)
11601          (gnus-summary-prepare-hook nil))
11602     ;; We do the sorting by regenerating the threads.
11603     (gnus-summary-prepare)
11604     ;; Hide subthreads if needed.
11605     (gnus-summary-maybe-hide-threads)))
11606
11607 (defun gnus-summary-sort (predicate reverse)
11608   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11609   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11610          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11611          (gnus-thread-sort-functions
11612           (if (not reverse)
11613               thread
11614             `(lambda (t1 t2)
11615                (,thread t2 t1))))
11616          (gnus-sort-gathered-threads-function
11617           gnus-thread-sort-functions)
11618          (gnus-article-sort-functions
11619           (if (not reverse)
11620               article
11621             `(lambda (t1 t2)
11622                (,article t2 t1))))
11623          (inhibit-read-only t)
11624          (gnus-summary-prepare-hook nil))
11625     ;; We do the sorting by regenerating the threads.
11626     (gnus-summary-prepare)
11627     ;; Hide subthreads if needed.
11628     (gnus-summary-maybe-hide-threads)))
11629
11630 ;; Summary saving commands.
11631
11632 (defun gnus-summary-save-article (&optional n not-saved)
11633   "Save the current article using the default saver function.
11634 If N is a positive number, save the N next articles.
11635 If N is a negative number, save the N previous articles.
11636 If N is nil and any articles have been marked with the process mark,
11637 save those articles instead.
11638 The variable `gnus-default-article-saver' specifies the saver function.
11639
11640 If the optional second argument NOT-SAVED is non-nil, articles saved
11641 will not be marked as saved."
11642   (interactive "P")
11643   (require 'gnus-art)
11644   (let* ((articles (gnus-summary-work-articles n))
11645          (save-buffer (save-excursion
11646                         (nnheader-set-temp-buffer " *Gnus Save*")))
11647          (num (length articles))
11648          ;; Whether to save decoded articles or raw articles.
11649          (decode (when gnus-article-save-coding-system
11650                    (get gnus-default-article-saver :decode)))
11651          ;; When saving many articles in a single file, use the other
11652          ;; function to save articles other than the first one.
11653          (saver2 (get gnus-default-article-saver :function))
11654          (gnus-prompt-before-saving (if saver2
11655                                         t
11656                                       gnus-prompt-before-saving))
11657          (gnus-default-article-saver gnus-default-article-saver)
11658          header file)
11659     (dolist (article articles)
11660       (setq header (gnus-summary-article-header article))
11661       (if (not (vectorp header))
11662           ;; This is a pseudo-article.
11663           (if (assq 'name header)
11664               (gnus-copy-file (cdr (assq 'name header)))
11665             (gnus-message 1 "Article %d is unsaveable" article))
11666         ;; This is a real article.
11667         (save-window-excursion
11668           (let ((gnus-display-mime-function (when decode
11669                                               gnus-display-mime-function))
11670                 (gnus-article-prepare-hook (when decode
11671                                              gnus-article-prepare-hook)))
11672             (gnus-summary-select-article t t nil article)
11673             (gnus-summary-goto-subject article)))
11674         (with-current-buffer save-buffer
11675           (erase-buffer)
11676           (insert-buffer-substring (if decode
11677                                        gnus-article-buffer
11678                                      gnus-original-article-buffer)))
11679         (setq file (gnus-article-save save-buffer file num))
11680         (gnus-summary-remove-process-mark article)
11681         (unless not-saved
11682           (gnus-summary-set-saved-mark article)))
11683       (when saver2
11684         (setq gnus-default-article-saver saver2
11685               saver2 nil)))
11686     (gnus-kill-buffer save-buffer)
11687     (gnus-summary-position-point)
11688     (gnus-set-mode-line 'summary)
11689     n))
11690
11691 (defun gnus-summary-pipe-output (&optional n sym)
11692   "Pipe the current article to a subprocess.
11693 If N is a positive number, pipe the N next articles.
11694 If N is a negative number, pipe the N previous articles.
11695 If N is nil and any articles have been marked with the process mark,
11696 pipe those articles instead.
11697 The default command to which articles are piped is specified by the
11698 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11699 will be prompted for the command.
11700
11701 The properties `:decode' and `:headers' that are put to the function
11702 symbol `gnus-summary-save-in-pipe' control whether this function
11703 decodes articles and what headers to keep (see the doc string for the
11704 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11705 is neither omitted nor the symbol `r', force including all headers
11706 regardless of the `:headers' property.  If it is the symbol `r',
11707 articles that are not decoded and include all headers will be piped
11708 no matter what the properties `:decode' and `:headers' are."
11709   (interactive (gnus-interactive "P\ny"))
11710   (require 'gnus-art)
11711   (let* ((articles (gnus-summary-work-articles n))
11712          (result-buffer "*Shell Command Output*")
11713          (all-headers (not (memq sym '(nil r))))
11714          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11715          (raw (eq sym 'r))
11716          (headers (get 'gnus-summary-save-in-pipe :headers))
11717          command result)
11718     (unless (numberp (car articles))
11719       (error "No article to pipe"))
11720     (setq command (gnus-read-shell-command
11721                    (concat "Shell command on "
11722                            (if (cdr articles)
11723                                (format "these %d articles" (length articles))
11724                              "this article")
11725                            ": ")
11726                    gnus-summary-pipe-output-default-command))
11727     (when (string-equal command "")
11728       (error "A command is required"))
11729     (when all-headers
11730       (put 'gnus-summary-save-in-pipe :headers nil))
11731     (unwind-protect
11732         (while articles
11733           (gnus-summary-goto-subject (pop articles))
11734           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11735           (when (and (get-buffer result-buffer)
11736                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11737             (setq result (concat result (with-current-buffer result-buffer
11738                                           (buffer-string))))))
11739       (put 'gnus-summary-save-in-pipe :headers headers))
11740     (unless (zerop (length result))
11741       (if (with-current-buffer (get-buffer-create result-buffer)
11742             (erase-buffer)
11743             (insert result)
11744             (prog1
11745                 (and (= (count-lines (point-min) (point)) 1)
11746                      (progn
11747                        (end-of-line 0)
11748                        (<= (current-column)
11749                            (window-width (minibuffer-window)))))
11750               (goto-char (point-min))))
11751           (message "%s" (substring result 0 -1))
11752         (message nil)
11753         (gnus-configure-windows 'pipe)))))
11754
11755 (defun gnus-summary-save-article-mail (&optional arg)
11756   "Append the current article to a Unix mail box file.
11757 If N is a positive number, save the N next articles.
11758 If N is a negative number, save the N previous articles.
11759 If N is nil and any articles have been marked with the process mark,
11760 save those articles instead."
11761   (interactive "P")
11762   (require 'gnus-art)
11763   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11764     (gnus-summary-save-article arg)))
11765
11766 (defun gnus-summary-save-article-rmail (&optional arg)
11767   "Append the current article to an rmail file.
11768 If N is a positive number, save the N next articles.
11769 If N is a negative number, save the N previous articles.
11770 If N is nil and any articles have been marked with the process mark,
11771 save those articles instead."
11772   (interactive "P")
11773   (require 'gnus-art)
11774   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11775     (gnus-summary-save-article arg)))
11776
11777 (defun gnus-summary-save-article-file (&optional arg)
11778   "Append the current article to a file.
11779 If N is a positive number, save the N next articles.
11780 If N is a negative number, save the N previous articles.
11781 If N is nil and any articles have been marked with the process mark,
11782 save those articles instead."
11783   (interactive "P")
11784   (require 'gnus-art)
11785   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11786     (gnus-summary-save-article arg)))
11787
11788 (defun gnus-summary-write-article-file (&optional arg)
11789   "Write the current article to a file, deleting the previous file.
11790 If N is a positive number, save the N next articles.
11791 If N is a negative number, save the N previous articles.
11792 If N is nil and any articles have been marked with the process mark,
11793 save those articles instead."
11794   (interactive "P")
11795   (require 'gnus-art)
11796   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11797     (gnus-summary-save-article arg)))
11798
11799 (defun gnus-summary-save-article-body-file (&optional arg)
11800   "Append the current article body to a file.
11801 If N is a positive number, save the N next articles.
11802 If N is a negative number, save the N previous articles.
11803 If N is nil and any articles have been marked with the process mark,
11804 save those articles instead."
11805   (interactive "P")
11806   (require 'gnus-art)
11807   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11808     (gnus-summary-save-article arg)))
11809
11810 (defun gnus-summary-write-article-body-file (&optional arg)
11811   "Write the current article body to a file, deleting the previous file.
11812 If N is a positive number, save the N next articles.
11813 If N is a negative number, save the N previous articles.
11814 If N is nil and any articles have been marked with the process mark,
11815 save those articles instead."
11816   (interactive "P")
11817   (require 'gnus-art)
11818   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11819     (gnus-summary-save-article arg)))
11820
11821 (defun gnus-summary-muttprint (&optional arg)
11822   "Print the current article using Muttprint.
11823 If N is a positive number, save the N next articles.
11824 If N is a negative number, save the N previous articles.
11825 If N is nil and any articles have been marked with the process mark,
11826 save those articles instead."
11827   (interactive "P")
11828   (require 'gnus-art)
11829   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11830     (gnus-summary-save-article arg t)))
11831
11832 (defun gnus-summary-pipe-message (program)
11833   "Pipe the current article through PROGRAM."
11834   (interactive "sProgram: ")
11835   (gnus-summary-select-article)
11836   (let ((mail-header-separator ""))
11837     (gnus-eval-in-buffer-window gnus-article-buffer
11838       (save-restriction
11839         (widen)
11840         (let ((start (window-start))
11841               (inhibit-read-only t))
11842           (message-pipe-buffer-body program)
11843           (set-window-start (get-buffer-window (current-buffer)) start))))))
11844
11845 (defun gnus-get-split-value (methods)
11846   "Return a value based on the split METHODS."
11847   (let (split-name method result match)
11848     (when methods
11849       (with-current-buffer gnus-original-article-buffer
11850         (save-restriction
11851           (nnheader-narrow-to-headers)
11852           (while (and methods (not split-name))
11853             (goto-char (point-min))
11854             (setq method (pop methods))
11855             (setq match (car method))
11856             (when (cond
11857                    ((stringp match)
11858                     ;; Regular expression.
11859                     (ignore-errors
11860                       (re-search-forward match nil t)))
11861                    ((functionp match)
11862                     ;; Function.
11863                     (save-restriction
11864                       (widen)
11865                       (setq result (funcall match gnus-newsgroup-name))))
11866                    ((consp match)
11867                     ;; Form.
11868                     (save-restriction
11869                       (widen)
11870                       (setq result (eval match)))))
11871               (setq split-name (cdr method))
11872               (cond ((stringp result)
11873                      (push (expand-file-name
11874                             result gnus-article-save-directory)
11875                            split-name))
11876                     ((consp result)
11877                      (setq split-name (append result split-name)))))))))
11878     (nreverse split-name)))
11879
11880 (defun gnus-valid-move-group-p (group)
11881   (and (boundp group)
11882        (symbol-name group)
11883        (symbol-value group)
11884        (gnus-get-function (gnus-find-method-for-group
11885                            (symbol-name group)) 'request-accept-article t)))
11886
11887 (defun gnus-read-move-group-name (prompt default articles prefix)
11888   "Read a group name."
11889   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11890          (minibuffer-confirm-incomplete nil) ; XEmacs
11891          (prom
11892           (format "%s %s to"
11893                   prompt
11894                   (if (> (length articles) 1)
11895                       (format "these %d articles" (length articles))
11896                     "this article")))
11897          (to-newsgroup
11898           (let (active group)
11899             (when (or (null split-name) (= 1 (length split-name)))
11900               (setq active (gnus-make-hashtable (length gnus-active-hashtb)))
11901               (mapatoms (lambda (symbol)
11902                           (setq group (symbol-name symbol))
11903                           (when (string-match "[^\000-\177]" group)
11904                             (setq group (gnus-group-decoded-name group)))
11905                           (set (intern group active) group))
11906                         gnus-active-hashtb))
11907             (cond
11908              ((null split-name)
11909               (gnus-completing-read-with-default
11910                default prom active 'gnus-valid-move-group-p nil prefix
11911                'gnus-group-history))
11912              ((= 1 (length split-name))
11913               (gnus-completing-read-with-default
11914                (car split-name) prom active 'gnus-valid-move-group-p nil nil
11915                'gnus-group-history))
11916              (t
11917               (gnus-completing-read-with-default
11918                nil prom (mapcar 'list (nreverse split-name)) nil nil nil
11919                'gnus-group-history)))))
11920          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11921          encoded)
11922     (when to-newsgroup
11923       (if (or (string= to-newsgroup "")
11924               (string= to-newsgroup prefix))
11925           (setq to-newsgroup default))
11926       (unless to-newsgroup
11927         (error "No group name entered"))
11928       (setq encoded (mm-encode-coding-string
11929                      to-newsgroup
11930                      (gnus-group-name-charset to-method to-newsgroup)))
11931       (or (gnus-active encoded)
11932           (gnus-activate-group encoded nil nil to-method)
11933           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11934                                      to-newsgroup))
11935               (or (and (gnus-request-create-group encoded to-method)
11936                        (gnus-activate-group encoded nil nil to-method)
11937                        (gnus-subscribe-group encoded))
11938                   (error "Couldn't create group %s" to-newsgroup)))
11939           (error "No such group: %s" to-newsgroup))
11940       encoded)))
11941
11942 (defvar gnus-summary-save-parts-counter)
11943 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11944
11945 (defun gnus-summary-save-parts (type dir n &optional reverse)
11946   "Save parts matching TYPE to DIR.
11947 If REVERSE, save parts that do not match TYPE."
11948   (interactive
11949    (list (read-string "Save parts of type: "
11950                       (or (car gnus-summary-save-parts-type-history)
11951                           gnus-summary-save-parts-default-mime)
11952                       'gnus-summary-save-parts-type-history)
11953          (setq gnus-summary-save-parts-last-directory
11954                (read-file-name "Save to directory: "
11955                                gnus-summary-save-parts-last-directory
11956                                nil t))
11957          current-prefix-arg))
11958   (gnus-summary-iterate n
11959     (let ((gnus-display-mime-function nil)
11960           gnus-article-prepare-hook
11961           gnus-article-decode-hook
11962           gnus-display-mime-function
11963           gnus-break-pages
11964           (gnus-inhibit-treatment t))
11965       (gnus-summary-select-article))
11966     (with-current-buffer gnus-article-buffer
11967       (let ((handles (or gnus-article-mime-handles
11968                          (mm-dissect-buffer nil gnus-article-loose-mime)
11969                          (and gnus-article-emulate-mime
11970                               (mm-uu-dissect))))
11971             (gnus-summary-save-parts-counter 1))
11972         (when handles
11973           (gnus-summary-save-parts-1 type dir handles reverse)
11974           (unless gnus-article-mime-handles ;; Don't destroy this case.
11975             (mm-destroy-parts handles)))))))
11976
11977 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11978   (if (stringp (car handle))
11979       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11980               (cdr handle))
11981     (when (if reverse
11982               (not (string-match type (mm-handle-media-type handle)))
11983             (string-match type (mm-handle-media-type handle)))
11984       (let ((file (expand-file-name
11985                    (gnus-map-function
11986                     mm-file-name-rewrite-functions
11987                     (file-name-nondirectory
11988                      (or
11989                       (mail-content-type-get
11990                        (mm-handle-disposition handle) 'filename)
11991                       (mail-content-type-get
11992                        (mm-handle-type handle) 'name)
11993                       (format "%s.%d.%d" gnus-newsgroup-name
11994                               (cdr gnus-article-current)
11995                               gnus-summary-save-parts-counter))))
11996                    dir)))
11997         (incf gnus-summary-save-parts-counter)
11998         (unless (file-exists-p file)
11999           (mm-save-part-to-file handle file))))))
12000
12001 ;; Summary extract commands
12002
12003 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12004   (let ((inhibit-read-only t)
12005         (article (gnus-summary-article-number))
12006         after-article b e)
12007     (unless (gnus-summary-goto-subject article)
12008       (error "No such article: %d" article))
12009     (gnus-summary-position-point)
12010     ;; If all commands are to be bunched up on one line, we collect
12011     ;; them here.
12012     (unless gnus-view-pseudos-separately
12013       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12014             files action)
12015         (while ps
12016           (setq action (cdr (assq 'action (car ps))))
12017           (setq files (list (cdr (assq 'name (car ps)))))
12018           (while (and ps (cdr ps)
12019                       (string= (or action "1")
12020                                (or (cdr (assq 'action (cadr ps))) "2")))
12021             (push (cdr (assq 'name (cadr ps))) files)
12022             (setcdr ps (cddr ps)))
12023           (when files
12024             (when (not (string-match "%s" action))
12025               (push " " files))
12026             (push " " files)
12027             (when (assq 'execute (car ps))
12028               (setcdr (assq 'execute (car ps))
12029                       (funcall (if (string-match "%s" action)
12030                                    'format 'concat)
12031                                action
12032                                (mapconcat
12033                                 (lambda (f)
12034                                   (if (equal f " ")
12035                                       f
12036                                     (shell-quote-argument f)))
12037                                 files " ")))))
12038           (setq ps (cdr ps)))))
12039     (if (and gnus-view-pseudos (not not-view))
12040         (while pslist
12041           (when (assq 'execute (car pslist))
12042             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12043                                   (eq gnus-view-pseudos 'not-confirm)))
12044           (setq pslist (cdr pslist)))
12045       (save-excursion
12046         (while pslist
12047           (setq after-article (or (cdr (assq 'article (car pslist)))
12048                                   (gnus-summary-article-number)))
12049           (gnus-summary-goto-subject after-article)
12050           (forward-line 1)
12051           (setq b (point))
12052           (insert "    " (file-name-nondirectory
12053                           (cdr (assq 'name (car pslist))))
12054                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12055           (setq e (point))
12056           (forward-line -1)             ; back to `b'
12057           (gnus-add-text-properties
12058            b (1- e) (list 'gnus-number gnus-reffed-article-number
12059                           gnus-mouse-face-prop gnus-mouse-face))
12060           (gnus-data-enter
12061            after-article gnus-reffed-article-number
12062            gnus-unread-mark b (car pslist) 0 (- e b))
12063           (setq gnus-newsgroup-unreads
12064                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12065                                          gnus-reffed-article-number))
12066           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12067           (setq pslist (cdr pslist)))))))
12068
12069 (defun gnus-pseudos< (p1 p2)
12070   (let ((c1 (cdr (assq 'action p1)))
12071         (c2 (cdr (assq 'action p2))))
12072     (and c1 c2 (string< c1 c2))))
12073
12074 (defun gnus-request-pseudo-article (props)
12075   (cond ((assq 'execute props)
12076          (gnus-execute-command (cdr (assq 'execute props)))))
12077   (let ((gnus-current-article (gnus-summary-article-number)))
12078     (gnus-run-hooks 'gnus-mark-article-hook)))
12079
12080 (defun gnus-execute-command (command &optional automatic)
12081   (save-excursion
12082     (gnus-article-setup-buffer)
12083     (set-buffer gnus-article-buffer)
12084     (setq buffer-read-only nil)
12085     (let ((command (if automatic command
12086                      (read-string "Command: " (cons command 0)))))
12087       (erase-buffer)
12088       (insert "$ " command "\n\n")
12089       (if gnus-view-pseudo-asynchronously
12090           (start-process "gnus-execute" (current-buffer) shell-file-name
12091                          shell-command-switch command)
12092         (call-process shell-file-name nil t nil
12093                       shell-command-switch command)))))
12094
12095 ;; Summary kill commands.
12096
12097 (defun gnus-summary-edit-global-kill (article)
12098   "Edit the \"global\" kill file."
12099   (interactive (list (gnus-summary-article-number)))
12100   (gnus-group-edit-global-kill article))
12101
12102 (defun gnus-summary-edit-local-kill ()
12103   "Edit a local kill file applied to the current newsgroup."
12104   (interactive)
12105   (setq gnus-current-headers (gnus-summary-article-header))
12106   (gnus-group-edit-local-kill
12107    (gnus-summary-article-number) gnus-newsgroup-name))
12108
12109 ;;; Header reading.
12110
12111 (defun gnus-read-header (id &optional header)
12112   "Read the headers of article ID and enter them into the Gnus system."
12113   (let ((group gnus-newsgroup-name)
12114         (gnus-override-method
12115          (or
12116           gnus-override-method
12117           (and (gnus-news-group-p gnus-newsgroup-name)
12118                (car (gnus-refer-article-methods)))))
12119         where)
12120     ;; First we check to see whether the header in question is already
12121     ;; fetched.
12122     (if (stringp id)
12123         ;; This is a Message-ID.
12124         (setq header (or header (gnus-id-to-header id)))
12125       ;; This is an article number.
12126       (setq header (or header (gnus-summary-article-header id))))
12127     (if (and header
12128              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12129         ;; We have found the header.
12130         header
12131       ;; We have to really fetch the header to this article.
12132       (with-current-buffer nntp-server-buffer
12133         (when (setq where (gnus-request-head id group))
12134           (nnheader-fold-continuation-lines)
12135           (goto-char (point-max))
12136           (insert ".\n")
12137           (goto-char (point-min))
12138           (insert "211 ")
12139           (princ (cond
12140                   ((numberp id) id)
12141                   ((cdr where) (cdr where))
12142                   (header (mail-header-number header))
12143                   (t gnus-reffed-article-number))
12144                  (current-buffer))
12145           (insert " Article retrieved.\n"))
12146         (if (or (not where)
12147                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12148             ()                          ; Malformed head.
12149           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12150             (when (and (stringp id)
12151                        (or
12152                         (not (string= (gnus-group-real-name group)
12153                                       (car where)))
12154                         (not (gnus-server-equal gnus-override-method
12155                                                 (gnus-group-method group)))))
12156               ;; If we fetched by Message-ID and the article came from
12157               ;; a different group (or server), we fudge some bogus
12158               ;; article numbers for this article.
12159               (mail-header-set-number header gnus-reffed-article-number))
12160             (with-current-buffer gnus-summary-buffer
12161               (decf gnus-reffed-article-number)
12162               (gnus-remove-header (mail-header-number header))
12163               (push header gnus-newsgroup-headers)
12164               (setq gnus-current-headers header)
12165               (push (mail-header-number header) gnus-newsgroup-limit)))
12166           header)))))
12167
12168 (defun gnus-remove-header (number)
12169   "Remove header NUMBER from `gnus-newsgroup-headers'."
12170   (if (and gnus-newsgroup-headers
12171            (= number (mail-header-number (car gnus-newsgroup-headers))))
12172       (pop gnus-newsgroup-headers)
12173     (let ((headers gnus-newsgroup-headers))
12174       (while (and (cdr headers)
12175                   (not (= number (mail-header-number (cadr headers)))))
12176         (pop headers))
12177       (when (cdr headers)
12178         (setcdr headers (cddr headers))))))
12179
12180 ;;;
12181 ;;; summary highlights
12182 ;;;
12183
12184 (defun gnus-highlight-selected-summary ()
12185   "Highlight selected article in summary buffer."
12186   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12187   (when gnus-summary-selected-face
12188     (save-excursion
12189       (let* ((beg (point-at-bol))
12190              (end (point-at-eol))
12191              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12192              (from (if (get-text-property beg gnus-mouse-face-prop)
12193                        beg
12194                      (or (next-single-property-change
12195                           beg gnus-mouse-face-prop nil end)
12196                          beg)))
12197              (to
12198               (if (= from end)
12199                   (- from 2)
12200                 (or (next-single-property-change
12201                      from gnus-mouse-face-prop nil end)
12202                     end))))
12203         ;; If no mouse-face prop on line we will have to = from = end,
12204         ;; so we highlight the entire line instead.
12205         (when (= (+ to 2) from)
12206           (setq from beg)
12207           (setq to end))
12208         (if gnus-newsgroup-selected-overlay
12209             ;; Move old overlay.
12210             (gnus-move-overlay
12211              gnus-newsgroup-selected-overlay from to (current-buffer))
12212           ;; Create new overlay.
12213           (gnus-overlay-put
12214            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12215            'face gnus-summary-selected-face))))))
12216
12217 (defvar gnus-summary-highlight-line-cached nil)
12218 (defvar gnus-summary-highlight-line-trigger nil)
12219
12220 (defun gnus-summary-highlight-line-0 ()
12221   (if (and (eq gnus-summary-highlight-line-trigger
12222                gnus-summary-highlight)
12223            gnus-summary-highlight-line-cached)
12224       gnus-summary-highlight-line-cached
12225     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12226           gnus-summary-highlight-line-cached
12227           (let* ((cond (list 'cond))
12228                  (c cond)
12229                  (list gnus-summary-highlight))
12230             (while list
12231               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12232                               nil))
12233               (setq c (cdr c)
12234                     list (cdr list)))
12235             (gnus-byte-compile (list 'lambda nil cond))))))
12236
12237 (defun gnus-summary-highlight-line ()
12238   "Highlight current line according to `gnus-summary-highlight'."
12239   (let* ((beg (point-at-bol))
12240          (article (or (gnus-summary-article-number) gnus-current-article))
12241          (score (or (cdr (assq article
12242                                gnus-newsgroup-scored))
12243                     gnus-summary-default-score 0))
12244          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12245          (inhibit-read-only t)
12246          (default gnus-summary-default-score)
12247          (default-high gnus-summary-default-high-score)
12248          (default-low gnus-summary-default-low-score)
12249          (uncached (and gnus-summary-use-undownloaded-faces
12250                         (memq article gnus-newsgroup-undownloaded)
12251                         (not (memq article gnus-newsgroup-cached)))))
12252     (let ((face (funcall (gnus-summary-highlight-line-0))))
12253       (unless (eq face (get-text-property beg 'face))
12254         (gnus-put-text-property-excluding-characters-with-faces
12255          beg (point-at-eol) 'face
12256          (setq face (if (boundp face) (symbol-value face) face)))
12257         (when gnus-summary-highlight-line-function
12258           (funcall gnus-summary-highlight-line-function article face))))))
12259
12260 (defun gnus-update-read-articles (group unread &optional compute)
12261   "Update the list of read articles in GROUP.
12262 UNREAD is a sorted list."
12263   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12264         (info (gnus-get-info group))
12265         (prev 1)
12266         read)
12267     (if (or (not info) (not active))
12268         ;; There is no info on this group if it was, in fact,
12269         ;; killed.  Gnus stores no information on killed groups, so
12270         ;; there's nothing to be done.
12271         ;; One could store the information somewhere temporarily,
12272         ;; perhaps...  Hmmm...
12273         ()
12274       ;; Remove any negative articles numbers.
12275       (while (and unread (< (car unread) 0))
12276         (setq unread (cdr unread)))
12277       ;; Remove any expired article numbers
12278       (while (and unread (< (car unread) (car active)))
12279         (setq unread (cdr unread)))
12280       ;; Compute the ranges of read articles by looking at the list of
12281       ;; unread articles.
12282       (while unread
12283         (when (/= (car unread) prev)
12284           (push (if (= prev (1- (car unread))) prev
12285                   (cons prev (1- (car unread))))
12286                 read))
12287         (setq prev (1+ (car unread)))
12288         (setq unread (cdr unread)))
12289       (when (<= prev (cdr active))
12290         (push (cons prev (cdr active)) read))
12291       (setq read (if (> (length read) 1) (nreverse read) read))
12292       (if compute
12293           read
12294         (save-excursion
12295           (let (setmarkundo)
12296             ;; Propagate the read marks to the backend.
12297             (when (and gnus-propagate-marks
12298                        (gnus-check-backend-function 'request-set-mark group))
12299               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12300                     (add (gnus-remove-from-range read (gnus-info-read info))))
12301                 (when (or add del)
12302                   (unless (gnus-check-group group)
12303                     (error "Can't open server for %s" group))
12304                   (gnus-request-set-mark
12305                    group (delq nil (list (if add (list add 'add '(read)))
12306                                          (if del (list del 'del '(read))))))
12307                   (setq setmarkundo
12308                         `(gnus-request-set-mark
12309                           ,group
12310                           ',(delq nil (list
12311                                        (if del (list del 'add '(read)))
12312                                        (if add (list add 'del '(read))))))))))
12313             (set-buffer gnus-group-buffer)
12314             (gnus-undo-register
12315               `(progn
12316                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12317                  (gnus-info-set-read ',info ',(gnus-info-read info))
12318                  (gnus-get-unread-articles-in-group ',info
12319                                                     (gnus-active ,group))
12320                  (gnus-group-update-group ,group t)
12321                  ,setmarkundo))))
12322         ;; Enter this list into the group info.
12323         (gnus-info-set-read info read)
12324         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12325         (gnus-get-unread-articles-in-group info (gnus-active group))
12326         t))))
12327
12328 (defun gnus-offer-save-summaries ()
12329   "Offer to save all active summary buffers."
12330   (let (buffers)
12331     ;; Go through all buffers and find all summaries.
12332     (dolist (buffer (buffer-list))
12333       (when (and (setq buffer (buffer-name buffer))
12334                  (string-match "Summary" buffer)
12335                  (with-current-buffer buffer
12336                    ;; We check that this is, indeed, a summary buffer.
12337                    (and (eq major-mode 'gnus-summary-mode)
12338                         ;; Also make sure this isn't bogus.
12339                         gnus-newsgroup-prepared
12340                         ;; Also make sure that this isn't a
12341                         ;; dead summary buffer.
12342                         (not gnus-dead-summary-mode))))
12343         (push buffer buffers)))
12344     ;; Go through all these summary buffers and offer to save them.
12345     (when buffers
12346       (save-excursion
12347         (map-y-or-n-p
12348          "Update summary buffer %s? "
12349          (lambda (buf)
12350            (switch-to-buffer buf)
12351            (gnus-summary-exit))
12352          buffers)))))
12353
12354 (defun gnus-summary-setup-default-charset ()
12355   "Setup newsgroup default charset."
12356   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12357       (setq gnus-newsgroup-charset nil)
12358     (let* ((ignored-charsets
12359             (or gnus-newsgroup-ephemeral-ignored-charsets
12360                 (append
12361                  (and gnus-newsgroup-name
12362                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12363                  gnus-newsgroup-ignored-charsets))))
12364       (setq gnus-newsgroup-charset
12365             (or gnus-newsgroup-ephemeral-charset
12366                 (and gnus-newsgroup-name
12367                      (gnus-parameter-charset gnus-newsgroup-name))
12368                 gnus-default-charset))
12369       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12370            ignored-charsets))))
12371
12372 ;;;
12373 ;;; Mime Commands
12374 ;;;
12375
12376 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12377   "Display the current article buffer fully MIME-buttonized.
12378 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12379 treated as multipart/mixed."
12380   (interactive "P")
12381   (require 'gnus-art)
12382   (let ((gnus-unbuttonized-mime-types nil)
12383         (gnus-mime-display-multipart-as-mixed show-all-parts))
12384     (gnus-summary-show-article)))
12385
12386 (defun gnus-summary-repair-multipart (article)
12387   "Add a Content-Type header to a multipart article without one."
12388   (interactive (list (gnus-summary-article-number)))
12389   (gnus-with-article article
12390     (message-narrow-to-head)
12391     (message-remove-header "Mime-Version")
12392     (goto-char (point-max))
12393     (insert "Mime-Version: 1.0\n")
12394     (widen)
12395     (when (search-forward "\n--" nil t)
12396       (let ((separator (buffer-substring (point) (point-at-eol))))
12397         (message-narrow-to-head)
12398         (message-remove-header "Content-Type")
12399         (goto-char (point-max))
12400         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12401                         separator))
12402         (widen))))
12403   (let (gnus-mark-article-hook)
12404     (gnus-summary-select-article t t nil article)))
12405
12406 (defun gnus-summary-toggle-display-buttonized ()
12407   "Toggle the buttonizing of the article buffer."
12408   (interactive)
12409   (require 'gnus-art)
12410   (if (setq gnus-inhibit-mime-unbuttonizing
12411             (not gnus-inhibit-mime-unbuttonizing))
12412       (let ((gnus-unbuttonized-mime-types nil))
12413         (gnus-summary-show-article))
12414     (gnus-summary-show-article)))
12415
12416 ;;;
12417 ;;; Generic summary marking commands
12418 ;;;
12419
12420 (defvar gnus-summary-marking-alist
12421   '((read gnus-del-mark "d")
12422     (unread gnus-unread-mark "u")
12423     (ticked gnus-ticked-mark "!")
12424     (dormant gnus-dormant-mark "?")
12425     (expirable gnus-expirable-mark "e"))
12426   "An alist of names/marks/keystrokes.")
12427
12428 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12429 (defvar gnus-summary-mark-map)
12430
12431 (defun gnus-summary-make-all-marking-commands ()
12432   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12433   (dolist (elem gnus-summary-marking-alist)
12434     (apply 'gnus-summary-make-marking-command elem)))
12435
12436 (defun gnus-summary-make-marking-command (name mark keystroke)
12437   (let ((map (make-sparse-keymap)))
12438     (define-key gnus-summary-generic-mark-map keystroke map)
12439     (dolist (lway `((next "next" next nil "n")
12440                     (next-unread "next unread" next t "N")
12441                     (prev "previous" prev nil "p")
12442                     (prev-unread "previous unread" prev t "P")
12443                     (nomove "" nil nil ,keystroke)))
12444       (let ((func (gnus-summary-make-marking-command-1
12445                    mark (car lway) lway name)))
12446         (setq func (eval func))
12447         (define-key map (nth 4 lway) func)))))
12448
12449 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12450   `(defun ,(intern
12451             (format "gnus-summary-put-mark-as-%s%s"
12452                     name (if (eq way 'nomove)
12453                              ""
12454                            (concat "-" (symbol-name way)))))
12455      (n)
12456      ,(format
12457        "Mark the current article as %s%s.
12458 If N, the prefix, then repeat N times.
12459 If N is negative, move in reverse order.
12460 The difference between N and the actual number of articles marked is
12461 returned."
12462        name (cadr lway))
12463      (interactive "p")
12464      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12465
12466 (defun gnus-summary-generic-mark (n mark move unread)
12467   "Mark N articles with MARK."
12468   (unless (eq major-mode 'gnus-summary-mode)
12469     (error "This command can only be used in the summary buffer"))
12470   (gnus-summary-show-thread)
12471   (let ((nummove
12472          (cond
12473           ((eq move 'next) 1)
12474           ((eq move 'prev) -1)
12475           (t 0))))
12476     (if (zerop nummove)
12477         (setq n 1)
12478       (when (< n 0)
12479         (setq n (abs n)
12480               nummove (* -1 nummove))))
12481     (while (and (> n 0)
12482                 (gnus-summary-mark-article nil mark)
12483                 (zerop (gnus-summary-next-subject nummove unread t)))
12484       (setq n (1- n)))
12485     (when (/= 0 n)
12486       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12487     (gnus-summary-recenter)
12488     (gnus-summary-position-point)
12489     (gnus-set-mode-line 'summary)
12490     n))
12491
12492 (defun gnus-summary-insert-articles (articles)
12493   (when (setq articles
12494               (gnus-sorted-difference articles
12495                                       (mapcar (lambda (h)
12496                                                 (mail-header-number h))
12497                                               gnus-newsgroup-headers)))
12498     (setq gnus-newsgroup-headers
12499           (gnus-merge 'list
12500                       gnus-newsgroup-headers
12501                       (gnus-fetch-headers articles)
12502                       'gnus-article-sort-by-number))
12503     ;; Suppress duplicates?
12504     (when gnus-suppress-duplicates
12505       (gnus-dup-suppress-articles))
12506
12507     (if (and gnus-fetch-old-headers
12508              (eq gnus-headers-retrieved-by 'nov))
12509         ;; We might want to build some more threads first.
12510         (if (eq gnus-fetch-old-headers 'invisible)
12511             (gnus-build-all-threads)
12512           (gnus-build-old-threads))
12513       ;; Mark the inserted articles that are unread as unread.
12514       (setq gnus-newsgroup-unreads
12515             (gnus-sorted-nunion
12516              gnus-newsgroup-unreads
12517              (gnus-sorted-nintersection
12518               (gnus-list-of-unread-articles gnus-newsgroup-name)
12519               articles)))
12520       ;; Mark the inserted articles as selected so that the information
12521       ;; of the marks having been changed by a user may be updated when
12522       ;; exiting this group.  See `gnus-summary-update-info'.
12523       (dolist (art articles)
12524         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12525     ;; Let the Gnus agent mark articles as read.
12526     (when gnus-agent
12527       (gnus-agent-get-undownloaded-list))
12528     ;; Remove list identifiers from subject
12529     (when gnus-list-identifiers
12530       (gnus-summary-remove-list-identifiers))
12531     ;; First and last article in this newsgroup.
12532     (when gnus-newsgroup-headers
12533       (setq gnus-newsgroup-begin
12534             (mail-header-number (car gnus-newsgroup-headers))
12535             gnus-newsgroup-end
12536             (mail-header-number
12537              (gnus-last-element gnus-newsgroup-headers))))
12538     (when gnus-use-scoring
12539       (gnus-possibly-score-headers))))
12540
12541 (defun gnus-summary-insert-old-articles (&optional all)
12542   "Insert all old articles in this group.
12543 If ALL is non-nil, already read articles become readable.
12544 If ALL is a number, fetch this number of articles."
12545   (interactive "P")
12546   (prog1
12547       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12548             older len)
12549         (setq older
12550               ;; Some nntp servers lie about their active range.  When
12551               ;; this happens, the active range can be in the millions.
12552               ;; Use a compressed range to avoid creating a huge list.
12553               (gnus-range-difference (list gnus-newsgroup-active) old))
12554         (setq len (gnus-range-length older))
12555         (cond
12556          ((null older) nil)
12557          ((numberp all)
12558           (if (< all len)
12559               (let ((older-range (nreverse older)))
12560                 (setq older nil)
12561
12562                 (while (> all 0)
12563                   (let* ((r (pop older-range))
12564                          (min (if (numberp r) r (car r)))
12565                          (max (if (numberp r) r (cdr r))))
12566                     (while (and (<= min max)
12567                                 (> all 0))
12568                       (push max older)
12569                       (setq all (1- all)
12570                             max (1- max))))))
12571             (setq older (gnus-uncompress-range older))))
12572          (all
12573           (setq older (gnus-uncompress-range older)))
12574          (t
12575           (when (and (numberp gnus-large-newsgroup)
12576                    (> len gnus-large-newsgroup))
12577               (let* ((cursor-in-echo-area nil)
12578                      (initial (gnus-parameter-large-newsgroup-initial
12579                                gnus-newsgroup-name))
12580                      (input
12581                       (read-string
12582                        (format
12583                         "How many articles from %s (%s %d): "
12584                         (gnus-group-decoded-name gnus-newsgroup-name)
12585                         (if initial "max" "default")
12586                         len)
12587                        (if initial
12588                            (cons (number-to-string initial)
12589                                  0)))))
12590                 (unless (string-match "^[ \t]*$" input)
12591                   (setq all (string-to-number input))
12592                   (if (< all len)
12593                       (let ((older-range (nreverse older)))
12594                         (setq older nil)
12595
12596                         (while (> all 0)
12597                           (let* ((r (pop older-range))
12598                                  (min (if (numberp r) r (car r)))
12599                                  (max (if (numberp r) r (cdr r))))
12600                             (while (and (<= min max)
12601                                         (> all 0))
12602                               (push max older)
12603                               (setq all (1- all)
12604                                     max (1- max))))))))))
12605           (setq older (gnus-uncompress-range older))))
12606         (if (not older)
12607             (message "No old news.")
12608           (gnus-summary-insert-articles older)
12609           (gnus-summary-limit (gnus-sorted-nunion old older))))
12610     (gnus-summary-position-point)))
12611
12612 (defun gnus-summary-insert-new-articles ()
12613   "Insert all new articles in this group."
12614   (interactive)
12615   (prog1
12616       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12617             (old-active gnus-newsgroup-active)
12618             (nnmail-fetched-sources (list t))
12619             i new)
12620         (setq gnus-newsgroup-active
12621               (gnus-activate-group gnus-newsgroup-name 'scan))
12622         (setq i (cdr gnus-newsgroup-active))
12623         (while (> i (cdr old-active))
12624           (push i new)
12625           (decf i))
12626         (if (not new)
12627             (message "No gnus is bad news")
12628           (gnus-summary-insert-articles new)
12629           (setq gnus-newsgroup-unreads
12630                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12631           (gnus-summary-limit (gnus-sorted-nunion old new))))
12632     (gnus-summary-position-point)))
12633
12634 ;;; Bookmark support for Gnus.
12635 (declare-function bookmark-make-record-default "bookmark" (&optional pos-only))
12636 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12637 (declare-function bookmark-default-handler "bookmark" (bmk))
12638 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12639
12640 (defun gnus-summary-bookmark-make-record ()
12641   "Make a bookmark entry for a Gnus summary buffer."
12642   (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12643     (error "Please retry from the Gnus summary buffer")) ;[1]
12644   (let* ((subject (elt (gnus-summary-article-header) 1))
12645          (grp     (car gnus-article-current))
12646          (art     (cdr gnus-article-current))
12647          (head    (gnus-summary-article-header art))
12648          (id      (mail-header-id head)))
12649     `(,subject
12650       ,@(bookmark-make-record-default 'point-only)
12651       (location . ,(format "Gnus %s:%d:%s" grp art id))
12652       (group . ,grp) (article . ,art)
12653       (message-id . ,id) (handler . gnus-summary-bookmark-jump))))
12654
12655 ;;;###autoload
12656 (defun gnus-summary-bookmark-jump (bookmark)
12657   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12658 BOOKMARK is a bookmark name or a bookmark record."
12659   (let ((group    (bookmark-prop-get bookmark 'group))
12660         (article  (bookmark-prop-get bookmark 'article))
12661         (id       (bookmark-prop-get bookmark 'message-id)))
12662     (gnus-fetch-group group (list article))
12663     (gnus-summary-insert-cached-articles)
12664     (gnus-summary-goto-article id nil 'force)
12665     (bookmark-default-handler
12666      `(""
12667        (buffer . ,(current-buffer))
12668        . ,(bookmark-get-bookmark-record bookmark)))))
12669
12670 (gnus-summary-make-all-marking-commands)
12671
12672 (gnus-ems-redefine)
12673
12674 (provide 'gnus-sum)
12675
12676 (run-hooks 'gnus-sum-load-hook)
12677
12678 ;; Local Variables:
12679 ;; coding: iso-8859-1
12680 ;; End:
12681
12682 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12683 ;;; gnus-sum.el ends here