Indent.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; For Emacs < 22.2.
29 (eval-and-compile
30   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31 (eval-when-compile
32   (require 'cl))
33 (eval-when-compile
34   (when (featurep 'xemacs)
35     (require 'easy-mmode))) ; for `define-minor-mode'
36
37 (defvar tool-bar-mode)
38 (defvar gnus-tmp-header)
39
40 (require 'gnus)
41 (require 'gnus-group)
42 (require 'gnus-spec)
43 (require 'gnus-range)
44 (require 'gnus-int)
45 (require 'gnus-undo)
46 (require 'gnus-util)
47 (require 'gmm-utils)
48 (require 'mm-decode)
49 (require 'nnoo)
50
51 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
52 (autoload 'gnus-cache-write-active "gnus-cache")
53 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
54 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
55 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
56 (autoload 'mm-uu-dissect "mm-uu")
57 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
58   "Deuglify broken Outlook (Express) articles and redisplay."
59   t)
60 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
61 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
62 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
63
64 (defcustom gnus-kill-summary-on-exit t
65   "*If non-nil, kill the summary buffer when you exit from it.
66 If nil, the summary will become a \"*Dead Summary*\" buffer, and
67 it will be killed sometime later."
68   :group 'gnus-summary-exit
69   :type 'boolean)
70
71 (defcustom gnus-summary-next-group-on-exit t
72   "If non-nil, go to the next unread newsgroup on summary exit.
73 See `gnus-group-goto-unread'."
74   :link '(custom-manual "(gnus)Group Maneuvering")
75   :group 'gnus-summary-exit
76   :version "23.1" ;; No Gnus
77   :type 'boolean)
78
79 (defcustom gnus-summary-stop-at-end-of-message nil
80   "If non-nil, don't select the next message when using `SPC'."
81   :link '(custom-manual "(gnus)Group Maneuvering")
82   :group 'gnus-summary-maneuvering
83   :version "24.1"
84   :type 'boolean)
85
86 (defcustom gnus-fetch-old-headers nil
87   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
88 If an unread article in the group refers to an older, already
89 read (or just marked as read) article, the old article will not
90 normally be displayed in the Summary buffer.  If this variable is
91 t, Gnus will attempt to grab the headers to the old articles, and
92 thereby build complete threads.  If it has the value `some', all
93 old headers will be fetched but only enough headers to connect
94 otherwise loose threads will be displayed.  This variable can
95 also be a number.  In that case, no more than that number of old
96 headers will be fetched.  If it has the value `invisible', all
97 old headers will be fetched, but none will be displayed.
98
99 The server has to support NOV for any of this to work.
100
101 This feature can seriously impact performance it ignores all
102 locally cached header entries.  Setting it to t for groups for a
103 server that doesn't expire articles (such as news.gmane.org),
104 leads to very slow summary generation."
105   :group 'gnus-thread
106   :type '(choice (const :tag "off" nil)
107                  (const :tag "on" t)
108                  (const some)
109                  (const invisible)
110                  number
111                  (sexp :menu-tag "other" t)))
112
113 (defcustom gnus-refer-thread-limit 500
114   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
115 If t, fetch all the available old headers."
116   :group 'gnus-thread
117   :type '(choice number
118                  (sexp :menu-tag "other" t)))
119
120 (defcustom gnus-summary-make-false-root 'adopt
121   "*nil means that Gnus won't gather loose threads.
122 If the root of a thread has expired or been read in a previous
123 session, the information necessary to build a complete thread has been
124 lost.  Instead of having many small sub-threads from this original thread
125 scattered all over the summary buffer, Gnus can gather them.
126
127 If non-nil, Gnus will try to gather all loose sub-threads from an
128 original thread into one large thread.
129
130 If this variable is non-nil, it should be one of `none', `adopt',
131 `dummy' or `empty'.
132
133 If this variable is `none', Gnus will not make a false root, but just
134 present the sub-threads after another.
135 If this variable is `dummy', Gnus will create a dummy root that will
136 have all the sub-threads as children.
137 If this variable is `adopt', Gnus will make one of the \"children\"
138 the parent and mark all the step-children as such.
139 If this variable is `empty', the \"children\" are printed with empty
140 subject fields.  (Or rather, they will be printed with a string
141 given by the `gnus-summary-same-subject' variable.)"
142   :group 'gnus-thread
143   :type '(choice (const :tag "off" nil)
144                  (const none)
145                  (const dummy)
146                  (const adopt)
147                  (const empty)))
148
149 (defcustom gnus-summary-make-false-root-always nil
150   "Always make a false dummy root."
151   :version "22.1"
152   :group 'gnus-thread
153   :type 'boolean)
154
155 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
156   "*A regexp to match subjects to be excluded from loose thread gathering.
157 As loose thread gathering is done on subjects only, that means that
158 there can be many false gatherings performed.  By rooting out certain
159 common subjects, gathering might become saner."
160   :group 'gnus-thread
161   :type 'regexp)
162
163 (defcustom gnus-summary-gather-subject-limit nil
164   "*Maximum length of subject comparisons when gathering loose threads.
165 Use nil to compare full subjects.  Setting this variable to a low
166 number will help gather threads that have been corrupted by
167 newsreaders chopping off subject lines, but it might also mean that
168 unrelated articles that have subject that happen to begin with the
169 same few characters will be incorrectly gathered.
170
171 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
172 comparing subjects."
173   :group 'gnus-thread
174   :type '(choice (const :tag "off" nil)
175                  (const fuzzy)
176                  (sexp :menu-tag "on" t)))
177
178 (defcustom gnus-simplify-subject-functions nil
179   "List of functions taking a string argument that simplify subjects.
180 The functions are applied recursively.
181
182 Useful functions to put in this list include:
183 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
184 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
185   :group 'gnus-thread
186   :type '(repeat function))
187
188 (defcustom gnus-simplify-ignored-prefixes nil
189   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
190   :group 'gnus-thread
191   :type '(choice (const :tag "off" nil)
192                  regexp))
193
194 (defcustom gnus-build-sparse-threads nil
195   "*If non-nil, fill in the gaps in threads.
196 If `some', only fill in the gaps that are needed to tie loose threads
197 together.  If `more', fill in all leaf nodes that Gnus can find.  If
198 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
199   :group 'gnus-thread
200   :type '(choice (const :tag "off" nil)
201                  (const some)
202                  (const more)
203                  (sexp :menu-tag "all" t)))
204
205 (defcustom gnus-summary-thread-gathering-function
206   'gnus-gather-threads-by-subject
207   "*Function used for gathering loose threads.
208 There are two pre-defined functions: `gnus-gather-threads-by-subject',
209 which only takes Subjects into consideration; and
210 `gnus-gather-threads-by-references', which compared the References
211 headers of the articles to find matches."
212   :group 'gnus-thread
213   :type '(radio (function-item gnus-gather-threads-by-subject)
214                 (function-item gnus-gather-threads-by-references)
215                 (function :tag "other")))
216
217 (defcustom gnus-summary-same-subject ""
218   "*String indicating that the current article has the same subject as the previous.
219 This variable will only be used if the value of
220 `gnus-summary-make-false-root' is `empty'."
221   :group 'gnus-summary-format
222   :type 'string)
223
224 (defcustom gnus-summary-goto-unread nil
225   "*If t, many commands will go to the next unread article.
226 This applies to marking commands as well as other commands that
227 \"naturally\" select the next article, like, for instance, `SPC' at
228 the end of an article.
229
230 If nil, the marking commands do NOT go to the next unread article
231 \(they go to the next article instead).  If `never', commands that
232 usually go to the next unread article, will go to the next article,
233 whether it is read or not."
234   :version "24.1"
235   :group 'gnus-summary-marks
236   :link '(custom-manual "(gnus)Setting Marks")
237   :type '(choice (const :tag "off" nil)
238                  (const never)
239                  (sexp :menu-tag "on" t)))
240
241 (defcustom gnus-summary-default-score 0
242   "*Default article score level.
243 All scores generated by the score files will be added to this score.
244 If this variable is nil, scoring will be disabled."
245   :group 'gnus-score-default
246   :type '(choice (const :tag "disable")
247                  integer))
248
249 (defcustom gnus-summary-default-high-score 0
250   "*Default threshold for a high scored article.
251 An article will be highlighted as high scored if its score is greater
252 than this score."
253   :version "22.1"
254   :group 'gnus-score-default
255   :type 'integer)
256
257 (defcustom gnus-summary-default-low-score 0
258   "*Default threshold for a low scored article.
259 An article will be highlighted as low scored if its score is smaller
260 than this score."
261   :version "22.1"
262   :group 'gnus-score-default
263   :type 'integer)
264
265 (defcustom gnus-summary-zcore-fuzz 0
266   "*Fuzziness factor for the zcore in the summary buffer.
267 Articles with scores closer than this to `gnus-summary-default-score'
268 will not be marked."
269   :group 'gnus-summary-format
270   :type 'integer)
271
272 (defcustom gnus-simplify-subject-fuzzy-regexp nil
273   "*Strings to be removed when doing fuzzy matches.
274 This can either be a regular expression or list of regular expressions
275 that will be removed from subject strings if fuzzy subject
276 simplification is selected."
277   :group 'gnus-thread
278   :type '(repeat regexp))
279
280 (defcustom gnus-show-threads t
281   "*If non-nil, display threads in summary mode."
282   :group 'gnus-thread
283   :type 'boolean)
284
285 (defcustom gnus-thread-hide-subtree nil
286   "*If non-nil, hide all threads initially.
287 This can be a predicate specifier which says which threads to hide.
288 If threads are hidden, you have to run the command
289 `gnus-summary-show-thread' by hand or select an article."
290   :group 'gnus-thread
291   :type '(radio (sexp :format "Non-nil\n"
292                       :match (lambda (widget value)
293                                (not (or (consp value) (functionp value))))
294                       :value t)
295                 (const nil)
296                 (sexp :tag "Predicate specifier")))
297
298 (defcustom gnus-thread-hide-killed t
299   "*If non-nil, hide killed threads automatically."
300   :group 'gnus-thread
301   :type 'boolean)
302
303 (defcustom gnus-thread-ignore-subject t
304   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
305 If nil, articles that have different subjects from their parents will
306 start separate threads."
307   :group 'gnus-thread
308   :type 'boolean)
309
310 (defcustom gnus-thread-operation-ignore-subject t
311   "*If non-nil, subjects will be ignored when doing thread commands.
312 This affects commands like `gnus-summary-kill-thread' and
313 `gnus-summary-lower-thread'.
314
315 If this variable is nil, articles in the same thread with different
316 subjects will not be included in the operation in question.  If this
317 variable is `fuzzy', only articles that have subjects that are fuzzily
318 equal will be included."
319   :group 'gnus-thread
320   :type '(choice (const :tag "off" nil)
321                  (const fuzzy)
322                  (sexp :tag "on" t)))
323
324 (defcustom gnus-thread-indent-level 4
325   "*Number that says how much each sub-thread should be indented."
326   :group 'gnus-thread
327   :type 'integer)
328
329 (defcustom gnus-auto-extend-newsgroup t
330   "*If non-nil, extend newsgroup forward and backward when requested."
331   :group 'gnus-summary-choose
332   :type 'boolean)
333
334 (defcustom gnus-auto-select-first t
335   "If non-nil, select an article on group entry.
336 An article is selected automatically when entering a group
337 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
338 `gnus-summary-catchup-and-goto-next-group'.
339
340 Which article is selected is controlled by the variable
341 `gnus-auto-select-subject'.
342
343 If you want to prevent automatic selection of articles in some
344 newsgroups, set the variable to nil in `gnus-select-group-hook'."
345   ;; Commands include...
346   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
347   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
348   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
349   :group 'gnus-group-select
350   :type '(choice (const :tag "none" nil)
351                  (sexp :menu-tag "first" t)))
352
353 (defcustom gnus-auto-select-subject 'unseen-or-unread
354   "*Says what subject to place under point when entering a group.
355
356 This variable can either be the symbols `first' (place point on the
357 first subject), `unread' (place point on the subject line of the first
358 unread article), `best' (place point on the subject line of the
359 higest-scored article), `unseen' (place point on the subject line of
360 the first unseen article), `unseen-or-unread' (place point on the subject
361 line of the first unseen article or, if all article have been seen, on the
362 subject line of the first unread article), or a function to be called to
363 place point on some subject line."
364   :version "24.1"
365   :group 'gnus-group-select
366   :type '(choice (const best)
367                  (const unread)
368                  (const first)
369                  (const unseen)
370                  (const unseen-or-unread)))
371
372 (defcustom gnus-auto-select-next t
373   "*If non-nil, offer to go to the next group from the end of the previous.
374 If the value is t and the next newsgroup is empty, Gnus will exit
375 summary mode and go back to group mode.  If the value is neither nil
376 nor t, Gnus will select the following unread newsgroup.  In
377 particular, if the value is the symbol `quietly', the next unread
378 newsgroup will be selected without any confirmation, and if it is
379 `almost-quietly', the next group will be selected without any
380 confirmation if you are located on the last article in the group.
381 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
382 will go to the next group without confirmation."
383   :group 'gnus-summary-maneuvering
384   :type '(choice (const :tag "off" nil)
385                  (const quietly)
386                  (const almost-quietly)
387                  (const slightly-quietly)
388                  (sexp :menu-tag "on" t)))
389
390 (defcustom gnus-auto-select-same nil
391   "*If non-nil, select the next article with the same subject.
392 If there are no more articles with the same subject, go to
393 the first unread article."
394   :group 'gnus-summary-maneuvering
395   :type 'boolean)
396
397 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
398   "What article should be selected after exiting an ephemeral group.
399 Valid values include:
400
401 `next'
402   Select the next article.
403 `next-unread'
404   Select the next unread article.
405 `next-noselect'
406   Move the cursor to the next article.  This is the default.
407 `next-unread-noselect'
408   Move the cursor to the next unread article.
409
410 If it has any other value or there is no next (unread) article, the
411 article selected before entering to the ephemeral group will appear."
412   :version "23.1" ;; No Gnus
413   :group 'gnus-summary-maneuvering
414   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
415                  (const next) (const next-unread)
416                  (const next-noselect) (const next-unread-noselect)
417                  (sexp :tag "other" :value nil)))
418
419 (defcustom gnus-auto-goto-ignores 'unfetched
420   "*Says how to handle unfetched articles when maneuvering.
421
422 This variable can either be the symbols nil (maneuver to any
423 article), `undownloaded' (maneuvering while unplugged ignores articles
424 that have not been fetched), `always-undownloaded' (maneuvering always
425 ignores articles that have not been fetched), `unfetched' (maneuvering
426 ignores articles whose headers have not been fetched).
427
428 NOTE: The list of unfetched articles will always be nil when plugged
429 and, when unplugged, a subset of the undownloaded article list."
430   :version "22.1"
431   :group 'gnus-summary-maneuvering
432   :type '(choice (const :tag "None" nil)
433                  (const :tag "Undownloaded when unplugged" undownloaded)
434                  (const :tag "Undownloaded" always-undownloaded)
435                  (const :tag "Unfetched" unfetched)))
436
437 (defcustom gnus-summary-check-current nil
438   "*If non-nil, consider the current article when moving.
439 The \"unread\" movement commands will stay on the same line if the
440 current article is unread."
441   :group 'gnus-summary-maneuvering
442   :type 'boolean)
443
444 (defcustom gnus-auto-center-summary 2
445   "*If non-nil, always center the current summary buffer.
446 In particular, if `vertical' do only vertical recentering.  If non-nil
447 and non-`vertical', do both horizontal and vertical recentering."
448   :group 'gnus-summary-maneuvering
449   :type '(choice (const :tag "none" nil)
450                  (const vertical)
451                  (integer :tag "height")
452                  (sexp :menu-tag "both" t)))
453
454 (defvar gnus-auto-center-group t
455   "*If non-nil, always center the group buffer.")
456
457 (defcustom gnus-show-all-headers nil
458   "*If non-nil, don't hide any headers."
459   :group 'gnus-article-hiding
460   :group 'gnus-article-headers
461   :type 'boolean)
462
463 (defcustom gnus-summary-ignore-duplicates nil
464   "*If non-nil, ignore articles with identical Message-ID headers."
465   :group 'gnus-summary
466   :type 'boolean)
467
468 (defcustom gnus-single-article-buffer nil
469   "*If non-nil, display all articles in the same buffer.
470 If nil, each group will get its own article buffer."
471   :version "24.1"
472   :group 'gnus-article-various
473   :type 'boolean)
474
475 (defcustom gnus-break-pages t
476   "*If non-nil, do page breaking on articles.
477 The page delimiter is specified by the `gnus-page-delimiter'
478 variable."
479   :group 'gnus-article-various
480   :type 'boolean)
481
482 (defcustom gnus-move-split-methods nil
483   "*Variable used to suggest where articles are to be moved to.
484 It uses the same syntax as the `gnus-split-methods' variable.
485 However, whereas `gnus-split-methods' specifies file names as targets,
486 this variable specifies group names."
487   :group 'gnus-summary-mail
488   :type '(repeat (choice (list :value (fun) function)
489                          (cons :value ("" "") regexp (repeat string))
490                          (sexp :value nil))))
491
492 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
493   "Function used to compute default prefix for article move/copy/etc prompts.
494 The function should take one argument, a group name, and return a
495 string with the suggested prefix."
496   :group 'gnus-summary-mail
497   :type 'function)
498
499 ;; FIXME: Although the custom type is `character' for the following variables,
500 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
501
502 (defcustom gnus-unread-mark ?           ;Whitespace
503   "*Mark used for unread articles."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-ticked-mark ?!
508   "*Mark used for ticked articles."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-dormant-mark ??
513   "*Mark used for dormant articles."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-del-mark ?r
518   "*Mark used for del'd articles."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-read-mark ?R
523   "*Mark used for read articles."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-expirable-mark ?E
528   "*Mark used for expirable articles."
529   :group 'gnus-summary-marks
530   :type 'character)
531
532 (defcustom gnus-killed-mark ?K
533   "*Mark used for killed articles."
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-spam-mark ?$
538   "*Mark used for spam articles."
539   :version "22.1"
540   :group 'gnus-summary-marks
541   :type 'character)
542
543 (defcustom gnus-kill-file-mark ?X
544   "*Mark used for articles killed by kill files."
545   :group 'gnus-summary-marks
546   :type 'character)
547
548 (defcustom gnus-low-score-mark ?Y
549   "*Mark used for articles with a low score."
550   :group 'gnus-summary-marks
551   :type 'character)
552
553 (defcustom gnus-catchup-mark ?C
554   "*Mark used for articles that are caught up."
555   :group 'gnus-summary-marks
556   :type 'character)
557
558 (defcustom gnus-replied-mark ?A
559   "*Mark used for articles that have been replied to."
560   :group 'gnus-summary-marks
561   :type 'character)
562
563 (defcustom gnus-forwarded-mark ?F
564   "*Mark used for articles that have been forwarded."
565   :version "22.1"
566   :group 'gnus-summary-marks
567   :type 'character)
568
569 (defcustom gnus-recent-mark ?N
570   "*Mark used for articles that are recent."
571   :version "22.1"
572   :group 'gnus-summary-marks
573   :type 'character)
574
575 (defcustom gnus-cached-mark ?*
576   "*Mark used for articles that are in the cache."
577   :group 'gnus-summary-marks
578   :type 'character)
579
580 (defcustom gnus-saved-mark ?S
581   "*Mark used for articles that have been saved."
582   :group 'gnus-summary-marks
583   :type 'character)
584
585 (defcustom gnus-unseen-mark ?.
586   "*Mark used for articles that haven't been seen."
587   :version "22.1"
588   :group 'gnus-summary-marks
589   :type 'character)
590
591 (defcustom gnus-no-mark ?               ;Whitespace
592   "*Mark used for articles that have no other secondary mark."
593   :version "22.1"
594   :group 'gnus-summary-marks
595   :type 'character)
596
597 (defcustom gnus-ancient-mark ?O
598   "*Mark used for ancient articles."
599   :group 'gnus-summary-marks
600   :type 'character)
601
602 (defcustom gnus-sparse-mark ?Q
603   "*Mark used for sparsely reffed articles."
604   :group 'gnus-summary-marks
605   :type 'character)
606
607 (defcustom gnus-canceled-mark ?G
608   "*Mark used for canceled articles."
609   :group 'gnus-summary-marks
610   :type 'character)
611
612 (defcustom gnus-duplicate-mark ?M
613   "*Mark used for duplicate articles."
614   :group 'gnus-summary-marks
615   :type 'character)
616
617 (defcustom gnus-undownloaded-mark ?-
618   "*Mark used for articles that weren't downloaded."
619   :version "22.1"
620   :group 'gnus-summary-marks
621   :type 'character)
622
623 (defcustom gnus-downloaded-mark ?+
624   "*Mark used for articles that were downloaded."
625   :group 'gnus-summary-marks
626   :type 'character)
627
628 (defcustom gnus-downloadable-mark ?%
629   "*Mark used for articles that are to be downloaded."
630   :group 'gnus-summary-marks
631   :type 'character)
632
633 (defcustom gnus-unsendable-mark ?=
634   "*Mark used for articles that won't be sent."
635   :group 'gnus-summary-marks
636   :type 'character)
637
638 (defcustom gnus-score-over-mark ?+
639   "*Score mark used for articles with high scores."
640   :group 'gnus-summary-marks
641   :type 'character)
642
643 (defcustom gnus-score-below-mark ?-
644   "*Score mark used for articles with low scores."
645   :group 'gnus-summary-marks
646   :type 'character)
647
648 (defcustom gnus-empty-thread-mark ?     ;Whitespace
649   "*There is no thread under the article."
650   :group 'gnus-summary-marks
651   :type 'character)
652
653 (defcustom gnus-not-empty-thread-mark ?=
654   "*There is a thread under the article."
655   :group 'gnus-summary-marks
656   :type 'character)
657
658 (defcustom gnus-view-pseudo-asynchronously nil
659   "*If non-nil, Gnus will view pseudo-articles asynchronously."
660   :group 'gnus-extract-view
661   :type 'boolean)
662
663 (defcustom gnus-auto-expirable-marks
664   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
665         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
666         gnus-duplicate-mark)
667   "*The list of marks converted into expiration if a group is auto-expirable."
668   :version "24.1"
669   :group 'gnus-summary
670   :type '(repeat character))
671
672 (defcustom gnus-inhibit-user-auto-expire t
673   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
674   :version "21.1"
675   :group 'gnus-summary
676   :type 'boolean)
677
678 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
679   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
680 If nil, the expirable marks will be unchanged except that the marks
681 will be removed when copying or moving articles to a group that has
682 not turned auto-expire on.  If non-nil, articles that have been read
683 will be marked as expirable when being copied or moved to a group in
684 which auto-expire is turned on."
685   :version "23.2"
686   :type 'boolean
687   :group 'gnus-summary-marks)
688
689 (defcustom gnus-view-pseudos nil
690   "*If `automatic', pseudo-articles will be viewed automatically.
691 If `not-confirm', pseudos will be viewed automatically, and the user
692 will not be asked to confirm the command."
693   :group 'gnus-extract-view
694   :type '(choice (const :tag "off" nil)
695                  (const automatic)
696                  (const not-confirm)))
697
698 (defcustom gnus-view-pseudos-separately t
699   "*If non-nil, one pseudo-article will be created for each file to be viewed.
700 If nil, all files that use the same viewing command will be given as a
701 list of parameters to that command."
702   :group 'gnus-extract-view
703   :type 'boolean)
704
705 (defcustom gnus-insert-pseudo-articles t
706   "*If non-nil, insert pseudo-articles when decoding articles."
707   :group 'gnus-extract-view
708   :type 'boolean)
709
710 (defcustom gnus-summary-dummy-line-format
711   "   %(:                             :%) %S\n"
712   "*The format specification for the dummy roots in the summary buffer.
713 It works along the same lines as a normal formatting string,
714 with some simple extensions.
715
716 %S  The subject
717
718 General format specifiers can also be used.
719 See `(gnus)Formatting Variables'."
720   :link '(custom-manual "(gnus)Formatting Variables")
721   :group 'gnus-threading
722   :type 'string)
723
724 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
725   "*The format specification for the summary mode line.
726 It works along the same lines as a normal formatting string,
727 with some simple extensions:
728
729 %G  Group name
730 %p  Unprefixed group name
731 %A  Current article number
732 %z  Current article score
733 %V  Gnus version
734 %U  Number of unread articles in the group
735 %e  Number of unselected articles in the group
736 %Z  A string with unread/unselected article counts
737 %g  Shortish group name
738 %S  Subject of the current article
739 %u  User-defined spec
740 %s  Current score file name
741 %d  Number of dormant articles
742 %r  Number of articles that have been marked as read in this session
743 %E  Number of articles expunged by the score files"
744   :group 'gnus-summary-format
745   :type 'string)
746
747 (defcustom gnus-list-identifiers nil
748   "Regexp that matches list identifiers to be removed from subject.
749 This can also be a list of regexps."
750   :version "21.1"
751   :group 'gnus-summary-format
752   :group 'gnus-article-hiding
753   :type '(choice (const :tag "none" nil)
754                  (regexp :value ".*")
755                  (repeat :value (".*") regexp)))
756
757 (defcustom gnus-summary-mark-below 0
758   "*Mark all articles with a score below this variable as read.
759 This variable is local to each summary buffer and usually set by the
760 score file."
761   :group 'gnus-score-default
762   :type 'integer)
763
764 (defun gnus-widget-reversible-match (widget value)
765   "Ignoring WIDGET, convert VALUE to internal form.
766 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
767   ;; (debug value)
768   (or (symbolp value)
769       (and (listp value)
770            (eq (length value) 2)
771            (eq (nth 0 value) 'not)
772            (symbolp (nth 1 value)))))
773
774 (defun gnus-widget-reversible-to-internal (widget value)
775   "Ignoring WIDGET, convert VALUE to internal form.
776 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
777 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
778   ;; (debug value)
779   (if (atom value)
780       (list value nil)
781     (list (nth 1 value) t)))
782
783 (defun gnus-widget-reversible-to-external (widget value)
784   "Ignoring WIDGET, convert VALUE to external form.
785 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
786 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
787   ;; (debug value)
788   (if (nth 1 value)
789       (list 'not (nth 0 value))
790     (nth 0 value)))
791
792 (define-widget 'gnus-widget-reversible 'group
793   "A `group' that convert values."
794   :match 'gnus-widget-reversible-match
795   :value-to-internal 'gnus-widget-reversible-to-internal
796   :value-to-external 'gnus-widget-reversible-to-external)
797
798 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
799   "*List of functions used for sorting articles in the summary buffer.
800
801 Each function takes two articles and returns non-nil if the first
802 article should be sorted before the other.  If you use more than one
803 function, the primary sort function should be the last.  You should
804 probably always include `gnus-article-sort-by-number' in the list of
805 sorting functions -- preferably first.  Also note that sorting by date
806 is often much slower than sorting by number, and the sorting order is
807 very similar.  (Sorting by date means sorting by the time the message
808 was sent, sorting by number means sorting by arrival time.)
809
810 Each item can also be a list `(not F)' where F is a function;
811 this reverses the sort order.
812
813 Ready-made functions include `gnus-article-sort-by-number',
814 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
815 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
816 and `gnus-article-sort-by-score'.
817
818 When threading is turned on, the variable `gnus-thread-sort-functions'
819 controls how articles are sorted."
820   :group 'gnus-summary-sort
821   :type '(repeat (gnus-widget-reversible
822                   (choice (function-item gnus-article-sort-by-number)
823                           (function-item gnus-article-sort-by-author)
824                           (function-item gnus-article-sort-by-subject)
825                           (function-item gnus-article-sort-by-date)
826                           (function-item gnus-article-sort-by-score)
827                           (function-item gnus-article-sort-by-random)
828                           (function :tag "other"))
829                   (boolean :tag "Reverse order"))))
830
831
832 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
833   "*List of functions used for sorting threads in the summary buffer.
834 By default, threads are sorted by article number.
835
836 Each function takes two threads and returns non-nil if the first
837 thread should be sorted before the other.  If you use more than one
838 function, the primary sort function should be the last.  You should
839 probably always include `gnus-thread-sort-by-number' in the list of
840 sorting functions -- preferably first.  Also note that sorting by date
841 is often much slower than sorting by number, and the sorting order is
842 very similar.  (Sorting by date means sorting by the time the message
843 was sent, sorting by number means sorting by arrival time.)
844
845 Each list item can also be a list `(not F)' where F is a
846 function; this specifies reversed sort order.
847
848 Ready-made functions include `gnus-thread-sort-by-number',
849 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
850 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
851 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
852 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
853 and `gnus-thread-sort-by-total-score' (see
854 `gnus-thread-score-function').
855
856 When threading is turned off, the variable
857 `gnus-article-sort-functions' controls how articles are sorted."
858   :group 'gnus-summary-sort
859   :type '(repeat
860           (gnus-widget-reversible
861            (choice (function-item gnus-thread-sort-by-number)
862                    (function-item gnus-thread-sort-by-author)
863                    (function-item gnus-thread-sort-by-recipient)
864                    (function-item gnus-thread-sort-by-subject)
865                    (function-item gnus-thread-sort-by-date)
866                    (function-item gnus-thread-sort-by-score)
867                    (function-item gnus-thread-sort-by-most-recent-number)
868                    (function-item gnus-thread-sort-by-most-recent-date)
869                    (function-item gnus-thread-sort-by-random)
870                    (function-item gnus-thread-sort-by-total-score)
871                    (function :tag "other"))
872            (boolean :tag "Reverse order"))))
873
874 (defcustom gnus-thread-score-function '+
875   "*Function used for calculating the total score of a thread.
876
877 The function is called with the scores of the article and each
878 subthread and should then return the score of the thread.
879
880 Some functions you can use are `+', `max', or `min'."
881   :group 'gnus-summary-sort
882   :type 'function)
883
884 (defcustom gnus-summary-expunge-below nil
885   "All articles that have a score less than this variable will be expunged.
886 This variable is local to the summary buffers."
887   :group 'gnus-score-default
888   :type '(choice (const :tag "off" nil)
889                  integer))
890
891 (defcustom gnus-thread-expunge-below nil
892   "All threads that have a total score less than this variable will be expunged.
893 See `gnus-thread-score-function' for en explanation of what a
894 \"thread score\" is.
895
896 This variable is local to the summary buffers."
897   :group 'gnus-threading
898   :group 'gnus-score-default
899   :type '(choice (const :tag "off" nil)
900                  integer))
901
902 (defcustom gnus-summary-mode-hook nil
903   "*A hook for Gnus summary mode.
904 This hook is run before any variables are set in the summary buffer."
905   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
906   :group 'gnus-summary-various
907   :type 'hook)
908
909 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
910 (when (featurep 'xemacs)
911   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
912   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
913   (add-hook 'gnus-summary-mode-hook
914             'gnus-xmas-switch-horizontal-scrollbar-off))
915
916 (defcustom gnus-summary-menu-hook nil
917   "*Hook run after the creation of the summary mode menu."
918   :group 'gnus-summary-visual
919   :type 'hook)
920
921 (defcustom gnus-summary-exit-hook nil
922   "*A hook called on exit from the summary buffer.
923 It will be called with point in the group buffer."
924   :group 'gnus-summary-exit
925   :type 'hook)
926
927 (defcustom gnus-summary-prepare-hook nil
928   "*A hook called after the summary buffer has been generated.
929 If you want to modify the summary buffer, you can use this hook."
930   :group 'gnus-summary-various
931   :type 'hook)
932
933 (defcustom gnus-summary-prepared-hook nil
934   "*A hook called as the last thing after the summary buffer has been generated."
935   :group 'gnus-summary-various
936   :type 'hook)
937
938 (defcustom gnus-summary-generate-hook nil
939   "*A hook run just before generating the summary buffer.
940 This hook is commonly used to customize threading variables and the
941 like."
942   :group 'gnus-summary-various
943   :type 'hook)
944
945 (defcustom gnus-select-group-hook nil
946   "*A hook called when a newsgroup is selected.
947
948 If you'd like to simplify subjects like the
949 `gnus-summary-next-same-subject' command does, you can use the
950 following hook:
951
952  (add-hook gnus-select-group-hook
953            (lambda ()
954              (mapcar (lambda (header)
955                        (mail-header-set-subject
956                         header
957                         (gnus-simplify-subject
958                          (mail-header-subject header) 're-only)))
959                      gnus-newsgroup-headers)))"
960   :group 'gnus-group-select
961   :type 'hook)
962
963 (defcustom gnus-select-article-hook nil
964   "*A hook called when an article is selected."
965   :group 'gnus-summary-choose
966   :options '(gnus-agent-fetch-selected-article)
967   :type 'hook)
968
969 (defcustom gnus-visual-mark-article-hook
970   (list 'gnus-highlight-selected-summary)
971   "*Hook run after selecting an article in the summary buffer.
972 It is meant to be used for highlighting the article in some way.  It
973 is not run if `gnus-visual' is nil."
974   :group 'gnus-summary-visual
975   :type 'hook)
976
977 (defcustom gnus-parse-headers-hook nil
978   "*A hook called before parsing the headers."
979   :group 'gnus-various
980   :type 'hook)
981
982 (defcustom gnus-exit-group-hook nil
983   "*A hook called when exiting summary mode.
984 This hook is not called from the non-updating exit commands like `Q'."
985   :group 'gnus-various
986   :type 'hook)
987
988 (defcustom gnus-summary-update-hook nil
989   "*A hook called when a summary line is changed.
990 The hook will not be called if `gnus-visual' is nil.
991
992 The default function `gnus-summary-highlight-line' will
993 highlight the line according to the `gnus-summary-highlight'
994 variable."
995   :group 'gnus-summary-visual
996   :type 'hook)
997
998 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
999   "*A hook called when an article is selected for the first time.
1000 The hook is intended to mark an article as read (or unread)
1001 automatically when it is selected."
1002   :group 'gnus-summary-choose
1003   :type 'hook)
1004
1005 (defcustom gnus-group-no-more-groups-hook nil
1006   "*A hook run when returning to group mode having no more (unread) groups."
1007   :group 'gnus-group-select
1008   :type 'hook)
1009
1010 (defcustom gnus-ps-print-hook nil
1011   "*A hook run before ps-printing something from Gnus."
1012   :group 'gnus-summary
1013   :type 'hook)
1014
1015 (defcustom gnus-summary-article-move-hook nil
1016   "*A hook called after an article is moved, copied, respooled, or crossposted."
1017   :version "22.1"
1018   :group 'gnus-summary
1019   :type 'hook)
1020
1021 (defcustom gnus-summary-article-delete-hook nil
1022   "*A hook called after an article is deleted."
1023   :version "22.1"
1024   :group 'gnus-summary
1025   :type 'hook)
1026
1027 (defcustom gnus-summary-article-expire-hook nil
1028   "*A hook called after an article is expired."
1029   :version "22.1"
1030   :group 'gnus-summary
1031   :type 'hook)
1032
1033 (defcustom gnus-summary-display-arrow
1034   (and (fboundp 'display-graphic-p)
1035        (display-graphic-p))
1036   "*If non-nil, display an arrow highlighting the current article."
1037   :version "22.1"
1038   :group 'gnus-summary
1039   :type 'boolean)
1040
1041 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1042   "Face used for highlighting the current article in the summary buffer."
1043   :group 'gnus-summary-visual
1044   :type 'face)
1045
1046 (defvar gnus-tmp-downloaded nil)
1047
1048 (defcustom gnus-summary-highlight
1049   '(((eq mark gnus-canceled-mark)
1050      . gnus-summary-cancelled)
1051     ((and uncached (> score default-high))
1052      . gnus-summary-high-undownloaded)
1053     ((and uncached (< score default-low))
1054      . gnus-summary-low-undownloaded)
1055     (uncached
1056      . gnus-summary-normal-undownloaded)
1057     ((and (> score default-high)
1058           (or (eq mark gnus-dormant-mark)
1059               (eq mark gnus-ticked-mark)))
1060      . gnus-summary-high-ticked)
1061     ((and (< score default-low)
1062           (or (eq mark gnus-dormant-mark)
1063               (eq mark gnus-ticked-mark)))
1064      . gnus-summary-low-ticked)
1065     ((or (eq mark gnus-dormant-mark)
1066          (eq mark gnus-ticked-mark))
1067      . gnus-summary-normal-ticked)
1068     ((and (> score default-high) (eq mark gnus-ancient-mark))
1069      . gnus-summary-high-ancient)
1070     ((and (< score default-low) (eq mark gnus-ancient-mark))
1071      . gnus-summary-low-ancient)
1072     ((eq mark gnus-ancient-mark)
1073      . gnus-summary-normal-ancient)
1074     ((and (> score default-high) (eq mark gnus-unread-mark))
1075      . gnus-summary-high-unread)
1076     ((and (< score default-low) (eq mark gnus-unread-mark))
1077      . gnus-summary-low-unread)
1078     ((eq mark gnus-unread-mark)
1079      . gnus-summary-normal-unread)
1080     ((> score default-high)
1081      . gnus-summary-high-read)
1082     ((< score default-low)
1083      . gnus-summary-low-read)
1084     (t
1085      . gnus-summary-normal-read))
1086   "*Controls the highlighting of summary buffer lines.
1087
1088 A list of (FORM . FACE) pairs.  When deciding how a particular
1089 summary line should be displayed, each form is evaluated.  The content
1090 of the face field after the first true form is used.  You can change
1091 how those summary lines are displayed, by editing the face field.
1092
1093 You can use the following variables in the FORM field.
1094
1095 score:        The article's score.
1096 default:      The default article score.
1097 default-high: The default score for high scored articles.
1098 default-low:  The default score for low scored articles.
1099 below:        The score below which articles are automatically marked as read.
1100 mark:         The article's mark.
1101 uncached:     Non-nil if the article is uncached."
1102   :group 'gnus-summary-visual
1103   :type '(repeat (cons (sexp :tag "Form" nil)
1104                        face)))
1105 (put 'gnus-summary-highlight 'risky-local-variable t)
1106
1107 (defcustom gnus-alter-header-function nil
1108   "Function called to allow alteration of article header structures.
1109 The function is called with one parameter, the article header vector,
1110 which it may alter in any way."
1111   :type '(choice (const :tag "None" nil)
1112                  function)
1113   :group 'gnus-summary)
1114
1115 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1116   "Function used to decode a string with encoded words.")
1117
1118 (defvar gnus-decode-encoded-address-function
1119   'mail-decode-encoded-address-string
1120   "Function used to decode addresses with encoded words.")
1121
1122 (defcustom gnus-extra-headers '(To Newsgroups)
1123   "*Extra headers to parse."
1124   :version "21.1"
1125   :group 'gnus-summary
1126   :type '(repeat symbol))
1127
1128 (defcustom gnus-ignored-from-addresses
1129   (and user-mail-address
1130        (not (string= user-mail-address ""))
1131        (regexp-quote user-mail-address))
1132   "*From headers that may be suppressed in favor of To headers.
1133 This can be a regexp or a list of regexps."
1134   :version "21.1"
1135   :group 'gnus-summary
1136   :type '(choice regexp
1137                  (repeat :tag "Regexp List" regexp)))
1138
1139 (defsubst gnus-ignored-from-addresses ()
1140   (gmm-regexp-concat gnus-ignored-from-addresses))
1141
1142 (defcustom gnus-summary-to-prefix "-> "
1143   "*String prefixed to the To field in the summary line when
1144 using `gnus-ignored-from-addresses'."
1145   :version "22.1"
1146   :group 'gnus-summary
1147   :type 'string)
1148
1149 (defcustom gnus-summary-newsgroup-prefix "=> "
1150   "*String prefixed to the Newsgroup field in the summary
1151 line when using `gnus-ignored-from-addresses'."
1152   :version "22.1"
1153   :group 'gnus-summary
1154   :type 'string)
1155
1156 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1157   "List of charsets that should be ignored.
1158 When these charsets are used in the \"charset\" parameter, the
1159 default charset will be used instead."
1160   :version "21.1"
1161   :type '(repeat symbol)
1162   :group 'gnus-charset)
1163
1164 (defcustom gnus-newsgroup-maximum-articles nil
1165   "The maximum number of articles a newsgroup.
1166 If this is a number, old articles in a newsgroup exceeding this number
1167 are silently ignored.  If it is nil, no article is ignored.  Note that
1168 setting this variable to a number might prevent you from reading very
1169 old articles."
1170   :group 'gnus-group-select
1171   :version "22.2"
1172   :type '(choice (const :tag "No limit" nil)
1173                  integer))
1174
1175 (gnus-define-group-parameter
1176  ignored-charsets
1177  :type list
1178  :function-document
1179  "Return the ignored charsets of GROUP."
1180  :variable gnus-group-ignored-charsets-alist
1181  :variable-default
1182  '(("alt\\.chinese\\.text" iso-8859-1))
1183  :variable-document
1184  "Alist of regexps (to match group names) and charsets that should be ignored.
1185 When these charsets are used in the \"charset\" parameter, the
1186 default charset will be used instead."
1187  :variable-group gnus-charset
1188  :variable-type '(repeat (cons (regexp :tag "Group")
1189                                (repeat symbol)))
1190  :parameter-type '(choice :tag "Ignored charsets"
1191                           :value nil
1192                           (repeat (symbol)))
1193  :parameter-document       "\
1194 List of charsets that should be ignored.
1195
1196 When these charsets are used in the \"charset\" parameter, the
1197 default charset will be used instead.")
1198
1199 (defcustom gnus-group-highlight-words-alist nil
1200   "Alist of group regexps and highlight regexps.
1201 This variable uses the same syntax as `gnus-emphasis-alist'."
1202   :version "21.1"
1203   :type '(repeat (cons (regexp :tag "Group")
1204                        (repeat (list (regexp :tag "Highlight regexp")
1205                                      (number :tag "Group for entire word" 0)
1206                                      (number :tag "Group for displayed part" 0)
1207                                      (symbol :tag "Face"
1208                                              gnus-emphasis-highlight-words)))))
1209   :group 'gnus-summary-visual)
1210
1211 (defcustom gnus-summary-show-article-charset-alist
1212   nil
1213   "Alist of number and charset.
1214 The article will be shown with the charset corresponding to the
1215 numbered argument.
1216 For example: ((1 . cn-gb-2312) (2 . big5))."
1217   :version "21.1"
1218   :type '(repeat (cons (number :tag "Argument" 1)
1219                        (symbol :tag "Charset")))
1220   :group 'gnus-charset)
1221
1222 (defcustom gnus-preserve-marks t
1223   "Whether marks are preserved when moving, copying and respooling messages."
1224   :version "21.1"
1225   :type 'boolean
1226   :group 'gnus-summary-marks)
1227
1228 (defcustom gnus-propagate-marks t
1229   "If non-nil, do not propagate marks to the backends."
1230   :version "23.1" ;; No Gnus
1231   :type 'boolean
1232   :group 'gnus-summary-marks)
1233
1234 (defcustom gnus-alter-articles-to-read-function nil
1235   "Function to be called to alter the list of articles to be selected."
1236   :type '(choice (const nil) function)
1237   :group 'gnus-summary)
1238
1239 (defcustom gnus-orphan-score nil
1240   "*All orphans get this score added.  Set in the score file."
1241   :group 'gnus-score-default
1242   :type '(choice (const nil)
1243                  integer))
1244
1245 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1246   "*A regexp to match MIME parts when saving multiple parts of a
1247 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1248 This regexp will be used by default when prompting the user for which
1249 type of files to save."
1250   :group 'gnus-summary
1251   :type 'regexp)
1252
1253 (defcustom gnus-read-all-available-headers nil
1254   "Whether Gnus should parse all headers made available to it.
1255 This is mostly relevant for slow back ends where the user may
1256 wish to widen the summary buffer to include all headers
1257 that were fetched."
1258   :version "22.1"
1259   :group 'gnus-summary
1260   :type '(choice boolean regexp))
1261
1262 (defcustom gnus-summary-pipe-output-default-command nil
1263   "Command (and optional arguments) used to pipe article to subprocess.
1264 This will be used as the default command if it is non-nil.  The value
1265 will be updated if you modify it when executing the command
1266 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1267   :version "23.1" ;; No Gnus
1268   :group 'gnus-summary
1269   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1270
1271 (defcustom gnus-summary-muttprint-program "muttprint"
1272   "Command (and optional arguments) used to run Muttprint.
1273 The value will be updated if you modify it when executing the command
1274 `gnus-summary-muttprint'."
1275   :version "22.1"
1276   :group 'gnus-summary
1277   :type 'string)
1278
1279 (defcustom gnus-article-loose-mime t
1280   "If non-nil, don't require MIME-Version header.
1281 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1282 supply the MIME-Version header or deliberately strip it from the mail.
1283 If non-nil (the default), Gnus will treat some articles as MIME
1284 even if the MIME-Version header is missing."
1285   :version "22.1"
1286   :type 'boolean
1287   :group 'gnus-article-mime)
1288
1289 (defcustom gnus-article-emulate-mime t
1290   "If non-nil, use MIME emulation for uuencode and the like.
1291 This means that Gnus will search message bodies for text that look
1292 like uuencoded bits, yEncoded bits, and so on, and present that using
1293 the normal Gnus MIME machinery."
1294   :version "22.1"
1295   :type 'boolean
1296   :group 'gnus-article-mime)
1297
1298 ;;; Internal variables
1299
1300 (defvar gnus-summary-display-cache nil)
1301 (defvar gnus-article-mime-handles nil)
1302 (defvar gnus-article-decoded-p nil)
1303 (defvar gnus-article-charset nil)
1304 (defvar gnus-article-ignored-charsets nil)
1305 (defvar gnus-scores-exclude-files nil)
1306 (defvar gnus-page-broken nil)
1307
1308 (defvar gnus-original-article nil)
1309 (defvar gnus-article-internal-prepare-hook nil)
1310 (defvar gnus-newsgroup-process-stack nil)
1311
1312 (defvar gnus-thread-indent-array nil)
1313 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1314 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1315   "Function called to sort the articles within a thread after it has been gathered together.")
1316
1317 (defvar gnus-summary-save-parts-type-history nil)
1318 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1319
1320 ;; Avoid highlighting in kill files.
1321 (defvar gnus-summary-inhibit-highlight nil)
1322 (defvar gnus-newsgroup-selected-overlay nil)
1323 (defvar gnus-inhibit-limiting nil)
1324 (defvar gnus-newsgroup-adaptive-score-file nil)
1325 (defvar gnus-current-score-file nil)
1326 (defvar gnus-current-move-group nil)
1327 (defvar gnus-current-copy-group nil)
1328 (defvar gnus-current-crosspost-group nil)
1329 (defvar gnus-newsgroup-display nil)
1330
1331 (defvar gnus-newsgroup-dependencies nil)
1332 (defvar gnus-newsgroup-adaptive nil)
1333 (defvar gnus-summary-display-article-function nil)
1334 (defvar gnus-summary-highlight-line-function nil
1335   "Function called after highlighting a summary line.")
1336
1337 (defvar gnus-summary-line-format-alist
1338   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1339     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1340     (?s gnus-tmp-subject-or-nil ?s)
1341     (?n gnus-tmp-name ?s)
1342     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1343         ?s)
1344     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1345             gnus-tmp-from) ?s)
1346     (?F gnus-tmp-from ?s)
1347     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1348     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1349     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1350     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1351     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1352     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1353     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1354     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1355     (?L gnus-tmp-lines ?s)
1356     (?O gnus-tmp-downloaded ?c)
1357     (?I gnus-tmp-indentation ?s)
1358     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1359     (?R gnus-tmp-replied ?c)
1360     (?\[ gnus-tmp-opening-bracket ?c)
1361     (?\] gnus-tmp-closing-bracket ?c)
1362     (?\> (make-string gnus-tmp-level ? ) ?s)
1363     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1364     (?i gnus-tmp-score ?d)
1365     (?z gnus-tmp-score-char ?c)
1366     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1367     (?U gnus-tmp-unread ?c)
1368     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1369         ?s)
1370     (?t (gnus-summary-number-of-articles-in-thread
1371          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1372         ?d)
1373     (?e (gnus-summary-number-of-articles-in-thread
1374          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1375         ?c)
1376     (?u gnus-tmp-user-defined ?s)
1377     (?P (gnus-pick-line-number) ?d)
1378     (?B gnus-tmp-thread-tree-header-string ?s)
1379     (user-date (gnus-user-date
1380                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1381   "An alist of format specifications that can appear in summary lines.
1382 These are paired with what variables they correspond with, along with
1383 the type of the variable (string, integer, character, etc).")
1384
1385 (defvar gnus-summary-dummy-line-format-alist
1386   `((?S gnus-tmp-subject ?s)
1387     (?N gnus-tmp-number ?d)
1388     (?u gnus-tmp-user-defined ?s)))
1389
1390 (defvar gnus-summary-mode-line-format-alist
1391   `((?G gnus-tmp-group-name ?s)
1392     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1393     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1394     (?A gnus-tmp-article-number ?d)
1395     (?Z gnus-tmp-unread-and-unselected ?s)
1396     (?V gnus-version ?s)
1397     (?U gnus-tmp-unread-and-unticked ?d)
1398     (?S gnus-tmp-subject ?s)
1399     (?e gnus-tmp-unselected ?d)
1400     (?u gnus-tmp-user-defined ?s)
1401     (?d (length gnus-newsgroup-dormant) ?d)
1402     (?t (length gnus-newsgroup-marked) ?d)
1403     (?h (length gnus-newsgroup-spam-marked) ?d)
1404     (?r (length gnus-newsgroup-reads) ?d)
1405     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1406     (?E gnus-newsgroup-expunged-tally ?d)
1407     (?s (gnus-current-score-file-nondirectory) ?s)))
1408
1409 ;; This is here rather than in gnus-art for compilation reasons.
1410 (defvar gnus-article-mode-line-format-alist
1411   (nconc '((?w (gnus-article-wash-status) ?s)
1412            (?m (gnus-article-mime-part-status) ?s))
1413          gnus-summary-mode-line-format-alist))
1414
1415 (defvar gnus-last-search-regexp nil
1416   "Default regexp for article search command.")
1417
1418 (defvar gnus-last-shell-command nil
1419   "Default shell command on article.")
1420
1421 (defvar gnus-newsgroup-agentized nil
1422   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1423 (defvar gnus-newsgroup-begin nil)
1424 (defvar gnus-newsgroup-end nil)
1425 (defvar gnus-newsgroup-last-rmail nil)
1426 (defvar gnus-newsgroup-last-mail nil)
1427 (defvar gnus-newsgroup-last-folder nil)
1428 (defvar gnus-newsgroup-last-file nil)
1429 (defvar gnus-newsgroup-last-directory nil)
1430 (defvar gnus-newsgroup-auto-expire nil)
1431 (defvar gnus-newsgroup-active nil)
1432
1433 (defvar gnus-newsgroup-data nil)
1434 (defvar gnus-newsgroup-data-reverse nil)
1435 (defvar gnus-newsgroup-limit nil)
1436 (defvar gnus-newsgroup-limits nil)
1437 (defvar gnus-summary-use-undownloaded-faces nil)
1438
1439 (defvar gnus-newsgroup-unreads nil
1440   "Sorted list of unread articles in the current newsgroup.")
1441
1442 (defvar gnus-newsgroup-unselected nil
1443   "Sorted list of unselected unread articles in the current newsgroup.")
1444
1445 (defvar gnus-newsgroup-reads nil
1446   "Alist of read articles and article marks in the current newsgroup.")
1447
1448 (defvar gnus-newsgroup-expunged-tally nil)
1449
1450 (defvar gnus-newsgroup-marked nil
1451   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1452
1453 (defvar gnus-newsgroup-spam-marked nil
1454   "List of ranges of articles that have been marked as spam.")
1455
1456 (defvar gnus-newsgroup-killed nil
1457   "List of ranges of articles that have been through the scoring process.")
1458
1459 (defvar gnus-newsgroup-cached nil
1460   "Sorted list of articles that come from the article cache.")
1461
1462 (defvar gnus-newsgroup-saved nil
1463   "List of articles that have been saved.")
1464
1465 (defvar gnus-newsgroup-kill-headers nil)
1466
1467 (defvar gnus-newsgroup-replied nil
1468   "List of articles that have been replied to in the current newsgroup.")
1469
1470 (defvar gnus-newsgroup-forwarded nil
1471   "List of articles that have been forwarded in the current newsgroup.")
1472
1473 (defvar gnus-newsgroup-recent nil
1474   "List of articles that have are recent in the current newsgroup.")
1475
1476 (defvar gnus-newsgroup-expirable nil
1477   "Sorted list of articles in the current newsgroup that can be expired.")
1478
1479 (defvar gnus-newsgroup-processable nil
1480   "List of articles in the current newsgroup that can be processed.")
1481
1482 (defvar gnus-newsgroup-downloadable nil
1483   "Sorted list of articles in the current newsgroup that can be processed.")
1484
1485 (defvar gnus-newsgroup-unfetched nil
1486   "Sorted list of articles in the current newsgroup whose headers have
1487 not been fetched into the agent.
1488
1489 This list will always be a subset of gnus-newsgroup-undownloaded.")
1490
1491 (defvar gnus-newsgroup-undownloaded nil
1492   "List of articles in the current newsgroup that haven't been downloaded.")
1493
1494 (defvar gnus-newsgroup-unsendable nil
1495   "List of articles in the current newsgroup that won't be sent.")
1496
1497 (defvar gnus-newsgroup-bookmarks nil
1498   "List of articles in the current newsgroup that have bookmarks.")
1499
1500 (defvar gnus-newsgroup-dormant nil
1501   "Sorted list of dormant articles in the current newsgroup.")
1502
1503 (defvar gnus-newsgroup-unseen nil
1504   "List of unseen articles in the current newsgroup.")
1505
1506 (defvar gnus-newsgroup-seen nil
1507   "Range of seen articles in the current newsgroup.")
1508
1509 (defvar gnus-newsgroup-articles nil
1510   "List of articles in the current newsgroup.")
1511
1512 (defvar gnus-newsgroup-scored nil
1513   "List of scored articles in the current newsgroup.")
1514
1515 (defvar gnus-newsgroup-headers nil
1516   "List of article headers in the current newsgroup.")
1517
1518 (defvar gnus-newsgroup-threads nil)
1519
1520 (defvar gnus-newsgroup-prepared nil
1521   "Whether the current group has been prepared properly.")
1522
1523 (defvar gnus-newsgroup-ancient nil
1524   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1525
1526 (defvar gnus-newsgroup-sparse nil)
1527
1528 (defvar gnus-current-article nil)
1529 (defvar gnus-article-current nil)
1530 (defvar gnus-current-headers nil)
1531 (defvar gnus-have-all-headers nil)
1532 (defvar gnus-last-article nil)
1533 (defvar gnus-newsgroup-history nil)
1534 (defvar gnus-newsgroup-charset nil)
1535 (defvar gnus-newsgroup-ephemeral-charset nil)
1536 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1537
1538 (defvar gnus-article-before-search nil)
1539
1540 (defvar gnus-summary-local-variables
1541   '(gnus-newsgroup-name
1542
1543     ;; Marks lists
1544     gnus-newsgroup-unreads
1545     gnus-newsgroup-unselected
1546     gnus-newsgroup-marked
1547     gnus-newsgroup-spam-marked
1548     gnus-newsgroup-reads
1549     gnus-newsgroup-saved
1550     gnus-newsgroup-replied
1551     gnus-newsgroup-forwarded
1552     gnus-newsgroup-recent
1553     gnus-newsgroup-expirable
1554     gnus-newsgroup-killed
1555     gnus-newsgroup-unseen
1556     gnus-newsgroup-seen
1557     gnus-newsgroup-cached
1558     gnus-newsgroup-downloadable
1559     gnus-newsgroup-undownloaded
1560     gnus-newsgroup-unsendable
1561
1562     gnus-newsgroup-begin gnus-newsgroup-end
1563     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1564     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1565     gnus-newsgroup-last-directory
1566     gnus-newsgroup-auto-expire
1567     gnus-newsgroup-processable
1568     gnus-newsgroup-unfetched
1569     gnus-newsgroup-articles
1570     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1571     gnus-newsgroup-headers gnus-newsgroup-threads
1572     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1573     gnus-current-article gnus-current-headers gnus-have-all-headers
1574     gnus-last-article gnus-article-internal-prepare-hook
1575     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1576     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1577     gnus-thread-expunge-below
1578     gnus-score-alist gnus-current-score-file
1579     (gnus-summary-expunge-below . global)
1580     (gnus-summary-mark-below . global)
1581     (gnus-orphan-score . global)
1582     gnus-newsgroup-active gnus-scores-exclude-files
1583     gnus-newsgroup-history gnus-newsgroup-ancient
1584     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1585     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1586     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1587     (gnus-newsgroup-expunged-tally . 0)
1588     gnus-cache-removable-articles
1589     gnus-newsgroup-data gnus-newsgroup-data-reverse
1590     gnus-newsgroup-limit gnus-newsgroup-limits
1591     gnus-newsgroup-charset gnus-newsgroup-display
1592     gnus-summary-use-undownloaded-faces)
1593   "Variables that are buffer-local to the summary buffers.")
1594
1595 (defvar gnus-newsgroup-variables nil
1596   "A list of variables that have separate values in different newsgroups.
1597 A list of newsgroup (summary buffer) local variables, or cons of
1598 variables and their default expressions to be evalled (when the default
1599 values are not nil), that should be made global while the summary buffer
1600 is active.
1601
1602 Note: The default expressions will be evaluated (using function `eval')
1603 before assignment to the local variable rather than just assigned to it.
1604 If the default expression is the symbol `global', that symbol will not
1605 be evaluated but the global value of the local variable will be used
1606 instead.
1607
1608 These variables can be used to set variables in the group parameters
1609 while still allowing them to affect operations done in other buffers.
1610 For example:
1611
1612 \(setq gnus-newsgroup-variables
1613      '(message-use-followup-to
1614        (gnus-visible-headers .
1615          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1616 ")
1617
1618 (eval-when-compile
1619   ;; Bind features so that require will believe that gnus-sum has
1620   ;; already been loaded (avoids infinite recursion)
1621   (let ((features (cons 'gnus-sum features)))
1622     (require 'gnus-art)))
1623
1624 ;; MIME stuff.
1625
1626 (defvar gnus-decode-encoded-word-methods
1627   '(mail-decode-encoded-word-string)
1628   "List of methods used to decode encoded words.
1629
1630 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1631 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1632 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1633 whose names match REGEXP.
1634
1635 For example:
1636 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1637  mail-decode-encoded-word-string
1638  (\"chinese\" . rfc1843-decode-string))")
1639
1640 (defvar gnus-decode-encoded-word-methods-cache nil)
1641
1642 (defun gnus-multi-decode-encoded-word-string (string)
1643   "Apply the functions from `gnus-encoded-word-methods' that match."
1644   (unless (and gnus-decode-encoded-word-methods-cache
1645                (eq gnus-newsgroup-name
1646                    (car gnus-decode-encoded-word-methods-cache)))
1647     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1648     (dolist (method gnus-decode-encoded-word-methods)
1649       (if (symbolp method)
1650           (nconc gnus-decode-encoded-word-methods-cache (list method))
1651         (if (and gnus-newsgroup-name
1652                  (string-match (car method) gnus-newsgroup-name))
1653             (nconc gnus-decode-encoded-word-methods-cache
1654                    (list (cdr method)))))))
1655   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1656     (setq string (funcall method string))))
1657
1658 ;; Subject simplification.
1659
1660 (defun gnus-simplify-whitespace (str)
1661   "Remove excessive whitespace from STR."
1662   ;; Multiple spaces.
1663   (while (string-match "[ \t][ \t]+" str)
1664     (setq str (concat (substring str 0 (match-beginning 0))
1665                         " "
1666                         (substring str (match-end 0)))))
1667   ;; Leading spaces.
1668   (when (string-match "^[ \t]+" str)
1669     (setq str (substring str (match-end 0))))
1670   ;; Trailing spaces.
1671   (when (string-match "[ \t]+$" str)
1672     (setq str (substring str 0 (match-beginning 0))))
1673   str)
1674
1675 (defun gnus-simplify-all-whitespace (str)
1676   "Remove all whitespace from STR."
1677   (while (string-match "[ \t\n]+" str)
1678     (setq str (replace-match "" nil nil str)))
1679   str)
1680
1681 (defsubst gnus-simplify-subject-re (subject)
1682   "Remove \"Re:\" from subject lines."
1683   (if (string-match message-subject-re-regexp subject)
1684       (substring subject (match-end 0))
1685     subject))
1686
1687 (defun gnus-simplify-subject (subject &optional re-only)
1688   "Remove `Re:' and words in parentheses.
1689 If RE-ONLY is non-nil, strip leading `Re:'s only."
1690   (let ((case-fold-search t))           ;Ignore case.
1691     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1692     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1693       (setq subject (substring subject (match-end 0))))
1694     ;; Remove uninteresting prefixes.
1695     (when (and (not re-only)
1696                gnus-simplify-ignored-prefixes
1697                (string-match gnus-simplify-ignored-prefixes subject))
1698       (setq subject (substring subject (match-end 0))))
1699     ;; Remove words in parentheses from end.
1700     (unless re-only
1701       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1702         (setq subject (substring subject 0 (match-beginning 0)))))
1703     ;; Return subject string.
1704     subject))
1705
1706 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1707 ;; all whitespace.
1708 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1709   (goto-char (point-min))
1710   (while (re-search-forward regexp nil t)
1711     (replace-match (or newtext ""))))
1712
1713 (defun gnus-simplify-buffer-fuzzy ()
1714   "Simplify string in the buffer fuzzily.
1715 The string in the accessible portion of the current buffer is simplified.
1716 It is assumed to be a single-line subject.
1717 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1718 matter is removed.  Additional things can be deleted by setting
1719 `gnus-simplify-subject-fuzzy-regexp'."
1720   (let ((case-fold-search t)
1721         (modified-tick))
1722     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1723
1724     (while (not (eq modified-tick (buffer-modified-tick)))
1725       (setq modified-tick (buffer-modified-tick))
1726       (cond
1727        ((listp gnus-simplify-subject-fuzzy-regexp)
1728         (mapc 'gnus-simplify-buffer-fuzzy-step
1729               gnus-simplify-subject-fuzzy-regexp))
1730        (gnus-simplify-subject-fuzzy-regexp
1731         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1732       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1733       (gnus-simplify-buffer-fuzzy-step
1734        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1735       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1736
1737     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1738     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1739     (gnus-simplify-buffer-fuzzy-step " $")
1740     (gnus-simplify-buffer-fuzzy-step "^ +")))
1741
1742 (defun gnus-simplify-subject-fuzzy (subject)
1743   "Simplify a subject string fuzzily.
1744 See `gnus-simplify-buffer-fuzzy' for details."
1745   (save-excursion
1746     (gnus-set-work-buffer)
1747     (let ((case-fold-search t))
1748       ;; Remove uninteresting prefixes.
1749       (when (and gnus-simplify-ignored-prefixes
1750                  (string-match gnus-simplify-ignored-prefixes subject))
1751         (setq subject (substring subject (match-end 0))))
1752       (insert subject)
1753       (inline (gnus-simplify-buffer-fuzzy))
1754       (buffer-string))))
1755
1756 (defsubst gnus-simplify-subject-fully (subject)
1757   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1758   (cond
1759    (gnus-simplify-subject-functions
1760     (gnus-map-function gnus-simplify-subject-functions subject))
1761    ((null gnus-summary-gather-subject-limit)
1762     (gnus-simplify-subject-re subject))
1763    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1764     (gnus-simplify-subject-fuzzy subject))
1765    ((numberp gnus-summary-gather-subject-limit)
1766     (truncate-string-to-width (gnus-simplify-subject-re subject)
1767                               gnus-summary-gather-subject-limit))
1768    (t
1769     subject)))
1770
1771 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1772   "Check whether two subjects are equal.
1773 If optional argument SIMPLE-FIRST is t, first argument is already
1774 simplified."
1775   (cond
1776    ((null simple-first)
1777     (equal (gnus-simplify-subject-fully s1)
1778            (gnus-simplify-subject-fully s2)))
1779    (t
1780     (equal s1
1781            (gnus-simplify-subject-fully s2)))))
1782
1783 (defun gnus-summary-bubble-group ()
1784   "Increase the score of the current group.
1785 This is a handy function to add to `gnus-summary-exit-hook' to
1786 increase the score of each group you read."
1787   (gnus-group-add-score gnus-newsgroup-name))
1788
1789 \f
1790 ;;;
1791 ;;; Gnus summary mode
1792 ;;;
1793
1794 (put 'gnus-summary-mode 'mode-class 'special)
1795
1796 (defvar gnus-article-commands-menu)
1797
1798 ;; Non-orthogonal keys
1799
1800 (gnus-define-keys gnus-summary-mode-map
1801   " " gnus-summary-next-page
1802   "\177" gnus-summary-prev-page
1803   [delete] gnus-summary-prev-page
1804   [backspace] gnus-summary-prev-page
1805   "\r" gnus-summary-scroll-up
1806   "\M-\r" gnus-summary-scroll-down
1807   "n" gnus-summary-next-unread-article
1808   "p" gnus-summary-prev-unread-article
1809   "N" gnus-summary-next-article
1810   "P" gnus-summary-prev-article
1811   "\M-\C-n" gnus-summary-next-same-subject
1812   "\M-\C-p" gnus-summary-prev-same-subject
1813   "\M-n" gnus-summary-next-unread-subject
1814   "\M-p" gnus-summary-prev-unread-subject
1815   "." gnus-summary-first-unread-article
1816   "," gnus-summary-best-unread-article
1817   "\M-s" gnus-summary-search-article-forward
1818   "\M-r" gnus-summary-search-article-backward
1819   "\M-S" gnus-summary-repeat-search-article-forward
1820   "\M-R" gnus-summary-repeat-search-article-backward
1821   "<" gnus-summary-beginning-of-article
1822   ">" gnus-summary-end-of-article
1823   "j" gnus-summary-goto-article
1824   "^" gnus-summary-refer-parent-article
1825   "\M-^" gnus-summary-refer-article
1826   "u" gnus-summary-tick-article-forward
1827   "!" gnus-summary-tick-article-forward
1828   "U" gnus-summary-tick-article-backward
1829   "d" gnus-summary-mark-as-read-forward
1830   "D" gnus-summary-mark-as-read-backward
1831   "E" gnus-summary-mark-as-expirable
1832   "\M-u" gnus-summary-clear-mark-forward
1833   "\M-U" gnus-summary-clear-mark-backward
1834   "k" gnus-summary-kill-same-subject-and-select
1835   "\C-k" gnus-summary-kill-same-subject
1836   "\M-\C-k" gnus-summary-kill-thread
1837   "\M-\C-l" gnus-summary-lower-thread
1838   "e" gnus-summary-edit-article
1839   "#" gnus-summary-mark-as-processable
1840   "\M-#" gnus-summary-unmark-as-processable
1841   "\M-\C-t" gnus-summary-toggle-threads
1842   "\M-\C-s" gnus-summary-show-thread
1843   "\M-\C-h" gnus-summary-hide-thread
1844   "\M-\C-f" gnus-summary-next-thread
1845   "\M-\C-b" gnus-summary-prev-thread
1846   [(meta down)] gnus-summary-next-thread
1847   [(meta up)] gnus-summary-prev-thread
1848   "\M-\C-u" gnus-summary-up-thread
1849   "\M-\C-d" gnus-summary-down-thread
1850   "&" gnus-summary-execute-command
1851   "c" gnus-summary-catchup-and-exit
1852   "\C-w" gnus-summary-mark-region-as-read
1853   "\C-t" gnus-summary-toggle-truncation
1854   "?" gnus-summary-mark-as-dormant
1855   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1856   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1857   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1858   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1859   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1860   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1861   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1862   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1863   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1864   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1865   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1866   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1867   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1868   "=" gnus-summary-expand-window
1869   "\C-x\C-s" gnus-summary-reselect-current-group
1870   "\M-g" gnus-summary-rescan-group
1871   "\C-c\C-r" gnus-summary-caesar-message
1872   "f" gnus-summary-followup
1873   "F" gnus-summary-followup-with-original
1874   "C" gnus-summary-cancel-article
1875   "r" gnus-summary-reply
1876   "R" gnus-summary-reply-with-original
1877   "\C-c\C-f" gnus-summary-mail-forward
1878   "o" gnus-summary-save-article
1879   "\C-o" gnus-summary-save-article-mail
1880   "|" gnus-summary-pipe-output
1881   "\M-k" gnus-summary-edit-local-kill
1882   "\M-K" gnus-summary-edit-global-kill
1883   ;; "V" gnus-version
1884   "\C-c\C-d" gnus-summary-describe-group
1885   "q" gnus-summary-exit
1886   "Q" gnus-summary-exit-no-update
1887   "\C-c\C-i" gnus-info-find-node
1888   gnus-mouse-2 gnus-mouse-pick-article
1889   [follow-link] mouse-face
1890   "m" gnus-summary-mail-other-window
1891   "a" gnus-summary-post-news
1892   "x" gnus-summary-limit-to-unread
1893   "s" gnus-summary-isearch-article
1894   "t" gnus-summary-toggle-header
1895   "g" gnus-summary-show-article
1896   "l" gnus-summary-goto-last-article
1897   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1898   "\C-d" gnus-summary-enter-digest-group
1899   "\M-\C-d" gnus-summary-read-document
1900   "\M-\C-e" gnus-summary-edit-parameters
1901   "\M-\C-a" gnus-summary-customize-parameters
1902   "\C-c\C-b" gnus-bug
1903   "*" gnus-cache-enter-article
1904   "\M-*" gnus-cache-remove-article
1905   "\M-&" gnus-summary-universal-argument
1906   "\C-l" gnus-recenter
1907   "I" gnus-summary-increase-score
1908   "L" gnus-summary-lower-score
1909   "\M-i" gnus-symbolic-argument
1910   "h" gnus-summary-select-article-buffer
1911
1912   "b" gnus-article-view-part
1913   "\M-t" gnus-summary-toggle-display-buttonized
1914
1915   "V" gnus-summary-score-map
1916   "X" gnus-uu-extract-map
1917   "S" gnus-summary-send-map)
1918
1919 ;; Sort of orthogonal keymap
1920 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1921   "t" gnus-summary-tick-article-forward
1922   "!" gnus-summary-tick-article-forward
1923   "d" gnus-summary-mark-as-read-forward
1924   "r" gnus-summary-mark-as-read-forward
1925   "c" gnus-summary-clear-mark-forward
1926   " " gnus-summary-clear-mark-forward
1927   "e" gnus-summary-mark-as-expirable
1928   "x" gnus-summary-mark-as-expirable
1929   "?" gnus-summary-mark-as-dormant
1930   "b" gnus-summary-set-bookmark
1931   "B" gnus-summary-remove-bookmark
1932   "#" gnus-summary-mark-as-processable
1933   "\M-#" gnus-summary-unmark-as-processable
1934   "S" gnus-summary-limit-include-expunged
1935   "C" gnus-summary-catchup
1936   "H" gnus-summary-catchup-to-here
1937   "h" gnus-summary-catchup-from-here
1938   "\C-c" gnus-summary-catchup-all
1939   "k" gnus-summary-kill-same-subject-and-select
1940   "K" gnus-summary-kill-same-subject
1941   "P" gnus-uu-mark-map)
1942
1943 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1944   "c" gnus-summary-clear-above
1945   "u" gnus-summary-tick-above
1946   "m" gnus-summary-mark-above
1947   "k" gnus-summary-kill-below)
1948
1949 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1950   "/" gnus-summary-limit-to-subject
1951   "n" gnus-summary-limit-to-articles
1952   "b" gnus-summary-limit-to-bodies
1953   "h" gnus-summary-limit-to-headers
1954   "w" gnus-summary-pop-limit
1955   "s" gnus-summary-limit-to-subject
1956   "a" gnus-summary-limit-to-author
1957   "u" gnus-summary-limit-to-unread
1958   "m" gnus-summary-limit-to-marks
1959   "M" gnus-summary-limit-exclude-marks
1960   "v" gnus-summary-limit-to-score
1961   "*" gnus-summary-limit-include-cached
1962   "D" gnus-summary-limit-include-dormant
1963   "T" gnus-summary-limit-include-thread
1964   "d" gnus-summary-limit-exclude-dormant
1965   "t" gnus-summary-limit-to-age
1966   "." gnus-summary-limit-to-unseen
1967   "x" gnus-summary-limit-to-extra
1968   "p" gnus-summary-limit-to-display-predicate
1969   "E" gnus-summary-limit-include-expunged
1970   "c" gnus-summary-limit-exclude-childless-dormant
1971   "C" gnus-summary-limit-mark-excluded-as-read
1972   "o" gnus-summary-insert-old-articles
1973   "N" gnus-summary-insert-new-articles
1974   "S" gnus-summary-limit-to-singletons
1975   "r" gnus-summary-limit-to-replied
1976   "R" gnus-summary-limit-to-recipient
1977   "A" gnus-summary-limit-to-address)
1978
1979 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1980   "n" gnus-summary-next-unread-article
1981   "p" gnus-summary-prev-unread-article
1982   "N" gnus-summary-next-article
1983   "P" gnus-summary-prev-article
1984   "\C-n" gnus-summary-next-same-subject
1985   "\C-p" gnus-summary-prev-same-subject
1986   "\M-n" gnus-summary-next-unread-subject
1987   "\M-p" gnus-summary-prev-unread-subject
1988   "f" gnus-summary-first-unread-article
1989   "b" gnus-summary-best-unread-article
1990   "j" gnus-summary-goto-article
1991   "g" gnus-summary-goto-subject
1992   "l" gnus-summary-goto-last-article
1993   "o" gnus-summary-pop-article)
1994
1995 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1996   "k" gnus-summary-kill-thread
1997   "E" gnus-summary-expire-thread
1998   "l" gnus-summary-lower-thread
1999   "i" gnus-summary-raise-thread
2000   "T" gnus-summary-toggle-threads
2001   "t" gnus-summary-rethread-current
2002   "^" gnus-summary-reparent-thread
2003   "\M-^" gnus-summary-reparent-children
2004   "s" gnus-summary-show-thread
2005   "S" gnus-summary-show-all-threads
2006   "h" gnus-summary-hide-thread
2007   "H" gnus-summary-hide-all-threads
2008   "n" gnus-summary-next-thread
2009   "p" gnus-summary-prev-thread
2010   "u" gnus-summary-up-thread
2011   "o" gnus-summary-top-thread
2012   "d" gnus-summary-down-thread
2013   "#" gnus-uu-mark-thread
2014   "\M-#" gnus-uu-unmark-thread)
2015
2016 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2017   "g" gnus-summary-prepare
2018   "c" gnus-summary-insert-cached-articles
2019   "d" gnus-summary-insert-dormant-articles
2020   "t" gnus-summary-insert-ticked-articles)
2021
2022 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2023   "c" gnus-summary-catchup-and-exit
2024   "C" gnus-summary-catchup-all-and-exit
2025   "E" gnus-summary-exit-no-update
2026   "Q" gnus-summary-exit
2027   "Z" gnus-summary-exit
2028   "n" gnus-summary-catchup-and-goto-next-group
2029   "p" gnus-summary-catchup-and-goto-prev-group
2030   "R" gnus-summary-reselect-current-group
2031   "G" gnus-summary-rescan-group
2032   "N" gnus-summary-next-group
2033   "s" gnus-summary-save-newsrc
2034   "P" gnus-summary-prev-group)
2035
2036 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2037   " " gnus-summary-next-page
2038   "n" gnus-summary-next-page
2039   "\177" gnus-summary-prev-page
2040   [delete] gnus-summary-prev-page
2041   "p" gnus-summary-prev-page
2042   "\r" gnus-summary-scroll-up
2043   "\M-\r" gnus-summary-scroll-down
2044   "<" gnus-summary-beginning-of-article
2045   ">" gnus-summary-end-of-article
2046   "b" gnus-summary-beginning-of-article
2047   "e" gnus-summary-end-of-article
2048   "^" gnus-summary-refer-parent-article
2049   "r" gnus-summary-refer-parent-article
2050   "D" gnus-summary-enter-digest-group
2051   "R" gnus-summary-refer-references
2052   "T" gnus-summary-refer-thread
2053   "g" gnus-summary-show-article
2054   "s" gnus-summary-isearch-article
2055   "P" gnus-summary-print-article
2056   "S" gnus-sticky-article
2057   "M" gnus-mailing-list-insinuate
2058   "t" gnus-article-babel)
2059
2060 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2061   "b" gnus-article-add-buttons
2062   "B" gnus-article-add-buttons-to-head
2063   "o" gnus-article-treat-overstrike
2064   "e" gnus-article-emphasize
2065   "w" gnus-article-fill-cited-article
2066   "Q" gnus-article-fill-long-lines
2067   "L" gnus-article-toggle-truncate-lines
2068   "C" gnus-article-capitalize-sentences
2069   "c" gnus-article-remove-cr
2070   "q" gnus-article-de-quoted-unreadable
2071   "6" gnus-article-de-base64-unreadable
2072   "Z" gnus-article-decode-HZ
2073   "A" gnus-article-treat-ansi-sequences
2074   "h" gnus-article-wash-html
2075   "u" gnus-article-unsplit-urls
2076   "s" gnus-summary-force-verify-and-decrypt
2077   "f" gnus-article-display-x-face
2078   "l" gnus-summary-stop-page-breaking
2079   "r" gnus-summary-caesar-message
2080   "m" gnus-summary-morse-message
2081   "t" gnus-summary-toggle-header
2082   "g" gnus-treat-smiley
2083   "v" gnus-summary-verbose-headers
2084   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2085   "p" gnus-article-verify-x-pgp-sig
2086   "d" gnus-article-treat-dumbquotes
2087   "i" gnus-summary-idna-message)
2088
2089 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2090   ;; mnemonic: deuglif*Y*
2091   "u" gnus-article-outlook-unwrap-lines
2092   "a" gnus-article-outlook-repair-attribution
2093   "c" gnus-article-outlook-rearrange-citation
2094   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2095
2096 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2097   "a" gnus-article-hide
2098   "h" gnus-article-hide-headers
2099   "b" gnus-article-hide-boring-headers
2100   "s" gnus-article-hide-signature
2101   "c" gnus-article-hide-citation
2102   "C" gnus-article-hide-citation-in-followups
2103   "l" gnus-article-hide-list-identifiers
2104   "B" gnus-article-strip-banner
2105   "P" gnus-article-hide-pem
2106   "\C-c" gnus-article-hide-citation-maybe)
2107
2108 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2109   "a" gnus-article-highlight
2110   "h" gnus-article-highlight-headers
2111   "c" gnus-article-highlight-citation
2112   "s" gnus-article-highlight-signature)
2113
2114 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2115   "f" gnus-article-treat-fold-headers
2116   "u" gnus-article-treat-unfold-headers
2117   "n" gnus-article-treat-fold-newsgroups)
2118
2119 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2120   "x" gnus-article-display-x-face
2121   "d" gnus-article-display-face
2122   "s" gnus-treat-smiley
2123   "D" gnus-article-remove-images
2124   "W" gnus-html-show-images
2125   "f" gnus-treat-from-picon
2126   "m" gnus-treat-mail-picon
2127   "n" gnus-treat-newsgroups-picon)
2128
2129 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2130   "w" gnus-article-decode-mime-words
2131   "c" gnus-article-decode-charset
2132   "v" gnus-mime-view-all-parts
2133   "b" gnus-article-view-part)
2134
2135 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2136   "z" gnus-article-date-ut
2137   "u" gnus-article-date-ut
2138   "l" gnus-article-date-local
2139   "p" gnus-article-date-english
2140   "e" gnus-article-date-lapsed
2141   "o" gnus-article-date-original
2142   "i" gnus-article-date-iso8601
2143   "s" gnus-article-date-user)
2144
2145 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2146   "t" gnus-article-remove-trailing-blank-lines
2147   "l" gnus-article-strip-leading-blank-lines
2148   "m" gnus-article-strip-multiple-blank-lines
2149   "a" gnus-article-strip-blank-lines
2150   "A" gnus-article-strip-all-blank-lines
2151   "s" gnus-article-strip-leading-space
2152   "e" gnus-article-strip-trailing-space
2153   "w" gnus-article-remove-leading-whitespace)
2154
2155 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2156   "v" gnus-version
2157   "f" gnus-summary-fetch-faq
2158   "d" gnus-summary-describe-group
2159   "h" gnus-summary-describe-briefly
2160   "i" gnus-info-find-node
2161   "c" gnus-group-fetch-charter
2162   "C" gnus-group-fetch-control)
2163
2164 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2165   "e" gnus-summary-expire-articles
2166   "\M-\C-e" gnus-summary-expire-articles-now
2167   "\177" gnus-summary-delete-article
2168   [delete] gnus-summary-delete-article
2169   [backspace] gnus-summary-delete-article
2170   "m" gnus-summary-move-article
2171   "r" gnus-summary-respool-article
2172   "w" gnus-summary-edit-article
2173   "c" gnus-summary-copy-article
2174   "B" gnus-summary-crosspost-article
2175   "q" gnus-summary-respool-query
2176   "t" gnus-summary-respool-trace
2177   "i" gnus-summary-import-article
2178   "I" gnus-summary-create-article
2179   "p" gnus-summary-article-posted-p)
2180
2181 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2182   "o" gnus-summary-save-article
2183   "m" gnus-summary-save-article-mail
2184   "F" gnus-summary-write-article-file
2185   "r" gnus-summary-save-article-rmail
2186   "f" gnus-summary-save-article-file
2187   "b" gnus-summary-save-article-body-file
2188   "B" gnus-summary-write-article-body-file
2189   "h" gnus-summary-save-article-folder
2190   "v" gnus-summary-save-article-vm
2191   "p" gnus-summary-pipe-output
2192   "P" gnus-summary-muttprint)
2193
2194 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2195   "b" gnus-summary-display-buttonized
2196   "m" gnus-summary-repair-multipart
2197   "v" gnus-article-view-part
2198   "o" gnus-article-save-part
2199   "O" gnus-article-save-part-and-strip
2200   "r" gnus-article-replace-part
2201   "d" gnus-article-delete-part
2202   "t" gnus-article-view-part-as-type
2203   "j" gnus-article-jump-to-part
2204   "c" gnus-article-copy-part
2205   "C" gnus-article-view-part-as-charset
2206   "e" gnus-article-view-part-externally
2207   "H" gnus-article-browse-html-article
2208   "E" gnus-article-encrypt-body
2209   "i" gnus-article-inline-part
2210   "|" gnus-article-pipe-part)
2211
2212 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2213   "p" gnus-summary-mark-as-processable
2214   "u" gnus-summary-unmark-as-processable
2215   "U" gnus-summary-unmark-all-processable
2216   "v" gnus-uu-mark-over
2217   "s" gnus-uu-mark-series
2218   "r" gnus-uu-mark-region
2219   "g" gnus-uu-unmark-region
2220   "R" gnus-uu-mark-by-regexp
2221   "G" gnus-uu-unmark-by-regexp
2222   "t" gnus-uu-mark-thread
2223   "T" gnus-uu-unmark-thread
2224   "a" gnus-uu-mark-all
2225   "b" gnus-uu-mark-buffer
2226   "S" gnus-uu-mark-sparse
2227   "k" gnus-summary-kill-process-mark
2228   "y" gnus-summary-yank-process-mark
2229   "w" gnus-summary-save-process-mark
2230   "i" gnus-uu-invert-processable)
2231
2232 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2233   ;;"x" gnus-uu-extract-any
2234   "m" gnus-summary-save-parts
2235   "u" gnus-uu-decode-uu
2236   "U" gnus-uu-decode-uu-and-save
2237   "s" gnus-uu-decode-unshar
2238   "S" gnus-uu-decode-unshar-and-save
2239   "o" gnus-uu-decode-save
2240   "O" gnus-uu-decode-save
2241   "b" gnus-uu-decode-binhex
2242   "B" gnus-uu-decode-binhex
2243   "Y" gnus-uu-decode-yenc
2244   "p" gnus-uu-decode-postscript
2245   "P" gnus-uu-decode-postscript-and-save)
2246
2247 (gnus-define-keys
2248     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2249   "u" gnus-uu-decode-uu-view
2250   "U" gnus-uu-decode-uu-and-save-view
2251   "s" gnus-uu-decode-unshar-view
2252   "S" gnus-uu-decode-unshar-and-save-view
2253   "o" gnus-uu-decode-save-view
2254   "O" gnus-uu-decode-save-view
2255   "b" gnus-uu-decode-binhex-view
2256   "B" gnus-uu-decode-binhex-view
2257   "p" gnus-uu-decode-postscript-view
2258   "P" gnus-uu-decode-postscript-and-save-view)
2259
2260 (defvar gnus-article-post-menu nil)
2261
2262 (defconst gnus-summary-menu-maxlen 20)
2263
2264 (defun gnus-summary-menu-split (menu)
2265   ;; If we have lots of elements, divide them into groups of 20
2266   ;; and make a pane (or submenu) for each one.
2267   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2268       (let ((menu menu) sublists next
2269             (i 1))
2270         (while menu
2271           ;; Pull off the next gnus-summary-menu-maxlen elements
2272           ;; and make them the next element of sublist.
2273           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2274           (if next
2275               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2276                       nil))
2277           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2278                                              (aref (car (last menu)) 0)) menu)
2279                                sublists))
2280           (setq i (1+ i))
2281           (setq menu next))
2282         (nreverse sublists))
2283     ;; Few elements--put them all in one pane.
2284     menu))
2285
2286 (defun gnus-summary-make-menu-bar ()
2287   (gnus-turn-off-edit-menu 'summary)
2288
2289   (unless (boundp 'gnus-summary-misc-menu)
2290
2291     (easy-menu-define
2292       gnus-summary-kill-menu gnus-summary-mode-map ""
2293       (cons
2294        "Score"
2295        (nconc
2296         (list
2297          ["Customize" gnus-score-customize t])
2298         (gnus-make-score-map 'increase)
2299         (gnus-make-score-map 'lower)
2300         '(("Mark"
2301            ["Kill below" gnus-summary-kill-below t]
2302            ["Mark above" gnus-summary-mark-above t]
2303            ["Tick above" gnus-summary-tick-above t]
2304            ["Clear above" gnus-summary-clear-above t])
2305           ["Current score" gnus-summary-current-score t]
2306           ["Set score" gnus-summary-set-score t]
2307           ["Switch current score file..." gnus-score-change-score-file t]
2308           ["Set mark below..." gnus-score-set-mark-below t]
2309           ["Set expunge below..." gnus-score-set-expunge-below t]
2310           ["Edit current score file" gnus-score-edit-current-scores t]
2311           ["Edit score file..." gnus-score-edit-file t]
2312           ["Trace score" gnus-score-find-trace t]
2313           ["Find words" gnus-score-find-favourite-words t]
2314           ["Rescore buffer" gnus-summary-rescore t]
2315           ["Increase score..." gnus-summary-increase-score t]
2316           ["Lower score..." gnus-summary-lower-score t]))))
2317
2318     ;; Define both the Article menu in the summary buffer and the
2319     ;; equivalent Commands menu in the article buffer here for
2320     ;; consistency.
2321     (let ((innards
2322            `(("Hide"
2323               ["All" gnus-article-hide t]
2324               ["Headers" gnus-article-hide-headers t]
2325               ["Signature" gnus-article-hide-signature t]
2326               ["Citation" gnus-article-hide-citation t]
2327               ["List identifiers" gnus-article-hide-list-identifiers t]
2328               ["Banner" gnus-article-strip-banner t]
2329               ["Boring headers" gnus-article-hide-boring-headers t])
2330              ("Highlight"
2331               ["All" gnus-article-highlight t]
2332               ["Headers" gnus-article-highlight-headers t]
2333               ["Signature" gnus-article-highlight-signature t]
2334               ["Citation" gnus-article-highlight-citation t])
2335              ("MIME"
2336               ["Words" gnus-article-decode-mime-words t]
2337               ["Charset" gnus-article-decode-charset t]
2338               ["QP" gnus-article-de-quoted-unreadable t]
2339               ["Base64" gnus-article-de-base64-unreadable t]
2340               ["View MIME buttons" gnus-summary-display-buttonized t]
2341               ["View all" gnus-mime-view-all-parts t]
2342               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2343               ["Encrypt body" gnus-article-encrypt-body
2344                :active (not (gnus-group-read-only-p))
2345                ,@(if (featurep 'xemacs) nil
2346                    '(:help "Encrypt the message body on disk"))]
2347               ["Extract all parts..." gnus-summary-save-parts t]
2348               ("Multipart"
2349                ["Repair multipart" gnus-summary-repair-multipart t]
2350                ["Pipe part..." gnus-article-pipe-part t]
2351                ["Inline part" gnus-article-inline-part t]
2352                ["View part as type..." gnus-article-view-part-as-type t]
2353                ["Encrypt body" gnus-article-encrypt-body
2354                 :active (not (gnus-group-read-only-p))
2355                ,@(if (featurep 'xemacs) nil
2356                    '(:help "Encrypt the message body on disk"))]
2357                ["View part externally" gnus-article-view-part-externally t]
2358                ["View HTML parts in browser" gnus-article-browse-html-article t]
2359                ["View part with charset..." gnus-article-view-part-as-charset t]
2360                ["Copy part" gnus-article-copy-part t]
2361                ["Save part..." gnus-article-save-part t]
2362                ["View part" gnus-article-view-part t]))
2363              ("Date"
2364               ["Local" gnus-article-date-local t]
2365               ["ISO8601" gnus-article-date-iso8601 t]
2366               ["UT" gnus-article-date-ut t]
2367               ["Original" gnus-article-date-original t]
2368               ["Lapsed" gnus-article-date-lapsed t]
2369               ["User-defined" gnus-article-date-user t])
2370              ("Display"
2371               ["Remove images" gnus-article-remove-images t]
2372               ["Toggle smiley" gnus-treat-smiley t]
2373               ["Show X-Face" gnus-article-display-x-face t]
2374               ["Show picons in From" gnus-treat-from-picon t]
2375               ["Show picons in mail headers" gnus-treat-mail-picon t]
2376               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2377               ("View as different encoding"
2378                ,@(gnus-summary-menu-split
2379                   (mapcar
2380                    (lambda (cs)
2381                      ;; Since easymenu under Emacs doesn't allow
2382                      ;; lambda forms for menu commands, we should
2383                      ;; provide intern'ed function symbols.
2384                      (let ((command (intern (format "\
2385 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2386                        (fset command
2387                              `(lambda ()
2388                                 (interactive)
2389                                 (let ((gnus-summary-show-article-charset-alist
2390                                        '((1 . ,cs))))
2391                                   (gnus-summary-show-article 1))))
2392                        `[,(symbol-name cs) ,command t]))
2393                    (sort (if (fboundp 'coding-system-list)
2394                              (coding-system-list)
2395                            (mapcar 'car mm-mime-mule-charset-alist))
2396                          'string<)))))
2397              ("Washing"
2398               ("Remove Blanks"
2399                ["Leading" gnus-article-strip-leading-blank-lines t]
2400                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2401                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2402                ["All of the above" gnus-article-strip-blank-lines t]
2403                ["All" gnus-article-strip-all-blank-lines t]
2404                ["Leading space" gnus-article-strip-leading-space t]
2405                ["Trailing space" gnus-article-strip-trailing-space t]
2406                ["Leading space in headers"
2407                 gnus-article-remove-leading-whitespace t])
2408               ["Overstrike" gnus-article-treat-overstrike t]
2409               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2410               ["Emphasis" gnus-article-emphasize t]
2411               ["Word wrap" gnus-article-fill-cited-article t]
2412               ["Fill long lines" gnus-article-fill-long-lines t]
2413               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2414               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2415               ["Remove CR" gnus-article-remove-cr t]
2416               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2417               ["Base64" gnus-article-de-base64-unreadable t]
2418               ["Rot 13" gnus-summary-caesar-message
2419                ,@(if (featurep 'xemacs) '(t)
2420                    '(:help "\"Caesar rotate\" article by 13"))]
2421               ["De-IDNA" gnus-summary-idna-message t]
2422               ["Morse decode" gnus-summary-morse-message t]
2423               ["Unix pipe..." gnus-summary-pipe-message t]
2424               ["Add buttons" gnus-article-add-buttons t]
2425               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2426               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2427               ["Verbose header" gnus-summary-verbose-headers t]
2428               ["Toggle header" gnus-summary-toggle-header t]
2429               ["Unfold headers" gnus-article-treat-unfold-headers t]
2430               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2431               ["Html" gnus-article-wash-html t]
2432               ["Unsplit URLs" gnus-article-unsplit-urls t]
2433               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2434               ["Decode HZ" gnus-article-decode-HZ t]
2435               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2436               ("(Outlook) Deuglify"
2437                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2438                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2439                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2440                ["Full (Outlook) deuglify"
2441                 gnus-article-outlook-deuglify-article t])
2442               )
2443              ("Output"
2444               ["Save in default format..." gnus-summary-save-article
2445                ,@(if (featurep 'xemacs) '(t)
2446                    '(:help "Save article using default method"))]
2447               ["Save in file..." gnus-summary-save-article-file
2448                ,@(if (featurep 'xemacs) '(t)
2449                    '(:help "Save article in file"))]
2450               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2451               ["Save in MH folder..." gnus-summary-save-article-folder t]
2452               ["Save in VM folder..." gnus-summary-save-article-vm t]
2453               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2454               ["Save body in file..." gnus-summary-save-article-body-file t]
2455               ["Pipe through a filter..." gnus-summary-pipe-output t]
2456               ["Print with Muttprint..." gnus-summary-muttprint t]
2457               ["Print" gnus-summary-print-article
2458                ,@(if (featurep 'xemacs) '(t)
2459                    '(:help "Generate and print a PostScript image"))])
2460              ("Copy, move,... (Backend)"
2461               ,@(if (featurep 'xemacs) nil
2462                   '(:help "Copying, moving, expiring articles..."))
2463               ["Respool article..." gnus-summary-respool-article t]
2464               ["Move article..." gnus-summary-move-article
2465                (gnus-check-backend-function
2466                 'request-move-article gnus-newsgroup-name)]
2467               ["Copy article..." gnus-summary-copy-article t]
2468               ["Crosspost article..." gnus-summary-crosspost-article
2469                (gnus-check-backend-function
2470                 'request-replace-article gnus-newsgroup-name)]
2471               ["Import file..." gnus-summary-import-article
2472                (gnus-check-backend-function
2473                 'request-accept-article gnus-newsgroup-name)]
2474               ["Create article..." gnus-summary-create-article
2475                (gnus-check-backend-function
2476                 'request-accept-article gnus-newsgroup-name)]
2477               ["Check if posted" gnus-summary-article-posted-p t]
2478               ["Edit article" gnus-summary-edit-article
2479                (not (gnus-group-read-only-p))]
2480               ["Delete article" gnus-summary-delete-article
2481                (gnus-check-backend-function
2482                 'request-expire-articles gnus-newsgroup-name)]
2483               ["Query respool" gnus-summary-respool-query t]
2484               ["Trace respool" gnus-summary-respool-trace t]
2485               ["Delete expirable articles" gnus-summary-expire-articles-now
2486                (gnus-check-backend-function
2487                 'request-expire-articles gnus-newsgroup-name)])
2488              ("Extract"
2489               ["Uudecode" gnus-uu-decode-uu
2490                ,@(if (featurep 'xemacs) '(t)
2491                    '(:help "Decode uuencoded article(s)"))]
2492               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2493               ["Unshar" gnus-uu-decode-unshar t]
2494               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2495               ["Save" gnus-uu-decode-save t]
2496               ["Binhex" gnus-uu-decode-binhex t]
2497               ["Postscript" gnus-uu-decode-postscript t]
2498               ["All MIME parts" gnus-summary-save-parts t])
2499              ("Cache"
2500               ["Enter article" gnus-cache-enter-article t]
2501               ["Remove article" gnus-cache-remove-article t])
2502              ["Translate" gnus-article-babel t]
2503              ["Select article buffer" gnus-summary-select-article-buffer t]
2504              ["Make article buffer sticky" gnus-sticky-article t]
2505              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2506              ["Isearch article..." gnus-summary-isearch-article t]
2507              ["Beginning of the article" gnus-summary-beginning-of-article t]
2508              ["End of the article" gnus-summary-end-of-article t]
2509              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2510              ["Fetch referenced articles" gnus-summary-refer-references t]
2511              ["Fetch current thread" gnus-summary-refer-thread t]
2512              ["Fetch article with id..." gnus-summary-refer-article t]
2513              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2514              ["Redisplay" gnus-summary-show-article t]
2515              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2516       (easy-menu-define
2517         gnus-summary-article-menu gnus-summary-mode-map ""
2518         (cons "Article" innards))
2519
2520       (if (not (keymapp gnus-summary-article-menu))
2521           (easy-menu-define
2522             gnus-article-commands-menu gnus-article-mode-map ""
2523             (cons "Commands" innards))
2524         ;; in Emacs, don't share menu.
2525         (setq gnus-article-commands-menu
2526               (copy-keymap gnus-summary-article-menu))
2527         (define-key gnus-article-mode-map [menu-bar commands]
2528           (cons "Commands" gnus-article-commands-menu))))
2529
2530     (easy-menu-define
2531       gnus-summary-thread-menu gnus-summary-mode-map ""
2532       '("Threads"
2533         ["Find all messages in thread" gnus-summary-refer-thread t]
2534         ["Toggle threading" gnus-summary-toggle-threads t]
2535         ["Hide threads" gnus-summary-hide-all-threads t]
2536         ["Show threads" gnus-summary-show-all-threads t]
2537         ["Hide thread" gnus-summary-hide-thread t]
2538         ["Show thread" gnus-summary-show-thread t]
2539         ["Go to next thread" gnus-summary-next-thread t]
2540         ["Go to previous thread" gnus-summary-prev-thread t]
2541         ["Go down thread" gnus-summary-down-thread t]
2542         ["Go up thread" gnus-summary-up-thread t]
2543         ["Top of thread" gnus-summary-top-thread t]
2544         ["Mark thread as read" gnus-summary-kill-thread t]
2545         ["Mark thread as expired" gnus-summary-expire-thread t]
2546         ["Lower thread score" gnus-summary-lower-thread t]
2547         ["Raise thread score" gnus-summary-raise-thread t]
2548         ["Rethread current" gnus-summary-rethread-current t]))
2549
2550     (easy-menu-define
2551       gnus-summary-post-menu gnus-summary-mode-map ""
2552       `("Post"
2553         ["Send a message (mail or news)" gnus-summary-post-news
2554          ,@(if (featurep 'xemacs) '(t)
2555              '(:help "Compose a new message (mail or news)"))]
2556         ["Followup" gnus-summary-followup
2557          ,@(if (featurep 'xemacs) '(t)
2558              '(:help "Post followup to this article"))]
2559         ["Followup and yank" gnus-summary-followup-with-original
2560          ,@(if (featurep 'xemacs) '(t)
2561              '(:help "Post followup to this article, quoting its contents"))]
2562         ["Supersede article" gnus-summary-supersede-article t]
2563         ["Cancel article" gnus-summary-cancel-article
2564          ,@(if (featurep 'xemacs) '(t)
2565              '(:help "Cancel an article you posted"))]
2566         ["Reply" gnus-summary-reply t]
2567         ["Reply and yank" gnus-summary-reply-with-original t]
2568         ["Wide reply" gnus-summary-wide-reply t]
2569         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2570          ,@(if (featurep 'xemacs) '(t)
2571              '(:help "Mail a reply, quoting this article"))]
2572         ["Very wide reply" gnus-summary-very-wide-reply t]
2573         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2574          ,@(if (featurep 'xemacs) '(t)
2575              '(:help "Mail a very wide reply, quoting this article"))]
2576         ["Mail forward" gnus-summary-mail-forward t]
2577         ["Post forward" gnus-summary-post-forward t]
2578         ["Digest and mail" gnus-uu-digest-mail-forward t]
2579         ["Digest and post" gnus-uu-digest-post-forward t]
2580         ["Resend message" gnus-summary-resend-message t]
2581         ["Resend message edit" gnus-summary-resend-message-edit t]
2582         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2583         ["Send a mail" gnus-summary-mail-other-window t]
2584         ["Create a local message" gnus-summary-news-other-window t]
2585         ["Uuencode and post" gnus-uu-post-news
2586          ,@(if (featurep 'xemacs) '(t)
2587              '(:help "Post a uuencoded article"))]
2588         ["Followup via news" gnus-summary-followup-to-mail t]
2589         ["Followup via news and yank"
2590          gnus-summary-followup-to-mail-with-original t]
2591         ["Strip signature on reply"
2592          (lambda ()
2593            (interactive)
2594            (if (not (memq message-cite-function
2595                           '(message-cite-original-without-signature
2596                             message-cite-original)))
2597                ;; Stupid workaround for XEmacs not honoring :visible.
2598                (message "Can't toggle this value of `message-cite-function'")
2599              (setq message-cite-function
2600                    (if (eq message-cite-function
2601                            'message-cite-original-without-signature)
2602                        'message-cite-original
2603                      'message-cite-original-without-signature))))
2604          ;; XEmacs barfs on :visible.
2605          ,@(if (featurep 'xemacs) nil
2606              '(:visible (memq message-cite-function
2607                               '(message-cite-original-without-signature
2608                                 message-cite-original))))
2609          :style toggle
2610          :selected (eq message-cite-function
2611                        'message-cite-original-without-signature)
2612          ,@(if (featurep 'xemacs) nil
2613              '(:help "Strip signature from cited article when replying."))]
2614         ;;("Draft"
2615         ;;["Send" gnus-summary-send-draft t]
2616         ;;["Send bounced" gnus-resend-bounced-mail t])
2617         ))
2618
2619     (cond
2620      ((not (keymapp gnus-summary-post-menu))
2621       (setq gnus-article-post-menu gnus-summary-post-menu))
2622      ((not gnus-article-post-menu)
2623       ;; Don't share post menu.
2624       (setq gnus-article-post-menu
2625             (copy-keymap gnus-summary-post-menu))))
2626     (define-key gnus-article-mode-map [menu-bar post]
2627       (cons "Post" gnus-article-post-menu))
2628
2629     (easy-menu-define
2630       gnus-summary-misc-menu gnus-summary-mode-map ""
2631       `("Gnus"
2632         ("Mark Read"
2633          ["Mark as read" gnus-summary-mark-as-read-forward t]
2634          ["Mark same subject and select"
2635           gnus-summary-kill-same-subject-and-select t]
2636          ["Mark same subject" gnus-summary-kill-same-subject t]
2637          ["Catchup" gnus-summary-catchup
2638           ,@(if (featurep 'xemacs) '(t)
2639               '(:help "Mark unread articles in this group as read"))]
2640          ["Catchup all" gnus-summary-catchup-all t]
2641          ["Catchup to here" gnus-summary-catchup-to-here t]
2642          ["Catchup from here" gnus-summary-catchup-from-here t]
2643          ["Catchup region" gnus-summary-mark-region-as-read
2644           (gnus-mark-active-p)]
2645          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2646         ("Mark Various"
2647          ["Tick" gnus-summary-tick-article-forward t]
2648          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2649          ["Remove marks" gnus-summary-clear-mark-forward t]
2650          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2651          ["Set bookmark" gnus-summary-set-bookmark t]
2652          ["Remove bookmark" gnus-summary-remove-bookmark t])
2653         ("Limit to"
2654          ["Marks..." gnus-summary-limit-to-marks t]
2655          ["Subject..." gnus-summary-limit-to-subject t]
2656          ["Author..." gnus-summary-limit-to-author t]
2657          ["Recipient..." gnus-summary-limit-to-recipient t]
2658          ["Address..." gnus-summary-limit-to-address t]
2659          ["Age..." gnus-summary-limit-to-age t]
2660          ["Extra..." gnus-summary-limit-to-extra t]
2661          ["Score..." gnus-summary-limit-to-score t]
2662          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2663          ["Unread" gnus-summary-limit-to-unread t]
2664          ["Unseen" gnus-summary-limit-to-unseen t]
2665          ["Singletons" gnus-summary-limit-to-singletons t]
2666          ["Replied" gnus-summary-limit-to-replied t]
2667          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2668          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2669          ["Pop limit" gnus-summary-pop-limit t]
2670          ["Show dormant" gnus-summary-limit-include-dormant t]
2671          ["Hide childless dormant"
2672           gnus-summary-limit-exclude-childless-dormant t]
2673          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2674          ["Hide marked" gnus-summary-limit-exclude-marks t]
2675          ["Show expunged" gnus-summary-limit-include-expunged t])
2676         ("Process Mark"
2677          ["Set mark" gnus-summary-mark-as-processable t]
2678          ["Remove mark" gnus-summary-unmark-as-processable t]
2679          ["Remove all marks" gnus-summary-unmark-all-processable t]
2680          ["Invert marks" gnus-uu-invert-processable t]
2681          ["Mark above" gnus-uu-mark-over t]
2682          ["Mark series" gnus-uu-mark-series t]
2683          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2684          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2685          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2686          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2687          ["Mark all" gnus-uu-mark-all t]
2688          ["Mark buffer" gnus-uu-mark-buffer t]
2689          ["Mark sparse" gnus-uu-mark-sparse t]
2690          ["Mark thread" gnus-uu-mark-thread t]
2691          ["Unmark thread" gnus-uu-unmark-thread t]
2692          ("Process Mark Sets"
2693           ["Kill" gnus-summary-kill-process-mark t]
2694           ["Yank" gnus-summary-yank-process-mark
2695            gnus-newsgroup-process-stack]
2696           ["Save" gnus-summary-save-process-mark t]
2697           ["Run command on marked..." gnus-summary-universal-argument t]))
2698         ("Registry Marks")
2699         ("Scroll article"
2700          ["Page forward" gnus-summary-next-page
2701           ,@(if (featurep 'xemacs) '(t)
2702               '(:help "Show next page of article"))]
2703          ["Page backward" gnus-summary-prev-page
2704           ,@(if (featurep 'xemacs) '(t)
2705               '(:help "Show previous page of article"))]
2706          ["Line forward" gnus-summary-scroll-up t])
2707         ("Move"
2708          ["Next unread article" gnus-summary-next-unread-article t]
2709          ["Previous unread article" gnus-summary-prev-unread-article t]
2710          ["Next article" gnus-summary-next-article t]
2711          ["Previous article" gnus-summary-prev-article t]
2712          ["Next unread subject" gnus-summary-next-unread-subject t]
2713          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2714          ["Next article same subject" gnus-summary-next-same-subject t]
2715          ["Previous article same subject" gnus-summary-prev-same-subject t]
2716          ["First unread article" gnus-summary-first-unread-article t]
2717          ["Best unread article" gnus-summary-best-unread-article t]
2718          ["Go to subject number..." gnus-summary-goto-subject t]
2719          ["Go to article number..." gnus-summary-goto-article t]
2720          ["Go to the last article" gnus-summary-goto-last-article t]
2721          ["Pop article off history" gnus-summary-pop-article t])
2722         ("Sort"
2723          ["Sort by number" gnus-summary-sort-by-number t]
2724          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2725          ["Sort by author" gnus-summary-sort-by-author t]
2726          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2727          ["Sort by subject" gnus-summary-sort-by-subject t]
2728          ["Sort by date" gnus-summary-sort-by-date t]
2729          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2730          ["Sort by score" gnus-summary-sort-by-score t]
2731          ["Sort by lines" gnus-summary-sort-by-lines t]
2732          ["Sort by characters" gnus-summary-sort-by-chars t]
2733          ["Randomize" gnus-summary-sort-by-random t]
2734          ["Original sort" gnus-summary-sort-by-original t])
2735         ("Help"
2736          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2737          ["Describe group" gnus-summary-describe-group t]
2738          ["Fetch charter" gnus-group-fetch-charter
2739           ,@(if (featurep 'xemacs) nil
2740               '(:help "Display the charter of the current group"))]
2741          ["Fetch control message" gnus-group-fetch-control
2742           ,@(if (featurep 'xemacs) nil
2743               '(:help "Display the archived control message for the current group"))]
2744          ["Read manual" gnus-info-find-node t])
2745         ("Modes"
2746          ["Pick and read" gnus-pick-mode t]
2747          ["Binary" gnus-binary-mode t])
2748         ("Regeneration"
2749          ["Regenerate" gnus-summary-prepare t]
2750          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2751          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2752          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2753          ["Toggle threading" gnus-summary-toggle-threads t])
2754         ["See old articles" gnus-summary-insert-old-articles t]
2755         ["See new articles" gnus-summary-insert-new-articles t]
2756         ["Filter articles..." gnus-summary-execute-command t]
2757         ["Run command on articles..." gnus-summary-universal-argument t]
2758         ["Search articles forward..." gnus-summary-search-article-forward t]
2759         ["Search articles backward..." gnus-summary-search-article-backward t]
2760         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2761         ["Expand window" gnus-summary-expand-window t]
2762         ["Expire expirable articles" gnus-summary-expire-articles
2763          (gnus-check-backend-function
2764           'request-expire-articles gnus-newsgroup-name)]
2765         ["Edit local kill file" gnus-summary-edit-local-kill t]
2766         ["Edit main kill file" gnus-summary-edit-global-kill t]
2767         ["Edit group parameters" gnus-summary-edit-parameters t]
2768         ["Customize group parameters" gnus-summary-customize-parameters t]
2769         ["Send a bug report" gnus-bug t]
2770         ("Exit"
2771          ["Catchup and exit" gnus-summary-catchup-and-exit
2772           ,@(if (featurep 'xemacs) '(t)
2773               '(:help "Mark unread articles in this group as read, then exit"))]
2774          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2775          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2776          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2777          ["Exit group" gnus-summary-exit
2778           ,@(if (featurep 'xemacs) '(t)
2779               '(:help "Exit current group, return to group selection mode"))]
2780          ["Exit group without updating" gnus-summary-exit-no-update t]
2781          ["Exit and goto next group" gnus-summary-next-group t]
2782          ["Exit and goto prev group" gnus-summary-prev-group t]
2783          ["Reselect group" gnus-summary-reselect-current-group t]
2784          ["Rescan group" gnus-summary-rescan-group t]
2785          ["Update dribble" gnus-summary-save-newsrc t])))
2786
2787     (gnus-run-hooks 'gnus-summary-menu-hook)))
2788
2789 (defvar gnus-summary-tool-bar-map nil)
2790
2791 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2792 ;; affect _new_ message buffers.  We might add a function that walks thru all
2793 ;; summary-mode buffers and force the update.
2794 (defun gnus-summary-tool-bar-update (&optional symbol value)
2795   "Update summary mode toolbar.
2796 Setter function for custom variables."
2797   (setq-default gnus-summary-tool-bar-map nil)
2798   (when symbol
2799     ;; When used as ":set" function:
2800     (set-default symbol value))
2801   (when (gnus-buffer-live-p gnus-summary-buffer)
2802     (with-current-buffer gnus-summary-buffer
2803       (gnus-summary-make-tool-bar))))
2804
2805 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2806                                      'gnus-summary-tool-bar-gnome
2807                                    'gnus-summary-tool-bar-retro)
2808   "Specifies the Gnus summary tool bar.
2809
2810 It can be either a list or a symbol refering to a list.  See
2811 `gmm-tool-bar-from-list' for the format of the list.  The
2812 default key map is `gnus-summary-mode-map'.
2813
2814 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2815 `gnus-summary-tool-bar-retro'."
2816   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2817                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2818                  (repeat :tag "User defined list" gmm-tool-bar-item)
2819                  (symbol))
2820   :version "23.1" ;; No Gnus
2821   :initialize 'custom-initialize-default
2822   :set 'gnus-summary-tool-bar-update
2823   :group 'gnus-summary)
2824
2825 (defcustom gnus-summary-tool-bar-gnome
2826   '((gnus-summary-post-news "mail/compose" nil)
2827     (gnus-summary-insert-new-articles "mail/inbox" nil
2828                                       :visible (or (not gnus-agent)
2829                                                    gnus-plugged))
2830     (gnus-summary-reply-with-original "mail/reply")
2831     (gnus-summary-reply "mail/reply" nil :visible nil)
2832     (gnus-summary-followup-with-original "mail/reply-all")
2833     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2834     (gnus-summary-mail-forward "mail/forward")
2835     (gnus-summary-save-article "mail/save")
2836     (gnus-summary-search-article-forward "search" nil :visible nil)
2837     (gnus-summary-print-article "print")
2838     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2839     ;; Some new commands that may need more suitable icons:
2840     (gnus-summary-save-newsrc "save" nil :visible nil)
2841     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2842     (gnus-summary-prev-article "left-arrow")
2843     (gnus-summary-next-article "right-arrow")
2844     (gnus-summary-next-page "next-page")
2845     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2846     ;;
2847     ;; Maybe some sort-by-... could be added:
2848     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2849     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2850     (gnus-summary-mark-as-expirable
2851      "delete" nil
2852      :visible (gnus-check-backend-function 'request-expire-articles
2853                                            gnus-newsgroup-name))
2854     (gnus-summary-mark-as-spam
2855      "mail/spam" t
2856      :visible (and (fboundp 'spam-group-ham-contents-p)
2857                    (spam-group-ham-contents-p gnus-newsgroup-name))
2858      :help "Mark as spam")
2859     (gnus-summary-mark-as-read-forward
2860      "mail/not-spam" nil
2861      :visible (and (fboundp 'spam-group-spam-contents-p)
2862                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2863     ;;
2864     (gnus-summary-exit "exit")
2865     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2866     (gnus-info-find-node "help"))
2867   "List of functions for the summary tool bar (GNOME style).
2868
2869 See `gmm-tool-bar-from-list' for the format of the list."
2870   :type '(repeat gmm-tool-bar-item)
2871   :version "23.1" ;; No Gnus
2872   :initialize 'custom-initialize-default
2873   :set 'gnus-summary-tool-bar-update
2874   :group 'gnus-summary)
2875
2876 (defcustom gnus-summary-tool-bar-retro
2877   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2878     (gnus-summary-next-unread-article "gnus/next-ur")
2879     (gnus-summary-post-news "gnus/post")
2880     (gnus-summary-followup-with-original "gnus/fuwo")
2881     (gnus-summary-followup "gnus/followup")
2882     (gnus-summary-reply-with-original "gnus/reply-wo")
2883     (gnus-summary-reply "gnus/reply")
2884     (gnus-summary-caesar-message "gnus/rot13")
2885     (gnus-uu-decode-uu "gnus/uu-decode")
2886     (gnus-summary-save-article-file "gnus/save-aif")
2887     (gnus-summary-save-article "gnus/save-art")
2888     (gnus-uu-post-news "gnus/uu-post")
2889     (gnus-summary-catchup "gnus/catchup")
2890     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2891     (gnus-summary-exit "gnus/exit-summ")
2892     ;; Some new command that may need more suitable icons:
2893     (gnus-summary-print-article "gnus/print" nil :visible nil)
2894     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2895     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2896     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2897     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2898     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2899     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2900     ;;
2901     (gnus-info-find-node "gnus/help" nil :visible nil))
2902   "List of functions for the summary tool bar (retro look).
2903
2904 See `gmm-tool-bar-from-list' for the format of the list."
2905   :type '(repeat gmm-tool-bar-item)
2906   :version "23.1" ;; No Gnus
2907   :initialize 'custom-initialize-default
2908   :set 'gnus-summary-tool-bar-update
2909   :group 'gnus-summary)
2910
2911 (defcustom gnus-summary-tool-bar-zap-list t
2912   "List of icon items from the global tool bar.
2913 These items are not displayed in the Gnus summary mode tool bar.
2914
2915 See `gmm-tool-bar-from-list' for the format of the list."
2916   :type 'gmm-tool-bar-zap-list
2917   :version "23.1" ;; No Gnus
2918   :initialize 'custom-initialize-default
2919   :set 'gnus-summary-tool-bar-update
2920   :group 'gnus-summary)
2921
2922 (defvar image-load-path)
2923 (defvar tool-bar-map)
2924
2925 (defun gnus-summary-make-tool-bar (&optional force)
2926   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2927 When FORCE, rebuild the tool bar."
2928   (when (and (not (featurep 'xemacs))
2929              (boundp 'tool-bar-mode)
2930              tool-bar-mode
2931              (or (not gnus-summary-tool-bar-map) force))
2932     (let* ((load-path
2933             (gmm-image-load-path-for-library "gnus"
2934                                              "mail/save.xpm"
2935                                              nil t))
2936            (image-load-path (cons (car load-path)
2937                                   (when (boundp 'image-load-path)
2938                                     image-load-path)))
2939            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2940                                         gnus-summary-tool-bar-zap-list
2941                                         'gnus-summary-mode-map)))
2942       (when map
2943         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2944         ;; uses it's value.
2945         (setq gnus-summary-tool-bar-map map))))
2946   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2947
2948 (defun gnus-score-set-default (var value)
2949   "A version of set that updates the GNU Emacs menu-bar."
2950   (set var value)
2951   ;; It is the message that forces the active status to be updated.
2952   (message ""))
2953
2954 (defun gnus-make-score-map (type)
2955   "Make a summary score map of type TYPE."
2956   (if t
2957       nil
2958     (let ((headers '(("author" "from" string)
2959                      ("subject" "subject" string)
2960                      ("article body" "body" string)
2961                      ("article head" "head" string)
2962                      ("xref" "xref" string)
2963                      ("extra header" "extra" string)
2964                      ("lines" "lines" number)
2965                      ("followups to author" "followup" string)))
2966           (types '((number ("less than" <)
2967                            ("greater than" >)
2968                            ("equal" =))
2969                    (string ("substring" s)
2970                            ("exact string" e)
2971                            ("fuzzy string" f)
2972                            ("regexp" r))))
2973           (perms '(("temporary" (current-time-string))
2974                    ("permanent" nil)
2975                    ("immediate" now)))
2976           header)
2977       (list
2978        (apply
2979         'nconc
2980         (list
2981          (if (eq type 'lower)
2982              "Lower score"
2983            "Increase score"))
2984         (let (outh)
2985           (while headers
2986             (setq header (car headers))
2987             (setq outh
2988                   (cons
2989                    (apply
2990                     'nconc
2991                     (list (car header))
2992                     (let ((ts (cdr (assoc (nth 2 header) types)))
2993                           outt)
2994                       (while ts
2995                         (setq outt
2996                               (cons
2997                                (apply
2998                                 'nconc
2999                                 (list (caar ts))
3000                                 (let ((ps perms)
3001                                       outp)
3002                                   (while ps
3003                                     (setq outp
3004                                           (cons
3005                                            (vector
3006                                             (caar ps)
3007                                             (list
3008                                              'gnus-summary-score-entry
3009                                              (nth 1 header)
3010                                              (if (or (string= (nth 1 header)
3011                                                               "head")
3012                                                      (string= (nth 1 header)
3013                                                               "body"))
3014                                                  ""
3015                                                (list 'gnus-summary-header
3016                                                      (nth 1 header)))
3017                                              (list 'quote (nth 1 (car ts)))
3018                                              (list 'gnus-score-delta-default
3019                                                    nil)
3020                                              (nth 1 (car ps))
3021                                              t)
3022                                             t)
3023                                            outp))
3024                                     (setq ps (cdr ps)))
3025                                   (list (nreverse outp))))
3026                                outt))
3027                         (setq ts (cdr ts)))
3028                       (list (nreverse outt))))
3029                    outh))
3030             (setq headers (cdr headers)))
3031           (list (nreverse outh))))))))
3032
3033
3034 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3035 (defvar bookmark-make-record-function)
3036 \f
3037
3038 (defun gnus-summary-mode (&optional group)
3039   "Major mode for reading articles.
3040
3041 All normal editing commands are switched off.
3042 \\<gnus-summary-mode-map>
3043 Each line in this buffer represents one article.  To read an
3044 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3045 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3046 respectively.
3047
3048 You can also post articles and send mail from this buffer.  To
3049 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3050 of an article, type `\\[gnus-summary-reply]'.
3051
3052 There are approx. one gazillion commands you can execute in this
3053 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3054
3055 The following commands are available:
3056
3057 \\{gnus-summary-mode-map}"
3058   (interactive)
3059   (kill-all-local-variables)
3060   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3061     (gnus-summary-make-local-variables))
3062   (gnus-summary-make-local-variables)
3063   (setq gnus-newsgroup-name group)
3064   (when (gnus-visual-p 'summary-menu 'menu)
3065     (gnus-summary-make-menu-bar)
3066     (gnus-summary-make-tool-bar))
3067   (gnus-make-thread-indent-array)
3068   (gnus-simplify-mode-line)
3069   (setq major-mode 'gnus-summary-mode)
3070   (setq mode-name "Summary")
3071   (use-local-map gnus-summary-mode-map)
3072   (buffer-disable-undo)
3073   (setq buffer-read-only t              ;Disable modification
3074         show-trailing-whitespace nil)
3075   (setq truncate-lines t)
3076   (add-to-invisibility-spec '(gnus-sum . t))
3077   (gnus-summary-set-display-table)
3078   (gnus-set-default-directory)
3079   (make-local-variable 'gnus-summary-line-format)
3080   (make-local-variable 'gnus-summary-line-format-spec)
3081   (make-local-variable 'gnus-summary-dummy-line-format)
3082   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3083   (make-local-variable 'gnus-summary-mark-positions)
3084   (gnus-make-local-hook 'pre-command-hook)
3085   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3086   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3087   (turn-on-gnus-mailing-list-mode)
3088   (mm-enable-multibyte)
3089   (set (make-local-variable 'bookmark-make-record-function)
3090        'gnus-summary-bookmark-make-record)
3091   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3092   (gnus-update-summary-mark-positions))
3093
3094 (defun gnus-summary-make-local-variables ()
3095   "Make all the local summary buffer variables."
3096   (let (global)
3097     (dolist (local gnus-summary-local-variables)
3098       (if (consp local)
3099           (progn
3100             (if (eq (cdr local) 'global)
3101                 ;; Copy the global value of the variable.
3102                 (setq global (symbol-value (car local)))
3103               ;; Use the value from the list.
3104               (setq global (eval (cdr local))))
3105             (set (make-local-variable (car local)) global))
3106         ;; Simple nil-valued local variable.
3107         (set (make-local-variable local) nil)))))
3108
3109 (defun gnus-summary-clear-local-variables ()
3110   (let ((locals gnus-summary-local-variables))
3111     (while locals
3112       (if (consp (car locals))
3113           (and (symbolp (caar locals))
3114                (set (caar locals) nil))
3115         (and (symbolp (car locals))
3116              (set (car locals) nil)))
3117       (setq locals (cdr locals)))))
3118
3119 ;; Summary data functions.
3120
3121 (defmacro gnus-data-number (data)
3122   `(car ,data))
3123
3124 (defmacro gnus-data-set-number (data number)
3125   `(setcar ,data ,number))
3126
3127 (defmacro gnus-data-mark (data)
3128   `(nth 1 ,data))
3129
3130 (defmacro gnus-data-set-mark (data mark)
3131   `(setcar (nthcdr 1 ,data) ,mark))
3132
3133 (defmacro gnus-data-pos (data)
3134   `(nth 2 ,data))
3135
3136 (defmacro gnus-data-set-pos (data pos)
3137   `(setcar (nthcdr 2 ,data) ,pos))
3138
3139 (defmacro gnus-data-header (data)
3140   `(nth 3 ,data))
3141
3142 (defmacro gnus-data-set-header (data header)
3143   `(setf (nth 3 ,data) ,header))
3144
3145 (defmacro gnus-data-level (data)
3146   `(nth 4 ,data))
3147
3148 (defmacro gnus-data-unread-p (data)
3149   `(= (nth 1 ,data) gnus-unread-mark))
3150
3151 (defmacro gnus-data-read-p (data)
3152   `(/= (nth 1 ,data) gnus-unread-mark))
3153
3154 (defmacro gnus-data-pseudo-p (data)
3155   `(consp (nth 3 ,data)))
3156
3157 (defmacro gnus-data-find (number)
3158   `(assq ,number gnus-newsgroup-data))
3159
3160 (defmacro gnus-data-find-list (number &optional data)
3161   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3162      (memq (assq ,number bdata)
3163            bdata)))
3164
3165 (defmacro gnus-data-make (number mark pos header level)
3166   `(list ,number ,mark ,pos ,header ,level))
3167
3168 (defun gnus-data-enter (after-article number mark pos header level offset)
3169   (let ((data (gnus-data-find-list after-article)))
3170     (unless data
3171       (error "No such article: %d" after-article))
3172     (setcdr data (cons (gnus-data-make number mark pos header level)
3173                        (cdr data)))
3174     (setq gnus-newsgroup-data-reverse nil)
3175     (gnus-data-update-list (cddr data) offset)))
3176
3177 (defun gnus-data-enter-list (after-article list &optional offset)
3178   (when list
3179     (let ((data (and after-article (gnus-data-find-list after-article)))
3180           (ilist list))
3181       (if (not (or data
3182                    after-article))
3183           (let ((odata gnus-newsgroup-data))
3184             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3185             (when offset
3186               (gnus-data-update-list odata offset)))
3187         ;; Find the last element in the list to be spliced into the main
3188         ;; list.
3189         (setq list (last list))
3190         (if (not data)
3191             (progn
3192               (setcdr list gnus-newsgroup-data)
3193               (setq gnus-newsgroup-data ilist)
3194               (when offset
3195                 (gnus-data-update-list (cdr list) offset)))
3196           (setcdr list (cdr data))
3197           (setcdr data ilist)
3198           (when offset
3199             (gnus-data-update-list (cdr list) offset))))
3200       (setq gnus-newsgroup-data-reverse nil))))
3201
3202 (defun gnus-data-remove (article &optional offset)
3203   (let ((data gnus-newsgroup-data))
3204     (if (= (gnus-data-number (car data)) article)
3205         (progn
3206           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3207                 gnus-newsgroup-data-reverse nil)
3208           (when offset
3209             (gnus-data-update-list gnus-newsgroup-data offset)))
3210       (while (cdr data)
3211         (when (= (gnus-data-number (cadr data)) article)
3212           (setcdr data (cddr data))
3213           (when offset
3214             (gnus-data-update-list (cdr data) offset))
3215           (setq data nil
3216                 gnus-newsgroup-data-reverse nil))
3217         (setq data (cdr data))))))
3218
3219 (defmacro gnus-data-list (backward)
3220   `(if ,backward
3221        (or gnus-newsgroup-data-reverse
3222            (setq gnus-newsgroup-data-reverse
3223                  (reverse gnus-newsgroup-data)))
3224      gnus-newsgroup-data))
3225
3226 (defun gnus-data-update-list (data offset)
3227   "Add OFFSET to the POS of all data entries in DATA."
3228   (setq gnus-newsgroup-data-reverse nil)
3229   (while data
3230     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3231     (setq data (cdr data))))
3232
3233 (defun gnus-summary-article-pseudo-p (article)
3234   "Say whether this article is a pseudo article or not."
3235   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3236
3237 (defmacro gnus-summary-article-sparse-p (article)
3238   "Say whether this article is a sparse article or not."
3239   `(memq ,article gnus-newsgroup-sparse))
3240
3241 (defmacro gnus-summary-article-ancient-p (article)
3242   "Say whether this article is a sparse article or not."
3243   `(memq ,article gnus-newsgroup-ancient))
3244
3245 (defun gnus-article-parent-p (number)
3246   "Say whether this article is a parent or not."
3247   (let ((data (gnus-data-find-list number)))
3248     (and (cdr data)              ; There has to be an article after...
3249          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3250             (gnus-data-level (nth 1 data))))))
3251
3252 (defun gnus-article-children (number)
3253   "Return a list of all children to NUMBER."
3254   (let* ((data (gnus-data-find-list number))
3255          (level (gnus-data-level (car data)))
3256          children)
3257     (setq data (cdr data))
3258     (while (and data
3259                 (= (gnus-data-level (car data)) (1+ level)))
3260       (push (gnus-data-number (car data)) children)
3261       (setq data (cdr data)))
3262     children))
3263
3264 (defmacro gnus-summary-skip-intangible ()
3265   "If the current article is intangible, then jump to a different article."
3266   '(let ((to (get-text-property (point) 'gnus-intangible)))
3267      (and to (gnus-summary-goto-subject to))))
3268
3269 (defmacro gnus-summary-article-intangible-p ()
3270   "Say whether this article is intangible or not."
3271   '(get-text-property (point) 'gnus-intangible))
3272
3273 (defun gnus-article-read-p (article)
3274   "Say whether ARTICLE is read or not."
3275   (not (or (memq article gnus-newsgroup-marked)
3276            (memq article gnus-newsgroup-spam-marked)
3277            (memq article gnus-newsgroup-unreads)
3278            (memq article gnus-newsgroup-unselected)
3279            (memq article gnus-newsgroup-dormant))))
3280
3281 ;; Some summary mode macros.
3282
3283 (defmacro gnus-summary-article-number ()
3284   "The article number of the article on the current line.
3285 If there isn't an article number here, then we return the current
3286 article number."
3287   '(progn
3288      (gnus-summary-skip-intangible)
3289      (or (get-text-property (point) 'gnus-number)
3290          (gnus-summary-last-subject))))
3291
3292 (defmacro gnus-summary-article-header (&optional number)
3293   "Return the header of article NUMBER."
3294   `(gnus-data-header (gnus-data-find
3295                       ,(or number '(gnus-summary-article-number)))))
3296
3297 (defmacro gnus-summary-thread-level (&optional number)
3298   "Return the level of thread that starts with article NUMBER."
3299   `(if (and (eq gnus-summary-make-false-root 'dummy)
3300             (get-text-property (point) 'gnus-intangible))
3301        0
3302      (gnus-data-level (gnus-data-find
3303                        ,(or number '(gnus-summary-article-number))))))
3304
3305 (defmacro gnus-summary-article-mark (&optional number)
3306   "Return the mark of article NUMBER."
3307   `(gnus-data-mark (gnus-data-find
3308                     ,(or number '(gnus-summary-article-number)))))
3309
3310 (defmacro gnus-summary-article-pos (&optional number)
3311   "Return the position of the line of article NUMBER."
3312   `(gnus-data-pos (gnus-data-find
3313                    ,(or number '(gnus-summary-article-number)))))
3314
3315 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3316 (defmacro gnus-summary-article-subject (&optional number)
3317   "Return current subject string or nil if nothing."
3318   `(let ((headers
3319           ,(if number
3320                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3321              '(gnus-data-header (assq (gnus-summary-article-number)
3322                                       gnus-newsgroup-data)))))
3323      (and headers
3324           (vectorp headers)
3325           (mail-header-subject headers))))
3326
3327 (defmacro gnus-summary-article-score (&optional number)
3328   "Return current article score."
3329   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3330                   gnus-newsgroup-scored))
3331        gnus-summary-default-score 0))
3332
3333 (defun gnus-summary-article-children (&optional number)
3334   "Return a list of article numbers that are children of article NUMBER."
3335   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3336          (level (gnus-data-level (car data)))
3337          l children)
3338     (while (and (setq data (cdr data))
3339                 (> (setq l (gnus-data-level (car data))) level))
3340       (and (= (1+ level) l)
3341            (push (gnus-data-number (car data))
3342                  children)))
3343     (nreverse children)))
3344
3345 (defun gnus-summary-article-parent (&optional number)
3346   "Return the article number of the parent of article NUMBER."
3347   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3348                                     (gnus-data-list t)))
3349          (level (gnus-data-level (car data))))
3350     (if (zerop level)
3351         ()                              ; This is a root.
3352       ;; We search until we find an article with a level less than
3353       ;; this one.  That function has to be the parent.
3354       (while (and (setq data (cdr data))
3355                   (not (< (gnus-data-level (car data)) level))))
3356       (and data (gnus-data-number (car data))))))
3357
3358 (defun gnus-unread-mark-p (mark)
3359   "Say whether MARK is the unread mark."
3360   (= mark gnus-unread-mark))
3361
3362 (defun gnus-read-mark-p (mark)
3363   "Say whether MARK is one of the marks that mark as read.
3364 This is all marks except unread, ticked, dormant, and expirable."
3365   (not (or (= mark gnus-unread-mark)
3366            (= mark gnus-ticked-mark)
3367            (= mark gnus-spam-mark)
3368            (= mark gnus-dormant-mark)
3369            (= mark gnus-expirable-mark))))
3370
3371 (defmacro gnus-article-mark (number)
3372   "Return the MARK of article NUMBER.
3373 This macro should only be used when computing the mark the \"first\"
3374 time; i.e., when generating the summary lines.  After that,
3375 `gnus-summary-article-mark' should be used to examine the
3376 marks of articles."
3377   `(cond
3378     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3379     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3380     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3381     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3382     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3383     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3384     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3385     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3386            gnus-ancient-mark))))
3387
3388 ;; Saving hidden threads.
3389
3390 (defmacro gnus-save-hidden-threads (&rest forms)
3391   "Save hidden threads, eval FORMS, and restore the hidden threads."
3392   (let ((config (make-symbol "config")))
3393     `(let ((,config (gnus-hidden-threads-configuration)))
3394        (unwind-protect
3395            (save-excursion
3396              ,@forms)
3397          (gnus-restore-hidden-threads-configuration ,config)))))
3398 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3399 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3400
3401 (defun gnus-data-compute-positions ()
3402   "Compute the positions of all articles."
3403   (setq gnus-newsgroup-data-reverse nil)
3404   (let ((data gnus-newsgroup-data))
3405     (save-excursion
3406       (gnus-save-hidden-threads
3407         (gnus-summary-show-all-threads)
3408         (goto-char (point-min))
3409         (while data
3410           (while (get-text-property (point) 'gnus-intangible)
3411             (forward-line 1))
3412           (gnus-data-set-pos (car data) (+ (point) 3))
3413           (setq data (cdr data))
3414           (forward-line 1))))))
3415
3416 (defun gnus-hidden-threads-configuration ()
3417   "Return the current hidden threads configuration."
3418   (save-excursion
3419     (let (config)
3420       (goto-char (point-min))
3421       (while (not (eobp))
3422         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3423           (push (save-excursion (forward-line 0) (point)) config))
3424         (forward-line 1))
3425       config)))
3426
3427 (defun gnus-restore-hidden-threads-configuration (config)
3428   "Restore hidden threads configuration from CONFIG."
3429   (save-excursion
3430     (let (point (inhibit-read-only t))
3431       (while (setq point (pop config))
3432         (goto-char point)
3433         (gnus-summary-hide-thread)))))
3434
3435 ;; Various summary mode internalish functions.
3436
3437 (defun gnus-mouse-pick-article (e)
3438   (interactive "e")
3439   (mouse-set-point e)
3440   (gnus-summary-next-page nil t))
3441
3442 (defun gnus-summary-set-display-table ()
3443   "Change the display table.
3444 Odd characters have a tendency to mess
3445 up nicely formatted displays - we make all possible glyphs
3446 display only a single character."
3447
3448   ;; We start from the standard display table, if any.
3449   (let ((table (or (copy-sequence standard-display-table)
3450                    (make-display-table)))
3451         (i 32))
3452     ;; Nix out all the control chars...
3453     (while (>= (setq i (1- i)) 0)
3454       (gnus-put-display-table i [??] table))
3455    ;; ... but not newline and cr, of course.  (cr is necessary for the
3456     ;; selective display).
3457     (gnus-put-display-table ?\n nil table)
3458     (gnus-put-display-table ?\r nil table)
3459     ;; We keep TAB as well.
3460     (gnus-put-display-table ?\t nil table)
3461     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3462     ;; Emacs 23 (unicode), that are not set already.
3463     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3464                  160
3465                256)))
3466       (while (>= (setq i (1- i)) 127)
3467         ;; Only modify if the entry is nil.
3468         (unless (gnus-get-display-table i table)
3469           (gnus-put-display-table i [??] table))))
3470     (setq buffer-display-table table)))
3471
3472 (defun gnus-summary-set-article-display-arrow (pos)
3473   "Update the overlay arrow to point to line at position POS."
3474   (when gnus-summary-display-arrow
3475     (make-local-variable 'overlay-arrow-position)
3476     (make-local-variable 'overlay-arrow-string)
3477     (save-excursion
3478       (goto-char pos)
3479       (beginning-of-line)
3480       (unless overlay-arrow-position
3481         (setq overlay-arrow-position (make-marker)))
3482       (setq overlay-arrow-string "=>"
3483             overlay-arrow-position (set-marker overlay-arrow-position
3484                                                (point)
3485                                                (current-buffer))))))
3486
3487 (defun gnus-summary-setup-buffer (group)
3488   "Initialize summary buffer."
3489   (let ((buffer (gnus-summary-buffer-name group))
3490         (dead-name (concat "*Dead Summary "
3491                            (gnus-group-decoded-name group) "*")))
3492     ;; If a dead summary buffer exists, we kill it.
3493     (when (gnus-buffer-live-p dead-name)
3494       (gnus-kill-buffer dead-name))
3495     (if (get-buffer buffer)
3496         (progn
3497           (set-buffer buffer)
3498           (setq gnus-summary-buffer (current-buffer))
3499           (not gnus-newsgroup-prepared))
3500       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3501       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3502       (gnus-summary-mode group)
3503       (when gnus-carpal
3504         (gnus-carpal-setup-buffer 'summary))
3505       (when (gnus-group-quit-config group)
3506         (set (make-local-variable 'gnus-single-article-buffer) nil))
3507       (make-local-variable 'gnus-article-buffer)
3508       (make-local-variable 'gnus-article-current)
3509       (make-local-variable 'gnus-original-article-buffer)
3510       (setq gnus-newsgroup-name group)
3511       ;; Set any local variables in the group parameters.
3512       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3513       t)))
3514
3515 (defun gnus-set-global-variables ()
3516   "Set the global equivalents of the buffer-local variables.
3517 They are set to the latest values they had.  These reflect the summary
3518 buffer that was in action when the last article was fetched."
3519   (when (eq major-mode 'gnus-summary-mode)
3520     (setq gnus-summary-buffer (current-buffer))
3521     (let ((name gnus-newsgroup-name)
3522           (marked gnus-newsgroup-marked)
3523           (spam gnus-newsgroup-spam-marked)
3524           (unread gnus-newsgroup-unreads)
3525           (headers gnus-current-headers)
3526           (data gnus-newsgroup-data)
3527           (summary gnus-summary-buffer)
3528           (article-buffer gnus-article-buffer)
3529           (original gnus-original-article-buffer)
3530           (gac gnus-article-current)
3531           (reffed gnus-reffed-article-number)
3532           (score-file gnus-current-score-file)
3533           (default-charset gnus-newsgroup-charset)
3534           vlist)
3535       (let ((locals gnus-newsgroup-variables))
3536         (while locals
3537           (if (consp (car locals))
3538               (push (eval (caar locals)) vlist)
3539             (push (eval (car locals)) vlist))
3540           (setq locals (cdr locals)))
3541         (setq vlist (nreverse vlist)))
3542       (with-current-buffer gnus-group-buffer
3543         (setq gnus-newsgroup-name name
3544               gnus-newsgroup-marked marked
3545               gnus-newsgroup-spam-marked spam
3546               gnus-newsgroup-unreads unread
3547               gnus-current-headers headers
3548               gnus-newsgroup-data data
3549               gnus-article-current gac
3550               gnus-summary-buffer summary
3551               gnus-article-buffer article-buffer
3552               gnus-original-article-buffer original
3553               gnus-reffed-article-number reffed
3554               gnus-current-score-file score-file
3555               gnus-newsgroup-charset default-charset)
3556         (let ((locals gnus-newsgroup-variables))
3557           (while locals
3558             (if (consp (car locals))
3559                 (set (caar locals) (pop vlist))
3560               (set (car locals) (pop vlist)))
3561             (setq locals (cdr locals))))
3562         ;; The article buffer also has local variables.
3563         (when (gnus-buffer-live-p gnus-article-buffer)
3564           (set-buffer gnus-article-buffer)
3565           (setq gnus-summary-buffer summary))))))
3566
3567 (defun gnus-summary-article-unread-p (article)
3568   "Say whether ARTICLE is unread or not."
3569   (memq article gnus-newsgroup-unreads))
3570
3571 (defun gnus-summary-first-article-p (&optional article)
3572   "Return whether ARTICLE is the first article in the buffer."
3573   (if (not (setq article (or article (gnus-summary-article-number))))
3574       nil
3575     (eq article (caar gnus-newsgroup-data))))
3576
3577 (defun gnus-summary-last-article-p (&optional article)
3578   "Return whether ARTICLE is the last article in the buffer."
3579   (if (not (setq article (or article (gnus-summary-article-number))))
3580       ;; All non-existent numbers are the last article.  :-)
3581       t
3582     (not (cdr (gnus-data-find-list article)))))
3583
3584 (defun gnus-make-thread-indent-array (&optional n)
3585   (when (or n
3586             (progn (setq n 200) nil)
3587             (null gnus-thread-indent-array)
3588             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3589     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3590           gnus-thread-indent-array-level gnus-thread-indent-level)
3591     (while (>= n 0)
3592       (aset gnus-thread-indent-array n
3593             (make-string (* n gnus-thread-indent-level) ? ))
3594       (setq n (1- n)))))
3595
3596 (defun gnus-update-summary-mark-positions ()
3597   "Compute where the summary marks are to go."
3598   (save-excursion
3599     (when (gnus-buffer-exists-p gnus-summary-buffer)
3600       (set-buffer gnus-summary-buffer))
3601     (let ((spec gnus-summary-line-format-spec)
3602           pos)
3603       (save-excursion
3604         (gnus-set-work-buffer)
3605         (let ((gnus-tmp-unread ?Z)
3606               (gnus-replied-mark ?Z)
3607               (gnus-score-below-mark ?Z)
3608               (gnus-score-over-mark ?Z)
3609               (gnus-undownloaded-mark ?Z)
3610               (gnus-summary-line-format-spec spec)
3611               (gnus-newsgroup-downloadable '(0))
3612               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3613               case-fold-search ignores)
3614           ;; Here, all marks are bound to Z.
3615           (gnus-summary-insert-line header
3616                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3617           (goto-char (point-min))
3618           ;; Memorize the positions of the same characters as dummy marks.
3619           (while (re-search-forward "[A-D]" nil t)
3620             (push (point) ignores))
3621           (erase-buffer)
3622           ;; We use A-D as dummy marks in order to know column positions
3623           ;; where marks should be inserted.
3624           (setq gnus-tmp-unread ?A
3625                 gnus-replied-mark ?B
3626                 gnus-score-below-mark ?C
3627                 gnus-score-over-mark ?C
3628                 gnus-undownloaded-mark ?D)
3629           (gnus-summary-insert-line header
3630                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3631           ;; Ignore characters which aren't dummy marks.
3632           (dolist (p ignores)
3633             (delete-region (goto-char (1- p)) p)
3634             (insert ?Z))
3635           (goto-char (point-min))
3636           (setq pos (list (cons 'unread
3637                                 (and (search-forward "A" nil t)
3638                                      (- (point) (point-min) 1)))))
3639           (goto-char (point-min))
3640           (push (cons 'replied (and (search-forward "B" nil t)
3641                                     (- (point) (point-min) 1)))
3642                 pos)
3643           (goto-char (point-min))
3644           (push (cons 'score (and (search-forward "C" nil t)
3645                                   (- (point) (point-min) 1)))
3646                 pos)
3647           (goto-char (point-min))
3648           (push (cons 'download (and (search-forward "D" nil t)
3649                                      (- (point) (point-min) 1)))
3650                 pos)))
3651       (setq gnus-summary-mark-positions pos))))
3652
3653 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3654   "Insert a dummy root in the summary buffer."
3655   (beginning-of-line)
3656   (gnus-add-text-properties
3657    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3658    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3659
3660 (defun gnus-summary-extract-address-component (from)
3661   (or (car (funcall gnus-extract-address-components from))
3662       from))
3663
3664 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3665   (let ((mail-parse-charset gnus-newsgroup-charset)
3666         (ignored-from-addresses (gnus-ignored-from-addresses))
3667         ; Is it really necessary to do this next part for each summary line?
3668         ; Luckily, doesn't seem to slow things down much.
3669         (mail-parse-ignored-charsets
3670          (with-current-buffer gnus-summary-buffer
3671            gnus-newsgroup-ignored-charsets)))
3672     (or
3673      (and ignored-from-addresses
3674           (string-match ignored-from-addresses gnus-tmp-from)
3675           (let ((extra-headers (mail-header-extra header))
3676                 to
3677                 newsgroups)
3678             (cond
3679              ((setq to (cdr (assq 'To extra-headers)))
3680               (concat gnus-summary-to-prefix
3681                       (inline
3682                         (gnus-summary-extract-address-component
3683                          (funcall gnus-decode-encoded-address-function to)))))
3684              ((setq newsgroups
3685                     (or
3686                      (cdr (assq 'Newsgroups extra-headers))
3687                      (and
3688                       (memq 'Newsgroups gnus-extra-headers)
3689                       (eq (car (gnus-find-method-for-group
3690                                 gnus-newsgroup-name)) 'nntp)
3691                       (gnus-group-real-name gnus-newsgroup-name))))
3692               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3693      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3694
3695 (defun gnus-summary-insert-line (gnus-tmp-header
3696                                  gnus-tmp-level gnus-tmp-current
3697                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3698                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3699                                  &optional gnus-tmp-dummy gnus-tmp-score
3700                                  gnus-tmp-process)
3701   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3702       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3703                                           gnus-tmp-level)))
3704   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3705          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3706          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3707          (gnus-tmp-score-char
3708           (if (or (null gnus-summary-default-score)
3709                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3710                       gnus-summary-zcore-fuzz))
3711               ?                         ;Whitespace
3712             (if (< gnus-tmp-score gnus-summary-default-score)
3713                 gnus-score-below-mark gnus-score-over-mark)))
3714          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3715          (gnus-tmp-replied
3716           (cond (gnus-tmp-process gnus-process-mark)
3717                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3718                  gnus-cached-mark)
3719                 (gnus-tmp-replied gnus-replied-mark)
3720                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3721                  gnus-forwarded-mark)
3722                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3723                  gnus-saved-mark)
3724                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3725                  gnus-recent-mark)
3726                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3727                  gnus-unseen-mark)
3728                 (t gnus-no-mark)))
3729          (gnus-tmp-downloaded
3730           (cond (undownloaded
3731                  gnus-undownloaded-mark)
3732                 (gnus-newsgroup-agentized
3733                  gnus-downloaded-mark)
3734                 (t
3735                  gnus-no-mark)))
3736          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3737          (gnus-tmp-name
3738           (cond
3739            ((string-match "<[^>]+> *$" gnus-tmp-from)
3740             (let ((beg (match-beginning 0)))
3741               (or (and (string-match "^\".+\"" gnus-tmp-from)
3742                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3743                   (substring gnus-tmp-from 0 beg))))
3744            ((string-match "(.+)" gnus-tmp-from)
3745             (substring gnus-tmp-from
3746                        (1+ (match-beginning 0)) (1- (match-end 0))))
3747            (t gnus-tmp-from)))
3748          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3749          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3750          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3751          (inhibit-read-only t))
3752     (when (string= gnus-tmp-name "")
3753       (setq gnus-tmp-name gnus-tmp-from))
3754     (unless (numberp gnus-tmp-lines)
3755       (setq gnus-tmp-lines -1))
3756     (if (= gnus-tmp-lines -1)
3757         (setq gnus-tmp-lines "?")
3758       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3759     (condition-case ()
3760         (gnus-put-text-property
3761          (point)
3762          (progn (eval gnus-summary-line-format-spec) (point))
3763          'gnus-number gnus-tmp-number)
3764       (error (gnus-message 5 "Error updating the summary line")))
3765     (when (gnus-visual-p 'summary-highlight 'highlight)
3766       (forward-line -1)
3767       (gnus-summary-highlight-line)
3768       (gnus-run-hooks 'gnus-summary-update-hook)
3769       (forward-line 1))))
3770
3771 (defun gnus-summary-update-line (&optional dont-update)
3772   "Update summary line after change."
3773   (when (and gnus-summary-default-score
3774              (not gnus-summary-inhibit-highlight))
3775     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3776            (article (gnus-summary-article-number))
3777            (score (gnus-summary-article-score article)))
3778       (unless dont-update
3779         (if (and gnus-summary-mark-below
3780                  (< (gnus-summary-article-score)
3781                     gnus-summary-mark-below))
3782             ;; This article has a low score, so we mark it as read.
3783             (when (memq article gnus-newsgroup-unreads)
3784               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3785           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3786             ;; This article was previously marked as read on account
3787             ;; of a low score, but now it has risen, so we mark it as
3788             ;; unread.
3789             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3790         (gnus-summary-update-mark
3791          (if (or (null gnus-summary-default-score)
3792                  (<= (abs (- score gnus-summary-default-score))
3793                      gnus-summary-zcore-fuzz))
3794              ?                          ;Whitespace
3795            (if (< score gnus-summary-default-score)
3796                gnus-score-below-mark gnus-score-over-mark))
3797          'score))
3798       ;; Do visual highlighting.
3799       (when (gnus-visual-p 'summary-highlight 'highlight)
3800         (gnus-summary-highlight-line)
3801         (gnus-run-hooks 'gnus-summary-update-hook)))))
3802
3803 (defvar gnus-tmp-new-adopts nil)
3804
3805 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3806   "Return the number of articles in THREAD.
3807 This may be 0 in some cases -- if none of the articles in
3808 the thread are to be displayed."
3809   (let* ((number
3810          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3811           (cond
3812            ((not (listp thread))
3813             1)
3814            ((and (consp thread) (cdr thread))
3815             (apply
3816              '+ 1 (mapcar
3817                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3818            ((null thread)
3819             1)
3820            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3821             1)
3822            (t 0))))
3823     (when (and level (zerop level) gnus-tmp-new-adopts)
3824       (incf number
3825             (apply '+ (mapcar
3826                        'gnus-summary-number-of-articles-in-thread
3827                        gnus-tmp-new-adopts))))
3828     (if char
3829         (if (> number 1) gnus-not-empty-thread-mark
3830           gnus-empty-thread-mark)
3831       number)))
3832
3833 (defsubst gnus-summary-line-message-size (head)
3834   "Return pretty-printed version of message size.
3835 This function is intended to be used in
3836 `gnus-summary-line-format-alist'."
3837   (let ((c (or (mail-header-chars head) -1)))
3838     (cond ((< c 0) "n/a")               ; chars not available
3839           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3840           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3841           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3842           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3843
3844
3845 (defun gnus-summary-set-local-parameters (group)
3846   "Go through the local params of GROUP and set all variable specs in that list."
3847   (let ((vars '(quit-config)))          ; Ignore quit-config.
3848     (dolist (elem (gnus-group-find-parameter group))
3849       (and (consp elem)                 ; Has to be a cons.
3850            (consp (cdr elem))           ; The cdr has to be a list.
3851            (symbolp (car elem))         ; Has to be a symbol in there.
3852            (not (memq (car elem) vars))
3853            (ignore-errors
3854              (push (car elem) vars)
3855              ;; Variables like `gnus-show-threads' that are globally
3856              ;; bound, if used as group parameters, need to get to be
3857              ;; buffer-local, whereas just parameters like `gcc-self',
3858              ;; `timestamp', etc. should not be bound as variables.
3859              (if (boundp (car elem))
3860                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3861                (eval (nth 1 elem))))))))
3862
3863 (defun gnus-summary-read-group (group &optional show-all no-article
3864                                       kill-buffer no-display backward
3865                                       select-articles)
3866   "Start reading news in newsgroup GROUP.
3867 If SHOW-ALL is non-nil, already read articles are also listed.
3868 If NO-ARTICLE is non-nil, no article is selected initially.
3869 If NO-DISPLAY, don't generate a summary buffer."
3870   (let (result)
3871     (while (and group
3872                 (null (setq result
3873                             (let ((gnus-auto-select-next nil))
3874                               (or (gnus-summary-read-group-1
3875                                    group show-all no-article
3876                                    kill-buffer no-display
3877                                    select-articles)
3878                                   (setq show-all nil
3879                                         select-articles nil)))))
3880                 (eq gnus-auto-select-next 'quietly))
3881       (set-buffer gnus-group-buffer)
3882       ;; The entry function called above goes to the next
3883       ;; group automatically, so we go two groups back
3884       ;; if we are searching for the previous group.
3885       (when backward
3886         (gnus-group-prev-unread-group 2))
3887       (if (not (equal group (gnus-group-group-name)))
3888           (setq group (gnus-group-group-name))
3889         (setq group nil)))
3890     result))
3891
3892 (defun gnus-summary-read-group-1 (group show-all no-article
3893                                         kill-buffer no-display
3894                                         &optional select-articles)
3895   ;; Killed foreign groups can't be entered.
3896   ;;  (when (and (not (gnus-group-native-p group))
3897   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3898   ;;    (error "Dead non-native groups can't be entered"))
3899   (gnus-message 5 "Retrieving newsgroup: %s..."
3900                 (gnus-group-decoded-name group))
3901   (let* ((new-group (gnus-summary-setup-buffer group))
3902          (quit-config (gnus-group-quit-config group))
3903          (did-select (and new-group (gnus-select-newsgroup
3904                                      group show-all select-articles))))
3905     (cond
3906      ;; This summary buffer exists already, so we just select it.
3907      ((not new-group)
3908       (gnus-set-global-variables)
3909       (when kill-buffer
3910         (gnus-kill-or-deaden-summary kill-buffer))
3911       (gnus-configure-windows 'summary 'force)
3912       (gnus-set-mode-line 'summary)
3913       (gnus-summary-position-point)
3914       (message "")
3915       t)
3916      ;; We couldn't select this group.
3917      ((null did-select)
3918       (when (and (eq major-mode 'gnus-summary-mode)
3919                  (not (equal (current-buffer) kill-buffer)))
3920         (kill-buffer (current-buffer))
3921         (if (not quit-config)
3922             (progn
3923               ;; Update the info -- marks might need to be removed,
3924               ;; for instance.
3925               (gnus-summary-update-info)
3926               (set-buffer gnus-group-buffer)
3927               (gnus-group-jump-to-group group)
3928               (gnus-group-next-unread-group 1))
3929           (gnus-handle-ephemeral-exit quit-config)))
3930       (let ((grpinfo (gnus-get-info group)))
3931         (if (null (gnus-info-read grpinfo))
3932             (gnus-message 3 "Group %s contains no messages"
3933                           (gnus-group-decoded-name group))
3934           (gnus-message 3 "Can't select group")))
3935       nil)
3936      ;; The user did a `C-g' while prompting for number of articles,
3937      ;; so we exit this group.
3938      ((eq did-select 'quit)
3939       (and (eq major-mode 'gnus-summary-mode)
3940            (not (equal (current-buffer) kill-buffer))
3941            (kill-buffer (current-buffer)))
3942       (when kill-buffer
3943         (gnus-kill-or-deaden-summary kill-buffer))
3944       (if (not quit-config)
3945           (progn
3946             (set-buffer gnus-group-buffer)
3947             (gnus-group-jump-to-group group)
3948             (gnus-configure-windows 'group 'force))
3949         (gnus-handle-ephemeral-exit quit-config))
3950       ;; Finally signal the quit.
3951       (signal 'quit nil))
3952      ;; The group was successfully selected.
3953      (t
3954       (gnus-set-global-variables)
3955       ;; Save the active value in effect when the group was entered.
3956       (setq gnus-newsgroup-active
3957             (gnus-copy-sequence
3958              (gnus-active gnus-newsgroup-name)))
3959       ;; You can change the summary buffer in some way with this hook.
3960       (gnus-run-hooks 'gnus-select-group-hook)
3961       (when (memq 'summary (gnus-update-format-specifications
3962                             nil 'summary 'summary-mode 'summary-dummy))
3963         ;; The format specification for the summary line was updated,
3964         ;; so we need to update the mark positions as well.
3965         (gnus-update-summary-mark-positions))
3966       ;; Do score processing.
3967       (when gnus-use-scoring
3968         (gnus-possibly-score-headers))
3969       ;; Check whether to fill in the gaps in the threads.
3970       (when gnus-build-sparse-threads
3971         (gnus-build-sparse-threads))
3972       ;; Find the initial limit.
3973       (if show-all
3974           (let ((gnus-newsgroup-dormant nil))
3975             (gnus-summary-initial-limit show-all))
3976         (gnus-summary-initial-limit show-all))
3977       ;; Generate the summary buffer.
3978       (unless no-display
3979         (gnus-summary-prepare))
3980       (when gnus-use-trees
3981         (gnus-tree-open group)
3982         (setq gnus-summary-highlight-line-function
3983               'gnus-tree-highlight-article))
3984       ;; If the summary buffer is empty, but there are some low-scored
3985       ;; articles or some excluded dormants, we include these in the
3986       ;; buffer.
3987       (when (and (zerop (buffer-size))
3988                  (not no-display))
3989         (cond (gnus-newsgroup-dormant
3990                (gnus-summary-limit-include-dormant))
3991               ((and gnus-newsgroup-scored show-all)
3992                (gnus-summary-limit-include-expunged t))))
3993       ;; Function `gnus-apply-kill-file' must be called in this hook.
3994       (gnus-run-hooks 'gnus-apply-kill-hook)
3995       (if (and (zerop (buffer-size))
3996                (not no-display))
3997           (progn
3998             ;; This newsgroup is empty.
3999             (gnus-summary-catchup-and-exit nil t)
4000             (gnus-message 6 "No unread news")
4001             (when kill-buffer
4002               (gnus-kill-or-deaden-summary kill-buffer))
4003             ;; Return nil from this function.
4004             nil)
4005         ;; Hide conversation thread subtrees.  We cannot do this in
4006         ;; gnus-summary-prepare-hook since kill processing may not
4007         ;; work with hidden articles.
4008         (gnus-summary-maybe-hide-threads)
4009         (when kill-buffer
4010           (gnus-kill-or-deaden-summary kill-buffer))
4011         (gnus-summary-auto-select-subject)
4012         ;; Show first unread article if requested.
4013         (if (and (not no-article)
4014                  (not no-display)
4015                  gnus-newsgroup-unreads
4016                  gnus-auto-select-first)
4017             (progn
4018               (gnus-configure-windows 'summary)
4019               (let ((art (gnus-summary-article-number)))
4020                 (unless (and (not gnus-plugged)
4021                              (or (memq art gnus-newsgroup-undownloaded)
4022                                  (memq art gnus-newsgroup-downloadable)))
4023                   (gnus-summary-goto-article art))))
4024           ;; Don't select any articles.
4025           (gnus-summary-position-point)
4026           (gnus-configure-windows 'summary 'force)
4027           (gnus-set-mode-line 'summary))
4028         (when (and gnus-auto-center-group
4029                    (get-buffer-window gnus-group-buffer t))
4030           ;; Gotta use windows, because recenter does weird stuff if
4031           ;; the current buffer ain't the displayed window.
4032           (let ((owin (selected-window)))
4033             (select-window (get-buffer-window gnus-group-buffer t))
4034             (when (gnus-group-goto-group group)
4035               (recenter))
4036             (select-window owin)))
4037         ;; Mark this buffer as "prepared".
4038         (setq gnus-newsgroup-prepared t)
4039         (gnus-run-hooks 'gnus-summary-prepared-hook)
4040         (unless (gnus-ephemeral-group-p group)
4041           (gnus-group-update-group group))
4042         t)))))
4043
4044 (defun gnus-summary-auto-select-subject ()
4045   "Select the subject line on initial group entry."
4046   (goto-char (point-min))
4047   (cond
4048    ((eq gnus-auto-select-subject 'best)
4049     (gnus-summary-best-unread-subject))
4050    ((eq gnus-auto-select-subject 'unread)
4051     (gnus-summary-first-unread-subject))
4052    ((eq gnus-auto-select-subject 'unseen)
4053     (gnus-summary-first-unseen-subject))
4054    ((eq gnus-auto-select-subject 'unseen-or-unread)
4055     (gnus-summary-first-unseen-or-unread-subject))
4056    ((eq gnus-auto-select-subject 'first)
4057     ;; Do nothing.
4058     )
4059    ((functionp gnus-auto-select-subject)
4060     (funcall gnus-auto-select-subject))))
4061
4062 (defun gnus-summary-prepare ()
4063   "Generate the summary buffer."
4064   (interactive)
4065   (let ((inhibit-read-only t))
4066     (erase-buffer)
4067     (setq gnus-newsgroup-data nil
4068           gnus-newsgroup-data-reverse nil)
4069     (gnus-run-hooks 'gnus-summary-generate-hook)
4070     ;; Generate the buffer, either with threads or without.
4071     (when gnus-newsgroup-headers
4072       (gnus-summary-prepare-threads
4073        (if gnus-show-threads
4074            (gnus-sort-gathered-threads
4075             (funcall gnus-summary-thread-gathering-function
4076                      (gnus-sort-threads
4077                       (gnus-cut-threads (gnus-make-threads)))))
4078          ;; Unthreaded display.
4079          (gnus-sort-articles gnus-newsgroup-headers))))
4080     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4081     ;; Call hooks for modifying summary buffer.
4082     (goto-char (point-min))
4083     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4084
4085 (defsubst gnus-general-simplify-subject (subject)
4086   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4087   (setq subject
4088         (cond
4089          ;; Truncate the subject.
4090          (gnus-simplify-subject-functions
4091           (gnus-map-function gnus-simplify-subject-functions subject))
4092          ((numberp gnus-summary-gather-subject-limit)
4093           (setq subject (gnus-simplify-subject-re subject))
4094           (if (> (length subject) gnus-summary-gather-subject-limit)
4095               (substring subject 0 gnus-summary-gather-subject-limit)
4096             subject))
4097          ;; Fuzzily simplify it.
4098          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4099           (gnus-simplify-subject-fuzzy subject))
4100          ;; Just remove the leading "Re:".
4101          (t
4102           (gnus-simplify-subject-re subject))))
4103
4104   (if (and gnus-summary-gather-exclude-subject
4105            (string-match gnus-summary-gather-exclude-subject subject))
4106       nil                         ; This article shouldn't be gathered
4107     subject))
4108
4109 (defun gnus-summary-simplify-subject-query ()
4110   "Query where the respool algorithm would put this article."
4111   (interactive)
4112   (gnus-summary-select-article)
4113   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4114
4115 (defun gnus-gather-threads-by-subject (threads)
4116   "Gather threads by looking at Subject headers."
4117   (if (not gnus-summary-make-false-root)
4118       threads
4119     (let ((hashtb (gnus-make-hashtable 1024))
4120           (prev threads)
4121           (result threads)
4122           subject hthread whole-subject)
4123       (while threads
4124         (setq subject (gnus-general-simplify-subject
4125                        (setq whole-subject (mail-header-subject
4126                                             (caar threads)))))
4127         (when subject
4128           (if (setq hthread (gnus-gethash subject hashtb))
4129               (progn
4130                 ;; We enter a dummy root into the thread, if we
4131                 ;; haven't done that already.
4132                 (unless (stringp (caar hthread))
4133                   (setcar hthread (list whole-subject (car hthread))))
4134                 ;; We add this new gathered thread to this gathered
4135                 ;; thread.
4136                 (setcdr (car hthread)
4137                         (nconc (cdar hthread) (list (car threads))))
4138                 ;; Remove it from the list of threads.
4139                 (setcdr prev (cdr threads))
4140                 (setq threads prev))
4141             ;; Enter this thread into the hash table.
4142             (gnus-sethash subject
4143                           (if gnus-summary-make-false-root-always
4144                               (progn
4145                                 ;; If you want a dummy root above all
4146                                 ;; threads...
4147                                 (setcar threads (list whole-subject
4148                                                       (car threads)))
4149                                 threads)
4150                             threads)
4151                           hashtb)))
4152         (setq prev threads)
4153         (setq threads (cdr threads)))
4154       result)))
4155
4156 (defun gnus-gather-threads-by-references (threads)
4157   "Gather threads by looking at References headers."
4158   (let ((idhashtb (gnus-make-hashtable 1024))
4159         (thhashtb (gnus-make-hashtable 1024))
4160         (prev threads)
4161         (result threads)
4162         ids references id gthread gid entered ref)
4163     (while threads
4164       (when (setq references (mail-header-references (caar threads)))
4165         (setq id (mail-header-id (caar threads))
4166               ids (inline (gnus-split-references references))
4167               entered nil)
4168         (while (setq ref (pop ids))
4169           (setq ids (delete ref ids))
4170           (if (not (setq gid (gnus-gethash ref idhashtb)))
4171               (progn
4172                 (gnus-sethash ref id idhashtb)
4173                 (gnus-sethash id threads thhashtb))
4174             (setq gthread (gnus-gethash gid thhashtb))
4175             (unless entered
4176               ;; We enter a dummy root into the thread, if we
4177               ;; haven't done that already.
4178               (unless (stringp (caar gthread))
4179                 (setcar gthread (list (mail-header-subject (caar gthread))
4180                                       (car gthread))))
4181               ;; We add this new gathered thread to this gathered
4182               ;; thread.
4183               (setcdr (car gthread)
4184                       (nconc (cdar gthread) (list (car threads)))))
4185             ;; Add it into the thread hash table.
4186             (gnus-sethash id gthread thhashtb)
4187             (setq entered t)
4188             ;; Remove it from the list of threads.
4189             (setcdr prev (cdr threads))
4190             (setq threads prev))))
4191       (setq prev threads)
4192       (setq threads (cdr threads)))
4193     result))
4194
4195 (defun gnus-sort-gathered-threads (threads)
4196   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4197   (let ((result threads))
4198     (while threads
4199       (when (stringp (caar threads))
4200         (setcdr (car threads)
4201                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4202       (setq threads (cdr threads)))
4203     result))
4204
4205 (defun gnus-thread-loop-p (root thread)
4206   "Say whether ROOT is in THREAD."
4207   (let ((stack (list thread))
4208         (infloop 0)
4209         th)
4210     (while (setq thread (pop stack))
4211       (setq th (cdr thread))
4212       (while (and th
4213                   (not (eq (caar th) root)))
4214         (pop th))
4215       (if th
4216           ;; We have found a loop.
4217           (let (ref-dep)
4218             (setcdr thread (delq (car th) (cdr thread)))
4219             (if (boundp (setq ref-dep (intern "none"
4220                                               gnus-newsgroup-dependencies)))
4221                 (setcdr (symbol-value ref-dep)
4222                         (nconc (cdr (symbol-value ref-dep))
4223                                (list (car th))))
4224               (set ref-dep (list nil (car th))))
4225             (setq infloop 1
4226                   stack nil))
4227         ;; Push all the subthreads onto the stack.
4228         (push (cdr thread) stack)))
4229     infloop))
4230
4231 (defun gnus-make-threads ()
4232   "Go through the dependency hashtb and find the roots.  Return all threads."
4233   (let (threads)
4234     (while (catch 'infloop
4235              (mapatoms
4236               (lambda (refs)
4237                 ;; Deal with self-referencing References loops.
4238                 (when (and (car (symbol-value refs))
4239                            (not (zerop
4240                                  (apply
4241                                   '+
4242                                   (mapcar
4243                                    (lambda (thread)
4244                                      (gnus-thread-loop-p
4245                                       (car (symbol-value refs)) thread))
4246                                    (cdr (symbol-value refs)))))))
4247                   (setq threads nil)
4248                   (throw 'infloop t))
4249                 (unless (car (symbol-value refs))
4250                   ;; These threads do not refer back to any other
4251                   ;; articles, so they're roots.
4252                   (setq threads (append (cdr (symbol-value refs)) threads))))
4253               gnus-newsgroup-dependencies)))
4254     threads))
4255
4256 ;; Build the thread tree.
4257 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4258   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4259
4260 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4261 if it was already present.
4262
4263 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4264 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4265 Message-IDs will be renamed to a unique Message-ID before being
4266 entered.
4267
4268 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4269   (let* ((id (mail-header-id header))
4270          (id-dep (and id (intern id dependencies)))
4271          parent-id ref ref-dep ref-header replaced)
4272     ;; Enter this `header' in the `dependencies' table.
4273     (cond
4274      ((not id-dep)
4275       (setq header nil))
4276      ;; The first two cases do the normal part: enter a new `header'
4277      ;; in the `dependencies' table.
4278      ((not (boundp id-dep))
4279       (set id-dep (list header)))
4280      ((null (car (symbol-value id-dep)))
4281       (setcar (symbol-value id-dep) header))
4282
4283      ;; From here the `header' was already present in the
4284      ;; `dependencies' table.
4285      (force-new
4286       ;; Overrides an existing entry;
4287       ;; just set the header part of the entry.
4288       (setcar (symbol-value id-dep) header)
4289       (setq replaced t))
4290
4291      ;; Renames the existing `header' to a unique Message-ID.
4292      ((not gnus-summary-ignore-duplicates)
4293       ;; An article with this Message-ID has already been seen.
4294       ;; We rename the Message-ID.
4295       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4296            (list header))
4297       (mail-header-set-id header id))
4298
4299      ;; The last case ignores an existing entry, except it adds any
4300      ;; additional Xrefs (in case the two articles came from different
4301      ;; servers.
4302      ;; Also sets `header' to `nil' meaning that the `dependencies'
4303      ;; table was *not* modified.
4304      (t
4305       (mail-header-set-xref
4306        (car (symbol-value id-dep))
4307        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4308                    "")
4309                (or (mail-header-xref header) "")))
4310       (setq header nil)))
4311
4312     (when (and header (not replaced))
4313       ;; First check that we are not creating a References loop.
4314       (setq parent-id (gnus-parent-id (mail-header-references header)))
4315       (setq ref parent-id)
4316       (while (and ref
4317                   (setq ref-dep (intern-soft ref dependencies))
4318                   (boundp ref-dep)
4319                   (setq ref-header (car (symbol-value ref-dep))))
4320         (if (string= id ref)
4321             ;; Yuk!  This is a reference loop.  Make the article be a
4322             ;; root article.
4323             (progn
4324               (mail-header-set-references (car (symbol-value id-dep)) "none")
4325               (setq ref nil)
4326               (setq parent-id nil))
4327           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4328       (setq ref-dep (intern (or parent-id "none") dependencies))
4329       (if (boundp ref-dep)
4330           (setcdr (symbol-value ref-dep)
4331                   (nconc (cdr (symbol-value ref-dep))
4332                          (list (symbol-value id-dep))))
4333         (set ref-dep (list nil (symbol-value id-dep)))))
4334     header))
4335
4336 (defun gnus-extract-message-id-from-in-reply-to (string)
4337   (if (string-match "<[^>]+>" string)
4338       (substring string (match-beginning 0) (match-end 0))
4339     nil))
4340
4341 (defun gnus-build-sparse-threads ()
4342   (let ((headers gnus-newsgroup-headers)
4343         (mail-parse-charset gnus-newsgroup-charset)
4344         (gnus-summary-ignore-duplicates t)
4345         header references generation relations
4346         subject child end new-child date)
4347     ;; First we create an alist of generations/relations, where
4348     ;; generations is how much we trust the relation, and the relation
4349     ;; is parent/child.
4350     (gnus-message 7 "Making sparse threads...")
4351     (save-excursion
4352       (nnheader-set-temp-buffer " *gnus sparse threads*")
4353       (while (setq header (pop headers))
4354         (when (and (setq references (mail-header-references header))
4355                    (not (string= references "")))
4356           (insert references)
4357           (setq child (mail-header-id header)
4358                 subject (mail-header-subject header)
4359                 date (mail-header-date header)
4360                 generation 0)
4361           (while (search-backward ">" nil t)
4362             (setq end (1+ (point)))
4363             (when (search-backward "<" nil t)
4364               (setq new-child (buffer-substring (point) end))
4365               (push (list (incf generation)
4366                           child (setq child new-child)
4367                           subject date)
4368                     relations)))
4369           (when child
4370             (push (list (1+ generation) child nil subject) relations))
4371           (erase-buffer)))
4372       (kill-buffer (current-buffer)))
4373     ;; Sort over trustworthiness.
4374     (dolist (relation (sort relations 'car-less-than-car))
4375       (when (gnus-dependencies-add-header
4376              (make-full-mail-header
4377               gnus-reffed-article-number
4378               (nth 3 relation) "" (or (nth 4 relation) "")
4379               (nth 1 relation)
4380               (or (nth 2 relation) "") 0 0 "")
4381              gnus-newsgroup-dependencies nil)
4382         (push gnus-reffed-article-number gnus-newsgroup-limit)
4383         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4384         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4385               gnus-newsgroup-reads)
4386         (decf gnus-reffed-article-number)))
4387     (gnus-message 7 "Making sparse threads...done")))
4388
4389 (defun gnus-build-old-threads ()
4390   ;; Look at all the articles that refer back to old articles, and
4391   ;; fetch the headers for the articles that aren't there.  This will
4392   ;; build complete threads - if the roots haven't been expired by the
4393   ;; server, that is.
4394   (let ((mail-parse-charset gnus-newsgroup-charset)
4395         id heads)
4396     (mapatoms
4397      (lambda (refs)
4398        (when (not (car (symbol-value refs)))
4399          (setq heads (cdr (symbol-value refs)))
4400          (while heads
4401            (if (memq (mail-header-number (caar heads))
4402                      gnus-newsgroup-dormant)
4403                (setq heads (cdr heads))
4404              (setq id (symbol-name refs))
4405              (while (and (setq id (gnus-build-get-header id))
4406                          (not (car (gnus-id-to-thread id)))))
4407              (setq heads nil)))))
4408      gnus-newsgroup-dependencies)))
4409
4410 (defsubst gnus-remove-odd-characters (string)
4411   "Translate STRING into something that doesn't contain weird characters."
4412   (mm-subst-char-in-string
4413    ?\r ?\-
4414    (mm-subst-char-in-string ?\n ?\- string t) t))
4415
4416 ;; This function has to be called with point after the article number
4417 ;; on the beginning of the line.
4418 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4419   (let ((eol (point-at-eol))
4420         (buffer (current-buffer))
4421         header references in-reply-to)
4422
4423     ;; overview: [num subject from date id refs chars lines misc]
4424     (unwind-protect
4425         (let (x)
4426           (narrow-to-region (point) eol)
4427           (unless (eobp)
4428             (forward-char))
4429
4430           (setq header
4431                 (make-full-mail-header
4432                  number                 ; number
4433                  (condition-case ()     ; subject
4434                      (gnus-remove-odd-characters
4435                       (funcall gnus-decode-encoded-word-function
4436                                (setq x (nnheader-nov-field))))
4437                    (error x))
4438                  (condition-case ()     ; from
4439                      (gnus-remove-odd-characters
4440                       (funcall gnus-decode-encoded-address-function
4441                                (setq x (nnheader-nov-field))))
4442                    (error x))
4443                  (nnheader-nov-field)   ; date
4444                  (nnheader-nov-read-message-id number)  ; id
4445                  (setq references (nnheader-nov-field)) ; refs
4446                  (nnheader-nov-read-integer) ; chars
4447                  (nnheader-nov-read-integer) ; lines
4448                  (unless (eobp)
4449                    (if (looking-at "Xref: ")
4450                        (goto-char (match-end 0)))
4451                    (nnheader-nov-field)) ; Xref
4452                  (nnheader-nov-parse-extra)))) ; extra
4453
4454       (widen))
4455
4456     (when (and (string= references "")
4457                (setq in-reply-to (mail-header-extra header))
4458                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4459       (mail-header-set-references
4460        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4461
4462     (when gnus-alter-header-function
4463       (funcall gnus-alter-header-function header))
4464     (gnus-dependencies-add-header header dependencies force-new)))
4465
4466 (defun gnus-build-get-header (id)
4467   "Look through the buffer of NOV lines and find the header to ID.
4468 Enter this line into the dependencies hash table, and return
4469 the id of the parent article (if any)."
4470   (let ((deps gnus-newsgroup-dependencies)
4471         found header)
4472     (prog1
4473         (with-current-buffer nntp-server-buffer
4474           (let ((case-fold-search nil))
4475             (goto-char (point-min))
4476             (while (and (not found)
4477                         (search-forward id nil t))
4478               (beginning-of-line)
4479               (setq found (looking-at
4480                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4481                                    (regexp-quote id))))
4482               (or found (beginning-of-line 2)))
4483             (when found
4484               (beginning-of-line)
4485               (and
4486                (setq header (gnus-nov-parse-line
4487                              (read (current-buffer)) deps))
4488                (gnus-parent-id (mail-header-references header))))))
4489       (when header
4490         (let ((number (mail-header-number header)))
4491           (push number gnus-newsgroup-limit)
4492           (push header gnus-newsgroup-headers)
4493           (if (memq number gnus-newsgroup-unselected)
4494               (progn
4495                 (setq gnus-newsgroup-unreads
4496                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4497                                                number))
4498                 (setq gnus-newsgroup-unselected
4499                       (delq number gnus-newsgroup-unselected)))
4500             (push number gnus-newsgroup-ancient)))))))
4501
4502 (defun gnus-build-all-threads ()
4503   "Read all the headers."
4504   (let ((gnus-summary-ignore-duplicates t)
4505         (mail-parse-charset gnus-newsgroup-charset)
4506         (dependencies gnus-newsgroup-dependencies)
4507         header article)
4508     (with-current-buffer nntp-server-buffer
4509       (let ((case-fold-search nil))
4510         (goto-char (point-min))
4511         (while (not (eobp))
4512           (ignore-errors
4513             (setq article (read (current-buffer))
4514                   header (gnus-nov-parse-line article dependencies)))
4515           (when header
4516             (with-current-buffer gnus-summary-buffer
4517               (push header gnus-newsgroup-headers)
4518               (if (memq (setq article (mail-header-number header))
4519                         gnus-newsgroup-unselected)
4520                   (progn
4521                     (setq gnus-newsgroup-unreads
4522                           (gnus-add-to-sorted-list
4523                            gnus-newsgroup-unreads article))
4524                     (setq gnus-newsgroup-unselected
4525                           (delq article gnus-newsgroup-unselected)))
4526                 (push article gnus-newsgroup-ancient)))
4527             (forward-line 1)))))))
4528
4529 (defun gnus-summary-update-article-line (article header)
4530   "Update the line for ARTICLE using HEADER."
4531   (let* ((id (mail-header-id header))
4532          (thread (gnus-id-to-thread id)))
4533     (unless thread
4534       (error "Article in no thread"))
4535     ;; Update the thread.
4536     (setcar thread header)
4537     (gnus-summary-goto-subject article)
4538     (let* ((datal (gnus-data-find-list article))
4539            (data (car datal))
4540            (inhibit-read-only t)
4541            (level (gnus-summary-thread-level)))
4542       (gnus-delete-line)
4543       (let ((inserted (- (point)
4544                          (progn
4545                            (gnus-summary-insert-line
4546                             header level nil
4547                             (memq article gnus-newsgroup-undownloaded)
4548                             (gnus-article-mark article)
4549                             (memq article gnus-newsgroup-replied)
4550                             (memq article gnus-newsgroup-expirable)
4551                             ;; Only insert the Subject string when it's different
4552                             ;; from the previous Subject string.
4553                             (if (and
4554                                  gnus-show-threads
4555                                  (gnus-subject-equal
4556                                   (condition-case ()
4557                                       (mail-header-subject
4558                                        (gnus-data-header
4559                                         (cadr
4560                                          (gnus-data-find-list
4561                                           article
4562                                           (gnus-data-list t)))))
4563                                     ;; Error on the side of excessive subjects.
4564                                     (error ""))
4565                                   (mail-header-subject header)))
4566                                 ""
4567                               (mail-header-subject header))
4568                             nil (cdr (assq article gnus-newsgroup-scored))
4569                             (memq article gnus-newsgroup-processable))
4570                            (point)))))
4571         (when (cdr datal)
4572           (gnus-data-update-list
4573            (cdr datal)
4574            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4575
4576 (defun gnus-summary-update-article (article &optional iheader)
4577   "Update ARTICLE in the summary buffer."
4578   (set-buffer gnus-summary-buffer)
4579   (let* ((header (gnus-summary-article-header article))
4580          (id (mail-header-id header))
4581          (data (gnus-data-find article))
4582          (thread (gnus-id-to-thread id))
4583          (references (mail-header-references header))
4584          (parent
4585           (gnus-id-to-thread
4586            (or (gnus-parent-id
4587                 (when (and references
4588                            (not (equal "" references)))
4589                   references))
4590                "none")))
4591          (inhibit-read-only t)
4592          (old (car thread)))
4593     (when thread
4594       (unless iheader
4595         (setcar thread nil)
4596         (when parent
4597           (delq thread parent)))
4598       (if (gnus-summary-insert-subject id header)
4599           ;; Set the (possibly) new article number in the data structure.
4600           (gnus-data-set-number data (gnus-id-to-article id))
4601         (setcar thread old)
4602         nil))))
4603
4604 (defun gnus-rebuild-thread (id &optional line)
4605   "Rebuild the thread containing ID.
4606 If LINE, insert the rebuilt thread starting on line LINE."
4607   (let ((inhibit-read-only t)
4608         old-pos current thread data)
4609     (if (not gnus-show-threads)
4610         (setq thread (list (car (gnus-id-to-thread id))))
4611       ;; Get the thread this article is part of.
4612       (setq thread (gnus-remove-thread id)))
4613     (setq old-pos (point-at-bol))
4614     (setq current (save-excursion
4615                     (and (re-search-backward "[\r\n]" nil t)
4616                          (gnus-summary-article-number))))
4617     ;; If this is a gathered thread, we have to go some re-gathering.
4618     (when (stringp (car thread))
4619       (let ((subject (car thread))
4620             roots thr)
4621         (setq thread (cdr thread))
4622         (while thread
4623           (unless (memq (setq thr (gnus-id-to-thread
4624                                    (gnus-root-id
4625                                     (mail-header-id (caar thread)))))
4626                         roots)
4627             (push thr roots))
4628           (setq thread (cdr thread)))
4629         ;; We now have all (unique) roots.
4630         (if (= (length roots) 1)
4631             ;; All the loose roots are now one solid root.
4632             (setq thread (car roots))
4633           (setq thread (cons subject (gnus-sort-threads roots))))))
4634     (let (threads)
4635       ;; We then insert this thread into the summary buffer.
4636       (when line
4637         (goto-char (point-min))
4638         (forward-line (1- line)))
4639       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4640         (if gnus-show-threads
4641             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4642           (gnus-summary-prepare-unthreaded thread))
4643         (setq data (nreverse gnus-newsgroup-data))
4644         (setq threads gnus-newsgroup-threads))
4645       ;; We splice the new data into the data structure.
4646       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4647       ;;!!! then we want to insert at the beginning of the buffer.
4648       ;;!!! That happens to be true with Gnus now, but that may
4649       ;;!!! change in the future.  Perhaps.
4650       (gnus-data-enter-list
4651        (if line nil current) data (- (point) old-pos))
4652       (setq gnus-newsgroup-threads
4653             (nconc threads gnus-newsgroup-threads))
4654       (gnus-data-compute-positions))))
4655
4656 (defun gnus-number-to-header (number)
4657   "Return the header for article NUMBER."
4658   (let ((headers gnus-newsgroup-headers))
4659     (while (and headers
4660                 (not (= number (mail-header-number (car headers)))))
4661       (pop headers))
4662     (when headers
4663       (car headers))))
4664
4665 (defun gnus-parent-headers (in-headers &optional generation)
4666   "Return the headers of the GENERATIONeth parent of HEADERS."
4667   (unless generation
4668     (setq generation 1))
4669   (let ((parent t)
4670         (headers in-headers)
4671         references)
4672     (while (and parent
4673                 (not (zerop generation))
4674                 (setq references (mail-header-references headers)))
4675       (setq headers (if (and references
4676                              (setq parent (gnus-parent-id references)))
4677                         (car (gnus-id-to-thread parent))
4678                       nil))
4679       (decf generation))
4680     (and (not (eq headers in-headers))
4681          headers)))
4682
4683 (defun gnus-id-to-thread (id)
4684   "Return the (sub-)thread where ID appears."
4685   (gnus-gethash id gnus-newsgroup-dependencies))
4686
4687 (defun gnus-id-to-article (id)
4688   "Return the article number of ID."
4689   (let ((thread (gnus-id-to-thread id)))
4690     (when (and thread
4691                (car thread))
4692       (mail-header-number (car thread)))))
4693
4694 (defun gnus-id-to-header (id)
4695   "Return the article headers of ID."
4696   (car (gnus-id-to-thread id)))
4697
4698 (defun gnus-article-displayed-root-p (article)
4699   "Say whether ARTICLE is a root(ish) article."
4700   (let ((level (gnus-summary-thread-level article))
4701         (refs (mail-header-references  (gnus-summary-article-header article)))
4702         particle)
4703     (cond
4704      ((null level) nil)
4705      ((zerop level) t)
4706      ((null refs) t)
4707      ((null (gnus-parent-id refs)) t)
4708      ((and (= 1 level)
4709            (null (setq particle (gnus-id-to-article
4710                                  (gnus-parent-id refs))))
4711            (null (gnus-summary-thread-level particle)))))))
4712
4713 (defun gnus-root-id (id)
4714   "Return the id of the root of the thread where ID appears."
4715   (let (last-id prev)
4716     (while (and id (setq prev (car (gnus-id-to-thread id))))
4717       (setq last-id id
4718             id (gnus-parent-id (mail-header-references prev))))
4719     last-id))
4720
4721 (defun gnus-articles-in-thread (thread)
4722   "Return the list of articles in THREAD."
4723   (cons (mail-header-number (car thread))
4724         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4725
4726 (defun gnus-remove-thread (id &optional dont-remove)
4727   "Remove the thread that has ID in it."
4728   (let (headers thread last-id)
4729     ;; First go up in this thread until we find the root.
4730     (setq last-id (gnus-root-id id)
4731           headers (message-flatten-list (gnus-id-to-thread last-id)))
4732     ;; We have now found the real root of this thread.  It might have
4733     ;; been gathered into some loose thread, so we have to search
4734     ;; through the threads to find the thread we wanted.
4735     (let ((threads gnus-newsgroup-threads)
4736           sub)
4737       (while threads
4738         (setq sub (car threads))
4739         (if (stringp (car sub))
4740             ;; This is a gathered thread, so we look at the roots
4741             ;; below it to find whether this article is in this
4742             ;; gathered root.
4743             (progn
4744               (setq sub (cdr sub))
4745               (while sub
4746                 (when (member (caar sub) headers)
4747                   (setq thread (car threads)
4748                         threads nil
4749                         sub nil))
4750                 (setq sub (cdr sub))))
4751           ;; It's an ordinary thread, so we check it.
4752           (when (eq (car sub) (car headers))
4753             (setq thread sub
4754                   threads nil)))
4755         (setq threads (cdr threads)))
4756       ;; If this article is in no thread, then it's a root.
4757       (if thread
4758           (unless dont-remove
4759             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4760         (setq thread (gnus-id-to-thread last-id)))
4761       (when thread
4762         (prog1
4763             thread                      ; We return this thread.
4764           (unless dont-remove
4765             (if (stringp (car thread))
4766                 (progn
4767                   ;; If we use dummy roots, then we have to remove the
4768                   ;; dummy root as well.
4769                   (when (eq gnus-summary-make-false-root 'dummy)
4770                     ;; We go to the dummy root by going to
4771                     ;; the first sub-"thread", and then one line up.
4772                     (gnus-summary-goto-article
4773                      (mail-header-number (caadr thread)))
4774                     (forward-line -1)
4775                     (gnus-delete-line)
4776                     (gnus-data-compute-positions))
4777                   (setq thread (cdr thread))
4778                   (while thread
4779                     (gnus-remove-thread-1 (car thread))
4780                     (setq thread (cdr thread))))
4781               (gnus-remove-thread-1 thread))))))))
4782
4783 (defun gnus-remove-thread-1 (thread)
4784   "Remove the thread THREAD recursively."
4785   (let ((number (mail-header-number (pop thread)))
4786         d)
4787     (setq thread (reverse thread))
4788     (while thread
4789       (gnus-remove-thread-1 (pop thread)))
4790     (when (setq d (gnus-data-find number))
4791       (goto-char (gnus-data-pos d))
4792       (gnus-summary-show-thread)
4793       (gnus-data-remove
4794        number
4795        (- (point-at-bol)
4796           (prog1
4797               (1+ (point-at-eol))
4798             (gnus-delete-line)))))))
4799
4800 (defun gnus-sort-threads-recursive (threads func)
4801   (sort (mapcar (lambda (thread)
4802                   (cons (car thread)
4803                         (and (cdr thread)
4804                              (gnus-sort-threads-recursive (cdr thread) func))))
4805                 threads) func))
4806
4807 (defun gnus-sort-threads-loop (threads func)
4808   (let* ((superthread (cons nil threads))
4809          (stack (list (cons superthread threads)))
4810          remaining-threads thread)
4811     (while stack
4812       (setq remaining-threads (cdr (car stack)))
4813       (if remaining-threads
4814           (progn (setq thread (car remaining-threads))
4815                  (setcdr (car stack) (cdr remaining-threads))
4816                  (if (cdr thread)
4817                      (push (cons thread (cdr thread)) stack)))
4818         (setq thread (caar stack))
4819         (setcdr thread (sort (cdr thread) func))
4820         (pop stack)))
4821     (cdr superthread)))
4822
4823 (defun gnus-sort-threads (threads)
4824   "Sort THREADS."
4825   (if (not gnus-thread-sort-functions)
4826       threads
4827     (gnus-message 8 "Sorting threads...")
4828     (prog1
4829         (condition-case nil
4830             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4831               (gnus-sort-threads-recursive
4832                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4833           ;; Even after binding max-lisp-eval-depth, the recursive
4834           ;; sorter might fail for very long threads.  In that case,
4835           ;; try using a (less well-tested) non-recursive sorter.
4836           (error (gnus-message 9 "Sorting threads with loop...")
4837                  (gnus-sort-threads-loop
4838                   threads (gnus-make-sort-function
4839                            gnus-thread-sort-functions))))
4840       (gnus-message 8 "Sorting threads...done"))))
4841
4842 (defun gnus-sort-articles (articles)
4843   "Sort ARTICLES."
4844   (when gnus-article-sort-functions
4845     (gnus-message 7 "Sorting articles...")
4846     (prog1
4847         (setq gnus-newsgroup-headers
4848               (sort articles (gnus-make-sort-function
4849                               gnus-article-sort-functions)))
4850       (gnus-message 7 "Sorting articles...done"))))
4851
4852 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4853 (defmacro gnus-thread-header (thread)
4854   "Return header of first article in THREAD.
4855 Note that THREAD must never, ever be anything else than a variable -
4856 using some other form will lead to serious barfage."
4857   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4858   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4859   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4860         (vector thread) 2))
4861
4862 (defsubst gnus-article-sort-by-number (h1 h2)
4863   "Sort articles by article number."
4864   (< (mail-header-number h1)
4865      (mail-header-number h2)))
4866
4867 (defun gnus-thread-sort-by-number (h1 h2)
4868   "Sort threads by root article number."
4869   (gnus-article-sort-by-number
4870    (gnus-thread-header h1) (gnus-thread-header h2)))
4871
4872 (defsubst gnus-article-sort-by-random (h1 h2)
4873   "Sort articles randomly."
4874   (zerop (random 2)))
4875
4876 (defun gnus-thread-sort-by-random (h1 h2)
4877   "Sort threads randomly."
4878   (gnus-article-sort-by-random
4879    (gnus-thread-header h1) (gnus-thread-header h2)))
4880
4881 (defsubst gnus-article-sort-by-lines (h1 h2)
4882   "Sort articles by article Lines header."
4883   (< (mail-header-lines h1)
4884      (mail-header-lines h2)))
4885
4886 (defun gnus-thread-sort-by-lines (h1 h2)
4887   "Sort threads by root article Lines header."
4888   (gnus-article-sort-by-lines
4889    (gnus-thread-header h1) (gnus-thread-header h2)))
4890
4891 (defsubst gnus-article-sort-by-chars (h1 h2)
4892   "Sort articles by octet length."
4893   (< (mail-header-chars h1)
4894      (mail-header-chars h2)))
4895
4896 (defun gnus-thread-sort-by-chars (h1 h2)
4897   "Sort threads by root article octet length."
4898   (gnus-article-sort-by-chars
4899    (gnus-thread-header h1) (gnus-thread-header h2)))
4900
4901 (defsubst gnus-article-sort-by-author (h1 h2)
4902   "Sort articles by root author."
4903   (gnus-string<
4904    (let ((extract (funcall
4905                    gnus-extract-address-components
4906                    (mail-header-from h1))))
4907      (or (car extract) (cadr extract) ""))
4908    (let ((extract (funcall
4909                    gnus-extract-address-components
4910                    (mail-header-from h2))))
4911      (or (car extract) (cadr extract) ""))))
4912
4913 (defun gnus-thread-sort-by-author (h1 h2)
4914   "Sort threads by root author."
4915   (gnus-article-sort-by-author
4916    (gnus-thread-header h1)  (gnus-thread-header h2)))
4917
4918 (defsubst gnus-article-sort-by-recipient (h1 h2)
4919   "Sort articles by recipient."
4920   (gnus-string<
4921    (let ((extract (funcall
4922                    gnus-extract-address-components
4923                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4924      (or (car extract) (cadr extract)))
4925    (let ((extract (funcall
4926                    gnus-extract-address-components
4927                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4928      (or (car extract) (cadr extract)))))
4929
4930 (defun gnus-thread-sort-by-recipient (h1 h2)
4931   "Sort threads by root recipient."
4932   (gnus-article-sort-by-recipient
4933    (gnus-thread-header h1) (gnus-thread-header h2)))
4934
4935 (defsubst gnus-article-sort-by-subject (h1 h2)
4936   "Sort articles by root subject."
4937   (gnus-string<
4938    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4939    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4940
4941 (defun gnus-thread-sort-by-subject (h1 h2)
4942   "Sort threads by root subject."
4943   (gnus-article-sort-by-subject
4944    (gnus-thread-header h1) (gnus-thread-header h2)))
4945
4946 (defsubst gnus-article-sort-by-date (h1 h2)
4947   "Sort articles by root article date."
4948   (time-less-p
4949    (gnus-date-get-time (mail-header-date h1))
4950    (gnus-date-get-time (mail-header-date h2))))
4951
4952 (defun gnus-thread-sort-by-date (h1 h2)
4953   "Sort threads by root article date."
4954   (gnus-article-sort-by-date
4955    (gnus-thread-header h1) (gnus-thread-header h2)))
4956
4957 (defsubst gnus-article-sort-by-score (h1 h2)
4958   "Sort articles by root article score.
4959 Unscored articles will be counted as having a score of zero."
4960   (> (or (cdr (assq (mail-header-number h1)
4961                     gnus-newsgroup-scored))
4962          gnus-summary-default-score 0)
4963      (or (cdr (assq (mail-header-number h2)
4964                     gnus-newsgroup-scored))
4965          gnus-summary-default-score 0)))
4966
4967 (defun gnus-thread-sort-by-score (h1 h2)
4968   "Sort threads by root article score."
4969   (gnus-article-sort-by-score
4970    (gnus-thread-header h1) (gnus-thread-header h2)))
4971
4972 (defun gnus-thread-sort-by-total-score (h1 h2)
4973   "Sort threads by the sum of all scores in the thread.
4974 Unscored articles will be counted as having a score of zero."
4975   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4976
4977 (defun gnus-thread-total-score (thread)
4978   ;; This function find the total score of THREAD.
4979   (cond
4980    ((null thread)
4981     0)
4982    ((consp thread)
4983     (if (stringp (car thread))
4984         (apply gnus-thread-score-function 0
4985                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4986       (gnus-thread-total-score-1 thread)))
4987    (t
4988     (gnus-thread-total-score-1 (list thread)))))
4989
4990 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4991   "Sort threads such that the thread with the most recently arrived article comes first."
4992   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4993
4994 (defun gnus-thread-highest-number (thread)
4995   "Return the highest article number in THREAD."
4996   (apply 'max (mapcar (lambda (header)
4997                         (mail-header-number header))
4998                       (message-flatten-list thread))))
4999
5000 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5001   "Sort threads such that the thread with the most recently dated article comes first."
5002   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5003
5004 ; Since this is called not only to sort the top-level threads, but
5005 ; also in recursive sorts to order the articles within a thread, each
5006 ; article will be processed many times.  Thus it speeds things up
5007 ; quite a bit to use gnus-date-get-time, which caches the time value.
5008 (defun gnus-thread-latest-date (thread)
5009   "Return the highest article date in THREAD."
5010   (apply 'max
5011          (mapcar (lambda (header) (gnus-float-time
5012                                    (gnus-date-get-time
5013                                     (mail-header-date header))))
5014                  (message-flatten-list thread))))
5015
5016 (defun gnus-thread-total-score-1 (root)
5017   ;; This function find the total score of the thread below ROOT.
5018   (setq root (car root))
5019   (apply gnus-thread-score-function
5020          (or (append
5021               (mapcar 'gnus-thread-total-score
5022                       (cdr (gnus-id-to-thread (mail-header-id root))))
5023               (when (> (mail-header-number root) 0)
5024                 (list (or (cdr (assq (mail-header-number root)
5025                                      gnus-newsgroup-scored))
5026                           gnus-summary-default-score 0))))
5027              (list gnus-summary-default-score)
5028              '(0))))
5029
5030 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5031 (defvar gnus-tmp-prev-subject nil)
5032 (defvar gnus-tmp-false-parent nil)
5033 (defvar gnus-tmp-root-expunged nil)
5034 (defvar gnus-tmp-dummy-line nil)
5035
5036 (defun gnus-extra-header (type &optional header)
5037   "Return the extra header of TYPE."
5038   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5039       ""))
5040
5041 (defvar gnus-tmp-thread-tree-header-string "")
5042
5043 (defcustom gnus-sum-thread-tree-root "> "
5044   "With %B spec, used for the root of a thread.
5045 If nil, use subject instead."
5046   :version "22.1"
5047   :type '(radio (const :format "%v  " nil) string)
5048   :group 'gnus-thread)
5049
5050 (defcustom gnus-sum-thread-tree-false-root "> "
5051   "With %B spec, used for a false root of a thread.
5052 If nil, use subject instead."
5053   :version "22.1"
5054   :type '(radio (const :format "%v  " nil) string)
5055   :group 'gnus-thread)
5056
5057 (defcustom gnus-sum-thread-tree-single-indent ""
5058   "With %B spec, used for a thread with just one message.
5059 If nil, use subject instead."
5060   :version "22.1"
5061   :type '(radio (const :format "%v  " nil) string)
5062   :group 'gnus-thread)
5063
5064 (defcustom gnus-sum-thread-tree-vertical "| "
5065   "With %B spec, used for drawing a vertical line."
5066   :version "22.1"
5067   :type 'string
5068   :group 'gnus-thread)
5069
5070 (defcustom gnus-sum-thread-tree-indent "  "
5071   "With %B spec, used for indenting."
5072   :version "22.1"
5073   :type 'string
5074   :group 'gnus-thread)
5075
5076 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5077   "With %B spec, used for a leaf with brothers."
5078   :version "22.1"
5079   :type 'string
5080   :group 'gnus-thread)
5081
5082 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5083   "With %B spec, used for a leaf without brothers."
5084   :version "22.1"
5085   :type 'string
5086   :group 'gnus-thread)
5087
5088 (defcustom gnus-summary-display-while-building nil
5089   "If non-nil, show and update the summary buffer as it's being built.
5090 If the value is t, update the buffer after every line is inserted.  If
5091 the value is an integer (N), update the display every N lines."
5092   :version "22.1"
5093   :group 'gnus-thread
5094   :type '(choice (const :tag "off" nil)
5095                  number
5096                  (const :tag "frequently" t)))
5097
5098 (defun gnus-summary-prepare-threads (threads)
5099   "Prepare summary buffer from THREADS and indentation LEVEL.
5100 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5101 or a straight list of headers."
5102   (gnus-message 7 "Generating summary...")
5103
5104   (setq gnus-newsgroup-threads threads)
5105   (beginning-of-line)
5106
5107   (let ((gnus-tmp-level 0)
5108         (default-score (or gnus-summary-default-score 0))
5109         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5110         (building-line-count gnus-summary-display-while-building)
5111         (building-count (integerp gnus-summary-display-while-building))
5112         thread number subject stack state gnus-tmp-gathered beg-match
5113         new-roots gnus-tmp-new-adopts thread-end simp-subject
5114         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5115         gnus-tmp-replied gnus-tmp-subject-or-nil
5116         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5117         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5118         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5119         tree-stack)
5120
5121     (setq gnus-tmp-prev-subject nil
5122           gnus-tmp-thread-tree-header-string "")
5123
5124     (if (vectorp (car threads))
5125         ;; If this is a straight (sic) list of headers, then a
5126         ;; threaded summary display isn't required, so we just create
5127         ;; an unthreaded one.
5128         (gnus-summary-prepare-unthreaded threads)
5129
5130       ;; Do the threaded display.
5131
5132       (if gnus-summary-display-while-building
5133           (switch-to-buffer (buffer-name)))
5134       (while (or threads stack gnus-tmp-new-adopts new-roots)
5135
5136         (if (and (= gnus-tmp-level 0)
5137                  (or (not stack)
5138                      (= (caar stack) 0))
5139                  (not gnus-tmp-false-parent)
5140                  (or gnus-tmp-new-adopts new-roots))
5141             (if gnus-tmp-new-adopts
5142                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5143                       thread (list (car gnus-tmp-new-adopts))
5144                       gnus-tmp-header (caar thread)
5145                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5146               (when new-roots
5147                 (setq thread (list (car new-roots))
5148                       gnus-tmp-header (caar thread)
5149                       new-roots (cdr new-roots))))
5150
5151           (if threads
5152               ;; If there are some threads, we do them before the
5153               ;; threads on the stack.
5154               (setq thread threads
5155                     gnus-tmp-header (caar thread))
5156             ;; There were no current threads, so we pop something off
5157             ;; the stack.
5158             (setq state (car stack)
5159                   gnus-tmp-level (car state)
5160                   tree-stack (cadr state)
5161                   thread (caddr state)
5162                   stack (cdr stack)
5163                   gnus-tmp-header (caar thread))))
5164
5165         (setq gnus-tmp-false-parent nil)
5166         (setq gnus-tmp-root-expunged nil)
5167         (setq thread-end nil)
5168
5169         (if (stringp gnus-tmp-header)
5170             ;; The header is a dummy root.
5171             (cond
5172              ((eq gnus-summary-make-false-root 'adopt)
5173               ;; We let the first article adopt the rest.
5174               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5175                                                (cddar thread)))
5176               (setq gnus-tmp-gathered
5177                     (nconc (mapcar
5178                             (lambda (h) (mail-header-number (car h)))
5179                             (cddar thread))
5180                            gnus-tmp-gathered))
5181               (setq thread (cons (list (caar thread)
5182                                        (cadar thread))
5183                                  (cdr thread)))
5184               (setq gnus-tmp-level -1
5185                     gnus-tmp-false-parent t))
5186              ((eq gnus-summary-make-false-root 'empty)
5187               ;; We print adopted articles with empty subject fields.
5188               (setq gnus-tmp-gathered
5189                     (nconc (mapcar
5190                             (lambda (h) (mail-header-number (car h)))
5191                             (cddar thread))
5192                            gnus-tmp-gathered))
5193               (setq gnus-tmp-level -1))
5194              ((eq gnus-summary-make-false-root 'dummy)
5195               ;; We remember that we probably want to output a dummy
5196               ;; root.
5197               (setq gnus-tmp-dummy-line gnus-tmp-header)
5198               (setq gnus-tmp-prev-subject gnus-tmp-header))
5199              (t
5200               ;; We do not make a root for the gathered
5201               ;; sub-threads at all.
5202               (setq gnus-tmp-level -1)))
5203
5204           (setq number (mail-header-number gnus-tmp-header)
5205                 subject (mail-header-subject gnus-tmp-header)
5206                 simp-subject (gnus-simplify-subject-fully subject))
5207
5208           (cond
5209            ;; If the thread has changed subject, we might want to make
5210            ;; this subthread into a root.
5211            ((and (null gnus-thread-ignore-subject)
5212                  (not (zerop gnus-tmp-level))
5213                  gnus-tmp-prev-subject
5214                  (not (string= gnus-tmp-prev-subject simp-subject)))
5215             (setq new-roots (nconc new-roots (list (car thread)))
5216                   thread-end t
5217                   gnus-tmp-header nil))
5218            ;; If the article lies outside the current limit,
5219            ;; then we do not display it.
5220            ((not (memq number gnus-newsgroup-limit))
5221             (setq gnus-tmp-gathered
5222                   (nconc (mapcar
5223                           (lambda (h) (mail-header-number (car h)))
5224                           (cdar thread))
5225                          gnus-tmp-gathered))
5226             (setq gnus-tmp-new-adopts (if (cdar thread)
5227                                           (append gnus-tmp-new-adopts
5228                                                   (cdar thread))
5229                                         gnus-tmp-new-adopts)
5230                   thread-end t
5231                   gnus-tmp-header nil)
5232             (when (zerop gnus-tmp-level)
5233               (setq gnus-tmp-root-expunged t)))
5234            ;; Perhaps this article is to be marked as read?
5235            ((and gnus-summary-mark-below
5236                  (< (or (cdr (assq number gnus-newsgroup-scored))
5237                         default-score)
5238                     gnus-summary-mark-below)
5239                  ;; Don't touch sparse articles.
5240                  (not (gnus-summary-article-sparse-p number))
5241                  (not (gnus-summary-article-ancient-p number)))
5242             (setq gnus-newsgroup-unreads
5243                   (delq number gnus-newsgroup-unreads))
5244             (if gnus-newsgroup-auto-expire
5245                 (setq gnus-newsgroup-expirable
5246                       (gnus-add-to-sorted-list
5247                        gnus-newsgroup-expirable number))
5248               (push (cons number gnus-low-score-mark)
5249                     gnus-newsgroup-reads))))
5250
5251           (when gnus-tmp-header
5252             ;; We may have an old dummy line to output before this
5253             ;; article.
5254             (when (and gnus-tmp-dummy-line
5255                        (gnus-subject-equal
5256                         gnus-tmp-dummy-line
5257                         (mail-header-subject gnus-tmp-header)))
5258               (gnus-summary-insert-dummy-line
5259                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5260               (setq gnus-tmp-dummy-line nil))
5261
5262             ;; Compute the mark.
5263             (setq gnus-tmp-unread (gnus-article-mark number))
5264
5265             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5266                                   gnus-tmp-header gnus-tmp-level)
5267                   gnus-newsgroup-data)
5268
5269             ;; Actually insert the line.
5270             (setq
5271              gnus-tmp-subject-or-nil
5272              (cond
5273               ((and gnus-thread-ignore-subject
5274                     gnus-tmp-prev-subject
5275                     (not (string= gnus-tmp-prev-subject simp-subject)))
5276                subject)
5277               ((zerop gnus-tmp-level)
5278                (if (and (eq gnus-summary-make-false-root 'empty)
5279                         (memq number gnus-tmp-gathered)
5280                         gnus-tmp-prev-subject
5281                         (string= gnus-tmp-prev-subject simp-subject))
5282                    gnus-summary-same-subject
5283                  subject))
5284               (t gnus-summary-same-subject)))
5285             (if (and (eq gnus-summary-make-false-root 'adopt)
5286                      (= gnus-tmp-level 1)
5287                      (memq number gnus-tmp-gathered))
5288                 (setq gnus-tmp-opening-bracket ?\<
5289                       gnus-tmp-closing-bracket ?\>)
5290               (setq gnus-tmp-opening-bracket ?\[
5291                     gnus-tmp-closing-bracket ?\]))
5292             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5293                 (gnus-make-thread-indent-array
5294                  (max (* 2 (length gnus-thread-indent-array))
5295                       gnus-tmp-level)))
5296             (setq
5297              gnus-tmp-indentation
5298              (aref gnus-thread-indent-array gnus-tmp-level)
5299              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5300              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5301                                 gnus-summary-default-score 0)
5302              gnus-tmp-score-char
5303              (if (or (null gnus-summary-default-score)
5304                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5305                          gnus-summary-zcore-fuzz))
5306                  ?                      ;Whitespace
5307                (if (< gnus-tmp-score gnus-summary-default-score)
5308                    gnus-score-below-mark gnus-score-over-mark))
5309              gnus-tmp-replied
5310              (cond ((memq number gnus-newsgroup-processable)
5311                     gnus-process-mark)
5312                    ((memq number gnus-newsgroup-cached)
5313                     gnus-cached-mark)
5314                    ((memq number gnus-newsgroup-replied)
5315                     gnus-replied-mark)
5316                    ((memq number gnus-newsgroup-forwarded)
5317                     gnus-forwarded-mark)
5318                    ((memq number gnus-newsgroup-saved)
5319                     gnus-saved-mark)
5320                    ((memq number gnus-newsgroup-recent)
5321                     gnus-recent-mark)
5322                    ((memq number gnus-newsgroup-unseen)
5323                     gnus-unseen-mark)
5324                    (t gnus-no-mark))
5325              gnus-tmp-downloaded
5326              (cond ((memq number gnus-newsgroup-undownloaded)
5327                     gnus-undownloaded-mark)
5328                    (gnus-newsgroup-agentized
5329                     gnus-downloaded-mark)
5330                    (t
5331                     gnus-no-mark))
5332              gnus-tmp-from (mail-header-from gnus-tmp-header)
5333              gnus-tmp-name
5334              (cond
5335               ((string-match "<[^>]+> *$" gnus-tmp-from)
5336                (setq beg-match (match-beginning 0))
5337                (or (and (string-match "^\".+\"" gnus-tmp-from)
5338                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5339                    (substring gnus-tmp-from 0 beg-match)))
5340               ((string-match "(.+)" gnus-tmp-from)
5341                (substring gnus-tmp-from
5342                           (1+ (match-beginning 0)) (1- (match-end 0))))
5343               (t gnus-tmp-from))
5344
5345              ;; Do the %B string
5346              gnus-tmp-thread-tree-header-string
5347              (cond
5348               ((not gnus-show-threads) "")
5349               ((zerop gnus-tmp-level)
5350                (cond ((cdar thread)
5351                       (or gnus-sum-thread-tree-root subject))
5352                      (gnus-tmp-new-adopts
5353                       (or gnus-sum-thread-tree-false-root subject))
5354                      (t
5355                       (or gnus-sum-thread-tree-single-indent subject))))
5356               (t
5357                (concat (apply 'concat
5358                               (mapcar (lambda (item)
5359                                         (if (= item 1)
5360                                             gnus-sum-thread-tree-vertical
5361                                           gnus-sum-thread-tree-indent))
5362                                       (cdr (reverse tree-stack))))
5363                        (if (nth 1 thread)
5364                            gnus-sum-thread-tree-leaf-with-other
5365                          gnus-sum-thread-tree-single-leaf)))))
5366             (when (string= gnus-tmp-name "")
5367               (setq gnus-tmp-name gnus-tmp-from))
5368             (unless (numberp gnus-tmp-lines)
5369               (setq gnus-tmp-lines -1))
5370             (if (= gnus-tmp-lines -1)
5371                 (setq gnus-tmp-lines "?")
5372               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5373             (gnus-put-text-property
5374              (point)
5375              (progn (eval gnus-summary-line-format-spec) (point))
5376              'gnus-number number)
5377             (when gnus-visual-p
5378               (forward-line -1)
5379               (gnus-summary-highlight-line)
5380               (when gnus-summary-update-hook
5381                 (gnus-run-hooks 'gnus-summary-update-hook))
5382               (forward-line 1))
5383
5384             (setq gnus-tmp-prev-subject simp-subject)))
5385
5386         (when (nth 1 thread)
5387           (push (list (max 0 gnus-tmp-level)
5388                       (copy-sequence tree-stack)
5389                       (nthcdr 1 thread))
5390                 stack))
5391         (push (if (nth 1 thread) 1 0) tree-stack)
5392         (incf gnus-tmp-level)
5393         (setq threads (if thread-end nil (cdar thread)))
5394         (if gnus-summary-display-while-building
5395             (if building-count
5396                 (progn
5397                   ;; use a set frequency
5398                   (setq building-line-count (1- building-line-count))
5399                   (when (= building-line-count 0)
5400                     (sit-for 0)
5401                     (setq building-line-count
5402                           gnus-summary-display-while-building)))
5403               ;; always
5404               (sit-for 0)))
5405         (unless threads
5406           (setq gnus-tmp-level 0)))))
5407   (gnus-message 7 "Generating summary...done"))
5408
5409 (defun gnus-summary-prepare-unthreaded (headers)
5410   "Generate an unthreaded summary buffer based on HEADERS."
5411   (let (header number mark)
5412
5413     (beginning-of-line)
5414
5415     (while headers
5416       ;; We may have to root out some bad articles...
5417       (when (memq (setq number (mail-header-number
5418                                 (setq header (pop headers))))
5419                   gnus-newsgroup-limit)
5420         ;; Mark article as read when it has a low score.
5421         (when (and gnus-summary-mark-below
5422                    (< (or (cdr (assq number gnus-newsgroup-scored))
5423                           gnus-summary-default-score 0)
5424                       gnus-summary-mark-below)
5425                    (not (gnus-summary-article-ancient-p number)))
5426           (setq gnus-newsgroup-unreads
5427                 (delq number gnus-newsgroup-unreads))
5428           (if gnus-newsgroup-auto-expire
5429               (push number gnus-newsgroup-expirable)
5430             (push (cons number gnus-low-score-mark)
5431                   gnus-newsgroup-reads)))
5432
5433         (setq mark (gnus-article-mark number))
5434         (push (gnus-data-make number mark (1+ (point)) header 0)
5435               gnus-newsgroup-data)
5436         (gnus-summary-insert-line
5437          header 0 number
5438          (memq number gnus-newsgroup-undownloaded)
5439          mark (memq number gnus-newsgroup-replied)
5440          (memq number gnus-newsgroup-expirable)
5441          (mail-header-subject header) nil
5442          (cdr (assq number gnus-newsgroup-scored))
5443          (memq number gnus-newsgroup-processable))))))
5444
5445 (defun gnus-summary-remove-list-identifiers ()
5446   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5447   (let ((regexp (if (consp gnus-list-identifiers)
5448                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5449                   gnus-list-identifiers))
5450         changed subject)
5451     (when regexp
5452       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5453       (dolist (header gnus-newsgroup-headers)
5454         (setq subject (mail-header-subject header)
5455               changed nil)
5456         (while (string-match regexp subject)
5457           (setq subject
5458                 (concat (substring subject 0 (match-beginning 1))
5459                         (substring subject (match-end 0)))
5460                 changed t))
5461         (when changed
5462           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5463             (setq subject
5464                   (concat (substring subject 0 (match-beginning 1))
5465                           (substring subject (match-end 1)))))
5466           (mail-header-set-subject header subject))))))
5467
5468 (defun gnus-fetch-headers (articles)
5469   "Fetch headers of ARTICLES."
5470   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5471     (gnus-message 5 "Fetching headers for %s..." name)
5472     (prog1
5473         (if (eq 'nov
5474                 (setq gnus-headers-retrieved-by
5475                       (gnus-retrieve-headers
5476                        articles gnus-newsgroup-name
5477                        ;; We might want to fetch old headers, but
5478                        ;; not if there is only 1 article.
5479                        (and (or (and
5480                                  (not (eq gnus-fetch-old-headers 'some))
5481                                  (not (numberp gnus-fetch-old-headers)))
5482                                 (> (length articles) 1))
5483                             gnus-fetch-old-headers))))
5484             (gnus-get-newsgroup-headers-xover
5485              articles nil nil gnus-newsgroup-name t)
5486           (gnus-get-newsgroup-headers))
5487       (gnus-message 5 "Fetching headers for %s...done" name))))
5488
5489 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5490   "Select newsgroup GROUP.
5491 If READ-ALL is non-nil, all articles in the group are selected.
5492 If SELECT-ARTICLES, only select those articles from GROUP."
5493   (let* ((entry (gnus-group-entry group))
5494          ;;!!! Dirty hack; should be removed.
5495          (gnus-summary-ignore-duplicates
5496           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5497               t
5498             gnus-summary-ignore-duplicates))
5499          (info (nth 2 entry))
5500          charset articles fetched-articles cached)
5501
5502     (unless (gnus-check-server
5503              (set (make-local-variable 'gnus-current-select-method)
5504                   (gnus-find-method-for-group group)))
5505       (error "Couldn't open server"))
5506     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5507
5508     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5509         (gnus-activate-group group)     ; Or we can activate it...
5510         (progn                          ; Or we bug out.
5511           (when (equal major-mode 'gnus-summary-mode)
5512             (gnus-kill-buffer (current-buffer)))
5513           (error
5514            "Couldn't activate group %s: %s"
5515            (mm-decode-coding-string group charset)
5516            (mm-decode-coding-string (gnus-status-message group) charset))))
5517
5518     (unless (gnus-request-group group t)
5519       (when (equal major-mode 'gnus-summary-mode)
5520         (gnus-kill-buffer (current-buffer)))
5521       (error "Couldn't request group %s: %s"
5522              (mm-decode-coding-string group charset)
5523              (mm-decode-coding-string (gnus-status-message group) charset)))
5524
5525     (when gnus-agent
5526       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5527
5528       (setq gnus-summary-use-undownloaded-faces
5529             (gnus-agent-find-parameter
5530              group
5531              'agent-enable-undownloaded-faces)))
5532
5533     (setq gnus-newsgroup-name group
5534           gnus-newsgroup-unselected nil
5535           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5536
5537     (let ((display (gnus-group-find-parameter group 'display)))
5538       (setq gnus-newsgroup-display
5539             (cond
5540              ((not (zerop (or (car-safe read-all) 0)))
5541               ;; The user entered the group with C-u SPC/RET, let's show
5542               ;; all articles.
5543               'gnus-not-ignore)
5544              ((eq display 'all)
5545               'gnus-not-ignore)
5546              ((arrayp display)
5547               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5548              ((numberp display)
5549               ;; The following is probably the "correct" solution, but
5550               ;; it makes Gnus fetch all headers and then limit the
5551               ;; articles (which is slow), so instead we hack the
5552               ;; select-articles parameter instead. -- Simon Josefsson
5553               ;; <jas@kth.se>
5554               ;;
5555               ;; (gnus-byte-compile
5556               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5557               ;;                         display)))))
5558               (setq select-articles
5559                     (gnus-uncompress-range
5560                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5561                              (if (> tmp 0)
5562                                  tmp
5563                                1))
5564                            (cdr (gnus-active group)))))
5565               nil)
5566              (t
5567               nil))))
5568
5569     (gnus-summary-setup-default-charset)
5570
5571     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5572     (when (gnus-virtual-group-p group)
5573       (setq cached gnus-newsgroup-cached))
5574
5575     (setq gnus-newsgroup-unreads
5576           (gnus-sorted-ndifference
5577            (gnus-sorted-ndifference gnus-newsgroup-unreads
5578                                     gnus-newsgroup-marked)
5579            gnus-newsgroup-dormant))
5580
5581     (setq gnus-newsgroup-processable nil)
5582
5583     (gnus-update-read-articles group gnus-newsgroup-unreads)
5584
5585     ;; Adjust and set lists of article marks.
5586     (when info
5587       (gnus-adjust-marked-articles info))
5588     (if (setq articles select-articles)
5589         (setq gnus-newsgroup-unselected
5590               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5591       (setq articles (gnus-articles-to-read group read-all)))
5592
5593     (cond
5594      ((null articles)
5595       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5596       'quit)
5597      ((eq articles 0) nil)
5598      (t
5599       ;; Init the dependencies hash table.
5600       (setq gnus-newsgroup-dependencies
5601             (gnus-make-hashtable (length articles)))
5602       (gnus-set-global-variables)
5603       ;; Retrieve the headers and read them in.
5604
5605       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5606
5607       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5608       (when cached
5609         (setq gnus-newsgroup-cached cached))
5610
5611       ;; Suppress duplicates?
5612       (when gnus-suppress-duplicates
5613         (gnus-dup-suppress-articles))
5614
5615       ;; Set the initial limit.
5616       (setq gnus-newsgroup-limit (copy-sequence articles))
5617       ;; Remove canceled articles from the list of unread articles.
5618       (setq fetched-articles
5619             (mapcar (lambda (headers) (mail-header-number headers))
5620                     gnus-newsgroup-headers))
5621       (setq gnus-newsgroup-articles fetched-articles)
5622       (setq gnus-newsgroup-unreads
5623             (gnus-sorted-nintersection
5624              gnus-newsgroup-unreads fetched-articles))
5625       (gnus-compute-unseen-list)
5626
5627       ;; Removed marked articles that do not exist.
5628       (gnus-update-missing-marks
5629        (gnus-sorted-difference articles fetched-articles))
5630       ;; We might want to build some more threads first.
5631       (when (and gnus-fetch-old-headers
5632                  (eq gnus-headers-retrieved-by 'nov))
5633         (if (eq gnus-fetch-old-headers 'invisible)
5634             (gnus-build-all-threads)
5635           (gnus-build-old-threads)))
5636       ;; Let the Gnus agent mark articles as read.
5637       (when gnus-agent
5638         (gnus-agent-get-undownloaded-list))
5639       ;; Remove list identifiers from subject
5640       (when gnus-list-identifiers
5641         (gnus-summary-remove-list-identifiers))
5642       ;; Check whether auto-expire is to be done in this group.
5643       (setq gnus-newsgroup-auto-expire
5644             (gnus-group-auto-expirable-p group))
5645       ;; Set up the article buffer now, if necessary.
5646       (unless (and gnus-single-article-buffer
5647                    (equal gnus-article-buffer "*Article*"))
5648         (gnus-article-setup-buffer))
5649       ;; First and last article in this newsgroup.
5650       (when gnus-newsgroup-headers
5651         (setq gnus-newsgroup-begin
5652               (mail-header-number (car gnus-newsgroup-headers))
5653               gnus-newsgroup-end
5654               (mail-header-number
5655                (gnus-last-element gnus-newsgroup-headers))))
5656       ;; GROUP is successfully selected.
5657       (or gnus-newsgroup-headers t)))))
5658
5659 (defun gnus-compute-unseen-list ()
5660   ;; The `seen' marks are treated specially.
5661   (if (not gnus-newsgroup-seen)
5662       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5663     (setq gnus-newsgroup-unseen
5664           (gnus-inverse-list-range-intersection
5665            gnus-newsgroup-articles gnus-newsgroup-seen))))
5666
5667 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5668
5669 (defun gnus-summary-display-make-predicate (display)
5670   (require 'gnus-agent)
5671   (when (= (length display) 1)
5672     (setq display (car display)))
5673   (unless gnus-summary-display-cache
5674     (dolist (elem (append '((unread . unread)
5675                             (read . read)
5676                             (unseen . unseen))
5677                           gnus-article-mark-lists))
5678       (push (cons (cdr elem)
5679                   (gnus-byte-compile
5680                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5681             gnus-summary-display-cache)))
5682   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5683         (gnus-category-predicate-cache gnus-summary-display-cache))
5684     (gnus-get-predicate display)))
5685
5686 ;; Uses the dynamically bound `number' variable.
5687 (defvar number)
5688 (defun gnus-article-marked-p (type &optional article)
5689   (let ((article (or article number)))
5690     (cond
5691      ((eq type 'tick)
5692       (memq article gnus-newsgroup-marked))
5693      ((eq type 'spam)
5694       (memq article gnus-newsgroup-spam-marked))
5695      ((eq type 'unsend)
5696       (memq article gnus-newsgroup-unsendable))
5697      ((eq type 'undownload)
5698       (memq article gnus-newsgroup-undownloaded))
5699      ((eq type 'download)
5700       (memq article gnus-newsgroup-downloadable))
5701      ((eq type 'unread)
5702       (memq article gnus-newsgroup-unreads))
5703      ((eq type 'read)
5704       (memq article gnus-newsgroup-reads))
5705      ((eq type 'dormant)
5706       (memq article gnus-newsgroup-dormant) )
5707      ((eq type 'expire)
5708       (memq article gnus-newsgroup-expirable))
5709      ((eq type 'reply)
5710       (memq article gnus-newsgroup-replied))
5711      ((eq type 'killed)
5712       (memq article gnus-newsgroup-killed))
5713      ((eq type 'bookmark)
5714       (assq article gnus-newsgroup-bookmarks))
5715      ((eq type 'score)
5716       (assq article gnus-newsgroup-scored))
5717      ((eq type 'save)
5718       (memq article gnus-newsgroup-saved))
5719      ((eq type 'cache)
5720       (memq article gnus-newsgroup-cached))
5721      ((eq type 'forward)
5722       (memq article gnus-newsgroup-forwarded))
5723      ((eq type 'seen)
5724       (not (memq article gnus-newsgroup-unseen)))
5725      ((eq type 'recent)
5726       (memq article gnus-newsgroup-recent))
5727      (t t))))
5728
5729 (defun gnus-articles-to-read (group &optional read-all)
5730   "Find out what articles the user wants to read."
5731   (let* ((articles
5732           ;; Select all articles if `read-all' is non-nil, or if there
5733           ;; are no unread articles.
5734           (if (or read-all
5735                   (and (zerop (length gnus-newsgroup-marked))
5736                        (zerop (length gnus-newsgroup-unreads)))
5737                   ;; Fetch all if the predicate is non-nil.
5738                   gnus-newsgroup-display)
5739               ;; We want to select the headers for all the articles in
5740               ;; the group, so we select either all the active
5741               ;; articles in the group, or (if that's nil), the
5742               ;; articles in the cache.
5743               (or
5744                (if gnus-newsgroup-maximum-articles
5745                    (let ((active (gnus-active group)))
5746                      (gnus-uncompress-range
5747                       (cons (max (car active)
5748                                  (- (cdr active)
5749                                     gnus-newsgroup-maximum-articles
5750                                     -1))
5751                             (cdr active))))
5752                  (gnus-uncompress-range (gnus-active group)))
5753                (gnus-cache-articles-in-group group))
5754             ;; Select only the "normal" subset of articles.
5755             (gnus-sorted-nunion
5756              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5757              gnus-newsgroup-unreads)))
5758          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5759          (scored (length scored-list))
5760          (number (length articles))
5761          (marked (+ (length gnus-newsgroup-marked)
5762                     (length gnus-newsgroup-dormant)))
5763          (select
5764           (cond
5765            ((numberp read-all)
5766             read-all)
5767            ((numberp gnus-newsgroup-display)
5768             gnus-newsgroup-display)
5769            (t
5770             (condition-case ()
5771                 (cond
5772                  ((and (or (<= scored marked) (= scored number))
5773                        (numberp gnus-large-newsgroup)
5774                        (> number gnus-large-newsgroup))
5775                   (let* ((cursor-in-echo-area nil)
5776                          (initial (gnus-parameter-large-newsgroup-initial
5777                                    gnus-newsgroup-name))
5778                          (input
5779                           (read-string
5780                            (format
5781                             "How many articles from %s (%s %d): "
5782                             (gnus-group-decoded-name gnus-newsgroup-name)
5783                             (if initial "max" "default")
5784                             number)
5785                            (if initial
5786                                (cons (number-to-string initial)
5787                                      0)))))
5788                     (if (string-match "^[ \t]*$" input) number input)))
5789                  ((and (> scored marked) (< scored number)
5790                        (> (- scored number) 20))
5791                   (let ((input
5792                          (read-string
5793                           (format "%s %s (%d scored, %d total): "
5794                                   "How many articles from"
5795                                   (gnus-group-decoded-name group)
5796                                   scored number))))
5797                     (if (string-match "^[ \t]*$" input)
5798                         number input)))
5799                  (t number))
5800               (quit
5801                (message "Quit getting the articles to read")
5802                nil))))))
5803     (setq select (if (stringp select) (string-to-number select) select))
5804     (if (or (null select) (zerop select))
5805         select
5806       (if (and (not (zerop scored)) (<= (abs select) scored))
5807           (progn
5808             (setq articles (sort scored-list '<))
5809             (setq number (length articles)))
5810         (setq articles (copy-sequence articles)))
5811
5812       (when (< (abs select) number)
5813         (if (< select 0)
5814             ;; Select the N oldest articles.
5815             (setcdr (nthcdr (1- (abs select)) articles) nil)
5816           ;; Select the N most recent articles.
5817           (setq articles (nthcdr (- number select) articles))))
5818       (setq gnus-newsgroup-unselected
5819             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5820       (when gnus-alter-articles-to-read-function
5821         (setq articles
5822               (sort
5823                (funcall gnus-alter-articles-to-read-function
5824                         gnus-newsgroup-name articles)
5825                '<)))
5826       articles)))
5827
5828 (defun gnus-killed-articles (killed articles)
5829   (let (out)
5830     (while articles
5831       (when (inline (gnus-member-of-range (car articles) killed))
5832         (push (car articles) out))
5833       (setq articles (cdr articles)))
5834     out))
5835
5836 (defun gnus-uncompress-marks (marks)
5837   "Uncompress the mark ranges in MARKS."
5838   (let ((uncompressed '(score bookmark))
5839         out)
5840     (while marks
5841       (if (memq (caar marks) uncompressed)
5842           (push (car marks) out)
5843         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5844       (setq marks (cdr marks)))
5845     out))
5846
5847 (defun gnus-article-mark-to-type (mark)
5848   "Return the type of MARK."
5849   (or (cadr (assq mark gnus-article-special-mark-lists))
5850       'list))
5851
5852 (defun gnus-article-unpropagatable-p (mark)
5853   "Return whether MARK should be propagated to back end."
5854   (memq mark gnus-article-unpropagated-mark-lists))
5855
5856 (defun gnus-adjust-marked-articles (info)
5857   "Set all article lists and remove all marks that are no longer valid."
5858   (let* ((marked-lists (gnus-info-marks info))
5859          (active (gnus-active (gnus-info-group info)))
5860          (min (car active))
5861          (max (cdr active))
5862          (types gnus-article-mark-lists)
5863          marks var articles article mark mark-type
5864          bgn end)
5865     ;; Hack to avoid adjusting marks for imap.
5866     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5867               'nnimap)
5868       (setq min 1))
5869
5870     (dolist (marks marked-lists)
5871       (setq mark (car marks)
5872             mark-type (gnus-article-mark-to-type mark)
5873             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5874
5875       ;; We set the variable according to the type of the marks list,
5876       ;; and then adjust the marks to a subset of the active articles.
5877       (cond
5878        ;; Adjust "simple" lists - compressed yet unsorted
5879        ((eq mark-type 'list)
5880         ;; Simultaneously uncompress and clip to active range
5881         ;; See gnus-uncompress-range for a description of possible marks
5882         (let (l lh)
5883           (if (not (cadr marks))
5884               (set var nil)
5885             (setq articles (if (numberp (cddr marks))
5886                                (list (cdr marks))
5887                              (cdr marks))
5888                   lh (cons nil nil)
5889                   l lh)
5890
5891             (while (setq article (pop articles))
5892               (cond ((consp article)
5893                      (setq bgn (max (car article) min)
5894                            end (min (cdr article) max))
5895                      (while (<= bgn end)
5896                        (setq l (setcdr l (cons bgn nil))
5897                              bgn (1+ bgn))))
5898                     ((and (<= min article)
5899                           (>= max article))
5900                      (setq l (setcdr l (cons article nil))))))
5901             (set var (cdr lh)))))
5902        ;; Adjust assocs.
5903        ((eq mark-type 'tuple)
5904         (set var (setq articles (cdr marks)))
5905         (when (not (listp (cdr (symbol-value var))))
5906           (set var (list (symbol-value var))))
5907         (when (not (listp (cdr articles)))
5908           (setq articles (list articles)))
5909         (while articles
5910           (when (or (not (consp (setq article (pop articles))))
5911                     (< (car article) min)
5912                     (> (car article) max))
5913             (set var (delq article (symbol-value var))))))
5914        ;; Adjust ranges (sloppily).
5915        ((eq mark-type 'range)
5916         (cond
5917          ((eq mark 'seen)
5918           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5919           ;; It should be (seen (NUM1 . NUM2)).
5920           (when (numberp (cddr marks))
5921             (setcdr marks (list (cdr marks))))
5922           (setq articles (cdr marks))
5923           (while (and articles
5924                       (or (and (consp (car articles))
5925                                (> min (cdar articles)))
5926                           (and (numberp (car articles))
5927                                (> min (car articles)))))
5928             (pop articles))
5929           (set var articles))))))))
5930
5931 (defun gnus-update-missing-marks (missing)
5932   "Go through the list of MISSING articles and remove them from the mark lists."
5933   (when missing
5934     (let (var m)
5935       ;; Go through all types.
5936       (dolist (elem gnus-article-mark-lists)
5937         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5938           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5939           (when (symbol-value var)
5940             ;; This list has articles.  So we delete all missing
5941             ;; articles from it.
5942             (setq m missing)
5943             (while m
5944               (set var (delq (pop m) (symbol-value var))))))))))
5945
5946 (defun gnus-update-marks ()
5947   "Enter the various lists of marked articles into the newsgroup info list."
5948   (let ((types gnus-article-mark-lists)
5949         (info (gnus-get-info gnus-newsgroup-name))
5950         type list newmarked symbol delta-marks)
5951     (when info
5952       ;; Add all marks lists to the list of marks lists.
5953       (while (setq type (pop types))
5954         (setq list (symbol-value
5955                     (setq symbol
5956                           (intern (format "gnus-newsgroup-%s" (car type))))))
5957
5958         (when list
5959           ;; Get rid of the entries of the articles that have the
5960           ;; default score.
5961           (when (and (eq (cdr type) 'score)
5962                      gnus-save-score
5963                      list)
5964             (let* ((arts list)
5965                    (prev (cons nil list))
5966                    (all prev))
5967               (while arts
5968                 (if (or (not (consp (car arts)))
5969                         (= (cdar arts) gnus-summary-default-score))
5970                     (setcdr prev (cdr arts))
5971                   (setq prev arts))
5972                 (setq arts (cdr arts)))
5973               (setq list (cdr all)))))
5974
5975         (when (eq (cdr type) 'seen)
5976           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5977
5978         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5979           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5980
5981         (when (and (gnus-check-backend-function
5982                     'request-set-mark gnus-newsgroup-name)
5983                    (not (gnus-article-unpropagatable-p (cdr type))))
5984           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5985                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5986                  (add (gnus-remove-from-range
5987                        (gnus-copy-sequence list) old)))
5988             (when add
5989               (push (list add 'add (list (cdr type))) delta-marks))
5990             (when del
5991               ;; Don't delete marks from outside the active range.  This
5992               ;; shouldn't happen, but is a sanity check.
5993               (setq del (gnus-sorted-range-intersection
5994                          (gnus-active gnus-newsgroup-name) del))
5995               (push (list del 'del (list (cdr type))) delta-marks))))
5996
5997         (when list
5998           (push (cons (cdr type) list) newmarked)))
5999
6000       (when delta-marks
6001         (unless (gnus-check-group gnus-newsgroup-name)
6002           (error "Can't open server for %s" gnus-newsgroup-name))
6003         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6004
6005       ;; Enter these new marks into the info of the group.
6006       (if (nthcdr 3 info)
6007           (setcar (nthcdr 3 info) newmarked)
6008         ;; Add the marks lists to the end of the info.
6009         (when newmarked
6010           (setcdr (nthcdr 2 info) (list newmarked))))
6011
6012       ;; Cut off the end of the info if there's nothing else there.
6013       (let ((i 5))
6014         (while (and (> i 2)
6015                     (not (nth i info)))
6016           (when (nthcdr (decf i) info)
6017             (setcdr (nthcdr i info) nil)))))))
6018
6019 (defun gnus-set-mode-line (where)
6020   "Set the mode line of the article or summary buffers.
6021 If WHERE is `summary', the summary mode line format will be used."
6022   ;; Is this mode line one we keep updated?
6023   (when (and (memq where gnus-updated-mode-lines)
6024              (symbol-value
6025               (intern (format "gnus-%s-mode-line-format-spec" where))))
6026     (let (mode-string)
6027       ;; We evaluate this in the summary buffer since these
6028       ;; variables are buffer-local to that buffer.
6029       (with-current-buffer gnus-summary-buffer
6030         ;; We bind all these variables that are used in the `eval' form
6031         ;; below.
6032         (let* ((mformat (symbol-value
6033                          (intern
6034                           (format "gnus-%s-mode-line-format-spec" where))))
6035                (gnus-tmp-group-name (gnus-mode-string-quote
6036                                      (gnus-group-decoded-name
6037                                       gnus-newsgroup-name)))
6038                (gnus-tmp-article-number (or gnus-current-article 0))
6039                (gnus-tmp-unread gnus-newsgroup-unreads)
6040                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6041                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6042                (gnus-tmp-unread-and-unselected
6043                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6044                             (zerop gnus-tmp-unselected))
6045                        "")
6046                       ((zerop gnus-tmp-unselected)
6047                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6048                       (t (format "{%d(+%d) more}"
6049                                  gnus-tmp-unread-and-unticked
6050                                  gnus-tmp-unselected))))
6051                (gnus-tmp-subject
6052                 (if (and gnus-current-headers
6053                          (vectorp gnus-current-headers))
6054                     (gnus-mode-string-quote
6055                      (mail-header-subject gnus-current-headers))
6056                   ""))
6057                bufname-length max-len
6058                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6059           (setq mode-string (eval mformat))
6060           (setq bufname-length (if (string-match "%b" mode-string)
6061                                    (- (length
6062                                        (buffer-name
6063                                         (if (eq where 'summary)
6064                                             nil
6065                                           (get-buffer gnus-article-buffer))))
6066                                       2)
6067                                  0))
6068           (setq max-len (max 4 (if gnus-mode-non-string-length
6069                                    (- (window-width)
6070                                       gnus-mode-non-string-length
6071                                       bufname-length)
6072                                  (length mode-string))))
6073           ;; We might have to chop a bit of the string off...
6074           (when (> (length mode-string) max-len)
6075             (setq mode-string
6076                   (concat (truncate-string-to-width mode-string (- max-len 3))
6077                           "...")))))
6078       ;; Update the mode line.
6079       (setq mode-line-buffer-identification
6080             (gnus-mode-line-buffer-identification (list mode-string)))
6081       (set-buffer-modified-p t))))
6082
6083 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6084   "Go through the HEADERS list and add all Xrefs to a hash table.
6085 The resulting hash table is returned, or nil if no Xrefs were found."
6086   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6087          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6088          (xref-hashtb (gnus-make-hashtable))
6089          start group entry number xrefs header)
6090     (while headers
6091       (setq header (pop headers))
6092       (when (and (setq xrefs (mail-header-xref header))
6093                  (not (memq (setq number (mail-header-number header))
6094                             unreads)))
6095         (setq start 0)
6096         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6097           (setq start (match-end 0))
6098           (setq group (if prefix
6099                           (concat prefix (substring xrefs (match-beginning 1)
6100                                                     (match-end 1)))
6101                         (substring xrefs (match-beginning 1) (match-end 1))))
6102           (setq number
6103                 (string-to-number (substring xrefs (match-beginning 2)
6104                                           (match-end 2))))
6105           (if (setq entry (gnus-gethash group xref-hashtb))
6106               (setcdr entry (cons number (cdr entry)))
6107             (gnus-sethash group (cons number nil) xref-hashtb)))))
6108     (and start xref-hashtb)))
6109
6110 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6111   "Look through all the headers and mark the Xrefs as read."
6112   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6113         name info xref-hashtb idlist method nth4)
6114     (with-current-buffer gnus-group-buffer
6115       (when (setq xref-hashtb
6116                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6117         (mapatoms
6118          (lambda (group)
6119            (unless (string= from-newsgroup (setq name (symbol-name group)))
6120              (setq idlist (symbol-value group))
6121              ;; Dead groups are not updated.
6122              (and (prog1
6123                       (setq info (gnus-get-info name))
6124                     (when (stringp (setq nth4 (gnus-info-method info)))
6125                       (setq nth4 (gnus-server-to-method nth4))))
6126                   ;; Only do the xrefs if the group has the same
6127                   ;; select method as the group we have just read.
6128                   (or (gnus-methods-equal-p
6129                        nth4 (gnus-find-method-for-group from-newsgroup))
6130                       virtual
6131                       (equal nth4 (setq method (gnus-find-method-for-group
6132                                                 from-newsgroup)))
6133                       (and (equal (car nth4) (car method))
6134                            (equal (nth 1 nth4) (nth 1 method))))
6135                   gnus-use-cross-reference
6136                   (or (not (eq gnus-use-cross-reference t))
6137                       virtual
6138                       ;; Only do cross-references on subscribed
6139                       ;; groups, if that is what is wanted.
6140                       (<= (gnus-info-level info) gnus-level-subscribed))
6141                   (gnus-group-make-articles-read name idlist))))
6142          xref-hashtb)))))
6143
6144 (defun gnus-compute-read-articles (group articles)
6145   (let* ((entry (gnus-group-entry group))
6146          (info (nth 2 entry))
6147          (active (gnus-active group))
6148          ninfo)
6149     (when entry
6150       ;; First peel off all invalid article numbers.
6151       (when active
6152         (let ((ids articles)
6153               id first)
6154           (while (setq id (pop ids))
6155             (when (and first (> id (cdr active)))
6156               ;; We'll end up in this situation in one particular
6157               ;; obscure situation.  If you re-scan a group and get
6158               ;; a new article that is cross-posted to a different
6159               ;; group that has not been re-scanned, you might get
6160               ;; crossposted article that has a higher number than
6161               ;; Gnus believes possible.  So we re-activate this
6162               ;; group as well.  This might mean doing the
6163               ;; crossposting thingy will *increase* the number
6164               ;; of articles in some groups.  Tsk, tsk.
6165               (setq active (or (gnus-activate-group group) active)))
6166             (when (or (> id (cdr active))
6167                       (< id (car active)))
6168               (setq articles (delq id articles))))))
6169       ;; If the read list is nil, we init it.
6170       (if (and active
6171                (null (gnus-info-read info))
6172                (> (car active) 1))
6173           (setq ninfo (cons 1 (1- (car active))))
6174         (setq ninfo (gnus-info-read info)))
6175       ;; Then we add the read articles to the range.
6176       (gnus-add-to-range
6177        ninfo (setq articles (sort articles '<))))))
6178
6179 (defun gnus-group-make-articles-read (group articles)
6180   "Update the info of GROUP to say that ARTICLES are read."
6181   (let* ((num 0)
6182          (entry (gnus-group-entry group))
6183          (info (nth 2 entry))
6184          (active (gnus-active group))
6185          range)
6186     (when entry
6187       (setq range (gnus-compute-read-articles group articles))
6188       (with-current-buffer gnus-group-buffer
6189         (gnus-undo-register
6190           `(progn
6191              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6192              (gnus-info-set-read ',info ',(gnus-info-read info))
6193              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6194              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6195              (gnus-group-update-group ,group t))))
6196       ;; Add the read articles to the range.
6197       (gnus-info-set-read info range)
6198       (gnus-request-set-mark group (list (list range 'add '(read))))
6199       ;; Then we have to re-compute how many unread
6200       ;; articles there are in this group.
6201       (when active
6202         (cond
6203          ((not range)
6204           (setq num (- (1+ (cdr active)) (car active))))
6205          ((not (listp (cdr range)))
6206           (setq num (- (cdr active) (- (1+ (cdr range))
6207                                        (car range)))))
6208          (t
6209           (while range
6210             (if (numberp (car range))
6211                 (setq num (1+ num))
6212               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6213             (setq range (cdr range)))
6214           (setq num (- (cdr active) num))))
6215         ;; Update the number of unread articles.
6216         (setcar entry num)
6217         ;; Update the group buffer.
6218         (unless (gnus-ephemeral-group-p group)
6219           (gnus-group-update-group group t))))))
6220
6221 (defvar gnus-newsgroup-none-id 0)
6222
6223 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6224   (let ((cur nntp-server-buffer)
6225         (dependencies
6226          (or dependencies
6227              (with-current-buffer gnus-summary-buffer
6228                gnus-newsgroup-dependencies)))
6229         headers id end ref number
6230         (mail-parse-charset gnus-newsgroup-charset)
6231         (mail-parse-ignored-charsets
6232          (save-current-buffer (condition-case nil
6233                                   (set-buffer gnus-summary-buffer)
6234                                 (error))
6235                               gnus-newsgroup-ignored-charsets)))
6236     (with-current-buffer nntp-server-buffer
6237       ;; Translate all TAB characters into SPACE characters.
6238       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6239       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6240       (ietf-drums-unfold-fws)
6241       (gnus-run-hooks 'gnus-parse-headers-hook)
6242       (let ((case-fold-search t)
6243             in-reply-to header p lines chars)
6244         (goto-char (point-min))
6245         ;; Search to the beginning of the next header.  Error messages
6246         ;; do not begin with 2 or 3.
6247         (while (re-search-forward "^[23][0-9]+ " nil t)
6248           (setq id nil
6249                 ref nil)
6250           ;; This implementation of this function, with nine
6251           ;; search-forwards instead of the one re-search-forward and
6252           ;; a case (which basically was the old function) is actually
6253           ;; about twice as fast, even though it looks messier.  You
6254           ;; can't have everything, I guess.  Speed and elegance
6255           ;; doesn't always go hand in hand.
6256           (setq
6257            header
6258            (vector
6259             ;; Number.
6260             (prog1
6261                 (setq number (read cur))
6262               (end-of-line)
6263               (setq p (point))
6264               (narrow-to-region (point)
6265                                 (or (and (search-forward "\n.\n" nil t)
6266                                          (- (point) 2))
6267                                     (point))))
6268             ;; Subject.
6269             (progn
6270               (goto-char p)
6271               (if (search-forward "\nsubject:" nil t)
6272                   (funcall gnus-decode-encoded-word-function
6273                            (nnheader-header-value))
6274                 "(none)"))
6275             ;; From.
6276             (progn
6277               (goto-char p)
6278               (if (search-forward "\nfrom:" nil t)
6279                   (funcall gnus-decode-encoded-address-function
6280                            (nnheader-header-value))
6281                 "(nobody)"))
6282             ;; Date.
6283             (progn
6284               (goto-char p)
6285               (if (search-forward "\ndate:" nil t)
6286                   (nnheader-header-value) ""))
6287             ;; Message-ID.
6288             (progn
6289               (goto-char p)
6290               (setq id (if (re-search-forward
6291                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6292                            ;; We do it this way to make sure the Message-ID
6293                            ;; is (somewhat) syntactically valid.
6294                            (buffer-substring (match-beginning 1)
6295                                              (match-end 1))
6296                          ;; If there was no message-id, we just fake one
6297                          ;; to make subsequent routines simpler.
6298                          (nnheader-generate-fake-message-id number))))
6299             ;; References.
6300             (progn
6301               (goto-char p)
6302               (if (search-forward "\nreferences:" nil t)
6303                   (progn
6304                     (setq end (point))
6305                     (prog1
6306                         (nnheader-header-value)
6307                       (setq ref
6308                             (buffer-substring
6309                              (progn
6310                                (end-of-line)
6311                                (search-backward ">" end t)
6312                                (1+ (point)))
6313                              (progn
6314                                (search-backward "<" end t)
6315                                (point))))))
6316                 ;; Get the references from the in-reply-to header if there
6317                 ;; were no references and the in-reply-to header looks
6318                 ;; promising.
6319                 (if (and (search-forward "\nin-reply-to:" nil t)
6320                          (setq in-reply-to (nnheader-header-value))
6321                          (string-match "<[^>]+>" in-reply-to))
6322                     (let (ref2)
6323                       (setq ref (substring in-reply-to (match-beginning 0)
6324                                            (match-end 0)))
6325                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6326                         (setq ref2 (substring in-reply-to (match-beginning 0)
6327                                               (match-end 0)))
6328                         (when (> (length ref2) (length ref))
6329                           (setq ref ref2)))
6330                       ref)
6331                   (setq ref nil))))
6332             ;; Chars.
6333             (progn
6334               (goto-char p)
6335               (if (search-forward "\nchars: " nil t)
6336                   (if (numberp (setq chars (ignore-errors (read cur))))
6337                       chars -1)
6338                 -1))
6339             ;; Lines.
6340             (progn
6341               (goto-char p)
6342               (if (search-forward "\nlines: " nil t)
6343                   (if (numberp (setq lines (ignore-errors (read cur))))
6344                       lines -1)
6345                 -1))
6346             ;; Xref.
6347             (progn
6348               (goto-char p)
6349               (and (search-forward "\nxref:" nil t)
6350                    (nnheader-header-value)))
6351             ;; Extra.
6352             (when gnus-extra-headers
6353               (let ((extra gnus-extra-headers)
6354                     out)
6355                 (while extra
6356                   (goto-char p)
6357                   (when (search-forward
6358                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6359                     (push (cons (car extra) (nnheader-header-value))
6360                           out))
6361                   (pop extra))
6362                 out))))
6363           (when (equal id ref)
6364             (setq ref nil))
6365
6366           (when gnus-alter-header-function
6367             (funcall gnus-alter-header-function header)
6368             (setq id (mail-header-id header)
6369                   ref (gnus-parent-id (mail-header-references header))))
6370
6371           (when (setq header
6372                       (gnus-dependencies-add-header
6373                        header dependencies force-new))
6374             (push header headers))
6375           (goto-char (point-max))
6376           (widen))
6377         (nreverse headers)))))
6378
6379 ;; Goes through the xover lines and returns a list of vectors
6380 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6381                                                   force-new dependencies
6382                                                   group also-fetch-heads)
6383   "Parse the news overview data in the server buffer.
6384 Return a list of headers that match SEQUENCE (see
6385 `nntp-retrieve-headers')."
6386   ;; Get the Xref when the users reads the articles since most/some
6387   ;; NNTP servers do not include Xrefs when using XOVER.
6388   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6389   (let ((mail-parse-charset gnus-newsgroup-charset)
6390         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6391         (cur nntp-server-buffer)
6392         (dependencies (or dependencies gnus-newsgroup-dependencies))
6393         (allp (cond
6394                ((eq gnus-read-all-available-headers t)
6395                 t)
6396                ((and (stringp gnus-read-all-available-headers)
6397                      group)
6398                 (string-match gnus-read-all-available-headers group))
6399                (t
6400                 nil)))
6401         number headers header)
6402     (with-current-buffer nntp-server-buffer
6403       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6404       ;; Allow the user to mangle the headers before parsing them.
6405       (gnus-run-hooks 'gnus-parse-headers-hook)
6406       (goto-char (point-min))
6407       (gnus-parse-without-error
6408         (while (and (or sequence allp)
6409                     (not (eobp)))
6410           (setq number (read cur))
6411           (when (not allp)
6412             (while (and sequence
6413                         (< (car sequence) number))
6414               (setq sequence (cdr sequence))))
6415           (when (and (or allp
6416                          (and sequence
6417                               (eq number (car sequence))))
6418                      (progn
6419                        (setq sequence (cdr sequence))
6420                        (setq header (inline
6421                                       (gnus-nov-parse-line
6422                                        number dependencies force-new)))))
6423             (push header headers))
6424           (forward-line 1)))
6425       ;; A common bug in inn is that if you have posted an article and
6426       ;; then retrieves the active file, it will answer correctly --
6427       ;; the new article is included.  However, a NOV entry for the
6428       ;; article may not have been generated yet, so this may fail.
6429       ;; We work around this problem by retrieving the last few
6430       ;; headers using HEAD.
6431       (if (or (not also-fetch-heads)
6432               (not sequence))
6433           ;; We (probably) got all the headers.
6434           (nreverse headers)
6435         (let ((gnus-nov-is-evil t))
6436           (nconc
6437            (nreverse headers)
6438            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6439              (gnus-get-newsgroup-headers))))))))
6440
6441 (defun gnus-article-get-xrefs ()
6442   "Fill in the Xref value in `gnus-current-headers', if necessary.
6443 This is meant to be called in `gnus-article-internal-prepare-hook'."
6444   (let ((headers (with-current-buffer gnus-summary-buffer
6445                    gnus-current-headers)))
6446     (or (not gnus-use-cross-reference)
6447         (not headers)
6448         (and (mail-header-xref headers)
6449              (not (string= (mail-header-xref headers) "")))
6450         (let ((case-fold-search t)
6451               xref)
6452           (save-restriction
6453             (nnheader-narrow-to-headers)
6454             (goto-char (point-min))
6455             (when (or (and (not (eobp))
6456                            (eq (downcase (char-after)) ?x)
6457                            (looking-at "Xref:"))
6458                       (search-forward "\nXref:" nil t))
6459               (goto-char (1+ (match-end 0)))
6460               (setq xref (buffer-substring (point) (point-at-eol)))
6461               (mail-header-set-xref headers xref)))))))
6462
6463 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6464   "Find article ID and insert the summary line for that article.
6465 OLD-HEADER can either be a header or a line number to insert
6466 the subject line on."
6467   (let* ((line (and (numberp old-header) old-header))
6468          (old-header (and (vectorp old-header) old-header))
6469          (header (cond ((and old-header use-old-header)
6470                         old-header)
6471                        ((and (numberp id)
6472                              (gnus-number-to-header id))
6473                         (gnus-number-to-header id))
6474                        (t
6475                         (gnus-read-header id))))
6476          (number (and (numberp id) id))
6477          d)
6478     (when header
6479       ;; Rebuild the thread that this article is part of and go to the
6480       ;; article we have fetched.
6481       (when (and (not gnus-show-threads)
6482                  old-header)
6483         (when (and number
6484                    (setq d (gnus-data-find (mail-header-number old-header))))
6485           (goto-char (gnus-data-pos d))
6486           (gnus-data-remove
6487            number
6488            (- (point-at-bol)
6489               (prog1
6490                   (1+ (point-at-eol))
6491                 (gnus-delete-line))))))
6492       ;; Remove list identifiers from subject.
6493       (when gnus-list-identifiers
6494         (let ((gnus-newsgroup-headers (list header)))
6495           (gnus-summary-remove-list-identifiers)))
6496       (when old-header
6497         (mail-header-set-number header (mail-header-number old-header)))
6498       (setq gnus-newsgroup-sparse
6499             (delq (setq number (mail-header-number header))
6500                   gnus-newsgroup-sparse))
6501       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6502       (push number gnus-newsgroup-limit)
6503       (gnus-rebuild-thread (mail-header-id header) line)
6504       (gnus-summary-goto-subject number nil t))
6505     (when (and (numberp number)
6506                (> number 0))
6507       ;; We have to update the boundaries even if we can't fetch the
6508       ;; article if ID is a number -- so that the next `P' or `N'
6509       ;; command will fetch the previous (or next) article even
6510       ;; if the one we tried to fetch this time has been canceled.
6511       (when (> number gnus-newsgroup-end)
6512         (setq gnus-newsgroup-end number))
6513       (when (< number gnus-newsgroup-begin)
6514         (setq gnus-newsgroup-begin number))
6515       (setq gnus-newsgroup-unselected
6516             (delq number gnus-newsgroup-unselected)))
6517     ;; Report back a success?
6518     (and header (mail-header-number header))))
6519
6520 ;;; Process/prefix in the summary buffer
6521
6522 (defun gnus-summary-work-articles (n)
6523   "Return a list of articles to be worked upon.
6524 The prefix argument, the list of process marked articles, and the
6525 current article will be taken into consideration."
6526   (with-current-buffer gnus-summary-buffer
6527     (cond
6528      (n
6529       ;; A numerical prefix has been given.
6530       (setq n (prefix-numeric-value n))
6531       (let ((backward (< n 0))
6532             (n (abs (prefix-numeric-value n)))
6533             articles article)
6534         (save-excursion
6535           (while
6536               (and (> n 0)
6537                    (push (setq article (gnus-summary-article-number))
6538                          articles)
6539                    (if backward
6540                        (gnus-summary-find-prev nil article)
6541                      (gnus-summary-find-next nil article)))
6542             (decf n)))
6543         (nreverse articles)))
6544      ((and (gnus-region-active-p) (mark))
6545       (message "region active")
6546       ;; Work on the region between point and mark.
6547       (let ((max (max (point) (mark)))
6548             articles article)
6549         (save-excursion
6550           (goto-char (min (point) (mark)))
6551           (while
6552               (and
6553                (push (setq article (gnus-summary-article-number)) articles)
6554                (gnus-summary-find-next nil article)
6555                (< (point) max)))
6556           (nreverse articles))))
6557      (gnus-newsgroup-processable
6558       ;; There are process-marked articles present.
6559       ;; Save current state.
6560       (gnus-summary-save-process-mark)
6561       ;; Return the list.
6562       (reverse gnus-newsgroup-processable))
6563      (t
6564       ;; Just return the current article.
6565       (list (gnus-summary-article-number))))))
6566
6567 (defmacro gnus-summary-iterate (arg &rest forms)
6568   "Iterate over the process/prefixed articles and do FORMS.
6569 ARG is the interactive prefix given to the command.  FORMS will be
6570 executed with point over the summary line of the articles."
6571   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6572     `(let ((,articles (gnus-summary-work-articles ,arg)))
6573        (while ,articles
6574          (gnus-summary-goto-subject (car ,articles))
6575          ,@forms
6576          (pop ,articles)))))
6577
6578 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6579 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6580
6581 (defun gnus-summary-save-process-mark ()
6582   "Push the current set of process marked articles on the stack."
6583   (interactive)
6584   (push (copy-sequence gnus-newsgroup-processable)
6585         gnus-newsgroup-process-stack))
6586
6587 (defun gnus-summary-kill-process-mark ()
6588   "Push the current set of process marked articles on the stack and unmark."
6589   (interactive)
6590   (gnus-summary-save-process-mark)
6591   (gnus-summary-unmark-all-processable))
6592
6593 (defun gnus-summary-yank-process-mark ()
6594   "Pop the last process mark state off the stack and restore it."
6595   (interactive)
6596   (unless gnus-newsgroup-process-stack
6597     (error "Empty mark stack"))
6598   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6599
6600 (defun gnus-summary-process-mark-set (set)
6601   "Make SET into the current process marked articles."
6602   (gnus-summary-unmark-all-processable)
6603   (mapc 'gnus-summary-set-process-mark set))
6604
6605 ;;; Searching and stuff
6606
6607 (defun gnus-summary-search-group (&optional backward use-level)
6608   "Search for next unread newsgroup.
6609 If optional argument BACKWARD is non-nil, search backward instead."
6610   (with-current-buffer gnus-group-buffer
6611     (when (gnus-group-search-forward
6612            backward nil (if use-level (gnus-group-group-level) nil))
6613       (gnus-group-group-name))))
6614
6615 (defun gnus-summary-best-group (&optional exclude-group)
6616   "Find the name of the best unread group.
6617 If EXCLUDE-GROUP, do not go to this group."
6618   (with-current-buffer gnus-group-buffer
6619     (save-excursion
6620       (gnus-group-best-unread-group exclude-group))))
6621
6622 (defun gnus-summary-find-next (&optional unread article backward)
6623   (if backward
6624       (gnus-summary-find-prev unread article)
6625     (let* ((dummy (gnus-summary-article-intangible-p))
6626            (article (or article (gnus-summary-article-number)))
6627            (data (gnus-data-find-list article))
6628            result)
6629       (when (and (not dummy)
6630                  (or (not gnus-summary-check-current)
6631                      (not unread)
6632                      (not (gnus-data-unread-p (car data)))))
6633         (setq data (cdr data)))
6634       (when (setq result
6635                   (if unread
6636                       (progn
6637                         (while data
6638                           (unless (memq (gnus-data-number (car data))
6639                                         (cond
6640                                          ((eq gnus-auto-goto-ignores
6641                                               'always-undownloaded)
6642                                           gnus-newsgroup-undownloaded)
6643                                          (gnus-plugged
6644                                           nil)
6645                                          ((eq gnus-auto-goto-ignores
6646                                               'unfetched)
6647                                           gnus-newsgroup-unfetched)
6648                                          ((eq gnus-auto-goto-ignores
6649                                               'undownloaded)
6650                                           gnus-newsgroup-undownloaded)))
6651                             (when (gnus-data-unread-p (car data))
6652                               (setq result (car data)
6653                                     data nil)))
6654                           (setq data (cdr data)))
6655                         result)
6656                     (car data)))
6657         (goto-char (gnus-data-pos result))
6658         (gnus-data-number result)))))
6659
6660 (defun gnus-summary-find-prev (&optional unread article)
6661   (let* ((eobp (eobp))
6662          (article (or article (gnus-summary-article-number)))
6663          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6664          result)
6665     (when (and (not eobp)
6666                (or (not gnus-summary-check-current)
6667                    (not unread)
6668                    (not (gnus-data-unread-p (car data)))))
6669       (setq data (cdr data)))
6670     (when (setq result
6671                 (if unread
6672                     (progn
6673                       (while data
6674                         (unless (memq (gnus-data-number (car data))
6675                                       (cond
6676                                        ((eq gnus-auto-goto-ignores
6677                                             'always-undownloaded)
6678                                         gnus-newsgroup-undownloaded)
6679                                        (gnus-plugged
6680                                         nil)
6681                                        ((eq gnus-auto-goto-ignores
6682                                             'unfetched)
6683                                         gnus-newsgroup-unfetched)
6684                                        ((eq gnus-auto-goto-ignores
6685                                             'undownloaded)
6686                                         gnus-newsgroup-undownloaded)))
6687                           (when (gnus-data-unread-p (car data))
6688                             (setq result (car data)
6689                                   data nil)))
6690                         (setq data (cdr data)))
6691                       result)
6692                   (car data)))
6693       (goto-char (gnus-data-pos result))
6694       (gnus-data-number result))))
6695
6696 (defun gnus-summary-find-subject (subject &optional unread backward article)
6697   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6698          (article (or article (gnus-summary-article-number)))
6699          (articles (gnus-data-list backward))
6700          (arts (gnus-data-find-list article articles))
6701          result)
6702     (when (or (not gnus-summary-check-current)
6703               (not unread)
6704               (not (gnus-data-unread-p (car arts))))
6705       (setq arts (cdr arts)))
6706     (while arts
6707       (and (or (not unread)
6708                (gnus-data-unread-p (car arts)))
6709            (vectorp (gnus-data-header (car arts)))
6710            (gnus-subject-equal
6711             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6712            (setq result (car arts)
6713                  arts nil))
6714       (setq arts (cdr arts)))
6715     (and result
6716          (goto-char (gnus-data-pos result))
6717          (gnus-data-number result))))
6718
6719 (defun gnus-summary-search-forward (&optional unread subject backward)
6720   "Search forward for an article.
6721 If UNREAD, look for unread articles.  If SUBJECT, look for
6722 articles with that subject.  If BACKWARD, search backward instead."
6723   (cond (subject (gnus-summary-find-subject subject unread backward))
6724         (backward (gnus-summary-find-prev unread))
6725         (t (gnus-summary-find-next unread))))
6726
6727 (defun gnus-recenter (&optional n)
6728   "Center point in window and redisplay frame.
6729 Also do horizontal recentering."
6730   (interactive "P")
6731   (when (and gnus-auto-center-summary
6732              (not (eq gnus-auto-center-summary 'vertical)))
6733     (gnus-horizontal-recenter))
6734   (if (fboundp 'recenter-top-bottom)
6735       (recenter-top-bottom n)
6736     (recenter n)))
6737
6738 (put 'gnus-recenter 'isearch-scroll t)
6739
6740 (defun gnus-forward-line-ignore-invisible (n)
6741   "Move N lines forward (backward if N is negative).
6742 Like forward-line, but skip over (and don't count) invisible lines."
6743   (let (done)
6744     (while (and (> n 0) (not done))
6745       ;; If the following character is currently invisible,
6746       ;; skip all characters with that same `invisible' property value.
6747       (while (gnus-invisible-p (point))
6748         (goto-char (gnus-next-char-property-change (point))))
6749       (forward-line 1)
6750       (if (eobp)
6751           (setq done t)
6752         (setq n (1- n))))
6753     (while (and (< n 0) (not done))
6754       (forward-line -1)
6755       (if (bobp) (setq done t)
6756         (setq n (1+ n))
6757         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6758           (goto-char (gnus-previous-char-property-change (point))))))))
6759
6760 (defun gnus-summary-recenter ()
6761   "Center point in the summary window.
6762 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6763 displayed, no centering will be performed."
6764   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6765   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6766   (interactive)
6767   ;; The user has to want it.
6768   (when gnus-auto-center-summary
6769     (let* ((top (cond ((< (window-height) 4) 0)
6770                       ((< (window-height) 7) 1)
6771                       (t (if (numberp gnus-auto-center-summary)
6772                              gnus-auto-center-summary
6773                            (/ (1- (window-height)) 2)))))
6774            (height (1- (window-height)))
6775            (bottom (save-excursion
6776                      (goto-char (point-max))
6777                      (gnus-forward-line-ignore-invisible (- height))
6778                      (point)))
6779            (window (get-buffer-window (current-buffer))))
6780       (when (get-buffer-window gnus-article-buffer)
6781         ;; Only do recentering when the article buffer is displayed,
6782         ;; Set the window start to either `bottom', which is the biggest
6783         ;; possible valid number, or the second line from the top,
6784         ;; whichever is the least.
6785         (let ((top-pos (save-excursion
6786                          (gnus-forward-line-ignore-invisible (- top))
6787                          (point))))
6788           (if (> bottom top-pos)
6789               ;; Keep the second line from the top visible
6790               (set-window-start window top-pos)
6791             ;; Try to keep the bottom line visible; if it's partially
6792             ;; obscured, either scroll one more line to make it fully
6793             ;; visible, or revert to using TOP-POS.
6794             (save-excursion
6795               (goto-char (point-max))
6796               (gnus-forward-line-ignore-invisible -1)
6797               (let ((last-line-start (point)))
6798                 (goto-char bottom)
6799                 (set-window-start window (point) t)
6800                 (when (not (pos-visible-in-window-p last-line-start window))
6801                   (gnus-forward-line-ignore-invisible 1)
6802                   (set-window-start window (min (point) top-pos) t)))))))
6803       ;; Do horizontal recentering while we're at it.
6804       (when (and (get-buffer-window (current-buffer) t)
6805                  (not (eq gnus-auto-center-summary 'vertical)))
6806         (let ((selected (selected-window)))
6807           (select-window (get-buffer-window (current-buffer) t))
6808           (gnus-summary-position-point)
6809           (gnus-horizontal-recenter)
6810           (select-window selected))))))
6811
6812 (defun gnus-summary-jump-to-group (newsgroup)
6813   "Move point to NEWSGROUP in group mode buffer."
6814   ;; Keep update point of group mode buffer if visible.
6815   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6816       (save-window-excursion
6817         ;; Take care of tree window mode.
6818         (when (get-buffer-window gnus-group-buffer)
6819           (pop-to-buffer gnus-group-buffer))
6820         (gnus-group-jump-to-group newsgroup))
6821     (save-excursion
6822       ;; Take care of tree window mode.
6823       (if (get-buffer-window gnus-group-buffer 0)
6824           (pop-to-buffer gnus-group-buffer)
6825         (set-buffer gnus-group-buffer))
6826       (gnus-group-jump-to-group newsgroup))))
6827
6828 ;; This function returns a list of article numbers based on the
6829 ;; difference between the ranges of read articles in this group and
6830 ;; the range of active articles.
6831 (defun gnus-list-of-unread-articles (group)
6832   (let* ((read (gnus-info-read (gnus-get-info group)))
6833          (active (or (gnus-active group) (gnus-activate-group group)))
6834          (last (or (cdr active)
6835                    (error "Group %s couldn't be activated " group)))
6836          (bottom (if gnus-newsgroup-maximum-articles
6837                      (max (car active)
6838                           (- last gnus-newsgroup-maximum-articles -1))
6839                    (car active)))
6840          first nlast unread)
6841     ;; If none are read, then all are unread.
6842     (if (not read)
6843         (setq first bottom)
6844       ;; If the range of read articles is a single range, then the
6845       ;; first unread article is the article after the last read
6846       ;; article.  Sounds logical, doesn't it?
6847       (if (and (not (listp (cdr read)))
6848                (or (< (car read) bottom)
6849                    (progn (setq read (list read))
6850                           nil)))
6851           (setq first (max bottom (1+ (cdr read))))
6852         ;; `read' is a list of ranges.
6853         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6854                                   (caar read)))
6855                   1)
6856           (setq first bottom))
6857         (while read
6858           (when first
6859             (while (< first nlast)
6860               (setq unread (cons first unread)
6861                     first (1+ first))))
6862           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6863           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6864           (setq read (cdr read)))))
6865     ;; And add the last unread articles.
6866     (while (<= first last)
6867       (setq unread (cons first unread)
6868             first (1+ first)))
6869     ;; Return the list of unread articles.
6870     (delq 0 (nreverse unread))))
6871
6872 (defun gnus-list-of-read-articles (group)
6873   "Return a list of unread, unticked and non-dormant articles."
6874   (let* ((info (gnus-get-info group))
6875          (marked (gnus-info-marks info))
6876          (active (gnus-active group)))
6877     (and info active
6878          (gnus-list-range-difference
6879           (gnus-list-range-difference
6880            (gnus-sorted-complement
6881             (gnus-uncompress-range
6882              (if gnus-newsgroup-maximum-articles
6883                  (cons (max (car active)
6884                             (- (cdr active)
6885                                gnus-newsgroup-maximum-articles
6886                                -1))
6887                        (cdr active))
6888                active))
6889             (gnus-list-of-unread-articles group))
6890            (cdr (assq 'dormant marked)))
6891           (cdr (assq 'tick marked))))))
6892
6893 ;; This function returns a sequence of article numbers based on the
6894 ;; difference between the ranges of read articles in this group and
6895 ;; the range of active articles.
6896 (defun gnus-sequence-of-unread-articles (group)
6897   (let* ((read (gnus-info-read (gnus-get-info group)))
6898          (active (or (gnus-active group) (gnus-activate-group group)))
6899          (last (cdr active))
6900          (bottom (if gnus-newsgroup-maximum-articles
6901                      (max (car active)
6902                           (- last gnus-newsgroup-maximum-articles -1))
6903                    (car active)))
6904          first nlast unread)
6905     ;; If none are read, then all are unread.
6906     (if (not read)
6907         (setq first bottom)
6908       ;; If the range of read articles is a single range, then the
6909       ;; first unread article is the article after the last read
6910       ;; article.  Sounds logical, doesn't it?
6911       (if (and (not (listp (cdr read)))
6912                (or (< (car read) bottom)
6913                    (progn (setq read (list read))
6914                           nil)))
6915           (setq first (max bottom (1+ (cdr read))))
6916         ;; `read' is a list of ranges.
6917         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6918                                   (caar read)))
6919                   1)
6920           (setq first bottom))
6921         (while read
6922           (when first
6923             (push (cons first nlast) unread))
6924           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6925           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6926           (setq read (cdr read)))))
6927     ;; And add the last unread articles.
6928     (cond ((not (and first last))
6929            nil)
6930           ((< first last)
6931            (push (cons first last) unread))
6932           ((= first last)
6933            (push first unread)))
6934     ;; Return the sequence of unread articles.
6935     (delq 0 (nreverse unread))))
6936
6937 ;; Various summary commands
6938
6939 (defun gnus-summary-select-article-buffer ()
6940   "Reconfigure windows to show article buffer."
6941   (interactive)
6942   (if (not (gnus-buffer-live-p gnus-article-buffer))
6943       (error "There is no article buffer for this summary buffer")
6944     (gnus-configure-windows 'article)
6945     (select-window (get-buffer-window gnus-article-buffer))))
6946
6947 (defun gnus-summary-universal-argument (arg)
6948   "Perform any operation on all articles that are process/prefixed."
6949   (interactive "P")
6950   (let ((articles (gnus-summary-work-articles arg))
6951         func article)
6952     (if (eq
6953          (setq
6954           func
6955           (key-binding
6956            (read-key-sequence
6957             (substitute-command-keys
6958              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6959          'undefined)
6960         (gnus-error 1 "Undefined key")
6961       (save-excursion
6962         (while articles
6963           (gnus-summary-goto-subject (setq article (pop articles)))
6964           (let (gnus-newsgroup-processable)
6965             (command-execute func))
6966           (gnus-summary-remove-process-mark article)))))
6967   (gnus-summary-position-point))
6968
6969 (defun gnus-summary-toggle-truncation (&optional arg)
6970   "Toggle truncation of summary lines.
6971 With ARG, turn line truncation on if ARG is positive."
6972   (interactive "P")
6973   (setq truncate-lines
6974         (if (null arg) (not truncate-lines)
6975           (> (prefix-numeric-value arg) 0)))
6976   (redraw-display))
6977
6978 (defun gnus-summary-find-for-reselect ()
6979   "Return the number of an article to stay on across a reselect.
6980 The current article is considered, then following articles, then previous
6981 articles.  An article is sought which is not cancelled and isn't a temporary
6982 insertion from another group.  If there's no such then return a dummy 0."
6983   (let (found)
6984     (dolist (rev '(nil t))
6985       (unless found      ; don't demand the reverse list if we don't need it
6986         (let ((data (gnus-data-find-list
6987                      (gnus-summary-article-number) (gnus-data-list rev))))
6988           (while (and data (not found))
6989             (if (and (< 0 (gnus-data-number (car data)))
6990                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6991                 (setq found (gnus-data-number (car data))))
6992             (setq data (cdr data))))))
6993     (or found 0)))
6994
6995 (defun gnus-summary-reselect-current-group (&optional all rescan)
6996   "Exit and then reselect the current newsgroup.
6997 The prefix argument ALL means to select all articles."
6998   (interactive "P")
6999   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7000     (error "Ephemeral groups can't be reselected"))
7001   (let ((current-subject (gnus-summary-find-for-reselect))
7002         (group gnus-newsgroup-name))
7003     (setq gnus-newsgroup-begin nil)
7004     (gnus-summary-exit nil 'leave-hidden)
7005     ;; We have to adjust the point of group mode buffer because
7006     ;; point was moved to the next unread newsgroup by exiting.
7007     (gnus-summary-jump-to-group group)
7008     (when rescan
7009       (save-excursion
7010         (gnus-group-get-new-news-this-group 1)))
7011     (gnus-group-read-group all t)
7012     (gnus-summary-goto-subject current-subject nil t)))
7013
7014 (defun gnus-summary-rescan-group (&optional all)
7015   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7016   (interactive "P")
7017   (gnus-summary-reselect-current-group all t))
7018
7019 (defun gnus-summary-update-info (&optional non-destructive)
7020   (save-excursion
7021     (let ((group gnus-newsgroup-name))
7022       (when group
7023         (when gnus-newsgroup-kill-headers
7024           (setq gnus-newsgroup-killed
7025                 (gnus-compress-sequence
7026                  (gnus-sorted-union
7027                   (gnus-list-range-intersection
7028                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7029                   gnus-newsgroup-unreads)
7030                  t)))
7031         (unless (listp (cdr gnus-newsgroup-killed))
7032           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7033         (let ((headers gnus-newsgroup-headers))
7034           ;; Set the new ranges of read articles.
7035           (with-current-buffer gnus-group-buffer
7036             (gnus-undo-force-boundary))
7037           (gnus-update-read-articles
7038            group (gnus-sorted-union
7039                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7040           ;; Set the current article marks.
7041           (let ((gnus-newsgroup-scored
7042                  (if (and (not gnus-save-score)
7043                           (not non-destructive))
7044                      nil
7045                    gnus-newsgroup-scored)))
7046             (save-excursion
7047               (gnus-update-marks)))
7048           ;; Do the cross-ref thing.
7049           (when gnus-use-cross-reference
7050             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7051           ;; Do not switch windows but change the buffer to work.
7052           (set-buffer gnus-group-buffer)
7053           (unless (gnus-ephemeral-group-p group)
7054             (gnus-group-update-group group)))))))
7055
7056 (defun gnus-summary-save-newsrc (&optional force)
7057   "Save the current number of read/marked articles in the dribble buffer.
7058 The dribble buffer will then be saved.
7059 If FORCE (the prefix), also save the .newsrc file(s)."
7060   (interactive "P")
7061   (gnus-summary-update-info t)
7062   (if force
7063       (gnus-save-newsrc-file)
7064     (gnus-dribble-save)))
7065
7066 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7067
7068 (defun gnus-summary-exit (&optional temporary leave-hidden)
7069   "Exit reading current newsgroup, and then return to group selection mode.
7070 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7071   (interactive)
7072   (gnus-set-global-variables)
7073   (when (gnus-buffer-live-p gnus-article-buffer)
7074     (with-current-buffer gnus-article-buffer
7075       (mm-destroy-parts gnus-article-mime-handles)
7076       ;; Set it to nil for safety reason.
7077       (setq gnus-article-mime-handle-alist nil)
7078       (setq gnus-article-mime-handles nil)))
7079   (gnus-kill-save-kill-buffer)
7080   (gnus-async-halt-prefetch)
7081   (let* ((group gnus-newsgroup-name)
7082          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7083          (gnus-group-is-exiting-p t)
7084          (mode major-mode)
7085          (group-point nil)
7086          (buf (current-buffer)))
7087     (unless quit-config
7088       ;; Do adaptive scoring, and possibly save score files.
7089       (when gnus-newsgroup-adaptive
7090         (gnus-score-adaptive))
7091       (when gnus-use-scoring
7092         (gnus-score-save)))
7093     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7094     ;; If we have several article buffers, we kill them at exit.
7095     (unless gnus-single-article-buffer
7096       (when (gnus-buffer-live-p gnus-article-buffer)
7097         (with-current-buffer gnus-article-buffer
7098           ;; Don't kill sticky article buffers
7099           (unless (eq major-mode 'gnus-sticky-article-mode)
7100             (gnus-kill-buffer gnus-article-buffer)
7101             (setq gnus-article-current nil))))
7102       (gnus-kill-buffer gnus-original-article-buffer))
7103     (when gnus-use-cache
7104       (gnus-cache-possibly-remove-articles)
7105       (gnus-cache-save-buffers))
7106     (gnus-async-prefetch-remove-group group)
7107     (when gnus-suppress-duplicates
7108       (gnus-dup-enter-articles))
7109     (when gnus-use-trees
7110       (gnus-tree-close group))
7111     (when gnus-use-cache
7112       (gnus-cache-write-active))
7113     ;; Remove entries for this group.
7114     (nnmail-purge-split-history (gnus-group-real-name group))
7115     ;; Make all changes in this group permanent.
7116     (unless quit-config
7117       (gnus-run-hooks 'gnus-exit-group-hook)
7118       (gnus-summary-update-info))
7119     (gnus-close-group group)
7120     ;; Make sure where we were, and go to next newsgroup.
7121     (set-buffer gnus-group-buffer)
7122     (unless quit-config
7123       (gnus-group-jump-to-group group))
7124     (gnus-run-hooks 'gnus-summary-exit-hook)
7125     (unless (or quit-config
7126                 (not gnus-summary-next-group-on-exit)
7127                 ;; If this group has disappeared from the summary
7128                 ;; buffer, don't skip forwards.
7129                 (not (string= group (gnus-group-group-name))))
7130       (gnus-group-next-unread-group 1))
7131     (setq group-point (point))
7132     (if temporary
7133         nil                             ;Nothing to do.
7134       (set-buffer buf)
7135       (if (not gnus-kill-summary-on-exit)
7136           (progn
7137             (gnus-deaden-summary)
7138             (setq mode nil))
7139         ;; We set all buffer-local variables to nil.  It is unclear why
7140         ;; this is needed, but if we don't, buffer-local variables are
7141         ;; not garbage-collected, it seems.  This would the lead to en
7142         ;; ever-growing Emacs.
7143         (gnus-summary-clear-local-variables)
7144         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7145           (gnus-summary-clear-local-variables))
7146         (when (get-buffer gnus-article-buffer)
7147           (bury-buffer gnus-article-buffer))
7148         ;; Return to group mode buffer.
7149         (when (eq mode 'gnus-summary-mode)
7150           (gnus-kill-buffer buf)))
7151       (setq gnus-current-select-method gnus-select-method)
7152       (set-buffer gnus-group-buffer)
7153       (if quit-config
7154           (gnus-handle-ephemeral-exit quit-config)
7155         (goto-char group-point)
7156         ;; If gnus-group-buffer is already displayed, make sure we also move
7157         ;; the cursor in the window that displays it.
7158         (let ((win (get-buffer-window (current-buffer) 0)))
7159           (if win (set-window-point win (point))))
7160         (unless leave-hidden
7161           (gnus-configure-windows 'group 'force)))
7162       ;; Clear the current group name.
7163       (unless quit-config
7164         (setq gnus-newsgroup-name nil)))))
7165
7166 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7167 (defun gnus-summary-exit-no-update (&optional no-questions)
7168   "Quit reading current newsgroup without updating read article info."
7169   (interactive)
7170   (let* ((group gnus-newsgroup-name)
7171          (gnus-group-is-exiting-p t)
7172          (gnus-group-is-exiting-without-update-p t)
7173          (quit-config (gnus-group-quit-config group)))
7174     (when (or no-questions
7175               gnus-expert-user
7176               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7177       (gnus-async-halt-prefetch)
7178       (run-hooks 'gnus-summary-prepare-exit-hook)
7179       (when (gnus-buffer-live-p gnus-article-buffer)
7180         (with-current-buffer gnus-article-buffer
7181           (mm-destroy-parts gnus-article-mime-handles)
7182           ;; Set it to nil for safety reason.
7183           (setq gnus-article-mime-handle-alist nil)
7184           (setq gnus-article-mime-handles nil)))
7185       ;; If we have several article buffers, we kill them at exit.
7186       (unless gnus-single-article-buffer
7187         (gnus-kill-buffer gnus-article-buffer)
7188         (gnus-kill-buffer gnus-original-article-buffer)
7189         (setq gnus-article-current nil))
7190       (if (not gnus-kill-summary-on-exit)
7191           (gnus-deaden-summary)
7192         (gnus-close-group group)
7193         (gnus-summary-clear-local-variables)
7194         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7195           (gnus-summary-clear-local-variables))
7196         (gnus-kill-buffer gnus-summary-buffer))
7197       (unless gnus-single-article-buffer
7198         (setq gnus-article-current nil))
7199       (when gnus-use-trees
7200         (gnus-tree-close group))
7201       (gnus-async-prefetch-remove-group group)
7202       (when (get-buffer gnus-article-buffer)
7203         (bury-buffer gnus-article-buffer))
7204       ;; Return to the group buffer.
7205       (gnus-configure-windows 'group 'force)
7206       ;; Clear the current group name.
7207       (setq gnus-newsgroup-name nil)
7208       (unless (gnus-ephemeral-group-p group)
7209         (gnus-group-update-group group))
7210       (when (equal (gnus-group-group-name) group)
7211         (gnus-group-next-unread-group 1))
7212       (when quit-config
7213         (gnus-handle-ephemeral-exit quit-config)))))
7214
7215 (defun gnus-handle-ephemeral-exit (quit-config)
7216   "Handle movement when leaving an ephemeral group.
7217 The state which existed when entering the ephemeral is reset."
7218   (if (not (buffer-name (car quit-config)))
7219       (gnus-configure-windows 'group 'force)
7220     (set-buffer (car quit-config))
7221     (cond ((eq major-mode 'gnus-summary-mode)
7222            (gnus-set-global-variables))
7223           ((eq major-mode 'gnus-article-mode)
7224            (save-current-buffer
7225              ;; The `gnus-summary-buffer' variable may point
7226              ;; to the old summary buffer when using a single
7227              ;; article buffer.
7228              (unless (gnus-buffer-live-p gnus-summary-buffer)
7229                (set-buffer gnus-group-buffer))
7230              (set-buffer gnus-summary-buffer)
7231              (gnus-set-global-variables))))
7232     (if (or (eq (cdr quit-config) 'article)
7233             (eq (cdr quit-config) 'pick))
7234         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7235             (gnus-configure-windows 'pick 'force)
7236           (gnus-configure-windows (cdr quit-config) 'force))
7237       (gnus-configure-windows (cdr quit-config) 'force))
7238     (when (eq major-mode 'gnus-summary-mode)
7239       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7240                                                      next-unread-noselect))
7241           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7242                                   'next-noselect)
7243                               (gnus-summary-next-subject 1 nil t))
7244                              ((eq gnus-auto-select-on-ephemeral-exit
7245                                   'next-unread-noselect)
7246                               (gnus-summary-next-subject 1 t t))))
7247             ;; Hide the article buffer which displays the article different
7248             ;; from the one that the cursor points to in the summary buffer.
7249             (gnus-configure-windows 'summary 'force))
7250         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7251                (gnus-summary-next-subject 1))
7252               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7253                (gnus-summary-next-subject 1 t))))
7254       (gnus-summary-recenter)
7255       (gnus-summary-position-point))))
7256
7257 ;;; Dead summaries.
7258
7259 (defvar gnus-dead-summary-mode-map
7260   (let ((map (make-keymap)))
7261     (suppress-keymap map)
7262     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7263     (dolist (key '("\C-d" "\r" "\177" [delete]))
7264       (define-key map key 'gnus-summary-wake-up-the-dead))
7265     (dolist (key '("q" "Q"))
7266       (define-key map key 'bury-buffer))
7267     map))
7268
7269 (define-minor-mode gnus-dead-summary-mode
7270   "Minor mode for Gnus summary buffers."
7271   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7272   (unless (derived-mode-p 'gnus-summary-mode)
7273     (setq gnus-dead-summary-mode nil)))
7274
7275 (defun gnus-deaden-summary ()
7276   "Make the current summary buffer into a dead summary buffer."
7277   ;; Kill any previous dead summary buffer.
7278   (when (and gnus-dead-summary
7279              (buffer-name gnus-dead-summary))
7280     (with-current-buffer gnus-dead-summary
7281       (when gnus-dead-summary-mode
7282         (kill-buffer (current-buffer)))))
7283   ;; Make this the current dead summary.
7284   (setq gnus-dead-summary (current-buffer))
7285   (gnus-dead-summary-mode 1)
7286   (let ((name (buffer-name)))
7287     (when (string-match "Summary" name)
7288       (rename-buffer
7289        (concat (substring name 0 (match-beginning 0)) "Dead "
7290                (substring name (match-beginning 0)))
7291        t)
7292       (bury-buffer))))
7293
7294 (defun gnus-kill-or-deaden-summary (buffer)
7295   "Kill or deaden the summary BUFFER."
7296   (save-excursion
7297     (when (and (buffer-name buffer)
7298                (not gnus-single-article-buffer))
7299       (with-current-buffer buffer
7300         (gnus-kill-buffer gnus-article-buffer)
7301         (gnus-kill-buffer gnus-original-article-buffer)))
7302     (cond
7303      ;; Kill the buffer.
7304      (gnus-kill-summary-on-exit
7305       (when (and gnus-use-trees
7306                  (gnus-buffer-exists-p buffer))
7307         (with-current-buffer buffer
7308           (gnus-tree-close gnus-newsgroup-name)))
7309       (gnus-kill-buffer buffer))
7310      ;; Deaden the buffer.
7311      ((gnus-buffer-exists-p buffer)
7312       (with-current-buffer buffer
7313         (gnus-deaden-summary))))))
7314
7315 (defun gnus-summary-wake-up-the-dead (&rest args)
7316   "Wake up the dead summary buffer."
7317   (interactive)
7318   (gnus-dead-summary-mode -1)
7319   (let ((name (buffer-name)))
7320     (when (string-match "Dead " name)
7321       (rename-buffer
7322        (concat (substring name 0 (match-beginning 0))
7323                (substring name (match-end 0)))
7324        t)))
7325   (gnus-message 3 "This dead summary is now alive again"))
7326
7327 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7328 (defun gnus-summary-fetch-faq (&optional faq-dir)
7329   "Fetch the FAQ for the current group.
7330 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7331 in."
7332   (interactive
7333    (list
7334     (when current-prefix-arg
7335       (completing-read
7336        "FAQ dir: " (and (listp gnus-group-faq-directory)
7337                         (mapcar 'list
7338                                 gnus-group-faq-directory))))))
7339   (let (gnus-faq-buffer)
7340     (when (setq gnus-faq-buffer
7341                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7342       (gnus-configure-windows 'summary-faq))))
7343
7344 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7345 (defun gnus-summary-describe-group (&optional force)
7346   "Describe the current newsgroup."
7347   (interactive "P")
7348   (gnus-group-describe-group force gnus-newsgroup-name))
7349
7350 (defun gnus-summary-describe-briefly ()
7351   "Describe summary mode commands briefly."
7352   (interactive)
7353   (gnus-message 6 "%s" (substitute-command-keys "\\<gnus-summary-mode-map>\\[gnus-summary-next-page]:Select  \\[gnus-summary-next-unread-article]:Forward  \\[gnus-summary-prev-unread-article]:Backward  \\[gnus-summary-exit]:Exit  \\[gnus-info-find-node]:Run Info   \\[gnus-summary-describe-briefly]:This help")))
7354
7355 ;; Walking around group mode buffer from summary mode.
7356
7357 (defun gnus-summary-next-group (&optional no-article target-group backward)
7358   "Exit current newsgroup and then select next unread newsgroup.
7359 If prefix argument NO-ARTICLE is non-nil, no article is selected
7360 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7361 previous group instead."
7362   (interactive "P")
7363   ;; Stop pre-fetching.
7364   (gnus-async-halt-prefetch)
7365   (let ((current-group gnus-newsgroup-name)
7366         (current-buffer (current-buffer))
7367         entered)
7368     ;; First we semi-exit this group to update Xrefs and all variables.
7369     ;; We can't do a real exit, because the window conf must remain
7370     ;; the same in case the user is prompted for info, and we don't
7371     ;; want the window conf to change before that...
7372     (gnus-summary-exit t)
7373     (while (not entered)
7374       ;; Then we find what group we are supposed to enter.
7375       (set-buffer gnus-group-buffer)
7376       (gnus-group-jump-to-group current-group)
7377       (setq target-group
7378             (or target-group
7379                 (if (eq gnus-keep-same-level 'best)
7380                     (gnus-summary-best-group gnus-newsgroup-name)
7381                   (gnus-summary-search-group backward gnus-keep-same-level))))
7382       (if (not target-group)
7383           ;; There are no further groups, so we return to the group
7384           ;; buffer.
7385           (progn
7386             (gnus-message 5 "Returning to the group buffer")
7387             (setq entered t)
7388             (when (gnus-buffer-live-p current-buffer)
7389               (set-buffer current-buffer)
7390               (gnus-summary-exit))
7391             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7392         ;; We try to enter the target group.
7393         (gnus-group-jump-to-group target-group)
7394         (let ((unreads (gnus-group-group-unread)))
7395           (if (and (or (eq t unreads)
7396                        (and unreads (not (zerop unreads))))
7397                    (gnus-summary-read-group
7398                     target-group nil no-article
7399                     (and (buffer-name current-buffer) current-buffer)
7400                     nil backward))
7401               (setq entered t)
7402             (setq current-group target-group
7403                   target-group nil)))))))
7404
7405 (defun gnus-summary-prev-group (&optional no-article)
7406   "Exit current newsgroup and then select previous unread newsgroup.
7407 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7408   (interactive "P")
7409   (gnus-summary-next-group no-article nil t))
7410
7411 ;; Walking around summary lines.
7412
7413 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7414   "Go to the first subject satisfying any non-nil constraint.
7415 If UNREAD is non-nil, the article should be unread.
7416 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7417 If UNSEEN is non-nil, the article should be unseen as well as unread.
7418 Returns the article selected or nil if there are no matching articles."
7419   (interactive "P")
7420   (cond
7421    ;; Empty summary.
7422    ((null gnus-newsgroup-data)
7423     (gnus-message 3 "No articles in the group")
7424     nil)
7425    ;; Pick the first article.
7426    ((not (or unread undownloaded unseen))
7427     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7428     (gnus-data-number (car gnus-newsgroup-data)))
7429    ;; Find the first unread article.
7430    (t
7431     (let ((data gnus-newsgroup-data))
7432       (while (and data
7433                   (let ((num (gnus-data-number (car data))))
7434                     (or (memq num gnus-newsgroup-unfetched)
7435                         (not (or (and unread
7436                                       (memq num gnus-newsgroup-unreads))
7437                                  (and undownloaded
7438                                       (memq num gnus-newsgroup-undownloaded))
7439                                  (and unseen
7440                                       (memq num gnus-newsgroup-unseen)
7441                                       (memq num gnus-newsgroup-unreads)))))))
7442         (setq data (cdr data)))
7443       (prog1
7444           (if data
7445               (progn
7446                 (goto-char (gnus-data-pos (car data)))
7447                 (gnus-data-number (car data)))
7448             (gnus-message 3 "No more%s articles"
7449                           (let* ((r (when unread " unread"))
7450                                  (d (when undownloaded " undownloaded"))
7451                                  (s (when unseen " unseen"))
7452                                  (l (delq nil (list r d s))))
7453                             (cond ((= 3 (length l))
7454                                    (concat r "," d ", or" s))
7455                                   ((= 2 (length l))
7456                                    (concat (car l) ", or" (cadr l)))
7457                                   ((= 1 (length l))
7458                                    (car l))
7459                                   (t
7460                                    ""))))
7461             nil
7462             )
7463         (gnus-summary-position-point))))))
7464
7465 (defun gnus-summary-next-subject (n &optional unread dont-display)
7466   "Go to next N'th summary line.
7467 If N is negative, go to the previous N'th subject line.
7468 If UNREAD is non-nil, only unread articles are selected.
7469 The difference between N and the actual number of steps taken is
7470 returned."
7471   (interactive "p")
7472   (let ((backward (< n 0))
7473         (n (abs n)))
7474     (while (and (> n 0)
7475                 (if backward
7476                     (gnus-summary-find-prev unread)
7477                   (gnus-summary-find-next unread)))
7478       (unless (zerop (setq n (1- n)))
7479         (gnus-summary-show-thread)))
7480     (when (/= 0 n)
7481       (gnus-message 7 "No more%s articles"
7482                     (if unread " unread" "")))
7483     (unless dont-display
7484       (gnus-summary-recenter)
7485       (gnus-summary-position-point))
7486     n))
7487
7488 (defun gnus-summary-next-unread-subject (n)
7489   "Go to next N'th unread summary line."
7490   (interactive "p")
7491   (gnus-summary-next-subject n t))
7492
7493 (defun gnus-summary-prev-subject (n &optional unread)
7494   "Go to previous N'th summary line.
7495 If optional argument UNREAD is non-nil, only unread article is selected."
7496   (interactive "p")
7497   (gnus-summary-next-subject (- n) unread))
7498
7499 (defun gnus-summary-prev-unread-subject (n)
7500   "Go to previous N'th unread summary line."
7501   (interactive "p")
7502   (gnus-summary-next-subject (- n) t))
7503
7504 (defun gnus-summary-goto-subjects (articles)
7505   "Insert the subject header for ARTICLES in the current buffer."
7506   (save-excursion
7507     (dolist (article articles)
7508       (gnus-summary-goto-subject article t)))
7509   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7510   (gnus-summary-position-point))
7511
7512 (defun gnus-summary-goto-subject (article &optional force silent)
7513   "Go to the subject line of ARTICLE.
7514 If FORCE, also allow jumping to articles not currently shown."
7515   (interactive "nArticle number: ")
7516   (unless (numberp article)
7517     (error "Article %s is not a number" article))
7518   (let ((b (point))
7519         (data (gnus-data-find article)))
7520     ;; We read in the article if we have to.
7521     (and (not data)
7522          force
7523          (gnus-summary-insert-subject
7524           article
7525           (if (or (numberp force) (vectorp force)) force)
7526           t)
7527          (setq data (gnus-data-find article)))
7528     (goto-char b)
7529     (if (not data)
7530         (progn
7531           (unless silent
7532             (gnus-message 3 "Can't find article %d" article))
7533           nil)
7534       (let ((pt (gnus-data-pos data)))
7535         (goto-char pt)
7536         (gnus-summary-set-article-display-arrow pt))
7537       (gnus-summary-position-point)
7538       article)))
7539
7540 ;; Walking around summary lines with displaying articles.
7541
7542 (defun gnus-summary-expand-window (&optional arg)
7543   "Make the summary buffer take up the entire Emacs frame.
7544 Given a prefix, will force an `article' buffer configuration."
7545   (interactive "P")
7546   (if arg
7547       (gnus-configure-windows 'article 'force)
7548     (gnus-configure-windows 'summary 'force)))
7549
7550 (defun gnus-summary-display-article (article &optional all-header)
7551   "Display ARTICLE in article buffer."
7552   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7553                (with-current-buffer gnus-article-buffer
7554                  (eq major-mode 'gnus-article-mode)))
7555     (gnus-article-setup-buffer))
7556   (gnus-set-global-variables)
7557   (with-current-buffer gnus-article-buffer
7558     (setq gnus-article-charset gnus-newsgroup-charset)
7559     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7560     (mm-enable-multibyte))
7561   (if (null article)
7562       nil
7563     (prog1
7564         (if gnus-summary-display-article-function
7565             (funcall gnus-summary-display-article-function article all-header)
7566           (gnus-article-prepare article all-header))
7567       (gnus-run-hooks 'gnus-select-article-hook)
7568       (when (and gnus-current-article
7569                  (not (zerop gnus-current-article)))
7570         (gnus-summary-goto-subject gnus-current-article))
7571       (gnus-summary-recenter)
7572       (when (and gnus-use-trees gnus-show-threads)
7573         (gnus-possibly-generate-tree article)
7574         (gnus-highlight-selected-tree article))
7575       ;; Successfully display article.
7576       (gnus-article-set-window-start
7577        (cdr (assq article gnus-newsgroup-bookmarks))))))
7578
7579 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7580   "Select the current article.
7581 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7582 non-nil, the article will be re-fetched even if it already present in
7583 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7584 be displayed."
7585   ;; Make sure we are in the summary buffer to work around bbdb bug.
7586   (unless (eq major-mode 'gnus-summary-mode)
7587     (set-buffer gnus-summary-buffer))
7588   (let ((article (or article (gnus-summary-article-number)))
7589         (all-headers (not (not all-headers))) ;Must be t or nil.
7590         gnus-summary-display-article-function)
7591     (and (not pseudo)
7592          (gnus-summary-article-pseudo-p article)
7593          (error "This is a pseudo-article"))
7594     (with-current-buffer gnus-summary-buffer
7595       (if (or (and gnus-single-article-buffer
7596                    (or (null gnus-current-article)
7597                        (null gnus-article-current)
7598                        (null (get-buffer gnus-article-buffer))
7599                        (not (eq article (cdr gnus-article-current)))
7600                        (not (equal (car gnus-article-current)
7601                                    gnus-newsgroup-name))))
7602               (and (not gnus-single-article-buffer)
7603                    (or (null gnus-current-article)
7604                        (not (eq gnus-current-article article))))
7605               force)
7606           ;; The requested article is different from the current article.
7607           (progn
7608             (gnus-summary-display-article article all-headers)
7609             (when (gnus-buffer-live-p gnus-article-buffer)
7610               (with-current-buffer gnus-article-buffer
7611                 (if (not gnus-article-decoded-p) ;; a local variable
7612                     (mm-disable-multibyte))))
7613             (gnus-article-set-window-start
7614              (cdr (assq article gnus-newsgroup-bookmarks)))
7615             article)
7616         'old))))
7617
7618 (defun gnus-summary-force-verify-and-decrypt ()
7619   "Display buttons for signed/encrypted parts and verify/decrypt them."
7620   (interactive)
7621   (let ((mm-verify-option 'known)
7622         (mm-decrypt-option 'known)
7623         (gnus-article-emulate-mime t)
7624         (gnus-buttonized-mime-types (append (list "multipart/signed"
7625                                                   "multipart/encrypted")
7626                                             gnus-buttonized-mime-types)))
7627     (gnus-summary-select-article nil 'force)))
7628
7629 (defun gnus-summary-set-current-mark (&optional current-mark)
7630   "Obsolete function."
7631   nil)
7632
7633 (defun gnus-summary-next-article (&optional unread subject backward push)
7634   "Select the next article.
7635 If UNREAD, only unread articles are selected.
7636 If SUBJECT, only articles with SUBJECT are selected.
7637 If BACKWARD, the previous article is selected instead of the next."
7638   (interactive "P")
7639   ;; Make sure we are in the summary buffer.
7640   (unless (eq major-mode 'gnus-summary-mode)
7641     (set-buffer gnus-summary-buffer))
7642   (cond
7643    ;; Is there such an article?
7644    ((and (gnus-summary-search-forward unread subject backward)
7645          (or (gnus-summary-display-article (gnus-summary-article-number))
7646              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7647     (gnus-summary-position-point))
7648    ;; If not, we try the first unread, if that is wanted.
7649    ((and subject
7650          gnus-auto-select-same
7651          (gnus-summary-first-unread-article))
7652     (gnus-summary-position-point)
7653     (gnus-message 6 "Wrapped"))
7654    ;; Try to get next/previous article not displayed in this group.
7655    ((and gnus-auto-extend-newsgroup
7656          (not unread) (not subject))
7657     (gnus-summary-goto-article
7658      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7659      nil (count-lines (point-min) (point))))
7660    ;; Go to next/previous group.
7661    (t
7662     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7663       (gnus-summary-jump-to-group gnus-newsgroup-name))
7664     (let ((cmd (if (featurep 'xemacs)
7665                    last-command-char
7666                  last-command-event))
7667           (point
7668            (with-current-buffer gnus-group-buffer
7669              (point)))
7670           (group
7671            (if (eq gnus-keep-same-level 'best)
7672                (gnus-summary-best-group gnus-newsgroup-name)
7673              (gnus-summary-search-group backward gnus-keep-same-level))))
7674       ;; For some reason, the group window gets selected.  We change
7675       ;; it back.
7676       (select-window (get-buffer-window (current-buffer)))
7677       ;; Select next unread newsgroup automagically.
7678       (cond
7679        ((or (not gnus-auto-select-next)
7680             (not cmd))
7681         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7682        ((or (eq gnus-auto-select-next 'quietly)
7683             (and (eq gnus-auto-select-next 'slightly-quietly)
7684                  push)
7685             (and (eq gnus-auto-select-next 'almost-quietly)
7686                  (gnus-summary-last-article-p)))
7687         ;; Select quietly.
7688         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7689             (gnus-summary-exit)
7690           (gnus-message 7 "No more%s articles (%s)..."
7691                         (if unread " unread" "")
7692                         (if group (concat "selecting " group)
7693                           "exiting"))
7694           (gnus-summary-next-group nil group backward)))
7695        (t
7696         (when (gnus-key-press-event-p last-input-event)
7697           (gnus-summary-walk-group-buffer
7698            gnus-newsgroup-name cmd unread backward point))))))))
7699
7700 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7701   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7702                       (?\C-p (gnus-group-prev-unread-group 1))))
7703         (cursor-in-echo-area t)
7704         keve key group ended prompt)
7705     (with-current-buffer gnus-group-buffer
7706       (goto-char start)
7707       (setq group
7708             (if (eq gnus-keep-same-level 'best)
7709                 (gnus-summary-best-group gnus-newsgroup-name)
7710               (gnus-summary-search-group backward gnus-keep-same-level))))
7711     (while (not ended)
7712       (setq prompt
7713             (format
7714              "No more%s articles%s " (if unread " unread" "")
7715              (if (and group
7716                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7717                  (format " (Type %s for %s [%s])"
7718                          (single-key-description cmd)
7719                          (gnus-group-decoded-name group)
7720                          (gnus-group-unread group))
7721                (format " (Type %s to exit %s)"
7722                        (single-key-description cmd)
7723                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7724       ;; Confirm auto selection.
7725       (setq key (car (setq keve (gnus-read-event-char prompt)))
7726             ended t)
7727       (cond
7728        ((assq key keystrokes)
7729         (let ((obuf (current-buffer)))
7730           (switch-to-buffer gnus-group-buffer)
7731           (when group
7732             (gnus-group-jump-to-group group))
7733           (eval (cadr (assq key keystrokes)))
7734           (setq group (gnus-group-group-name))
7735           (switch-to-buffer obuf))
7736         (setq ended nil))
7737        ((equal key cmd)
7738         (if (or (not group)
7739                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7740             (gnus-summary-exit)
7741           (gnus-summary-next-group nil group backward)))
7742        (t
7743         (push (cdr keve) unread-command-events))))))
7744
7745 (defun gnus-summary-next-unread-article ()
7746   "Select unread article after current one."
7747   (interactive)
7748   (gnus-summary-next-article
7749    (or (not (eq gnus-summary-goto-unread 'never))
7750        (gnus-summary-last-article-p (gnus-summary-article-number)))
7751    (and gnus-auto-select-same
7752         (gnus-summary-article-subject))))
7753
7754 (defun gnus-summary-prev-article (&optional unread subject)
7755   "Select the article before the current one.
7756 If UNREAD is non-nil, only unread articles are selected."
7757   (interactive "P")
7758   (gnus-summary-next-article unread subject t))
7759
7760 (defun gnus-summary-prev-unread-article ()
7761   "Select unread article before current one."
7762   (interactive)
7763   (gnus-summary-prev-article
7764    (or (not (eq gnus-summary-goto-unread 'never))
7765        (gnus-summary-first-article-p (gnus-summary-article-number)))
7766    (and gnus-auto-select-same
7767         (gnus-summary-article-subject))))
7768
7769 (defun gnus-summary-next-page (&optional lines circular stop)
7770   "Show next page of the selected article.
7771 If at the end of the current article, select the next article.
7772 LINES says how many lines should be scrolled up.
7773
7774 If CIRCULAR is non-nil, go to the start of the article instead of
7775 selecting the next article when reaching the end of the current
7776 article.
7777
7778 If STOP is non-nil, just stop when reaching the end of the message.
7779
7780 Also see the variable `gnus-article-skip-boring'."
7781   (interactive "P")
7782   (setq gnus-summary-buffer (current-buffer))
7783   (gnus-set-global-variables)
7784   (let ((article (gnus-summary-article-number))
7785         (article-window (get-buffer-window gnus-article-buffer t))
7786         endp)
7787     ;; If the buffer is empty, we have no article.
7788     (unless article
7789       (error "No article to select"))
7790     (gnus-configure-windows 'article)
7791     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7792         (if (and (eq gnus-summary-goto-unread 'never)
7793                  (not (gnus-summary-last-article-p article)))
7794             (gnus-summary-next-article)
7795           (gnus-summary-next-unread-article))
7796       (if (or (null gnus-current-article)
7797               (null gnus-article-current)
7798               (/= article (cdr gnus-article-current))
7799               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7800           ;; Selected subject is different from current article's.
7801           (gnus-summary-display-article article)
7802         (when article-window
7803           (gnus-eval-in-buffer-window gnus-article-buffer
7804             (setq endp (or (gnus-article-next-page lines)
7805                            (gnus-article-only-boring-p))))
7806           (when endp
7807             (cond ((or stop gnus-summary-stop-at-end-of-message)
7808                    (gnus-message 3 "End of message"))
7809                   (circular
7810                    (gnus-summary-beginning-of-article))
7811                   (lines
7812                    (gnus-message 3 "End of message"))
7813                   ((null lines)
7814                    (if (and (eq gnus-summary-goto-unread 'never)
7815                             (not (gnus-summary-last-article-p article)))
7816                        (gnus-summary-next-article)
7817                      (gnus-summary-next-unread-article))))))))
7818     (gnus-summary-recenter)
7819     (gnus-summary-position-point)))
7820
7821 (defun gnus-summary-prev-page (&optional lines move)
7822   "Show previous page of selected article.
7823 Argument LINES specifies lines to be scrolled down.
7824 If MOVE, move to the previous unread article if point is at
7825 the beginning of the buffer."
7826   (interactive "P")
7827   (let ((article (gnus-summary-article-number))
7828         (article-window (get-buffer-window gnus-article-buffer t))
7829         endp)
7830     (gnus-configure-windows 'article)
7831     (if (or (null gnus-current-article)
7832             (null gnus-article-current)
7833             (/= article (cdr gnus-article-current))
7834             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7835         ;; Selected subject is different from current article's.
7836         (gnus-summary-display-article article)
7837       (gnus-summary-recenter)
7838       (when article-window
7839         (gnus-eval-in-buffer-window gnus-article-buffer
7840           (setq endp (gnus-article-prev-page lines)))
7841         (when (and move endp)
7842           (cond (lines
7843                  (gnus-message 3 "Beginning of message"))
7844                 ((null lines)
7845                  (if (and (eq gnus-summary-goto-unread 'never)
7846                           (not (gnus-summary-first-article-p article)))
7847                      (gnus-summary-prev-article)
7848                    (gnus-summary-prev-unread-article))))))))
7849   (gnus-summary-position-point))
7850
7851 (defun gnus-summary-prev-page-or-article (&optional lines)
7852   "Show previous page of selected article.
7853 Argument LINES specifies lines to be scrolled down.
7854 If at the beginning of the article, go to the next article."
7855   (interactive "P")
7856   (gnus-summary-prev-page lines t))
7857
7858 (defun gnus-summary-scroll-up (lines)
7859   "Scroll up (or down) one line current article.
7860 Argument LINES specifies lines to be scrolled up (or down if negative)."
7861   (interactive "p")
7862   (gnus-configure-windows 'article)
7863   (gnus-summary-show-thread)
7864   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7865     (gnus-eval-in-buffer-window gnus-article-buffer
7866       (cond ((> lines 0)
7867              (when (gnus-article-next-page lines)
7868                (gnus-message 3 "End of message")))
7869             ((< lines 0)
7870              (gnus-article-prev-page (- lines))))))
7871   (gnus-summary-recenter)
7872   (gnus-summary-position-point))
7873
7874 (defun gnus-summary-scroll-down (lines)
7875   "Scroll down (or up) one line current article.
7876 Argument LINES specifies lines to be scrolled down (or up if negative)."
7877   (interactive "p")
7878   (gnus-summary-scroll-up (- lines)))
7879
7880 (defun gnus-summary-next-same-subject ()
7881   "Select next article which has the same subject as current one."
7882   (interactive)
7883   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7884
7885 (defun gnus-summary-prev-same-subject ()
7886   "Select previous article which has the same subject as current one."
7887   (interactive)
7888   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7889
7890 (defun gnus-summary-next-unread-same-subject ()
7891   "Select next unread article which has the same subject as current one."
7892   (interactive)
7893   (gnus-summary-next-article t (gnus-summary-article-subject)))
7894
7895 (defun gnus-summary-prev-unread-same-subject ()
7896   "Select previous unread article which has the same subject as current one."
7897   (interactive)
7898   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7899
7900 (defun gnus-summary-first-unread-article ()
7901   "Select the first unread article.
7902 Return nil if there are no unread articles."
7903   (interactive)
7904   (prog1
7905       (when (gnus-summary-first-subject t)
7906         (gnus-summary-show-thread)
7907         (gnus-summary-first-subject t)
7908         (gnus-summary-display-article (gnus-summary-article-number)))
7909     (gnus-summary-position-point)))
7910
7911 (defun gnus-summary-first-unread-subject ()
7912   "Place the point on the subject line of the first unread article.
7913 Return nil if there are no unread articles."
7914   (interactive)
7915   (prog1
7916       (when (gnus-summary-first-subject t)
7917         (gnus-summary-show-thread)
7918         (gnus-summary-first-subject t))
7919     (gnus-summary-position-point)))
7920
7921 (defun gnus-summary-first-unseen-subject ()
7922   "Place the point on the subject line of the first unseen article.
7923 Return nil if there are no unseen articles."
7924   (interactive)
7925   (prog1
7926       (when (gnus-summary-first-subject nil nil t)
7927         (gnus-summary-show-thread)
7928         (gnus-summary-first-subject nil nil t))
7929     (gnus-summary-position-point)))
7930
7931 (defun gnus-summary-first-unseen-or-unread-subject ()
7932   "Place the point on the subject line of the first unseen and unread article.
7933 If all article have been seen, on the subject line of the first unread
7934 article."
7935   (interactive)
7936   (prog1
7937       (unless (when (gnus-summary-first-subject nil nil t)
7938                 (gnus-summary-show-thread)
7939                 (gnus-summary-first-subject nil nil t))
7940         (when (gnus-summary-first-subject t)
7941           (gnus-summary-show-thread)
7942           (gnus-summary-first-subject t)))
7943     (gnus-summary-position-point)))
7944
7945 (defun gnus-summary-first-article ()
7946   "Select the first article.
7947 Return nil if there are no articles."
7948   (interactive)
7949   (prog1
7950       (when (gnus-summary-first-subject)
7951         (gnus-summary-show-thread)
7952         (gnus-summary-first-subject)
7953         (gnus-summary-display-article (gnus-summary-article-number)))
7954     (gnus-summary-position-point)))
7955
7956 (defun gnus-summary-best-unread-article (&optional arg)
7957   "Select the unread article with the highest score.
7958 If given a prefix argument, select the next unread article that has a
7959 score higher than the default score."
7960   (interactive "P")
7961   (let ((article (if arg
7962                      (gnus-summary-better-unread-subject)
7963                    (gnus-summary-best-unread-subject))))
7964     (if article
7965         (gnus-summary-goto-article article)
7966       (error "No unread articles"))))
7967
7968 (defun gnus-summary-best-unread-subject ()
7969   "Select the unread subject with the highest score."
7970   (interactive)
7971   (let ((best -1000000)
7972         (data gnus-newsgroup-data)
7973         article score)
7974     (while data
7975       (and (gnus-data-unread-p (car data))
7976            (> (setq score
7977                     (gnus-summary-article-score (gnus-data-number (car data))))
7978               best)
7979            (setq best score
7980                  article (gnus-data-number (car data))))
7981       (setq data (cdr data)))
7982     (when article
7983       (gnus-summary-goto-subject article))
7984     (gnus-summary-position-point)
7985     article))
7986
7987 (defun gnus-summary-better-unread-subject ()
7988   "Select the first unread subject that has a score over the default score."
7989   (interactive)
7990   (let ((data gnus-newsgroup-data)
7991         article score)
7992     (while (and (setq article (gnus-data-number (car data)))
7993                 (or (gnus-data-read-p (car data))
7994                     (not (> (gnus-summary-article-score article)
7995                             gnus-summary-default-score))))
7996       (setq data (cdr data)))
7997     (when article
7998       (gnus-summary-goto-subject article))
7999     (gnus-summary-position-point)
8000     article))
8001
8002 (defun gnus-summary-last-subject ()
8003   "Go to the last displayed subject line in the group."
8004   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8005     (when article
8006       (gnus-summary-goto-subject article))))
8007
8008 (defun gnus-summary-goto-article (article &optional all-headers force)
8009   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8010 If ALL-HEADERS is non-nil, no header lines are hidden.
8011 If FORCE, go to the article even if it isn't displayed.  If FORCE
8012 is a number, it is the line the article is to be displayed on."
8013   (interactive
8014    (list
8015     (completing-read
8016      "Article number or Message-ID: "
8017      (mapcar (lambda (number) (list (int-to-string number)))
8018              gnus-newsgroup-limit))
8019     current-prefix-arg
8020     t))
8021   (prog1
8022       (if (and (stringp article)
8023                (string-match "@\\|%40" article))
8024           (gnus-summary-refer-article article)
8025         (when (stringp article)
8026           (setq article (string-to-number article)))
8027         (if (gnus-summary-goto-subject article force)
8028             (gnus-summary-display-article article all-headers)
8029           (gnus-message 4 "Couldn't go to article %s" article) nil))
8030     (gnus-summary-position-point)))
8031
8032 (defun gnus-summary-goto-last-article ()
8033   "Go to the previously read article."
8034   (interactive)
8035   (prog1
8036       (when gnus-last-article
8037         (gnus-summary-goto-article gnus-last-article nil t))
8038     (gnus-summary-position-point)))
8039
8040 (defun gnus-summary-pop-article (number)
8041   "Pop one article off the history and go to the previous.
8042 NUMBER articles will be popped off."
8043   (interactive "p")
8044   (let (to)
8045     (setq gnus-newsgroup-history
8046           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8047     (if to
8048         (gnus-summary-goto-article (car to) nil t)
8049       (error "Article history empty")))
8050   (gnus-summary-position-point))
8051
8052 ;; Summary commands and functions for limiting the summary buffer.
8053
8054 (defun gnus-summary-limit-to-articles (n)
8055   "Limit the summary buffer to the next N articles.
8056 If not given a prefix, use the process marked articles instead."
8057   (interactive "P")
8058   (prog1
8059       (let ((articles (gnus-summary-work-articles n)))
8060         (setq gnus-newsgroup-processable nil)
8061         (gnus-summary-limit articles))
8062     (gnus-summary-position-point)))
8063
8064 (defun gnus-summary-pop-limit (&optional total)
8065   "Restore the previous limit.
8066 If given a prefix, remove all limits."
8067   (interactive "P")
8068   (when total
8069     (setq gnus-newsgroup-limits
8070           (list (mapcar (lambda (h) (mail-header-number h))
8071                         gnus-newsgroup-headers))))
8072   (unless gnus-newsgroup-limits
8073     (error "No limit to pop"))
8074   (prog1
8075       (gnus-summary-limit nil 'pop)
8076     (gnus-summary-position-point)))
8077
8078 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8079   "Limit the summary buffer to articles that have subjects that match a regexp.
8080 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8081   (interactive
8082    (list (read-string (if current-prefix-arg
8083                           "Exclude subject (regexp): "
8084                         "Limit to subject (regexp): "))
8085          nil current-prefix-arg))
8086   (unless header
8087     (setq header "subject"))
8088   (when (not (equal "" subject))
8089     (prog1
8090         (let ((articles (gnus-summary-find-matching
8091                          (or header "subject") subject 'all nil nil
8092                          not-matching)))
8093           (unless articles
8094             (error "Found no matches for \"%s\"" subject))
8095           (gnus-summary-limit articles))
8096       (gnus-summary-position-point))))
8097
8098 (defun gnus-summary-limit-to-author (from &optional not-matching)
8099   "Limit the summary buffer to articles that have authors that match a regexp.
8100 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8101   (interactive
8102    (list (read-string (if current-prefix-arg
8103                           "Exclude author (regexp): "
8104                         "Limit to author (regexp): "))
8105          current-prefix-arg))
8106   (gnus-summary-limit-to-subject from "from" not-matching))
8107
8108 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8109   "Limit the summary buffer to articles with the given RECIPIENT.
8110
8111 If NOT-MATCHING, exclude RECIPIENT.
8112
8113 To and Cc headers are checked.  You need to include them in
8114 `nnmail-extra-headers'."
8115   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8116   (interactive
8117    (list (read-string (format "%s recipient (regexp): "
8118                               (if current-prefix-arg "Exclude" "Limit to")))
8119          current-prefix-arg))
8120   (when (not (equal "" recipient))
8121     (prog1 (let* ((to
8122                    (if (memq 'To nnmail-extra-headers)
8123                        (gnus-summary-find-matching
8124                         (cons 'extra 'To) recipient 'all nil nil
8125                         not-matching)
8126                      (gnus-message
8127                       1 "`To' isn't present in `nnmail-extra-headers'")
8128                      (sit-for 1)
8129                      nil))
8130                   (cc
8131                    (if (memq 'Cc nnmail-extra-headers)
8132                        (gnus-summary-find-matching
8133                         (cons 'extra 'Cc) recipient 'all nil nil
8134                         not-matching)
8135                      (gnus-message
8136                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8137                      (sit-for 1)
8138                      nil))
8139                   (articles
8140                    (if not-matching
8141                        ;; We need the numbers that are in both lists:
8142                        (mapcar (lambda (a)
8143                                  (and (memq a to) a))
8144                                cc)
8145                      (nconc to cc))))
8146              (unless articles
8147                (error "Found no matches for \"%s\"" recipient))
8148              (gnus-summary-limit articles))
8149       (gnus-summary-position-point))))
8150
8151 (defun gnus-summary-limit-to-address (address &optional not-matching)
8152   "Limit the summary buffer to articles with the given ADDRESS.
8153
8154 If NOT-MATCHING, exclude ADDRESS.
8155
8156 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8157 in `nnmail-extra-headers'."
8158   (interactive
8159    (list (read-string (format "%s address (regexp): "
8160                               (if current-prefix-arg "Exclude" "Limit to")))
8161          current-prefix-arg))
8162   (when (not (equal "" address))
8163     (prog1 (let* ((to
8164                    (if (memq 'To nnmail-extra-headers)
8165                        (gnus-summary-find-matching
8166                         (cons 'extra 'To) address 'all nil nil
8167                         not-matching)
8168                      (gnus-message
8169                       1 "`To' isn't present in `nnmail-extra-headers'")
8170                      (sit-for 1)
8171                      t))
8172                   (cc
8173                    (if (memq 'Cc nnmail-extra-headers)
8174                        (gnus-summary-find-matching
8175                         (cons 'extra 'Cc) address 'all nil nil
8176                         not-matching)
8177                      (gnus-message
8178                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8179                      (sit-for 1)
8180                      t))
8181                   (from
8182                    (gnus-summary-find-matching "from" address
8183                                                'all nil nil not-matching))
8184                   (articles
8185                    (if not-matching
8186                        ;; We need the numbers that are in all lists:
8187                        (if (eq cc t)
8188                            (if (eq to t)
8189                                from
8190                              (mapcar (lambda (a) (car (memq a from))) to))
8191                          (if (eq to t)
8192                              (mapcar (lambda (a) (car (memq a from))) cc)
8193                            (mapcar (lambda (a) (car (memq a from)))
8194                                    (mapcar (lambda (a) (car (memq a to)))
8195                                            cc))))
8196                      (nconc (if (eq to t) nil to)
8197                             (if (eq cc t) nil cc)
8198                             from))))
8199              (unless articles
8200                (error "Found no matches for \"%s\"" address))
8201              (gnus-summary-limit articles))
8202       (gnus-summary-position-point))))
8203
8204 (defun gnus-summary-limit-strange-charsets-predicate (header)
8205   (when (fboundp 'char-charset)
8206     (let ((string (concat (mail-header-subject header)
8207                           (mail-header-from header)))
8208           charset found)
8209       (dotimes (i (1- (length string)))
8210         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8211         (when (string-match "unicode\\|big\\|japanese" charset)
8212           (setq found t)))
8213       found)))
8214
8215 (defun gnus-summary-limit-to-predicate (predicate)
8216   "Limit to articles where PREDICATE returns non-nil.
8217 PREDICATE will be called with the header structures of the
8218 articles."
8219   (let ((articles nil)
8220         (case-fold-search t))
8221     (dolist (header gnus-newsgroup-headers)
8222       (when (funcall predicate header)
8223         (push (mail-header-number header) articles)))
8224     (gnus-summary-limit (nreverse articles))))
8225
8226 (defun gnus-summary-limit-to-age (age &optional younger-p)
8227   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8228 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8229 articles that are younger than AGE days."
8230   (interactive
8231    (let ((younger current-prefix-arg)
8232          (days-got nil)
8233          days)
8234      (while (not days-got)
8235        (setq days (if younger
8236                       (read-string "Limit to articles younger than (in days, older when negative): ")
8237                     (read-string
8238                      "Limit to articles older than (in days, younger when negative): ")))
8239        (when (> (length days) 0)
8240          (setq days (read days)))
8241        (if (numberp days)
8242            (progn
8243              (setq days-got t)
8244              (when (< days 0)
8245                (setq younger (not younger))
8246                (setq days (* days -1))))
8247          (message "Please enter a number.")
8248          (sleep-for 1)))
8249      (list days younger)))
8250   (prog1
8251       (let ((data gnus-newsgroup-data)
8252             (cutoff (days-to-time age))
8253             articles d date is-younger)
8254         (while (setq d (pop data))
8255           (when (and (vectorp (gnus-data-header d))
8256                      (setq date (mail-header-date (gnus-data-header d))))
8257             (setq is-younger (time-less-p
8258                               (time-since (gnus-date-get-time date))
8259                               cutoff))
8260             (when (if younger-p
8261                       is-younger
8262                     (not is-younger))
8263               (push (gnus-data-number d) articles))))
8264         (gnus-summary-limit (nreverse articles)))
8265     (gnus-summary-position-point)))
8266
8267 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8268   "Limit the summary buffer to articles that match an 'extra' header."
8269   (interactive
8270    (let ((header
8271           (intern
8272            (gnus-completing-read-with-default
8273             (symbol-name (car gnus-extra-headers))
8274             (if current-prefix-arg
8275                 "Exclude extra header"
8276               "Limit extra header")
8277             (mapcar (lambda (x)
8278                       (cons (symbol-name x) x))
8279                     gnus-extra-headers)
8280             nil
8281             t))))
8282      (list header
8283            (read-string (format "%s header %s (regexp): "
8284                                 (if current-prefix-arg "Exclude" "Limit to")
8285                                 header))
8286            current-prefix-arg)))
8287   (when (not (equal "" regexp))
8288     (prog1
8289         (let ((articles (gnus-summary-find-matching
8290                          (cons 'extra header) regexp 'all nil nil
8291                          not-matching)))
8292           (unless articles
8293             (error "Found no matches for \"%s\"" regexp))
8294           (gnus-summary-limit articles))
8295       (gnus-summary-position-point))))
8296
8297 (defun gnus-summary-limit-to-display-predicate ()
8298   "Limit the summary buffer to the predicated in the `display' group parameter."
8299   (interactive)
8300   (unless gnus-newsgroup-display
8301     (error "There is no `display' group parameter"))
8302   (let (articles)
8303     (dolist (number gnus-newsgroup-articles)
8304       (when (funcall gnus-newsgroup-display)
8305         (push number articles)))
8306     (gnus-summary-limit articles))
8307   (gnus-summary-position-point))
8308
8309 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8310 (make-obsolete
8311  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread "Emacs 20.4")
8312
8313 (defun gnus-summary-limit-to-unread (&optional all)
8314   "Limit the summary buffer to articles that are not marked as read.
8315 If ALL is non-nil, limit strictly to unread articles."
8316   (interactive "P")
8317   (if all
8318       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8319     (gnus-summary-limit-to-marks
8320      ;; Concat all the marks that say that an article is read and have
8321      ;; those removed.
8322      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8323            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8324            gnus-low-score-mark gnus-expirable-mark
8325            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8326            gnus-duplicate-mark)
8327      'reverse)))
8328
8329 (defun gnus-summary-limit-to-headers (match &optional reverse)
8330   "Limit the summary buffer to articles that have headers that match MATCH.
8331 If REVERSE (the prefix), limit to articles that don't match."
8332   (interactive "sMatch headers (regexp): \nP")
8333   (gnus-summary-limit-to-bodies match reverse t))
8334
8335 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8336   "Limit the summary buffer to articles that have bodies that match MATCH.
8337 If REVERSE (the prefix), limit to articles that don't match."
8338   (interactive "sMatch body (regexp): \nP")
8339   (let ((articles nil)
8340         (gnus-select-article-hook nil)  ;Disable hook.
8341         (gnus-article-prepare-hook nil)
8342         (gnus-use-article-prefetch nil)
8343         (gnus-keep-backlog nil)
8344         (gnus-break-pages nil)
8345         (gnus-summary-display-arrow nil)
8346         (gnus-updated-mode-lines nil)
8347         (gnus-auto-center-summary nil)
8348         (gnus-display-mime-function nil))
8349     (dolist (data gnus-newsgroup-data)
8350       (let (gnus-mark-article-hook)
8351         (gnus-summary-select-article t t nil (gnus-data-number data)))
8352       (with-current-buffer gnus-article-buffer
8353         (article-goto-body)
8354         (let* ((case-fold-search t)
8355                (found (if headersp
8356                           (re-search-backward match nil t)
8357                         (re-search-forward match nil t))))
8358           (when (or (and found
8359                          (not reverse))
8360                     (and (not found)
8361                          reverse))
8362             (push (gnus-data-number data) articles)))))
8363     (if (not articles)
8364         (message "No messages matched")
8365       (gnus-summary-limit articles)))
8366   (gnus-summary-position-point))
8367
8368 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8369   "Limit the summary buffer to articles that aren't part on any thread.
8370 If THREADSP (the prefix), limit to articles that are in threads."
8371   (interactive "P")
8372   (let ((articles nil)
8373         thread-articles
8374         threads)
8375     (dolist (thread gnus-newsgroup-threads)
8376       (if (stringp (car thread))
8377           (dolist (thread (cdr thread))
8378             (push thread threads))
8379         (push thread threads)))
8380     (dolist (thread threads)
8381       (setq thread-articles (gnus-articles-in-thread thread))
8382       (when (or (and threadsp
8383                      (> (length thread-articles) 1))
8384                 (and (not threadsp)
8385                      (= (length thread-articles) 1)))
8386         (setq articles (nconc thread-articles articles))))
8387     (if (not articles)
8388         (message "No messages matched")
8389       (gnus-summary-limit articles))
8390     (gnus-summary-position-point)))
8391
8392 (defun gnus-summary-limit-to-replied (&optional unreplied)
8393   "Limit the summary buffer to replied articles.
8394 If UNREPLIED (the prefix), limit to unreplied articles."
8395   (interactive "P")
8396   (if unreplied
8397       (gnus-summary-limit
8398        (gnus-set-difference gnus-newsgroup-articles
8399         gnus-newsgroup-replied))
8400     (gnus-summary-limit gnus-newsgroup-replied))
8401   (gnus-summary-position-point))
8402
8403 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8404 (make-obsolete 'gnus-summary-delete-marked-with
8405                'gnus-summary-limit-exclude-marks "Emacs 20.4")
8406
8407 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8408   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8409 If REVERSE, limit the summary buffer to articles that are marked
8410 with MARKS.  MARKS can either be a string of marks or a list of marks.
8411 Returns how many articles were removed."
8412   (interactive "sMarks: ")
8413   (gnus-summary-limit-to-marks marks t))
8414
8415 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8416   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8417 If REVERSE (the prefix), limit the summary buffer to articles that are
8418 not marked with MARKS.  MARKS can either be a string of marks or a
8419 list of marks.
8420 Returns how many articles were removed."
8421   (interactive "sMarks: \nP")
8422   (prog1
8423       (let ((data gnus-newsgroup-data)
8424             (marks (if (listp marks) marks
8425                      (append marks nil))) ; Transform to list.
8426             articles)
8427         (while data
8428           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8429                   (memq (gnus-data-mark (car data)) marks))
8430             (push (gnus-data-number (car data)) articles))
8431           (setq data (cdr data)))
8432         (gnus-summary-limit articles))
8433     (gnus-summary-position-point)))
8434
8435 (defun gnus-summary-limit-to-score (score)
8436   "Limit to articles with score at or above SCORE."
8437   (interactive "NLimit to articles with score of at least: ")
8438   (let ((data gnus-newsgroup-data)
8439         articles)
8440     (while data
8441       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8442                 score)
8443         (push (gnus-data-number (car data)) articles))
8444       (setq data (cdr data)))
8445     (prog1
8446         (gnus-summary-limit articles)
8447       (gnus-summary-position-point))))
8448
8449 (defun gnus-summary-limit-to-unseen ()
8450   "Limit to unseen articles."
8451   (interactive)
8452   (prog1
8453       (gnus-summary-limit gnus-newsgroup-unseen)
8454     (gnus-summary-position-point)))
8455
8456 (defun gnus-summary-limit-include-thread (id)
8457   "Display all the hidden articles that is in the thread with ID in it.
8458 When called interactively, ID is the Message-ID of the current
8459 article."
8460   (interactive (list (mail-header-id (gnus-summary-article-header))))
8461   (let ((articles (gnus-articles-in-thread
8462                    (gnus-id-to-thread (gnus-root-id id)))))
8463     (prog1
8464         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8465       (gnus-summary-limit-include-matching-articles
8466        "subject"
8467        (regexp-quote (gnus-simplify-subject-re
8468                       (mail-header-subject (gnus-id-to-header id)))))
8469       (gnus-summary-position-point))))
8470
8471 (defun gnus-summary-limit-include-matching-articles (header regexp)
8472   "Display all the hidden articles that have HEADERs that match REGEXP."
8473   (interactive (list (read-string "Match on header: ")
8474                      (read-string "Regexp: ")))
8475   (let ((articles (gnus-find-matching-articles header regexp)))
8476     (prog1
8477         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8478       (gnus-summary-position-point))))
8479
8480 (defun gnus-summary-insert-dormant-articles ()
8481   "Insert all the dormant articles for this group into the current buffer."
8482   (interactive)
8483   (let ((gnus-verbose (max 6 gnus-verbose)))
8484     (if (not gnus-newsgroup-dormant)
8485         (gnus-message 3 "No dormant articles for this group")
8486       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8487
8488 (defun gnus-summary-insert-ticked-articles ()
8489   "Insert ticked articles for this group into the current buffer."
8490   (interactive)
8491   (let ((gnus-verbose (max 6 gnus-verbose)))
8492     (if (not gnus-newsgroup-marked)
8493         (gnus-message 3 "No ticked articles for this group")
8494       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8495
8496 (defun gnus-summary-limit-include-dormant ()
8497   "Display all the hidden articles that are marked as dormant.
8498 Note that this command only works on a subset of the articles currently
8499 fetched for this group."
8500   (interactive)
8501   (unless gnus-newsgroup-dormant
8502     (error "There are no dormant articles in this group"))
8503   (prog1
8504       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8505     (gnus-summary-position-point)))
8506
8507 (defun gnus-summary-limit-exclude-dormant ()
8508   "Hide all dormant articles."
8509   (interactive)
8510   (prog1
8511       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8512     (gnus-summary-position-point)))
8513
8514 (defun gnus-summary-limit-exclude-childless-dormant ()
8515   "Hide all dormant articles that have no children."
8516   (interactive)
8517   (let ((data (gnus-data-list t))
8518         articles d children)
8519     ;; Find all articles that are either not dormant or have
8520     ;; children.
8521     (while (setq d (pop data))
8522       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8523                 (and (setq children
8524                            (gnus-article-children (gnus-data-number d)))
8525                      (let (found)
8526                        (while children
8527                          (when (memq (car children) articles)
8528                            (setq children nil
8529                                  found t))
8530                          (pop children))
8531                        found)))
8532         (push (gnus-data-number d) articles)))
8533     ;; Do the limiting.
8534     (prog1
8535         (gnus-summary-limit articles)
8536       (gnus-summary-position-point))))
8537
8538 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8539   "Mark all unread excluded articles as read.
8540 If ALL, mark even excluded ticked and dormants as read."
8541   (interactive "P")
8542   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8543   (let ((articles (gnus-sorted-ndifference
8544                    (sort
8545                     (mapcar (lambda (h) (mail-header-number h))
8546                             gnus-newsgroup-headers)
8547                     '<)
8548                    gnus-newsgroup-limit))
8549         article)
8550     (setq gnus-newsgroup-unreads
8551           (gnus-sorted-intersection gnus-newsgroup-unreads
8552                                     gnus-newsgroup-limit))
8553     (if all
8554         (setq gnus-newsgroup-dormant nil
8555               gnus-newsgroup-marked nil
8556               gnus-newsgroup-reads
8557               (nconc
8558                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8559                gnus-newsgroup-reads))
8560       (while (setq article (pop articles))
8561         (unless (or (memq article gnus-newsgroup-dormant)
8562                     (memq article gnus-newsgroup-marked))
8563           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8564
8565 (defun gnus-summary-limit (articles &optional pop)
8566   (if pop
8567       ;; We pop the previous limit off the stack and use that.
8568       (setq articles (car gnus-newsgroup-limits)
8569             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8570     ;; We use the new limit, so we push the old limit on the stack.
8571     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8572   ;; Set the limit.
8573   (setq gnus-newsgroup-limit articles)
8574   (let ((total (length gnus-newsgroup-data))
8575         (data (gnus-data-find-list (gnus-summary-article-number)))
8576         (gnus-summary-mark-below nil)   ; Inhibit this.
8577         found)
8578     ;; This will do all the work of generating the new summary buffer
8579     ;; according to the new limit.
8580     (gnus-summary-prepare)
8581     ;; Hide any threads, possibly.
8582     (gnus-summary-maybe-hide-threads)
8583     ;; Try to return to the article you were at, or one in the
8584     ;; neighborhood.
8585     (when data
8586       ;; We try to find some article after the current one.
8587       (while data
8588         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8589           (setq data nil
8590                 found t))
8591         (setq data (cdr data))))
8592     (unless found
8593       ;; If there is no data, that means that we were after the last
8594       ;; article.  The same goes when we can't find any articles
8595       ;; after the current one.
8596       (goto-char (point-max))
8597       (gnus-summary-find-prev))
8598     (gnus-set-mode-line 'summary)
8599     ;; We return how many articles were removed from the summary
8600     ;; buffer as a result of the new limit.
8601     (- total (length gnus-newsgroup-data))))
8602
8603 (defsubst gnus-invisible-cut-children (threads)
8604   (let ((num 0))
8605     (while threads
8606       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8607         (incf num))
8608       (pop threads))
8609     (< num 2)))
8610
8611 (defsubst gnus-cut-thread (thread)
8612   "Go forwards in the thread until we find an article that we want to display."
8613   (when (or (eq gnus-fetch-old-headers 'some)
8614             (eq gnus-fetch-old-headers 'invisible)
8615             (numberp gnus-fetch-old-headers)
8616             (eq gnus-build-sparse-threads 'some)
8617             (eq gnus-build-sparse-threads 'more))
8618     ;; Deal with old-fetched headers and sparse threads.
8619     (while (and
8620             thread
8621             (or
8622              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8623              (gnus-summary-article-ancient-p
8624               (mail-header-number (car thread))))
8625             (if (or (<= (length (cdr thread)) 1)
8626                     (eq gnus-fetch-old-headers 'invisible))
8627                 (setq gnus-newsgroup-limit
8628                       (delq (mail-header-number (car thread))
8629                             gnus-newsgroup-limit)
8630                       thread (cadr thread))
8631               (when (gnus-invisible-cut-children (cdr thread))
8632                 (let ((th (cdr thread)))
8633                   (while th
8634                     (if (memq (mail-header-number (caar th))
8635                               gnus-newsgroup-limit)
8636                         (setq thread (car th)
8637                               th nil)
8638                       (setq th (cdr th))))))))))
8639   thread)
8640
8641 (defun gnus-cut-threads (threads)
8642   "Cut off all uninteresting articles from the beginning of THREADS."
8643   (when (or (eq gnus-fetch-old-headers 'some)
8644             (eq gnus-fetch-old-headers 'invisible)
8645             (numberp gnus-fetch-old-headers)
8646             (eq gnus-build-sparse-threads 'some)
8647             (eq gnus-build-sparse-threads 'more))
8648     (let ((th threads))
8649       (while th
8650         (setcar th (gnus-cut-thread (car th)))
8651         (setq th (cdr th)))))
8652   ;; Remove nixed out threads.
8653   (delq nil threads))
8654
8655 (defun gnus-summary-initial-limit (&optional show-if-empty)
8656   "Figure out what the initial limit is supposed to be on group entry.
8657 This entails weeding out unwanted dormants, low-scored articles,
8658 fetch-old-headers verbiage, and so on."
8659   ;; Most groups have nothing to remove.
8660   (unless (or gnus-inhibit-limiting
8661               (and (null gnus-newsgroup-dormant)
8662                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8663                    (not (eq gnus-fetch-old-headers 'some))
8664                    (not (numberp gnus-fetch-old-headers))
8665                    (not (eq gnus-fetch-old-headers 'invisible))
8666                    (null gnus-summary-expunge-below)
8667                    (not (eq gnus-build-sparse-threads 'some))
8668                    (not (eq gnus-build-sparse-threads 'more))
8669                    (null gnus-thread-expunge-below)
8670                    (not gnus-use-nocem)))
8671     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8672     (setq gnus-newsgroup-limit nil)
8673     (mapatoms
8674      (lambda (node)
8675        (unless (car (symbol-value node))
8676          ;; These threads have no parents -- they are roots.
8677          (let ((nodes (cdr (symbol-value node)))
8678                thread)
8679            (while nodes
8680              (if (and gnus-thread-expunge-below
8681                       (< (gnus-thread-total-score (car nodes))
8682                          gnus-thread-expunge-below))
8683                  (gnus-expunge-thread (pop nodes))
8684                (setq thread (pop nodes))
8685                (gnus-summary-limit-children thread))))))
8686      gnus-newsgroup-dependencies)
8687     ;; If this limitation resulted in an empty group, we might
8688     ;; pop the previous limit and use it instead.
8689     (when (and (not gnus-newsgroup-limit)
8690                show-if-empty)
8691       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8692     gnus-newsgroup-limit))
8693
8694 (defun gnus-summary-limit-children (thread)
8695   "Return 1 if this subthread is visible and 0 if it is not."
8696   ;; First we get the number of visible children to this thread.  This
8697   ;; is done by recursing down the thread using this function, so this
8698   ;; will really go down to a leaf article first, before slowly
8699   ;; working its way up towards the root.
8700   (when thread
8701     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8702            (children
8703            (if (cdr thread)
8704                (apply '+ (mapcar 'gnus-summary-limit-children
8705                                  (cdr thread)))
8706              0))
8707           (number (mail-header-number (car thread)))
8708           score)
8709       (if (and
8710            (not (memq number gnus-newsgroup-marked))
8711            (or
8712             ;; If this article is dormant and has absolutely no visible
8713             ;; children, then this article isn't visible.
8714             (and (memq number gnus-newsgroup-dormant)
8715                  (zerop children))
8716             ;; If this is "fetch-old-headered" and there is no
8717             ;; visible children, then we don't want this article.
8718             (and (or (eq gnus-fetch-old-headers 'some)
8719                      (numberp gnus-fetch-old-headers))
8720                  (gnus-summary-article-ancient-p number)
8721                  (zerop children))
8722             ;; If this is "fetch-old-headered" and `invisible', then
8723             ;; we don't want this article.
8724             (and (eq gnus-fetch-old-headers 'invisible)
8725                  (gnus-summary-article-ancient-p number))
8726             ;; If this is a sparsely inserted article with no children,
8727             ;; we don't want it.
8728             (and (eq gnus-build-sparse-threads 'some)
8729                  (gnus-summary-article-sparse-p number)
8730                  (zerop children))
8731             ;; If we use expunging, and this article is really
8732             ;; low-scored, then we don't want this article.
8733             (when (and gnus-summary-expunge-below
8734                        (< (setq score
8735                                 (or (cdr (assq number gnus-newsgroup-scored))
8736                                     gnus-summary-default-score))
8737                           gnus-summary-expunge-below))
8738               ;; We increase the expunge-tally here, but that has
8739               ;; nothing to do with the limits, really.
8740               (incf gnus-newsgroup-expunged-tally)
8741               ;; We also mark as read here, if that's wanted.
8742               (when (and gnus-summary-mark-below
8743                          (< score gnus-summary-mark-below))
8744                 (setq gnus-newsgroup-unreads
8745                       (delq number gnus-newsgroup-unreads))
8746                 (if gnus-newsgroup-auto-expire
8747                     (push number gnus-newsgroup-expirable)
8748                   (push (cons number gnus-low-score-mark)
8749                         gnus-newsgroup-reads)))
8750               t)
8751             ;; Do the `display' group parameter.
8752             (and gnus-newsgroup-display
8753                  (not (funcall gnus-newsgroup-display)))
8754             ;; Check NoCeM things.
8755             (when (and gnus-use-nocem
8756                        (gnus-nocem-unwanted-article-p
8757                         (mail-header-id (car thread))))
8758               (setq gnus-newsgroup-unreads
8759                     (delq number gnus-newsgroup-unreads))
8760               t)))
8761           ;; Nope, invisible article.
8762           0
8763         ;; Ok, this article is to be visible, so we add it to the limit
8764         ;; and return 1.
8765         (push number gnus-newsgroup-limit)
8766         1))))
8767
8768 (defun gnus-expunge-thread (thread)
8769   "Mark all articles in THREAD as read."
8770   (let* ((number (mail-header-number (car thread))))
8771     (incf gnus-newsgroup-expunged-tally)
8772     ;; We also mark as read here, if that's wanted.
8773     (setq gnus-newsgroup-unreads
8774           (delq number gnus-newsgroup-unreads))
8775     (if gnus-newsgroup-auto-expire
8776         (push number gnus-newsgroup-expirable)
8777       (push (cons number gnus-low-score-mark)
8778             gnus-newsgroup-reads)))
8779   ;; Go recursively through all subthreads.
8780   (mapcar 'gnus-expunge-thread (cdr thread)))
8781
8782 ;; Summary article oriented commands
8783
8784 (defun gnus-summary-refer-parent-article (n)
8785   "Refer parent article N times.
8786 If N is negative, go to ancestor -N instead.
8787 The difference between N and the number of articles fetched is returned."
8788   (interactive "p")
8789   (let ((skip 1)
8790         error header ref)
8791     (when (not (natnump n))
8792       (setq skip (abs n)
8793             n 1))
8794     (while (and (> n 0)
8795                 (not error))
8796       (setq header (gnus-summary-article-header))
8797       (if (and (eq (mail-header-number header)
8798                    (cdr gnus-article-current))
8799                (equal gnus-newsgroup-name
8800                       (car gnus-article-current)))
8801           ;; If we try to find the parent of the currently
8802           ;; displayed article, then we take a look at the actual
8803           ;; References header, since this is slightly more
8804           ;; reliable than the References field we got from the
8805           ;; server.
8806           (with-current-buffer gnus-original-article-buffer
8807             (nnheader-narrow-to-headers)
8808             (unless (setq ref (message-fetch-field "references"))
8809               (when (setq ref (message-fetch-field "in-reply-to"))
8810                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8811             (widen))
8812         (setq ref
8813               ;; It's not the current article, so we take a bet on
8814               ;; the value we got from the server.
8815               (mail-header-references header)))
8816       (if (and ref
8817                (not (equal ref "")))
8818           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8819             (gnus-message 1 "Couldn't find parent"))
8820         (gnus-message 1 "No references in article %d"
8821                       (gnus-summary-article-number))
8822         (setq error t))
8823       (decf n))
8824     (gnus-summary-position-point)
8825     n))
8826
8827 (defun gnus-summary-refer-references ()
8828   "Fetch all articles mentioned in the References header.
8829 Return the number of articles fetched."
8830   (interactive)
8831   (let ((ref (mail-header-references (gnus-summary-article-header)))
8832         (current (gnus-summary-article-number))
8833         (n 0))
8834     (if (or (not ref)
8835             (equal ref ""))
8836         (error "No References in the current article")
8837       ;; For each Message-ID in the References header...
8838       (while (string-match "<[^>]*>" ref)
8839         (incf n)
8840         ;; ... fetch that article.
8841         (gnus-summary-refer-article
8842          (prog1 (match-string 0 ref)
8843            (setq ref (substring ref (match-end 0))))))
8844       (gnus-summary-goto-subject current)
8845       (gnus-summary-position-point)
8846       n)))
8847
8848 (defun gnus-summary-refer-thread (&optional limit)
8849   "Fetch all articles in the current thread.
8850 If LIMIT (the numerical prefix), fetch that many old headers instead
8851 of what's specified by the `gnus-refer-thread-limit' variable."
8852   (interactive "P")
8853   (let ((id (mail-header-id (gnus-summary-article-header)))
8854         (limit (if limit (prefix-numeric-value limit)
8855                  gnus-refer-thread-limit)))
8856     (unless (eq gnus-fetch-old-headers 'invisible)
8857       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8858       ;; Retrieve the headers and read them in.
8859       (if (eq (if (numberp limit)
8860                   (gnus-retrieve-headers
8861                    (list (min
8862                           (+ (mail-header-number
8863                               (gnus-summary-article-header))
8864                              limit)
8865                           gnus-newsgroup-end))
8866                    gnus-newsgroup-name (* limit 2))
8867                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8868                 ;; headers.
8869                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8870                                        gnus-newsgroup-name limit))
8871               'nov)
8872           (gnus-build-all-threads)
8873         (error "Can't fetch thread from back ends that don't support NOV"))
8874       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8875     (gnus-summary-limit-include-thread id)))
8876
8877 (defun gnus-summary-refer-article (message-id)
8878   "Fetch an article specified by MESSAGE-ID."
8879   (interactive "sMessage-ID: ")
8880   (when (and (stringp message-id)
8881              (not (zerop (length message-id))))
8882     (setq message-id (gnus-replace-in-string message-id " " ""))
8883     ;; Construct the correct Message-ID if necessary.
8884     ;; Suggested by tale@pawl.rpi.edu.
8885     (unless (string-match "^<" message-id)
8886       (setq message-id (concat "<" message-id)))
8887     (unless (string-match ">$" message-id)
8888       (setq message-id (concat message-id ">")))
8889     ;; People often post MIDs from URLs, so unhex it:
8890     (unless (string-match "@" message-id)
8891       (setq message-id (gnus-url-unhex-string message-id)))
8892     (let* ((header (gnus-id-to-header message-id))
8893            (sparse (and header
8894                         (gnus-summary-article-sparse-p
8895                          (mail-header-number header))
8896                         (memq (mail-header-number header)
8897                               gnus-newsgroup-limit)))
8898            number)
8899       (cond
8900        ;; If the article is present in the buffer we just go to it.
8901        ((and header
8902              (or (not (gnus-summary-article-sparse-p
8903                        (mail-header-number header)))
8904                  sparse))
8905         (prog1
8906             (gnus-summary-goto-article
8907              (mail-header-number header) nil t)
8908           (when sparse
8909             (gnus-summary-update-article (mail-header-number header)))))
8910        (t
8911         ;; We fetch the article.
8912         (catch 'found
8913           (dolist (gnus-override-method (gnus-refer-article-methods))
8914             (when (and (gnus-check-server gnus-override-method)
8915                        ;; Fetch the header,
8916                        (setq number (gnus-summary-insert-subject message-id)))
8917               ;; and display the article.
8918               (gnus-summary-select-article nil nil nil number)
8919               (throw 'found t)))
8920           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8921
8922 (defun gnus-refer-article-methods ()
8923   "Return a list of referable methods."
8924   (cond
8925    ;; No method, so we default to current and native.
8926    ((null gnus-refer-article-method)
8927     (list gnus-current-select-method gnus-select-method))
8928    ;; Current.
8929    ((eq 'current gnus-refer-article-method)
8930     (list gnus-current-select-method))
8931    ;; List of select methods.
8932    ((not (and (symbolp (car gnus-refer-article-method))
8933               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8934     (let (out)
8935       (dolist (method gnus-refer-article-method)
8936         (push (if (eq 'current method)
8937                   gnus-current-select-method
8938                 method)
8939               out))
8940       (nreverse out)))
8941    ;; One single select method.
8942    (t
8943     (list gnus-refer-article-method))))
8944
8945 (defun gnus-summary-edit-parameters ()
8946   "Edit the group parameters of the current group."
8947   (interactive)
8948   (gnus-group-edit-group gnus-newsgroup-name 'params))
8949
8950 (defun gnus-summary-customize-parameters ()
8951   "Customize the group parameters of the current group."
8952   (interactive)
8953   (gnus-group-customize gnus-newsgroup-name))
8954
8955 (defun gnus-summary-enter-digest-group (&optional force)
8956   "Enter an nndoc group based on the current article.
8957 If FORCE, force a digest interpretation.  If not, try
8958 to guess what the document format is."
8959   (interactive "P")
8960   (let ((conf gnus-current-window-configuration))
8961     (save-window-excursion
8962       (save-excursion
8963         (let (gnus-article-prepare-hook
8964               gnus-display-mime-function
8965               gnus-break-pages)
8966           (gnus-summary-select-article))))
8967     (setq gnus-current-window-configuration conf)
8968     (let* ((name (format "%s-%d"
8969                          (gnus-group-prefixed-name
8970                           gnus-newsgroup-name (list 'nndoc ""))
8971                          (with-current-buffer gnus-summary-buffer
8972                            gnus-current-article)))
8973            (ogroup gnus-newsgroup-name)
8974            (params (append (gnus-info-params (gnus-get-info ogroup))
8975                            (list (cons 'to-group ogroup))
8976                            (list (cons 'parent-group ogroup))
8977                            (list (cons 'save-article-group ogroup))))
8978            (case-fold-search t)
8979            (buf (current-buffer))
8980            dig to-address)
8981       (with-current-buffer gnus-original-article-buffer
8982         ;; Have the digest group inherit the main mail address of
8983         ;; the parent article.
8984         (when (setq to-address (or (gnus-fetch-field "reply-to")
8985                                    (gnus-fetch-field "from")))
8986           (setq params
8987                 (append
8988                  (list (cons 'to-address
8989                              (funcall gnus-decode-encoded-address-function
8990                                       to-address))))))
8991         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8992         (insert-buffer-substring gnus-original-article-buffer)
8993         ;; Remove lines that may lead nndoc to misinterpret the
8994         ;; document type.
8995         (narrow-to-region
8996          (goto-char (point-min))
8997          (or (search-forward "\n\n" nil t) (point)))
8998         (goto-char (point-min))
8999         (delete-matching-lines "^Path:\\|^From ")
9000         (widen))
9001       (unwind-protect
9002           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
9003                     (gnus-newsgroup-ephemeral-ignored-charsets
9004                      gnus-newsgroup-ignored-charsets))
9005                 (gnus-group-read-ephemeral-group
9006                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9007                               (nndoc-article-type
9008                                ,(if force 'mbox 'guess)))
9009                  t nil nil nil
9010                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9011                                                         "ADAPT")))))
9012               ;; Make all postings to this group go to the parent group.
9013               (nconc (gnus-info-params (gnus-get-info name))
9014                      params)
9015             ;; Couldn't select this doc group.
9016             (switch-to-buffer buf)
9017             (gnus-set-global-variables)
9018             (gnus-configure-windows 'summary)
9019             (gnus-message 3 "Article couldn't be entered?"))
9020         (kill-buffer dig)))))
9021
9022 (defun gnus-summary-read-document (n)
9023   "Open a new group based on the current article(s).
9024 This will allow you to read digests and other similar
9025 documents as newsgroups.
9026 Obeys the standard process/prefix convention."
9027   (interactive "P")
9028   (let* ((ogroup gnus-newsgroup-name)
9029          (params (append (gnus-info-params (gnus-get-info ogroup))
9030                          (list (cons 'to-group ogroup))))
9031          group egroup groups vgroup)
9032     (dolist (article (gnus-summary-work-articles n))
9033       (setq group (format "%s-%d" gnus-newsgroup-name article))
9034       (gnus-summary-remove-process-mark article)
9035       (when (gnus-summary-display-article article)
9036         (save-excursion ;;What for?
9037           (with-temp-buffer
9038             (insert-buffer-substring gnus-original-article-buffer)
9039             ;; Remove some headers that may lead nndoc to make
9040             ;; the wrong guess.
9041             (message-narrow-to-head)
9042             (goto-char (point-min))
9043             (delete-matching-lines "^Path:\\|^From ")
9044             (widen)
9045             (if (setq egroup
9046                       (gnus-group-read-ephemeral-group
9047                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9048                                      (nndoc-article-type guess))
9049                        t nil t))
9050                 (progn
9051                   ;; Make all postings to this group go to the parent group.
9052                   (nconc (gnus-info-params (gnus-get-info egroup))
9053                          params)
9054                   (push egroup groups))
9055               ;; Couldn't select this doc group.
9056               (gnus-error 3 "Article couldn't be entered"))))))
9057     ;; Now we have selected all the documents.
9058     (cond
9059      ((not groups)
9060       (error "None of the articles could be interpreted as documents"))
9061      ((gnus-group-read-ephemeral-group
9062        (setq vgroup (format
9063                      "nnvirtual:%s-%s" gnus-newsgroup-name
9064                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9065        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9066        t
9067        (cons (current-buffer) 'summary)))
9068      (t
9069       (error "Couldn't select virtual nndoc group")))))
9070
9071 (defun gnus-summary-isearch-article (&optional regexp-p)
9072   "Do incremental search forward on the current article.
9073 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9074   (interactive "P")
9075   (gnus-summary-select-article)
9076   (gnus-configure-windows 'article)
9077   (gnus-eval-in-buffer-window gnus-article-buffer
9078     (save-restriction
9079       (widen)
9080       (isearch-forward regexp-p))))
9081
9082 (defun gnus-summary-repeat-search-article-forward ()
9083   "Repeat the previous search forwards."
9084   (interactive)
9085   (unless gnus-last-search-regexp
9086     (error "No previous search"))
9087   (gnus-summary-search-article-forward gnus-last-search-regexp))
9088
9089 (defun gnus-summary-repeat-search-article-backward ()
9090   "Repeat the previous search backwards."
9091   (interactive)
9092   (unless gnus-last-search-regexp
9093     (error "No previous search"))
9094   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9095
9096 (defun gnus-summary-search-article-forward (regexp &optional backward)
9097   "Search for an article containing REGEXP forward.
9098 If BACKWARD, search backward instead."
9099   (interactive
9100    (list (read-string
9101           (format "Search article %s (regexp%s): "
9102                   (if current-prefix-arg "backward" "forward")
9103                   (if gnus-last-search-regexp
9104                       (concat ", default " gnus-last-search-regexp)
9105                     "")))
9106          current-prefix-arg))
9107   (if (string-equal regexp "")
9108       (setq regexp (or gnus-last-search-regexp ""))
9109     (setq gnus-last-search-regexp regexp)
9110     (setq gnus-article-before-search gnus-current-article))
9111   ;; Intentionally set gnus-last-article.
9112   (setq gnus-last-article gnus-article-before-search)
9113   (let ((gnus-last-article gnus-last-article))
9114     (if (gnus-summary-search-article regexp backward)
9115         (gnus-summary-show-thread)
9116       (signal 'search-failed (list regexp)))))
9117
9118 (defun gnus-summary-search-article-backward (regexp)
9119   "Search for an article containing REGEXP backward."
9120   (interactive
9121    (list (read-string
9122           (format "Search article backward (regexp%s): "
9123                   (if gnus-last-search-regexp
9124                       (concat ", default " gnus-last-search-regexp)
9125                     "")))))
9126   (gnus-summary-search-article-forward regexp 'backward))
9127
9128 (defun gnus-summary-search-article (regexp &optional backward)
9129   "Search for an article containing REGEXP.
9130 Optional argument BACKWARD means do search for backward.
9131 `gnus-select-article-hook' is not called during the search."
9132   ;; We have to require this here to make sure that the following
9133   ;; dynamic binding isn't shadowed by autoloading.
9134   (require 'gnus-async)
9135   (require 'gnus-art)
9136   (let ((gnus-select-article-hook nil)  ;Disable hook.
9137         (gnus-article-prepare-hook nil)
9138         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9139         (gnus-use-article-prefetch nil)
9140         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9141         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9142         (gnus-visual nil)
9143         (gnus-keep-backlog nil)
9144         (gnus-break-pages nil)
9145         (gnus-summary-display-arrow nil)
9146         (gnus-updated-mode-lines nil)
9147         (gnus-auto-center-summary nil)
9148         (sum (current-buffer))
9149         (gnus-display-mime-function nil)
9150         (found nil)
9151         point)
9152     (gnus-save-hidden-threads
9153       (gnus-summary-select-article)
9154       (set-buffer gnus-article-buffer)
9155       (goto-char (window-point (get-buffer-window (current-buffer))))
9156       (when backward
9157         (forward-line -1))
9158       (while (not found)
9159         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9160         (if (if backward
9161                 (re-search-backward regexp nil t)
9162               (re-search-forward regexp nil t))
9163             ;; We found the regexp.
9164             (progn
9165               (setq found 'found)
9166               (beginning-of-line)
9167               (set-window-start
9168                (get-buffer-window (current-buffer))
9169                (point))
9170               (forward-line 1)
9171               (set-window-point
9172                (get-buffer-window (current-buffer))
9173                (point))
9174               (set-buffer sum)
9175               (setq point (point)))
9176           ;; We didn't find it, so we go to the next article.
9177           (set-buffer sum)
9178           (setq found 'not)
9179           (while (eq found 'not)
9180             (if (not (if backward (gnus-summary-find-prev)
9181                        (gnus-summary-find-next)))
9182                 ;; No more articles.
9183                 (setq found t)
9184               ;; Select the next article and adjust point.
9185               (unless (gnus-summary-article-sparse-p
9186                        (gnus-summary-article-number))
9187                 (setq found nil)
9188                 (gnus-summary-select-article)
9189                 (set-buffer gnus-article-buffer)
9190                 (widen)
9191                 (goto-char (if backward (point-max) (point-min))))))))
9192       (gnus-message 7 ""))
9193     ;; Return whether we found the regexp.
9194     (when (eq found 'found)
9195       (goto-char point)
9196       (gnus-summary-show-thread)
9197       (gnus-summary-goto-subject gnus-current-article)
9198       (gnus-summary-position-point)
9199       t)))
9200
9201 (defun gnus-find-matching-articles (header regexp)
9202   "Return a list of all articles that match REGEXP on HEADER.
9203 This search includes all articles in the current group that Gnus has
9204 fetched headers for, whether they are displayed or not."
9205   (let ((articles nil)
9206         ;; Can't eta-reduce because it's a macro.
9207         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9208         (case-fold-search t))
9209     (dolist (header gnus-newsgroup-headers)
9210       (when (string-match regexp (funcall func header))
9211         (push (mail-header-number header) articles)))
9212     (nreverse articles)))
9213
9214 (defun gnus-summary-find-matching (header regexp &optional backward unread
9215                                           not-case-fold not-matching)
9216   "Return a list of all articles that match REGEXP on HEADER.
9217 The search stars on the current article and goes forwards unless
9218 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9219 If UNREAD is non-nil, only unread articles will
9220 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9221 in the comparisons. If NOT-MATCHING, return a list of all articles that
9222 not match REGEXP on HEADER."
9223   (let ((case-fold-search (not not-case-fold))
9224         articles d func)
9225     (if (consp header)
9226         (if (eq (car header) 'extra)
9227             (setq func
9228                   `(lambda (h)
9229                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9230                          "")))
9231           (error "%s is an invalid header" header))
9232       (unless (fboundp (intern (concat "mail-header-" header)))
9233         (error "%s is not a valid header" header))
9234       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9235     (dolist (d (if (eq backward 'all)
9236                    gnus-newsgroup-data
9237                  (gnus-data-find-list
9238                   (gnus-summary-article-number)
9239                   (gnus-data-list backward))))
9240       (when (and (or (not unread)       ; We want all articles...
9241                      (gnus-data-unread-p d)) ; Or just unreads.
9242                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9243                  (if not-matching
9244                      (not (string-match
9245                            regexp
9246                            (funcall func (gnus-data-header d))))
9247                    (string-match regexp
9248                                  (funcall func (gnus-data-header d)))))
9249         (push (gnus-data-number d) articles))) ; Success!
9250     (nreverse articles)))
9251
9252 (defun gnus-summary-execute-command (header regexp command &optional backward)
9253   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9254 If HEADER is an empty string (or nil), the match is done on the entire
9255 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9256   (interactive
9257    (list (let ((completion-ignore-case t))
9258            (completing-read
9259             "Header name: "
9260             (mapcar (lambda (header) (list (format "%s" header)))
9261                     (append
9262                      '("Number" "Subject" "From" "Lines" "Date"
9263                        "Message-ID" "Xref" "References" "Body")
9264                      gnus-extra-headers))
9265             nil 'require-match))
9266          (read-string "Regexp: ")
9267          (read-key-sequence "Command: ")
9268          current-prefix-arg))
9269   (when (equal header "Body")
9270     (setq header ""))
9271   ;; Hidden thread subtrees must be searched as well.
9272   (gnus-summary-show-all-threads)
9273   ;; We don't want to change current point nor window configuration.
9274   (save-excursion
9275     (save-window-excursion
9276       (let (gnus-visual
9277             gnus-treat-strip-trailing-blank-lines
9278             gnus-treat-strip-leading-blank-lines
9279             gnus-treat-strip-multiple-blank-lines
9280             gnus-treat-hide-boring-headers
9281             gnus-treat-fold-newsgroups
9282             gnus-article-prepare-hook)
9283         (gnus-message 6 "Executing %s..." (key-description command))
9284         ;; We'd like to execute COMMAND interactively so as to give arguments.
9285         (gnus-execute header regexp
9286                       `(call-interactively ',(key-binding command))
9287                       backward)
9288         (gnus-message 6 "Executing %s...done" (key-description command))))))
9289
9290 (defun gnus-summary-beginning-of-article ()
9291   "Scroll the article back to the beginning."
9292   (interactive)
9293   (gnus-summary-select-article)
9294   (gnus-configure-windows 'article)
9295   (gnus-eval-in-buffer-window gnus-article-buffer
9296     (widen)
9297     (goto-char (point-min))
9298     (when gnus-break-pages
9299       (gnus-narrow-to-page))))
9300
9301 (defun gnus-summary-end-of-article ()
9302   "Scroll to the end of the article."
9303   (interactive)
9304   (gnus-summary-select-article)
9305   (gnus-configure-windows 'article)
9306   (gnus-eval-in-buffer-window gnus-article-buffer
9307     (widen)
9308     (goto-char (point-max))
9309     (recenter -3)
9310     (when gnus-break-pages
9311       (gnus-narrow-to-page))))
9312
9313 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9314   "Truncate to LEN and quote all \"(\"'s in STRING."
9315   (gnus-replace-in-string (if (and len (> (length string) len))
9316                               (substring string 0 len)
9317                             string)
9318                           "[()]" "\\\\\\&"))
9319
9320 (defun gnus-summary-print-article (&optional filename n)
9321   "Generate and print a PostScript image of the process-marked (mail) articles.
9322
9323 If used interactively, print the current article if none are
9324 process-marked.  With prefix arg, prompt the user for the name of the
9325 file to save in.
9326
9327 When used from Lisp, accept two optional args FILENAME and N.  N means
9328 to print the next N articles.  If N is negative, print the N previous
9329 articles.  If N is nil and articles have been marked with the process
9330 mark, print these instead.
9331
9332 If the optional first argument FILENAME is nil, send the image to the
9333 printer.  If FILENAME is a string, save the PostScript image in a file with
9334 that name.  If FILENAME is a number, prompt the user for the name of the file
9335 to save in."
9336   (interactive (list (ps-print-preprint current-prefix-arg)))
9337   (dolist (article (gnus-summary-work-articles n))
9338     (gnus-summary-select-article nil nil 'pseudo article)
9339     (gnus-eval-in-buffer-window gnus-article-buffer
9340       (gnus-print-buffer))
9341     (gnus-summary-remove-process-mark article))
9342   (ps-despool filename))
9343
9344 (defun gnus-print-buffer ()
9345   (let ((buffer (generate-new-buffer " *print*")))
9346     (unwind-protect
9347         (progn
9348           (copy-to-buffer buffer (point-min) (point-max))
9349           (set-buffer buffer)
9350           (gnus-remove-text-with-property 'gnus-decoration)
9351           (when (gnus-visual-p 'article-highlight 'highlight)
9352             ;; Copy-to-buffer doesn't copy overlay.  So redo
9353             ;; highlight.
9354             (let ((gnus-article-buffer buffer))
9355               (gnus-article-highlight-citation t)
9356               (gnus-article-highlight-signature)
9357               (gnus-article-emphasize)
9358               (gnus-article-delete-invisible-text)))
9359           (let ((ps-left-header
9360                  (list
9361                   (concat "("
9362                           (gnus-summary-print-truncate-and-quote
9363                            (mail-header-subject gnus-current-headers)
9364                            66) ")")
9365                   (concat "("
9366                           (gnus-summary-print-truncate-and-quote
9367                            (mail-header-from gnus-current-headers)
9368                            45) ")")))
9369                 (ps-right-header
9370                  (list
9371                   "/pagenumberstring load"
9372                   (concat "("
9373                           (mail-header-date gnus-current-headers) ")"))))
9374             (gnus-run-hooks 'gnus-ps-print-hook)
9375             (save-excursion
9376               (if ps-print-color-p
9377                   (ps-spool-buffer-with-faces)
9378                 (ps-spool-buffer)))))
9379       (kill-buffer buffer))))
9380
9381 (defun gnus-summary-show-article (&optional arg)
9382   "Force redisplaying of the current article.
9383 If ARG (the prefix) is a number, show the article with the charset
9384 defined in `gnus-summary-show-article-charset-alist', or the charset
9385 input.
9386 If ARG (the prefix) is non-nil and not a number, show the raw article
9387 without any article massaging functions being run.  Normally, the key
9388 strokes are `C-u g'."
9389   (interactive "P")
9390   (cond
9391    ((numberp arg)
9392     (gnus-summary-show-article t)
9393     (let ((gnus-newsgroup-charset
9394            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9395                (mm-read-coding-system
9396                 "View as charset: " ;; actually it is coding system.
9397                 (with-current-buffer gnus-article-buffer
9398                   (mm-detect-coding-region (point) (point-max))))))
9399           (gnus-newsgroup-ignored-charsets 'gnus-all))
9400       (gnus-summary-select-article nil 'force)
9401       (let ((deps gnus-newsgroup-dependencies)
9402             head header lines)
9403         (with-current-buffer gnus-original-article-buffer
9404           (save-restriction
9405             (message-narrow-to-head)
9406             (setq head (buffer-string))
9407             (goto-char (point-min))
9408             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9409               (goto-char (point-max))
9410               (widen)
9411               (setq lines (1- (count-lines (point) (point-max))))))
9412           (with-temp-buffer
9413             (insert (format "211 %d Article retrieved.\n"
9414                             (cdr gnus-article-current)))
9415             (insert head)
9416             (if lines (insert (format "Lines: %d\n" lines)))
9417             (insert ".\n")
9418             (let ((nntp-server-buffer (current-buffer)))
9419               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9420         (gnus-data-set-header
9421          (gnus-data-find (cdr gnus-article-current))
9422          header)
9423         (gnus-summary-update-article-line
9424          (cdr gnus-article-current) header)
9425         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9426           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9427    ((not arg)
9428     ;; Select the article the normal way.
9429     (gnus-summary-select-article nil 'force))
9430    (t
9431     ;; We have to require this here to make sure that the following
9432     ;; dynamic binding isn't shadowed by autoloading.
9433     (require 'gnus-async)
9434     (require 'gnus-art)
9435     ;; Bind the article treatment functions to nil.
9436     (let ((gnus-have-all-headers t)
9437           gnus-article-prepare-hook
9438           gnus-article-decode-hook
9439           gnus-display-mime-function
9440           gnus-break-pages)
9441       ;; Destroy any MIME parts.
9442       (when (gnus-buffer-live-p gnus-article-buffer)
9443         (with-current-buffer gnus-article-buffer
9444           (mm-destroy-parts gnus-article-mime-handles)
9445           ;; Set it to nil for safety reason.
9446           (setq gnus-article-mime-handle-alist nil)
9447           (setq gnus-article-mime-handles nil)))
9448       (gnus-summary-select-article nil 'force))))
9449   (gnus-summary-goto-subject gnus-current-article)
9450   (gnus-summary-position-point))
9451
9452 (defun gnus-summary-show-raw-article ()
9453   "Show the raw article without any article massaging functions being run."
9454   (interactive)
9455   (gnus-summary-show-article t))
9456
9457 (defun gnus-summary-verbose-headers (&optional arg)
9458   "Toggle permanent full header display.
9459 If ARG is a positive number, turn header display on.
9460 If ARG is a negative number, turn header display off."
9461   (interactive "P")
9462   (setq gnus-show-all-headers
9463         (cond ((or (not (numberp arg))
9464                    (zerop arg))
9465                (not gnus-show-all-headers))
9466               ((natnump arg)
9467                t)))
9468   (gnus-summary-show-article))
9469
9470 (defun gnus-summary-toggle-header (&optional arg)
9471   "Show the headers if they are hidden, or hide them if they are shown.
9472 If ARG is a positive number, show the entire header.
9473 If ARG is a negative number, hide the unwanted header lines."
9474   (interactive "P")
9475   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9476                      (get-buffer-window gnus-article-buffer t))))
9477     (with-current-buffer gnus-article-buffer
9478       (widen)
9479       (article-narrow-to-head)
9480       (let* ((inhibit-read-only t)
9481              (inhibit-point-motion-hooks t)
9482              (hidden (if (numberp arg)
9483                          (>= arg 0)
9484                        (or (not (looking-at "[^ \t\n]+:"))
9485                            (gnus-article-hidden-text-p 'headers))))
9486              s e)
9487         (delete-region (point-min) (point-max))
9488         (with-current-buffer gnus-original-article-buffer
9489           (goto-char (setq s (point-min)))
9490           (setq e (if (search-forward "\n\n" nil t)
9491                       (1- (point))
9492                     (point-max))))
9493         (insert-buffer-substring gnus-original-article-buffer s e)
9494         (run-hooks 'gnus-article-decode-hook)
9495         (if hidden
9496             (let ((gnus-treat-hide-headers nil)
9497                   (gnus-treat-hide-boring-headers nil))
9498               (gnus-delete-wash-type 'headers)
9499               (gnus-treat-article 'head))
9500           (gnus-treat-article 'head))
9501         (widen)
9502         (if window
9503             (set-window-start window (goto-char (point-min))))
9504         (if gnus-break-pages
9505             (gnus-narrow-to-page)
9506           (when (gnus-visual-p 'page-marker)
9507             (let ((inhibit-read-only t))
9508               (gnus-remove-text-with-property 'gnus-prev)
9509               (gnus-remove-text-with-property 'gnus-next))))
9510         (gnus-set-mode-line 'article)))))
9511
9512 (defun gnus-summary-show-all-headers ()
9513   "Make all header lines visible."
9514   (interactive)
9515   (gnus-summary-toggle-header 1))
9516
9517 (defun gnus-summary-caesar-message (&optional arg)
9518   "Caesar rotate the current article by 13.
9519 With a non-numerical prefix, also rotate headers.  A numerical
9520 prefix specifies how many places to rotate each letter forward."
9521   (interactive "P")
9522   (gnus-summary-select-article)
9523   (let ((mail-header-separator ""))
9524     (gnus-eval-in-buffer-window gnus-article-buffer
9525       (save-restriction
9526         (widen)
9527         (let ((start (window-start))
9528               (inhibit-read-only t))
9529           (if (equal arg '(4))
9530               (message-caesar-buffer-body nil t)
9531             (message-caesar-buffer-body arg))
9532           (set-window-start (get-buffer-window (current-buffer)) start)))))
9533   ;; Create buttons and stuff...
9534   (gnus-treat-article nil))
9535
9536 (declare-function idna-to-unicode "ext:idna" (str))
9537
9538 (defun gnus-summary-idna-message (&optional arg)
9539   "Decode IDNA encoded domain names in the current articles.
9540 IDNA encoded domain names looks like `xn--bar'.  If a string
9541 remain unencoded after running this function, it is likely an
9542 invalid IDNA string (`xn--bar' is invalid).
9543
9544 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9545 installed for this command to work."
9546   (interactive "P")
9547   (if (not (and (condition-case nil (require 'idna)
9548                   (file-error))
9549                 (mm-coding-system-p 'utf-8)
9550                 (executable-find (symbol-value 'idna-program))))
9551       (gnus-message
9552        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9553     (gnus-summary-select-article)
9554     (let ((mail-header-separator ""))
9555       (gnus-eval-in-buffer-window gnus-article-buffer
9556         (save-restriction
9557           (widen)
9558           (let ((start (window-start))
9559                 buffer-read-only)
9560             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9561               (replace-match (idna-to-unicode (match-string 1))))
9562             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9563
9564 (defun gnus-summary-morse-message (&optional arg)
9565   "Morse decode the current article."
9566   (interactive "P")
9567   (gnus-summary-select-article)
9568   (let ((mail-header-separator ""))
9569     (gnus-eval-in-buffer-window gnus-article-buffer
9570       (save-excursion
9571         (save-restriction
9572           (widen)
9573           (let ((pos (window-start))
9574                 (inhibit-read-only t))
9575             (goto-char (point-min))
9576             (when (message-goto-body)
9577               (gnus-narrow-to-body))
9578             (goto-char (point-min))
9579             (while (search-forward "·" (point-max) t)
9580               (replace-match "."))
9581             (unmorse-region (point-min) (point-max))
9582             (widen)
9583             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9584
9585 (defun gnus-summary-stop-page-breaking ()
9586   "Stop page breaking in the current article."
9587   (interactive)
9588   (gnus-summary-select-article)
9589   (gnus-eval-in-buffer-window gnus-article-buffer
9590     (widen)
9591     (when (gnus-visual-p 'page-marker)
9592       (let ((inhibit-read-only t))
9593         (gnus-remove-text-with-property 'gnus-prev)
9594         (gnus-remove-text-with-property 'gnus-next))
9595       (setq gnus-page-broken nil))))
9596
9597 (defun gnus-summary-move-article (&optional n to-newsgroup
9598                                             select-method action)
9599   "Move the current article to a different newsgroup.
9600 If N is a positive number, move the N next articles.
9601 If N is a negative number, move the N previous articles.
9602 If N is nil and any articles have been marked with the process mark,
9603 move those articles instead.
9604 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9605 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9606 re-spool using this method.
9607
9608 When called interactively with TO-NEWSGROUP being nil, the value of
9609 the variable `gnus-move-split-methods' is used for finding a default
9610 for the target newsgroup.
9611
9612 For this function to work, both the current newsgroup and the
9613 newsgroup that you want to move to have to support the `request-move'
9614 and `request-accept' functions.
9615
9616 ACTION can be either `move' (the default), `crosspost' or `copy'."
9617   (interactive "P")
9618   (unless action
9619     (setq action 'move))
9620   ;; Check whether the source group supports the required functions.
9621   (cond ((and (eq action 'move)
9622               (not (gnus-check-backend-function
9623                     'request-move-article gnus-newsgroup-name)))
9624          (error "The current group does not support article moving"))
9625         ((and (eq action 'crosspost)
9626               (not (gnus-check-backend-function
9627                     'request-replace-article gnus-newsgroup-name)))
9628          (error "The current group does not support article editing")))
9629   (let ((articles (gnus-summary-work-articles n))
9630         (prefix (if (gnus-check-backend-function
9631                      'request-move-article gnus-newsgroup-name)
9632                     (funcall gnus-move-group-prefix-function
9633                              gnus-newsgroup-name)
9634                   ""))
9635         (names '((move "Move" "Moving")
9636                  (copy "Copy" "Copying")
9637                  (crosspost "Crosspost" "Crossposting")))
9638         (copy-buf (save-excursion
9639                     (nnheader-set-temp-buffer " *copy article*")))
9640         art-group to-method new-xref article to-groups
9641         articles-to-update-marks encoded)
9642     (unless (assq action names)
9643       (error "Unknown action %s" action))
9644     ;; Read the newsgroup name.
9645     (when (and (not to-newsgroup)
9646                (not select-method))
9647       (if (and gnus-move-split-methods
9648                (not
9649                 (and (memq gnus-current-article articles)
9650                      (gnus-buffer-live-p gnus-original-article-buffer))))
9651           ;; When `gnus-move-split-methods' is non-nil, we have to
9652           ;; select an article to give `gnus-read-move-group-name' an
9653           ;; opportunity to suggest an appropriate default.  However,
9654           ;; we needn't render or mark the article.
9655           (let ((gnus-display-mime-function nil)
9656                 (gnus-article-prepare-hook nil)
9657                 (gnus-mark-article-hook nil))
9658             (gnus-summary-select-article nil nil nil (car articles))))
9659       (setq to-newsgroup (gnus-read-move-group-name
9660                           (cadr (assq action names))
9661                           (symbol-value
9662                            (intern (format "gnus-current-%s-group" action)))
9663                           articles prefix)
9664             encoded to-newsgroup
9665             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9666       (set (intern (format "gnus-current-%s-group" action))
9667            (mm-decode-coding-string
9668             to-newsgroup
9669             (gnus-group-name-charset to-method to-newsgroup))))
9670     (unless to-method
9671       (setq to-method (or select-method
9672                           (gnus-server-to-method
9673                            (gnus-group-method to-newsgroup)))))
9674     (setq to-newsgroup
9675           (or encoded
9676               (and to-newsgroup
9677                    (mm-encode-coding-string
9678                     to-newsgroup
9679                     (gnus-group-name-charset to-method to-newsgroup)))))
9680     ;; Check the method we are to move this article to...
9681     (unless (gnus-check-backend-function
9682              'request-accept-article (car to-method))
9683       (error "%s does not support article copying" (car to-method)))
9684     (unless (gnus-check-server to-method)
9685       (error "Can't open server %s" (car to-method)))
9686     (gnus-message 6 "%s to %s: %s..."
9687                   (caddr (assq action names))
9688                   (or (car select-method)
9689                       (gnus-group-decoded-name to-newsgroup))
9690                   articles)
9691     (while articles
9692       (setq article (pop articles))
9693       (setq
9694        art-group
9695        (cond
9696         ;; Move the article.
9697         ((eq action 'move)
9698          ;; Remove this article from future suppression.
9699          (gnus-dup-unsuppress-article article)
9700          (let* ((from-method (gnus-find-method-for-group
9701                               gnus-newsgroup-name))
9702                 (to-method (or select-method
9703                                (gnus-find-method-for-group to-newsgroup)))
9704                 (move-is-internal (gnus-server-equal from-method to-method)))
9705            (gnus-request-move-article
9706             article                     ; Article to move
9707             gnus-newsgroup-name         ; From newsgroup
9708             (nth 1 (gnus-find-method-for-group
9709                     gnus-newsgroup-name)) ; Server
9710             (list 'gnus-request-accept-article
9711                   to-newsgroup (list 'quote select-method)
9712                   (not articles) t)     ; Accept form
9713             (not articles)              ; Only save nov last time
9714             (and move-is-internal
9715                  to-newsgroup           ; Not respooling
9716                  (gnus-group-real-name to-newsgroup))))) ; Is this move internal?
9717         ;; Copy the article.
9718         ((eq action 'copy)
9719          (with-current-buffer copy-buf
9720            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9721              (save-restriction
9722                (nnheader-narrow-to-headers)
9723                (dolist (hdr gnus-copy-article-ignored-headers)
9724                  (message-remove-header hdr t)))
9725              (gnus-request-accept-article
9726               to-newsgroup select-method (not articles) t))))
9727         ;; Crosspost the article.
9728         ((eq action 'crosspost)
9729          (let ((xref (message-tokenize-header
9730                       (mail-header-xref (gnus-summary-article-header article))
9731                       " ")))
9732            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9733                                   ":" (number-to-string article)))
9734            (unless xref
9735              (setq xref (list (system-name))))
9736            (setq new-xref
9737                  (concat
9738                   (mapconcat 'identity
9739                              (delete "Xref:" (delete new-xref xref))
9740                              " ")
9741                   " " new-xref))
9742            (with-current-buffer copy-buf
9743              ;; First put the article in the destination group.
9744              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9745              (when (consp (setq art-group
9746                                 (gnus-request-accept-article
9747                                  to-newsgroup select-method (not articles) t)))
9748                (setq new-xref (concat new-xref " " (car art-group)
9749                                       ":"
9750                                       (number-to-string (cdr art-group))))
9751                ;; Now we have the new Xrefs header, so we insert
9752                ;; it and replace the new article.
9753                (nnheader-replace-header "Xref" new-xref)
9754                (gnus-request-replace-article
9755                 (cdr art-group) to-newsgroup (current-buffer) t)
9756                art-group))))))
9757       (cond
9758        ((not art-group)
9759         (gnus-message 1 "Couldn't %s article %s: %s"
9760                       (cadr (assq action names)) article
9761                       (nnheader-get-report (car to-method))))
9762        ((eq art-group 'junk)
9763         (when (eq action 'move)
9764           (gnus-summary-mark-article article gnus-canceled-mark)
9765           (gnus-message 4 "Deleted article %s" article)
9766           ;; run the delete hook
9767           (run-hook-with-args 'gnus-summary-article-delete-hook
9768                               action
9769                               (gnus-data-header
9770                                (assoc article (gnus-data-list nil)))
9771                               gnus-newsgroup-name nil
9772                               select-method)))
9773        (t
9774         (let* ((pto-group (gnus-group-prefixed-name
9775                            (car art-group) to-method))
9776                (info (gnus-get-info pto-group))
9777                (to-group (gnus-info-group info))
9778                to-marks)
9779           ;; Update the group that has been moved to.
9780           (when (and info
9781                      (memq action '(move copy)))
9782             (unless (member to-group to-groups)
9783               (push to-group to-groups))
9784
9785             (unless (memq article gnus-newsgroup-unreads)
9786               (push 'read to-marks)
9787               (gnus-info-set-read
9788                info (gnus-add-to-range (gnus-info-read info)
9789                                        (list (cdr art-group)))))
9790
9791             ;; See whether the article is to be put in the cache.
9792             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9793                    (marks (if expirable
9794                               gnus-article-mark-lists
9795                             (delete '(expirable . expire)
9796                                     (copy-sequence gnus-article-mark-lists))))
9797                    (to-article (cdr art-group)))
9798
9799               ;; Enter the article into the cache in the new group,
9800               ;; if that is required.
9801               (when gnus-use-cache
9802                 (gnus-cache-possibly-enter-article
9803                  to-group to-article
9804                  (memq article gnus-newsgroup-marked)
9805                  (memq article gnus-newsgroup-dormant)
9806                  (memq article gnus-newsgroup-unreads)))
9807
9808               (when gnus-preserve-marks
9809                 ;; Copy any marks over to the new group.
9810                 (when (and (equal to-group gnus-newsgroup-name)
9811                            (not (memq article gnus-newsgroup-unreads)))
9812                   ;; Mark this article as read in this group.
9813                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9814                   (setcdr (gnus-active to-group) to-article)
9815                   (setcdr gnus-newsgroup-active to-article))
9816
9817                 (while marks
9818                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9819                     (when (memq article (symbol-value
9820                                          (intern (format "gnus-newsgroup-%s"
9821                                                          (caar marks)))))
9822                       (push (cdar marks) to-marks)
9823                       ;; If the other group is the same as this group,
9824                       ;; then we have to add the mark to the list.
9825                       (when (equal to-group gnus-newsgroup-name)
9826                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9827                              (cons to-article
9828                                    (symbol-value
9829                                     (intern (format "gnus-newsgroup-%s"
9830                                                     (caar marks)))))))
9831                       ;; Copy the marks to other group.
9832                       (gnus-add-marked-articles
9833                        to-group (cdar marks) (list to-article) info)))
9834                   (setq marks (cdr marks)))
9835
9836                 (when (and expirable
9837                            gnus-mark-copied-or-moved-articles-as-expirable
9838                            (not (memq 'expire to-marks)))
9839                   ;; Mark this article as expirable.
9840                   (push 'expire to-marks)
9841                   (when (equal to-group gnus-newsgroup-name)
9842                     (push to-article gnus-newsgroup-expirable))
9843                   ;; Copy the expirable mark to other group.
9844                   (gnus-add-marked-articles
9845                    to-group 'expire (list to-article) info))
9846
9847                 (when to-marks
9848                   (gnus-request-set-mark
9849                    to-group (list (list (list to-article) 'add to-marks)))))
9850
9851               (gnus-dribble-enter
9852                (concat "(gnus-group-set-info '"
9853                        (gnus-prin1-to-string (gnus-get-info to-group))
9854                        ")"))))
9855
9856           ;; Update the Xref header in this article to point to
9857           ;; the new crossposted article we have just created.
9858           (when (eq action 'crosspost)
9859             (with-current-buffer copy-buf
9860               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9861               (nnheader-replace-header "Xref" new-xref)
9862               (gnus-request-replace-article
9863                article gnus-newsgroup-name (current-buffer) t)))
9864
9865           ;; run the move/copy/crosspost/respool hook
9866           (run-hook-with-args 'gnus-summary-article-move-hook
9867                               action
9868                               (gnus-data-header
9869                                (assoc article (gnus-data-list nil)))
9870                               gnus-newsgroup-name
9871                               to-newsgroup
9872                               select-method))
9873
9874         ;;;!!!Why is this necessary?
9875         (set-buffer gnus-summary-buffer)
9876
9877         (when (eq action 'move)
9878           (save-excursion
9879             (gnus-summary-goto-subject article)
9880             (gnus-summary-mark-article article gnus-canceled-mark)))))
9881       (push article articles-to-update-marks))
9882
9883     (save-excursion
9884       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9885     ;; Re-activate all groups that have been moved to.
9886     (with-current-buffer gnus-group-buffer
9887       (let ((gnus-group-marked to-groups))
9888         (gnus-group-get-new-news-this-group nil t)))
9889
9890     (gnus-kill-buffer copy-buf)
9891     (gnus-summary-position-point)
9892     (gnus-set-mode-line 'summary)))
9893
9894 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9895   "Copy the current article to some other group.
9896 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9897 When called interactively, if TO-NEWSGROUP is nil, use the value of
9898 the variable `gnus-move-split-methods' for finding a default target
9899 newsgroup.
9900 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9901 re-spool using this method."
9902   (interactive "P")
9903   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9904
9905 (defun gnus-summary-crosspost-article (&optional n)
9906   "Crosspost the current article to some other group."
9907   (interactive "P")
9908   (gnus-summary-move-article n nil nil 'crosspost))
9909
9910 (defcustom gnus-summary-respool-default-method nil
9911   "Default method type for respooling an article.
9912 If nil, use to the current newsgroup method."
9913   :type 'symbol
9914   :group 'gnus-summary-mail)
9915
9916 (defun gnus-summary-respool-article (&optional n method)
9917   "Respool the current article.
9918 The article will be squeezed through the mail spooling process again,
9919 which means that it will be put in some mail newsgroup or other
9920 depending on `nnmail-split-methods'.
9921 If N is a positive number, respool the N next articles.
9922 If N is a negative number, respool the N previous articles.
9923 If N is nil and any articles have been marked with the process mark,
9924 respool those articles instead.
9925
9926 Respooling can be done both from mail groups and \"real\" newsgroups.
9927 In the former case, the articles in question will be moved from the
9928 current group into whatever groups they are destined to.  In the
9929 latter case, they will be copied into the relevant groups."
9930   (interactive
9931    (list current-prefix-arg
9932          (let* ((methods (gnus-methods-using 'respool))
9933                 (methname
9934                  (symbol-name (or gnus-summary-respool-default-method
9935                                   (car (gnus-find-method-for-group
9936                                         gnus-newsgroup-name)))))
9937                 (method
9938                  (gnus-completing-read-with-default
9939                   methname "Backend to use when respooling"
9940                   methods nil t nil 'gnus-mail-method-history))
9941                 ms)
9942            (cond
9943             ((zerop (length (setq ms (gnus-servers-using-backend
9944                                       (intern method)))))
9945              (list (intern method) ""))
9946             ((= 1 (length ms))
9947              (car ms))
9948             (t
9949              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9950                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9951                            ms-alist))))))))
9952   (unless method
9953     (error "No method given for respooling"))
9954   (if (assoc (symbol-name
9955               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9956              (gnus-methods-using 'respool))
9957       (gnus-summary-move-article n nil method)
9958     (gnus-summary-copy-article n nil method)))
9959
9960 (defun gnus-summary-import-article (file &optional edit)
9961   "Import an arbitrary file into a mail newsgroup."
9962   (interactive "fImport file: \nP")
9963   (let ((group gnus-newsgroup-name)
9964         (now (current-time))
9965         atts lines group-art)
9966     (unless (gnus-check-backend-function 'request-accept-article group)
9967       (error "%s does not support article importing" group))
9968     (or (file-readable-p file)
9969         (not (file-regular-p file))
9970         (error "Can't read %s" file))
9971     (with-current-buffer (gnus-get-buffer-create " *import file*")
9972       (erase-buffer)
9973       (nnheader-insert-file-contents file)
9974       (goto-char (point-min))
9975       (if (nnheader-article-p)
9976           (save-restriction
9977             (goto-char (point-min))
9978             (search-forward "\n\n" nil t)
9979             (narrow-to-region (point-min) (1- (point)))
9980             (goto-char (point-min))
9981             (unless (re-search-forward "^date:" nil t)
9982               (goto-char (point-max))
9983               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9984        ;; This doesn't look like an article, so we fudge some headers.
9985         (setq atts (file-attributes file)
9986               lines (count-lines (point-min) (point-max)))
9987         (insert "From: " (read-string "From: ") "\n"
9988                 "Subject: " (read-string "Subject: ") "\n"
9989                 "Date: " (message-make-date (nth 5 atts)) "\n"
9990                 "Message-ID: " (message-make-message-id) "\n"
9991                 "Lines: " (int-to-string lines) "\n"
9992                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9993       (setq group-art (gnus-request-accept-article group nil t))
9994       (kill-buffer (current-buffer)))
9995     (setq gnus-newsgroup-active (gnus-activate-group group))
9996     (forward-line 1)
9997     (gnus-summary-goto-article (cdr group-art) nil t)
9998     (when edit
9999       (gnus-summary-edit-article))))
10000
10001 (defun gnus-summary-create-article ()
10002   "Create an article in a mail newsgroup."
10003   (interactive)
10004   (let ((group gnus-newsgroup-name)
10005         (now (current-time))
10006         group-art)
10007     (unless (gnus-check-backend-function 'request-accept-article group)
10008       (error "%s does not support article importing" group))
10009     (with-current-buffer (gnus-get-buffer-create " *import file*")
10010       (erase-buffer)
10011       (goto-char (point-min))
10012       ;; This doesn't look like an article, so we fudge some headers.
10013       (insert "From: " (read-string "From: ") "\n"
10014               "Subject: " (read-string "Subject: ") "\n"
10015               "Date: " (message-make-date now) "\n"
10016               "Message-ID: " (message-make-message-id) "\n")
10017       (setq group-art (gnus-request-accept-article group nil t))
10018       (kill-buffer (current-buffer)))
10019     (setq gnus-newsgroup-active (gnus-activate-group group))
10020     (forward-line 1)
10021     (gnus-summary-goto-article (cdr group-art) nil t)
10022     (gnus-summary-edit-article)))
10023
10024 (defun gnus-summary-article-posted-p ()
10025   "Say whether the current (mail) article is available from news as well.
10026 This will be the case if the article has both been mailed and posted."
10027   (interactive)
10028   (let ((id (mail-header-references (gnus-summary-article-header)))
10029         (gnus-override-method (car (gnus-refer-article-methods))))
10030     (if (gnus-request-head id "")
10031         (gnus-message 2 "The current message was found on %s"
10032                       gnus-override-method)
10033       (gnus-message 2 "The current message couldn't be found on %s"
10034                     gnus-override-method)
10035       nil)))
10036
10037 (defun gnus-summary-expire-articles (&optional now)
10038   "Expire all articles that are marked as expirable in the current group."
10039   (interactive)
10040   (when (and (not gnus-group-is-exiting-without-update-p)
10041              (gnus-check-backend-function
10042               'request-expire-articles gnus-newsgroup-name))
10043     ;; This backend supports expiry.
10044     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10045            (expirable (if total
10046                           (progn
10047                             ;; We need to update the info for
10048                             ;; this group for `gnus-list-of-read-articles'
10049                             ;; to give us the right answer.
10050                             (gnus-run-hooks 'gnus-exit-group-hook)
10051                             (gnus-summary-update-info)
10052                             (gnus-list-of-read-articles gnus-newsgroup-name))
10053                         (setq gnus-newsgroup-expirable
10054                               (sort gnus-newsgroup-expirable '<))))
10055            (expiry-wait (if now 'immediate
10056                           (gnus-group-find-parameter
10057                            gnus-newsgroup-name 'expiry-wait)))
10058            (nnmail-expiry-target
10059             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10060                 nnmail-expiry-target))
10061            es)
10062       (when expirable
10063         ;; There are expirable articles in this group, so we run them
10064         ;; through the expiry process.
10065         (gnus-message 6 "Expiring articles...")
10066         (unless (gnus-check-group gnus-newsgroup-name)
10067           (error "Can't open server for %s" gnus-newsgroup-name))
10068         ;; The list of articles that weren't expired is returned.
10069         (save-excursion
10070           (if expiry-wait
10071               (let ((nnmail-expiry-wait-function nil)
10072                     (nnmail-expiry-wait expiry-wait))
10073                 (setq es (gnus-request-expire-articles
10074                           expirable gnus-newsgroup-name)))
10075             (setq es (gnus-request-expire-articles
10076                       expirable gnus-newsgroup-name)))
10077           (unless total
10078             (setq gnus-newsgroup-expirable es))
10079           ;; We go through the old list of expirable, and mark all
10080           ;; really expired articles as nonexistent.
10081           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10082             (let ((gnus-use-cache nil))
10083               (dolist (article expirable)
10084                 (when (and (not (memq article es))
10085                            (gnus-data-find article))
10086                   (gnus-summary-mark-article article gnus-canceled-mark)
10087                   (run-hook-with-args 'gnus-summary-article-expire-hook
10088                                       'delete
10089                                       (gnus-data-header
10090                                        (assoc article (gnus-data-list nil)))
10091                                       gnus-newsgroup-name
10092                                       nil
10093                                       nil))))))
10094         (gnus-message 6 "Expiring articles...done")))))
10095
10096 (defun gnus-summary-expire-articles-now ()
10097   "Expunge all expirable articles in the current group.
10098 This means that *all* articles that are marked as expirable will be
10099 deleted forever, right now."
10100   (interactive)
10101   (or gnus-expert-user
10102       (gnus-yes-or-no-p
10103        "Are you really, really sure you want to delete all expirable messages? ")
10104       (error "Phew!"))
10105   (gnus-summary-expire-articles t))
10106
10107 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10108 (defun gnus-summary-delete-article (&optional n)
10109   "Delete the N next (mail) articles.
10110 This command actually deletes articles.  This is not a marking
10111 command.  The article will disappear forever from your life, never to
10112 return.
10113
10114 If N is negative, delete backwards.
10115 If N is nil and articles have been marked with the process mark,
10116 delete these instead.
10117
10118 If `gnus-novice-user' is non-nil you will be asked for
10119 confirmation before the articles are deleted."
10120   (interactive "P")
10121   (unless (gnus-check-backend-function 'request-expire-articles
10122                                        gnus-newsgroup-name)
10123     (error "The current newsgroup does not support article deletion"))
10124   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10125     (error "Couldn't open server"))
10126   ;; Compute the list of articles to delete.
10127   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10128         (nnmail-expiry-target 'delete)
10129         not-deleted)
10130     (if (and gnus-novice-user
10131              (not (gnus-yes-or-no-p
10132                    (format "Do you really want to delete %s forever? "
10133                            (if (> (length articles) 1)
10134                                (format "these %s articles" (length articles))
10135                              "this article")))))
10136         ()
10137       ;; Delete the articles.
10138       (setq not-deleted (gnus-request-expire-articles
10139                          articles gnus-newsgroup-name 'force))
10140       (save-excursion
10141         (while articles
10142           (gnus-summary-remove-process-mark (car articles))
10143           ;; The backend might not have been able to delete the article
10144           ;; after all.
10145           (unless (memq (car articles) not-deleted)
10146             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10147           (let* ((article (car articles))
10148                  (ghead  (gnus-data-header
10149                           (assoc article (gnus-data-list nil)))))
10150             (run-hook-with-args 'gnus-summary-article-delete-hook
10151                                 'delete ghead gnus-newsgroup-name nil
10152                                 nil))
10153           (setq articles (cdr articles))))
10154       (when not-deleted
10155         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10156     (gnus-summary-position-point)
10157     (gnus-set-mode-line 'summary)
10158     not-deleted))
10159
10160 (defun gnus-summary-edit-article (&optional arg)
10161   "Edit the current article.
10162 This will have permanent effect only in mail groups.
10163 If ARG is nil, edit the decoded articles.
10164 If ARG is 1, edit the raw articles.
10165 If ARG is 2, edit the raw articles even in read-only groups.
10166 If ARG is 3, edit the articles with the current handles.
10167 Otherwise, allow editing of articles even in read-only
10168 groups."
10169   (interactive "P")
10170   (let (force raw current-handles)
10171     (cond
10172      ((null arg))
10173      ((eq arg 1)
10174       (setq raw t))
10175      ((eq arg 2)
10176       (setq raw t
10177             force t))
10178      ((eq arg 3)
10179       (setq current-handles
10180             (and (gnus-buffer-live-p gnus-article-buffer)
10181                  (with-current-buffer gnus-article-buffer
10182                    (prog1
10183                        gnus-article-mime-handles
10184                      (setq gnus-article-mime-handles nil))))))
10185      (t
10186       (setq force t)))
10187     (when (and raw (not force)
10188                (member gnus-newsgroup-name '("nndraft:delayed"
10189                                              "nndraft:drafts"
10190                                              "nndraft:queue")))
10191       (error "Can't edit the raw article in group %s"
10192              gnus-newsgroup-name))
10193     (with-current-buffer gnus-summary-buffer
10194       (let ((mail-parse-charset gnus-newsgroup-charset)
10195             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10196         (gnus-set-global-variables)
10197         (when (and (not force)
10198                    (gnus-group-read-only-p))
10199           (error "The current newsgroup does not support article editing"))
10200         (gnus-summary-show-article t)
10201         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10202           (with-current-buffer gnus-article-buffer
10203             (mm-enable-multibyte)))
10204         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10205             (setq raw t))
10206         (gnus-article-edit-article
10207          (if raw 'ignore
10208            `(lambda ()
10209               (let ((mbl mml-buffer-list))
10210                 (setq mml-buffer-list nil)
10211                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10212                   (mime-to-mml ,'current-handles))
10213                 (let ((mbl1 mml-buffer-list))
10214                   (setq mml-buffer-list mbl)
10215                   (set (make-local-variable 'mml-buffer-list) mbl1))
10216                 (gnus-make-local-hook 'kill-buffer-hook)
10217                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10218          `(lambda (no-highlight)
10219             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10220                   (message-options message-options)
10221                   (message-options-set-recipient)
10222                   (mail-parse-ignored-charsets
10223                    ',gnus-newsgroup-ignored-charsets)
10224                   (rfc2047-header-encoding-alist
10225                    ',(let ((charset (gnus-group-name-charset
10226                                      (gnus-find-method-for-group
10227                                       gnus-newsgroup-name)
10228                                      gnus-newsgroup-name)))
10229                        (append (list (cons "Newsgroups" charset)
10230                                      (cons "Followup-To" charset)
10231                                      (cons "Xref" charset))
10232                                rfc2047-header-encoding-alist))))
10233               ,(if (not raw) '(progn
10234                                 (mml-to-mime)
10235                                 (mml-destroy-buffers)
10236                                 (remove-hook 'kill-buffer-hook
10237                                              'mml-destroy-buffers t)
10238                                 (kill-local-variable 'mml-buffer-list)))
10239               (gnus-summary-edit-article-done
10240                ,(or (mail-header-references gnus-current-headers) "")
10241                ,(gnus-group-read-only-p)
10242                ,gnus-summary-buffer no-highlight))))))))
10243
10244 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10245
10246 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10247                                                  no-highlight)
10248   "Make edits to the current article permanent."
10249   (interactive)
10250   (save-excursion
10251    ;; The buffer restriction contains the entire article if it exists.
10252     (when (article-goto-body)
10253       (let ((lines (count-lines (point) (point-max)))
10254             (length (- (point-max) (point)))
10255             (case-fold-search t)
10256             (body (copy-marker (point))))
10257         (goto-char (point-min))
10258         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10259           (delete-region (match-beginning 1) (match-end 1))
10260           (insert (number-to-string length)))
10261         (goto-char (point-min))
10262         (when (re-search-forward
10263                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10264           (delete-region (match-beginning 1) (match-end 1))
10265           (insert (number-to-string length)))
10266         (goto-char (point-min))
10267         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10268           (delete-region (match-beginning 1) (match-end 1))
10269           (insert (number-to-string lines))))))
10270   ;; Replace the article.
10271   (let ((buf (current-buffer)))
10272     (with-temp-buffer
10273       (insert-buffer-substring buf)
10274
10275       (if (and (not read-only)
10276                (not (gnus-request-replace-article
10277                      (cdr gnus-article-current) (car gnus-article-current)
10278                      (current-buffer) t)))
10279           (error "Couldn't replace article")
10280         ;; Update the summary buffer.
10281         (if (and references
10282                  (equal (message-tokenize-header references " ")
10283                         (message-tokenize-header
10284                          (or (message-fetch-field "references") "") " ")))
10285             ;; We only have to update this line.
10286             (save-excursion
10287               (save-restriction
10288                 (message-narrow-to-head)
10289                 (let ((head (buffer-substring-no-properties
10290                              (point-min) (point-max)))
10291                       header)
10292                   (with-temp-buffer
10293                     (insert (format "211 %d Article retrieved.\n"
10294                                     (cdr gnus-article-current)))
10295                     (insert head)
10296                     (insert ".\n")
10297                     (let ((nntp-server-buffer (current-buffer)))
10298                       (setq header (car (gnus-get-newsgroup-headers
10299                                          nil t))))
10300                     (with-current-buffer gnus-summary-buffer
10301                       (gnus-data-set-header
10302                        (gnus-data-find (cdr gnus-article-current))
10303                        header)
10304                       (gnus-summary-update-article-line
10305                        (cdr gnus-article-current) header)
10306                       (if (gnus-summary-goto-subject
10307                            (cdr gnus-article-current) nil t)
10308                           (gnus-summary-update-secondary-mark
10309                            (cdr gnus-article-current))))))))
10310           ;; Update threads.
10311           (set-buffer (or buffer gnus-summary-buffer))
10312           (gnus-summary-update-article (cdr gnus-article-current))
10313           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10314               (gnus-summary-update-secondary-mark
10315                (cdr gnus-article-current))))
10316         ;; Prettify the article buffer again.
10317         (unless no-highlight
10318           (with-current-buffer gnus-article-buffer
10319             ;;;!!! Fix this -- article should be rehighlighted.
10320             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10321             (set-buffer gnus-original-article-buffer)
10322             (gnus-request-article
10323              (cdr gnus-article-current)
10324              (car gnus-article-current) (current-buffer))))
10325         ;; Prettify the summary buffer line.
10326         (when (gnus-visual-p 'summary-highlight 'highlight)
10327           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10328
10329 (defun gnus-summary-edit-wash (key)
10330   "Perform editing command KEY in the article buffer."
10331   (interactive
10332    (list
10333     (progn
10334       (message "%s" (concat (this-command-keys) "- "))
10335       (read-char))))
10336   (message "")
10337   (gnus-summary-edit-article)
10338   (execute-kbd-macro (concat (this-command-keys) key))
10339   (gnus-article-edit-done))
10340
10341 ;;; Respooling
10342
10343 (defun gnus-summary-respool-query (&optional silent trace)
10344   "Query where the respool algorithm would put this article."
10345   (interactive)
10346   (let (gnus-mark-article-hook)
10347     (gnus-summary-select-article)
10348     (with-current-buffer gnus-original-article-buffer
10349       (let ((groups (nnmail-article-group 'identity trace)))
10350         (unless silent
10351           (if groups
10352               (message "This message would go to %s"
10353                        (mapconcat 'car groups ", "))
10354             (message "This message would go to no groups"))
10355           groups)))))
10356
10357 (defun gnus-summary-respool-trace ()
10358   "Trace where the respool algorithm would put this article.
10359 Display a buffer showing all fancy splitting patterns which matched."
10360   (interactive)
10361   (gnus-summary-respool-query nil t))
10362
10363 ;; Summary marking commands.
10364
10365 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10366   "Mark articles which has the same subject as read, and then select the next.
10367 If UNMARK is positive, remove any kind of mark.
10368 If UNMARK is negative, tick articles."
10369   (interactive "P")
10370   (when unmark
10371     (setq unmark (prefix-numeric-value unmark)))
10372   (let ((count
10373          (gnus-summary-mark-same-subject
10374           (gnus-summary-article-subject) unmark)))
10375     ;; Select next unread article.  If auto-select-same mode, should
10376     ;; select the first unread article.
10377     (gnus-summary-next-article t (and gnus-auto-select-same
10378                                       (gnus-summary-article-subject)))
10379     (gnus-message 7 "%d article%s marked as %s"
10380                   count (if (= count 1) " is" "s are")
10381                   (if unmark "unread" "read"))))
10382
10383 (defun gnus-summary-kill-same-subject (&optional unmark)
10384   "Mark articles which has the same subject as read.
10385 If UNMARK is positive, remove any kind of mark.
10386 If UNMARK is negative, tick articles."
10387   (interactive "P")
10388   (when unmark
10389     (setq unmark (prefix-numeric-value unmark)))
10390   (let ((count
10391          (gnus-summary-mark-same-subject
10392           (gnus-summary-article-subject) unmark)))
10393     ;; If marked as read, go to next unread subject.
10394     (when (null unmark)
10395       ;; Go to next unread subject.
10396       (gnus-summary-next-subject 1 t))
10397     (gnus-message 7 "%d articles are marked as %s"
10398                   count (if unmark "unread" "read"))))
10399
10400 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10401   "Mark articles with same SUBJECT as read, and return marked number.
10402 If optional argument UNMARK is positive, remove any kinds of marks.
10403 If optional argument UNMARK is negative, mark articles as unread instead."
10404   (let ((count 1))
10405     (save-excursion
10406       (cond
10407        ((null unmark)                   ; Mark as read.
10408         (while (and
10409                 (progn
10410                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10411                   (gnus-summary-show-thread) t)
10412                 (gnus-summary-find-subject subject))
10413           (setq count (1+ count))))
10414        ((> unmark 0)                    ; Tick.
10415         (while (and
10416                 (progn
10417                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10418                   (gnus-summary-show-thread) t)
10419                 (gnus-summary-find-subject subject))
10420           (setq count (1+ count))))
10421        (t                               ; Mark as unread.
10422         (while (and
10423                 (progn
10424                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10425                   (gnus-summary-show-thread) t)
10426                 (gnus-summary-find-subject subject))
10427           (setq count (1+ count)))))
10428       (gnus-set-mode-line 'summary)
10429       ;; Return the number of marked articles.
10430       count)))
10431
10432 (defun gnus-summary-mark-as-processable (n &optional unmark)
10433   "Set the process mark on the next N articles.
10434 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10435 the process mark instead.  The difference between N and the actual
10436 number of articles marked is returned."
10437   (interactive "P")
10438   (if (and (null n) (gnus-region-active-p))
10439       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10440     (setq n (prefix-numeric-value n))
10441     (let ((backward (< n 0))
10442           (n (abs n)))
10443       (while (and
10444               (> n 0)
10445               (if unmark
10446                   (gnus-summary-remove-process-mark
10447                    (gnus-summary-article-number))
10448                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10449               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10450         (setq n (1- n)))
10451       (when (/= 0 n)
10452         (gnus-message 7 "No more articles"))
10453       (gnus-summary-recenter)
10454       (gnus-summary-position-point)
10455       n)))
10456
10457 (defun gnus-summary-unmark-as-processable (n)
10458   "Remove the process mark from the next N articles.
10459 If N is negative, unmark backward instead.  The difference between N and
10460 the actual number of articles unmarked is returned."
10461   (interactive "P")
10462   (gnus-summary-mark-as-processable n t))
10463
10464 (defun gnus-summary-unmark-all-processable ()
10465   "Remove the process mark from all articles."
10466   (interactive)
10467   (save-excursion
10468     (while gnus-newsgroup-processable
10469       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10470   (gnus-summary-position-point))
10471
10472 (defun gnus-summary-add-mark (article type)
10473   "Mark ARTICLE with a mark of TYPE."
10474   (let ((vtype (car (assq type gnus-article-mark-lists)))
10475         var)
10476     (if (not vtype)
10477         (error "No such mark type: %s" type)
10478       (setq var (intern (format "gnus-newsgroup-%s" type)))
10479       (set var (cons article (symbol-value var)))
10480       (if (memq type '(processable cached replied forwarded recent saved))
10481           (gnus-summary-update-secondary-mark article)
10482         ;;; !!! This is bogus.  We should find out what primary
10483         ;;; !!! mark we want to set.
10484         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10485
10486 (defun gnus-summary-mark-as-expirable (n)
10487   "Mark N articles forward as expirable.
10488 If N is negative, mark backward instead.  The difference between N and
10489 the actual number of articles marked is returned."
10490   (interactive "p")
10491   (gnus-summary-mark-forward n gnus-expirable-mark))
10492
10493 (defun gnus-summary-mark-as-spam (n)
10494   "Mark N articles forward as spam.
10495 If N is negative, mark backward instead.  The difference between N and
10496 the actual number of articles marked is returned."
10497   (interactive "p")
10498   (gnus-summary-mark-forward n gnus-spam-mark))
10499
10500 (defun gnus-summary-mark-article-as-replied (article)
10501   "Mark ARTICLE as replied to and update the summary line.
10502 ARTICLE can also be a list of articles."
10503   (interactive (list (gnus-summary-article-number)))
10504   (let ((articles (if (listp article) article (list article))))
10505     (dolist (article articles)
10506       (unless (numberp article)
10507         (error "%s is not a number" article))
10508       (push article gnus-newsgroup-replied)
10509       (let ((inhibit-read-only t))
10510         (when (gnus-summary-goto-subject article nil t)
10511           (gnus-summary-update-secondary-mark article))))))
10512
10513 (defun gnus-summary-mark-article-as-forwarded (article)
10514   "Mark ARTICLE as forwarded and update the summary line.
10515 ARTICLE can also be a list of articles."
10516   (let ((articles (if (listp article) article (list article))))
10517     (dolist (article articles)
10518       (push article gnus-newsgroup-forwarded)
10519       (let ((inhibit-read-only t))
10520         (when (gnus-summary-goto-subject article nil t)
10521           (gnus-summary-update-secondary-mark article))))))
10522
10523 (defun gnus-summary-set-bookmark (article)
10524   "Set a bookmark in current article."
10525   (interactive (list (gnus-summary-article-number)))
10526   (when (or (not (get-buffer gnus-article-buffer))
10527             (not gnus-current-article)
10528             (not gnus-article-current)
10529             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10530     (error "No current article selected"))
10531   ;; Remove old bookmark, if one exists.
10532   (gnus-pull article gnus-newsgroup-bookmarks)
10533   ;; Set the new bookmark, which is on the form
10534   ;; (article-number . line-number-in-body).
10535   (push
10536    (cons article
10537          (with-current-buffer gnus-article-buffer
10538            (count-lines
10539             (min (point)
10540                  (save-excursion
10541                    (article-goto-body)
10542                    (point)))
10543             (point))))
10544    gnus-newsgroup-bookmarks)
10545   (gnus-message 6 "A bookmark has been added to the current article."))
10546
10547 (defun gnus-summary-remove-bookmark (article)
10548   "Remove the bookmark from the current article."
10549   (interactive (list (gnus-summary-article-number)))
10550   ;; Remove old bookmark, if one exists.
10551   (if (not (assq article gnus-newsgroup-bookmarks))
10552       (gnus-message 6 "No bookmark in current article.")
10553     (gnus-pull article gnus-newsgroup-bookmarks)
10554     (gnus-message 6 "Removed bookmark.")))
10555
10556 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10557 (defun gnus-summary-mark-as-dormant (n)
10558   "Mark N articles forward as dormant.
10559 If N is negative, mark backward instead.  The difference between N and
10560 the actual number of articles marked is returned."
10561   (interactive "p")
10562   (gnus-summary-mark-forward n gnus-dormant-mark))
10563
10564 (defun gnus-summary-set-process-mark (article)
10565   "Set the process mark on ARTICLE and update the summary line."
10566   (setq gnus-newsgroup-processable
10567         (cons article
10568               (delq article gnus-newsgroup-processable)))
10569   (when (gnus-summary-goto-subject article)
10570     (gnus-summary-show-thread)
10571     (gnus-summary-goto-subject article)
10572     (gnus-summary-update-secondary-mark article)))
10573
10574 (defun gnus-summary-remove-process-mark (&rest articles)
10575   "Remove the process mark from ARTICLES and update the summary line."
10576   (dolist (article articles)
10577     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10578     (when (gnus-summary-goto-subject article)
10579       (gnus-summary-show-thread)
10580       (gnus-summary-goto-subject article)
10581       (gnus-summary-update-secondary-mark article)))
10582   t)
10583
10584 (defun gnus-summary-set-saved-mark (article)
10585   "Set the process mark on ARTICLE and update the summary line."
10586   (push article gnus-newsgroup-saved)
10587   (when (gnus-summary-goto-subject article)
10588     (gnus-summary-update-secondary-mark article)))
10589
10590 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10591   "Mark N articles as read forwards.
10592 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10593 The difference between N and the actual number of articles marked is
10594 returned.
10595 If NO-EXPIRE, auto-expiry will be inhibited."
10596   (interactive "p")
10597   (gnus-summary-show-thread)
10598   (let ((backward (< n 0))
10599         (gnus-summary-goto-unread
10600          (and gnus-summary-goto-unread
10601               (not (eq gnus-summary-goto-unread 'never))
10602               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10603                                     gnus-ticked-mark gnus-dormant-mark)))))
10604         (n (abs n))
10605         (mark (or mark gnus-del-mark)))
10606     (while (and (> n 0)
10607                 (gnus-summary-mark-article nil mark no-expire)
10608                 (zerop (gnus-summary-next-subject
10609                         (if backward -1 1)
10610                         (and gnus-summary-goto-unread
10611                              (not (eq gnus-summary-goto-unread 'never)))
10612                         t)))
10613       (setq n (1- n)))
10614     (when (/= 0 n)
10615       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10616     (gnus-summary-recenter)
10617     (gnus-summary-position-point)
10618     (gnus-set-mode-line 'summary)
10619     n))
10620
10621 (defun gnus-summary-mark-article-as-read (mark)
10622   "Mark the current article quickly as read with MARK."
10623   (let ((article (gnus-summary-article-number)))
10624     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10625     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10626     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10627     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10628     (push (cons article mark) gnus-newsgroup-reads)
10629     ;; Possibly remove from cache, if that is used.
10630     (when gnus-use-cache
10631       (gnus-cache-enter-remove-article article))
10632     ;; Allow the backend to change the mark.
10633     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10634     ;; Check for auto-expiry.
10635     (when (and gnus-newsgroup-auto-expire
10636                (memq mark gnus-auto-expirable-marks))
10637       (setq mark gnus-expirable-mark)
10638       ;; Let the backend know about the mark change.
10639       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10640       (push article gnus-newsgroup-expirable))
10641     ;; Set the mark in the buffer.
10642     (gnus-summary-update-mark mark 'unread)
10643     t))
10644
10645 (defun gnus-summary-mark-article-as-unread (mark)
10646   "Mark the current article quickly as unread with MARK."
10647   (let* ((article (gnus-summary-article-number))
10648          (old-mark (gnus-summary-article-mark article)))
10649     ;; Allow the backend to change the mark.
10650     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10651     (if (eq mark old-mark)
10652         t
10653       (if (<= article 0)
10654           (progn
10655             (gnus-error 1 "Can't mark negative article numbers")
10656             nil)
10657         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10658         (setq gnus-newsgroup-spam-marked
10659               (delq article gnus-newsgroup-spam-marked))
10660         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10661         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10662         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10663         (cond ((= mark gnus-ticked-mark)
10664                (setq gnus-newsgroup-marked
10665                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10666                                               article)))
10667               ((= mark gnus-spam-mark)
10668                (setq gnus-newsgroup-spam-marked
10669                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10670                                               article)))
10671               ((= mark gnus-dormant-mark)
10672                (setq gnus-newsgroup-dormant
10673                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10674                                               article)))
10675               (t
10676                (setq gnus-newsgroup-unreads
10677                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10678                                               article))))
10679         (gnus-pull article gnus-newsgroup-reads)
10680
10681         ;; See whether the article is to be put in the cache.
10682         (and gnus-use-cache
10683              (vectorp (gnus-summary-article-header article))
10684              (save-excursion
10685                (gnus-cache-possibly-enter-article
10686                 gnus-newsgroup-name article
10687                 (= mark gnus-ticked-mark)
10688                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10689
10690         ;; Fix the mark.
10691         (gnus-summary-update-mark mark 'unread)
10692         t))))
10693
10694 (defun gnus-summary-mark-article (&optional article mark no-expire)
10695   "Mark ARTICLE with MARK.  MARK can be any character.
10696 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10697 `??' (dormant) and `?E' (expirable).
10698 If MARK is nil, then the default character `?r' is used.
10699 If ARTICLE is nil, then the article on the current line will be
10700 marked.
10701 If NO-EXPIRE, auto-expiry will be inhibited."
10702   ;; The mark might be a string.
10703   (when (stringp mark)
10704     (setq mark (aref mark 0)))
10705   ;; If no mark is given, then we check auto-expiring.
10706   (when (null mark)
10707     (setq mark gnus-del-mark))
10708   (when (and (not no-expire)
10709              gnus-newsgroup-auto-expire
10710              (memq mark gnus-auto-expirable-marks))
10711     (setq mark gnus-expirable-mark))
10712   (let ((article (or article (gnus-summary-article-number)))
10713         (old-mark (gnus-summary-article-mark article)))
10714     ;; Allow the backend to change the mark.
10715     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10716     (if (eq mark old-mark)
10717         t
10718       (unless article
10719         (error "No article on current line"))
10720       (if (not (if (or (= mark gnus-unread-mark)
10721                        (= mark gnus-ticked-mark)
10722                        (= mark gnus-spam-mark)
10723                        (= mark gnus-dormant-mark))
10724                    (gnus-mark-article-as-unread article mark)
10725                  (gnus-mark-article-as-read article mark)))
10726           t
10727         ;; See whether the article is to be put in the cache.
10728         (and gnus-use-cache
10729              (not (= mark gnus-canceled-mark))
10730              (vectorp (gnus-summary-article-header article))
10731              (save-excursion
10732                (gnus-cache-possibly-enter-article
10733                 gnus-newsgroup-name article
10734                 (= mark gnus-ticked-mark)
10735                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10736
10737         (when (gnus-summary-goto-subject article nil t)
10738           (let ((inhibit-read-only t))
10739             (gnus-summary-show-thread)
10740             ;; Fix the mark.
10741             (gnus-summary-update-mark mark 'unread)
10742             t))))))
10743
10744 (defun gnus-summary-update-secondary-mark (article)
10745   "Update the secondary (read, process, cache) mark."
10746   (gnus-summary-update-mark
10747    (cond ((memq article gnus-newsgroup-processable)
10748           gnus-process-mark)
10749          ((memq article gnus-newsgroup-cached)
10750           gnus-cached-mark)
10751          ((memq article gnus-newsgroup-replied)
10752           gnus-replied-mark)
10753          ((memq article gnus-newsgroup-forwarded)
10754           gnus-forwarded-mark)
10755          ((memq article gnus-newsgroup-saved)
10756           gnus-saved-mark)
10757          ((memq article gnus-newsgroup-recent)
10758           gnus-recent-mark)
10759          ((memq article gnus-newsgroup-unseen)
10760           gnus-unseen-mark)
10761          (t gnus-no-mark))
10762    'replied)
10763   (when (gnus-visual-p 'summary-highlight 'highlight)
10764     (gnus-summary-highlight-line)
10765     (gnus-run-hooks 'gnus-summary-update-hook))
10766   t)
10767
10768 (defun gnus-summary-update-download-mark (article)
10769   "Update the download mark."
10770   (gnus-summary-update-mark
10771    (cond ((memq article gnus-newsgroup-undownloaded)
10772           gnus-undownloaded-mark)
10773          (gnus-newsgroup-agentized
10774           gnus-downloaded-mark)
10775          (t
10776           gnus-no-mark))
10777    'download)
10778   (gnus-summary-update-line t)
10779   t)
10780
10781 (defun gnus-summary-update-mark (mark type)
10782   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10783         (inhibit-read-only t))
10784     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10785     (when forward
10786       (when (looking-at "\r")
10787         (incf forward))
10788       (when (<= (+ forward (point)) (point-max))
10789         ;; Go to the right position on the line.
10790         (goto-char (+ forward (point)))
10791         ;; Replace the old mark with the new mark.
10792         (let ((to-insert
10793                (mm-subst-char-in-string
10794                 (char-after) mark
10795                 (buffer-substring (point) (1+ (point))))))
10796           (delete-region (point) (1+ (point)))
10797           (insert to-insert))
10798         ;; Optionally update the marks by some user rule.
10799         (when (eq type 'unread)
10800           (gnus-data-set-mark
10801            (gnus-data-find (gnus-summary-article-number)) mark)
10802           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10803
10804 (defun gnus-mark-article-as-read (article &optional mark)
10805   "Enter ARTICLE in the pertinent lists and remove it from others."
10806   ;; Make the article expirable.
10807   (let ((mark (or mark gnus-del-mark)))
10808     (setq gnus-newsgroup-expirable
10809           (if (= mark gnus-expirable-mark)
10810               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10811             (delq article gnus-newsgroup-expirable)))
10812     ;; Remove from unread and marked lists.
10813     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10814     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10815     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10816     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10817     (push (cons article mark) gnus-newsgroup-reads)
10818     ;; Possibly remove from cache, if that is used.
10819     (when gnus-use-cache
10820       (gnus-cache-enter-remove-article article))
10821     t))
10822
10823 (defun gnus-mark-article-as-unread (article &optional mark)
10824   "Enter ARTICLE in the pertinent lists and remove it from others."
10825   (let ((mark (or mark gnus-ticked-mark)))
10826     (if (<= article 0)
10827         (progn
10828           (gnus-error 1 "Can't mark negative article numbers")
10829           nil)
10830       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10831             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10832             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10833             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10834             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10835
10836       ;; Unsuppress duplicates?
10837       (when gnus-suppress-duplicates
10838         (gnus-dup-unsuppress-article article))
10839
10840       (cond ((= mark gnus-ticked-mark)
10841              (setq gnus-newsgroup-marked
10842                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10843             ((= mark gnus-spam-mark)
10844              (setq gnus-newsgroup-spam-marked
10845                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10846                                             article)))
10847             ((= mark gnus-dormant-mark)
10848              (setq gnus-newsgroup-dormant
10849                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10850             (t
10851              (setq gnus-newsgroup-unreads
10852                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10853       (gnus-pull article gnus-newsgroup-reads)
10854       t)))
10855
10856 (defalias 'gnus-summary-mark-as-unread-forward
10857   'gnus-summary-tick-article-forward)
10858 (make-obsolete 'gnus-summary-mark-as-unread-forward
10859                'gnus-summary-tick-article-forward "Emacs 20.4")
10860 (defun gnus-summary-tick-article-forward (n)
10861   "Tick N articles forwards.
10862 If N is negative, tick backwards instead.
10863 The difference between N and the number of articles ticked is returned."
10864   (interactive "p")
10865   (gnus-summary-mark-forward n gnus-ticked-mark))
10866
10867 (defalias 'gnus-summary-mark-as-unread-backward
10868   'gnus-summary-tick-article-backward)
10869 (make-obsolete 'gnus-summary-mark-as-unread-backward
10870                'gnus-summary-tick-article-backward "Emacs 20.4")
10871 (defun gnus-summary-tick-article-backward (n)
10872   "Tick N articles backwards.
10873 The difference between N and the number of articles ticked is returned."
10874   (interactive "p")
10875   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10876
10877 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10878 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article "Emacs 20.4")
10879 (defun gnus-summary-tick-article (&optional article clear-mark)
10880   "Mark current article as unread.
10881 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10882 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10883   (interactive)
10884   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10885                                        gnus-ticked-mark)))
10886
10887 (defun gnus-summary-mark-as-read-forward (n)
10888   "Mark N articles as read forwards.
10889 If N is negative, mark backwards instead.
10890 The difference between N and the actual number of articles marked is
10891 returned."
10892   (interactive "p")
10893   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10894
10895 (defun gnus-summary-mark-as-read-backward (n)
10896   "Mark the N articles as read backwards.
10897 The difference between N and the actual number of articles marked is
10898 returned."
10899   (interactive "p")
10900   (gnus-summary-mark-forward
10901    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10902
10903 (defun gnus-summary-mark-as-read (&optional article mark)
10904   "Mark current article as read.
10905 ARTICLE specifies the article to be marked as read.
10906 MARK specifies a string to be inserted at the beginning of the line."
10907   (gnus-summary-mark-article article mark))
10908
10909 (defun gnus-summary-clear-mark-forward (n)
10910   "Clear marks from N articles forward.
10911 If N is negative, clear backward instead.
10912 The difference between N and the number of marks cleared is returned."
10913   (interactive "p")
10914   (gnus-summary-mark-forward n gnus-unread-mark))
10915
10916 (defun gnus-summary-clear-mark-backward (n)
10917   "Clear marks from N articles backward.
10918 The difference between N and the number of marks cleared is returned."
10919   (interactive "p")
10920   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10921
10922 (defun gnus-summary-mark-unread-as-read ()
10923   "Intended to be used by `gnus-mark-article-hook'."
10924   (when (memq gnus-current-article gnus-newsgroup-unreads)
10925     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10926
10927 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10928   "Intended to be used by `gnus-mark-article-hook'."
10929   (let ((mark (gnus-summary-article-mark)))
10930     (when (or (gnus-unread-mark-p mark)
10931               (gnus-read-mark-p mark))
10932       (gnus-summary-mark-article gnus-current-article
10933                                  (or new-mark gnus-read-mark)))))
10934
10935 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10936   "Intended to be used by `gnus-mark-article-hook'."
10937   (let ((mark (gnus-summary-article-mark)))
10938     (when (or (gnus-unread-mark-p mark)
10939               (gnus-read-mark-p mark))
10940       (gnus-summary-mark-article (gnus-summary-article-number)
10941                                  (or new-mark gnus-read-mark)))))
10942
10943 (defun gnus-summary-mark-unread-as-ticked ()
10944   "Intended to be used by `gnus-mark-article-hook'."
10945   (when (memq gnus-current-article gnus-newsgroup-unreads)
10946     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10947
10948 (defun gnus-summary-mark-region-as-read (point mark all)
10949   "Mark all unread articles between point and mark as read.
10950 If given a prefix, mark all articles between point and mark as read,
10951 even ticked and dormant ones."
10952   (interactive "r\nP")
10953   (save-excursion
10954     (let (article)
10955       (goto-char point)
10956       (beginning-of-line)
10957       (while (and
10958               (< (point) mark)
10959               (progn
10960                 (when (or all
10961                           (memq (setq article (gnus-summary-article-number))
10962                                 gnus-newsgroup-unreads))
10963                   (gnus-summary-mark-article article gnus-del-mark))
10964                 t)
10965               (gnus-summary-find-next))))))
10966
10967 (defun gnus-summary-mark-below (score mark)
10968   "Mark articles with score less than SCORE with MARK."
10969   (interactive "P\ncMark: ")
10970   (setq score (if score
10971                   (prefix-numeric-value score)
10972                 (or gnus-summary-default-score 0)))
10973   (with-current-buffer gnus-summary-buffer
10974     (goto-char (point-min))
10975     (while
10976         (progn
10977           (and (< (gnus-summary-article-score) score)
10978                (gnus-summary-mark-article nil mark))
10979           (gnus-summary-find-next)))))
10980
10981 (defun gnus-summary-kill-below (&optional score)
10982   "Mark articles with score below SCORE as read."
10983   (interactive "P")
10984   (gnus-summary-mark-below score gnus-killed-mark))
10985
10986 (defun gnus-summary-clear-above (&optional score)
10987   "Clear all marks from articles with score above SCORE."
10988   (interactive "P")
10989   (gnus-summary-mark-above score gnus-unread-mark))
10990
10991 (defun gnus-summary-tick-above (&optional score)
10992   "Tick all articles with score above SCORE."
10993   (interactive "P")
10994   (gnus-summary-mark-above score gnus-ticked-mark))
10995
10996 (defun gnus-summary-mark-above (score mark)
10997   "Mark articles with score over SCORE with MARK."
10998   (interactive "P\ncMark: ")
10999   (setq score (if score
11000                   (prefix-numeric-value score)
11001                 (or gnus-summary-default-score 0)))
11002   (with-current-buffer gnus-summary-buffer
11003     (goto-char (point-min))
11004     (while (and (progn
11005                   (when (> (gnus-summary-article-score) score)
11006                     (gnus-summary-mark-article nil mark))
11007                   t)
11008                 (gnus-summary-find-next)))))
11009
11010 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11011 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11012 (defun gnus-summary-limit-include-expunged (&optional no-error)
11013   "Display all the hidden articles that were expunged for low scores."
11014   (interactive)
11015   (let ((inhibit-read-only t))
11016     (let ((scored gnus-newsgroup-scored)
11017           headers h)
11018       (while scored
11019         (unless (gnus-summary-article-header (caar scored))
11020           (and (setq h (gnus-number-to-header (caar scored)))
11021                (< (cdar scored) gnus-summary-expunge-below)
11022                (push h headers)))
11023         (setq scored (cdr scored)))
11024       (if (not headers)
11025           (when (not no-error)
11026             (error "No expunged articles hidden"))
11027         (goto-char (point-min))
11028         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11029         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11030         (dolist (x headers)
11031           (push (mail-header-number x) gnus-newsgroup-limit))
11032         (gnus-summary-prepare-unthreaded (nreverse headers))
11033         (goto-char (point-min))
11034         (gnus-summary-position-point)
11035         t))))
11036
11037 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11038   "Mark all unread articles in this newsgroup as read.
11039 If prefix argument ALL is non-nil, ticked and dormant articles will
11040 also be marked as read.
11041 If QUIETLY is non-nil, no questions will be asked.
11042
11043 If TO-HERE is non-nil, it should be a point in the buffer.  All
11044 articles before (after, if REVERSE is set) this point will be marked
11045 as read.
11046
11047 Note that this function will only catch up the unread article
11048 in the current summary buffer limitation.
11049
11050 The number of articles marked as read is returned."
11051   (interactive "P")
11052   (prog1
11053       (save-excursion
11054         (when (or quietly
11055                   (not gnus-interactive-catchup) ;Without confirmation?
11056                   gnus-expert-user
11057                   (gnus-y-or-n-p
11058                    (if all
11059                        "Mark absolutely all articles as read? "
11060                      "Mark all unread articles as read? ")))
11061           (if (and not-mark
11062                    (not gnus-newsgroup-adaptive)
11063                    (not gnus-newsgroup-auto-expire)
11064                    (not gnus-suppress-duplicates)
11065                    (or (not gnus-use-cache)
11066                        (eq gnus-use-cache 'passive)))
11067               (progn
11068                 (when all
11069                   (setq gnus-newsgroup-marked nil
11070                         gnus-newsgroup-spam-marked nil
11071                         gnus-newsgroup-dormant nil))
11072                 (setq gnus-newsgroup-unreads
11073                       (gnus-sorted-nunion
11074                        (gnus-sorted-intersection gnus-newsgroup-unreads
11075                                                  gnus-newsgroup-downloadable)
11076                        (gnus-sorted-difference gnus-newsgroup-unfetched
11077                                                gnus-newsgroup-cached))))
11078             ;; We actually mark all articles as canceled, which we
11079             ;; have to do when using auto-expiry or adaptive scoring.
11080             (gnus-summary-show-all-threads)
11081             (if (and to-here reverse)
11082                 (progn
11083                   (goto-char to-here)
11084                   (gnus-summary-mark-current-read-and-unread-as-read
11085                    gnus-catchup-mark)
11086                   (while (gnus-summary-find-next (not all))
11087                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11088               (when (gnus-summary-first-subject (not all))
11089                 (while (and
11090                         (if to-here (< (point) to-here) t)
11091                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11092                         (gnus-summary-find-next (not all))))))
11093             (gnus-set-mode-line 'summary))
11094           t))
11095     (gnus-summary-position-point)))
11096
11097 (defun gnus-summary-catchup-to-here (&optional all)
11098   "Mark all unticked articles before the current one as read.
11099 If ALL is non-nil, also mark ticked and dormant articles as read."
11100   (interactive "P")
11101   (save-excursion
11102     (gnus-save-hidden-threads
11103       (let ((beg (point)))
11104         ;; We check that there are unread articles.
11105         (when (or all (gnus-summary-find-prev))
11106           (gnus-summary-catchup all t beg)))))
11107   (gnus-summary-position-point))
11108
11109 (defun gnus-summary-catchup-from-here (&optional all)
11110   "Mark all unticked articles after (and including) the current one as read.
11111 If ALL is non-nil, also mark ticked and dormant articles as read."
11112   (interactive "P")
11113   (save-excursion
11114     (gnus-save-hidden-threads
11115       (let ((beg (point)))
11116         ;; We check that there are unread articles.
11117         (when (or all (gnus-summary-find-next))
11118           (gnus-summary-catchup all t beg nil t)))))
11119   (gnus-summary-position-point))
11120
11121 (defun gnus-summary-catchup-all (&optional quietly)
11122   "Mark all articles in this newsgroup as read.
11123 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11124 instead, which marks only unread articles as read."
11125   (interactive "P")
11126   (gnus-summary-catchup t quietly))
11127
11128 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11129   "Mark all unread articles in this group as read, then exit.
11130 If prefix argument ALL is non-nil, all articles are marked as read.
11131 If QUIETLY is non-nil, no questions will be asked."
11132   (interactive "P")
11133   (when (gnus-summary-catchup all quietly nil 'fast)
11134     ;; Select next newsgroup or exit.
11135     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11136              (eq gnus-auto-select-next 'quietly))
11137         (gnus-summary-next-group nil)
11138       (gnus-summary-exit))))
11139
11140 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11141   "Mark all articles in this newsgroup as read, and then exit.
11142 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11143 instead, which marks only unread articles as read."
11144   (interactive "P")
11145   (gnus-summary-catchup-and-exit t quietly))
11146
11147 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11148   "Mark all articles in this group as read and select the next group.
11149 If given a prefix, mark all articles, unread as well as ticked, as
11150 read."
11151   (interactive "P")
11152   (save-excursion
11153     (gnus-summary-catchup all))
11154   (gnus-summary-next-group))
11155
11156 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11157   "Mark all articles in this group as read and select the previous group.
11158 If given a prefix, mark all articles, unread as well as ticked, as
11159 read."
11160   (interactive "P")
11161   (save-excursion
11162     (gnus-summary-catchup all))
11163   (gnus-summary-next-group nil nil t))
11164
11165 ;;;
11166 ;;; with article
11167 ;;;
11168
11169 (defmacro gnus-with-article (article &rest forms)
11170   "Select ARTICLE and perform FORMS in the original article buffer.
11171 Then replace the article with the result."
11172   `(progn
11173      ;; We don't want the article to be marked as read.
11174      (let (gnus-mark-article-hook)
11175        (gnus-summary-select-article t t nil ,article))
11176      (set-buffer gnus-original-article-buffer)
11177      ,@forms
11178      (if (not (gnus-check-backend-function
11179                'request-replace-article (car gnus-article-current)))
11180          (gnus-message 5 "Read-only group; not replacing")
11181        (unless (gnus-request-replace-article
11182                 ,article (car gnus-article-current)
11183                 (current-buffer) t)
11184          (error "Couldn't replace article")))
11185      ;; The cache and backlog have to be flushed somewhat.
11186      (when gnus-keep-backlog
11187        (gnus-backlog-remove-article
11188         (car gnus-article-current) (cdr gnus-article-current)))
11189      (when gnus-use-cache
11190        (gnus-cache-update-article
11191         (car gnus-article-current) (cdr gnus-article-current)))))
11192
11193 (put 'gnus-with-article 'lisp-indent-function 1)
11194 (put 'gnus-with-article 'edebug-form-spec '(form body))
11195
11196 ;; Thread-based commands.
11197
11198 (defun gnus-summary-articles-in-thread (&optional article)
11199   "Return a list of all articles in the current thread.
11200 If ARTICLE is non-nil, return all articles in the thread that starts
11201 with that article."
11202   (let* ((article (or article (gnus-summary-article-number)))
11203          (data (gnus-data-find-list article))
11204          (top-level (gnus-data-level (car data)))
11205          (top-subject
11206           (cond ((null gnus-thread-operation-ignore-subject)
11207                  (gnus-simplify-subject-re
11208                   (mail-header-subject (gnus-data-header (car data)))))
11209                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11210                  (gnus-simplify-subject-fuzzy
11211                   (mail-header-subject (gnus-data-header (car data)))))
11212                 (t nil)))
11213          (end-point (save-excursion
11214                       (if (gnus-summary-go-to-next-thread)
11215                           (point) (point-max))))
11216          articles)
11217     (while (and data
11218                 (< (gnus-data-pos (car data)) end-point))
11219       (when (or (not top-subject)
11220                 (string= top-subject
11221                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11222                              (gnus-simplify-subject-fuzzy
11223                               (mail-header-subject
11224                                (gnus-data-header (car data))))
11225                            (gnus-simplify-subject-re
11226                             (mail-header-subject
11227                              (gnus-data-header (car data)))))))
11228         (push (gnus-data-number (car data)) articles))
11229       (unless (and (setq data (cdr data))
11230                    (> (gnus-data-level (car data)) top-level))
11231         (setq data nil)))
11232     ;; Return the list of articles.
11233     (nreverse articles)))
11234
11235 (defun gnus-summary-rethread-current ()
11236   "Rethread the thread the current article is part of."
11237   (interactive)
11238   (let* ((gnus-show-threads t)
11239          (article (gnus-summary-article-number))
11240          (id (mail-header-id (gnus-summary-article-header)))
11241          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11242     (unless id
11243       (error "No article on the current line"))
11244     (gnus-rebuild-thread id)
11245     (gnus-summary-goto-subject article)))
11246
11247 (defun gnus-summary-reparent-thread ()
11248   "Make the current article child of the marked (or previous) article.
11249
11250 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11251 is non-nil or the Subject: of both articles are the same."
11252   (interactive)
11253   (unless (not (gnus-group-read-only-p))
11254     (error "The current newsgroup does not support article editing"))
11255   (unless (<= (length gnus-newsgroup-processable) 1)
11256     (error "No more than one article may be marked"))
11257   (let ((child (gnus-summary-article-number))
11258         ;; First grab the marked article, otherwise one line up.
11259         (parent (if (not (null gnus-newsgroup-processable))
11260                     (car gnus-newsgroup-processable)
11261                   (save-excursion
11262                     (if (eq (forward-line -1) 0)
11263                         (gnus-summary-article-number)
11264                       (error "Beginning of summary buffer"))))))
11265     (gnus-summary-reparent-children parent (list child))))
11266
11267 (defun gnus-summary-reparent-children (parent children)
11268   "Make PARENT the parent of CHILDREN.
11269 When called interactively, PARENT is the current article and CHILDREN
11270 are the process-marked articles."
11271   (interactive
11272    (list (gnus-summary-article-number)
11273          (gnus-summary-work-articles nil)))
11274   (dolist (child children)
11275     (save-window-excursion
11276       (let ((gnus-article-buffer " *reparent*"))
11277         (unless (not (eq parent child))
11278           (error "An article may not be self-referential"))
11279         (let ((message-id (mail-header-id
11280                            (gnus-summary-article-header parent))))
11281           (unless (and message-id (not (equal message-id "")))
11282             (error "No message-id in desired parent"))
11283           (gnus-with-article child
11284             (save-restriction
11285               (goto-char (point-min))
11286               (message-narrow-to-head)
11287               (if (re-search-forward "^References: " nil t)
11288                   (progn
11289                     (re-search-forward "^[^ \t]" nil t)
11290                     (forward-line -1)
11291                     (end-of-line)
11292                     (insert " " message-id))
11293                 (insert "References: " message-id "\n"))))
11294           (set-buffer gnus-summary-buffer)
11295           (gnus-summary-unmark-all-processable)
11296           (gnus-summary-update-article child)
11297           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11298             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11299           (gnus-summary-rethread-current)
11300           (gnus-message 3 "Article %d is now the child of article %d"
11301                         child parent))))))
11302
11303 (defun gnus-summary-toggle-threads (&optional arg)
11304   "Toggle showing conversation threads.
11305 If ARG is positive number, turn showing conversation threads on."
11306   (interactive "P")
11307   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11308     (setq gnus-show-threads
11309           (if (null arg) (not gnus-show-threads)
11310             (> (prefix-numeric-value arg) 0)))
11311     (gnus-summary-prepare)
11312     (gnus-summary-goto-subject current)
11313     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11314     (gnus-summary-position-point)))
11315
11316 (eval-and-compile
11317   (if (fboundp 'remove-overlays)
11318       (defalias 'gnus-remove-overlays 'remove-overlays)
11319     (defun gnus-remove-overlays (beg end name val)
11320       "Clear BEG and END of overlays whose property NAME has value VAL.
11321 For compatibility with XEmacs."
11322       (dolist (ov (gnus-overlays-in beg end))
11323         (when (eq (gnus-overlay-get ov name) val)
11324           (gnus-delete-overlay ov))))))
11325
11326 (defun gnus-summary-show-all-threads ()
11327   "Show all threads."
11328   (interactive)
11329   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11330   (gnus-summary-position-point))
11331
11332 (defun gnus-summary-show-thread ()
11333   "Show thread subtrees.
11334 Returns nil if no thread was there to be shown."
11335   (interactive)
11336   (let* ((orig (point))
11337          (end (point-at-eol))
11338          ;; Leave point at bol
11339          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11340          (eoi (when (eq (get-char-property end 'invisible) 'gnus-sum)
11341                 (if (fboundp 'next-single-char-property-change)
11342                     (or (next-single-char-property-change end 'invisible)
11343                         (point-max))
11344                   (while (progn
11345                            (end-of-line 2)
11346                            (and (not (eobp))
11347                                 (eq (get-char-property (point) 'invisible)
11348                                     'gnus-sum))))
11349                   (point)))))
11350     (when eoi
11351       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11352       (goto-char orig)
11353       (gnus-summary-position-point)
11354       eoi)))
11355
11356 (defun gnus-summary-maybe-hide-threads ()
11357   "If requested, hide the threads that should be hidden."
11358   (when (and gnus-show-threads
11359              gnus-thread-hide-subtree)
11360     (gnus-summary-hide-all-threads
11361      (if (or (consp gnus-thread-hide-subtree)
11362              (functionp gnus-thread-hide-subtree))
11363          (gnus-make-predicate gnus-thread-hide-subtree)
11364        nil))))
11365
11366 ;;; Hiding predicates.
11367
11368 (defun gnus-article-unread-p (header)
11369   (memq (mail-header-number header) gnus-newsgroup-unreads))
11370
11371 (defun gnus-article-unseen-p (header)
11372   (memq (mail-header-number header) gnus-newsgroup-unseen))
11373
11374 (defun gnus-map-articles (predicate articles)
11375   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11376   (apply 'gnus-or (mapcar predicate
11377                           (mapcar (lambda (number)
11378                                     (gnus-summary-article-header number))
11379                                   articles))))
11380
11381 (defun gnus-summary-hide-all-threads (&optional predicate)
11382   "Hide all thread subtrees.
11383 If PREDICATE is supplied, threads that satisfy this predicate
11384 will not be hidden."
11385   (interactive)
11386   (save-excursion
11387     (goto-char (point-min))
11388     (let ((end nil))
11389       (while (not end)
11390         (when (or (not predicate)
11391                   (gnus-map-articles
11392                    predicate (gnus-summary-article-children)))
11393             (gnus-summary-hide-thread))
11394         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11395   (gnus-summary-position-point))
11396
11397 (defun gnus-summary-hide-thread ()
11398   "Hide thread subtrees.
11399 If PREDICATE is supplied, threads that satisfy this predicate
11400 will not be hidden.
11401 Returns nil if no threads were there to be hidden."
11402   (interactive)
11403   (let ((start (point))
11404         (starteol (line-end-position))
11405         (article (gnus-summary-article-number)))
11406     (goto-char start)
11407     ;; Go forward until either the buffer ends or the subthread ends.
11408     (when (and (not (eobp))
11409                (or (zerop (gnus-summary-next-thread 1 t))
11410                    (goto-char (point-max))))
11411       (if (and (> (point) start)
11412                ;; FIXME: this should actually search for a non-invisible \n.
11413                (search-backward "\n" start t))
11414           (progn
11415             (when (> (point) starteol)
11416               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11417               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11418                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11419                 (gnus-overlay-put ol 'evaporate t)))
11420             (gnus-summary-goto-subject article))
11421         (goto-char start)
11422         nil))))
11423
11424 (defun gnus-summary-go-to-next-thread (&optional previous)
11425   "Go to the same level (or less) next thread.
11426 If PREVIOUS is non-nil, go to previous thread instead.
11427 Return the article number moved to, or nil if moving was impossible."
11428   (let ((level (gnus-summary-thread-level))
11429         (way (if previous -1 1))
11430         (beg (point)))
11431     (forward-line way)
11432     (while (and (not (eobp))
11433                 (< level (gnus-summary-thread-level)))
11434       (forward-line way))
11435     (if (eobp)
11436         (progn
11437           (goto-char beg)
11438           nil)
11439       (setq beg (point))
11440       (prog1
11441           (gnus-summary-article-number)
11442         (goto-char beg)))))
11443
11444 (defun gnus-summary-next-thread (n &optional silent)
11445   "Go to the same level next N'th thread.
11446 If N is negative, search backward instead.
11447 Returns the difference between N and the number of skips actually
11448 done.
11449
11450 If SILENT, don't output messages."
11451   (interactive "p")
11452   (let ((backward (< n 0))
11453         (n (abs n)))
11454     (while (and (> n 0)
11455                 (gnus-summary-go-to-next-thread backward))
11456       (decf n))
11457     (unless silent
11458       (gnus-summary-position-point))
11459     (when (and (not silent) (/= 0 n))
11460       (gnus-message 7 "No more threads"))
11461     n))
11462
11463 (defun gnus-summary-prev-thread (n)
11464   "Go to the same level previous N'th thread.
11465 Returns the difference between N and the number of skips actually
11466 done."
11467   (interactive "p")
11468   (gnus-summary-next-thread (- n)))
11469
11470 (defun gnus-summary-go-down-thread ()
11471   "Go down one level in the current thread."
11472   (let ((children (gnus-summary-article-children)))
11473     (when children
11474       (gnus-summary-goto-subject (car children)))))
11475
11476 (defun gnus-summary-go-up-thread ()
11477   "Go up one level in the current thread."
11478   (let ((parent (gnus-summary-article-parent)))
11479     (when parent
11480       (gnus-summary-goto-subject parent))))
11481
11482 (defun gnus-summary-down-thread (n)
11483   "Go down thread N steps.
11484 If N is negative, go up instead.
11485 Returns the difference between N and how many steps down that were
11486 taken."
11487   (interactive "p")
11488   (let ((up (< n 0))
11489         (n (abs n)))
11490     (while (and (> n 0)
11491                 (if up (gnus-summary-go-up-thread)
11492                   (gnus-summary-go-down-thread)))
11493       (setq n (1- n)))
11494     (gnus-summary-position-point)
11495     (when (/= 0 n)
11496       (gnus-message 7 "Can't go further"))
11497     n))
11498
11499 (defun gnus-summary-up-thread (n)
11500   "Go up thread N steps.
11501 If N is negative, go down instead.
11502 Returns the difference between N and how many steps down that were
11503 taken."
11504   (interactive "p")
11505   (gnus-summary-down-thread (- n)))
11506
11507 (defun gnus-summary-top-thread ()
11508   "Go to the top of the thread."
11509   (interactive)
11510   (while (gnus-summary-go-up-thread))
11511   (gnus-summary-article-number))
11512
11513 (defun gnus-summary-expire-thread ()
11514   "Mark articles under current thread as expired."
11515   (interactive)
11516   (gnus-summary-kill-thread 0))
11517
11518 (defun gnus-summary-kill-thread (&optional unmark)
11519   "Mark articles under current thread as read.
11520 If the prefix argument is positive, remove any kinds of marks.
11521 If the prefix argument is zero, mark thread as expired.
11522 If the prefix argument is negative, tick articles instead."
11523   (interactive "P")
11524   (when unmark
11525     (setq unmark (prefix-numeric-value unmark)))
11526   (let ((articles (gnus-summary-articles-in-thread))
11527         (hide (or (null unmark) (= unmark 0))))
11528     (save-excursion
11529       ;; Expand the thread.
11530       (gnus-summary-show-thread)
11531       ;; Mark all the articles.
11532       (while articles
11533         (gnus-summary-goto-subject (car articles))
11534         (cond ((null unmark)
11535                (gnus-summary-mark-article-as-read gnus-killed-mark))
11536               ((> unmark 0)
11537                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11538               ((= unmark 0)
11539                (gnus-summary-mark-article nil gnus-expirable-mark))
11540               (t
11541                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11542         (setq articles (cdr articles))))
11543     ;; Hide killed subtrees when hide is true.
11544     (and hide
11545          gnus-thread-hide-killed
11546          (gnus-summary-hide-thread))
11547     ;; If hide is t, go to next unread subject.
11548     (when hide
11549       ;; Go to next unread subject.
11550       (gnus-summary-next-subject 1 t)))
11551   (gnus-set-mode-line 'summary))
11552
11553 ;; Summary sorting commands
11554
11555 (defun gnus-summary-sort-by-number (&optional reverse)
11556   "Sort the summary buffer by article number.
11557 Argument REVERSE means reverse order."
11558   (interactive "P")
11559   (gnus-summary-sort 'number reverse))
11560
11561 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11562   "Sort the summary buffer by most recent article number.
11563 Argument REVERSE means reverse order."
11564   (interactive "P")
11565   (gnus-summary-sort 'most-recent-number reverse))
11566
11567 (defun gnus-summary-sort-by-random (&optional reverse)
11568   "Randomize the order in the summary buffer.
11569 Argument REVERSE means to randomize in reverse order."
11570   (interactive "P")
11571   (gnus-summary-sort 'random reverse))
11572
11573 (defun gnus-summary-sort-by-author (&optional reverse)
11574   "Sort the summary buffer by author name alphabetically.
11575 If `case-fold-search' is non-nil, case of letters is ignored.
11576 Argument REVERSE means reverse order."
11577   (interactive "P")
11578   (gnus-summary-sort 'author reverse))
11579
11580 (defun gnus-summary-sort-by-recipient (&optional reverse)
11581   "Sort the summary buffer by recipient name alphabetically.
11582 If `case-fold-search' is non-nil, case of letters is ignored.
11583 Argument REVERSE means reverse order."
11584   (interactive "P")
11585   (gnus-summary-sort 'recipient reverse))
11586
11587 (defun gnus-summary-sort-by-subject (&optional reverse)
11588   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11589 If `case-fold-search' is non-nil, case of letters is ignored.
11590 Argument REVERSE means reverse order."
11591   (interactive "P")
11592   (gnus-summary-sort 'subject reverse))
11593
11594 (defun gnus-summary-sort-by-date (&optional reverse)
11595   "Sort the summary buffer by date.
11596 Argument REVERSE means reverse order."
11597   (interactive "P")
11598   (gnus-summary-sort 'date reverse))
11599
11600 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11601   "Sort the summary buffer by most recent date.
11602 Argument REVERSE means reverse order."
11603   (interactive "P")
11604   (gnus-summary-sort 'most-recent-date reverse))
11605
11606 (defun gnus-summary-sort-by-score (&optional reverse)
11607   "Sort the summary buffer by score.
11608 Argument REVERSE means reverse order."
11609   (interactive "P")
11610   (gnus-summary-sort 'score reverse))
11611
11612 (defun gnus-summary-sort-by-lines (&optional reverse)
11613   "Sort the summary buffer by the number of lines.
11614 Argument REVERSE means reverse order."
11615   (interactive "P")
11616   (gnus-summary-sort 'lines reverse))
11617
11618 (defun gnus-summary-sort-by-chars (&optional reverse)
11619   "Sort the summary buffer by article length.
11620 Argument REVERSE means reverse order."
11621   (interactive "P")
11622   (gnus-summary-sort 'chars reverse))
11623
11624 (defun gnus-summary-sort-by-original (&optional reverse)
11625   "Sort the summary buffer using the default sorting method.
11626 Argument REVERSE means reverse order."
11627   (interactive "P")
11628   (let* ((inhibit-read-only t)
11629          (gnus-summary-prepare-hook nil))
11630     ;; We do the sorting by regenerating the threads.
11631     (gnus-summary-prepare)
11632     ;; Hide subthreads if needed.
11633     (gnus-summary-maybe-hide-threads)))
11634
11635 (defun gnus-summary-sort (predicate reverse)
11636   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11637   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11638          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11639          (gnus-thread-sort-functions
11640           (if (not reverse)
11641               thread
11642             `(lambda (t1 t2)
11643                (,thread t2 t1))))
11644          (gnus-sort-gathered-threads-function
11645           gnus-thread-sort-functions)
11646          (gnus-article-sort-functions
11647           (if (not reverse)
11648               article
11649             `(lambda (t1 t2)
11650                (,article t2 t1))))
11651          (inhibit-read-only t)
11652          (gnus-summary-prepare-hook nil))
11653     ;; We do the sorting by regenerating the threads.
11654     (gnus-summary-prepare)
11655     ;; Hide subthreads if needed.
11656     (gnus-summary-maybe-hide-threads)))
11657
11658 ;; Summary saving commands.
11659
11660 (defun gnus-summary-save-article (&optional n not-saved)
11661   "Save the current article using the default saver function.
11662 If N is a positive number, save the N next articles.
11663 If N is a negative number, save the N previous articles.
11664 If N is nil and any articles have been marked with the process mark,
11665 save those articles instead.
11666 The variable `gnus-default-article-saver' specifies the saver function.
11667
11668 If the optional second argument NOT-SAVED is non-nil, articles saved
11669 will not be marked as saved."
11670   (interactive "P")
11671   (require 'gnus-art)
11672   (let* ((articles (gnus-summary-work-articles n))
11673          (save-buffer (save-excursion
11674                         (nnheader-set-temp-buffer " *Gnus Save*")))
11675          (num (length articles))
11676          ;; Whether to save decoded articles or raw articles.
11677          (decode (when gnus-article-save-coding-system
11678                    (get gnus-default-article-saver :decode)))
11679          ;; When saving many articles in a single file, use the other
11680          ;; function to save articles other than the first one.
11681          (saver2 (get gnus-default-article-saver :function))
11682          (gnus-prompt-before-saving (if saver2
11683                                         t
11684                                       gnus-prompt-before-saving))
11685          (gnus-default-article-saver gnus-default-article-saver)
11686          header file)
11687     (dolist (article articles)
11688       (setq header (gnus-summary-article-header article))
11689       (if (not (vectorp header))
11690           ;; This is a pseudo-article.
11691           (if (assq 'name header)
11692               (gnus-copy-file (cdr (assq 'name header)))
11693             (gnus-message 1 "Article %d is unsaveable" article))
11694         ;; This is a real article.
11695         (save-window-excursion
11696           (gnus-summary-select-article decode decode nil article)
11697           (gnus-summary-goto-subject article))
11698         (with-current-buffer save-buffer
11699           (erase-buffer)
11700           (insert-buffer-substring (if decode
11701                                        gnus-article-buffer
11702                                      gnus-original-article-buffer)))
11703         (setq file (gnus-article-save save-buffer file num))
11704         (gnus-summary-remove-process-mark article)
11705         (unless not-saved
11706           (gnus-summary-set-saved-mark article)))
11707       (when saver2
11708         (setq gnus-default-article-saver saver2
11709               saver2 nil)))
11710     (gnus-kill-buffer save-buffer)
11711     (gnus-summary-position-point)
11712     (gnus-set-mode-line 'summary)
11713     n))
11714
11715 (defun gnus-summary-pipe-output (&optional n sym)
11716   "Pipe the current article to a subprocess.
11717 If N is a positive number, pipe the N next articles.
11718 If N is a negative number, pipe the N previous articles.
11719 If N is nil and any articles have been marked with the process mark,
11720 pipe those articles instead.
11721 The default command to which articles are piped is specified by the
11722 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11723 will be prompted for the command.
11724
11725 The properties `:decode' and `:headers' that are put to the function
11726 symbol `gnus-summary-save-in-pipe' control whether this function
11727 decodes articles and what headers to keep (see the doc string for the
11728 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11729 is neither omitted nor the symbol `r', force including all headers
11730 regardless of the `:headers' property.  If it is the symbol `r',
11731 articles that are not decoded and include all headers will be piped
11732 no matter what the properties `:decode' and `:headers' are."
11733   (interactive (gnus-interactive "P\ny"))
11734   (require 'gnus-art)
11735   (let* ((articles (gnus-summary-work-articles n))
11736          (result-buffer "*Shell Command Output*")
11737          (all-headers (not (memq sym '(nil r))))
11738          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11739          (raw (eq sym 'r))
11740          (headers (get 'gnus-summary-save-in-pipe :headers))
11741          command result)
11742     (unless (numberp (car articles))
11743       (error "No article to pipe"))
11744     (setq command (gnus-read-shell-command
11745                    (concat "Shell command on "
11746                            (if (cdr articles)
11747                                (format "these %d articles" (length articles))
11748                              "this article")
11749                            ": ")
11750                    gnus-summary-pipe-output-default-command))
11751     (when (string-equal command "")
11752       (error "A command is required"))
11753     (when all-headers
11754       (put 'gnus-summary-save-in-pipe :headers nil))
11755     (unwind-protect
11756         (while articles
11757           (gnus-summary-goto-subject (pop articles))
11758           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11759           (when (and (get-buffer result-buffer)
11760                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11761             (setq result (concat result (with-current-buffer result-buffer
11762                                           (buffer-string))))))
11763       (put 'gnus-summary-save-in-pipe :headers headers))
11764     (unless (zerop (length result))
11765       (if (with-current-buffer (get-buffer-create result-buffer)
11766             (erase-buffer)
11767             (insert result)
11768             (prog1
11769                 (and (= (count-lines (point-min) (point)) 1)
11770                      (progn
11771                        (end-of-line 0)
11772                        (<= (current-column)
11773                            (window-width (minibuffer-window)))))
11774               (goto-char (point-min))))
11775           (message "%s" (substring result 0 -1))
11776         (message nil)
11777         (gnus-configure-windows 'pipe)))))
11778
11779 (defun gnus-summary-save-article-mail (&optional arg)
11780   "Append the current article to a Unix mail box file.
11781 If N is a positive number, save the N next articles.
11782 If N is a negative number, save the N previous articles.
11783 If N is nil and any articles have been marked with the process mark,
11784 save those articles instead."
11785   (interactive "P")
11786   (require 'gnus-art)
11787   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11788     (gnus-summary-save-article arg)))
11789
11790 (defun gnus-summary-save-article-rmail (&optional arg)
11791   "Append the current article to an rmail file.
11792 If N is a positive number, save the N next articles.
11793 If N is a negative number, save the N previous articles.
11794 If N is nil and any articles have been marked with the process mark,
11795 save those articles instead."
11796   (interactive "P")
11797   (require 'gnus-art)
11798   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11799     (gnus-summary-save-article arg)))
11800
11801 (defun gnus-summary-save-article-file (&optional arg)
11802   "Append the current article to a file.
11803 If N is a positive number, save the N next articles.
11804 If N is a negative number, save the N previous articles.
11805 If N is nil and any articles have been marked with the process mark,
11806 save those articles instead."
11807   (interactive "P")
11808   (require 'gnus-art)
11809   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11810     (gnus-summary-save-article arg)))
11811
11812 (defun gnus-summary-write-article-file (&optional arg)
11813   "Write the current article to a file, deleting the previous file.
11814 If N is a positive number, save the N next articles.
11815 If N is a negative number, save the N previous articles.
11816 If N is nil and any articles have been marked with the process mark,
11817 save those articles instead."
11818   (interactive "P")
11819   (require 'gnus-art)
11820   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11821     (gnus-summary-save-article arg)))
11822
11823 (defun gnus-summary-save-article-body-file (&optional arg)
11824   "Append the current article body to a file.
11825 If N is a positive number, save the N next articles.
11826 If N is a negative number, save the N previous articles.
11827 If N is nil and any articles have been marked with the process mark,
11828 save those articles instead."
11829   (interactive "P")
11830   (require 'gnus-art)
11831   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11832     (gnus-summary-save-article arg)))
11833
11834 (defun gnus-summary-write-article-body-file (&optional arg)
11835   "Write the current article body to a file, deleting the previous file.
11836 If N is a positive number, save the N next articles.
11837 If N is a negative number, save the N previous articles.
11838 If N is nil and any articles have been marked with the process mark,
11839 save those articles instead."
11840   (interactive "P")
11841   (require 'gnus-art)
11842   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11843     (gnus-summary-save-article arg)))
11844
11845 (defun gnus-summary-muttprint (&optional arg)
11846   "Print the current article using Muttprint.
11847 If N is a positive number, save the N next articles.
11848 If N is a negative number, save the N previous articles.
11849 If N is nil and any articles have been marked with the process mark,
11850 save those articles instead."
11851   (interactive "P")
11852   (require 'gnus-art)
11853   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11854     (gnus-summary-save-article arg t)))
11855
11856 (defun gnus-summary-pipe-message (program)
11857   "Pipe the current article through PROGRAM."
11858   (interactive "sProgram: ")
11859   (gnus-summary-select-article)
11860   (let ((mail-header-separator ""))
11861     (gnus-eval-in-buffer-window gnus-article-buffer
11862       (save-restriction
11863         (widen)
11864         (let ((start (window-start))
11865               (inhibit-read-only t))
11866           (message-pipe-buffer-body program)
11867           (set-window-start (get-buffer-window (current-buffer)) start))))))
11868
11869 (defun gnus-get-split-value (methods)
11870   "Return a value based on the split METHODS."
11871   (let (split-name method result match)
11872     (when methods
11873       (with-current-buffer gnus-original-article-buffer
11874         (save-restriction
11875           (nnheader-narrow-to-headers)
11876           (while (and methods (not split-name))
11877             (goto-char (point-min))
11878             (setq method (pop methods))
11879             (setq match (car method))
11880             (when (cond
11881                    ((stringp match)
11882                     ;; Regular expression.
11883                     (ignore-errors
11884                       (re-search-forward match nil t)))
11885                    ((functionp match)
11886                     ;; Function.
11887                     (save-restriction
11888                       (widen)
11889                       (setq result (funcall match gnus-newsgroup-name))))
11890                    ((consp match)
11891                     ;; Form.
11892                     (save-restriction
11893                       (widen)
11894                       (setq result (eval match)))))
11895               (setq split-name (cdr method))
11896               (cond ((stringp result)
11897                      (push (expand-file-name
11898                             result gnus-article-save-directory)
11899                            split-name))
11900                     ((consp result)
11901                      (setq split-name (append result split-name)))))))))
11902     (nreverse split-name)))
11903
11904 (defun gnus-valid-move-group-p (group)
11905   (and (boundp group)
11906        (symbol-name group)
11907        (symbol-value group)
11908        (gnus-get-function (gnus-find-method-for-group
11909                            (symbol-name group)) 'request-accept-article t)))
11910
11911 (defun gnus-read-move-group-name (prompt default articles prefix)
11912   "Read a group name."
11913   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11914          (minibuffer-confirm-incomplete nil) ; XEmacs
11915          (prom
11916           (format "%s %s to"
11917                   prompt
11918                   (if (> (length articles) 1)
11919                       (format "these %d articles" (length articles))
11920                     "this article")))
11921          (to-newsgroup
11922           (let (active group)
11923             (when (or (null split-name) (= 1 (length split-name)))
11924               (setq active (gnus-make-hashtable (length gnus-active-hashtb)))
11925               (mapatoms (lambda (symbol)
11926                           (setq group (symbol-name symbol))
11927                           (when (string-match "[^\000-\177]" group)
11928                             (setq group (gnus-group-decoded-name group)))
11929                           (set (intern group active) group))
11930                         gnus-active-hashtb))
11931             (cond
11932              ((null split-name)
11933               (gnus-completing-read-with-default
11934                default prom active 'gnus-valid-move-group-p nil prefix
11935                'gnus-group-history))
11936              ((= 1 (length split-name))
11937               (gnus-completing-read-with-default
11938                (car split-name) prom active 'gnus-valid-move-group-p nil nil
11939                'gnus-group-history))
11940              (t
11941               (gnus-completing-read-with-default
11942                nil prom (mapcar 'list (nreverse split-name)) nil nil nil
11943                'gnus-group-history)))))
11944          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11945          encoded)
11946     (when to-newsgroup
11947       (if (or (string= to-newsgroup "")
11948               (string= to-newsgroup prefix))
11949           (setq to-newsgroup default))
11950       (unless to-newsgroup
11951         (error "No group name entered"))
11952       (setq encoded (mm-encode-coding-string
11953                      to-newsgroup
11954                      (gnus-group-name-charset to-method to-newsgroup)))
11955       (or (gnus-active encoded)
11956           (gnus-activate-group encoded nil nil to-method)
11957           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11958                                      to-newsgroup))
11959               (or (and (gnus-request-create-group encoded to-method)
11960                        (gnus-activate-group encoded nil nil to-method)
11961                        (gnus-subscribe-group encoded))
11962                   (error "Couldn't create group %s" to-newsgroup)))
11963           (error "No such group: %s" to-newsgroup))
11964       encoded)))
11965
11966 (defvar gnus-summary-save-parts-counter)
11967 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11968
11969 (defun gnus-summary-save-parts (type dir n &optional reverse)
11970   "Save parts matching TYPE to DIR.
11971 If REVERSE, save parts that do not match TYPE."
11972   (interactive
11973    (list (read-string "Save parts of type: "
11974                       (or (car gnus-summary-save-parts-type-history)
11975                           gnus-summary-save-parts-default-mime)
11976                       'gnus-summary-save-parts-type-history)
11977          (setq gnus-summary-save-parts-last-directory
11978                (read-file-name "Save to directory: "
11979                                gnus-summary-save-parts-last-directory
11980                                nil t))
11981          current-prefix-arg))
11982   (gnus-summary-iterate n
11983     (let ((gnus-display-mime-function nil)
11984           gnus-article-prepare-hook
11985           gnus-article-decode-hook
11986           gnus-display-mime-function
11987           gnus-break-pages
11988           (gnus-inhibit-treatment t))
11989       (gnus-summary-select-article))
11990     (with-current-buffer gnus-article-buffer
11991       (let ((handles (or gnus-article-mime-handles
11992                          (mm-dissect-buffer nil gnus-article-loose-mime)
11993                          (and gnus-article-emulate-mime
11994                               (mm-uu-dissect))))
11995             (gnus-summary-save-parts-counter 1))
11996         (when handles
11997           (gnus-summary-save-parts-1 type dir handles reverse)
11998           (unless gnus-article-mime-handles ;; Don't destroy this case.
11999             (mm-destroy-parts handles)))))))
12000
12001 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12002   (if (stringp (car handle))
12003       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12004               (cdr handle))
12005     (when (if reverse
12006               (not (string-match type (mm-handle-media-type handle)))
12007             (string-match type (mm-handle-media-type handle)))
12008       (let ((file (expand-file-name
12009                    (gnus-map-function
12010                     mm-file-name-rewrite-functions
12011                     (file-name-nondirectory
12012                      (or
12013                       (mail-content-type-get
12014                        (mm-handle-disposition handle) 'filename)
12015                       (mail-content-type-get
12016                        (mm-handle-type handle) 'name)
12017                       (format "%s.%d.%d" gnus-newsgroup-name
12018                               (cdr gnus-article-current)
12019                               gnus-summary-save-parts-counter))))
12020                    dir)))
12021         (incf gnus-summary-save-parts-counter)
12022         (unless (file-exists-p file)
12023           (mm-save-part-to-file handle file))))))
12024
12025 ;; Summary extract commands
12026
12027 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12028   (let ((inhibit-read-only t)
12029         (article (gnus-summary-article-number))
12030         after-article b e)
12031     (unless (gnus-summary-goto-subject article)
12032       (error "No such article: %d" article))
12033     (gnus-summary-position-point)
12034     ;; If all commands are to be bunched up on one line, we collect
12035     ;; them here.
12036     (unless gnus-view-pseudos-separately
12037       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12038             files action)
12039         (while ps
12040           (setq action (cdr (assq 'action (car ps))))
12041           (setq files (list (cdr (assq 'name (car ps)))))
12042           (while (and ps (cdr ps)
12043                       (string= (or action "1")
12044                                (or (cdr (assq 'action (cadr ps))) "2")))
12045             (push (cdr (assq 'name (cadr ps))) files)
12046             (setcdr ps (cddr ps)))
12047           (when files
12048             (when (not (string-match "%s" action))
12049               (push " " files))
12050             (push " " files)
12051             (when (assq 'execute (car ps))
12052               (setcdr (assq 'execute (car ps))
12053                       (funcall (if (string-match "%s" action)
12054                                    'format 'concat)
12055                                action
12056                                (mapconcat
12057                                 (lambda (f)
12058                                   (if (equal f " ")
12059                                       f
12060                                     (shell-quote-argument f)))
12061                                 files " ")))))
12062           (setq ps (cdr ps)))))
12063     (if (and gnus-view-pseudos (not not-view))
12064         (while pslist
12065           (when (assq 'execute (car pslist))
12066             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12067                                   (eq gnus-view-pseudos 'not-confirm)))
12068           (setq pslist (cdr pslist)))
12069       (save-excursion
12070         (while pslist
12071           (setq after-article (or (cdr (assq 'article (car pslist)))
12072                                   (gnus-summary-article-number)))
12073           (gnus-summary-goto-subject after-article)
12074           (forward-line 1)
12075           (setq b (point))
12076           (insert "    " (file-name-nondirectory
12077                           (cdr (assq 'name (car pslist))))
12078                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12079           (setq e (point))
12080           (forward-line -1)             ; back to `b'
12081           (gnus-add-text-properties
12082            b (1- e) (list 'gnus-number gnus-reffed-article-number
12083                           gnus-mouse-face-prop gnus-mouse-face))
12084           (gnus-data-enter
12085            after-article gnus-reffed-article-number
12086            gnus-unread-mark b (car pslist) 0 (- e b))
12087           (setq gnus-newsgroup-unreads
12088                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12089                                          gnus-reffed-article-number))
12090           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12091           (setq pslist (cdr pslist)))))))
12092
12093 (defun gnus-pseudos< (p1 p2)
12094   (let ((c1 (cdr (assq 'action p1)))
12095         (c2 (cdr (assq 'action p2))))
12096     (and c1 c2 (string< c1 c2))))
12097
12098 (defun gnus-request-pseudo-article (props)
12099   (cond ((assq 'execute props)
12100          (gnus-execute-command (cdr (assq 'execute props)))))
12101   (let ((gnus-current-article (gnus-summary-article-number)))
12102     (gnus-run-hooks 'gnus-mark-article-hook)))
12103
12104 (defun gnus-execute-command (command &optional automatic)
12105   (save-excursion
12106     (gnus-article-setup-buffer)
12107     (set-buffer gnus-article-buffer)
12108     (setq buffer-read-only nil)
12109     (let ((command (if automatic command
12110                      (read-string "Command: " (cons command 0)))))
12111       (erase-buffer)
12112       (insert "$ " command "\n\n")
12113       (if gnus-view-pseudo-asynchronously
12114           (start-process "gnus-execute" (current-buffer) shell-file-name
12115                          shell-command-switch command)
12116         (call-process shell-file-name nil t nil
12117                       shell-command-switch command)))))
12118
12119 ;; Summary kill commands.
12120
12121 (defun gnus-summary-edit-global-kill (article)
12122   "Edit the \"global\" kill file."
12123   (interactive (list (gnus-summary-article-number)))
12124   (gnus-group-edit-global-kill article))
12125
12126 (defun gnus-summary-edit-local-kill ()
12127   "Edit a local kill file applied to the current newsgroup."
12128   (interactive)
12129   (setq gnus-current-headers (gnus-summary-article-header))
12130   (gnus-group-edit-local-kill
12131    (gnus-summary-article-number) gnus-newsgroup-name))
12132
12133 ;;; Header reading.
12134
12135 (defun gnus-read-header (id &optional header)
12136   "Read the headers of article ID and enter them into the Gnus system."
12137   (let ((group gnus-newsgroup-name)
12138         (gnus-override-method
12139          (or
12140           gnus-override-method
12141           (and (gnus-news-group-p gnus-newsgroup-name)
12142                (car (gnus-refer-article-methods)))))
12143         where)
12144     ;; First we check to see whether the header in question is already
12145     ;; fetched.
12146     (if (stringp id)
12147         ;; This is a Message-ID.
12148         (setq header (or header (gnus-id-to-header id)))
12149       ;; This is an article number.
12150       (setq header (or header (gnus-summary-article-header id))))
12151     (if (and header
12152              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12153         ;; We have found the header.
12154         header
12155       ;; We have to really fetch the header to this article.
12156       (with-current-buffer nntp-server-buffer
12157         (when (setq where (gnus-request-head id group))
12158           (nnheader-fold-continuation-lines)
12159           (goto-char (point-max))
12160           (insert ".\n")
12161           (goto-char (point-min))
12162           (insert "211 ")
12163           (princ (cond
12164                   ((numberp id) id)
12165                   ((cdr where) (cdr where))
12166                   (header (mail-header-number header))
12167                   (t gnus-reffed-article-number))
12168                  (current-buffer))
12169           (insert " Article retrieved.\n"))
12170         (if (or (not where)
12171                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12172             ()                          ; Malformed head.
12173           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12174             (when (and (stringp id)
12175                        (or
12176                         (not (string= (gnus-group-real-name group)
12177                                       (car where)))
12178                         (not (gnus-server-equal gnus-override-method
12179                                                 (gnus-group-method group)))))
12180               ;; If we fetched by Message-ID and the article came from
12181               ;; a different group (or server), we fudge some bogus
12182               ;; article numbers for this article.
12183               (mail-header-set-number header gnus-reffed-article-number))
12184             (with-current-buffer gnus-summary-buffer
12185               (decf gnus-reffed-article-number)
12186               (gnus-remove-header (mail-header-number header))
12187               (push header gnus-newsgroup-headers)
12188               (setq gnus-current-headers header)
12189               (push (mail-header-number header) gnus-newsgroup-limit)))
12190           header)))))
12191
12192 (defun gnus-remove-header (number)
12193   "Remove header NUMBER from `gnus-newsgroup-headers'."
12194   (if (and gnus-newsgroup-headers
12195            (= number (mail-header-number (car gnus-newsgroup-headers))))
12196       (pop gnus-newsgroup-headers)
12197     (let ((headers gnus-newsgroup-headers))
12198       (while (and (cdr headers)
12199                   (not (= number (mail-header-number (cadr headers)))))
12200         (pop headers))
12201       (when (cdr headers)
12202         (setcdr headers (cddr headers))))))
12203
12204 ;;;
12205 ;;; summary highlights
12206 ;;;
12207
12208 (defun gnus-highlight-selected-summary ()
12209   "Highlight selected article in summary buffer."
12210   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12211   (when gnus-summary-selected-face
12212     (save-excursion
12213       (let* ((beg (point-at-bol))
12214              (end (point-at-eol))
12215              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12216              (from (if (get-text-property beg gnus-mouse-face-prop)
12217                        beg
12218                      (or (next-single-property-change
12219                           beg gnus-mouse-face-prop nil end)
12220                          beg)))
12221              (to
12222               (if (= from end)
12223                   (- from 2)
12224                 (or (next-single-property-change
12225                      from gnus-mouse-face-prop nil end)
12226                     end))))
12227         ;; If no mouse-face prop on line we will have to = from = end,
12228         ;; so we highlight the entire line instead.
12229         (when (= (+ to 2) from)
12230           (setq from beg)
12231           (setq to end))
12232         (if gnus-newsgroup-selected-overlay
12233             ;; Move old overlay.
12234             (gnus-move-overlay
12235              gnus-newsgroup-selected-overlay from to (current-buffer))
12236           ;; Create new overlay.
12237           (gnus-overlay-put
12238            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12239            'face gnus-summary-selected-face))))))
12240
12241 (defvar gnus-summary-highlight-line-cached nil)
12242 (defvar gnus-summary-highlight-line-trigger nil)
12243
12244 (defun gnus-summary-highlight-line-0 ()
12245   (if (and (eq gnus-summary-highlight-line-trigger
12246                gnus-summary-highlight)
12247            gnus-summary-highlight-line-cached)
12248       gnus-summary-highlight-line-cached
12249     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12250           gnus-summary-highlight-line-cached
12251           (let* ((cond (list 'cond))
12252                  (c cond)
12253                  (list gnus-summary-highlight))
12254             (while list
12255               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12256                               nil))
12257               (setq c (cdr c)
12258                     list (cdr list)))
12259             (gnus-byte-compile (list 'lambda nil cond))))))
12260
12261 (defun gnus-summary-highlight-line ()
12262   "Highlight current line according to `gnus-summary-highlight'."
12263   (let* ((beg (point-at-bol))
12264          (article (or (gnus-summary-article-number) gnus-current-article))
12265          (score (or (cdr (assq article
12266                                gnus-newsgroup-scored))
12267                     gnus-summary-default-score 0))
12268          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12269          (inhibit-read-only t)
12270          (default gnus-summary-default-score)
12271          (default-high gnus-summary-default-high-score)
12272          (default-low gnus-summary-default-low-score)
12273          (uncached (and gnus-summary-use-undownloaded-faces
12274                         (memq article gnus-newsgroup-undownloaded)
12275                         (not (memq article gnus-newsgroup-cached)))))
12276     (let ((face (funcall (gnus-summary-highlight-line-0))))
12277       (unless (eq face (get-text-property beg 'face))
12278         (gnus-put-text-property-excluding-characters-with-faces
12279          beg (point-at-eol) 'face
12280          (setq face (if (boundp face) (symbol-value face) face)))
12281         (when gnus-summary-highlight-line-function
12282           (funcall gnus-summary-highlight-line-function article face))))))
12283
12284 (defun gnus-update-read-articles (group unread &optional compute)
12285   "Update the list of read articles in GROUP.
12286 UNREAD is a sorted list."
12287   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12288         (info (gnus-get-info group))
12289         (prev 1)
12290         read)
12291     (if (or (not info) (not active))
12292         ;; There is no info on this group if it was, in fact,
12293         ;; killed.  Gnus stores no information on killed groups, so
12294         ;; there's nothing to be done.
12295         ;; One could store the information somewhere temporarily,
12296         ;; perhaps...  Hmmm...
12297         ()
12298       ;; Remove any negative articles numbers.
12299       (while (and unread (< (car unread) 0))
12300         (setq unread (cdr unread)))
12301       ;; Remove any expired article numbers
12302       (while (and unread (< (car unread) (car active)))
12303         (setq unread (cdr unread)))
12304       ;; Compute the ranges of read articles by looking at the list of
12305       ;; unread articles.
12306       (while unread
12307         (when (/= (car unread) prev)
12308           (push (if (= prev (1- (car unread))) prev
12309                   (cons prev (1- (car unread))))
12310                 read))
12311         (setq prev (1+ (car unread)))
12312         (setq unread (cdr unread)))
12313       (when (<= prev (cdr active))
12314         (push (cons prev (cdr active)) read))
12315       (setq read (if (> (length read) 1) (nreverse read) read))
12316       (if compute
12317           read
12318         (save-excursion
12319           (let (setmarkundo)
12320             ;; Propagate the read marks to the backend.
12321             (when (and gnus-propagate-marks
12322                        (gnus-check-backend-function 'request-set-mark group))
12323               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12324                     (add (gnus-remove-from-range read (gnus-info-read info))))
12325                 (when (or add del)
12326                   (unless (gnus-check-group group)
12327                     (error "Can't open server for %s" group))
12328                   (gnus-request-set-mark
12329                    group (delq nil (list (if add (list add 'add '(read)))
12330                                          (if del (list del 'del '(read))))))
12331                   (setq setmarkundo
12332                         `(gnus-request-set-mark
12333                           ,group
12334                           ',(delq nil (list
12335                                        (if del (list del 'add '(read)))
12336                                        (if add (list add 'del '(read))))))))))
12337             (set-buffer gnus-group-buffer)
12338             (gnus-undo-register
12339               `(progn
12340                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12341                  (gnus-info-set-read ',info ',(gnus-info-read info))
12342                  (gnus-get-unread-articles-in-group ',info
12343                                                     (gnus-active ,group))
12344                  (gnus-group-update-group ,group t)
12345                  ,setmarkundo))))
12346         ;; Enter this list into the group info.
12347         (gnus-info-set-read info read)
12348         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12349         (gnus-get-unread-articles-in-group info (gnus-active group))
12350         t))))
12351
12352 (defun gnus-offer-save-summaries ()
12353   "Offer to save all active summary buffers."
12354   (let (buffers)
12355     ;; Go through all buffers and find all summaries.
12356     (dolist (buffer (buffer-list))
12357       (when (and (setq buffer (buffer-name buffer))
12358                  (string-match "Summary" buffer)
12359                  (with-current-buffer buffer
12360                    ;; We check that this is, indeed, a summary buffer.
12361                    (and (eq major-mode 'gnus-summary-mode)
12362                         ;; Also make sure this isn't bogus.
12363                         gnus-newsgroup-prepared
12364                         ;; Also make sure that this isn't a
12365                         ;; dead summary buffer.
12366                         (not gnus-dead-summary-mode))))
12367         (push buffer buffers)))
12368     ;; Go through all these summary buffers and offer to save them.
12369     (when buffers
12370       (save-excursion
12371         (map-y-or-n-p
12372          "Update summary buffer %s? "
12373          (lambda (buf)
12374            (switch-to-buffer buf)
12375            (gnus-summary-exit))
12376          buffers)))))
12377
12378 (defun gnus-summary-setup-default-charset ()
12379   "Setup newsgroup default charset."
12380   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12381       (setq gnus-newsgroup-charset nil)
12382     (let* ((ignored-charsets
12383             (or gnus-newsgroup-ephemeral-ignored-charsets
12384                 (append
12385                  (and gnus-newsgroup-name
12386                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12387                  gnus-newsgroup-ignored-charsets))))
12388       (setq gnus-newsgroup-charset
12389             (or gnus-newsgroup-ephemeral-charset
12390                 (and gnus-newsgroup-name
12391                      (gnus-parameter-charset gnus-newsgroup-name))
12392                 gnus-default-charset))
12393       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12394            ignored-charsets))))
12395
12396 ;;;
12397 ;;; Mime Commands
12398 ;;;
12399
12400 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12401   "Display the current article buffer fully MIME-buttonized.
12402 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12403 treated as multipart/mixed."
12404   (interactive "P")
12405   (require 'gnus-art)
12406   (let ((gnus-unbuttonized-mime-types nil)
12407         (gnus-mime-display-multipart-as-mixed show-all-parts))
12408     (gnus-summary-show-article)))
12409
12410 (defun gnus-summary-repair-multipart (article)
12411   "Add a Content-Type header to a multipart article without one."
12412   (interactive (list (gnus-summary-article-number)))
12413   (gnus-with-article article
12414     (message-narrow-to-head)
12415     (message-remove-header "Mime-Version")
12416     (goto-char (point-max))
12417     (insert "Mime-Version: 1.0\n")
12418     (widen)
12419     (when (search-forward "\n--" nil t)
12420       (let ((separator (buffer-substring (point) (point-at-eol))))
12421         (message-narrow-to-head)
12422         (message-remove-header "Content-Type")
12423         (goto-char (point-max))
12424         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12425                         separator))
12426         (widen))))
12427   (let (gnus-mark-article-hook)
12428     (gnus-summary-select-article t t nil article)))
12429
12430 (defun gnus-summary-toggle-display-buttonized ()
12431   "Toggle the buttonizing of the article buffer."
12432   (interactive)
12433   (require 'gnus-art)
12434   (if (setq gnus-inhibit-mime-unbuttonizing
12435             (not gnus-inhibit-mime-unbuttonizing))
12436       (let ((gnus-unbuttonized-mime-types nil))
12437         (gnus-summary-show-article))
12438     (gnus-summary-show-article)))
12439
12440 ;;;
12441 ;;; Generic summary marking commands
12442 ;;;
12443
12444 (defvar gnus-summary-marking-alist
12445   '((read gnus-del-mark "d")
12446     (unread gnus-unread-mark "u")
12447     (ticked gnus-ticked-mark "!")
12448     (dormant gnus-dormant-mark "?")
12449     (expirable gnus-expirable-mark "e"))
12450   "An alist of names/marks/keystrokes.")
12451
12452 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12453 (defvar gnus-summary-mark-map)
12454
12455 (defun gnus-summary-make-all-marking-commands ()
12456   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12457   (dolist (elem gnus-summary-marking-alist)
12458     (apply 'gnus-summary-make-marking-command elem)))
12459
12460 (defun gnus-summary-make-marking-command (name mark keystroke)
12461   (let ((map (make-sparse-keymap)))
12462     (define-key gnus-summary-generic-mark-map keystroke map)
12463     (dolist (lway `((next "next" next nil "n")
12464                     (next-unread "next unread" next t "N")
12465                     (prev "previous" prev nil "p")
12466                     (prev-unread "previous unread" prev t "P")
12467                     (nomove "" nil nil ,keystroke)))
12468       (let ((func (gnus-summary-make-marking-command-1
12469                    mark (car lway) lway name)))
12470         (setq func (eval func))
12471         (define-key map (nth 4 lway) func)))))
12472
12473 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12474   `(defun ,(intern
12475             (format "gnus-summary-put-mark-as-%s%s"
12476                     name (if (eq way 'nomove)
12477                              ""
12478                            (concat "-" (symbol-name way)))))
12479      (n)
12480      ,(format
12481        "Mark the current article as %s%s.
12482 If N, the prefix, then repeat N times.
12483 If N is negative, move in reverse order.
12484 The difference between N and the actual number of articles marked is
12485 returned."
12486        name (cadr lway))
12487      (interactive "p")
12488      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12489
12490 (defun gnus-summary-generic-mark (n mark move unread)
12491   "Mark N articles with MARK."
12492   (unless (eq major-mode 'gnus-summary-mode)
12493     (error "This command can only be used in the summary buffer"))
12494   (gnus-summary-show-thread)
12495   (let ((nummove
12496          (cond
12497           ((eq move 'next) 1)
12498           ((eq move 'prev) -1)
12499           (t 0))))
12500     (if (zerop nummove)
12501         (setq n 1)
12502       (when (< n 0)
12503         (setq n (abs n)
12504               nummove (* -1 nummove))))
12505     (while (and (> n 0)
12506                 (gnus-summary-mark-article nil mark)
12507                 (zerop (gnus-summary-next-subject nummove unread t)))
12508       (setq n (1- n)))
12509     (when (/= 0 n)
12510       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12511     (gnus-summary-recenter)
12512     (gnus-summary-position-point)
12513     (gnus-set-mode-line 'summary)
12514     n))
12515
12516 (defun gnus-summary-insert-articles (articles)
12517   (when (setq articles
12518               (gnus-sorted-difference articles
12519                                       (mapcar (lambda (h)
12520                                                 (mail-header-number h))
12521                                               gnus-newsgroup-headers)))
12522     (setq gnus-newsgroup-headers
12523           (gnus-merge 'list
12524                       gnus-newsgroup-headers
12525                       (gnus-fetch-headers articles)
12526                       'gnus-article-sort-by-number))
12527     ;; Suppress duplicates?
12528     (when gnus-suppress-duplicates
12529       (gnus-dup-suppress-articles))
12530
12531     (if (and gnus-fetch-old-headers
12532              (eq gnus-headers-retrieved-by 'nov))
12533         ;; We might want to build some more threads first.
12534         (if (eq gnus-fetch-old-headers 'invisible)
12535             (gnus-build-all-threads)
12536           (gnus-build-old-threads))
12537       ;; Mark the inserted articles that are unread as unread.
12538       (setq gnus-newsgroup-unreads
12539             (gnus-sorted-nunion
12540              gnus-newsgroup-unreads
12541              (gnus-sorted-nintersection
12542               (gnus-list-of-unread-articles gnus-newsgroup-name)
12543               articles)))
12544       ;; Mark the inserted articles as selected so that the information
12545       ;; of the marks having been changed by a user may be updated when
12546       ;; exiting this group.  See `gnus-summary-update-info'.
12547       (dolist (art articles)
12548         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12549     ;; Let the Gnus agent mark articles as read.
12550     (when gnus-agent
12551       (gnus-agent-get-undownloaded-list))
12552     ;; Remove list identifiers from subject
12553     (when gnus-list-identifiers
12554       (gnus-summary-remove-list-identifiers))
12555     ;; First and last article in this newsgroup.
12556     (when gnus-newsgroup-headers
12557       (setq gnus-newsgroup-begin
12558             (mail-header-number (car gnus-newsgroup-headers))
12559             gnus-newsgroup-end
12560             (mail-header-number
12561              (gnus-last-element gnus-newsgroup-headers))))
12562     (when gnus-use-scoring
12563       (gnus-possibly-score-headers))))
12564
12565 (defun gnus-summary-insert-old-articles (&optional all)
12566   "Insert all old articles in this group.
12567 If ALL is non-nil, already read articles become readable.
12568 If ALL is a number, fetch this number of articles."
12569   (interactive "P")
12570   (prog1
12571       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12572             older len)
12573         (setq older
12574               ;; Some nntp servers lie about their active range.  When
12575               ;; this happens, the active range can be in the millions.
12576               ;; Use a compressed range to avoid creating a huge list.
12577               (gnus-range-difference (list gnus-newsgroup-active) old))
12578         (setq len (gnus-range-length older))
12579         (cond
12580          ((null older) nil)
12581          ((numberp all)
12582           (if (< all len)
12583               (let ((older-range (nreverse older)))
12584                 (setq older nil)
12585
12586                 (while (> all 0)
12587                   (let* ((r (pop older-range))
12588                          (min (if (numberp r) r (car r)))
12589                          (max (if (numberp r) r (cdr r))))
12590                     (while (and (<= min max)
12591                                 (> all 0))
12592                       (push max older)
12593                       (setq all (1- all)
12594                             max (1- max))))))
12595             (setq older (gnus-uncompress-range older))))
12596          (all
12597           (setq older (gnus-uncompress-range older)))
12598          (t
12599           (when (and (numberp gnus-large-newsgroup)
12600                    (> len gnus-large-newsgroup))
12601               (let* ((cursor-in-echo-area nil)
12602                      (initial (gnus-parameter-large-newsgroup-initial
12603                                gnus-newsgroup-name))
12604                      (input
12605                       (read-string
12606                        (format
12607                         "How many articles from %s (%s %d): "
12608                         (gnus-group-decoded-name gnus-newsgroup-name)
12609                         (if initial "max" "default")
12610                         len)
12611                        (if initial
12612                            (cons (number-to-string initial)
12613                                  0)))))
12614                 (unless (string-match "^[ \t]*$" input)
12615                   (setq all (string-to-number input))
12616                   (if (< all len)
12617                       (let ((older-range (nreverse older)))
12618                         (setq older nil)
12619
12620                         (while (> all 0)
12621                           (let* ((r (pop older-range))
12622                                  (min (if (numberp r) r (car r)))
12623                                  (max (if (numberp r) r (cdr r))))
12624                             (while (and (<= min max)
12625                                         (> all 0))
12626                               (push max older)
12627                               (setq all (1- all)
12628                                     max (1- max))))))))))
12629           (setq older (gnus-uncompress-range older))))
12630         (if (not older)
12631             (message "No old news.")
12632           (gnus-summary-insert-articles older)
12633           (gnus-summary-limit (gnus-sorted-nunion old older))))
12634     (gnus-summary-position-point)))
12635
12636 (defun gnus-summary-insert-new-articles ()
12637   "Insert all new articles in this group."
12638   (interactive)
12639   (prog1
12640       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12641             (old-active gnus-newsgroup-active)
12642             (nnmail-fetched-sources (list t))
12643             i new)
12644         (setq gnus-newsgroup-active
12645               (gnus-activate-group gnus-newsgroup-name 'scan))
12646         (setq i (cdr gnus-newsgroup-active))
12647         (while (> i (cdr old-active))
12648           (push i new)
12649           (decf i))
12650         (if (not new)
12651             (message "No gnus is bad news")
12652           (gnus-summary-insert-articles new)
12653           (setq gnus-newsgroup-unreads
12654                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12655           (gnus-summary-limit (gnus-sorted-nunion old new))))
12656     (gnus-summary-position-point)))
12657
12658 ;;; Bookmark support for Gnus.
12659 (declare-function bookmark-make-record-default
12660                   "bookmark" (&optional no-file no-context posn))
12661 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12662 (declare-function bookmark-default-handler "bookmark" (bmk))
12663 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12664 (defvar bookmark-yank-point)
12665 (defvar bookmark-current-buffer)
12666
12667 (defun gnus-summary-bookmark-make-record ()
12668   "Make a bookmark entry for a Gnus summary buffer."
12669   (let (pos buf)
12670     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12671       (save-restriction              ; FIXME is it necessary to widen?
12672         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12673       (setq buf "art") ; We are recording bookmark from article buffer.
12674       (setq bookmark-yank-point (point))
12675       (setq bookmark-current-buffer (current-buffer))
12676       (gnus-article-show-summary))      ; Go back in summary buffer.
12677     ;; We are now recording bookmark from summary buffer.
12678     (unless buf (setq buf "sum"))
12679     (let* ((subject (elt (gnus-summary-article-header) 1))
12680            (grp     (car gnus-article-current))
12681            (art     (cdr gnus-article-current))
12682            (head    (gnus-summary-article-header art))
12683            (id      (mail-header-id head)))
12684       `(,subject
12685         ,@(condition-case nil
12686               (bookmark-make-record-default 'no-file 'no-context pos)
12687             (wrong-number-of-arguments
12688              (bookmark-make-record-default 'point-only)))
12689         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12690         (group . ,grp) (article . ,art)
12691         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12692
12693 ;;;###autoload
12694 (defun gnus-summary-bookmark-jump (bookmark)
12695   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12696 BOOKMARK is a bookmark name or a bookmark record."
12697   (let ((group    (bookmark-prop-get bookmark 'group))
12698         (article  (bookmark-prop-get bookmark 'article))
12699         (id       (bookmark-prop-get bookmark 'message-id))
12700         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12701     (gnus-fetch-group group (list article))
12702     (gnus-summary-insert-cached-articles)
12703     (gnus-summary-goto-article id nil 'force)
12704     ;; FIXME we have to wait article buffer is ready (only large buffer)
12705     ;; Is there a better solution to know that?
12706     ;; If we don't wait `bookmark-default-handler' will have no chance
12707     ;; to set position. However there is no error, just wrong pos.
12708     (sit-for 1)
12709     (when (string= buf "Gnus-art")
12710       (other-window 1))
12711     (bookmark-default-handler
12712      `(""
12713        (buffer . ,(current-buffer))
12714        . ,(bookmark-get-bookmark-record bookmark)))))
12715
12716 (gnus-summary-make-all-marking-commands)
12717
12718 (gnus-ems-redefine)
12719
12720 (provide 'gnus-sum)
12721
12722 (run-hooks 'gnus-sum-load-hook)
12723
12724 ;; Local Variables:
12725 ;; coding: iso-8859-1
12726 ;; End:
12727
12728 ;;; gnus-sum.el ends here