Fix inloop if the server dies before the async -finish is called
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996-2012 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (eval-when-compile
31   (require 'cl))
32 (eval-when-compile
33   (when (featurep 'xemacs)
34     (require 'easy-mmode))) ; for `define-minor-mode'
35
36 (defvar tool-bar-mode)
37 (defvar gnus-tmp-header)
38
39 (require 'gnus)
40 (require 'gnus-group)
41 (require 'gnus-spec)
42 (require 'gnus-range)
43 (require 'gnus-int)
44 (require 'gnus-undo)
45 (require 'gnus-util)
46 (require 'gmm-utils)
47 (require 'mm-decode)
48 (require 'nnoo)
49
50 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
51 (autoload 'gnus-cache-write-active "gnus-cache")
52 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
53 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
54 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
55 (autoload 'mm-uu-dissect "mm-uu")
56 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
57   "Deuglify broken Outlook (Express) articles and redisplay."
58   t)
59 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
60 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
61 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
62 (autoload 'nnir-article-rsv "nnir" nil nil 'macro)
63 (autoload 'nnir-article-group "nnir" nil nil 'macro)
64
65 (defcustom gnus-kill-summary-on-exit t
66   "*If non-nil, kill the summary buffer when you exit from it.
67 If nil, the summary will become a \"*Dead Summary*\" buffer, and
68 it will be killed sometime later."
69   :group 'gnus-summary-exit
70   :type 'boolean)
71
72 (defcustom gnus-summary-next-group-on-exit t
73   "If non-nil, go to the next unread newsgroup on summary exit.
74 See `gnus-group-goto-unread'."
75   :link '(custom-manual "(gnus)Group Maneuvering")
76   :group 'gnus-summary-exit
77   :version "23.1" ;; No Gnus
78   :type 'boolean)
79
80 (defcustom gnus-summary-stop-at-end-of-message nil
81   "If non-nil, don't select the next message when using `SPC'."
82   :link '(custom-manual "(gnus)Group Maneuvering")
83   :group 'gnus-summary-maneuvering
84   :version "24.1"
85   :type 'boolean)
86
87 (defcustom gnus-fetch-old-headers nil
88   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
89 If an unread article in the group refers to an older, already
90 read (or just marked as read) article, the old article will not
91 normally be displayed in the Summary buffer.  If this variable is
92 t, Gnus will attempt to grab the headers to the old articles, and
93 thereby build complete threads.  If it has the value `some', all
94 old headers will be fetched but only enough headers to connect
95 otherwise loose threads will be displayed.  This variable can
96 also be a number.  In that case, no more than that number of old
97 headers will be fetched.  If it has the value `invisible', all
98 old headers will be fetched, but none will be displayed.
99
100 The server has to support NOV for any of this to work.
101
102 This feature can seriously impact performance it ignores all
103 locally cached header entries.  Setting it to t for groups for a
104 server that doesn't expire articles (such as news.gmane.org),
105 leads to very slow summary generation."
106   :group 'gnus-thread
107   :type '(choice (const :tag "off" nil)
108                  (const :tag "on" t)
109                  (const some)
110                  (const invisible)
111                  number
112                  (sexp :menu-tag "other" t)))
113
114 (defcustom gnus-refer-thread-limit 500
115   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
116 If t, fetch all the available old headers."
117   :group 'gnus-thread
118   :type '(choice number
119                  (sexp :menu-tag "other" t)))
120
121 (defcustom gnus-refer-thread-use-nnir nil
122   "*Use nnir to search an entire server when referring threads. A
123 nil value will only search for thread-related articles in the
124 current group."
125   :group 'gnus-thread
126   :type 'boolean)
127
128 (defcustom gnus-summary-make-false-root 'adopt
129   "*nil means that Gnus won't gather loose threads.
130 If the root of a thread has expired or been read in a previous
131 session, the information necessary to build a complete thread has been
132 lost.  Instead of having many small sub-threads from this original thread
133 scattered all over the summary buffer, Gnus can gather them.
134
135 If non-nil, Gnus will try to gather all loose sub-threads from an
136 original thread into one large thread.
137
138 If this variable is non-nil, it should be one of `none', `adopt',
139 `dummy' or `empty'.
140
141 If this variable is `none', Gnus will not make a false root, but just
142 present the sub-threads after another.
143 If this variable is `dummy', Gnus will create a dummy root that will
144 have all the sub-threads as children.
145 If this variable is `adopt', Gnus will make one of the \"children\"
146 the parent and mark all the step-children as such.
147 If this variable is `empty', the \"children\" are printed with empty
148 subject fields.  (Or rather, they will be printed with a string
149 given by the `gnus-summary-same-subject' variable.)"
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  (const none)
153                  (const dummy)
154                  (const adopt)
155                  (const empty)))
156
157 (defcustom gnus-summary-make-false-root-always nil
158   "Always make a false dummy root."
159   :version "22.1"
160   :group 'gnus-thread
161   :type 'boolean)
162
163 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
164   "*A regexp to match subjects to be excluded from loose thread gathering.
165 As loose thread gathering is done on subjects only, that means that
166 there can be many false gatherings performed.  By rooting out certain
167 common subjects, gathering might become saner."
168   :group 'gnus-thread
169   :type 'regexp)
170
171 (defcustom gnus-summary-gather-subject-limit nil
172   "*Maximum length of subject comparisons when gathering loose threads.
173 Use nil to compare full subjects.  Setting this variable to a low
174 number will help gather threads that have been corrupted by
175 newsreaders chopping off subject lines, but it might also mean that
176 unrelated articles that have subject that happen to begin with the
177 same few characters will be incorrectly gathered.
178
179 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
180 comparing subjects."
181   :group 'gnus-thread
182   :type '(choice (const :tag "off" nil)
183                  (const fuzzy)
184                  (sexp :menu-tag "on" t)))
185
186 (defcustom gnus-simplify-subject-functions nil
187   "List of functions taking a string argument that simplify subjects.
188 The functions are applied recursively.
189
190 Useful functions to put in this list include:
191 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
192 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
193   :group 'gnus-thread
194   :type '(repeat function))
195
196 (defcustom gnus-simplify-ignored-prefixes nil
197   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
198   :group 'gnus-thread
199   :type '(choice (const :tag "off" nil)
200                  regexp))
201
202 (defcustom gnus-build-sparse-threads nil
203   "*If non-nil, fill in the gaps in threads.
204 If `some', only fill in the gaps that are needed to tie loose threads
205 together.  If `more', fill in all leaf nodes that Gnus can find.  If
206 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
207   :group 'gnus-thread
208   :type '(choice (const :tag "off" nil)
209                  (const some)
210                  (const more)
211                  (sexp :menu-tag "all" t)))
212
213 (defcustom gnus-summary-thread-gathering-function
214   'gnus-gather-threads-by-subject
215   "*Function used for gathering loose threads.
216 There are two pre-defined functions: `gnus-gather-threads-by-subject',
217 which only takes Subjects into consideration; and
218 `gnus-gather-threads-by-references', which compared the References
219 headers of the articles to find matches."
220   :group 'gnus-thread
221   :type '(radio (function-item gnus-gather-threads-by-subject)
222                 (function-item gnus-gather-threads-by-references)
223                 (function :tag "other")))
224
225 (defcustom gnus-summary-same-subject ""
226   "*String indicating that the current article has the same subject as the previous.
227 This variable will only be used if the value of
228 `gnus-summary-make-false-root' is `empty'."
229   :group 'gnus-summary-format
230   :type 'string)
231
232 (defcustom gnus-summary-goto-unread nil
233   "*If t, many commands will go to the next unread article.
234 This applies to marking commands as well as other commands that
235 \"naturally\" select the next article, like, for instance, `SPC' at
236 the end of an article.
237
238 If nil, the marking commands do NOT go to the next unread article
239 \(they go to the next article instead).  If `never', commands that
240 usually go to the next unread article, will go to the next article,
241 whether it is read or not."
242   :version "24.1"
243   :group 'gnus-summary-marks
244   :link '(custom-manual "(gnus)Setting Marks")
245   :type '(choice (const :tag "off" nil)
246                  (const never)
247                  (sexp :menu-tag "on" t)))
248
249 (defcustom gnus-summary-default-score 0
250   "*Default article score level.
251 All scores generated by the score files will be added to this score.
252 If this variable is nil, scoring will be disabled."
253   :group 'gnus-score-default
254   :type '(choice (const :tag "disable")
255                  integer))
256
257 (defcustom gnus-summary-default-high-score 0
258   "*Default threshold for a high scored article.
259 An article will be highlighted as high scored if its score is greater
260 than this score."
261   :version "22.1"
262   :group 'gnus-score-default
263   :type 'integer)
264
265 (defcustom gnus-summary-default-low-score 0
266   "*Default threshold for a low scored article.
267 An article will be highlighted as low scored if its score is smaller
268 than this score."
269   :version "22.1"
270   :group 'gnus-score-default
271   :type 'integer)
272
273 (defcustom gnus-summary-zcore-fuzz 0
274   "*Fuzziness factor for the zcore in the summary buffer.
275 Articles with scores closer than this to `gnus-summary-default-score'
276 will not be marked."
277   :group 'gnus-summary-format
278   :type 'integer)
279
280 (defcustom gnus-simplify-subject-fuzzy-regexp nil
281   "*Strings to be removed when doing fuzzy matches.
282 This can either be a regular expression or list of regular expressions
283 that will be removed from subject strings if fuzzy subject
284 simplification is selected."
285   :group 'gnus-thread
286   :type '(repeat regexp))
287
288 (defcustom gnus-show-threads t
289   "*If non-nil, display threads in summary mode."
290   :group 'gnus-thread
291   :type 'boolean)
292
293 (defcustom gnus-thread-hide-subtree nil
294   "*If non-nil, hide all threads initially.
295 This can be a predicate specifier which says which threads to hide.
296 If threads are hidden, you have to run the command
297 `gnus-summary-show-thread' by hand or select an article."
298   :group 'gnus-thread
299   :type '(radio (sexp :format "Non-nil\n"
300                       :match (lambda (widget value)
301                                (not (or (consp value) (functionp value))))
302                       :value t)
303                 (const nil)
304                 (sexp :tag "Predicate specifier")))
305
306 (defcustom gnus-thread-hide-killed t
307   "*If non-nil, hide killed threads automatically."
308   :group 'gnus-thread
309   :type 'boolean)
310
311 (defcustom gnus-thread-ignore-subject t
312   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
313 If nil, articles that have different subjects from their parents will
314 start separate threads."
315   :group 'gnus-thread
316   :type 'boolean)
317
318 (defcustom gnus-thread-operation-ignore-subject t
319   "*If non-nil, subjects will be ignored when doing thread commands.
320 This affects commands like `gnus-summary-kill-thread' and
321 `gnus-summary-lower-thread'.
322
323 If this variable is nil, articles in the same thread with different
324 subjects will not be included in the operation in question.  If this
325 variable is `fuzzy', only articles that have subjects that are fuzzily
326 equal will be included."
327   :group 'gnus-thread
328   :type '(choice (const :tag "off" nil)
329                  (const fuzzy)
330                  (sexp :tag "on" t)))
331
332 (defcustom gnus-thread-indent-level 4
333   "*Number that says how much each sub-thread should be indented."
334   :group 'gnus-thread
335   :type 'integer)
336
337 (defcustom gnus-auto-extend-newsgroup t
338   "*If non-nil, extend newsgroup forward and backward when requested."
339   :group 'gnus-summary-choose
340   :type 'boolean)
341
342 (defcustom gnus-auto-select-first t
343   "If non-nil, select an article on group entry.
344 An article is selected automatically when entering a group
345 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
346 `gnus-summary-catchup-and-goto-next-group'.
347
348 Which article is selected is controlled by the variable
349 `gnus-auto-select-subject'.
350
351 If you want to prevent automatic selection of articles in some
352 newsgroups, set the variable to nil in `gnus-select-group-hook'."
353   ;; Commands include...
354   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
355   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
356   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
357   :group 'gnus-group-select
358   :type '(choice (const :tag "none" nil)
359                  (sexp :menu-tag "first" t)))
360
361 (defcustom gnus-auto-select-subject 'unseen-or-unread
362   "*Says what subject to place under point when entering a group.
363
364 This variable can either be the symbols `first' (place point on the
365 first subject), `unread' (place point on the subject line of the first
366 unread article), `best' (place point on the subject line of the
367 highest-scored article), `unseen' (place point on the subject line of
368 the first unseen article), `unseen-or-unread' (place point on the subject
369 line of the first unseen article or, if all articles have been seen, on the
370 subject line of the first unread article), or a function to be called to
371 place point on some subject line."
372   :version "24.1"
373   :group 'gnus-group-select
374   :type '(choice (const best)
375                  (const unread)
376                  (const first)
377                  (const unseen)
378                  (const unseen-or-unread)
379                  (function :tag "Function to call")))
380
381 (defcustom gnus-auto-select-next t
382   "*If non-nil, offer to go to the next group from the end of the previous.
383 If the value is t and the next newsgroup is empty, Gnus will exit
384 summary mode and go back to group mode.  If the value is neither nil
385 nor t, Gnus will select the following unread newsgroup.  In
386 particular, if the value is the symbol `quietly', the next unread
387 newsgroup will be selected without any confirmation, and if it is
388 `almost-quietly', the next group will be selected without any
389 confirmation if you are located on the last article in the group.
390 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
391 will go to the next group without confirmation."
392   :group 'gnus-summary-maneuvering
393   :type '(choice (const :tag "off" nil)
394                  (const quietly)
395                  (const almost-quietly)
396                  (const slightly-quietly)
397                  (sexp :menu-tag "on" t)))
398
399 (defcustom gnus-auto-select-same nil
400   "*If non-nil, select the next article with the same subject.
401 If there are no more articles with the same subject, go to
402 the first unread article."
403   :group 'gnus-summary-maneuvering
404   :type 'boolean)
405
406 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
407   "What article should be selected after exiting an ephemeral group.
408 Valid values include:
409
410 `next'
411   Select the next article.
412 `next-unread'
413   Select the next unread article.
414 `next-noselect'
415   Move the cursor to the next article.  This is the default.
416 `next-unread-noselect'
417   Move the cursor to the next unread article.
418
419 If it has any other value or there is no next (unread) article, the
420 article selected before entering to the ephemeral group will appear."
421   :version "23.1" ;; No Gnus
422   :group 'gnus-summary-maneuvering
423   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
424                  (const next) (const next-unread)
425                  (const next-noselect) (const next-unread-noselect)
426                  (sexp :tag "other" :value nil)))
427
428 (defcustom gnus-auto-goto-ignores 'unfetched
429   "*Says how to handle unfetched articles when maneuvering.
430
431 This variable can either be the symbols nil (maneuver to any
432 article), `undownloaded' (maneuvering while unplugged ignores articles
433 that have not been fetched), `always-undownloaded' (maneuvering always
434 ignores articles that have not been fetched), `unfetched' (maneuvering
435 ignores articles whose headers have not been fetched).
436
437 NOTE: The list of unfetched articles will always be nil when plugged
438 and, when unplugged, a subset of the undownloaded article list."
439   :version "22.1"
440   :group 'gnus-summary-maneuvering
441   :type '(choice (const :tag "None" nil)
442                  (const :tag "Undownloaded when unplugged" undownloaded)
443                  (const :tag "Undownloaded" always-undownloaded)
444                  (const :tag "Unfetched" unfetched)))
445
446 (defcustom gnus-summary-check-current nil
447   "*If non-nil, consider the current article when moving.
448 The \"unread\" movement commands will stay on the same line if the
449 current article is unread."
450   :group 'gnus-summary-maneuvering
451   :type 'boolean)
452
453 (defcustom gnus-auto-center-summary 2
454   "*If non-nil, always center the current summary buffer.
455 In particular, if `vertical' do only vertical recentering.  If non-nil
456 and non-`vertical', do both horizontal and vertical recentering."
457   :group 'gnus-summary-maneuvering
458   :type '(choice (const :tag "none" nil)
459                  (const vertical)
460                  (integer :tag "height")
461                  (sexp :menu-tag "both" t)))
462
463 (defcustom gnus-auto-center-group t
464   "If non-nil, always center the group buffer."
465   :group 'gnus-summary-maneuvering
466   :type 'boolean)
467
468 (defcustom gnus-show-all-headers nil
469   "*If non-nil, don't hide any headers."
470   :group 'gnus-article-hiding
471   :group 'gnus-article-headers
472   :type 'boolean)
473
474 (defcustom gnus-summary-ignore-duplicates nil
475   "*If non-nil, ignore articles with identical Message-ID headers."
476   :group 'gnus-summary
477   :type 'boolean)
478
479 (defcustom gnus-single-article-buffer nil
480   "*If non-nil, display all articles in the same buffer.
481 If nil, each group will get its own article buffer."
482   :version "24.1"
483   :group 'gnus-article-various
484   :type 'boolean)
485
486 (defcustom gnus-widen-article-window nil
487   "If non-nil, selecting the article buffer will display only the article buffer."
488   :version "24.1"
489   :group 'gnus-article-various
490   :type 'boolean)
491
492 (defcustom gnus-break-pages t
493   "*If non-nil, do page breaking on articles.
494 The page delimiter is specified by the `gnus-page-delimiter'
495 variable."
496   :group 'gnus-article-various
497   :type 'boolean)
498
499 (defcustom gnus-move-split-methods nil
500   "*Variable used to suggest where articles are to be moved to.
501 It uses the same syntax as the `gnus-split-methods' variable.
502 However, whereas `gnus-split-methods' specifies file names as targets,
503 this variable specifies group names."
504   :group 'gnus-summary-mail
505   :type '(repeat (choice (list :value (fun) function)
506                          (cons :value ("" "") regexp (repeat string))
507                          (sexp :value nil))))
508
509 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
510   "Function used to compute default prefix for article move/copy/etc prompts.
511 The function should take one argument, a group name, and return a
512 string with the suggested prefix."
513   :group 'gnus-summary-mail
514   :type 'function)
515
516 ;; FIXME: Although the custom type is `character' for the following variables,
517 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
518
519 (defcustom gnus-unread-mark ?           ;Whitespace
520   "*Mark used for unread articles."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-ticked-mark ?!
525   "*Mark used for ticked articles."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-dormant-mark ??
530   "*Mark used for dormant articles."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-del-mark ?r
535   "*Mark used for del'd articles."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-read-mark ?R
540   "*Mark used for read articles."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-expirable-mark ?E
545   "*Mark used for expirable articles."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-killed-mark ?K
550   "*Mark used for killed articles."
551   :group 'gnus-summary-marks
552   :type 'character)
553
554 (defcustom gnus-spam-mark ?$
555   "*Mark used for spam articles."
556   :version "22.1"
557   :group 'gnus-summary-marks
558   :type 'character)
559
560 (defcustom gnus-kill-file-mark ?X
561   "*Mark used for articles killed by kill files."
562   :group 'gnus-summary-marks
563   :type 'character)
564
565 (defcustom gnus-low-score-mark ?Y
566   "*Mark used for articles with a low score."
567   :group 'gnus-summary-marks
568   :type 'character)
569
570 (defcustom gnus-catchup-mark ?C
571   "*Mark used for articles that are caught up."
572   :group 'gnus-summary-marks
573   :type 'character)
574
575 (defcustom gnus-replied-mark ?A
576   "*Mark used for articles that have been replied to."
577   :group 'gnus-summary-marks
578   :type 'character)
579
580 (defcustom gnus-forwarded-mark ?F
581   "*Mark used for articles that have been forwarded."
582   :version "22.1"
583   :group 'gnus-summary-marks
584   :type 'character)
585
586 (defcustom gnus-recent-mark ?N
587   "*Mark used for articles that are recent."
588   :version "22.1"
589   :group 'gnus-summary-marks
590   :type 'character)
591
592 (defcustom gnus-cached-mark ?*
593   "*Mark used for articles that are in the cache."
594   :group 'gnus-summary-marks
595   :type 'character)
596
597 (defcustom gnus-saved-mark ?S
598   "*Mark used for articles that have been saved."
599   :group 'gnus-summary-marks
600   :type 'character)
601
602 (defcustom gnus-unseen-mark ?.
603   "*Mark used for articles that haven't been seen."
604   :version "22.1"
605   :group 'gnus-summary-marks
606   :type 'character)
607
608 (defcustom gnus-no-mark ?               ;Whitespace
609   "*Mark used for articles that have no other secondary mark."
610   :version "22.1"
611   :group 'gnus-summary-marks
612   :type 'character)
613
614 (defcustom gnus-ancient-mark ?O
615   "*Mark used for ancient articles."
616   :group 'gnus-summary-marks
617   :type 'character)
618
619 (defcustom gnus-sparse-mark ?Q
620   "*Mark used for sparsely reffed articles."
621   :group 'gnus-summary-marks
622   :type 'character)
623
624 (defcustom gnus-canceled-mark ?G
625   "*Mark used for canceled articles."
626   :group 'gnus-summary-marks
627   :type 'character)
628
629 (defcustom gnus-duplicate-mark ?M
630   "*Mark used for duplicate articles."
631   :group 'gnus-summary-marks
632   :type 'character)
633
634 (defcustom gnus-undownloaded-mark ?-
635   "*Mark used for articles that weren't downloaded."
636   :version "22.1"
637   :group 'gnus-summary-marks
638   :type 'character)
639
640 (defcustom gnus-downloaded-mark ?+
641   "*Mark used for articles that were downloaded."
642   :group 'gnus-summary-marks
643   :type 'character)
644
645 (defcustom gnus-downloadable-mark ?%
646   "*Mark used for articles that are to be downloaded."
647   :group 'gnus-summary-marks
648   :type 'character)
649
650 (defcustom gnus-unsendable-mark ?=
651   "*Mark used for articles that won't be sent."
652   :group 'gnus-summary-marks
653   :type 'character)
654
655 (defcustom gnus-score-over-mark ?+
656   "*Score mark used for articles with high scores."
657   :group 'gnus-summary-marks
658   :type 'character)
659
660 (defcustom gnus-score-below-mark ?-
661   "*Score mark used for articles with low scores."
662   :group 'gnus-summary-marks
663   :type 'character)
664
665 (defcustom gnus-empty-thread-mark ?     ;Whitespace
666   "*There is no thread under the article."
667   :group 'gnus-summary-marks
668   :type 'character)
669
670 (defcustom gnus-not-empty-thread-mark ?=
671   "*There is a thread under the article."
672   :group 'gnus-summary-marks
673   :type 'character)
674
675 (defcustom gnus-view-pseudo-asynchronously nil
676   "*If non-nil, Gnus will view pseudo-articles asynchronously."
677   :group 'gnus-extract-view
678   :type 'boolean)
679
680 (defcustom gnus-auto-expirable-marks
681   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
682         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
683         gnus-duplicate-mark)
684   "*The list of marks converted into expiration if a group is auto-expirable."
685   :version "24.1"
686   :group 'gnus-summary
687   :type '(repeat character))
688
689 (defcustom gnus-inhibit-user-auto-expire t
690   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
691   :version "21.1"
692   :group 'gnus-summary
693   :type 'boolean)
694
695 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
696   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
697 If nil, the expirable marks will be unchanged except that the marks
698 will be removed when copying or moving articles to a group that has
699 not turned auto-expire on.  If non-nil, articles that have been read
700 will be marked as expirable when being copied or moved to a group in
701 which auto-expire is turned on."
702   :version "23.2"
703   :type 'boolean
704   :group 'gnus-summary-marks)
705
706 (defcustom gnus-view-pseudos nil
707   "*If `automatic', pseudo-articles will be viewed automatically.
708 If `not-confirm', pseudos will be viewed automatically, and the user
709 will not be asked to confirm the command."
710   :group 'gnus-extract-view
711   :type '(choice (const :tag "off" nil)
712                  (const automatic)
713                  (const not-confirm)))
714
715 (defcustom gnus-view-pseudos-separately t
716   "*If non-nil, one pseudo-article will be created for each file to be viewed.
717 If nil, all files that use the same viewing command will be given as a
718 list of parameters to that command."
719   :group 'gnus-extract-view
720   :type 'boolean)
721
722 (defcustom gnus-insert-pseudo-articles t
723   "*If non-nil, insert pseudo-articles when decoding articles."
724   :group 'gnus-extract-view
725   :type 'boolean)
726
727 (defcustom gnus-summary-dummy-line-format
728   "   %(:                             :%) %S\n"
729   "*The format specification for the dummy roots in the summary buffer.
730 It works along the same lines as a normal formatting string,
731 with some simple extensions.
732
733 %S  The subject
734
735 General format specifiers can also be used.
736 See `(gnus)Formatting Variables'."
737   :link '(custom-manual "(gnus)Formatting Variables")
738   :group 'gnus-threading
739   :type 'string)
740
741 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
742   "*The format specification for the summary mode line.
743 It works along the same lines as a normal formatting string,
744 with some simple extensions:
745
746 %G  Group name
747 %p  Unprefixed group name
748 %A  Current article number
749 %z  Current article score
750 %V  Gnus version
751 %U  Number of unread articles in the group
752 %e  Number of unselected articles in the group
753 %Z  A string with unread/unselected article counts
754 %g  Shortish group name
755 %S  Subject of the current article
756 %u  User-defined spec
757 %s  Current score file name
758 %d  Number of dormant articles
759 %r  Number of articles that have been marked as read in this session
760 %E  Number of articles expunged by the score files"
761   :group 'gnus-summary-format
762   :type 'string)
763
764 (defcustom gnus-list-identifiers nil
765   "Regexp that matches list identifiers to be removed from subject.
766 This can also be a list of regexps."
767   :version "21.1"
768   :group 'gnus-summary-format
769   :group 'gnus-article-hiding
770   :type '(choice (const :tag "none" nil)
771                  (regexp :value ".*")
772                  (repeat :value (".*") regexp)))
773
774 (defcustom gnus-summary-mark-below 0
775   "*Mark all articles with a score below this variable as read.
776 This variable is local to each summary buffer and usually set by the
777 score file."
778   :group 'gnus-score-default
779   :type 'integer)
780
781 (defun gnus-widget-reversible-match (widget value)
782   "Ignoring WIDGET, convert VALUE to internal form.
783 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
784   ;; (debug value)
785   (or (symbolp value)
786       (and (listp value)
787            (eq (length value) 2)
788            (eq (nth 0 value) 'not)
789            (symbolp (nth 1 value)))))
790
791 (defun gnus-widget-reversible-to-internal (widget value)
792   "Ignoring WIDGET, convert VALUE to internal form.
793 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
794 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
795   ;; (debug value)
796   (if (atom value)
797       (list value nil)
798     (list (nth 1 value) t)))
799
800 (defun gnus-widget-reversible-to-external (widget value)
801   "Ignoring WIDGET, convert VALUE to external form.
802 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
803 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
804   ;; (debug value)
805   (if (nth 1 value)
806       (list 'not (nth 0 value))
807     (nth 0 value)))
808
809 (define-widget 'gnus-widget-reversible 'group
810   "A `group' that convert values."
811   :match 'gnus-widget-reversible-match
812   :value-to-internal 'gnus-widget-reversible-to-internal
813   :value-to-external 'gnus-widget-reversible-to-external)
814
815 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
816   "*List of functions used for sorting articles in the summary buffer.
817
818 Each function takes two articles and returns non-nil if the first
819 article should be sorted before the other.  If you use more than one
820 function, the primary sort function should be the last.  You should
821 probably always include `gnus-article-sort-by-number' in the list of
822 sorting functions -- preferably first.  Also note that sorting by date
823 is often much slower than sorting by number, and the sorting order is
824 very similar.  (Sorting by date means sorting by the time the message
825 was sent, sorting by number means sorting by arrival time.)
826
827 Each item can also be a list `(not F)' where F is a function;
828 this reverses the sort order.
829
830 Ready-made functions include `gnus-article-sort-by-number',
831 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
832 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
833 and `gnus-article-sort-by-score'.
834
835 When threading is turned on, the variable `gnus-thread-sort-functions'
836 controls how articles are sorted."
837   :group 'gnus-summary-sort
838   :type '(repeat (gnus-widget-reversible
839                   (choice (function-item gnus-article-sort-by-number)
840                           (function-item gnus-article-sort-by-author)
841                           (function-item gnus-article-sort-by-subject)
842                           (function-item gnus-article-sort-by-date)
843                           (function-item gnus-article-sort-by-score)
844                           (function-item gnus-article-sort-by-random)
845                           (function :tag "other"))
846                   (boolean :tag "Reverse order"))))
847
848
849 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
850   "*List of functions used for sorting threads in the summary buffer.
851 By default, threads are sorted by article number.
852
853 Each function takes two threads and returns non-nil if the first
854 thread should be sorted before the other.  If you use more than one
855 function, the primary sort function should be the last.  You should
856 probably always include `gnus-thread-sort-by-number' in the list of
857 sorting functions -- preferably first.  Also note that sorting by date
858 is often much slower than sorting by number, and the sorting order is
859 very similar.  (Sorting by date means sorting by the time the message
860 was sent, sorting by number means sorting by arrival time.)
861
862 Each list item can also be a list `(not F)' where F is a
863 function; this specifies reversed sort order.
864
865 Ready-made functions include `gnus-thread-sort-by-number',
866 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
867 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
868 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
869 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
870 and `gnus-thread-sort-by-total-score' (see
871 `gnus-thread-score-function').
872
873 When threading is turned off, the variable
874 `gnus-article-sort-functions' controls how articles are sorted."
875   :group 'gnus-summary-sort
876   :type '(repeat
877           (gnus-widget-reversible
878            (choice (function-item gnus-thread-sort-by-number)
879                    (function-item gnus-thread-sort-by-author)
880                    (function-item gnus-thread-sort-by-recipient)
881                    (function-item gnus-thread-sort-by-subject)
882                    (function-item gnus-thread-sort-by-date)
883                    (function-item gnus-thread-sort-by-score)
884                    (function-item gnus-thread-sort-by-most-recent-number)
885                    (function-item gnus-thread-sort-by-most-recent-date)
886                    (function-item gnus-thread-sort-by-random)
887                    (function-item gnus-thread-sort-by-total-score)
888                    (function :tag "other"))
889            (boolean :tag "Reverse order"))))
890
891 (defcustom gnus-thread-score-function '+
892   "*Function used for calculating the total score of a thread.
893
894 The function is called with the scores of the article and each
895 subthread and should then return the score of the thread.
896
897 Some functions you can use are `+', `max', or `min'."
898   :group 'gnus-summary-sort
899   :type 'function)
900
901 (defcustom gnus-summary-expunge-below nil
902   "All articles that have a score less than this variable will be expunged.
903 This variable is local to the summary buffers."
904   :group 'gnus-score-default
905   :type '(choice (const :tag "off" nil)
906                  integer))
907
908 (defcustom gnus-thread-expunge-below nil
909   "All threads that have a total score less than this variable will be expunged.
910 See `gnus-thread-score-function' for en explanation of what a
911 \"thread score\" is.
912
913 This variable is local to the summary buffers."
914   :group 'gnus-threading
915   :group 'gnus-score-default
916   :type '(choice (const :tag "off" nil)
917                  integer))
918
919 (defcustom gnus-summary-mode-hook nil
920   "*A hook for Gnus summary mode.
921 This hook is run before any variables are set in the summary buffer."
922   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
923   :group 'gnus-summary-various
924   :type 'hook)
925
926 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
927 (when (featurep 'xemacs)
928   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
929   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
930   (add-hook 'gnus-summary-mode-hook
931             'gnus-xmas-switch-horizontal-scrollbar-off))
932
933 (defcustom gnus-summary-menu-hook nil
934   "*Hook run after the creation of the summary mode menu."
935   :group 'gnus-summary-visual
936   :type 'hook)
937
938 (defcustom gnus-summary-exit-hook nil
939   "*A hook called on exit from the summary buffer.
940 It will be called with point in the group buffer."
941   :group 'gnus-summary-exit
942   :type 'hook)
943
944 (defcustom gnus-summary-prepare-hook nil
945   "*A hook called after the summary buffer has been generated.
946 If you want to modify the summary buffer, you can use this hook."
947   :group 'gnus-summary-various
948   :type 'hook)
949
950 (defcustom gnus-summary-prepared-hook nil
951   "*A hook called as the last thing after the summary buffer has been generated."
952   :group 'gnus-summary-various
953   :type 'hook)
954
955 (defcustom gnus-summary-generate-hook nil
956   "*A hook run just before generating the summary buffer.
957 This hook is commonly used to customize threading variables and the
958 like."
959   :group 'gnus-summary-various
960   :type 'hook)
961
962 (defcustom gnus-select-group-hook nil
963   "*A hook called when a newsgroup is selected.
964
965 If you'd like to simplify subjects like the
966 `gnus-summary-next-same-subject' command does, you can use the
967 following hook:
968
969  (add-hook gnus-select-group-hook
970            (lambda ()
971              (mapcar (lambda (header)
972                        (mail-header-set-subject
973                         header
974                         (gnus-simplify-subject
975                          (mail-header-subject header) 're-only)))
976                      gnus-newsgroup-headers)))"
977   :group 'gnus-group-select
978   :type 'hook)
979
980 (defcustom gnus-select-article-hook nil
981   "*A hook called when an article is selected."
982   :group 'gnus-summary-choose
983   :options '(gnus-agent-fetch-selected-article)
984   :type 'hook)
985
986 (defcustom gnus-visual-mark-article-hook
987   (list 'gnus-highlight-selected-summary)
988   "*Hook run after selecting an article in the summary buffer.
989 It is meant to be used for highlighting the article in some way.  It
990 is not run if `gnus-visual' is nil."
991   :group 'gnus-summary-visual
992   :type 'hook)
993
994 (defcustom gnus-parse-headers-hook nil
995   "*A hook called before parsing the headers."
996   :group 'gnus-various
997   :type 'hook)
998
999 (defcustom gnus-exit-group-hook nil
1000   "*A hook called when exiting summary mode.
1001 This hook is not called from the non-updating exit commands like `Q'."
1002   :group 'gnus-various
1003   :type 'hook)
1004
1005 (defcustom gnus-summary-update-hook nil
1006   "*A hook called when a summary line is changed.
1007 The hook will not be called if `gnus-visual' is nil.
1008
1009 The default function `gnus-summary-highlight-line' will
1010 highlight the line according to the `gnus-summary-highlight'
1011 variable."
1012   :group 'gnus-summary-visual
1013   :type 'hook)
1014
1015 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1016   "*A hook called when an article is selected for the first time.
1017 The hook is intended to mark an article as read (or unread)
1018 automatically when it is selected."
1019   :group 'gnus-summary-choose
1020   :type 'hook)
1021
1022 (defcustom gnus-group-no-more-groups-hook nil
1023   "*A hook run when returning to group mode having no more (unread) groups."
1024   :group 'gnus-group-select
1025   :type 'hook)
1026
1027 (defcustom gnus-ps-print-hook nil
1028   "*A hook run before ps-printing something from Gnus."
1029   :group 'gnus-summary
1030   :type 'hook)
1031
1032 (defcustom gnus-summary-article-move-hook nil
1033   "*A hook called after an article is moved, copied, respooled, or crossposted."
1034   :version "22.1"
1035   :group 'gnus-summary
1036   :type 'hook)
1037
1038 (defcustom gnus-summary-article-delete-hook nil
1039   "*A hook called after an article is deleted."
1040   :version "22.1"
1041   :group 'gnus-summary
1042   :type 'hook)
1043
1044 (defcustom gnus-summary-article-expire-hook nil
1045   "*A hook called after an article is expired."
1046   :version "22.1"
1047   :group 'gnus-summary
1048   :type 'hook)
1049
1050 (defcustom gnus-summary-display-arrow
1051   (and (fboundp 'display-graphic-p)
1052        (display-graphic-p))
1053   "*If non-nil, display an arrow highlighting the current article."
1054   :version "22.1"
1055   :group 'gnus-summary
1056   :type 'boolean)
1057
1058 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1059   "Face used for highlighting the current article in the summary buffer."
1060   :group 'gnus-summary-visual
1061   :type 'face)
1062
1063 (defvar gnus-tmp-downloaded nil)
1064
1065 (defcustom gnus-summary-highlight
1066   '(((eq mark gnus-canceled-mark)
1067      . gnus-summary-cancelled)
1068     ((and uncached (> score default-high))
1069      . gnus-summary-high-undownloaded)
1070     ((and uncached (< score default-low))
1071      . gnus-summary-low-undownloaded)
1072     (uncached
1073      . gnus-summary-normal-undownloaded)
1074     ((and (> score default-high)
1075           (or (eq mark gnus-dormant-mark)
1076               (eq mark gnus-ticked-mark)))
1077      . gnus-summary-high-ticked)
1078     ((and (< score default-low)
1079           (or (eq mark gnus-dormant-mark)
1080               (eq mark gnus-ticked-mark)))
1081      . gnus-summary-low-ticked)
1082     ((or (eq mark gnus-dormant-mark)
1083          (eq mark gnus-ticked-mark))
1084      . gnus-summary-normal-ticked)
1085     ((and (> score default-high) (eq mark gnus-ancient-mark))
1086      . gnus-summary-high-ancient)
1087     ((and (< score default-low) (eq mark gnus-ancient-mark))
1088      . gnus-summary-low-ancient)
1089     ((eq mark gnus-ancient-mark)
1090      . gnus-summary-normal-ancient)
1091     ((and (> score default-high) (eq mark gnus-unread-mark))
1092      . gnus-summary-high-unread)
1093     ((and (< score default-low) (eq mark gnus-unread-mark))
1094      . gnus-summary-low-unread)
1095     ((eq mark gnus-unread-mark)
1096      . gnus-summary-normal-unread)
1097     ((> score default-high)
1098      . gnus-summary-high-read)
1099     ((< score default-low)
1100      . gnus-summary-low-read)
1101     (t
1102      . gnus-summary-normal-read))
1103   "*Controls the highlighting of summary buffer lines.
1104
1105 A list of (FORM . FACE) pairs.  When deciding how a particular
1106 summary line should be displayed, each form is evaluated.  The content
1107 of the face field after the first true form is used.  You can change
1108 how those summary lines are displayed, by editing the face field.
1109
1110 You can use the following variables in the FORM field.
1111
1112 score:        The article's score.
1113 default:      The default article score.
1114 default-high: The default score for high scored articles.
1115 default-low:  The default score for low scored articles.
1116 below:        The score below which articles are automatically marked as read.
1117 mark:         The article's mark.
1118 uncached:     Non-nil if the article is uncached."
1119   :group 'gnus-summary-visual
1120   :type '(repeat (cons (sexp :tag "Form" nil)
1121                        face)))
1122 (put 'gnus-summary-highlight 'risky-local-variable t)
1123
1124 (defcustom gnus-alter-header-function nil
1125   "Function called to allow alteration of article header structures.
1126 The function is called with one parameter, the article header vector,
1127 which it may alter in any way."
1128   :type '(choice (const :tag "None" nil)
1129                  function)
1130   :group 'gnus-summary)
1131
1132 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1133   "Function used to decode a string with encoded words.")
1134
1135 (defvar gnus-decode-encoded-address-function
1136   'mail-decode-encoded-address-string
1137   "Function used to decode addresses with encoded words.")
1138
1139 (defcustom gnus-extra-headers '(To Cc Keywords Gcc Newsgroups)
1140   "*Extra headers to parse."
1141   :version "24.1"                       ; added Cc Keywords Gcc
1142   :group 'gnus-summary
1143   :type '(repeat symbol))
1144
1145 (defcustom gnus-ignored-from-addresses
1146   (and user-mail-address
1147        (not (string= user-mail-address ""))
1148        (regexp-quote user-mail-address))
1149   "*From headers that may be suppressed in favor of To headers.
1150 This can be a regexp or a list of regexps."
1151   :version "21.1"
1152   :group 'gnus-summary
1153   :type '(choice regexp
1154                  (repeat :tag "Regexp List" regexp)))
1155
1156 (defsubst gnus-ignored-from-addresses ()
1157   (gmm-regexp-concat gnus-ignored-from-addresses))
1158
1159 (defcustom gnus-summary-to-prefix "-> "
1160   "*String prefixed to the To field in the summary line when
1161 using `gnus-ignored-from-addresses'."
1162   :version "22.1"
1163   :group 'gnus-summary
1164   :type 'string)
1165
1166 (defcustom gnus-summary-newsgroup-prefix "=> "
1167   "*String prefixed to the Newsgroup field in the summary
1168 line when using `gnus-ignored-from-addresses'."
1169   :version "22.1"
1170   :group 'gnus-summary
1171   :type 'string)
1172
1173 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1174   "List of charsets that should be ignored.
1175 When these charsets are used in the \"charset\" parameter, the
1176 default charset will be used instead."
1177   :version "21.1"
1178   :type '(repeat symbol)
1179   :group 'gnus-charset)
1180
1181 (defcustom gnus-newsgroup-maximum-articles nil
1182   "The maximum number of articles a newsgroup.
1183 If this is a number, old articles in a newsgroup exceeding this number
1184 are silently ignored.  If it is nil, no article is ignored.  Note that
1185 setting this variable to a number might prevent you from reading very
1186 old articles."
1187   :group 'gnus-group-select
1188   :version "22.2"
1189   :type '(choice (const :tag "No limit" nil)
1190                  integer))
1191
1192 (gnus-define-group-parameter
1193  ignored-charsets
1194  :type list
1195  :function-document
1196  "Return the ignored charsets of GROUP."
1197  :variable gnus-group-ignored-charsets-alist
1198  :variable-default
1199  '(("alt\\.chinese\\.text" iso-8859-1))
1200  :variable-document
1201  "Alist of regexps (to match group names) and charsets that should be ignored.
1202 When these charsets are used in the \"charset\" parameter, the
1203 default charset will be used instead."
1204  :variable-group gnus-charset
1205  :variable-type '(repeat (cons (regexp :tag "Group")
1206                                (repeat symbol)))
1207  :parameter-type '(choice :tag "Ignored charsets"
1208                           :value nil
1209                           (repeat (symbol)))
1210  :parameter-document       "\
1211 List of charsets that should be ignored.
1212
1213 When these charsets are used in the \"charset\" parameter, the
1214 default charset will be used instead.")
1215
1216 (defcustom gnus-group-highlight-words-alist nil
1217   "Alist of group regexps and highlight regexps.
1218 This variable uses the same syntax as `gnus-emphasis-alist'."
1219   :version "21.1"
1220   :type '(repeat (cons (regexp :tag "Group")
1221                        (repeat (list (regexp :tag "Highlight regexp")
1222                                      (number :tag "Group for entire word" 0)
1223                                      (number :tag "Group for displayed part" 0)
1224                                      (symbol :tag "Face"
1225                                              gnus-emphasis-highlight-words)))))
1226   :group 'gnus-summary-visual)
1227
1228 (defcustom gnus-summary-show-article-charset-alist
1229   nil
1230   "Alist of number and charset.
1231 The article will be shown with the charset corresponding to the
1232 numbered argument.
1233 For example: ((1 . cn-gb-2312) (2 . big5))."
1234   :version "21.1"
1235   :type '(repeat (cons (number :tag "Argument" 1)
1236                        (symbol :tag "Charset")))
1237   :group 'gnus-charset)
1238
1239 (defcustom gnus-preserve-marks t
1240   "Whether marks are preserved when moving, copying and respooling messages."
1241   :version "21.1"
1242   :type 'boolean
1243   :group 'gnus-summary-marks)
1244
1245 (defcustom gnus-propagate-marks nil
1246   "If non-nil, Gnus will store and retrieve marks from the backends.
1247 This means that marks will be stored both in .newsrc.eld and in
1248 the backend, and will slow operation down somewhat."
1249   :type 'boolean
1250   :group 'gnus-summary-marks)
1251
1252 (defcustom gnus-alter-articles-to-read-function nil
1253   "Function to be called to alter the list of articles to be selected."
1254   :type '(choice (const nil) function)
1255   :group 'gnus-summary)
1256
1257 (defcustom gnus-orphan-score nil
1258   "*All orphans get this score added.  Set in the score file."
1259   :group 'gnus-score-default
1260   :type '(choice (const nil)
1261                  integer))
1262
1263 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1264   "*A regexp to match MIME parts when saving multiple parts of a
1265 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1266 This regexp will be used by default when prompting the user for which
1267 type of files to save."
1268   :group 'gnus-summary
1269   :type 'regexp)
1270
1271 (defcustom gnus-read-all-available-headers nil
1272   "Whether Gnus should parse all headers made available to it.
1273 This is mostly relevant for slow back ends where the user may
1274 wish to widen the summary buffer to include all headers
1275 that were fetched."
1276   :version "22.1"
1277   :group 'gnus-summary
1278   :type '(choice boolean regexp))
1279
1280 (defcustom gnus-summary-pipe-output-default-command nil
1281   "Command (and optional arguments) used to pipe article to subprocess.
1282 This will be used as the default command if it is non-nil.  The value
1283 will be updated if you modify it when executing the command
1284 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1285   :version "23.1" ;; No Gnus
1286   :group 'gnus-summary
1287   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1288
1289 (defcustom gnus-summary-muttprint-program "muttprint"
1290   "Command (and optional arguments) used to run Muttprint.
1291 The value will be updated if you modify it when executing the command
1292 `gnus-summary-muttprint'."
1293   :version "22.1"
1294   :group 'gnus-summary
1295   :type 'string)
1296
1297 (defcustom gnus-article-loose-mime t
1298   "If non-nil, don't require MIME-Version header.
1299 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1300 supply the MIME-Version header or deliberately strip it from the mail.
1301 If non-nil (the default), Gnus will treat some articles as MIME
1302 even if the MIME-Version header is missing."
1303   :version "22.1"
1304   :type 'boolean
1305   :group 'gnus-article-mime)
1306
1307 (defcustom gnus-article-emulate-mime t
1308   "If non-nil, use MIME emulation for uuencode and the like.
1309 This means that Gnus will search message bodies for text that look
1310 like uuencoded bits, yEncoded bits, and so on, and present that using
1311 the normal Gnus MIME machinery."
1312   :version "22.1"
1313   :type 'boolean
1314   :group 'gnus-article-mime)
1315
1316 ;;; Internal variables
1317
1318 (defvar gnus-summary-display-cache nil)
1319 (defvar gnus-article-mime-handles nil)
1320 (defvar gnus-article-decoded-p nil)
1321 (defvar gnus-article-charset nil)
1322 (defvar gnus-article-ignored-charsets nil)
1323 (defvar gnus-scores-exclude-files nil)
1324 (defvar gnus-page-broken nil)
1325
1326 (defvar gnus-original-article nil)
1327 (defvar gnus-article-internal-prepare-hook nil)
1328 (defvar gnus-newsgroup-process-stack nil)
1329
1330 (defvar gnus-thread-indent-array nil)
1331 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1332 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1333   "Function called to sort the articles within a thread after it has been gathered together.")
1334
1335 (defvar gnus-summary-save-parts-type-history nil)
1336 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1337
1338 ;; Avoid highlighting in kill files.
1339 (defvar gnus-summary-inhibit-highlight nil)
1340 (defvar gnus-newsgroup-selected-overlay nil)
1341 (defvar gnus-inhibit-limiting nil)
1342 (defvar gnus-newsgroup-adaptive-score-file nil)
1343 (defvar gnus-current-score-file nil)
1344 (defvar gnus-current-move-group nil)
1345 (defvar gnus-current-copy-group nil)
1346 (defvar gnus-current-crosspost-group nil)
1347 (defvar gnus-newsgroup-display nil)
1348
1349 (defvar gnus-newsgroup-dependencies nil)
1350 (defvar gnus-newsgroup-adaptive nil)
1351 (defvar gnus-summary-display-article-function nil)
1352 (defvar gnus-summary-highlight-line-function nil
1353   "Function called after highlighting a summary line.")
1354
1355 (defvar gnus-summary-line-format-alist
1356   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1357     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1358     (?s gnus-tmp-subject-or-nil ?s)
1359     (?n gnus-tmp-name ?s)
1360     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1361         ?s)
1362     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1363             gnus-tmp-from) ?s)
1364     (?F gnus-tmp-from ?s)
1365     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1366     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1367     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1368     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1369     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1370     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1371     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1372     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1373     (?L gnus-tmp-lines ?s)
1374     (?Z (or (nnir-article-rsv (mail-header-number gnus-tmp-header))
1375             0) ?d)
1376     (?G (or (nnir-article-group (mail-header-number gnus-tmp-header))
1377             "") ?s)
1378     (?g (or (gnus-group-short-name
1379              (nnir-article-group (mail-header-number gnus-tmp-header)))
1380             "") ?s)
1381     (?O gnus-tmp-downloaded ?c)
1382     (?I gnus-tmp-indentation ?s)
1383     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1384     (?R gnus-tmp-replied ?c)
1385     (?\[ gnus-tmp-opening-bracket ?c)
1386     (?\] gnus-tmp-closing-bracket ?c)
1387     (?\> (make-string gnus-tmp-level ? ) ?s)
1388     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1389     (?i gnus-tmp-score ?d)
1390     (?z gnus-tmp-score-char ?c)
1391     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1392     (?U gnus-tmp-unread ?c)
1393     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1394         ?s)
1395     (?t (gnus-summary-number-of-articles-in-thread
1396          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1397         ?d)
1398     (?e (gnus-summary-number-of-articles-in-thread
1399          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1400         ?c)
1401     (?u gnus-tmp-user-defined ?s)
1402     (?P (gnus-pick-line-number) ?d)
1403     (?B gnus-tmp-thread-tree-header-string ?s)
1404     (user-date (gnus-user-date
1405                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1406   "An alist of format specifications that can appear in summary lines.
1407 These are paired with what variables they correspond with, along with
1408 the type of the variable (string, integer, character, etc).")
1409
1410 (defvar gnus-summary-dummy-line-format-alist
1411   `((?S gnus-tmp-subject ?s)
1412     (?N gnus-tmp-number ?d)
1413     (?u gnus-tmp-user-defined ?s)))
1414
1415 (defvar gnus-summary-mode-line-format-alist
1416   `((?G gnus-tmp-group-name ?s)
1417     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1418     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1419     (?A gnus-tmp-article-number ?d)
1420     (?Z gnus-tmp-unread-and-unselected ?s)
1421     (?V gnus-version ?s)
1422     (?U gnus-tmp-unread-and-unticked ?d)
1423     (?S gnus-tmp-subject ?s)
1424     (?e gnus-tmp-unselected ?d)
1425     (?u gnus-tmp-user-defined ?s)
1426     (?d (length gnus-newsgroup-dormant) ?d)
1427     (?t (length gnus-newsgroup-marked) ?d)
1428     (?h (length gnus-newsgroup-spam-marked) ?d)
1429     (?r (length gnus-newsgroup-reads) ?d)
1430     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1431     (?E gnus-newsgroup-expunged-tally ?d)
1432     (?s (gnus-current-score-file-nondirectory) ?s)))
1433
1434 ;; This is here rather than in gnus-art for compilation reasons.
1435 (defvar gnus-article-mode-line-format-alist
1436   (nconc '((?w (gnus-article-wash-status) ?s)
1437            (?m (gnus-article-mime-part-status) ?s))
1438          gnus-summary-mode-line-format-alist))
1439
1440 (defvar gnus-last-search-regexp nil
1441   "Default regexp for article search command.")
1442
1443 (defvar gnus-last-shell-command nil
1444   "Default shell command on article.")
1445
1446 (defvar gnus-newsgroup-agentized nil
1447   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1448 (defvar gnus-newsgroup-begin nil)
1449 (defvar gnus-newsgroup-end nil)
1450 (defvar gnus-newsgroup-last-rmail nil)
1451 (defvar gnus-newsgroup-last-mail nil)
1452 (defvar gnus-newsgroup-last-folder nil)
1453 (defvar gnus-newsgroup-last-file nil)
1454 (defvar gnus-newsgroup-last-directory nil)
1455 (defvar gnus-newsgroup-auto-expire nil)
1456 (defvar gnus-newsgroup-active nil)
1457 (defvar gnus-newsgroup-highest nil)
1458
1459 (defvar gnus-newsgroup-data nil)
1460 (defvar gnus-newsgroup-data-reverse nil)
1461 (defvar gnus-newsgroup-limit nil)
1462 (defvar gnus-newsgroup-limits nil)
1463 (defvar gnus-summary-use-undownloaded-faces nil)
1464
1465 (defvar gnus-newsgroup-unreads nil
1466   "Sorted list of unread articles in the current newsgroup.")
1467
1468 (defvar gnus-newsgroup-unselected nil
1469   "Sorted list of unselected unread articles in the current newsgroup.")
1470
1471 (defvar gnus-newsgroup-reads nil
1472   "Alist of read articles and article marks in the current newsgroup.")
1473
1474 (defvar gnus-newsgroup-expunged-tally nil)
1475
1476 (defvar gnus-newsgroup-marked nil
1477   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1478
1479 (defvar gnus-newsgroup-spam-marked nil
1480   "List of ranges of articles that have been marked as spam.")
1481
1482 (defvar gnus-newsgroup-killed nil
1483   "List of ranges of articles that have been through the scoring process.")
1484
1485 (defvar gnus-newsgroup-cached nil
1486   "Sorted list of articles that come from the article cache.")
1487
1488 (defvar gnus-newsgroup-saved nil
1489   "List of articles that have been saved.")
1490
1491 (defvar gnus-newsgroup-kill-headers nil)
1492
1493 (defvar gnus-newsgroup-replied nil
1494   "List of articles that have been replied to in the current newsgroup.")
1495
1496 (defvar gnus-newsgroup-forwarded nil
1497   "List of articles that have been forwarded in the current newsgroup.")
1498
1499 (defvar gnus-newsgroup-expirable nil
1500   "Sorted list of articles in the current newsgroup that can be expired.")
1501
1502 (defvar gnus-newsgroup-processable nil
1503   "List of articles in the current newsgroup that can be processed.")
1504
1505 (defvar gnus-newsgroup-downloadable nil
1506   "Sorted list of articles in the current newsgroup that can be processed.")
1507
1508 (defvar gnus-newsgroup-unfetched nil
1509   "Sorted list of articles in the current newsgroup whose headers have
1510 not been fetched into the agent.
1511
1512 This list will always be a subset of gnus-newsgroup-undownloaded.")
1513
1514 (defvar gnus-newsgroup-undownloaded nil
1515   "List of articles in the current newsgroup that haven't been downloaded.")
1516
1517 (defvar gnus-newsgroup-unsendable nil
1518   "List of articles in the current newsgroup that won't be sent.")
1519
1520 (defvar gnus-newsgroup-bookmarks nil
1521   "List of articles in the current newsgroup that have bookmarks.")
1522
1523 (defvar gnus-newsgroup-dormant nil
1524   "Sorted list of dormant articles in the current newsgroup.")
1525
1526 (defvar gnus-newsgroup-unseen nil
1527   "List of unseen articles in the current newsgroup.")
1528
1529 (defvar gnus-newsgroup-seen nil
1530   "Range of seen articles in the current newsgroup.")
1531
1532 (defvar gnus-newsgroup-articles nil
1533   "List of articles in the current newsgroup.")
1534
1535 (defvar gnus-newsgroup-scored nil
1536   "List of scored articles in the current newsgroup.")
1537
1538 (defvar gnus-newsgroup-headers nil
1539   "List of article headers in the current newsgroup.")
1540
1541 (defvar gnus-newsgroup-threads nil)
1542
1543 (defvar gnus-newsgroup-prepared nil
1544   "Whether the current group has been prepared properly.")
1545
1546 (defvar gnus-newsgroup-ancient nil
1547   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1548
1549 (defvar gnus-newsgroup-sparse nil)
1550
1551 (defvar gnus-current-article nil)
1552 (defvar gnus-article-current nil)
1553 (defvar gnus-current-headers nil)
1554 (defvar gnus-have-all-headers nil)
1555 (defvar gnus-last-article nil)
1556 (defvar gnus-newsgroup-history nil)
1557 (defvar gnus-newsgroup-charset nil)
1558 (defvar gnus-newsgroup-ephemeral-charset nil)
1559 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1560
1561 (defvar gnus-article-before-search nil)
1562
1563 (defvar gnus-summary-local-variables
1564   '(gnus-newsgroup-name
1565
1566     ;; Marks lists
1567     gnus-newsgroup-unreads
1568     gnus-newsgroup-unselected
1569     gnus-newsgroup-marked
1570     gnus-newsgroup-spam-marked
1571     gnus-newsgroup-reads
1572     gnus-newsgroup-saved
1573     gnus-newsgroup-replied
1574     gnus-newsgroup-forwarded
1575     gnus-newsgroup-expirable
1576     gnus-newsgroup-killed
1577     gnus-newsgroup-unseen
1578     gnus-newsgroup-seen
1579     gnus-newsgroup-cached
1580     gnus-newsgroup-downloadable
1581     gnus-newsgroup-undownloaded
1582     gnus-newsgroup-unsendable
1583
1584     gnus-newsgroup-begin gnus-newsgroup-end
1585     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1586     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1587     gnus-newsgroup-last-directory
1588     gnus-newsgroup-auto-expire
1589     gnus-newsgroup-processable
1590     gnus-newsgroup-unfetched
1591     gnus-newsgroup-articles
1592     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1593     gnus-newsgroup-headers gnus-newsgroup-threads
1594     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1595     gnus-current-article gnus-current-headers gnus-have-all-headers
1596     gnus-last-article gnus-article-internal-prepare-hook
1597     (gnus-summary-article-delete-hook . global)
1598     (gnus-summary-article-move-hook . global)
1599     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1600     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1601     gnus-thread-expunge-below
1602     gnus-score-alist gnus-current-score-file
1603     (gnus-summary-expunge-below . global)
1604     (gnus-summary-mark-below . global)
1605     (gnus-orphan-score . global)
1606     gnus-newsgroup-active gnus-scores-exclude-files
1607     gnus-newsgroup-highest
1608     gnus-newsgroup-history gnus-newsgroup-ancient
1609     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1610     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1611     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1612     (gnus-newsgroup-expunged-tally . 0)
1613     gnus-cache-removable-articles
1614     gnus-newsgroup-data gnus-newsgroup-data-reverse
1615     gnus-newsgroup-limit gnus-newsgroup-limits
1616     gnus-newsgroup-charset gnus-newsgroup-display
1617     gnus-summary-use-undownloaded-faces)
1618   "Variables that are buffer-local to the summary buffers.")
1619
1620 (defvar gnus-newsgroup-variables nil
1621   "A list of variables that have separate values in different newsgroups.
1622 A list of newsgroup (summary buffer) local variables, or cons of
1623 variables and their default expressions to be evalled (when the default
1624 values are not nil), that should be made global while the summary buffer
1625 is active.
1626
1627 Note: The default expressions will be evaluated (using function `eval')
1628 before assignment to the local variable rather than just assigned to it.
1629 If the default expression is the symbol `global', that symbol will not
1630 be evaluated but the global value of the local variable will be used
1631 instead.
1632
1633 These variables can be used to set variables in the group parameters
1634 while still allowing them to affect operations done in other buffers.
1635 For example:
1636
1637 \(setq gnus-newsgroup-variables
1638      '(message-use-followup-to
1639        (gnus-visible-headers .
1640          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1641 ")
1642
1643 (eval-when-compile
1644   ;; Bind features so that require will believe that gnus-sum has
1645   ;; already been loaded (avoids infinite recursion)
1646   (let ((features (cons 'gnus-sum features)))
1647     (require 'gnus-art)))
1648
1649 ;; MIME stuff.
1650
1651 (defvar gnus-decode-encoded-word-methods
1652   '(mail-decode-encoded-word-string)
1653   "List of methods used to decode encoded words.
1654
1655 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1656 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1657 \(REGEXP . FUNCTION), FUNCTION will be applied only to the newsgroups
1658 whose names match REGEXP.
1659
1660 For example:
1661 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1662  mail-decode-encoded-word-string
1663  (\"chinese\" . rfc1843-decode-string))")
1664
1665 (defvar gnus-decode-encoded-word-methods-cache nil)
1666
1667 (defun gnus-multi-decode-encoded-word-string (string)
1668   "Apply the functions from `gnus-encoded-word-methods' that match."
1669   (unless (and gnus-decode-encoded-word-methods-cache
1670                (eq gnus-newsgroup-name
1671                    (car gnus-decode-encoded-word-methods-cache)))
1672     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1673     (dolist (method gnus-decode-encoded-word-methods)
1674       (if (symbolp method)
1675           (nconc gnus-decode-encoded-word-methods-cache (list method))
1676         (if (and gnus-newsgroup-name
1677                  (string-match (car method) gnus-newsgroup-name))
1678             (nconc gnus-decode-encoded-word-methods-cache
1679                    (list (cdr method)))))))
1680   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1681     (setq string (funcall method string))))
1682
1683 ;; Subject simplification.
1684
1685 (defun gnus-simplify-whitespace (str)
1686   "Remove excessive whitespace from STR."
1687   ;; Multiple spaces.
1688   (while (string-match "[ \t][ \t]+" str)
1689     (setq str (concat (substring str 0 (match-beginning 0))
1690                         " "
1691                         (substring str (match-end 0)))))
1692   ;; Leading spaces.
1693   (when (string-match "^[ \t]+" str)
1694     (setq str (substring str (match-end 0))))
1695   ;; Trailing spaces.
1696   (when (string-match "[ \t]+$" str)
1697     (setq str (substring str 0 (match-beginning 0))))
1698   str)
1699
1700 (defun gnus-simplify-all-whitespace (str)
1701   "Remove all whitespace from STR."
1702   (while (string-match "[ \t\n]+" str)
1703     (setq str (replace-match "" nil nil str)))
1704   str)
1705
1706 (defsubst gnus-simplify-subject-re (subject)
1707   "Remove \"Re:\" from subject lines."
1708   (if (string-match message-subject-re-regexp subject)
1709       (substring subject (match-end 0))
1710     subject))
1711
1712 (defun gnus-simplify-subject (subject &optional re-only)
1713   "Remove `Re:' and words in parentheses.
1714 If RE-ONLY is non-nil, strip leading `Re:'s only."
1715   (let ((case-fold-search t))           ;Ignore case.
1716     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1717     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1718       (setq subject (substring subject (match-end 0))))
1719     ;; Remove uninteresting prefixes.
1720     (when (and (not re-only)
1721                gnus-simplify-ignored-prefixes
1722                (string-match gnus-simplify-ignored-prefixes subject))
1723       (setq subject (substring subject (match-end 0))))
1724     ;; Remove words in parentheses from end.
1725     (unless re-only
1726       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1727         (setq subject (substring subject 0 (match-beginning 0)))))
1728     ;; Return subject string.
1729     subject))
1730
1731 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1732 ;; all whitespace.
1733 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1734   (goto-char (point-min))
1735   (while (re-search-forward regexp nil t)
1736     (replace-match (or newtext ""))))
1737
1738 (defun gnus-simplify-buffer-fuzzy (regexp)
1739   "Simplify string in the buffer fuzzily.
1740 The string in the accessible portion of the current buffer is simplified.
1741 It is assumed to be a single-line subject.
1742 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1743 matter is removed.  Additional things can be deleted by setting
1744 `gnus-simplify-subject-fuzzy-regexp'."
1745   (let ((case-fold-search t)
1746         (modified-tick))
1747     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1748
1749     (while (not (eq modified-tick (buffer-modified-tick)))
1750       (setq modified-tick (buffer-modified-tick))
1751       (cond
1752        ((listp regexp)
1753         (mapc 'gnus-simplify-buffer-fuzzy-step regexp))
1754        (regexp
1755         (gnus-simplify-buffer-fuzzy-step regexp)))
1756       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1757       (gnus-simplify-buffer-fuzzy-step
1758        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1759       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1760
1761     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1762     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1763     (gnus-simplify-buffer-fuzzy-step " $")
1764     (gnus-simplify-buffer-fuzzy-step "^ +")))
1765
1766 (defun gnus-simplify-subject-fuzzy (subject)
1767   "Simplify a subject string fuzzily.
1768 See `gnus-simplify-buffer-fuzzy' for details."
1769   (save-excursion
1770     (let ((regexp gnus-simplify-subject-fuzzy-regexp))
1771       (gnus-set-work-buffer)
1772       (let ((case-fold-search t))
1773         ;; Remove uninteresting prefixes.
1774         (when (and gnus-simplify-ignored-prefixes
1775                    (string-match gnus-simplify-ignored-prefixes subject))
1776           (setq subject (substring subject (match-end 0))))
1777         (insert subject)
1778         (inline (gnus-simplify-buffer-fuzzy regexp))
1779         (buffer-string)))))
1780
1781 (defsubst gnus-simplify-subject-fully (subject)
1782   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1783   (cond
1784    (gnus-simplify-subject-functions
1785     (gnus-map-function gnus-simplify-subject-functions subject))
1786    ((null gnus-summary-gather-subject-limit)
1787     (gnus-simplify-subject-re subject))
1788    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1789     (gnus-simplify-subject-fuzzy subject))
1790    ((numberp gnus-summary-gather-subject-limit)
1791     (truncate-string-to-width (gnus-simplify-subject-re subject)
1792                               gnus-summary-gather-subject-limit))
1793    (t
1794     subject)))
1795
1796 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1797   "Check whether two subjects are equal.
1798 If optional argument SIMPLE-FIRST is t, first argument is already
1799 simplified."
1800   (cond
1801    ((null simple-first)
1802     (equal (gnus-simplify-subject-fully s1)
1803            (gnus-simplify-subject-fully s2)))
1804    (t
1805     (equal s1
1806            (gnus-simplify-subject-fully s2)))))
1807
1808 (defun gnus-summary-bubble-group ()
1809   "Increase the score of the current group.
1810 This is a handy function to add to `gnus-summary-exit-hook' to
1811 increase the score of each group you read."
1812   (gnus-group-add-score gnus-newsgroup-name))
1813
1814 \f
1815 ;;;
1816 ;;; Gnus summary mode
1817 ;;;
1818
1819 (put 'gnus-summary-mode 'mode-class 'special)
1820
1821 (defvar gnus-article-commands-menu)
1822
1823 ;; Non-orthogonal keys
1824
1825 (gnus-define-keys gnus-summary-mode-map
1826   " " gnus-summary-next-page
1827   "\177" gnus-summary-prev-page
1828   [delete] gnus-summary-prev-page
1829   [backspace] gnus-summary-prev-page
1830   "\r" gnus-summary-scroll-up
1831   "\M-\r" gnus-summary-scroll-down
1832   "n" gnus-summary-next-unread-article
1833   "p" gnus-summary-prev-unread-article
1834   "N" gnus-summary-next-article
1835   "P" gnus-summary-prev-article
1836   "\M-\C-n" gnus-summary-next-same-subject
1837   "\M-\C-p" gnus-summary-prev-same-subject
1838   "\M-n" gnus-summary-next-unread-subject
1839   "\M-p" gnus-summary-prev-unread-subject
1840   "." gnus-summary-first-unread-article
1841   "," gnus-summary-best-unread-article
1842   "\M-s" gnus-summary-search-article-forward
1843   "\M-r" gnus-summary-search-article-backward
1844   "\M-S" gnus-summary-repeat-search-article-forward
1845   "\M-R" gnus-summary-repeat-search-article-backward
1846   "<" gnus-summary-beginning-of-article
1847   ">" gnus-summary-end-of-article
1848   "j" gnus-summary-goto-article
1849   "^" gnus-summary-refer-parent-article
1850   "\M-^" gnus-summary-refer-article
1851   "u" gnus-summary-tick-article-forward
1852   "!" gnus-summary-tick-article-forward
1853   "U" gnus-summary-tick-article-backward
1854   "d" gnus-summary-mark-as-read-forward
1855   "D" gnus-summary-mark-as-read-backward
1856   "E" gnus-summary-mark-as-expirable
1857   "\M-u" gnus-summary-clear-mark-forward
1858   "\M-U" gnus-summary-clear-mark-backward
1859   "k" gnus-summary-kill-same-subject-and-select
1860   "\C-k" gnus-summary-kill-same-subject
1861   "\M-\C-k" gnus-summary-kill-thread
1862   "\M-\C-l" gnus-summary-lower-thread
1863   "e" gnus-summary-edit-article
1864   "#" gnus-summary-mark-as-processable
1865   "\M-#" gnus-summary-unmark-as-processable
1866   "\M-\C-t" gnus-summary-toggle-threads
1867   "\M-\C-s" gnus-summary-show-thread
1868   "\M-\C-h" gnus-summary-hide-thread
1869   "\M-\C-f" gnus-summary-next-thread
1870   "\M-\C-b" gnus-summary-prev-thread
1871   [(meta down)] gnus-summary-next-thread
1872   [(meta up)] gnus-summary-prev-thread
1873   "\M-\C-u" gnus-summary-up-thread
1874   "\M-\C-d" gnus-summary-down-thread
1875   "&" gnus-summary-execute-command
1876   "c" gnus-summary-catchup-and-exit
1877   "\C-w" gnus-summary-mark-region-as-read
1878   "\C-t" gnus-summary-toggle-truncation
1879   "?" gnus-summary-mark-as-dormant
1880   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1881   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1882   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1883   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1884   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1885   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1886   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1887   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1888   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1889   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1890   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1891   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1892   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1893   "=" gnus-summary-expand-window
1894   "\C-x\C-s" gnus-summary-reselect-current-group
1895   "\M-g" gnus-summary-rescan-group
1896   "\C-c\C-r" gnus-summary-caesar-message
1897   "f" gnus-summary-followup
1898   "F" gnus-summary-followup-with-original
1899   "C" gnus-summary-cancel-article
1900   "r" gnus-summary-reply
1901   "R" gnus-summary-reply-with-original
1902   "\C-c\C-f" gnus-summary-mail-forward
1903   "o" gnus-summary-save-article
1904   "\C-o" gnus-summary-save-article-mail
1905   "|" gnus-summary-pipe-output
1906   "\M-k" gnus-summary-edit-local-kill
1907   "\M-K" gnus-summary-edit-global-kill
1908   ;; "V" gnus-version
1909   "\C-c\C-d" gnus-summary-describe-group
1910   "q" gnus-summary-exit
1911   "Q" gnus-summary-exit-no-update
1912   "\C-c\C-i" gnus-info-find-node
1913   gnus-mouse-2 gnus-mouse-pick-article
1914   [follow-link] mouse-face
1915   "m" gnus-summary-mail-other-window
1916   "a" gnus-summary-post-news
1917   "x" gnus-summary-limit-to-unread
1918   "s" gnus-summary-isearch-article
1919   [tab] gnus-summary-widget-forward
1920   "t" gnus-summary-toggle-header
1921   "g" gnus-summary-show-article
1922   "l" gnus-summary-goto-last-article
1923   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1924   "\C-d" gnus-summary-enter-digest-group
1925   "\M-\C-d" gnus-summary-read-document
1926   "\M-\C-e" gnus-summary-edit-parameters
1927   "\M-\C-a" gnus-summary-customize-parameters
1928   "\C-c\C-b" gnus-bug
1929   "*" gnus-cache-enter-article
1930   "\M-*" gnus-cache-remove-article
1931   "\M-&" gnus-summary-universal-argument
1932   "\C-l" gnus-recenter
1933   "I" gnus-summary-increase-score
1934   "L" gnus-summary-lower-score
1935   "\M-i" gnus-symbolic-argument
1936   "h" gnus-summary-select-article-buffer
1937
1938   "b" gnus-article-view-part
1939   "\M-t" gnus-summary-toggle-display-buttonized
1940
1941   "V" gnus-summary-score-map
1942   "X" gnus-uu-extract-map
1943   "S" gnus-summary-send-map)
1944
1945 ;; Sort of orthogonal keymap
1946 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1947   "t" gnus-summary-tick-article-forward
1948   "!" gnus-summary-tick-article-forward
1949   "d" gnus-summary-mark-as-read-forward
1950   "r" gnus-summary-mark-as-read-forward
1951   "c" gnus-summary-clear-mark-forward
1952   " " gnus-summary-clear-mark-forward
1953   "e" gnus-summary-mark-as-expirable
1954   "x" gnus-summary-mark-as-expirable
1955   "?" gnus-summary-mark-as-dormant
1956   "b" gnus-summary-set-bookmark
1957   "B" gnus-summary-remove-bookmark
1958   "#" gnus-summary-mark-as-processable
1959   "\M-#" gnus-summary-unmark-as-processable
1960   "S" gnus-summary-limit-include-expunged
1961   "C" gnus-summary-catchup
1962   "H" gnus-summary-catchup-to-here
1963   "h" gnus-summary-catchup-from-here
1964   "\C-c" gnus-summary-catchup-all
1965   "k" gnus-summary-kill-same-subject-and-select
1966   "K" gnus-summary-kill-same-subject
1967   "P" gnus-uu-mark-map)
1968
1969 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1970   "c" gnus-summary-clear-above
1971   "u" gnus-summary-tick-above
1972   "m" gnus-summary-mark-above
1973   "k" gnus-summary-kill-below)
1974
1975 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1976   "/" gnus-summary-limit-to-subject
1977   "n" gnus-summary-limit-to-articles
1978   "b" gnus-summary-limit-to-bodies
1979   "h" gnus-summary-limit-to-headers
1980   "w" gnus-summary-pop-limit
1981   "s" gnus-summary-limit-to-subject
1982   "a" gnus-summary-limit-to-author
1983   "u" gnus-summary-limit-to-unread
1984   "m" gnus-summary-limit-to-marks
1985   "M" gnus-summary-limit-exclude-marks
1986   "v" gnus-summary-limit-to-score
1987   "*" gnus-summary-limit-include-cached
1988   "D" gnus-summary-limit-include-dormant
1989   "T" gnus-summary-limit-include-thread
1990   "d" gnus-summary-limit-exclude-dormant
1991   "t" gnus-summary-limit-to-age
1992   "." gnus-summary-limit-to-unseen
1993   "x" gnus-summary-limit-to-extra
1994   "p" gnus-summary-limit-to-display-predicate
1995   "E" gnus-summary-limit-include-expunged
1996   "c" gnus-summary-limit-exclude-childless-dormant
1997   "C" gnus-summary-limit-mark-excluded-as-read
1998   "o" gnus-summary-insert-old-articles
1999   "N" gnus-summary-insert-new-articles
2000   "S" gnus-summary-limit-to-singletons
2001   "r" gnus-summary-limit-to-replied
2002   "R" gnus-summary-limit-to-recipient
2003   "A" gnus-summary-limit-to-address)
2004
2005 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
2006   "n" gnus-summary-next-unread-article
2007   "p" gnus-summary-prev-unread-article
2008   "N" gnus-summary-next-article
2009   "P" gnus-summary-prev-article
2010   "\C-n" gnus-summary-next-same-subject
2011   "\C-p" gnus-summary-prev-same-subject
2012   "\M-n" gnus-summary-next-unread-subject
2013   "\M-p" gnus-summary-prev-unread-subject
2014   "f" gnus-summary-first-unread-article
2015   "b" gnus-summary-best-unread-article
2016   "j" gnus-summary-goto-article
2017   "g" gnus-summary-goto-subject
2018   "l" gnus-summary-goto-last-article
2019   "o" gnus-summary-pop-article)
2020
2021 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2022   "k" gnus-summary-kill-thread
2023   "E" gnus-summary-expire-thread
2024   "l" gnus-summary-lower-thread
2025   "i" gnus-summary-raise-thread
2026   "T" gnus-summary-toggle-threads
2027   "t" gnus-summary-rethread-current
2028   "^" gnus-summary-reparent-thread
2029   "\M-^" gnus-summary-reparent-children
2030   "s" gnus-summary-show-thread
2031   "S" gnus-summary-show-all-threads
2032   "h" gnus-summary-hide-thread
2033   "H" gnus-summary-hide-all-threads
2034   "n" gnus-summary-next-thread
2035   "p" gnus-summary-prev-thread
2036   "u" gnus-summary-up-thread
2037   "o" gnus-summary-top-thread
2038   "d" gnus-summary-down-thread
2039   "#" gnus-uu-mark-thread
2040   "\M-#" gnus-uu-unmark-thread)
2041
2042 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2043   "g" gnus-summary-prepare
2044   "c" gnus-summary-insert-cached-articles
2045   "d" gnus-summary-insert-dormant-articles
2046   "t" gnus-summary-insert-ticked-articles)
2047
2048 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2049   "c" gnus-summary-catchup-and-exit
2050   "C" gnus-summary-catchup-all-and-exit
2051   "E" gnus-summary-exit-no-update
2052   "Q" gnus-summary-exit
2053   "Z" gnus-summary-exit
2054   "n" gnus-summary-catchup-and-goto-next-group
2055   "p" gnus-summary-catchup-and-goto-prev-group
2056   "R" gnus-summary-reselect-current-group
2057   "G" gnus-summary-rescan-group
2058   "N" gnus-summary-next-group
2059   "s" gnus-summary-save-newsrc
2060   "P" gnus-summary-prev-group)
2061
2062 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2063   " " gnus-summary-next-page
2064   "n" gnus-summary-next-page
2065   "\177" gnus-summary-prev-page
2066   [delete] gnus-summary-prev-page
2067   "p" gnus-summary-prev-page
2068   "\r" gnus-summary-scroll-up
2069   "\M-\r" gnus-summary-scroll-down
2070   "<" gnus-summary-beginning-of-article
2071   ">" gnus-summary-end-of-article
2072   "b" gnus-summary-beginning-of-article
2073   "e" gnus-summary-end-of-article
2074   "^" gnus-summary-refer-parent-article
2075   "r" gnus-summary-refer-parent-article
2076   "C" gnus-summary-show-complete-article
2077   "D" gnus-summary-enter-digest-group
2078   "R" gnus-summary-refer-references
2079   "T" gnus-summary-refer-thread
2080   "W" gnus-warp-to-article
2081   "g" gnus-summary-show-article
2082   "s" gnus-summary-isearch-article
2083   [tab] gnus-summary-widget-forward
2084   "P" gnus-summary-print-article
2085   "S" gnus-sticky-article
2086   "M" gnus-mailing-list-insinuate
2087   "t" gnus-article-babel)
2088
2089 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2090   "b" gnus-article-add-buttons
2091   "B" gnus-article-add-buttons-to-head
2092   "o" gnus-article-treat-overstrike
2093   "e" gnus-article-emphasize
2094   "w" gnus-article-fill-cited-article
2095   "Q" gnus-article-fill-long-lines
2096   "L" gnus-article-toggle-truncate-lines
2097   "C" gnus-article-capitalize-sentences
2098   "c" gnus-article-remove-cr
2099   "q" gnus-article-de-quoted-unreadable
2100   "6" gnus-article-de-base64-unreadable
2101   "Z" gnus-article-decode-HZ
2102   "A" gnus-article-treat-ansi-sequences
2103   "h" gnus-article-wash-html
2104   "u" gnus-article-unsplit-urls
2105   "s" gnus-summary-force-verify-and-decrypt
2106   "f" gnus-article-display-x-face
2107   "l" gnus-summary-stop-page-breaking
2108   "r" gnus-summary-caesar-message
2109   "m" gnus-summary-morse-message
2110   "t" gnus-summary-toggle-header
2111   "g" gnus-treat-smiley
2112   "v" gnus-summary-verbose-headers
2113   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2114   "p" gnus-article-verify-x-pgp-sig
2115   "d" gnus-article-treat-dumbquotes
2116   "U" gnus-article-treat-non-ascii
2117   "i" gnus-summary-idna-message)
2118
2119 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2120   ;; mnemonic: deuglif*Y*
2121   "u" gnus-article-outlook-unwrap-lines
2122   "a" gnus-article-outlook-repair-attribution
2123   "c" gnus-article-outlook-rearrange-citation
2124   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2125
2126 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2127   "a" gnus-article-hide
2128   "h" gnus-article-hide-headers
2129   "b" gnus-article-hide-boring-headers
2130   "s" gnus-article-hide-signature
2131   "c" gnus-article-hide-citation
2132   "C" gnus-article-hide-citation-in-followups
2133   "l" gnus-article-hide-list-identifiers
2134   "B" gnus-article-strip-banner
2135   "P" gnus-article-hide-pem
2136   "\C-c" gnus-article-hide-citation-maybe)
2137
2138 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2139   "a" gnus-article-highlight
2140   "h" gnus-article-highlight-headers
2141   "c" gnus-article-highlight-citation
2142   "s" gnus-article-highlight-signature)
2143
2144 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2145   "f" gnus-article-treat-fold-headers
2146   "u" gnus-article-treat-unfold-headers
2147   "n" gnus-article-treat-fold-newsgroups)
2148
2149 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2150   "x" gnus-article-display-x-face
2151   "d" gnus-article-display-face
2152   "s" gnus-treat-smiley
2153   "D" gnus-article-remove-images
2154   "W" gnus-article-show-images
2155   "f" gnus-treat-from-picon
2156   "m" gnus-treat-mail-picon
2157   "n" gnus-treat-newsgroups-picon
2158   "g" gnus-treat-from-gravatar
2159   "h" gnus-treat-mail-gravatar)
2160
2161 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2162   "w" gnus-article-decode-mime-words
2163   "c" gnus-article-decode-charset
2164   "v" gnus-mime-view-all-parts
2165   "b" gnus-article-view-part)
2166
2167 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2168   "z" gnus-article-date-ut
2169   "u" gnus-article-date-ut
2170   "l" gnus-article-date-local
2171   "p" gnus-article-date-english
2172   "e" gnus-article-date-lapsed
2173   "o" gnus-article-date-original
2174   "i" gnus-article-date-iso8601
2175   "s" gnus-article-date-user)
2176
2177 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2178   "t" gnus-article-remove-trailing-blank-lines
2179   "l" gnus-article-strip-leading-blank-lines
2180   "m" gnus-article-strip-multiple-blank-lines
2181   "a" gnus-article-strip-blank-lines
2182   "A" gnus-article-strip-all-blank-lines
2183   "s" gnus-article-strip-leading-space
2184   "e" gnus-article-strip-trailing-space
2185   "w" gnus-article-remove-leading-whitespace)
2186
2187 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2188   "v" gnus-version
2189   "d" gnus-summary-describe-group
2190   "h" gnus-summary-describe-briefly
2191   "i" gnus-info-find-node)
2192
2193 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2194   "e" gnus-summary-expire-articles
2195   "\M-\C-e" gnus-summary-expire-articles-now
2196   "\177" gnus-summary-delete-article
2197   [delete] gnus-summary-delete-article
2198   [backspace] gnus-summary-delete-article
2199   "m" gnus-summary-move-article
2200   "r" gnus-summary-respool-article
2201   "w" gnus-summary-edit-article
2202   "c" gnus-summary-copy-article
2203   "B" gnus-summary-crosspost-article
2204   "q" gnus-summary-respool-query
2205   "t" gnus-summary-respool-trace
2206   "i" gnus-summary-import-article
2207   "I" gnus-summary-create-article
2208   "p" gnus-summary-article-posted-p)
2209
2210 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2211   "o" gnus-summary-save-article
2212   "m" gnus-summary-save-article-mail
2213   "F" gnus-summary-write-article-file
2214   "r" gnus-summary-save-article-rmail
2215   "f" gnus-summary-save-article-file
2216   "b" gnus-summary-save-article-body-file
2217   "B" gnus-summary-write-article-body-file
2218   "h" gnus-summary-save-article-folder
2219   "v" gnus-summary-save-article-vm
2220   "p" gnus-summary-pipe-output
2221   "P" gnus-summary-muttprint)
2222
2223 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2224   "b" gnus-summary-display-buttonized
2225   "m" gnus-summary-repair-multipart
2226   "v" gnus-article-view-part
2227   "o" gnus-article-save-part
2228   "O" gnus-article-save-part-and-strip
2229   "r" gnus-article-replace-part
2230   "d" gnus-article-delete-part
2231   "t" gnus-article-view-part-as-type
2232   "j" gnus-article-jump-to-part
2233   "c" gnus-article-copy-part
2234   "C" gnus-article-view-part-as-charset
2235   "e" gnus-article-view-part-externally
2236   "H" gnus-article-browse-html-article
2237   "E" gnus-article-encrypt-body
2238   "i" gnus-article-inline-part
2239   "|" gnus-article-pipe-part)
2240
2241 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2242   "p" gnus-summary-mark-as-processable
2243   "u" gnus-summary-unmark-as-processable
2244   "U" gnus-summary-unmark-all-processable
2245   "v" gnus-uu-mark-over
2246   "s" gnus-uu-mark-series
2247   "r" gnus-uu-mark-region
2248   "g" gnus-uu-unmark-region
2249   "R" gnus-uu-mark-by-regexp
2250   "G" gnus-uu-unmark-by-regexp
2251   "t" gnus-uu-mark-thread
2252   "T" gnus-uu-unmark-thread
2253   "a" gnus-uu-mark-all
2254   "b" gnus-uu-mark-buffer
2255   "S" gnus-uu-mark-sparse
2256   "k" gnus-summary-kill-process-mark
2257   "y" gnus-summary-yank-process-mark
2258   "w" gnus-summary-save-process-mark
2259   "i" gnus-uu-invert-processable)
2260
2261 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2262   ;;"x" gnus-uu-extract-any
2263   "m" gnus-summary-save-parts
2264   "u" gnus-uu-decode-uu
2265   "U" gnus-uu-decode-uu-and-save
2266   "s" gnus-uu-decode-unshar
2267   "S" gnus-uu-decode-unshar-and-save
2268   "o" gnus-uu-decode-save
2269   "O" gnus-uu-decode-save
2270   "b" gnus-uu-decode-binhex
2271   "B" gnus-uu-decode-binhex
2272   "Y" gnus-uu-decode-yenc
2273   "p" gnus-uu-decode-postscript
2274   "P" gnus-uu-decode-postscript-and-save)
2275
2276 (gnus-define-keys
2277     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2278   "u" gnus-uu-decode-uu-view
2279   "U" gnus-uu-decode-uu-and-save-view
2280   "s" gnus-uu-decode-unshar-view
2281   "S" gnus-uu-decode-unshar-and-save-view
2282   "o" gnus-uu-decode-save-view
2283   "O" gnus-uu-decode-save-view
2284   "b" gnus-uu-decode-binhex-view
2285   "B" gnus-uu-decode-binhex-view
2286   "p" gnus-uu-decode-postscript-view
2287   "P" gnus-uu-decode-postscript-and-save-view)
2288
2289 (defvar gnus-article-post-menu nil)
2290
2291 (defconst gnus-summary-menu-maxlen 20)
2292
2293 (defun gnus-summary-menu-split (menu)
2294   ;; If we have lots of elements, divide them into groups of 20
2295   ;; and make a pane (or submenu) for each one.
2296   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2297       (let ((menu menu) sublists next
2298             (i 1))
2299         (while menu
2300           ;; Pull off the next gnus-summary-menu-maxlen elements
2301           ;; and make them the next element of sublist.
2302           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2303           (if next
2304               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2305                       nil))
2306           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2307                                              (aref (car (last menu)) 0)) menu)
2308                                sublists))
2309           (setq i (1+ i))
2310           (setq menu next))
2311         (nreverse sublists))
2312     ;; Few elements--put them all in one pane.
2313     menu))
2314
2315 (defun gnus-summary-make-menu-bar ()
2316   (gnus-turn-off-edit-menu 'summary)
2317
2318   (unless (boundp 'gnus-summary-misc-menu)
2319
2320     (easy-menu-define
2321       gnus-summary-kill-menu gnus-summary-mode-map ""
2322       (cons
2323        "Score"
2324        (nconc
2325         (list
2326          ["Customize" gnus-score-customize t])
2327         (gnus-make-score-map 'increase)
2328         (gnus-make-score-map 'lower)
2329         '(("Mark"
2330            ["Kill below" gnus-summary-kill-below t]
2331            ["Mark above" gnus-summary-mark-above t]
2332            ["Tick above" gnus-summary-tick-above t]
2333            ["Clear above" gnus-summary-clear-above t])
2334           ["Current score" gnus-summary-current-score t]
2335           ["Set score" gnus-summary-set-score t]
2336           ["Switch current score file..." gnus-score-change-score-file t]
2337           ["Set mark below..." gnus-score-set-mark-below t]
2338           ["Set expunge below..." gnus-score-set-expunge-below t]
2339           ["Edit current score file" gnus-score-edit-current-scores t]
2340           ["Edit score file..." gnus-score-edit-file t]
2341           ["Trace score" gnus-score-find-trace t]
2342           ["Find words" gnus-score-find-favourite-words t]
2343           ["Rescore buffer" gnus-summary-rescore t]
2344           ["Increase score..." gnus-summary-increase-score t]
2345           ["Lower score..." gnus-summary-lower-score t]))))
2346
2347     ;; Define both the Article menu in the summary buffer and the
2348     ;; equivalent Commands menu in the article buffer here for
2349     ;; consistency.
2350     (let ((innards
2351            `(("Hide"
2352               ["All" gnus-article-hide t]
2353               ["Headers" gnus-article-hide-headers t]
2354               ["Signature" gnus-article-hide-signature t]
2355               ["Citation" gnus-article-hide-citation t]
2356               ["List identifiers" gnus-article-hide-list-identifiers t]
2357               ["Banner" gnus-article-strip-banner t]
2358               ["Boring headers" gnus-article-hide-boring-headers t])
2359              ("Highlight"
2360               ["All" gnus-article-highlight t]
2361               ["Headers" gnus-article-highlight-headers t]
2362               ["Signature" gnus-article-highlight-signature t]
2363               ["Citation" gnus-article-highlight-citation t])
2364              ("MIME"
2365               ["Words" gnus-article-decode-mime-words t]
2366               ["Charset" gnus-article-decode-charset t]
2367               ["QP" gnus-article-de-quoted-unreadable t]
2368               ["Base64" gnus-article-de-base64-unreadable t]
2369               ["View MIME buttons" gnus-summary-display-buttonized t]
2370               ["View all" gnus-mime-view-all-parts t]
2371               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2372               ["Encrypt body" gnus-article-encrypt-body
2373                :active (not (gnus-group-read-only-p))
2374                ,@(if (featurep 'xemacs) nil
2375                    '(:help "Encrypt the message body on disk"))]
2376               ["Extract all parts..." gnus-summary-save-parts t]
2377               ("Multipart"
2378                ["Repair multipart" gnus-summary-repair-multipart t]
2379                ["Pipe part..." gnus-article-pipe-part t]
2380                ["Inline part" gnus-article-inline-part t]
2381                ["View part as type..." gnus-article-view-part-as-type t]
2382                ["Encrypt body" gnus-article-encrypt-body
2383                 :active (not (gnus-group-read-only-p))
2384                ,@(if (featurep 'xemacs) nil
2385                    '(:help "Encrypt the message body on disk"))]
2386                ["View part externally" gnus-article-view-part-externally t]
2387                ["View HTML parts in browser" gnus-article-browse-html-article t]
2388                ["View part with charset..." gnus-article-view-part-as-charset t]
2389                ["Copy part" gnus-article-copy-part t]
2390                ["Save part..." gnus-article-save-part t]
2391                ["View part" gnus-article-view-part t]))
2392              ("Date"
2393               ["Local" gnus-article-date-local t]
2394               ["ISO8601" gnus-article-date-iso8601 t]
2395               ["UT" gnus-article-date-ut t]
2396               ["Original" gnus-article-date-original t]
2397               ["Lapsed" gnus-article-date-lapsed t]
2398               ["User-defined" gnus-article-date-user t])
2399              ("Display"
2400               ["Remove images" gnus-article-remove-images t]
2401               ["Toggle smiley" gnus-treat-smiley t]
2402               ["Show X-Face" gnus-article-display-x-face t]
2403               ["Show picons in From" gnus-treat-from-picon t]
2404               ["Show picons in mail headers" gnus-treat-mail-picon t]
2405               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2406               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2407               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2408               ("View as different encoding"
2409                ,@(gnus-summary-menu-split
2410                   (mapcar
2411                    (lambda (cs)
2412                      ;; Since easymenu under Emacs doesn't allow
2413                      ;; lambda forms for menu commands, we should
2414                      ;; provide intern'ed function symbols.
2415                      (let ((command (intern (format "\
2416 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2417                        (fset command
2418                              `(lambda ()
2419                                 (interactive)
2420                                 (let ((gnus-summary-show-article-charset-alist
2421                                        '((1 . ,cs))))
2422                                   (gnus-summary-show-article 1))))
2423                        `[,(symbol-name cs) ,command t]))
2424                    (sort (if (fboundp 'coding-system-list)
2425                              (coding-system-list)
2426                            (mapcar 'car mm-mime-mule-charset-alist))
2427                          'string<)))))
2428              ("Washing"
2429               ("Remove Blanks"
2430                ["Leading" gnus-article-strip-leading-blank-lines t]
2431                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2432                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2433                ["All of the above" gnus-article-strip-blank-lines t]
2434                ["All" gnus-article-strip-all-blank-lines t]
2435                ["Leading space" gnus-article-strip-leading-space t]
2436                ["Trailing space" gnus-article-strip-trailing-space t]
2437                ["Leading space in headers"
2438                 gnus-article-remove-leading-whitespace t])
2439               ["Overstrike" gnus-article-treat-overstrike t]
2440               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2441               ["Non-ASCII" gnus-article-treat-non-ascii t]
2442               ["Emphasis" gnus-article-emphasize t]
2443               ["Word wrap" gnus-article-fill-cited-article t]
2444               ["Fill long lines" gnus-article-fill-long-lines t]
2445               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2446               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2447               ["Remove CR" gnus-article-remove-cr t]
2448               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2449               ["Base64" gnus-article-de-base64-unreadable t]
2450               ["Rot 13" gnus-summary-caesar-message
2451                ,@(if (featurep 'xemacs) '(t)
2452                    '(:help "\"Caesar rotate\" article by 13"))]
2453               ["De-IDNA" gnus-summary-idna-message t]
2454               ["Morse decode" gnus-summary-morse-message t]
2455               ["Unix pipe..." gnus-summary-pipe-message t]
2456               ["Add buttons" gnus-article-add-buttons t]
2457               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2458               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2459               ["Verbose header" gnus-summary-verbose-headers t]
2460               ["Toggle header" gnus-summary-toggle-header t]
2461               ["Unfold headers" gnus-article-treat-unfold-headers t]
2462               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2463               ["Html" gnus-article-wash-html t]
2464               ["Unsplit URLs" gnus-article-unsplit-urls t]
2465               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2466               ["Decode HZ" gnus-article-decode-HZ t]
2467               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2468               ("(Outlook) Deuglify"
2469                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2470                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2471                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2472                ["Full (Outlook) deuglify"
2473                 gnus-article-outlook-deuglify-article t])
2474               )
2475              ("Output"
2476               ["Save in default format..." gnus-summary-save-article
2477                ,@(if (featurep 'xemacs) '(t)
2478                    '(:help "Save article using default method"))]
2479               ["Save in file..." gnus-summary-save-article-file
2480                ,@(if (featurep 'xemacs) '(t)
2481                    '(:help "Save article in file"))]
2482               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2483               ["Save in MH folder..." gnus-summary-save-article-folder t]
2484               ["Save in VM folder..." gnus-summary-save-article-vm t]
2485               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2486               ["Save body in file..." gnus-summary-save-article-body-file t]
2487               ["Pipe through a filter..." gnus-summary-pipe-output t]
2488               ["Print with Muttprint..." gnus-summary-muttprint t]
2489               ["Print" gnus-summary-print-article
2490                ,@(if (featurep 'xemacs) '(t)
2491                    '(:help "Generate and print a PostScript image"))])
2492              ("Copy, move,... (Backend)"
2493               ,@(if (featurep 'xemacs) nil
2494                   '(:help "Copying, moving, expiring articles..."))
2495               ["Respool article..." gnus-summary-respool-article t]
2496               ["Move article..." gnus-summary-move-article
2497                (gnus-check-backend-function
2498                 'request-move-article gnus-newsgroup-name)]
2499               ["Copy article..." gnus-summary-copy-article t]
2500               ["Crosspost article..." gnus-summary-crosspost-article
2501                (gnus-check-backend-function
2502                 'request-replace-article gnus-newsgroup-name)]
2503               ["Import file..." gnus-summary-import-article
2504                (gnus-check-backend-function
2505                 'request-accept-article gnus-newsgroup-name)]
2506               ["Create article..." gnus-summary-create-article
2507                (gnus-check-backend-function
2508                 'request-accept-article gnus-newsgroup-name)]
2509               ["Check if posted" gnus-summary-article-posted-p t]
2510               ["Edit article" gnus-summary-edit-article
2511                (not (gnus-group-read-only-p))]
2512               ["Delete article" gnus-summary-delete-article
2513                (gnus-check-backend-function
2514                 'request-expire-articles gnus-newsgroup-name)]
2515               ["Query respool" gnus-summary-respool-query t]
2516               ["Trace respool" gnus-summary-respool-trace t]
2517               ["Delete expirable articles" gnus-summary-expire-articles-now
2518                (gnus-check-backend-function
2519                 'request-expire-articles gnus-newsgroup-name)])
2520              ("Extract"
2521               ["Uudecode" gnus-uu-decode-uu
2522                ,@(if (featurep 'xemacs) '(t)
2523                    '(:help "Decode uuencoded article(s)"))]
2524               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2525               ["Unshar" gnus-uu-decode-unshar t]
2526               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2527               ["Save" gnus-uu-decode-save t]
2528               ["Binhex" gnus-uu-decode-binhex t]
2529               ["PostScript" gnus-uu-decode-postscript t]
2530               ["All MIME parts" gnus-summary-save-parts t])
2531              ("Cache"
2532               ["Enter article" gnus-cache-enter-article t]
2533               ["Remove article" gnus-cache-remove-article t])
2534              ["Translate" gnus-article-babel t]
2535              ["Select article buffer" gnus-summary-select-article-buffer t]
2536              ["Make article buffer sticky" gnus-sticky-article t]
2537              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2538              ["Isearch article..." gnus-summary-isearch-article t]
2539              ["Beginning of the article" gnus-summary-beginning-of-article t]
2540              ["End of the article" gnus-summary-end-of-article t]
2541              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2542              ["Fetch referenced articles" gnus-summary-refer-references t]
2543              ["Fetch current thread" gnus-summary-refer-thread t]
2544              ["Fetch article with id..." gnus-summary-refer-article t]
2545              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2546              ["Redisplay" gnus-summary-show-article t]
2547              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2548       (easy-menu-define
2549         gnus-summary-article-menu gnus-summary-mode-map ""
2550         (cons "Article" innards))
2551
2552       (if (not (keymapp gnus-summary-article-menu))
2553           (easy-menu-define
2554             gnus-article-commands-menu gnus-article-mode-map ""
2555             (cons "Commands" innards))
2556         ;; in Emacs, don't share menu.
2557         (setq gnus-article-commands-menu
2558               (copy-keymap gnus-summary-article-menu))
2559         (define-key gnus-article-mode-map [menu-bar commands]
2560           (cons "Commands" gnus-article-commands-menu))))
2561
2562     (easy-menu-define
2563       gnus-summary-thread-menu gnus-summary-mode-map ""
2564       '("Threads"
2565         ["Find all messages in thread" gnus-summary-refer-thread t]
2566         ["Toggle threading" gnus-summary-toggle-threads t]
2567         ["Hide threads" gnus-summary-hide-all-threads t]
2568         ["Show threads" gnus-summary-show-all-threads t]
2569         ["Hide thread" gnus-summary-hide-thread t]
2570         ["Show thread" gnus-summary-show-thread t]
2571         ["Go to next thread" gnus-summary-next-thread t]
2572         ["Go to previous thread" gnus-summary-prev-thread t]
2573         ["Go down thread" gnus-summary-down-thread t]
2574         ["Go up thread" gnus-summary-up-thread t]
2575         ["Top of thread" gnus-summary-top-thread t]
2576         ["Mark thread as read" gnus-summary-kill-thread t]
2577         ["Mark thread as expired" gnus-summary-expire-thread t]
2578         ["Lower thread score" gnus-summary-lower-thread t]
2579         ["Raise thread score" gnus-summary-raise-thread t]
2580         ["Rethread current" gnus-summary-rethread-current t]))
2581
2582     (easy-menu-define
2583       gnus-summary-post-menu gnus-summary-mode-map ""
2584       `("Post"
2585         ["Send a message (mail or news)" gnus-summary-post-news
2586          ,@(if (featurep 'xemacs) '(t)
2587              '(:help "Compose a new message (mail or news)"))]
2588         ["Followup" gnus-summary-followup
2589          ,@(if (featurep 'xemacs) '(t)
2590              '(:help "Post followup to this article"))]
2591         ["Followup and yank" gnus-summary-followup-with-original
2592          ,@(if (featurep 'xemacs) '(t)
2593              '(:help "Post followup to this article, quoting its contents"))]
2594         ["Supersede article" gnus-summary-supersede-article t]
2595         ["Cancel article" gnus-summary-cancel-article
2596          ,@(if (featurep 'xemacs) '(t)
2597              '(:help "Cancel an article you posted"))]
2598         ["Reply" gnus-summary-reply t]
2599         ["Reply and yank" gnus-summary-reply-with-original t]
2600         ["Wide reply" gnus-summary-wide-reply t]
2601         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2602          ,@(if (featurep 'xemacs) '(t)
2603              '(:help "Mail a reply, quoting this article"))]
2604         ["Very wide reply" gnus-summary-very-wide-reply t]
2605         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2606          ,@(if (featurep 'xemacs) '(t)
2607              '(:help "Mail a very wide reply, quoting this article"))]
2608         ["Mail forward" gnus-summary-mail-forward t]
2609         ["Post forward" gnus-summary-post-forward t]
2610         ["Digest and mail" gnus-uu-digest-mail-forward t]
2611         ["Digest and post" gnus-uu-digest-post-forward t]
2612         ["Resend message" gnus-summary-resend-message t]
2613         ["Resend message edit" gnus-summary-resend-message-edit t]
2614         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2615         ["Send a mail" gnus-summary-mail-other-window t]
2616         ["Create a local message" gnus-summary-news-other-window t]
2617         ["Uuencode and post" gnus-uu-post-news
2618          ,@(if (featurep 'xemacs) '(t)
2619              '(:help "Post a uuencoded article"))]
2620         ["Followup via news" gnus-summary-followup-to-mail t]
2621         ["Followup via news and yank"
2622          gnus-summary-followup-to-mail-with-original t]
2623         ["Strip signature on reply"
2624          (lambda ()
2625            (interactive)
2626            (if (not (memq message-cite-function
2627                           '(message-cite-original-without-signature
2628                             message-cite-original)))
2629                ;; Stupid workaround for XEmacs not honoring :visible.
2630                (message "Can't toggle this value of `message-cite-function'")
2631              (setq message-cite-function
2632                    (if (eq message-cite-function
2633                            'message-cite-original-without-signature)
2634                        'message-cite-original
2635                      'message-cite-original-without-signature))))
2636          ;; XEmacs barfs on :visible.
2637          ,@(if (featurep 'xemacs) nil
2638              '(:visible (memq message-cite-function
2639                               '(message-cite-original-without-signature
2640                                 message-cite-original))))
2641          :style toggle
2642          :selected (eq message-cite-function
2643                        'message-cite-original-without-signature)
2644          ,@(if (featurep 'xemacs) nil
2645              '(:help "Strip signature from cited article when replying."))]
2646         ;;("Draft"
2647         ;;["Send" gnus-summary-send-draft t]
2648         ;;["Send bounced" gnus-resend-bounced-mail t])
2649         ))
2650
2651     (cond
2652      ((not (keymapp gnus-summary-post-menu))
2653       (setq gnus-article-post-menu gnus-summary-post-menu))
2654      ((not gnus-article-post-menu)
2655       ;; Don't share post menu.
2656       (setq gnus-article-post-menu
2657             (copy-keymap gnus-summary-post-menu))))
2658     (define-key gnus-article-mode-map [menu-bar post]
2659       (cons "Post" gnus-article-post-menu))
2660
2661     (easy-menu-define
2662       gnus-summary-misc-menu gnus-summary-mode-map ""
2663       `("Gnus"
2664         ("Mark Read"
2665          ["Mark as read" gnus-summary-mark-as-read-forward t]
2666          ["Mark same subject and select"
2667           gnus-summary-kill-same-subject-and-select t]
2668          ["Mark same subject" gnus-summary-kill-same-subject t]
2669          ["Catchup" gnus-summary-catchup
2670           ,@(if (featurep 'xemacs) '(t)
2671               '(:help "Mark unread articles in this group as read"))]
2672          ["Catchup all" gnus-summary-catchup-all t]
2673          ["Catchup to here" gnus-summary-catchup-to-here t]
2674          ["Catchup from here" gnus-summary-catchup-from-here t]
2675          ["Catchup region" gnus-summary-mark-region-as-read
2676           (gnus-mark-active-p)]
2677          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2678         ("Mark Various"
2679          ["Tick" gnus-summary-tick-article-forward t]
2680          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2681          ["Remove marks" gnus-summary-clear-mark-forward t]
2682          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2683          ["Set bookmark" gnus-summary-set-bookmark t]
2684          ["Remove bookmark" gnus-summary-remove-bookmark t])
2685         ("Limit to"
2686          ["Marks..." gnus-summary-limit-to-marks t]
2687          ["Subject..." gnus-summary-limit-to-subject t]
2688          ["Author..." gnus-summary-limit-to-author t]
2689          ["Recipient..." gnus-summary-limit-to-recipient t]
2690          ["Address..." gnus-summary-limit-to-address t]
2691          ["Age..." gnus-summary-limit-to-age t]
2692          ["Extra..." gnus-summary-limit-to-extra t]
2693          ["Score..." gnus-summary-limit-to-score t]
2694          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2695          ["Unread" gnus-summary-limit-to-unread t]
2696          ["Unseen" gnus-summary-limit-to-unseen t]
2697          ["Singletons" gnus-summary-limit-to-singletons t]
2698          ["Replied" gnus-summary-limit-to-replied t]
2699          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2700          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2701          ["Pop limit" gnus-summary-pop-limit t]
2702          ["Show dormant" gnus-summary-limit-include-dormant t]
2703          ["Hide childless dormant"
2704           gnus-summary-limit-exclude-childless-dormant t]
2705          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2706          ["Hide marked" gnus-summary-limit-exclude-marks t]
2707          ["Show expunged" gnus-summary-limit-include-expunged t])
2708         ("Process Mark"
2709          ["Set mark" gnus-summary-mark-as-processable t]
2710          ["Remove mark" gnus-summary-unmark-as-processable t]
2711          ["Remove all marks" gnus-summary-unmark-all-processable t]
2712          ["Invert marks" gnus-uu-invert-processable t]
2713          ["Mark above" gnus-uu-mark-over t]
2714          ["Mark series" gnus-uu-mark-series t]
2715          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2716          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2717          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2718          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2719          ["Mark all" gnus-uu-mark-all t]
2720          ["Mark buffer" gnus-uu-mark-buffer t]
2721          ["Mark sparse" gnus-uu-mark-sparse t]
2722          ["Mark thread" gnus-uu-mark-thread t]
2723          ["Unmark thread" gnus-uu-unmark-thread t]
2724          ("Process Mark Sets"
2725           ["Kill" gnus-summary-kill-process-mark t]
2726           ["Yank" gnus-summary-yank-process-mark
2727            gnus-newsgroup-process-stack]
2728           ["Save" gnus-summary-save-process-mark t]
2729           ["Run command on marked..." gnus-summary-universal-argument t]))
2730         ("Registry Marks")
2731         ("Scroll article"
2732          ["Page forward" gnus-summary-next-page
2733           ,@(if (featurep 'xemacs) '(t)
2734               '(:help "Show next page of article"))]
2735          ["Page backward" gnus-summary-prev-page
2736           ,@(if (featurep 'xemacs) '(t)
2737               '(:help "Show previous page of article"))]
2738          ["Line forward" gnus-summary-scroll-up t])
2739         ("Move"
2740          ["Next unread article" gnus-summary-next-unread-article t]
2741          ["Previous unread article" gnus-summary-prev-unread-article t]
2742          ["Next article" gnus-summary-next-article t]
2743          ["Previous article" gnus-summary-prev-article t]
2744          ["Next unread subject" gnus-summary-next-unread-subject t]
2745          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2746          ["Next article same subject" gnus-summary-next-same-subject t]
2747          ["Previous article same subject" gnus-summary-prev-same-subject t]
2748          ["First unread article" gnus-summary-first-unread-article t]
2749          ["Best unread article" gnus-summary-best-unread-article t]
2750          ["Go to subject number..." gnus-summary-goto-subject t]
2751          ["Go to article number..." gnus-summary-goto-article t]
2752          ["Go to the last article" gnus-summary-goto-last-article t]
2753          ["Pop article off history" gnus-summary-pop-article t])
2754         ("Sort"
2755          ["Sort by number" gnus-summary-sort-by-number t]
2756          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2757          ["Sort by author" gnus-summary-sort-by-author t]
2758          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2759          ["Sort by subject" gnus-summary-sort-by-subject t]
2760          ["Sort by date" gnus-summary-sort-by-date t]
2761          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2762          ["Sort by score" gnus-summary-sort-by-score t]
2763          ["Sort by lines" gnus-summary-sort-by-lines t]
2764          ["Sort by characters" gnus-summary-sort-by-chars t]
2765          ["Randomize" gnus-summary-sort-by-random t]
2766          ["Original sort" gnus-summary-sort-by-original t])
2767         ("Help"
2768          ["Describe group" gnus-summary-describe-group t]
2769          ["Read manual" gnus-info-find-node t])
2770         ("Modes"
2771          ["Pick and read" gnus-pick-mode t]
2772          ["Binary" gnus-binary-mode t])
2773         ("Regeneration"
2774          ["Regenerate" gnus-summary-prepare t]
2775          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2776          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2777          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2778          ["Toggle threading" gnus-summary-toggle-threads t])
2779         ["See old articles" gnus-summary-insert-old-articles t]
2780         ["See new articles" gnus-summary-insert-new-articles t]
2781         ["Filter articles..." gnus-summary-execute-command t]
2782         ["Run command on articles..." gnus-summary-universal-argument t]
2783         ["Search articles forward..." gnus-summary-search-article-forward t]
2784         ["Search articles backward..." gnus-summary-search-article-backward t]
2785         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2786         ["Expand window" gnus-summary-expand-window t]
2787         ["Expire expirable articles" gnus-summary-expire-articles
2788          (gnus-check-backend-function
2789           'request-expire-articles gnus-newsgroup-name)]
2790         ["Edit local kill file" gnus-summary-edit-local-kill t]
2791         ["Edit main kill file" gnus-summary-edit-global-kill t]
2792         ["Edit group parameters" gnus-summary-edit-parameters t]
2793         ["Customize group parameters" gnus-summary-customize-parameters t]
2794         ["Send a bug report" gnus-bug t]
2795         ("Exit"
2796          ["Catchup and exit" gnus-summary-catchup-and-exit
2797           ,@(if (featurep 'xemacs) '(t)
2798               '(:help "Mark unread articles in this group as read, then exit"))]
2799          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2800          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2801          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2802          ["Exit group" gnus-summary-exit
2803           ,@(if (featurep 'xemacs) '(t)
2804               '(:help "Exit current group, return to group selection mode"))]
2805          ["Exit group without updating" gnus-summary-exit-no-update t]
2806          ["Exit and goto next group" gnus-summary-next-group t]
2807          ["Exit and goto prev group" gnus-summary-prev-group t]
2808          ["Reselect group" gnus-summary-reselect-current-group t]
2809          ["Rescan group" gnus-summary-rescan-group t]
2810          ["Update dribble" gnus-summary-save-newsrc t])))
2811
2812     (gnus-run-hooks 'gnus-summary-menu-hook)))
2813
2814 (defvar gnus-summary-tool-bar-map nil)
2815
2816 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2817 ;; affect _new_ message buffers.  We might add a function that walks thru all
2818 ;; summary-mode buffers and force the update.
2819 (defun gnus-summary-tool-bar-update (&optional symbol value)
2820   "Update summary mode toolbar.
2821 Setter function for custom variables."
2822   (setq-default gnus-summary-tool-bar-map nil)
2823   (when symbol
2824     ;; When used as ":set" function:
2825     (set-default symbol value))
2826   (when (gnus-buffer-live-p gnus-summary-buffer)
2827     (with-current-buffer gnus-summary-buffer
2828       (gnus-summary-make-tool-bar))))
2829
2830 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2831                                      'gnus-summary-tool-bar-gnome
2832                                    'gnus-summary-tool-bar-retro)
2833   "Specifies the Gnus summary tool bar.
2834
2835 It can be either a list or a symbol referring to a list.  See
2836 `gmm-tool-bar-from-list' for the format of the list.  The
2837 default key map is `gnus-summary-mode-map'.
2838
2839 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2840 `gnus-summary-tool-bar-retro'."
2841   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2842                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2843                  (repeat :tag "User defined list" gmm-tool-bar-item)
2844                  (symbol))
2845   :version "23.1" ;; No Gnus
2846   :initialize 'custom-initialize-default
2847   :set 'gnus-summary-tool-bar-update
2848   :group 'gnus-summary)
2849
2850 (defcustom gnus-summary-tool-bar-gnome
2851   '((gnus-summary-post-news "mail/compose" nil)
2852     (gnus-summary-insert-new-articles "mail/inbox" nil
2853                                       :visible (or (not gnus-agent)
2854                                                    gnus-plugged))
2855     (gnus-summary-reply-with-original "mail/reply")
2856     (gnus-summary-reply "mail/reply" nil :visible nil)
2857     (gnus-summary-followup-with-original "mail/reply-all")
2858     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2859     (gnus-summary-mail-forward "mail/forward")
2860     (gnus-summary-save-article "mail/save")
2861     (gnus-summary-search-article-forward "search" nil :visible nil)
2862     (gnus-summary-print-article "print")
2863     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2864     ;; Some new commands that may need more suitable icons:
2865     (gnus-summary-save-newsrc "save" nil :visible nil)
2866     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2867     (gnus-summary-prev-article "left-arrow")
2868     (gnus-summary-next-article "right-arrow")
2869     (gnus-summary-next-page "next-page")
2870     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2871     ;;
2872     ;; Maybe some sort-by-... could be added:
2873     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2874     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2875     (gnus-summary-mark-as-expirable
2876      "delete" nil
2877      :visible (gnus-check-backend-function 'request-expire-articles
2878                                            gnus-newsgroup-name))
2879     (gnus-summary-mark-as-spam
2880      "mail/spam" t
2881      :visible (and (fboundp 'spam-group-ham-contents-p)
2882                    (spam-group-ham-contents-p gnus-newsgroup-name))
2883      :help "Mark as spam")
2884     (gnus-summary-mark-as-read-forward
2885      "mail/not-spam" nil
2886      :visible (and (fboundp 'spam-group-spam-contents-p)
2887                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2888     ;;
2889     (gnus-summary-exit "exit")
2890     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2891     (gnus-info-find-node "help"))
2892   "List of functions for the summary tool bar (GNOME style).
2893
2894 See `gmm-tool-bar-from-list' for the format of the list."
2895   :type '(repeat gmm-tool-bar-item)
2896   :version "23.1" ;; No Gnus
2897   :initialize 'custom-initialize-default
2898   :set 'gnus-summary-tool-bar-update
2899   :group 'gnus-summary)
2900
2901 (defcustom gnus-summary-tool-bar-retro
2902   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2903     (gnus-summary-next-unread-article "gnus/next-ur")
2904     (gnus-summary-post-news "gnus/post")
2905     (gnus-summary-followup-with-original "gnus/fuwo")
2906     (gnus-summary-followup "gnus/followup")
2907     (gnus-summary-reply-with-original "gnus/reply-wo")
2908     (gnus-summary-reply "gnus/reply")
2909     (gnus-summary-caesar-message "gnus/rot13")
2910     (gnus-uu-decode-uu "gnus/uu-decode")
2911     (gnus-summary-save-article-file "gnus/save-aif")
2912     (gnus-summary-save-article "gnus/save-art")
2913     (gnus-uu-post-news "gnus/uu-post")
2914     (gnus-summary-catchup "gnus/catchup")
2915     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2916     (gnus-summary-exit "gnus/exit-summ")
2917     ;; Some new command that may need more suitable icons:
2918     (gnus-summary-print-article "gnus/print" nil :visible nil)
2919     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2920     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2921     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2922     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2923     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2924     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2925     ;;
2926     (gnus-info-find-node "gnus/help" nil :visible nil))
2927   "List of functions for the summary tool bar (retro look).
2928
2929 See `gmm-tool-bar-from-list' for the format of the list."
2930   :type '(repeat gmm-tool-bar-item)
2931   :version "23.1" ;; No Gnus
2932   :initialize 'custom-initialize-default
2933   :set 'gnus-summary-tool-bar-update
2934   :group 'gnus-summary)
2935
2936 (defcustom gnus-summary-tool-bar-zap-list t
2937   "List of icon items from the global tool bar.
2938 These items are not displayed in the Gnus summary mode tool bar.
2939
2940 See `gmm-tool-bar-from-list' for the format of the list."
2941   :type 'gmm-tool-bar-zap-list
2942   :version "23.1" ;; No Gnus
2943   :initialize 'custom-initialize-default
2944   :set 'gnus-summary-tool-bar-update
2945   :group 'gnus-summary)
2946
2947 (defvar image-load-path)
2948 (defvar tool-bar-map)
2949
2950 (defun gnus-summary-make-tool-bar (&optional force)
2951   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2952 When FORCE, rebuild the tool bar."
2953   (when (and (not (featurep 'xemacs))
2954              (boundp 'tool-bar-mode)
2955              tool-bar-mode
2956              (or (not gnus-summary-tool-bar-map) force))
2957     (let* ((load-path
2958             (gmm-image-load-path-for-library "gnus"
2959                                              "mail/save.xpm"
2960                                              nil t))
2961            (image-load-path (cons (car load-path)
2962                                   (when (boundp 'image-load-path)
2963                                     image-load-path)))
2964            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2965                                         gnus-summary-tool-bar-zap-list
2966                                         'gnus-summary-mode-map)))
2967       (when map
2968         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2969         ;; uses its value.
2970         (setq gnus-summary-tool-bar-map map))))
2971   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2972
2973 (defun gnus-score-set-default (var value)
2974   "A version of set that updates the GNU Emacs menu-bar."
2975   (set var value)
2976   ;; It is the message that forces the active status to be updated.
2977   (message ""))
2978
2979 (defun gnus-make-score-map (type)
2980   "Make a summary score map of type TYPE."
2981   (if t
2982       nil
2983     (let ((headers '(("author" "from" string)
2984                      ("subject" "subject" string)
2985                      ("article body" "body" string)
2986                      ("article head" "head" string)
2987                      ("xref" "xref" string)
2988                      ("extra header" "extra" string)
2989                      ("lines" "lines" number)
2990                      ("followups to author" "followup" string)))
2991           (types '((number ("less than" <)
2992                            ("greater than" >)
2993                            ("equal" =))
2994                    (string ("substring" s)
2995                            ("exact string" e)
2996                            ("fuzzy string" f)
2997                            ("regexp" r))))
2998           (perms '(("temporary" (current-time-string))
2999                    ("permanent" nil)
3000                    ("immediate" now)))
3001           header)
3002       (list
3003        (apply
3004         'nconc
3005         (list
3006          (if (eq type 'lower)
3007              "Lower score"
3008            "Increase score"))
3009         (let (outh)
3010           (while headers
3011             (setq header (car headers))
3012             (setq outh
3013                   (cons
3014                    (apply
3015                     'nconc
3016                     (list (car header))
3017                     (let ((ts (cdr (assoc (nth 2 header) types)))
3018                           outt)
3019                       (while ts
3020                         (setq outt
3021                               (cons
3022                                (apply
3023                                 'nconc
3024                                 (list (caar ts))
3025                                 (let ((ps perms)
3026                                       outp)
3027                                   (while ps
3028                                     (setq outp
3029                                           (cons
3030                                            (vector
3031                                             (caar ps)
3032                                             (list
3033                                              'gnus-summary-score-entry
3034                                              (nth 1 header)
3035                                              (if (or (string= (nth 1 header)
3036                                                               "head")
3037                                                      (string= (nth 1 header)
3038                                                               "body"))
3039                                                  ""
3040                                                (list 'gnus-summary-header
3041                                                      (nth 1 header)))
3042                                              (list 'quote (nth 1 (car ts)))
3043                                              (list 'gnus-score-delta-default
3044                                                    nil)
3045                                              (nth 1 (car ps))
3046                                              t)
3047                                             t)
3048                                            outp))
3049                                     (setq ps (cdr ps)))
3050                                   (list (nreverse outp))))
3051                                outt))
3052                         (setq ts (cdr ts)))
3053                       (list (nreverse outt))))
3054                    outh))
3055             (setq headers (cdr headers)))
3056           (list (nreverse outh))))))))
3057
3058
3059 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3060 (defvar bookmark-make-record-function)
3061 \f
3062 (defvar bidi-paragraph-direction)
3063
3064 (defun gnus-summary-mode (&optional group)
3065   "Major mode for reading articles.
3066
3067 All normal editing commands are switched off.
3068 \\<gnus-summary-mode-map>
3069 Each line in this buffer represents one article.  To read an
3070 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3071 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3072 respectively.
3073
3074 You can also post articles and send mail from this buffer.  To
3075 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3076 of an article, type `\\[gnus-summary-reply]'.
3077
3078 There are approx. one gazillion commands you can execute in this
3079 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3080
3081 The following commands are available:
3082
3083 \\{gnus-summary-mode-map}"
3084   (interactive)
3085   (kill-all-local-variables)
3086   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3087     (gnus-summary-make-local-variables))
3088   (gnus-summary-make-local-variables)
3089   (setq gnus-newsgroup-name group)
3090   (when (gnus-visual-p 'summary-menu 'menu)
3091     (gnus-summary-make-menu-bar)
3092     (gnus-summary-make-tool-bar))
3093   (gnus-make-thread-indent-array)
3094   (gnus-simplify-mode-line)
3095   (setq major-mode 'gnus-summary-mode)
3096   (setq mode-name "Summary")
3097   (use-local-map gnus-summary-mode-map)
3098   (buffer-disable-undo)
3099   (setq buffer-read-only t              ;Disable modification
3100         show-trailing-whitespace nil)
3101   (setq truncate-lines t)
3102   ;; Force paragraph direction to be left-to-right.  Don't make it
3103   ;; bound globally in old Emacsen and XEmacsen.
3104   (set (make-local-variable 'bidi-paragraph-direction) 'left-to-right)
3105   (add-to-invisibility-spec '(gnus-sum . t))
3106   (gnus-summary-set-display-table)
3107   (gnus-set-default-directory)
3108   (make-local-variable 'gnus-summary-line-format)
3109   (make-local-variable 'gnus-summary-line-format-spec)
3110   (make-local-variable 'gnus-summary-dummy-line-format)
3111   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3112   (make-local-variable 'gnus-summary-mark-positions)
3113   (gnus-make-local-hook 'pre-command-hook)
3114   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3115   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3116   (turn-on-gnus-mailing-list-mode)
3117   (mm-enable-multibyte)
3118   (set (make-local-variable 'bookmark-make-record-function)
3119        'gnus-summary-bookmark-make-record)
3120   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3121   (gnus-update-summary-mark-positions))
3122
3123 (defun gnus-summary-make-local-variables ()
3124   "Make all the local summary buffer variables."
3125   (let (global)
3126     (dolist (local gnus-summary-local-variables)
3127       (if (consp local)
3128           (progn
3129             (if (eq (cdr local) 'global)
3130                 ;; Copy the global value of the variable.
3131                 (setq global (symbol-value (car local)))
3132               ;; Use the value from the list.
3133               (setq global (eval (cdr local))))
3134             (set (make-local-variable (car local)) global))
3135         ;; Simple nil-valued local variable.
3136         (set (make-local-variable local) nil)))))
3137
3138 ;; Summary data functions.
3139
3140 (defmacro gnus-data-number (data)
3141   `(car ,data))
3142
3143 (defmacro gnus-data-set-number (data number)
3144   `(setcar ,data ,number))
3145
3146 (defmacro gnus-data-mark (data)
3147   `(nth 1 ,data))
3148
3149 (defmacro gnus-data-set-mark (data mark)
3150   `(setcar (nthcdr 1 ,data) ,mark))
3151
3152 (defmacro gnus-data-pos (data)
3153   `(nth 2 ,data))
3154
3155 (defmacro gnus-data-set-pos (data pos)
3156   `(setcar (nthcdr 2 ,data) ,pos))
3157
3158 (defmacro gnus-data-header (data)
3159   `(nth 3 ,data))
3160
3161 (defmacro gnus-data-set-header (data header)
3162   `(setf (nth 3 ,data) ,header))
3163
3164 (defmacro gnus-data-level (data)
3165   `(nth 4 ,data))
3166
3167 (defmacro gnus-data-unread-p (data)
3168   `(= (nth 1 ,data) gnus-unread-mark))
3169
3170 (defmacro gnus-data-read-p (data)
3171   `(/= (nth 1 ,data) gnus-unread-mark))
3172
3173 (defmacro gnus-data-pseudo-p (data)
3174   `(consp (nth 3 ,data)))
3175
3176 (defmacro gnus-data-find (number)
3177   `(assq ,number gnus-newsgroup-data))
3178
3179 (defmacro gnus-data-find-list (number &optional data)
3180   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3181      (memq (assq ,number bdata)
3182            bdata)))
3183
3184 (defmacro gnus-data-make (number mark pos header level)
3185   `(list ,number ,mark ,pos ,header ,level))
3186
3187 (defun gnus-data-enter (after-article number mark pos header level offset)
3188   (let ((data (gnus-data-find-list after-article)))
3189     (unless data
3190       (error "No such article: %d" after-article))
3191     (setcdr data (cons (gnus-data-make number mark pos header level)
3192                        (cdr data)))
3193     (setq gnus-newsgroup-data-reverse nil)
3194     (gnus-data-update-list (cddr data) offset)))
3195
3196 (defun gnus-data-enter-list (after-article list &optional offset)
3197   (when list
3198     (let ((data (and after-article (gnus-data-find-list after-article)))
3199           (ilist list))
3200       (if (not (or data
3201                    after-article))
3202           (let ((odata gnus-newsgroup-data))
3203             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3204             (when offset
3205               (gnus-data-update-list odata offset)))
3206         ;; Find the last element in the list to be spliced into the main
3207         ;; list.
3208         (setq list (last list))
3209         (if (not data)
3210             (progn
3211               (setcdr list gnus-newsgroup-data)
3212               (setq gnus-newsgroup-data ilist)
3213               (when offset
3214                 (gnus-data-update-list (cdr list) offset)))
3215           (setcdr list (cdr data))
3216           (setcdr data ilist)
3217           (when offset
3218             (gnus-data-update-list (cdr list) offset))))
3219       (setq gnus-newsgroup-data-reverse nil))))
3220
3221 (defun gnus-data-remove (article &optional offset)
3222   (let ((data gnus-newsgroup-data))
3223     (if (= (gnus-data-number (car data)) article)
3224         (progn
3225           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3226                 gnus-newsgroup-data-reverse nil)
3227           (when offset
3228             (gnus-data-update-list gnus-newsgroup-data offset)))
3229       (while (cdr data)
3230         (when (= (gnus-data-number (cadr data)) article)
3231           (setcdr data (cddr data))
3232           (when offset
3233             (gnus-data-update-list (cdr data) offset))
3234           (setq data nil
3235                 gnus-newsgroup-data-reverse nil))
3236         (setq data (cdr data))))))
3237
3238 (defmacro gnus-data-list (backward)
3239   `(if ,backward
3240        (or gnus-newsgroup-data-reverse
3241            (setq gnus-newsgroup-data-reverse
3242                  (reverse gnus-newsgroup-data)))
3243      gnus-newsgroup-data))
3244
3245 (defun gnus-data-update-list (data offset)
3246   "Add OFFSET to the POS of all data entries in DATA."
3247   (setq gnus-newsgroup-data-reverse nil)
3248   (while data
3249     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3250     (setq data (cdr data))))
3251
3252 (defun gnus-summary-article-pseudo-p (article)
3253   "Say whether this article is a pseudo article or not."
3254   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3255
3256 (defmacro gnus-summary-article-sparse-p (article)
3257   "Say whether this article is a sparse article or not."
3258   `(memq ,article gnus-newsgroup-sparse))
3259
3260 (defmacro gnus-summary-article-ancient-p (article)
3261   "Say whether this article is a sparse article or not."
3262   `(memq ,article gnus-newsgroup-ancient))
3263
3264 (defun gnus-article-parent-p (number)
3265   "Say whether this article is a parent or not."
3266   (let ((data (gnus-data-find-list number)))
3267     (and (cdr data)              ; There has to be an article after...
3268          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3269             (gnus-data-level (nth 1 data))))))
3270
3271 (defun gnus-article-children (number)
3272   "Return a list of all children to NUMBER."
3273   (let* ((data (gnus-data-find-list number))
3274          (level (gnus-data-level (car data)))
3275          children)
3276     (setq data (cdr data))
3277     (while (and data
3278                 (= (gnus-data-level (car data)) (1+ level)))
3279       (push (gnus-data-number (car data)) children)
3280       (setq data (cdr data)))
3281     children))
3282
3283 (defmacro gnus-summary-skip-intangible ()
3284   "If the current article is intangible, then jump to a different article."
3285   '(let ((to (get-text-property (point) 'gnus-intangible)))
3286      (and to (gnus-summary-goto-subject to))))
3287
3288 (defmacro gnus-summary-article-intangible-p ()
3289   "Say whether this article is intangible or not."
3290   '(get-text-property (point) 'gnus-intangible))
3291
3292 (defun gnus-article-read-p (article)
3293   "Say whether ARTICLE is read or not."
3294   (not (or (memq article gnus-newsgroup-marked)
3295            (memq article gnus-newsgroup-spam-marked)
3296            (memq article gnus-newsgroup-unreads)
3297            (memq article gnus-newsgroup-unselected)
3298            (memq article gnus-newsgroup-dormant))))
3299
3300 ;; Some summary mode macros.
3301
3302 (defmacro gnus-summary-article-number ()
3303   "The article number of the article on the current line.
3304 If there isn't an article number here, then we return the current
3305 article number."
3306   '(progn
3307      (gnus-summary-skip-intangible)
3308      (or (get-text-property (point) 'gnus-number)
3309          (gnus-summary-last-subject))))
3310
3311 (defmacro gnus-summary-article-header (&optional number)
3312   "Return the header of article NUMBER."
3313   `(gnus-data-header (gnus-data-find
3314                       ,(or number '(gnus-summary-article-number)))))
3315
3316 (defmacro gnus-summary-thread-level (&optional number)
3317   "Return the level of thread that starts with article NUMBER."
3318   `(if (and (eq gnus-summary-make-false-root 'dummy)
3319             (get-text-property (point) 'gnus-intangible))
3320        0
3321      (gnus-data-level (gnus-data-find
3322                        ,(or number '(gnus-summary-article-number))))))
3323
3324 (defmacro gnus-summary-article-mark (&optional number)
3325   "Return the mark of article NUMBER."
3326   `(gnus-data-mark (gnus-data-find
3327                     ,(or number '(gnus-summary-article-number)))))
3328
3329 (defmacro gnus-summary-article-pos (&optional number)
3330   "Return the position of the line of article NUMBER."
3331   `(gnus-data-pos (gnus-data-find
3332                    ,(or number '(gnus-summary-article-number)))))
3333
3334 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3335 (defmacro gnus-summary-article-subject (&optional number)
3336   "Return current subject string or nil if nothing."
3337   `(let ((headers
3338           ,(if number
3339                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3340              '(gnus-data-header (assq (gnus-summary-article-number)
3341                                       gnus-newsgroup-data)))))
3342      (and headers
3343           (vectorp headers)
3344           (mail-header-subject headers))))
3345
3346 (defmacro gnus-summary-article-score (&optional number)
3347   "Return current article score."
3348   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3349                   gnus-newsgroup-scored))
3350        gnus-summary-default-score 0))
3351
3352 (defun gnus-summary-article-children (&optional number)
3353   "Return a list of article numbers that are children of article NUMBER."
3354   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3355          (level (gnus-data-level (car data)))
3356          l children)
3357     (while (and (setq data (cdr data))
3358                 (> (setq l (gnus-data-level (car data))) level))
3359       (and (= (1+ level) l)
3360            (push (gnus-data-number (car data))
3361                  children)))
3362     (nreverse children)))
3363
3364 (defun gnus-summary-article-parent (&optional number)
3365   "Return the article number of the parent of article NUMBER."
3366   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3367                                     (gnus-data-list t)))
3368          (level (gnus-data-level (car data))))
3369     (if (zerop level)
3370         ()                              ; This is a root.
3371       ;; We search until we find an article with a level less than
3372       ;; this one.  That function has to be the parent.
3373       (while (and (setq data (cdr data))
3374                   (not (< (gnus-data-level (car data)) level))))
3375       (and data (gnus-data-number (car data))))))
3376
3377 (defun gnus-unread-mark-p (mark)
3378   "Say whether MARK is the unread mark."
3379   (= mark gnus-unread-mark))
3380
3381 (defun gnus-read-mark-p (mark)
3382   "Say whether MARK is one of the marks that mark as read.
3383 This is all marks except unread, ticked, dormant, and expirable."
3384   (not (or (= mark gnus-unread-mark)
3385            (= mark gnus-ticked-mark)
3386            (= mark gnus-spam-mark)
3387            (= mark gnus-dormant-mark)
3388            (= mark gnus-expirable-mark))))
3389
3390 (defmacro gnus-article-mark (number)
3391   "Return the MARK of article NUMBER.
3392 This macro should only be used when computing the mark the \"first\"
3393 time; i.e., when generating the summary lines.  After that,
3394 `gnus-summary-article-mark' should be used to examine the
3395 marks of articles."
3396   `(cond
3397     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3398     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3399     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3400     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3401     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3402     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3403     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3404     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3405            gnus-ancient-mark))))
3406
3407 ;; Saving hidden threads.
3408
3409 (defmacro gnus-save-hidden-threads (&rest forms)
3410   "Save hidden threads, eval FORMS, and restore the hidden threads."
3411   (let ((config (make-symbol "config")))
3412     `(let ((,config (gnus-hidden-threads-configuration)))
3413        (unwind-protect
3414            (save-excursion
3415              ,@forms)
3416          (gnus-restore-hidden-threads-configuration ,config)))))
3417 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3418 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3419
3420 (defun gnus-data-compute-positions ()
3421   "Compute the positions of all articles."
3422   (setq gnus-newsgroup-data-reverse nil)
3423   (let ((data gnus-newsgroup-data))
3424     (save-excursion
3425       (gnus-save-hidden-threads
3426         (gnus-summary-show-all-threads)
3427         (goto-char (point-min))
3428         (while data
3429           (while (get-text-property (point) 'gnus-intangible)
3430             (forward-line 1))
3431           (gnus-data-set-pos (car data) (+ (point) 3))
3432           (setq data (cdr data))
3433           (forward-line 1))))))
3434
3435 (defun gnus-hidden-threads-configuration ()
3436   "Return the current hidden threads configuration."
3437   (save-excursion
3438     (let (config)
3439       (goto-char (point-min))
3440       (while (not (eobp))
3441         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3442           (push (save-excursion (forward-line 0) (point)) config))
3443         (forward-line 1))
3444       config)))
3445
3446 (defun gnus-restore-hidden-threads-configuration (config)
3447   "Restore hidden threads configuration from CONFIG."
3448   (save-excursion
3449     (let (point (inhibit-read-only t))
3450       (while (setq point (pop config))
3451         (goto-char point)
3452         (gnus-summary-hide-thread)))))
3453
3454 ;; Various summary mode internalish functions.
3455
3456 (defun gnus-mouse-pick-article (e)
3457   (interactive "e")
3458   (mouse-set-point e)
3459   (gnus-summary-next-page nil t))
3460
3461 (defun gnus-summary-set-display-table ()
3462   "Change the display table.
3463 Odd characters have a tendency to mess
3464 up nicely formatted displays - we make all possible glyphs
3465 display only a single character."
3466
3467   ;; We start from the standard display table, if any.
3468   (let ((table (or (copy-sequence standard-display-table)
3469                    (make-display-table)))
3470         (i 32))
3471     ;; Nix out all the control chars...
3472     (while (>= (setq i (1- i)) 0)
3473       (gnus-put-display-table i [??] table))
3474    ;; ... but not newline and cr, of course.  (cr is necessary for the
3475     ;; selective display).
3476     (gnus-put-display-table ?\n nil table)
3477     (gnus-put-display-table ?\r nil table)
3478     ;; We keep TAB as well.
3479     (gnus-put-display-table ?\t nil table)
3480     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3481     ;; Emacs 23 (unicode), that are not set already.
3482     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3483                  160
3484                256)))
3485       (while (>= (setq i (1- i)) 127)
3486         ;; Only modify if the entry is nil.
3487         (unless (gnus-get-display-table i table)
3488           (gnus-put-display-table i [??] table))))
3489     (setq buffer-display-table table)))
3490
3491 (defun gnus-summary-set-article-display-arrow (pos)
3492   "Update the overlay arrow to point to line at position POS."
3493   (when gnus-summary-display-arrow
3494     (make-local-variable 'overlay-arrow-position)
3495     (make-local-variable 'overlay-arrow-string)
3496     (save-excursion
3497       (goto-char pos)
3498       (beginning-of-line)
3499       (unless overlay-arrow-position
3500         (setq overlay-arrow-position (make-marker)))
3501       (setq overlay-arrow-string "=>"
3502             overlay-arrow-position (set-marker overlay-arrow-position
3503                                                (point)
3504                                                (current-buffer))))))
3505
3506 (defun gnus-summary-setup-buffer (group)
3507   "Initialize summary buffer.
3508 If the setup was successful, non-nil is returned."
3509   (let ((buffer (gnus-summary-buffer-name group))
3510         (dead-name (concat "*Dead Summary "
3511                            (gnus-group-decoded-name group) "*")))
3512     ;; If a dead summary buffer exists, we kill it.
3513     (when (gnus-buffer-live-p dead-name)
3514       (gnus-kill-buffer dead-name))
3515     (if (get-buffer buffer)
3516         (progn
3517           (set-buffer buffer)
3518           (setq gnus-summary-buffer (current-buffer))
3519           (not gnus-newsgroup-prepared))
3520       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3521       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3522       (gnus-summary-mode group)
3523       (when (gnus-group-quit-config group)
3524         (set (make-local-variable 'gnus-single-article-buffer) nil))
3525       (make-local-variable 'gnus-article-buffer)
3526       (make-local-variable 'gnus-article-current)
3527       (make-local-variable 'gnus-original-article-buffer)
3528       (setq gnus-newsgroup-name group)
3529       ;; Set any local variables in the group parameters.
3530       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3531       t)))
3532
3533 (defun gnus-set-global-variables ()
3534   "Set the global equivalents of the buffer-local variables.
3535 They are set to the latest values they had.  These reflect the summary
3536 buffer that was in action when the last article was fetched."
3537   (when (eq major-mode 'gnus-summary-mode)
3538     (setq gnus-summary-buffer (current-buffer))
3539     (let ((name gnus-newsgroup-name)
3540           (marked gnus-newsgroup-marked)
3541           (spam gnus-newsgroup-spam-marked)
3542           (unread gnus-newsgroup-unreads)
3543           (headers gnus-current-headers)
3544           (data gnus-newsgroup-data)
3545           (summary gnus-summary-buffer)
3546           (article-buffer gnus-article-buffer)
3547           (original gnus-original-article-buffer)
3548           (gac gnus-article-current)
3549           (reffed gnus-reffed-article-number)
3550           (score-file gnus-current-score-file)
3551           (default-charset gnus-newsgroup-charset)
3552           vlist)
3553       (let ((locals gnus-newsgroup-variables))
3554         (while locals
3555           (if (consp (car locals))
3556               (push (eval (caar locals)) vlist)
3557             (push (eval (car locals)) vlist))
3558           (setq locals (cdr locals)))
3559         (setq vlist (nreverse vlist)))
3560       (with-current-buffer gnus-group-buffer
3561         (setq gnus-newsgroup-name name
3562               gnus-newsgroup-marked marked
3563               gnus-newsgroup-spam-marked spam
3564               gnus-newsgroup-unreads unread
3565               gnus-current-headers headers
3566               gnus-newsgroup-data data
3567               gnus-article-current gac
3568               gnus-summary-buffer summary
3569               gnus-article-buffer article-buffer
3570               gnus-original-article-buffer original
3571               gnus-reffed-article-number reffed
3572               gnus-current-score-file score-file
3573               gnus-newsgroup-charset default-charset)
3574         (let ((locals gnus-newsgroup-variables))
3575           (while locals
3576             (if (consp (car locals))
3577                 (set (caar locals) (pop vlist))
3578               (set (car locals) (pop vlist)))
3579             (setq locals (cdr locals))))
3580         ;; The article buffer also has local variables.
3581         (when (gnus-buffer-live-p gnus-article-buffer)
3582           (set-buffer gnus-article-buffer)
3583           (setq gnus-summary-buffer summary))))))
3584
3585 (defun gnus-summary-article-unread-p (article)
3586   "Say whether ARTICLE is unread or not."
3587   (memq article gnus-newsgroup-unreads))
3588
3589 (defun gnus-summary-first-article-p (&optional article)
3590   "Return whether ARTICLE is the first article in the buffer."
3591   (if (not (setq article (or article (gnus-summary-article-number))))
3592       nil
3593     (eq article (caar gnus-newsgroup-data))))
3594
3595 (defun gnus-summary-last-article-p (&optional article)
3596   "Return whether ARTICLE is the last article in the buffer."
3597   (if (not (setq article (or article (gnus-summary-article-number))))
3598       ;; All non-existent numbers are the last article.  :-)
3599       t
3600     (not (cdr (gnus-data-find-list article)))))
3601
3602 (defun gnus-make-thread-indent-array (&optional n)
3603   (when (or n
3604             (progn (setq n 200) nil)
3605             (null gnus-thread-indent-array)
3606             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3607     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3608           gnus-thread-indent-array-level gnus-thread-indent-level)
3609     (while (>= n 0)
3610       (aset gnus-thread-indent-array n
3611             (make-string (* n gnus-thread-indent-level) ? ))
3612       (setq n (1- n)))))
3613
3614 (defun gnus-update-summary-mark-positions ()
3615   "Compute where the summary marks are to go."
3616   (save-excursion
3617     (when (gnus-buffer-exists-p gnus-summary-buffer)
3618       (set-buffer gnus-summary-buffer))
3619     (let ((spec gnus-summary-line-format-spec)
3620           pos)
3621       (save-excursion
3622         (gnus-set-work-buffer)
3623         (let ((gnus-tmp-unread ?Z)
3624               (gnus-replied-mark ?Z)
3625               (gnus-score-below-mark ?Z)
3626               (gnus-score-over-mark ?Z)
3627               (gnus-undownloaded-mark ?Z)
3628               (gnus-summary-line-format-spec spec)
3629               (gnus-newsgroup-downloadable '(0))
3630               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3631               case-fold-search ignores)
3632           ;; Here, all marks are bound to Z.
3633           (gnus-summary-insert-line header
3634                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3635           (goto-char (point-min))
3636           ;; Memorize the positions of the same characters as dummy marks.
3637           (while (re-search-forward "[A-D]" nil t)
3638             (push (point) ignores))
3639           (erase-buffer)
3640           ;; We use A-D as dummy marks in order to know column positions
3641           ;; where marks should be inserted.
3642           (setq gnus-tmp-unread ?A
3643                 gnus-replied-mark ?B
3644                 gnus-score-below-mark ?C
3645                 gnus-score-over-mark ?C
3646                 gnus-undownloaded-mark ?D)
3647           (gnus-summary-insert-line header
3648                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3649           ;; Ignore characters which aren't dummy marks.
3650           (dolist (p ignores)
3651             (delete-region (goto-char (1- p)) p)
3652             (insert ?Z))
3653           (goto-char (point-min))
3654           (setq pos (list (cons 'unread
3655                                 (and (search-forward "A" nil t)
3656                                      (- (point) (point-min) 1)))))
3657           (goto-char (point-min))
3658           (push (cons 'replied (and (search-forward "B" nil t)
3659                                     (- (point) (point-min) 1)))
3660                 pos)
3661           (goto-char (point-min))
3662           (push (cons 'score (and (search-forward "C" nil t)
3663                                   (- (point) (point-min) 1)))
3664                 pos)
3665           (goto-char (point-min))
3666           (push (cons 'download (and (search-forward "D" nil t)
3667                                      (- (point) (point-min) 1)))
3668                 pos)))
3669       (setq gnus-summary-mark-positions pos))))
3670
3671 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3672   "Insert a dummy root in the summary buffer."
3673   (beginning-of-line)
3674   (gnus-add-text-properties
3675    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3676    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3677
3678 (defun gnus-summary-extract-address-component (from)
3679   (or (car (funcall gnus-extract-address-components from))
3680       from))
3681
3682 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3683   (let ((mail-parse-charset gnus-newsgroup-charset)
3684         (ignored-from-addresses (gnus-ignored-from-addresses))
3685         ; Is it really necessary to do this next part for each summary line?
3686         ; Luckily, doesn't seem to slow things down much.
3687         (mail-parse-ignored-charsets
3688          (with-current-buffer gnus-summary-buffer
3689            gnus-newsgroup-ignored-charsets)))
3690     (or
3691      (and ignored-from-addresses
3692           (string-match ignored-from-addresses gnus-tmp-from)
3693           (let ((extra-headers (mail-header-extra header))
3694                 to
3695                 newsgroups)
3696             (cond
3697              ((setq to (cdr (assq 'To extra-headers)))
3698               (concat gnus-summary-to-prefix
3699                       (inline
3700                         (gnus-summary-extract-address-component
3701                          (funcall gnus-decode-encoded-address-function to)))))
3702              ((setq newsgroups
3703                     (or
3704                      (cdr (assq 'Newsgroups extra-headers))
3705                      (and
3706                       (memq 'Newsgroups gnus-extra-headers)
3707                       (eq (car (gnus-find-method-for-group
3708                                 gnus-newsgroup-name)) 'nntp)
3709                       (gnus-group-real-name gnus-newsgroup-name))))
3710               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3711      (gnus-string-mark-left-to-right
3712       (inline
3713        (gnus-summary-extract-address-component gnus-tmp-from))))))
3714
3715 (defun gnus-summary-insert-line (gnus-tmp-header
3716                                  gnus-tmp-level gnus-tmp-current
3717                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3718                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3719                                  &optional gnus-tmp-dummy gnus-tmp-score
3720                                  gnus-tmp-process)
3721   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3722       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3723                                           gnus-tmp-level)))
3724   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3725          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3726          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3727          (gnus-tmp-score-char
3728           (if (or (null gnus-summary-default-score)
3729                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3730                       gnus-summary-zcore-fuzz))
3731               ?                         ;Whitespace
3732             (if (< gnus-tmp-score gnus-summary-default-score)
3733                 gnus-score-below-mark gnus-score-over-mark)))
3734          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3735          (gnus-tmp-replied
3736           (cond (gnus-tmp-process gnus-process-mark)
3737                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3738                  gnus-cached-mark)
3739                 (gnus-tmp-replied gnus-replied-mark)
3740                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3741                  gnus-forwarded-mark)
3742                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3743                  gnus-saved-mark)
3744                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3745                  gnus-unseen-mark)
3746                 (t gnus-no-mark)))
3747          (gnus-tmp-downloaded
3748           (cond (undownloaded
3749                  gnus-undownloaded-mark)
3750                 (gnus-newsgroup-agentized
3751                  gnus-downloaded-mark)
3752                 (t
3753                  gnus-no-mark)))
3754          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3755          (gnus-tmp-name
3756           (cond
3757            ((string-match "<[^>]+> *$" gnus-tmp-from)
3758             (let ((beg (match-beginning 0)))
3759               (or (and (string-match "^\".+\"" gnus-tmp-from)
3760                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3761                   (substring gnus-tmp-from 0 beg))))
3762            ((string-match "(.+)" gnus-tmp-from)
3763             (substring gnus-tmp-from
3764                        (1+ (match-beginning 0)) (1- (match-end 0))))
3765            (t gnus-tmp-from)))
3766          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3767          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3768          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3769          (inhibit-read-only t))
3770     (when (string= gnus-tmp-name "")
3771       (setq gnus-tmp-name gnus-tmp-from))
3772     (unless (numberp gnus-tmp-lines)
3773       (setq gnus-tmp-lines -1))
3774     (if (= gnus-tmp-lines -1)
3775         (setq gnus-tmp-lines "?")
3776       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3777     (condition-case ()
3778         (gnus-put-text-property
3779          (point)
3780          (progn (eval gnus-summary-line-format-spec) (point))
3781          'gnus-number gnus-tmp-number)
3782       (error (gnus-message 5 "Error updating the summary line")))
3783     (when (gnus-visual-p 'summary-highlight 'highlight)
3784       (forward-line -1)
3785       (gnus-summary-highlight-line)
3786       (gnus-run-hooks 'gnus-summary-update-hook)
3787       (forward-line 1))))
3788
3789 (defun gnus-summary-update-line (&optional dont-update)
3790   "Update summary line after change."
3791   (when (and gnus-summary-default-score
3792              (not gnus-summary-inhibit-highlight))
3793     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3794            (article (gnus-summary-article-number))
3795            (score (gnus-summary-article-score article)))
3796       (unless dont-update
3797         (if (and gnus-summary-mark-below
3798                  (< (gnus-summary-article-score)
3799                     gnus-summary-mark-below))
3800             ;; This article has a low score, so we mark it as read.
3801             (when (memq article gnus-newsgroup-unreads)
3802               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3803           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3804             ;; This article was previously marked as read on account
3805             ;; of a low score, but now it has risen, so we mark it as
3806             ;; unread.
3807             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3808         (gnus-summary-update-mark
3809          (if (or (null gnus-summary-default-score)
3810                  (<= (abs (- score gnus-summary-default-score))
3811                      gnus-summary-zcore-fuzz))
3812              ?                          ;Whitespace
3813            (if (< score gnus-summary-default-score)
3814                gnus-score-below-mark gnus-score-over-mark))
3815          'score))
3816       ;; Do visual highlighting.
3817       (when (gnus-visual-p 'summary-highlight 'highlight)
3818         (gnus-summary-highlight-line)
3819         (gnus-run-hooks 'gnus-summary-update-hook)))))
3820
3821 (defvar gnus-tmp-new-adopts nil)
3822
3823 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3824   "Return the number of articles in THREAD.
3825 This may be 0 in some cases -- if none of the articles in
3826 the thread are to be displayed."
3827   (let* ((number
3828          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3829           (cond
3830            ((not (listp thread))
3831             1)
3832            ((and (consp thread) (cdr thread))
3833             (apply
3834              '+ 1 (mapcar
3835                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3836            ((null thread)
3837             1)
3838            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3839             1)
3840            (t 0))))
3841     (when (and level (zerop level) gnus-tmp-new-adopts)
3842       (incf number
3843             (apply '+ (mapcar
3844                        'gnus-summary-number-of-articles-in-thread
3845                        gnus-tmp-new-adopts))))
3846     (if char
3847         (if (> number 1) gnus-not-empty-thread-mark
3848           gnus-empty-thread-mark)
3849       number)))
3850
3851 (defsubst gnus-summary-line-message-size (head)
3852   "Return pretty-printed version of message size.
3853 This function is intended to be used in
3854 `gnus-summary-line-format-alist'."
3855   (let ((c (or (mail-header-chars head) -1)))
3856     (cond ((< c 0) "n/a")               ; chars not available
3857           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3858           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3859           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3860           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3861
3862 (defcustom gnus-user-date-format-alist
3863   '(((gnus-seconds-today) . "Today, %H:%M")
3864     ((+ 86400 (gnus-seconds-today)) . "Yesterday, %H:%M")
3865     (604800 . "%A %H:%M")               ; That's one week
3866     ((gnus-seconds-month) . "%A %d")
3867     ((gnus-seconds-year) . "%B %d")
3868     (t . "%b %d %Y"))                   ; This one is used when no other
3869                                         ; does match
3870   "Specifies date format depending on age of article.
3871 This is an alist of items (AGE . FORMAT).  AGE can be a number (of
3872 seconds) or a Lisp expression evaluating to a number.  When the age of
3873 the article is less than this number, then use `format-time-string'
3874 with the corresponding FORMAT for displaying the date of the article.
3875 If AGE is not a number or a Lisp expression evaluating to a
3876 non-number, then the corresponding FORMAT is used as a default value.
3877
3878 Note that the list is processed from the beginning, so it should be
3879 sorted by ascending AGE.  Also note that items following the first
3880 non-number AGE will be ignored.
3881
3882 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
3883 and `gnus-seconds-year' in the AGE spec.  They return the number of
3884 seconds passed since the start of today, of this month, of this year,
3885 respectively."
3886   :version "24.1"
3887   :group 'gnus-summary-format
3888   :type '(alist :key-type sexp :value-type string))
3889
3890 (defun gnus-user-date (messy-date)
3891   "Format the messy-date according to `gnus-user-date-format-alist'.
3892 Returns \"  ?  \" if there's bad input or if another error occurs.
3893 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
3894   (condition-case ()
3895       (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
3896              (now (gnus-float-time))
3897              ;;If we don't find something suitable we'll use this one
3898              (my-format "%b %d '%y"))
3899         (let* ((difference (- now messy-date))
3900                (templist gnus-user-date-format-alist)
3901                (top (eval (caar templist))))
3902           (while (if (numberp top) (< top difference) (not top))
3903             (progn
3904               (setq templist (cdr templist))
3905               (setq top (eval (caar templist)))))
3906           (if (stringp (cdr (car templist)))
3907               (setq my-format (cdr (car templist)))))
3908         (format-time-string (eval my-format) (seconds-to-time messy-date)))
3909     (error "  ?   ")))
3910
3911 (defun gnus-summary-set-local-parameters (group)
3912   "Go through the local params of GROUP and set all variable specs in that list."
3913   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3914                                         ; really variables.
3915     (dolist (elem (gnus-group-find-parameter group))
3916       (and (consp elem)                 ; Has to be a cons.
3917            (consp (cdr elem))           ; The cdr has to be a list.
3918            (symbolp (car elem))         ; Has to be a symbol in there.
3919            (not (memq (car elem) vars))
3920            (ignore-errors
3921              (push (car elem) vars)
3922              ;; Variables like `gnus-show-threads' that are globally
3923              ;; bound, if used as group parameters, need to get to be
3924              ;; buffer-local, whereas just parameters like `gcc-self',
3925              ;; `timestamp', etc. should not be bound as variables.
3926              (if (boundp (car elem))
3927                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3928                (eval (nth 1 elem))))))))
3929
3930 (defun gnus-summary-read-group (group &optional show-all no-article
3931                                       kill-buffer no-display backward
3932                                       select-articles)
3933   "Start reading news in newsgroup GROUP.
3934 If SHOW-ALL is non-nil, already read articles are also listed.
3935 If NO-ARTICLE is non-nil, no article is selected initially.
3936 If NO-DISPLAY, don't generate the summary buffer contents.
3937 If KILL-BUFFER, it should be a buffer that's killed once the new
3938 summary buffer has been generated.
3939 If BACKWARD, move point to the previous group in the group buffer
3940 If SELECT-ARTICLES, only select those articles from GROUP."
3941   (let (result)
3942     (while (and group
3943                 (null (setq result
3944                             (let ((gnus-auto-select-next nil))
3945                               (or (gnus-summary-read-group-1
3946                                    group show-all no-article
3947                                    kill-buffer no-display
3948                                    select-articles)
3949                                   (setq show-all nil
3950                                         select-articles nil)))))
3951                 (eq gnus-auto-select-next 'quietly))
3952       (set-buffer gnus-group-buffer)
3953       ;; The entry function called above goes to the next
3954       ;; group automatically, so we go two groups back
3955       ;; if we are searching for the previous group.
3956       (when backward
3957         (gnus-group-prev-unread-group 2))
3958       (if (not (equal group (gnus-group-group-name)))
3959           (setq group (gnus-group-group-name))
3960         (setq group nil)))
3961     result))
3962
3963 (defun gnus-summary-read-group-1 (group show-all no-article
3964                                         kill-buffer no-display
3965                                         &optional select-articles)
3966   ;; Killed foreign groups can't be entered.
3967   ;;  (when (and (not (gnus-group-native-p group))
3968   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3969   ;;    (error "Dead non-native groups can't be entered"))
3970   (gnus-message 7 "Retrieving newsgroup: %s..."
3971                 (gnus-group-decoded-name group))
3972   (let* ((new-group (gnus-summary-setup-buffer group))
3973          (quit-config (gnus-group-quit-config group))
3974          (did-select (and new-group (gnus-select-newsgroup
3975                                      group show-all select-articles))))
3976     (cond
3977      ;; This summary buffer exists already, so we just select it.
3978      ((not new-group)
3979       (gnus-set-global-variables)
3980       (when kill-buffer
3981         (gnus-kill-or-deaden-summary kill-buffer))
3982       (gnus-configure-windows 'summary 'force)
3983       (gnus-set-mode-line 'summary)
3984       (gnus-summary-position-point)
3985       (message "")
3986       t)
3987      ;; We couldn't select this group.
3988      ((null did-select)
3989       (when (and (eq major-mode 'gnus-summary-mode)
3990                  (not (equal (current-buffer) kill-buffer)))
3991         (kill-buffer (current-buffer))
3992         (if (not quit-config)
3993             (progn
3994               ;; Update the info -- marks might need to be removed,
3995               ;; for instance.
3996               (gnus-summary-update-info)
3997               (set-buffer gnus-group-buffer)
3998               (gnus-group-jump-to-group group)
3999               (gnus-group-next-unread-group 1))
4000           (gnus-handle-ephemeral-exit quit-config)))
4001       (if (null (gnus-list-of-unread-articles group))
4002           (gnus-message 3 "Group %s contains no messages" group)
4003         (gnus-message 3 "Can't select group"))
4004       nil)
4005      ;; The user did a `C-g' while prompting for number of articles,
4006      ;; so we exit this group.
4007      ((eq did-select 'quit)
4008       (and (eq major-mode 'gnus-summary-mode)
4009            (not (equal (current-buffer) kill-buffer))
4010            (kill-buffer (current-buffer)))
4011       (when kill-buffer
4012         (gnus-kill-or-deaden-summary kill-buffer))
4013       (if (not quit-config)
4014           (progn
4015             (set-buffer gnus-group-buffer)
4016             (gnus-group-jump-to-group group)
4017             (gnus-configure-windows 'group 'force))
4018         (gnus-handle-ephemeral-exit quit-config))
4019       ;; Finally signal the quit.
4020       (signal 'quit nil))
4021      ;; The group was successfully selected.
4022      (t
4023       (gnus-set-global-variables)
4024       ;; Save the active value in effect when the group was entered.
4025       (setq gnus-newsgroup-active
4026             (gnus-copy-sequence
4027              (gnus-active gnus-newsgroup-name)))
4028       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
4029       ;; You can change the summary buffer in some way with this hook.
4030       (gnus-run-hooks 'gnus-select-group-hook)
4031       (when (memq 'summary (gnus-update-format-specifications
4032                             nil 'summary 'summary-mode 'summary-dummy))
4033         ;; The format specification for the summary line was updated,
4034         ;; so we need to update the mark positions as well.
4035         (gnus-update-summary-mark-positions))
4036       ;; Do score processing.
4037       (when gnus-use-scoring
4038         (gnus-possibly-score-headers))
4039       ;; Check whether to fill in the gaps in the threads.
4040       (when gnus-build-sparse-threads
4041         (gnus-build-sparse-threads))
4042       ;; Find the initial limit.
4043       (if show-all
4044           (let ((gnus-newsgroup-dormant nil))
4045             (gnus-summary-initial-limit show-all))
4046         (gnus-summary-initial-limit show-all))
4047       ;; Generate the summary buffer.
4048       (unless no-display
4049         (gnus-summary-prepare))
4050       (when gnus-use-trees
4051         (gnus-tree-open group)
4052         (setq gnus-summary-highlight-line-function
4053               'gnus-tree-highlight-article))
4054       ;; If the summary buffer is empty, but there are some low-scored
4055       ;; articles or some excluded dormants, we include these in the
4056       ;; buffer.
4057       (when (and (zerop (buffer-size))
4058                  (not no-display))
4059         (cond (gnus-newsgroup-dormant
4060                (gnus-summary-limit-include-dormant))
4061               ((and gnus-newsgroup-scored show-all)
4062                (gnus-summary-limit-include-expunged t))))
4063       ;; Function `gnus-apply-kill-file' must be called in this hook.
4064       (gnus-run-hooks 'gnus-apply-kill-hook)
4065       (if (and (zerop (buffer-size))
4066                (not no-display))
4067           (progn
4068             ;; This newsgroup is empty.
4069             (gnus-summary-catchup-and-exit nil t)
4070             (gnus-message 6 "No unread news")
4071             (when kill-buffer
4072               (gnus-kill-or-deaden-summary kill-buffer))
4073             ;; Return nil from this function.
4074             nil)
4075         ;; Hide conversation thread subtrees.  We cannot do this in
4076         ;; gnus-summary-prepare-hook since kill processing may not
4077         ;; work with hidden articles.
4078         (gnus-summary-maybe-hide-threads)
4079         (gnus-configure-windows 'summary)
4080         (when kill-buffer
4081           (gnus-kill-or-deaden-summary kill-buffer))
4082         (gnus-summary-auto-select-subject)
4083         ;; Show first unread article if requested.
4084         (if (and (not no-article)
4085                  (not no-display)
4086                  gnus-newsgroup-unreads
4087                  gnus-auto-select-first)
4088             (progn
4089               (let ((art (gnus-summary-article-number)))
4090                 (unless (and (not gnus-plugged)
4091                              (or (memq art gnus-newsgroup-undownloaded)
4092                                  (memq art gnus-newsgroup-downloadable)))
4093                   (gnus-summary-goto-article art))))
4094           ;; Don't select any articles.
4095           (gnus-summary-position-point)
4096           (gnus-configure-windows 'summary 'force)
4097           (gnus-set-mode-line 'summary))
4098         (when (and gnus-auto-center-group
4099                    (get-buffer-window gnus-group-buffer t))
4100           ;; Gotta use windows, because recenter does weird stuff if
4101           ;; the current buffer ain't the displayed window.
4102           (let ((owin (selected-window)))
4103             (select-window (get-buffer-window gnus-group-buffer t))
4104             (when (gnus-group-goto-group group)
4105               (recenter))
4106             (select-window owin)))
4107         ;; Mark this buffer as "prepared".
4108         (setq gnus-newsgroup-prepared t)
4109         (gnus-run-hooks 'gnus-summary-prepared-hook)
4110         (unless (gnus-ephemeral-group-p group)
4111           (gnus-group-update-group group nil t))
4112         t)))))
4113
4114 (defun gnus-summary-auto-select-subject ()
4115   "Select the subject line on initial group entry."
4116   (goto-char (point-min))
4117   (cond
4118    ((eq gnus-auto-select-subject 'best)
4119     (gnus-summary-best-unread-subject))
4120    ((eq gnus-auto-select-subject 'unread)
4121     (gnus-summary-first-unread-subject))
4122    ((eq gnus-auto-select-subject 'unseen)
4123     (gnus-summary-first-unseen-subject))
4124    ((eq gnus-auto-select-subject 'unseen-or-unread)
4125     (gnus-summary-first-unseen-or-unread-subject))
4126    ((eq gnus-auto-select-subject 'first)
4127     ;; Do nothing.
4128     )
4129    ((functionp gnus-auto-select-subject)
4130     (funcall gnus-auto-select-subject))))
4131
4132 (defun gnus-summary-prepare ()
4133   "Generate the summary buffer."
4134   (interactive)
4135   (let ((inhibit-read-only t))
4136     (erase-buffer)
4137     (setq gnus-newsgroup-data nil
4138           gnus-newsgroup-data-reverse nil)
4139     (gnus-run-hooks 'gnus-summary-generate-hook)
4140     ;; Generate the buffer, either with threads or without.
4141     (when gnus-newsgroup-headers
4142       (gnus-summary-prepare-threads
4143        (if gnus-show-threads
4144            (gnus-sort-gathered-threads
4145             (funcall gnus-summary-thread-gathering-function
4146                      (gnus-sort-threads
4147                       (gnus-cut-threads (gnus-make-threads)))))
4148          ;; Unthreaded display.
4149          (gnus-sort-articles gnus-newsgroup-headers))))
4150     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4151     ;; Call hooks for modifying summary buffer.
4152     (goto-char (point-min))
4153     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4154
4155 (defsubst gnus-general-simplify-subject (subject)
4156   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4157   (setq subject
4158         (cond
4159          ;; Truncate the subject.
4160          (gnus-simplify-subject-functions
4161           (gnus-map-function gnus-simplify-subject-functions subject))
4162          ((numberp gnus-summary-gather-subject-limit)
4163           (setq subject (gnus-simplify-subject-re subject))
4164           (if (> (length subject) gnus-summary-gather-subject-limit)
4165               (substring subject 0 gnus-summary-gather-subject-limit)
4166             subject))
4167          ;; Fuzzily simplify it.
4168          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4169           (gnus-simplify-subject-fuzzy subject))
4170          ;; Just remove the leading "Re:".
4171          (t
4172           (gnus-simplify-subject-re subject))))
4173
4174   (if (and gnus-summary-gather-exclude-subject
4175            (string-match gnus-summary-gather-exclude-subject subject))
4176       nil                         ; This article shouldn't be gathered
4177     subject))
4178
4179 (defun gnus-summary-simplify-subject-query ()
4180   "Query where the respool algorithm would put this article."
4181   (interactive)
4182   (gnus-summary-select-article)
4183   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4184
4185 (defun gnus-gather-threads-by-subject (threads)
4186   "Gather threads by looking at Subject headers."
4187   (if (not gnus-summary-make-false-root)
4188       threads
4189     (let ((hashtb (gnus-make-hashtable 1024))
4190           (prev threads)
4191           (result threads)
4192           subject hthread whole-subject)
4193       (while threads
4194         (setq subject (gnus-general-simplify-subject
4195                        (setq whole-subject (mail-header-subject
4196                                             (caar threads)))))
4197         (when subject
4198           (if (setq hthread (gnus-gethash subject hashtb))
4199               (progn
4200                 ;; We enter a dummy root into the thread, if we
4201                 ;; haven't done that already.
4202                 (unless (stringp (caar hthread))
4203                   (setcar hthread (list whole-subject (car hthread))))
4204                 ;; We add this new gathered thread to this gathered
4205                 ;; thread.
4206                 (setcdr (car hthread)
4207                         (nconc (cdar hthread) (list (car threads))))
4208                 ;; Remove it from the list of threads.
4209                 (setcdr prev (cdr threads))
4210                 (setq threads prev))
4211             ;; Enter this thread into the hash table.
4212             (gnus-sethash subject
4213                           (if gnus-summary-make-false-root-always
4214                               (progn
4215                                 ;; If you want a dummy root above all
4216                                 ;; threads...
4217                                 (setcar threads (list whole-subject
4218                                                       (car threads)))
4219                                 threads)
4220                             threads)
4221                           hashtb)))
4222         (setq prev threads)
4223         (setq threads (cdr threads)))
4224       result)))
4225
4226 (defun gnus-gather-threads-by-references (threads)
4227   "Gather threads by looking at References headers."
4228   (let ((idhashtb (gnus-make-hashtable 1024))
4229         (thhashtb (gnus-make-hashtable 1024))
4230         (prev threads)
4231         (result threads)
4232         ids references id gthread gid entered ref)
4233     (while threads
4234       (when (setq references (mail-header-references (caar threads)))
4235         (setq id (mail-header-id (caar threads))
4236               ids (inline (gnus-split-references references))
4237               entered nil)
4238         (while (setq ref (pop ids))
4239           (setq ids (delete ref ids))
4240           (if (not (setq gid (gnus-gethash ref idhashtb)))
4241               (progn
4242                 (gnus-sethash ref id idhashtb)
4243                 (gnus-sethash id threads thhashtb))
4244             (setq gthread (gnus-gethash gid thhashtb))
4245             (unless entered
4246               ;; We enter a dummy root into the thread, if we
4247               ;; haven't done that already.
4248               (unless (stringp (caar gthread))
4249                 (setcar gthread (list (mail-header-subject (caar gthread))
4250                                       (car gthread))))
4251               ;; We add this new gathered thread to this gathered
4252               ;; thread.
4253               (setcdr (car gthread)
4254                       (nconc (cdar gthread) (list (car threads)))))
4255             ;; Add it into the thread hash table.
4256             (gnus-sethash id gthread thhashtb)
4257             (setq entered t)
4258             ;; Remove it from the list of threads.
4259             (setcdr prev (cdr threads))
4260             (setq threads prev))))
4261       (setq prev threads)
4262       (setq threads (cdr threads)))
4263     result))
4264
4265 (defun gnus-sort-gathered-threads (threads)
4266   "Sort subthreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4267   (let ((result threads))
4268     (while threads
4269       (when (stringp (caar threads))
4270         (setcdr (car threads)
4271                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4272       (setq threads (cdr threads)))
4273     result))
4274
4275 (defun gnus-thread-loop-p (root thread)
4276   "Say whether ROOT is in THREAD."
4277   (let ((stack (list thread))
4278         (infloop 0)
4279         th)
4280     (while (setq thread (pop stack))
4281       (setq th (cdr thread))
4282       (while (and th
4283                   (not (eq (caar th) root)))
4284         (pop th))
4285       (if th
4286           ;; We have found a loop.
4287           (let (ref-dep)
4288             (setcdr thread (delq (car th) (cdr thread)))
4289             (if (boundp (setq ref-dep (intern "none"
4290                                               gnus-newsgroup-dependencies)))
4291                 (setcdr (symbol-value ref-dep)
4292                         (nconc (cdr (symbol-value ref-dep))
4293                                (list (car th))))
4294               (set ref-dep (list nil (car th))))
4295             (setq infloop 1
4296                   stack nil))
4297         ;; Push all the subthreads onto the stack.
4298         (push (cdr thread) stack)))
4299     infloop))
4300
4301 (defun gnus-make-threads ()
4302   "Go through the dependency hashtb and find the roots.  Return all threads."
4303   (let (threads)
4304     (while (catch 'infloop
4305              (mapatoms
4306               (lambda (refs)
4307                 ;; Deal with self-referencing References loops.
4308                 (when (and (car (symbol-value refs))
4309                            (not (zerop
4310                                  (apply
4311                                   '+
4312                                   (mapcar
4313                                    (lambda (thread)
4314                                      (gnus-thread-loop-p
4315                                       (car (symbol-value refs)) thread))
4316                                    (cdr (symbol-value refs)))))))
4317                   (setq threads nil)
4318                   (throw 'infloop t))
4319                 (unless (car (symbol-value refs))
4320                   ;; These threads do not refer back to any other
4321                   ;; articles, so they're roots.
4322                   (setq threads (append (cdr (symbol-value refs)) threads))))
4323               gnus-newsgroup-dependencies)))
4324     threads))
4325
4326 ;; Build the thread tree.
4327 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4328   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4329
4330 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4331 if it was already present.
4332
4333 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4334 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4335 Message-IDs will be renamed to a unique Message-ID before being
4336 entered.
4337
4338 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4339   (let* ((id (mail-header-id header))
4340          (id-dep (and id (intern id dependencies)))
4341          parent-id ref ref-dep ref-header replaced)
4342     ;; Enter this `header' in the `dependencies' table.
4343     (cond
4344      ((not id-dep)
4345       (setq header nil))
4346      ;; The first two cases do the normal part: enter a new `header'
4347      ;; in the `dependencies' table.
4348      ((not (boundp id-dep))
4349       (set id-dep (list header)))
4350      ((null (car (symbol-value id-dep)))
4351       (setcar (symbol-value id-dep) header))
4352
4353      ;; From here the `header' was already present in the
4354      ;; `dependencies' table.
4355      (force-new
4356       ;; Overrides an existing entry;
4357       ;; just set the header part of the entry.
4358       (setcar (symbol-value id-dep) header)
4359       (setq replaced t))
4360
4361      ;; Renames the existing `header' to a unique Message-ID.
4362      ((not gnus-summary-ignore-duplicates)
4363       ;; An article with this Message-ID has already been seen.
4364       ;; We rename the Message-ID.
4365       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4366            (list header))
4367       (mail-header-set-id header id))
4368
4369      ;; The last case ignores an existing entry, except it adds any
4370      ;; additional Xrefs (in case the two articles came from different
4371      ;; servers.
4372      ;; Also sets `header' to `nil' meaning that the `dependencies'
4373      ;; table was *not* modified.
4374      (t
4375       (mail-header-set-xref
4376        (car (symbol-value id-dep))
4377        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4378                    "")
4379                (or (mail-header-xref header) "")))
4380       (setq header nil)))
4381
4382     (when (and header (not replaced))
4383       ;; First check that we are not creating a References loop.
4384       (setq parent-id (gnus-parent-id (mail-header-references header)))
4385       (setq ref parent-id)
4386       (while (and ref
4387                   (setq ref-dep (intern-soft ref dependencies))
4388                   (boundp ref-dep)
4389                   (setq ref-header (car (symbol-value ref-dep))))
4390         (if (string= id ref)
4391             ;; Yuk!  This is a reference loop.  Make the article be a
4392             ;; root article.
4393             (progn
4394               (mail-header-set-references (car (symbol-value id-dep)) "none")
4395               (setq ref nil)
4396               (setq parent-id nil))
4397           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4398       (setq ref-dep (intern (or parent-id "none") dependencies))
4399       (if (boundp ref-dep)
4400           (setcdr (symbol-value ref-dep)
4401                   (nconc (cdr (symbol-value ref-dep))
4402                          (list (symbol-value id-dep))))
4403         (set ref-dep (list nil (symbol-value id-dep)))))
4404     header))
4405
4406 (defun gnus-extract-message-id-from-in-reply-to (string)
4407   (if (string-match "<[^>]+>" string)
4408       (substring string (match-beginning 0) (match-end 0))
4409     nil))
4410
4411 (defun gnus-build-sparse-threads ()
4412   (let ((headers gnus-newsgroup-headers)
4413         (mail-parse-charset gnus-newsgroup-charset)
4414         (gnus-summary-ignore-duplicates t)
4415         header references generation relations
4416         subject child end new-child date)
4417     ;; First we create an alist of generations/relations, where
4418     ;; generations is how much we trust the relation, and the relation
4419     ;; is parent/child.
4420     (gnus-message 7 "Making sparse threads...")
4421     (save-excursion
4422       (nnheader-set-temp-buffer " *gnus sparse threads*")
4423       (while (setq header (pop headers))
4424         (when (and (setq references (mail-header-references header))
4425                    (not (string= references "")))
4426           (insert references)
4427           (setq child (mail-header-id header)
4428                 subject (mail-header-subject header)
4429                 date (mail-header-date header)
4430                 generation 0)
4431           (while (search-backward ">" nil t)
4432             (setq end (1+ (point)))
4433             (when (search-backward "<" nil t)
4434               (setq new-child (buffer-substring (point) end))
4435               (push (list (incf generation)
4436                           child (setq child new-child)
4437                           subject date)
4438                     relations)))
4439           (when child
4440             (push (list (1+ generation) child nil subject) relations))
4441           (erase-buffer)))
4442       (kill-buffer (current-buffer)))
4443     ;; Sort over trustworthiness.
4444     (dolist (relation (sort relations 'car-less-than-car))
4445       (when (gnus-dependencies-add-header
4446              (make-full-mail-header
4447               gnus-reffed-article-number
4448               (nth 3 relation) "" (or (nth 4 relation) "")
4449               (nth 1 relation)
4450               (or (nth 2 relation) "") 0 0 "")
4451              gnus-newsgroup-dependencies nil)
4452         (push gnus-reffed-article-number gnus-newsgroup-limit)
4453         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4454         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4455               gnus-newsgroup-reads)
4456         (decf gnus-reffed-article-number)))
4457     (gnus-message 7 "Making sparse threads...done")))
4458
4459 (defun gnus-build-old-threads ()
4460   ;; Look at all the articles that refer back to old articles, and
4461   ;; fetch the headers for the articles that aren't there.  This will
4462   ;; build complete threads - if the roots haven't been expired by the
4463   ;; server, that is.
4464   (let ((mail-parse-charset gnus-newsgroup-charset)
4465         id heads)
4466     (mapatoms
4467      (lambda (refs)
4468        (when (not (car (symbol-value refs)))
4469          (setq heads (cdr (symbol-value refs)))
4470          (while heads
4471            (if (memq (mail-header-number (caar heads))
4472                      gnus-newsgroup-dormant)
4473                (setq heads (cdr heads))
4474              (setq id (symbol-name refs))
4475              (while (and (setq id (gnus-build-get-header id))
4476                          (not (car (gnus-id-to-thread id)))))
4477              (setq heads nil)))))
4478      gnus-newsgroup-dependencies)))
4479
4480 (defsubst gnus-remove-odd-characters (string)
4481   "Translate STRING into something that doesn't contain weird characters."
4482   (mm-subst-char-in-string
4483    ?\r ?\-
4484    (mm-subst-char-in-string ?\n ?\- string t) t))
4485
4486 ;; This function has to be called with point after the article number
4487 ;; on the beginning of the line.
4488 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4489   (let ((eol (point-at-eol))
4490         (buffer (current-buffer))
4491         header references in-reply-to)
4492
4493     ;; overview: [num subject from date id refs chars lines misc]
4494     (unwind-protect
4495         (let (x)
4496           (narrow-to-region (point) eol)
4497           (unless (eobp)
4498             (forward-char))
4499
4500           (setq header
4501                 (make-full-mail-header
4502                  number                 ; number
4503                  (condition-case ()     ; subject
4504                      (gnus-remove-odd-characters
4505                       (funcall gnus-decode-encoded-word-function
4506                                (setq x (nnheader-nov-field))))
4507                    (error x))
4508                  (condition-case ()     ; from
4509                      (gnus-remove-odd-characters
4510                       (funcall gnus-decode-encoded-address-function
4511                                (setq x (nnheader-nov-field))))
4512                    (error x))
4513                  (nnheader-nov-field)   ; date
4514                  (nnheader-nov-read-message-id number)  ; id
4515                  (setq references (nnheader-nov-field)) ; refs
4516                  (nnheader-nov-read-integer) ; chars
4517                  (nnheader-nov-read-integer) ; lines
4518                  (unless (eobp)
4519                    (if (looking-at "Xref: ")
4520                        (goto-char (match-end 0)))
4521                    (nnheader-nov-field)) ; Xref
4522                  (nnheader-nov-parse-extra)))) ; extra
4523
4524       (widen))
4525
4526     (when (and (string= references "")
4527                (setq in-reply-to (mail-header-extra header))
4528                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4529       (mail-header-set-references
4530        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4531
4532     (when gnus-alter-header-function
4533       (funcall gnus-alter-header-function header))
4534     (gnus-dependencies-add-header header dependencies force-new)))
4535
4536 (defun gnus-build-get-header (id)
4537   "Look through the buffer of NOV lines and find the header to ID.
4538 Enter this line into the dependencies hash table, and return
4539 the id of the parent article (if any)."
4540   (let ((deps gnus-newsgroup-dependencies)
4541         found header)
4542     (prog1
4543         (with-current-buffer nntp-server-buffer
4544           (let ((case-fold-search nil))
4545             (goto-char (point-min))
4546             (while (and (not found)
4547                         (search-forward id nil t))
4548               (beginning-of-line)
4549               (setq found (looking-at
4550                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4551                                    (regexp-quote id))))
4552               (or found (beginning-of-line 2)))
4553             (when found
4554               (beginning-of-line)
4555               (and
4556                (setq header (gnus-nov-parse-line
4557                              (read (current-buffer)) deps))
4558                (gnus-parent-id (mail-header-references header))))))
4559       (when header
4560         (let ((number (mail-header-number header)))
4561           (push number gnus-newsgroup-limit)
4562           (push header gnus-newsgroup-headers)
4563           (if (memq number gnus-newsgroup-unselected)
4564               (progn
4565                 (setq gnus-newsgroup-unreads
4566                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4567                                                number))
4568                 (setq gnus-newsgroup-unselected
4569                       (delq number gnus-newsgroup-unselected)))
4570             (push number gnus-newsgroup-ancient)))))))
4571
4572 (defun gnus-build-all-threads ()
4573   "Read all the headers."
4574   (let ((gnus-summary-ignore-duplicates t)
4575         (mail-parse-charset gnus-newsgroup-charset)
4576         (dependencies gnus-newsgroup-dependencies)
4577         header article)
4578     (with-current-buffer nntp-server-buffer
4579       (let ((case-fold-search nil))
4580         (goto-char (point-min))
4581         (while (not (eobp))
4582           (ignore-errors
4583             (setq article (read (current-buffer))
4584                   header (gnus-nov-parse-line article dependencies t)))
4585           (when header
4586             (with-current-buffer gnus-summary-buffer
4587               (push header gnus-newsgroup-headers)
4588               (if (memq (setq article (mail-header-number header))
4589                         gnus-newsgroup-unselected)
4590                   (progn
4591                     (setq gnus-newsgroup-unreads
4592                           (gnus-add-to-sorted-list
4593                            gnus-newsgroup-unreads article))
4594                     (setq gnus-newsgroup-unselected
4595                           (delq article gnus-newsgroup-unselected)))
4596                 (push article gnus-newsgroup-ancient)))
4597             (forward-line 1)))))))
4598
4599 (defun gnus-summary-update-article-line (article header)
4600   "Update the line for ARTICLE using HEADER."
4601   (let* ((id (mail-header-id header))
4602          (thread (gnus-id-to-thread id)))
4603     (unless thread
4604       (error "Article in no thread"))
4605     ;; Update the thread.
4606     (setcar thread header)
4607     (gnus-summary-goto-subject article)
4608     (let* ((datal (gnus-data-find-list article))
4609            (data (car datal))
4610            (inhibit-read-only t)
4611            (level (gnus-summary-thread-level)))
4612       (gnus-delete-line)
4613       (let ((inserted (- (point)
4614                          (progn
4615                            (gnus-summary-insert-line
4616                             header level nil
4617                             (memq article gnus-newsgroup-undownloaded)
4618                             (gnus-article-mark article)
4619                             (memq article gnus-newsgroup-replied)
4620                             (memq article gnus-newsgroup-expirable)
4621                             ;; Only insert the Subject string when it's different
4622                             ;; from the previous Subject string.
4623                             (if (and
4624                                  gnus-show-threads
4625                                  (gnus-subject-equal
4626                                   (condition-case ()
4627                                       (mail-header-subject
4628                                        (gnus-data-header
4629                                         (cadr
4630                                          (gnus-data-find-list
4631                                           article
4632                                           (gnus-data-list t)))))
4633                                     ;; Error on the side of excessive subjects.
4634                                     (error ""))
4635                                   (mail-header-subject header)))
4636                                 ""
4637                               (mail-header-subject header))
4638                             nil (cdr (assq article gnus-newsgroup-scored))
4639                             (memq article gnus-newsgroup-processable))
4640                            (point)))))
4641         (when (cdr datal)
4642           (gnus-data-update-list
4643            (cdr datal)
4644            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4645
4646 (defun gnus-summary-update-article (article &optional iheader)
4647   "Update ARTICLE in the summary buffer."
4648   (set-buffer gnus-summary-buffer)
4649   (let* ((header (gnus-summary-article-header article))
4650          (id (mail-header-id header))
4651          (data (gnus-data-find article))
4652          (thread (gnus-id-to-thread id))
4653          (references (mail-header-references header))
4654          (parent
4655           (gnus-id-to-thread
4656            (or (gnus-parent-id
4657                 (when (and references
4658                            (not (equal "" references)))
4659                   references))
4660                "none")))
4661          (inhibit-read-only t)
4662          (old (car thread)))
4663     (when thread
4664       (unless iheader
4665         (setcar thread nil)
4666         (when parent
4667           (delq thread parent)))
4668       (if (gnus-summary-insert-subject id header)
4669           ;; Set the (possibly) new article number in the data structure.
4670           (gnus-data-set-number data (gnus-id-to-article id))
4671         (setcar thread old)
4672         nil))))
4673
4674 (defun gnus-rebuild-thread (id &optional line)
4675   "Rebuild the thread containing ID.
4676 If LINE, insert the rebuilt thread starting on line LINE."
4677   (let ((inhibit-read-only t)
4678         old-pos current thread data)
4679     (if (not gnus-show-threads)
4680         (setq thread (list (car (gnus-id-to-thread id))))
4681       ;; Get the thread this article is part of.
4682       (setq thread (gnus-remove-thread id)))
4683     (setq old-pos (point-at-bol))
4684     (setq current (save-excursion
4685                     (and (re-search-backward "[\r\n]" nil t)
4686                          (gnus-summary-article-number))))
4687     ;; If this is a gathered thread, we have to go some re-gathering.
4688     (when (stringp (car thread))
4689       (let ((subject (car thread))
4690             roots thr)
4691         (setq thread (cdr thread))
4692         (while thread
4693           (unless (memq (setq thr (gnus-id-to-thread
4694                                    (gnus-root-id
4695                                     (mail-header-id (caar thread)))))
4696                         roots)
4697             (push thr roots))
4698           (setq thread (cdr thread)))
4699         ;; We now have all (unique) roots.
4700         (if (= (length roots) 1)
4701             ;; All the loose roots are now one solid root.
4702             (setq thread (car roots))
4703           (setq thread (cons subject (gnus-sort-threads roots))))))
4704     (let (threads)
4705       ;; We then insert this thread into the summary buffer.
4706       (when line
4707         (goto-char (point-min))
4708         (forward-line (1- line)))
4709       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4710         (if gnus-show-threads
4711             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4712           (gnus-summary-prepare-unthreaded thread))
4713         (setq data (nreverse gnus-newsgroup-data))
4714         (setq threads gnus-newsgroup-threads))
4715       ;; We splice the new data into the data structure.
4716       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4717       ;;!!! then we want to insert at the beginning of the buffer.
4718       ;;!!! That happens to be true with Gnus now, but that may
4719       ;;!!! change in the future.  Perhaps.
4720       (gnus-data-enter-list
4721        (if line nil current) data (- (point) old-pos))
4722       (setq gnus-newsgroup-threads
4723             (nconc threads gnus-newsgroup-threads))
4724       (gnus-data-compute-positions))))
4725
4726 (defun gnus-number-to-header (number)
4727   "Return the header for article NUMBER."
4728   (let ((headers gnus-newsgroup-headers))
4729     (while (and headers
4730                 (not (= number (mail-header-number (car headers)))))
4731       (pop headers))
4732     (when headers
4733       (car headers))))
4734
4735 (defun gnus-parent-headers (in-headers &optional generation)
4736   "Return the headers of the GENERATIONth parent of HEADERS."
4737   (unless generation
4738     (setq generation 1))
4739   (let ((parent t)
4740         (headers in-headers)
4741         references)
4742     (while (and parent
4743                 (not (zerop generation))
4744                 (setq references (mail-header-references headers)))
4745       (setq headers (if (and references
4746                              (setq parent (gnus-parent-id references)))
4747                         (car (gnus-id-to-thread parent))
4748                       nil))
4749       (decf generation))
4750     (and (not (eq headers in-headers))
4751          headers)))
4752
4753 (defun gnus-id-to-thread (id)
4754   "Return the (sub-)thread where ID appears."
4755   (gnus-gethash id gnus-newsgroup-dependencies))
4756
4757 (defun gnus-id-to-article (id)
4758   "Return the article number of ID."
4759   (let ((thread (gnus-id-to-thread id)))
4760     (when (and thread
4761                (car thread))
4762       (mail-header-number (car thread)))))
4763
4764 (defun gnus-id-to-header (id)
4765   "Return the article headers of ID."
4766   (car (gnus-id-to-thread id)))
4767
4768 (defun gnus-article-displayed-root-p (article)
4769   "Say whether ARTICLE is a root(ish) article."
4770   (let ((level (gnus-summary-thread-level article))
4771         (refs (mail-header-references  (gnus-summary-article-header article)))
4772         particle)
4773     (cond
4774      ((null level) nil)
4775      ((zerop level) t)
4776      ((null refs) t)
4777      ((null (gnus-parent-id refs)) t)
4778      ((and (= 1 level)
4779            (null (setq particle (gnus-id-to-article
4780                                  (gnus-parent-id refs))))
4781            (null (gnus-summary-thread-level particle)))))))
4782
4783 (defun gnus-root-id (id)
4784   "Return the id of the root of the thread where ID appears."
4785   (let (last-id prev)
4786     (while (and id (setq prev (car (gnus-id-to-thread id))))
4787       (setq last-id id
4788             id (gnus-parent-id (mail-header-references prev))))
4789     last-id))
4790
4791 (defun gnus-articles-in-thread (thread)
4792   "Return the list of articles in THREAD."
4793   (cons (mail-header-number (car thread))
4794         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4795
4796 (defun gnus-remove-thread (id &optional dont-remove)
4797   "Remove the thread that has ID in it."
4798   (let (headers thread last-id)
4799     ;; First go up in this thread until we find the root.
4800     (setq last-id (gnus-root-id id)
4801           headers (message-flatten-list (gnus-id-to-thread last-id)))
4802     ;; We have now found the real root of this thread.  It might have
4803     ;; been gathered into some loose thread, so we have to search
4804     ;; through the threads to find the thread we wanted.
4805     (let ((threads gnus-newsgroup-threads)
4806           sub)
4807       (while threads
4808         (setq sub (car threads))
4809         (if (stringp (car sub))
4810             ;; This is a gathered thread, so we look at the roots
4811             ;; below it to find whether this article is in this
4812             ;; gathered root.
4813             (progn
4814               (setq sub (cdr sub))
4815               (while sub
4816                 (when (member (caar sub) headers)
4817                   (setq thread (car threads)
4818                         threads nil
4819                         sub nil))
4820                 (setq sub (cdr sub))))
4821           ;; It's an ordinary thread, so we check it.
4822           (when (eq (car sub) (car headers))
4823             (setq thread sub
4824                   threads nil)))
4825         (setq threads (cdr threads)))
4826       ;; If this article is in no thread, then it's a root.
4827       (if thread
4828           (unless dont-remove
4829             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4830         (setq thread (gnus-id-to-thread last-id)))
4831       (when thread
4832         (prog1
4833             thread                      ; We return this thread.
4834           (unless dont-remove
4835             (if (stringp (car thread))
4836                 (progn
4837                   ;; If we use dummy roots, then we have to remove the
4838                   ;; dummy root as well.
4839                   (when (eq gnus-summary-make-false-root 'dummy)
4840                     ;; We go to the dummy root by going to
4841                     ;; the first sub-"thread", and then one line up.
4842                     (gnus-summary-goto-article
4843                      (mail-header-number (caadr thread)))
4844                     (forward-line -1)
4845                     (gnus-delete-line)
4846                     (gnus-data-compute-positions))
4847                   (setq thread (cdr thread))
4848                   (while thread
4849                     (gnus-remove-thread-1 (car thread))
4850                     (setq thread (cdr thread))))
4851               (gnus-remove-thread-1 thread))))))))
4852
4853 (defun gnus-remove-thread-1 (thread)
4854   "Remove the thread THREAD recursively."
4855   (let ((number (mail-header-number (pop thread)))
4856         d)
4857     (setq thread (reverse thread))
4858     (while thread
4859       (gnus-remove-thread-1 (pop thread)))
4860     (when (setq d (gnus-data-find number))
4861       (goto-char (gnus-data-pos d))
4862       (gnus-summary-show-thread)
4863       (gnus-data-remove
4864        number
4865        (- (point-at-bol)
4866           (prog1
4867               (1+ (point-at-eol))
4868             (gnus-delete-line)))))))
4869
4870 (defun gnus-sort-threads-recursive (threads func)
4871   (sort (mapcar (lambda (thread)
4872                   (cons (car thread)
4873                         (and (cdr thread)
4874                              (gnus-sort-threads-recursive (cdr thread) func))))
4875                 threads) func))
4876
4877 (defun gnus-sort-threads-loop (threads func)
4878   (let* ((superthread (cons nil threads))
4879          (stack (list (cons superthread threads)))
4880          remaining-threads thread)
4881     (while stack
4882       (setq remaining-threads (cdr (car stack)))
4883       (if remaining-threads
4884           (progn (setq thread (car remaining-threads))
4885                  (setcdr (car stack) (cdr remaining-threads))
4886                  (if (cdr thread)
4887                      (push (cons thread (cdr thread)) stack)))
4888         (setq thread (caar stack))
4889         (setcdr thread (sort (cdr thread) func))
4890         (pop stack)))
4891     (cdr superthread)))
4892
4893 (defun gnus-sort-threads (threads)
4894   "Sort THREADS."
4895   (if (not gnus-thread-sort-functions)
4896       threads
4897     (gnus-message 8 "Sorting threads...")
4898     (prog1
4899         (condition-case nil
4900             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4901               (gnus-sort-threads-recursive
4902                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4903           ;; Even after binding max-lisp-eval-depth, the recursive
4904           ;; sorter might fail for very long threads.  In that case,
4905           ;; try using a (less well-tested) non-recursive sorter.
4906           (error (gnus-message 9 "Sorting threads with loop...")
4907                  (gnus-sort-threads-loop
4908                   threads (gnus-make-sort-function
4909                            gnus-thread-sort-functions))))
4910       (gnus-message 8 "Sorting threads...done"))))
4911
4912 (defun gnus-sort-articles (articles)
4913   "Sort ARTICLES."
4914   (when gnus-article-sort-functions
4915     (gnus-message 7 "Sorting articles...")
4916     (prog1
4917         (setq gnus-newsgroup-headers
4918               (sort articles (gnus-make-sort-function
4919                               gnus-article-sort-functions)))
4920       (gnus-message 7 "Sorting articles...done"))))
4921
4922 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4923 (defmacro gnus-thread-header (thread)
4924   "Return header of first article in THREAD.
4925 Note that THREAD must never, ever be anything else than a variable -
4926 using some other form will lead to serious barfage."
4927   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4928   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4929   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4930         (vector thread) 2))
4931
4932 (defsubst gnus-article-sort-by-number (h1 h2)
4933   "Sort articles by article number."
4934   (< (mail-header-number h1)
4935      (mail-header-number h2)))
4936
4937 (defun gnus-thread-sort-by-number (h1 h2)
4938   "Sort threads by root article number."
4939   (gnus-article-sort-by-number
4940    (gnus-thread-header h1) (gnus-thread-header h2)))
4941
4942 (defsubst gnus-article-sort-by-random (h1 h2)
4943   "Sort articles randomly."
4944   (zerop (random 2)))
4945
4946 (defun gnus-thread-sort-by-random (h1 h2)
4947   "Sort threads randomly."
4948   (gnus-article-sort-by-random
4949    (gnus-thread-header h1) (gnus-thread-header h2)))
4950
4951 (defsubst gnus-article-sort-by-lines (h1 h2)
4952   "Sort articles by article Lines header."
4953   (< (mail-header-lines h1)
4954      (mail-header-lines h2)))
4955
4956 (defun gnus-thread-sort-by-lines (h1 h2)
4957   "Sort threads by root article Lines header."
4958   (gnus-article-sort-by-lines
4959    (gnus-thread-header h1) (gnus-thread-header h2)))
4960
4961 (defsubst gnus-article-sort-by-chars (h1 h2)
4962   "Sort articles by octet length."
4963   (< (mail-header-chars h1)
4964      (mail-header-chars h2)))
4965
4966 (defun gnus-thread-sort-by-chars (h1 h2)
4967   "Sort threads by root article octet length."
4968   (gnus-article-sort-by-chars
4969    (gnus-thread-header h1) (gnus-thread-header h2)))
4970
4971 (defsubst gnus-article-sort-by-author (h1 h2)
4972   "Sort articles by root author."
4973   (gnus-string<
4974    (let ((extract (funcall
4975                    gnus-extract-address-components
4976                    (mail-header-from h1))))
4977      (or (car extract) (cadr extract) ""))
4978    (let ((extract (funcall
4979                    gnus-extract-address-components
4980                    (mail-header-from h2))))
4981      (or (car extract) (cadr extract) ""))))
4982
4983 (defun gnus-thread-sort-by-author (h1 h2)
4984   "Sort threads by root author."
4985   (gnus-article-sort-by-author
4986    (gnus-thread-header h1)  (gnus-thread-header h2)))
4987
4988 (defsubst gnus-article-sort-by-recipient (h1 h2)
4989   "Sort articles by recipient."
4990   (gnus-string<
4991    (let ((extract (funcall
4992                    gnus-extract-address-components
4993                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4994      (or (car extract) (cadr extract)))
4995    (let ((extract (funcall
4996                    gnus-extract-address-components
4997                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4998      (or (car extract) (cadr extract)))))
4999
5000 (defun gnus-thread-sort-by-recipient (h1 h2)
5001   "Sort threads by root recipient."
5002   (gnus-article-sort-by-recipient
5003    (gnus-thread-header h1) (gnus-thread-header h2)))
5004
5005 (defsubst gnus-article-sort-by-subject (h1 h2)
5006   "Sort articles by root subject."
5007   (gnus-string<
5008    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
5009    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
5010
5011 (defun gnus-thread-sort-by-subject (h1 h2)
5012   "Sort threads by root subject."
5013   (gnus-article-sort-by-subject
5014    (gnus-thread-header h1) (gnus-thread-header h2)))
5015
5016 (defsubst gnus-article-sort-by-date (h1 h2)
5017   "Sort articles by root article date."
5018   (time-less-p
5019    (gnus-date-get-time (mail-header-date h1))
5020    (gnus-date-get-time (mail-header-date h2))))
5021
5022 (defun gnus-thread-sort-by-date (h1 h2)
5023   "Sort threads by root article date."
5024   (gnus-article-sort-by-date
5025    (gnus-thread-header h1) (gnus-thread-header h2)))
5026
5027 (defsubst gnus-article-sort-by-score (h1 h2)
5028   "Sort articles by root article score.
5029 Unscored articles will be counted as having a score of zero."
5030   (> (or (cdr (assq (mail-header-number h1)
5031                     gnus-newsgroup-scored))
5032          gnus-summary-default-score 0)
5033      (or (cdr (assq (mail-header-number h2)
5034                     gnus-newsgroup-scored))
5035          gnus-summary-default-score 0)))
5036
5037 (defun gnus-thread-sort-by-score (h1 h2)
5038   "Sort threads by root article score."
5039   (gnus-article-sort-by-score
5040    (gnus-thread-header h1) (gnus-thread-header h2)))
5041
5042 (defun gnus-thread-sort-by-total-score (h1 h2)
5043   "Sort threads by the sum of all scores in the thread.
5044 Unscored articles will be counted as having a score of zero."
5045   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5046
5047 (defun gnus-thread-total-score (thread)
5048   ;; This function find the total score of THREAD.
5049   (cond
5050    ((null thread)
5051     0)
5052    ((consp thread)
5053     (if (stringp (car thread))
5054         (apply gnus-thread-score-function 0
5055                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5056       (gnus-thread-total-score-1 thread)))
5057    (t
5058     (gnus-thread-total-score-1 (list thread)))))
5059
5060 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5061   "Sort articles by number."
5062   (gnus-article-sort-by-number h1 h2))
5063
5064 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5065   "Sort threads such that the thread with the most recently arrived article comes first."
5066   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5067
5068 (defun gnus-thread-highest-number (thread)
5069   "Return the highest article number in THREAD."
5070   (apply 'max (mapcar (lambda (header)
5071                         (mail-header-number header))
5072                       (message-flatten-list thread))))
5073
5074 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5075   "Sort articles by number."
5076   (gnus-article-sort-by-date h1 h2))
5077
5078 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5079   "Sort threads such that the thread with the most recently dated article comes first."
5080   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5081
5082 ; Since this is called not only to sort the top-level threads, but
5083 ; also in recursive sorts to order the articles within a thread, each
5084 ; article will be processed many times.  Thus it speeds things up
5085 ; quite a bit to use gnus-date-get-time, which caches the time value.
5086 (defun gnus-thread-latest-date (thread)
5087   "Return the highest article date in THREAD."
5088   (apply 'max
5089          (mapcar (lambda (header) (gnus-float-time
5090                                    (gnus-date-get-time
5091                                     (mail-header-date header))))
5092                  (message-flatten-list thread))))
5093
5094 (defun gnus-thread-total-score-1 (root)
5095   ;; This function find the total score of the thread below ROOT.
5096   (setq root (car root))
5097   (apply gnus-thread-score-function
5098          (or (append
5099               (mapcar 'gnus-thread-total-score
5100                       (cdr (gnus-id-to-thread (mail-header-id root))))
5101               (when (> (mail-header-number root) 0)
5102                 (list (or (cdr (assq (mail-header-number root)
5103                                      gnus-newsgroup-scored))
5104                           gnus-summary-default-score 0))))
5105              (list gnus-summary-default-score)
5106              '(0))))
5107
5108 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5109 (defvar gnus-tmp-prev-subject nil)
5110 (defvar gnus-tmp-false-parent nil)
5111 (defvar gnus-tmp-root-expunged nil)
5112 (defvar gnus-tmp-dummy-line nil)
5113
5114 (defun gnus-extra-header (type &optional header)
5115   "Return the extra header of TYPE."
5116   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5117       ""))
5118
5119 (defvar gnus-tmp-thread-tree-header-string "")
5120
5121 (defcustom gnus-sum-thread-tree-root "> "
5122   "With %B spec, used for the root of a thread.
5123 If nil, use subject instead."
5124   :version "22.1"
5125   :type '(radio (const :format "%v  " nil) string)
5126   :group 'gnus-thread)
5127
5128 (defcustom gnus-sum-thread-tree-false-root "> "
5129   "With %B spec, used for a false root of a thread.
5130 If nil, use subject instead."
5131   :version "22.1"
5132   :type '(radio (const :format "%v  " nil) string)
5133   :group 'gnus-thread)
5134
5135 (defcustom gnus-sum-thread-tree-single-indent ""
5136   "With %B spec, used for a thread with just one message.
5137 If nil, use subject instead."
5138   :version "22.1"
5139   :type '(radio (const :format "%v  " nil) string)
5140   :group 'gnus-thread)
5141
5142 (defcustom gnus-sum-thread-tree-vertical "| "
5143   "With %B spec, used for drawing a vertical line."
5144   :version "22.1"
5145   :type 'string
5146   :group 'gnus-thread)
5147
5148 (defcustom gnus-sum-thread-tree-indent "  "
5149   "With %B spec, used for indenting."
5150   :version "22.1"
5151   :type 'string
5152   :group 'gnus-thread)
5153
5154 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5155   "With %B spec, used for a leaf with brothers."
5156   :version "22.1"
5157   :type 'string
5158   :group 'gnus-thread)
5159
5160 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5161   "With %B spec, used for a leaf without brothers."
5162   :version "22.1"
5163   :type 'string
5164   :group 'gnus-thread)
5165
5166 (defcustom gnus-summary-display-while-building nil
5167   "If non-nil, show and update the summary buffer as it's being built.
5168 If the value is t, update the buffer after every line is inserted.  If
5169 the value is an integer (N), update the display every N lines."
5170   :version "22.1"
5171   :group 'gnus-thread
5172   :type '(choice (const :tag "off" nil)
5173                  number
5174                  (const :tag "frequently" t)))
5175
5176 (defun gnus-summary-prepare-threads (threads)
5177   "Prepare summary buffer from THREADS and indentation LEVEL.
5178 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5179 or a straight list of headers."
5180   (gnus-message 7 "Generating summary...")
5181
5182   (setq gnus-newsgroup-threads threads)
5183   (beginning-of-line)
5184
5185   (let ((gnus-tmp-level 0)
5186         (default-score (or gnus-summary-default-score 0))
5187         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5188         (building-line-count gnus-summary-display-while-building)
5189         (building-count (integerp gnus-summary-display-while-building))
5190         thread number subject stack state gnus-tmp-gathered beg-match
5191         new-roots gnus-tmp-new-adopts thread-end simp-subject
5192         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5193         gnus-tmp-replied gnus-tmp-subject-or-nil
5194         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5195         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5196         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5197         tree-stack)
5198
5199     (setq gnus-tmp-prev-subject nil
5200           gnus-tmp-thread-tree-header-string "")
5201
5202     (if (vectorp (car threads))
5203         ;; If this is a straight (sic) list of headers, then a
5204         ;; threaded summary display isn't required, so we just create
5205         ;; an unthreaded one.
5206         (gnus-summary-prepare-unthreaded threads)
5207
5208       ;; Do the threaded display.
5209
5210       (if gnus-summary-display-while-building
5211           (switch-to-buffer (buffer-name)))
5212       (while (or threads stack gnus-tmp-new-adopts new-roots)
5213
5214         (if (and (= gnus-tmp-level 0)
5215                  (or (not stack)
5216                      (= (caar stack) 0))
5217                  (not gnus-tmp-false-parent)
5218                  (or gnus-tmp-new-adopts new-roots))
5219             (if gnus-tmp-new-adopts
5220                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5221                       thread (list (car gnus-tmp-new-adopts))
5222                       gnus-tmp-header (caar thread)
5223                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5224               (when new-roots
5225                 (setq thread (list (car new-roots))
5226                       gnus-tmp-header (caar thread)
5227                       new-roots (cdr new-roots))))
5228
5229           (if threads
5230               ;; If there are some threads, we do them before the
5231               ;; threads on the stack.
5232               (setq thread threads
5233                     gnus-tmp-header (caar thread))
5234             ;; There were no current threads, so we pop something off
5235             ;; the stack.
5236             (setq state (car stack)
5237                   gnus-tmp-level (car state)
5238                   tree-stack (cadr state)
5239                   thread (caddr state)
5240                   stack (cdr stack)
5241                   gnus-tmp-header (caar thread))))
5242
5243         (setq gnus-tmp-false-parent nil)
5244         (setq gnus-tmp-root-expunged nil)
5245         (setq thread-end nil)
5246
5247         (if (stringp gnus-tmp-header)
5248             ;; The header is a dummy root.
5249             (cond
5250              ((eq gnus-summary-make-false-root 'adopt)
5251               ;; We let the first article adopt the rest.
5252               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5253                                                (cddar thread)))
5254               (setq gnus-tmp-gathered
5255                     (nconc (mapcar
5256                             (lambda (h) (mail-header-number (car h)))
5257                             (cddar thread))
5258                            gnus-tmp-gathered))
5259               (setq thread (cons (list (caar thread)
5260                                        (cadar thread))
5261                                  (cdr thread)))
5262               (setq gnus-tmp-level -1
5263                     gnus-tmp-false-parent t))
5264              ((eq gnus-summary-make-false-root 'empty)
5265               ;; We print adopted articles with empty subject fields.
5266               (setq gnus-tmp-gathered
5267                     (nconc (mapcar
5268                             (lambda (h) (mail-header-number (car h)))
5269                             (cddar thread))
5270                            gnus-tmp-gathered))
5271               (setq gnus-tmp-level -1))
5272              ((eq gnus-summary-make-false-root 'dummy)
5273               ;; We remember that we probably want to output a dummy
5274               ;; root.
5275               (setq gnus-tmp-dummy-line gnus-tmp-header)
5276               (setq gnus-tmp-prev-subject gnus-tmp-header))
5277              (t
5278               ;; We do not make a root for the gathered
5279               ;; sub-threads at all.
5280               (setq gnus-tmp-level -1)))
5281
5282           (setq number (mail-header-number gnus-tmp-header)
5283                 subject (mail-header-subject gnus-tmp-header)
5284                 simp-subject (gnus-simplify-subject-fully subject))
5285
5286           (cond
5287            ;; If the thread has changed subject, we might want to make
5288            ;; this subthread into a root.
5289            ((and (null gnus-thread-ignore-subject)
5290                  (not (zerop gnus-tmp-level))
5291                  gnus-tmp-prev-subject
5292                  (not (string= gnus-tmp-prev-subject simp-subject)))
5293             (setq new-roots (nconc new-roots (list (car thread)))
5294                   thread-end t
5295                   gnus-tmp-header nil))
5296            ;; If the article lies outside the current limit,
5297            ;; then we do not display it.
5298            ((not (memq number gnus-newsgroup-limit))
5299             (setq gnus-tmp-gathered
5300                   (nconc (mapcar
5301                           (lambda (h) (mail-header-number (car h)))
5302                           (cdar thread))
5303                          gnus-tmp-gathered))
5304             (setq gnus-tmp-new-adopts (if (cdar thread)
5305                                           (append gnus-tmp-new-adopts
5306                                                   (cdar thread))
5307                                         gnus-tmp-new-adopts)
5308                   thread-end t
5309                   gnus-tmp-header nil)
5310             (when (zerop gnus-tmp-level)
5311               (setq gnus-tmp-root-expunged t)))
5312            ;; Perhaps this article is to be marked as read?
5313            ((and gnus-summary-mark-below
5314                  (< (or (cdr (assq number gnus-newsgroup-scored))
5315                         default-score)
5316                     gnus-summary-mark-below)
5317                  ;; Don't touch sparse articles.
5318                  (not (gnus-summary-article-sparse-p number))
5319                  (not (gnus-summary-article-ancient-p number)))
5320             (setq gnus-newsgroup-unreads
5321                   (delq number gnus-newsgroup-unreads))
5322             (if gnus-newsgroup-auto-expire
5323                 (setq gnus-newsgroup-expirable
5324                       (gnus-add-to-sorted-list
5325                        gnus-newsgroup-expirable number))
5326               (push (cons number gnus-low-score-mark)
5327                     gnus-newsgroup-reads))))
5328
5329           (when gnus-tmp-header
5330             ;; We may have an old dummy line to output before this
5331             ;; article.
5332             (when (and gnus-tmp-dummy-line
5333                        (gnus-subject-equal
5334                         gnus-tmp-dummy-line
5335                         (mail-header-subject gnus-tmp-header)))
5336               (gnus-summary-insert-dummy-line
5337                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5338               (setq gnus-tmp-dummy-line nil))
5339
5340             ;; Compute the mark.
5341             (setq gnus-tmp-unread (gnus-article-mark number))
5342
5343             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5344                                   gnus-tmp-header gnus-tmp-level)
5345                   gnus-newsgroup-data)
5346
5347             ;; Actually insert the line.
5348             (setq
5349              gnus-tmp-subject-or-nil
5350              (cond
5351               ((and gnus-thread-ignore-subject
5352                     gnus-tmp-prev-subject
5353                     (not (string= gnus-tmp-prev-subject simp-subject)))
5354                subject)
5355               ((zerop gnus-tmp-level)
5356                (if (and (eq gnus-summary-make-false-root 'empty)
5357                         (memq number gnus-tmp-gathered)
5358                         gnus-tmp-prev-subject
5359                         (string= gnus-tmp-prev-subject simp-subject))
5360                    gnus-summary-same-subject
5361                  subject))
5362               (t gnus-summary-same-subject)))
5363             (if (and (eq gnus-summary-make-false-root 'adopt)
5364                      (= gnus-tmp-level 1)
5365                      (memq number gnus-tmp-gathered))
5366                 (setq gnus-tmp-opening-bracket ?\<
5367                       gnus-tmp-closing-bracket ?\>)
5368               (setq gnus-tmp-opening-bracket ?\[
5369                     gnus-tmp-closing-bracket ?\]))
5370             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5371                 (gnus-make-thread-indent-array
5372                  (max (* 2 (length gnus-thread-indent-array))
5373                       gnus-tmp-level)))
5374             (setq
5375              gnus-tmp-indentation
5376              (aref gnus-thread-indent-array gnus-tmp-level)
5377              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5378              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5379                                 gnus-summary-default-score 0)
5380              gnus-tmp-score-char
5381              (if (or (null gnus-summary-default-score)
5382                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5383                          gnus-summary-zcore-fuzz))
5384                  ?                      ;Whitespace
5385                (if (< gnus-tmp-score gnus-summary-default-score)
5386                    gnus-score-below-mark gnus-score-over-mark))
5387              gnus-tmp-replied
5388              (cond ((memq number gnus-newsgroup-processable)
5389                     gnus-process-mark)
5390                    ((memq number gnus-newsgroup-cached)
5391                     gnus-cached-mark)
5392                    ((memq number gnus-newsgroup-replied)
5393                     gnus-replied-mark)
5394                    ((memq number gnus-newsgroup-forwarded)
5395                     gnus-forwarded-mark)
5396                    ((memq number gnus-newsgroup-saved)
5397                     gnus-saved-mark)
5398                    ((memq number gnus-newsgroup-unseen)
5399                     gnus-unseen-mark)
5400                    (t gnus-no-mark))
5401              gnus-tmp-downloaded
5402              (cond ((memq number gnus-newsgroup-undownloaded)
5403                     gnus-undownloaded-mark)
5404                    (gnus-newsgroup-agentized
5405                     gnus-downloaded-mark)
5406                    (t
5407                     gnus-no-mark))
5408              gnus-tmp-from (mail-header-from gnus-tmp-header)
5409              gnus-tmp-name
5410              (cond
5411               ((string-match "<[^>]+> *$" gnus-tmp-from)
5412                (setq beg-match (match-beginning 0))
5413                (or (and (string-match "^\".+\"" gnus-tmp-from)
5414                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5415                    (substring gnus-tmp-from 0 beg-match)))
5416               ((string-match "(.+)" gnus-tmp-from)
5417                (substring gnus-tmp-from
5418                           (1+ (match-beginning 0)) (1- (match-end 0))))
5419               (t gnus-tmp-from))
5420
5421              ;; Do the %B string
5422              gnus-tmp-thread-tree-header-string
5423              (cond
5424               ((not gnus-show-threads) "")
5425               ((zerop gnus-tmp-level)
5426                (cond ((cdar thread)
5427                       (or gnus-sum-thread-tree-root subject))
5428                      (gnus-tmp-new-adopts
5429                       (or gnus-sum-thread-tree-false-root subject))
5430                      (t
5431                       (or gnus-sum-thread-tree-single-indent subject))))
5432               (t
5433                (concat (apply 'concat
5434                               (mapcar (lambda (item)
5435                                         (if (= item 1)
5436                                             gnus-sum-thread-tree-vertical
5437                                           gnus-sum-thread-tree-indent))
5438                                       (cdr (reverse tree-stack))))
5439                        (if (nth 1 thread)
5440                            gnus-sum-thread-tree-leaf-with-other
5441                          gnus-sum-thread-tree-single-leaf)))))
5442             (when (string= gnus-tmp-name "")
5443               (setq gnus-tmp-name gnus-tmp-from))
5444             (unless (numberp gnus-tmp-lines)
5445               (setq gnus-tmp-lines -1))
5446             (if (= gnus-tmp-lines -1)
5447                 (setq gnus-tmp-lines "?")
5448               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5449             (gnus-put-text-property
5450              (point)
5451              (progn (eval gnus-summary-line-format-spec) (point))
5452              'gnus-number number)
5453             (when gnus-visual-p
5454               (forward-line -1)
5455               (gnus-summary-highlight-line)
5456               (when gnus-summary-update-hook
5457                 (gnus-run-hooks 'gnus-summary-update-hook))
5458               (forward-line 1))
5459
5460             (setq gnus-tmp-prev-subject simp-subject)))
5461
5462         (when (nth 1 thread)
5463           (push (list (max 0 gnus-tmp-level)
5464                       (copy-sequence tree-stack)
5465                       (nthcdr 1 thread))
5466                 stack))
5467         (push (if (nth 1 thread) 1 0) tree-stack)
5468         (incf gnus-tmp-level)
5469         (setq threads (if thread-end nil (cdar thread)))
5470         (if gnus-summary-display-while-building
5471             (if building-count
5472                 (progn
5473                   ;; use a set frequency
5474                   (setq building-line-count (1- building-line-count))
5475                   (when (= building-line-count 0)
5476                     (sit-for 0)
5477                     (setq building-line-count
5478                           gnus-summary-display-while-building)))
5479               ;; always
5480               (sit-for 0)))
5481         (unless threads
5482           (setq gnus-tmp-level 0)))))
5483   (gnus-message 7 "Generating summary...done"))
5484
5485 (defun gnus-summary-prepare-unthreaded (headers)
5486   "Generate an unthreaded summary buffer based on HEADERS."
5487   (let (header number mark)
5488
5489     (beginning-of-line)
5490
5491     (while headers
5492       ;; We may have to root out some bad articles...
5493       (when (memq (setq number (mail-header-number
5494                                 (setq header (pop headers))))
5495                   gnus-newsgroup-limit)
5496         ;; Mark article as read when it has a low score.
5497         (when (and gnus-summary-mark-below
5498                    (< (or (cdr (assq number gnus-newsgroup-scored))
5499                           gnus-summary-default-score 0)
5500                       gnus-summary-mark-below)
5501                    (not (gnus-summary-article-ancient-p number)))
5502           (setq gnus-newsgroup-unreads
5503                 (delq number gnus-newsgroup-unreads))
5504           (if gnus-newsgroup-auto-expire
5505               (push number gnus-newsgroup-expirable)
5506             (push (cons number gnus-low-score-mark)
5507                   gnus-newsgroup-reads)))
5508
5509         (setq mark (gnus-article-mark number))
5510         (push (gnus-data-make number mark (1+ (point)) header 0)
5511               gnus-newsgroup-data)
5512         (gnus-summary-insert-line
5513          header 0 number
5514          (memq number gnus-newsgroup-undownloaded)
5515          mark (memq number gnus-newsgroup-replied)
5516          (memq number gnus-newsgroup-expirable)
5517          (mail-header-subject header) nil
5518          (cdr (assq number gnus-newsgroup-scored))
5519          (memq number gnus-newsgroup-processable))))))
5520
5521 (defun gnus-group-get-list-identifiers (group)
5522   "Get list identifier regexp for GROUP."
5523   (or (gnus-parameter-list-identifier group)
5524       (if (consp gnus-list-identifiers)
5525           (mapconcat 'identity gnus-list-identifiers " *\\|")
5526         gnus-list-identifiers)))
5527
5528 (defun gnus-summary-remove-list-identifiers ()
5529   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5530   (let ((regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
5531         changed subject)
5532     (when regexp
5533       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5534       (dolist (header gnus-newsgroup-headers)
5535         (setq subject (mail-header-subject header)
5536               changed nil)
5537         (while (string-match regexp subject)
5538           (setq subject
5539                 (concat (substring subject 0 (match-beginning 1))
5540                         (substring subject (match-end 0)))
5541                 changed t))
5542         (when changed
5543           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5544             (setq subject
5545                   (concat (substring subject 0 (match-beginning 1))
5546                           (substring subject (match-end 1)))))
5547           (mail-header-set-subject header subject))))))
5548
5549 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5550   "Fetch headers of ARTICLES."
5551   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5552     (gnus-message 7 "Fetching headers for %s..." name)
5553     (prog1
5554         (if (eq 'nov
5555                 (setq gnus-headers-retrieved-by
5556                       (gnus-retrieve-headers
5557                        articles gnus-newsgroup-name
5558                        (or limit
5559                            ;; We might want to fetch old headers, but
5560                            ;; not if there is only 1 article.
5561                            (and (or (and
5562                                      (not (eq gnus-fetch-old-headers 'some))
5563                                      (not (numberp gnus-fetch-old-headers)))
5564                                     (> (length articles) 1))
5565                                 gnus-fetch-old-headers)))))
5566             (gnus-get-newsgroup-headers-xover
5567              articles force-new dependencies gnus-newsgroup-name t)
5568           (gnus-get-newsgroup-headers dependencies force-new))
5569       (gnus-message 7 "Fetching headers for %s...done" name))))
5570
5571 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5572   "Select newsgroup GROUP.
5573 If READ-ALL is non-nil, all articles in the group are selected.
5574 If SELECT-ARTICLES, only select those articles from GROUP."
5575   (let* ((entry (gnus-group-entry group))
5576          ;;!!! Dirty hack; should be removed.
5577          (gnus-summary-ignore-duplicates
5578           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5579               t
5580             gnus-summary-ignore-duplicates))
5581          (info (nth 2 entry))
5582          charset articles fetched-articles cached)
5583
5584     (unless (gnus-check-server
5585              (set (make-local-variable 'gnus-current-select-method)
5586                   (gnus-find-method-for-group group)))
5587       (error "Couldn't open server"))
5588     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5589
5590     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5591         (gnus-activate-group group)     ; Or we can activate it...
5592         (progn                          ; Or we bug out.
5593           (when (equal major-mode 'gnus-summary-mode)
5594             (gnus-kill-buffer (current-buffer)))
5595           (error
5596            "Couldn't activate group %s: %s"
5597            (mm-decode-coding-string group charset)
5598            (mm-decode-coding-string (gnus-status-message group) charset))))
5599
5600     (unless (gnus-request-group group t)
5601       (when (equal major-mode 'gnus-summary-mode)
5602         (gnus-kill-buffer (current-buffer)))
5603       (error "Couldn't request group %s: %s"
5604              (mm-decode-coding-string group charset)
5605              (mm-decode-coding-string (gnus-status-message group) charset)))
5606
5607     (when (and gnus-agent
5608                (gnus-active group))
5609       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5610
5611       (setq gnus-summary-use-undownloaded-faces
5612             (gnus-agent-find-parameter
5613              group
5614              'agent-enable-undownloaded-faces)))
5615
5616     (setq gnus-newsgroup-name group
5617           gnus-newsgroup-unselected nil
5618           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5619
5620     (let ((display (gnus-group-find-parameter group 'display)))
5621       (setq gnus-newsgroup-display
5622             (cond
5623              ((not (zerop (or (car-safe read-all) 0)))
5624               ;; The user entered the group with C-u SPC/RET, let's show
5625               ;; all articles.
5626               'gnus-not-ignore)
5627              ((eq display 'all)
5628               'gnus-not-ignore)
5629              ((arrayp display)
5630               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5631              ((numberp display)
5632               ;; The following is probably the "correct" solution, but
5633               ;; it makes Gnus fetch all headers and then limit the
5634               ;; articles (which is slow), so instead we hack the
5635               ;; select-articles parameter instead. -- Simon Josefsson
5636               ;; <jas@kth.se>
5637               ;;
5638               ;; (gnus-byte-compile
5639               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5640               ;;                         display)))))
5641               (setq select-articles
5642                     (gnus-uncompress-range
5643                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5644                              (if (> tmp 0)
5645                                  tmp
5646                                1))
5647                            (cdr (gnus-active group)))))
5648               nil)
5649              (t
5650               nil))))
5651
5652     (gnus-summary-setup-default-charset)
5653
5654     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5655     (when (gnus-virtual-group-p group)
5656       (setq cached gnus-newsgroup-cached))
5657
5658     (setq gnus-newsgroup-unreads
5659           (gnus-sorted-ndifference
5660            (gnus-sorted-ndifference gnus-newsgroup-unreads
5661                                     gnus-newsgroup-marked)
5662            gnus-newsgroup-dormant))
5663
5664     (setq gnus-newsgroup-processable nil)
5665
5666     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5667
5668     ;; Adjust and set lists of article marks.
5669     (when info
5670       (gnus-adjust-marked-articles info))
5671     (if (setq articles select-articles)
5672         (setq gnus-newsgroup-unselected
5673               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5674       (setq articles (gnus-articles-to-read group read-all)))
5675
5676     (cond
5677      ((null articles)
5678       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5679       'quit)
5680      ((eq articles 0) nil)
5681      (t
5682       ;; Init the dependencies hash table.
5683       (setq gnus-newsgroup-dependencies
5684             (gnus-make-hashtable (length articles)))
5685       (gnus-set-global-variables)
5686       ;; Retrieve the headers and read them in.
5687
5688       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5689
5690       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5691       (when cached
5692         (setq gnus-newsgroup-cached cached))
5693
5694       ;; Suppress duplicates?
5695       (when gnus-suppress-duplicates
5696         (gnus-dup-suppress-articles))
5697
5698       ;; Set the initial limit.
5699       (setq gnus-newsgroup-limit (copy-sequence articles))
5700       ;; Remove canceled articles from the list of unread articles.
5701       (setq fetched-articles
5702             (mapcar (lambda (headers) (mail-header-number headers))
5703                     gnus-newsgroup-headers))
5704       (setq gnus-newsgroup-articles fetched-articles)
5705       (setq gnus-newsgroup-unreads
5706             (gnus-sorted-nintersection
5707              gnus-newsgroup-unreads fetched-articles))
5708       (gnus-compute-unseen-list)
5709
5710       ;; Removed marked articles that do not exist.
5711       (gnus-update-missing-marks
5712        (gnus-sorted-difference articles fetched-articles))
5713       ;; We might want to build some more threads first.
5714       (when (and gnus-fetch-old-headers
5715                  (eq gnus-headers-retrieved-by 'nov))
5716         (if (eq gnus-fetch-old-headers 'invisible)
5717             (gnus-build-all-threads)
5718           (gnus-build-old-threads)))
5719       ;; Let the Gnus agent mark articles as read.
5720       (when gnus-agent
5721         (gnus-agent-get-undownloaded-list))
5722       ;; Remove list identifiers from subject
5723       (gnus-summary-remove-list-identifiers)
5724       ;; Check whether auto-expire is to be done in this group.
5725       (setq gnus-newsgroup-auto-expire
5726             (and (gnus-group-auto-expirable-p group)
5727                  (not (gnus-group-read-only-p group))))
5728       ;; Set up the article buffer now, if necessary.
5729       (unless (and gnus-single-article-buffer
5730                    (equal gnus-article-buffer "*Article*"))
5731         (gnus-article-setup-buffer))
5732       ;; First and last article in this newsgroup.
5733       (when gnus-newsgroup-headers
5734         (setq gnus-newsgroup-begin
5735               (mail-header-number (car gnus-newsgroup-headers))
5736               gnus-newsgroup-end
5737               (mail-header-number
5738                (gnus-last-element gnus-newsgroup-headers))))
5739       ;; GROUP is successfully selected.
5740       (or gnus-newsgroup-headers t)))))
5741
5742 (defun gnus-compute-unseen-list ()
5743   ;; The `seen' marks are treated specially.
5744   (if (not gnus-newsgroup-seen)
5745       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5746     (setq gnus-newsgroup-unseen
5747           (gnus-inverse-list-range-intersection
5748            gnus-newsgroup-articles gnus-newsgroup-seen))))
5749
5750 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5751
5752 (defun gnus-summary-display-make-predicate (display)
5753   (require 'gnus-agent)
5754   (when (= (length display) 1)
5755     (setq display (car display)))
5756   (unless gnus-summary-display-cache
5757     (dolist (elem (append '((unread . unread)
5758                             (read . read)
5759                             (unseen . unseen))
5760                           gnus-article-mark-lists))
5761       (push (cons (cdr elem)
5762                   (gnus-byte-compile    ;Why bother?
5763                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5764             gnus-summary-display-cache)))
5765   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5766         (gnus-category-predicate-cache gnus-summary-display-cache))
5767     (gnus-get-predicate display)))
5768
5769 ;; Uses the dynamically bound `gnus-number' variable.
5770 (defvar gnus-number)
5771 (defun gnus-article-marked-p (type &optional article)
5772   (let ((article (or article gnus-number)))
5773     (cond
5774      ((eq type 'tick)
5775       (memq article gnus-newsgroup-marked))
5776      ((eq type 'spam)
5777       (memq article gnus-newsgroup-spam-marked))
5778      ((eq type 'unsend)
5779       (memq article gnus-newsgroup-unsendable))
5780      ((eq type 'undownload)
5781       (memq article gnus-newsgroup-undownloaded))
5782      ((eq type 'download)
5783       (memq article gnus-newsgroup-downloadable))
5784      ((eq type 'unread)
5785       (memq article gnus-newsgroup-unreads))
5786      ((eq type 'read)
5787       (memq article gnus-newsgroup-reads))
5788      ((eq type 'dormant)
5789       (memq article gnus-newsgroup-dormant) )
5790      ((eq type 'expire)
5791       (memq article gnus-newsgroup-expirable))
5792      ((eq type 'reply)
5793       (memq article gnus-newsgroup-replied))
5794      ((eq type 'killed)
5795       (memq article gnus-newsgroup-killed))
5796      ((eq type 'bookmark)
5797       (assq article gnus-newsgroup-bookmarks))
5798      ((eq type 'score)
5799       (assq article gnus-newsgroup-scored))
5800      ((eq type 'save)
5801       (memq article gnus-newsgroup-saved))
5802      ((eq type 'cache)
5803       (memq article gnus-newsgroup-cached))
5804      ((eq type 'forward)
5805       (memq article gnus-newsgroup-forwarded))
5806      ((eq type 'seen)
5807       (not (memq article gnus-newsgroup-unseen)))
5808      (t t))))
5809
5810 (defun gnus-articles-to-read (group &optional read-all)
5811   "Find out what articles the user wants to read."
5812   (let* ((only-read-p t)
5813          (articles
5814           ;; Select all articles if `read-all' is non-nil, or if there
5815           ;; are no unread articles.
5816           (if (or read-all
5817                   (and (zerop (length gnus-newsgroup-marked))
5818                        (zerop (length gnus-newsgroup-unreads)))
5819                   ;; Fetch all if the predicate is non-nil.
5820                   gnus-newsgroup-display)
5821               ;; We want to select the headers for all the articles in
5822               ;; the group, so we select either all the active
5823               ;; articles in the group, or (if that's nil), the
5824               ;; articles in the cache.
5825               (or
5826                (if gnus-newsgroup-maximum-articles
5827                    (let ((active (gnus-active group)))
5828                      (gnus-uncompress-range
5829                       (cons (max (car active)
5830                                  (- (cdr active)
5831                                     gnus-newsgroup-maximum-articles
5832                                     -1))
5833                             (cdr active))))
5834                  (gnus-uncompress-range (gnus-active group)))
5835                (gnus-cache-articles-in-group group))
5836             ;; Select only the "normal" subset of articles.
5837             (setq only-read-p nil)
5838             (gnus-sorted-nunion
5839              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5840              gnus-newsgroup-unreads)))
5841          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5842          (scored (length scored-list))
5843          (number (length articles))
5844          (marked (+ (length gnus-newsgroup-marked)
5845                     (length gnus-newsgroup-dormant)))
5846          (select
5847           (cond
5848            ((numberp read-all)
5849             read-all)
5850            ((numberp gnus-newsgroup-display)
5851             gnus-newsgroup-display)
5852            (t
5853             (condition-case ()
5854                 (cond
5855                  ((and (or (<= scored marked) (= scored number))
5856                        (numberp gnus-large-newsgroup)
5857                        (> number gnus-large-newsgroup))
5858                   (let* ((cursor-in-echo-area nil)
5859                          (initial (gnus-parameter-large-newsgroup-initial
5860                                    gnus-newsgroup-name))
5861                          (default (if only-read-p
5862                                       (or initial gnus-large-newsgroup)
5863                                     number))
5864                          (input
5865                           (read-string
5866                            (if only-read-p
5867                                (format
5868                                 "How many articles from %s (available %d, default %d): "
5869                                 (gnus-group-decoded-name
5870                                  (gnus-group-real-name gnus-newsgroup-name))
5871                                 number default)
5872                              (format
5873                               "How many articles from %s (%d default): "
5874                               (gnus-group-decoded-name
5875                                (gnus-group-real-name gnus-newsgroup-name))
5876                               default))
5877                            nil
5878                            nil
5879                            (number-to-string default))))
5880                     (if (string-match "^[ \t]*$" input) number input)))
5881                  ((and (> scored marked) (< scored number)
5882                        (> (- scored number) 20))
5883                   (let ((input
5884                          (read-string
5885                           (format "%s %s (%d scored, %d total): "
5886                                   "How many articles from"
5887                                   (gnus-group-decoded-name
5888                                    (gnus-group-real-name gnus-newsgroup-name))
5889                                   scored number))))
5890                     (if (string-match "^[ \t]*$" input)
5891                         number input)))
5892                  (t number))
5893               (quit
5894                (message "Quit getting the articles to read")
5895                nil))))))
5896     (setq select (if (stringp select) (string-to-number select) select))
5897     (if (or (null select) (zerop select))
5898         select
5899       (if (and (not (zerop scored)) (<= (abs select) scored))
5900           (progn
5901             (setq articles (sort scored-list '<))
5902             (setq number (length articles)))
5903         (setq articles (copy-sequence articles)))
5904
5905       (when (< (abs select) number)
5906         (if (< select 0)
5907             ;; Select the N oldest articles.
5908             (setcdr (nthcdr (1- (abs select)) articles) nil)
5909           ;; Select the N most recent articles.
5910           (setq articles (nthcdr (- number select) articles))))
5911       (setq gnus-newsgroup-unselected
5912             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5913       (when gnus-alter-articles-to-read-function
5914         (setq articles
5915               (sort
5916                (funcall gnus-alter-articles-to-read-function
5917                         gnus-newsgroup-name articles)
5918                '<)))
5919       articles)))
5920
5921 (defun gnus-killed-articles (killed articles)
5922   (let (out)
5923     (while articles
5924       (when (inline (gnus-member-of-range (car articles) killed))
5925         (push (car articles) out))
5926       (setq articles (cdr articles)))
5927     out))
5928
5929 (defun gnus-uncompress-marks (marks)
5930   "Uncompress the mark ranges in MARKS."
5931   (let ((uncompressed '(score bookmark))
5932         out)
5933     (while marks
5934       (if (memq (caar marks) uncompressed)
5935           (push (car marks) out)
5936         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5937       (setq marks (cdr marks)))
5938     out))
5939
5940 (defun gnus-article-mark-to-type (mark)
5941   "Return the type of MARK."
5942   (or (cadr (assq mark gnus-article-special-mark-lists))
5943       'list))
5944
5945 (defun gnus-article-unpropagatable-p (mark)
5946   "Return whether MARK should be propagated to back end."
5947   (memq mark gnus-article-unpropagated-mark-lists))
5948
5949 (defun gnus-adjust-marked-articles (info)
5950   "Set all article lists and remove all marks that are no longer valid."
5951   (let* ((marked-lists (gnus-info-marks info))
5952          (active (gnus-active (gnus-info-group info)))
5953          (min (car active))
5954          (max (cdr active))
5955          (types gnus-article-mark-lists)
5956          marks var articles article mark mark-type
5957          bgn end)
5958     ;; Hack to avoid adjusting marks for imap.
5959     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5960               'nnimap)
5961       (setq min 1))
5962
5963     (dolist (marks marked-lists)
5964       (setq mark (car marks)
5965             mark-type (gnus-article-mark-to-type mark)
5966             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5967
5968       ;; We set the variable according to the type of the marks list,
5969       ;; and then adjust the marks to a subset of the active articles.
5970       (cond
5971        ;; Adjust "simple" lists - compressed yet unsorted
5972        ((eq mark-type 'list)
5973         ;; Simultaneously uncompress and clip to active range
5974         ;; See gnus-uncompress-range for a description of possible marks
5975         (let (l lh)
5976           (if (not (cadr marks))
5977               (set var nil)
5978             (setq articles (if (numberp (cddr marks))
5979                                (list (cdr marks))
5980                              (cdr marks))
5981                   lh (cons nil nil)
5982                   l lh)
5983
5984             (while (setq article (pop articles))
5985               (cond ((consp article)
5986                      (setq bgn (max (car article) min)
5987                            end (min (cdr article) max))
5988                      (while (<= bgn end)
5989                        (setq l (setcdr l (cons bgn nil))
5990                              bgn (1+ bgn))))
5991                     ((and (<= min article)
5992                           (>= max article))
5993                      (setq l (setcdr l (cons article nil))))))
5994             (set var (cdr lh)))))
5995        ;; Adjust assocs.
5996        ((eq mark-type 'tuple)
5997         (set var (setq articles (cdr marks)))
5998         (when (not (listp (cdr (symbol-value var))))
5999           (set var (list (symbol-value var))))
6000         (when (not (listp (cdr articles)))
6001           (setq articles (list articles)))
6002         (while articles
6003           (when (or (not (consp (setq article (pop articles))))
6004                     (< (car article) min)
6005                     (> (car article) max))
6006             (set var (delq article (symbol-value var))))))
6007        ;; Adjust ranges (sloppily).
6008        ((eq mark-type 'range)
6009         (cond
6010          ((eq mark 'seen)
6011           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
6012           ;; It should be (seen (NUM1 . NUM2)).
6013           (when (numberp (cddr marks))
6014             (setcdr marks (list (cdr marks))))
6015           (setq articles (cdr marks))
6016           (while (and articles
6017                       (or (and (consp (car articles))
6018                                (> min (cdar articles)))
6019                           (and (numberp (car articles))
6020                                (> min (car articles)))))
6021             (pop articles))
6022           (set var articles))))))))
6023
6024 (defun gnus-update-missing-marks (missing)
6025   "Go through the list of MISSING articles and remove them from the mark lists."
6026   (when missing
6027     (let (var m)
6028       ;; Go through all types.
6029       (dolist (elem gnus-article-mark-lists)
6030         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
6031           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
6032           (when (symbol-value var)
6033             ;; This list has articles.  So we delete all missing
6034             ;; articles from it.
6035             (setq m missing)
6036             (while m
6037               (set var (delq (pop m) (symbol-value var))))))))))
6038
6039 (defun gnus-update-marks ()
6040   "Enter the various lists of marked articles into the newsgroup info list."
6041   (let ((types gnus-article-mark-lists)
6042         (info (gnus-get-info gnus-newsgroup-name))
6043         type list newmarked symbol delta-marks)
6044     (when info
6045       ;; Add all marks lists to the list of marks lists.
6046       (while (setq type (pop types))
6047         (setq list (symbol-value
6048                     (setq symbol
6049                           (intern (format "gnus-newsgroup-%s" (car type))))))
6050
6051         (when list
6052           ;; Get rid of the entries of the articles that have the
6053           ;; default score.
6054           (when (and (eq (cdr type) 'score)
6055                      gnus-save-score
6056                      list)
6057             (let* ((arts list)
6058                    (prev (cons nil list))
6059                    (all prev))
6060               (while arts
6061                 (if (or (not (consp (car arts)))
6062                         (= (cdar arts) gnus-summary-default-score))
6063                     (setcdr prev (cdr arts))
6064                   (setq prev arts))
6065                 (setq arts (cdr arts)))
6066               (setq list (cdr all)))))
6067
6068         (when (eq (cdr type) 'seen)
6069           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6070
6071         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6072           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6073
6074         (when (and (gnus-check-backend-function
6075                     'request-set-mark gnus-newsgroup-name)
6076                    (not (gnus-article-unpropagatable-p (cdr type))))
6077           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6078                  ;; Don't do anything about marks for articles we
6079                  ;; didn't actually get any headers for.
6080                  (del
6081                   (gnus-list-range-intersection
6082                    gnus-newsgroup-articles
6083                    (gnus-remove-from-range (gnus-copy-sequence old) list)))
6084                  (add
6085                   (gnus-list-range-intersection
6086                    gnus-newsgroup-articles
6087                    (gnus-remove-from-range
6088                     (gnus-copy-sequence list) old))))
6089             (when add
6090               (push (list add 'add (list (cdr type))) delta-marks))
6091             (when del
6092               ;; Don't delete marks from outside the active range.
6093               ;; This shouldn't happen, but is a sanity check.
6094               (setq del (gnus-sorted-range-intersection
6095                          (gnus-active gnus-newsgroup-name) del))
6096               (push (list del 'del (list (cdr type))) delta-marks))))
6097
6098         (when list
6099           (push (cons (cdr type) list) newmarked)))
6100
6101       (when delta-marks
6102         (unless (gnus-check-group gnus-newsgroup-name)
6103           (error "Can't open server for %s" gnus-newsgroup-name))
6104         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6105
6106       ;; Enter these new marks into the info of the group.
6107       (if (nthcdr 3 info)
6108           (setcar (nthcdr 3 info) newmarked)
6109         ;; Add the marks lists to the end of the info.
6110         (when newmarked
6111           (setcdr (nthcdr 2 info) (list newmarked))))
6112
6113       ;; Cut off the end of the info if there's nothing else there.
6114       (let ((i 5))
6115         (while (and (> i 2)
6116                     (not (nth i info)))
6117           (when (nthcdr (decf i) info)
6118             (setcdr (nthcdr i info) nil)))))))
6119
6120 (defun gnus-set-mode-line (where)
6121   "Set the mode line of the article or summary buffers.
6122 If WHERE is `summary', the summary mode line format will be used."
6123   ;; Is this mode line one we keep updated?
6124   (when (and (memq where gnus-updated-mode-lines)
6125              (symbol-value
6126               (intern (format "gnus-%s-mode-line-format-spec" where))))
6127     (let (mode-string)
6128       ;; We evaluate this in the summary buffer since these
6129       ;; variables are buffer-local to that buffer.
6130       (with-current-buffer gnus-summary-buffer
6131         ;; We bind all these variables that are used in the `eval' form
6132         ;; below.
6133         (let* ((mformat (symbol-value
6134                          (intern
6135                           (format "gnus-%s-mode-line-format-spec" where))))
6136                (gnus-tmp-group-name (gnus-mode-string-quote
6137                                      (gnus-group-decoded-name
6138                                       gnus-newsgroup-name)))
6139                (gnus-tmp-article-number (or gnus-current-article 0))
6140                (gnus-tmp-unread gnus-newsgroup-unreads)
6141                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6142                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6143                (gnus-tmp-unread-and-unselected
6144                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6145                             (zerop gnus-tmp-unselected))
6146                        "")
6147                       ((zerop gnus-tmp-unselected)
6148                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6149                       (t (format "{%d(+%d) more}"
6150                                  gnus-tmp-unread-and-unticked
6151                                  gnus-tmp-unselected))))
6152                (gnus-tmp-subject
6153                 (if (and gnus-current-headers
6154                          (vectorp gnus-current-headers))
6155                     (gnus-mode-string-quote
6156                      (mail-header-subject gnus-current-headers))
6157                   ""))
6158                bufname-length max-len
6159                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6160           (setq mode-string (eval mformat))
6161           (setq bufname-length (if (string-match "%b" mode-string)
6162                                    (- (length
6163                                        (buffer-name
6164                                         (if (eq where 'summary)
6165                                             nil
6166                                           (get-buffer gnus-article-buffer))))
6167                                       2)
6168                                  0))
6169           (setq max-len (max 4 (if gnus-mode-non-string-length
6170                                    (- (window-width)
6171                                       gnus-mode-non-string-length
6172                                       bufname-length)
6173                                  (length mode-string))))
6174           ;; We might have to chop a bit of the string off...
6175           (when (> (length mode-string) max-len)
6176             (setq mode-string
6177                   (concat (truncate-string-to-width mode-string (- max-len 3))
6178                           "...")))))
6179       ;; Update the mode line.
6180       (setq mode-line-buffer-identification
6181             (gnus-mode-line-buffer-identification (list mode-string)))
6182       (set-buffer-modified-p t))))
6183
6184 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6185   "Go through the HEADERS list and add all Xrefs to a hash table.
6186 The resulting hash table is returned, or nil if no Xrefs were found."
6187   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6188          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6189          (xref-hashtb (gnus-make-hashtable))
6190          start group entry number xrefs header)
6191     (while headers
6192       (setq header (pop headers))
6193       (when (and (setq xrefs (mail-header-xref header))
6194                  (not (memq (setq number (mail-header-number header))
6195                             unreads)))
6196         (setq start 0)
6197         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6198           (setq start (match-end 0))
6199           (setq group (if prefix
6200                           (concat prefix (substring xrefs (match-beginning 1)
6201                                                     (match-end 1)))
6202                         (substring xrefs (match-beginning 1) (match-end 1))))
6203           (setq number
6204                 (string-to-number (substring xrefs (match-beginning 2)
6205                                           (match-end 2))))
6206           (if (setq entry (gnus-gethash group xref-hashtb))
6207               (setcdr entry (cons number (cdr entry)))
6208             (gnus-sethash group (cons number nil) xref-hashtb)))))
6209     (and start xref-hashtb)))
6210
6211 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6212   "Look through all the headers and mark the Xrefs as read."
6213   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6214         name info xref-hashtb idlist method nth4)
6215     (with-current-buffer gnus-group-buffer
6216       (when (setq xref-hashtb
6217                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6218         (mapatoms
6219          (lambda (group)
6220            (unless (string= from-newsgroup (setq name (symbol-name group)))
6221              (setq idlist (symbol-value group))
6222              ;; Dead groups are not updated.
6223              (and (prog1
6224                       (setq info (gnus-get-info name))
6225                     (when (stringp (setq nth4 (gnus-info-method info)))
6226                       (setq nth4 (gnus-server-to-method nth4))))
6227                   ;; Only do the xrefs if the group has the same
6228                   ;; select method as the group we have just read.
6229                   (or (gnus-methods-equal-p
6230                        nth4 (gnus-find-method-for-group from-newsgroup))
6231                       virtual
6232                       (equal nth4 (setq method (gnus-find-method-for-group
6233                                                 from-newsgroup)))
6234                       (and (equal (car nth4) (car method))
6235                            (equal (nth 1 nth4) (nth 1 method))))
6236                   gnus-use-cross-reference
6237                   (or (not (eq gnus-use-cross-reference t))
6238                       virtual
6239                       ;; Only do cross-references on subscribed
6240                       ;; groups, if that is what is wanted.
6241                       (<= (gnus-info-level info) gnus-level-subscribed))
6242                   (gnus-group-make-articles-read name idlist))))
6243          xref-hashtb)))))
6244
6245 (defun gnus-compute-read-articles (group articles)
6246   (let* ((entry (gnus-group-entry group))
6247          (info (nth 2 entry))
6248          (active (gnus-active group))
6249          ninfo)
6250     (when entry
6251       ;; First peel off all invalid article numbers.
6252       (when active
6253         (let ((ids articles)
6254               id first)
6255           (while (setq id (pop ids))
6256             (when (and first (> id (cdr active)))
6257               ;; We'll end up in this situation in one particular
6258               ;; obscure situation.  If you re-scan a group and get
6259               ;; a new article that is cross-posted to a different
6260               ;; group that has not been re-scanned, you might get
6261               ;; crossposted article that has a higher number than
6262               ;; Gnus believes possible.  So we re-activate this
6263               ;; group as well.  This might mean doing the
6264               ;; crossposting thingy will *increase* the number
6265               ;; of articles in some groups.  Tsk, tsk.
6266               (setq active (or (gnus-activate-group group) active)))
6267             (when (or (> id (cdr active))
6268                       (< id (car active)))
6269               (setq articles (delq id articles))))))
6270       ;; If the read list is nil, we init it.
6271       (if (and active
6272                (null (gnus-info-read info))
6273                (> (car active) 1))
6274           (setq ninfo (cons 1 (1- (car active))))
6275         (setq ninfo (gnus-info-read info)))
6276       ;; Then we add the read articles to the range.
6277       (gnus-add-to-range
6278        ninfo (setq articles (sort articles '<))))))
6279
6280 (defun gnus-group-make-articles-read (group articles)
6281   "Update the info of GROUP to say that ARTICLES are read."
6282   (let* ((num 0)
6283          (entry (gnus-group-entry group))
6284          (info (nth 2 entry))
6285          (active (gnus-active group))
6286          (set-marks
6287           (or gnus-propagate-marks
6288               (gnus-method-option-p
6289                (gnus-find-method-for-group group)
6290                'server-marks)))
6291          range)
6292     (if (not entry)
6293         ;; Group that Gnus doesn't know exists, but still allow the
6294         ;; backend to set marks.
6295         (when set-marks
6296           (gnus-request-set-mark
6297            group (list (list (gnus-compress-sequence (sort articles #'<))
6298                              'add '(read)))))
6299       ;; Normal, subscribed groups.
6300       (setq range (gnus-compute-read-articles group articles))
6301       (with-current-buffer gnus-group-buffer
6302         (gnus-undo-register
6303           `(progn
6304              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6305              (gnus-info-set-read ',info ',(gnus-info-read info))
6306              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6307              (when ,set-marks
6308                (gnus-request-set-mark
6309                 ,group (list (list ',range 'del '(read)))))
6310              (gnus-group-update-group ,group t))))
6311       ;; Add the read articles to the range.
6312       (gnus-info-set-read info range)
6313       (when set-marks
6314         (gnus-request-set-mark group (list (list range 'add '(read)))))
6315       ;; Then we have to re-compute how many unread
6316       ;; articles there are in this group.
6317       (when active
6318         (cond
6319          ((not range)
6320           (setq num (- (1+ (cdr active)) (car active))))
6321          ((not (listp (cdr range)))
6322           (setq num (- (cdr active) (- (1+ (cdr range))
6323                                        (car range)))))
6324          (t
6325           (while range
6326             (if (numberp (car range))
6327                 (setq num (1+ num))
6328               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6329             (setq range (cdr range)))
6330           (setq num (- (cdr active) num))))
6331         ;; Update the number of unread articles.
6332         (setcar entry num)
6333         ;; Update the group buffer.
6334         (unless (gnus-ephemeral-group-p group)
6335           (gnus-group-update-group group t))))))
6336
6337 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6338   (let ((cur nntp-server-buffer)
6339         (dependencies
6340          (or dependencies
6341              (with-current-buffer gnus-summary-buffer
6342                gnus-newsgroup-dependencies)))
6343         headers id end ref number
6344         (mail-parse-charset gnus-newsgroup-charset)
6345         (mail-parse-ignored-charsets
6346          (save-current-buffer (condition-case nil
6347                                   (set-buffer gnus-summary-buffer)
6348                                 (error))
6349                               gnus-newsgroup-ignored-charsets)))
6350     (with-current-buffer nntp-server-buffer
6351       ;; Translate all TAB characters into SPACE characters.
6352       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6353       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6354       (ietf-drums-unfold-fws)
6355       (gnus-run-hooks 'gnus-parse-headers-hook)
6356       (let ((case-fold-search t)
6357             in-reply-to header p lines chars)
6358         (goto-char (point-min))
6359         ;; Search to the beginning of the next header.  Error messages
6360         ;; do not begin with 2 or 3.
6361         (while (re-search-forward "^[23][0-9]+ " nil t)
6362           (setq id nil
6363                 ref nil)
6364           ;; This implementation of this function, with nine
6365           ;; search-forwards instead of the one re-search-forward and
6366           ;; a case (which basically was the old function) is actually
6367           ;; about twice as fast, even though it looks messier.  You
6368           ;; can't have everything, I guess.  Speed and elegance
6369           ;; doesn't always go hand in hand.
6370           (setq
6371            header
6372            (vector
6373             ;; Number.
6374             (prog1
6375                 (setq number (read cur))
6376               (end-of-line)
6377               (setq p (point))
6378               (narrow-to-region (point)
6379                                 (or (and (search-forward "\n.\n" nil t)
6380                                          (- (point) 2))
6381                                     (point))))
6382             ;; Subject.
6383             (progn
6384               (goto-char p)
6385               (if (search-forward "\nsubject:" nil t)
6386                   (funcall gnus-decode-encoded-word-function
6387                            (nnheader-header-value))
6388                 "(none)"))
6389             ;; From.
6390             (progn
6391               (goto-char p)
6392               (if (search-forward "\nfrom:" nil t)
6393                   (funcall gnus-decode-encoded-address-function
6394                            (nnheader-header-value))
6395                 "(nobody)"))
6396             ;; Date.
6397             (progn
6398               (goto-char p)
6399               (if (search-forward "\ndate:" nil t)
6400                   (nnheader-header-value) ""))
6401             ;; Message-ID.
6402             (progn
6403               (goto-char p)
6404               (setq id (if (re-search-forward
6405                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6406                            ;; We do it this way to make sure the Message-ID
6407                            ;; is (somewhat) syntactically valid.
6408                            (buffer-substring (match-beginning 1)
6409                                              (match-end 1))
6410                          ;; If there was no message-id, we just fake one
6411                          ;; to make subsequent routines simpler.
6412                          (nnheader-generate-fake-message-id number))))
6413             ;; References.
6414             (progn
6415               (goto-char p)
6416               (if (search-forward "\nreferences:" nil t)
6417                   (progn
6418                     (setq end (point))
6419                     (prog1
6420                         (nnheader-header-value)
6421                       (setq ref
6422                             (buffer-substring
6423                              (progn
6424                                (end-of-line)
6425                                (search-backward ">" end t)
6426                                (1+ (point)))
6427                              (progn
6428                                (search-backward "<" end t)
6429                                (point))))))
6430                 ;; Get the references from the in-reply-to header if there
6431                 ;; were no references and the in-reply-to header looks
6432                 ;; promising.
6433                 (if (and (search-forward "\nin-reply-to:" nil t)
6434                          (setq in-reply-to (nnheader-header-value))
6435                          (string-match "<[^>]+>" in-reply-to))
6436                     (let (ref2)
6437                       (setq ref (substring in-reply-to (match-beginning 0)
6438                                            (match-end 0)))
6439                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6440                         (setq ref2 (substring in-reply-to (match-beginning 0)
6441                                               (match-end 0)))
6442                         (when (> (length ref2) (length ref))
6443                           (setq ref ref2)))
6444                       ref)
6445                   (setq ref nil))))
6446             ;; Chars.
6447             (progn
6448               (goto-char p)
6449               (if (search-forward "\nchars: " nil t)
6450                   (if (numberp (setq chars (ignore-errors (read cur))))
6451                       chars -1)
6452                 -1))
6453             ;; Lines.
6454             (progn
6455               (goto-char p)
6456               (if (search-forward "\nlines: " nil t)
6457                   (if (numberp (setq lines (ignore-errors (read cur))))
6458                       lines -1)
6459                 -1))
6460             ;; Xref.
6461             (progn
6462               (goto-char p)
6463               (and (search-forward "\nxref:" nil t)
6464                    (nnheader-header-value)))
6465             ;; Extra.
6466             (when gnus-extra-headers
6467               (let ((extra gnus-extra-headers)
6468                     out)
6469                 (while extra
6470                   (goto-char p)
6471                   (when (search-forward
6472                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6473                     (push (cons (car extra) (nnheader-header-value))
6474                           out))
6475                   (pop extra))
6476                 out))))
6477           (when (equal id ref)
6478             (setq ref nil))
6479
6480           (when gnus-alter-header-function
6481             (funcall gnus-alter-header-function header)
6482             (setq id (mail-header-id header)
6483                   ref (gnus-parent-id (mail-header-references header))))
6484
6485           (when (setq header
6486                       (gnus-dependencies-add-header
6487                        header dependencies force-new))
6488             (push header headers))
6489           (goto-char (point-max))
6490           (widen))
6491         (nreverse headers)))))
6492
6493 ;; Goes through the xover lines and returns a list of vectors
6494 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6495                                                   force-new dependencies
6496                                                   group also-fetch-heads)
6497   "Parse the news overview data in the server buffer.
6498 Return a list of headers that match SEQUENCE (see
6499 `nntp-retrieve-headers')."
6500   ;; Get the Xref when the users reads the articles since most/some
6501   ;; NNTP servers do not include Xrefs when using XOVER.
6502   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6503   (let ((mail-parse-charset gnus-newsgroup-charset)
6504         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6505         (cur nntp-server-buffer)
6506         (dependencies (or dependencies gnus-newsgroup-dependencies))
6507         (allp (cond
6508                ((eq gnus-read-all-available-headers t)
6509                 t)
6510                ((and (stringp gnus-read-all-available-headers)
6511                      group)
6512                 (string-match gnus-read-all-available-headers group))
6513                (t
6514                 nil)))
6515         number headers header)
6516     (with-current-buffer nntp-server-buffer
6517       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6518       ;; Allow the user to mangle the headers before parsing them.
6519       (gnus-run-hooks 'gnus-parse-headers-hook)
6520       (goto-char (point-min))
6521       (gnus-parse-without-error
6522         (while (and (or sequence allp)
6523                     (not (eobp)))
6524           (setq number (read cur))
6525           (when (not allp)
6526             (while (and sequence
6527                         (< (car sequence) number))
6528               (setq sequence (cdr sequence))))
6529           (when (and (or allp
6530                          (and sequence
6531                               (eq number (car sequence))))
6532                      (progn
6533                        (setq sequence (cdr sequence))
6534                        (setq header (inline
6535                                       (gnus-nov-parse-line
6536                                        number dependencies force-new)))))
6537             (push header headers))
6538           (forward-line 1)))
6539       ;; A common bug in inn is that if you have posted an article and
6540       ;; then retrieves the active file, it will answer correctly --
6541       ;; the new article is included.  However, a NOV entry for the
6542       ;; article may not have been generated yet, so this may fail.
6543       ;; We work around this problem by retrieving the last few
6544       ;; headers using HEAD.
6545       (if (or (not also-fetch-heads)
6546               (not sequence))
6547           ;; We (probably) got all the headers.
6548           (nreverse headers)
6549         (let ((gnus-nov-is-evil t))
6550           (nconc
6551            (nreverse headers)
6552            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6553              (gnus-get-newsgroup-headers))))))))
6554
6555 (defun gnus-article-get-xrefs ()
6556   "Fill in the Xref value in `gnus-current-headers', if necessary.
6557 This is meant to be called in `gnus-article-internal-prepare-hook'."
6558   (let ((headers (with-current-buffer gnus-summary-buffer
6559                    gnus-current-headers)))
6560     (or (not gnus-use-cross-reference)
6561         (not headers)
6562         (and (mail-header-xref headers)
6563              (not (string= (mail-header-xref headers) "")))
6564         (let ((case-fold-search t)
6565               xref)
6566           (save-restriction
6567             (nnheader-narrow-to-headers)
6568             (goto-char (point-min))
6569             (when (or (and (not (eobp))
6570                            (eq (downcase (char-after)) ?x)
6571                            (looking-at "Xref:"))
6572                       (search-forward "\nXref:" nil t))
6573               (goto-char (1+ (match-end 0)))
6574               (setq xref (buffer-substring (point) (point-at-eol)))
6575               (mail-header-set-xref headers xref)))))))
6576
6577 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6578   "Find article ID and insert the summary line for that article.
6579 OLD-HEADER can either be a header or a line number to insert
6580 the subject line on.
6581 If USE-OLD-HEADER is non-nil, then OLD-HEADER should be a header,
6582 and OLD-HEADER will be used when the summary line is inserted,
6583 too, instead of trying to fetch new headers."
6584   (let* ((line (and (numberp old-header) old-header))
6585          (old-header (and (vectorp old-header) old-header))
6586          (header (cond ((and old-header use-old-header)
6587                         old-header)
6588                        ((and (numberp id)
6589                              (gnus-number-to-header id))
6590                         (gnus-number-to-header id))
6591                        (t
6592                         (gnus-read-header id))))
6593          (number (and (numberp id) id))
6594          d)
6595     (when header
6596       ;; Rebuild the thread that this article is part of and go to the
6597       ;; article we have fetched.
6598       (when (and (not gnus-show-threads)
6599                  old-header)
6600         (when (and number
6601                    (setq d (gnus-data-find (mail-header-number old-header))))
6602           (goto-char (gnus-data-pos d))
6603           (gnus-data-remove
6604            number
6605            (- (point-at-bol)
6606               (prog1
6607                   (1+ (point-at-eol))
6608                 (gnus-delete-line))))))
6609       ;; Remove list identifiers from subject.
6610       (let ((gnus-newsgroup-headers (list header)))
6611         (gnus-summary-remove-list-identifiers))
6612       (when old-header
6613         (mail-header-set-number header (mail-header-number old-header)))
6614       (setq gnus-newsgroup-sparse
6615             (delq (setq number (mail-header-number header))
6616                   gnus-newsgroup-sparse))
6617       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6618       (push number gnus-newsgroup-limit)
6619       (gnus-rebuild-thread (mail-header-id header) line)
6620       (gnus-summary-goto-subject number nil t))
6621     (when (and (numberp number)
6622                (> number 0))
6623       ;; We have to update the boundaries even if we can't fetch the
6624       ;; article if ID is a number -- so that the next `P' or `N'
6625       ;; command will fetch the previous (or next) article even
6626       ;; if the one we tried to fetch this time has been canceled.
6627       (when (> number gnus-newsgroup-end)
6628         (setq gnus-newsgroup-end number))
6629       (when (< number gnus-newsgroup-begin)
6630         (setq gnus-newsgroup-begin number))
6631       (setq gnus-newsgroup-unselected
6632             (delq number gnus-newsgroup-unselected)))
6633     ;; Report back a success?
6634     (and header (mail-header-number header))))
6635
6636 ;;; Process/prefix in the summary buffer
6637
6638 (defun gnus-summary-work-articles (n)
6639   "Return a list of articles to be worked upon.
6640 The prefix argument, the list of process marked articles, and the
6641 current article will be taken into consideration."
6642   (with-current-buffer gnus-summary-buffer
6643     (cond
6644      (n
6645       ;; A numerical prefix has been given.
6646       (setq n (prefix-numeric-value n))
6647       (let ((backward (< n 0))
6648             (n (abs (prefix-numeric-value n)))
6649             articles article)
6650         (save-excursion
6651           (while
6652               (and (> n 0)
6653                    (push (setq article (gnus-summary-article-number))
6654                          articles)
6655                    (if backward
6656                        (gnus-summary-find-prev nil article)
6657                      (gnus-summary-find-next nil article)))
6658             (decf n)))
6659         (nreverse articles)))
6660      ((and (gnus-region-active-p) (mark))
6661       (message "region active")
6662       ;; Work on the region between point and mark.
6663       (let ((max (max (point) (mark)))
6664             articles article)
6665         (save-excursion
6666           (goto-char (min (point) (mark)))
6667           (while
6668               (and
6669                (push (setq article (gnus-summary-article-number)) articles)
6670                (gnus-summary-find-next nil article)
6671                (< (point) max)))
6672           (nreverse articles))))
6673      (gnus-newsgroup-processable
6674       ;; There are process-marked articles present.
6675       ;; Save current state.
6676       (gnus-summary-save-process-mark)
6677       ;; Return the list.
6678       (reverse gnus-newsgroup-processable))
6679      (t
6680       ;; Just return the current article.
6681       (list (gnus-summary-article-number))))))
6682
6683 (defmacro gnus-summary-iterate (arg &rest forms)
6684   "Iterate over the process/prefixed articles and do FORMS.
6685 ARG is the interactive prefix given to the command.  FORMS will be
6686 executed with point over the summary line of the articles."
6687   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6688     `(let ((,articles (gnus-summary-work-articles ,arg)))
6689        (while ,articles
6690          (gnus-summary-goto-subject (car ,articles))
6691          ,@forms
6692          (pop ,articles)))))
6693
6694 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6695 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6696
6697 (defun gnus-summary-save-process-mark ()
6698   "Push the current set of process marked articles on the stack."
6699   (interactive)
6700   (push (copy-sequence gnus-newsgroup-processable)
6701         gnus-newsgroup-process-stack))
6702
6703 (defun gnus-summary-kill-process-mark ()
6704   "Push the current set of process marked articles on the stack and unmark."
6705   (interactive)
6706   (gnus-summary-save-process-mark)
6707   (gnus-summary-unmark-all-processable))
6708
6709 (defun gnus-summary-yank-process-mark ()
6710   "Pop the last process mark state off the stack and restore it."
6711   (interactive)
6712   (unless gnus-newsgroup-process-stack
6713     (error "Empty mark stack"))
6714   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6715
6716 (defun gnus-summary-process-mark-set (set)
6717   "Make SET into the current process marked articles."
6718   (gnus-summary-unmark-all-processable)
6719   (mapc 'gnus-summary-set-process-mark set))
6720
6721 ;;; Searching and stuff
6722
6723 (defun gnus-summary-search-group (&optional backward use-level)
6724   "Search for next unread newsgroup.
6725 If optional argument BACKWARD is non-nil, search backward instead."
6726   (with-current-buffer gnus-group-buffer
6727     (when (gnus-group-search-forward
6728            backward nil (if use-level (gnus-group-group-level) nil))
6729       (gnus-group-group-name))))
6730
6731 (defun gnus-summary-best-group (&optional exclude-group)
6732   "Find the name of the best unread group.
6733 If EXCLUDE-GROUP, do not go to this group."
6734   (with-current-buffer gnus-group-buffer
6735     (save-excursion
6736       (gnus-group-best-unread-group exclude-group))))
6737
6738 (defun gnus-summary-find-next (&optional unread article backward)
6739   (if backward
6740       (gnus-summary-find-prev unread article)
6741     (let* ((dummy (gnus-summary-article-intangible-p))
6742            (article (or article (gnus-summary-article-number)))
6743            (data (gnus-data-find-list article))
6744            result)
6745       (when (and (not dummy)
6746                  (or (not gnus-summary-check-current)
6747                      (not unread)
6748                      (not (gnus-data-unread-p (car data)))))
6749         (setq data (cdr data)))
6750       (when (setq result
6751                   (if unread
6752                       (progn
6753                         (while data
6754                           (unless (memq (gnus-data-number (car data))
6755                                         (cond
6756                                          ((eq gnus-auto-goto-ignores
6757                                               'always-undownloaded)
6758                                           gnus-newsgroup-undownloaded)
6759                                          (gnus-plugged
6760                                           nil)
6761                                          ((eq gnus-auto-goto-ignores
6762                                               'unfetched)
6763                                           gnus-newsgroup-unfetched)
6764                                          ((eq gnus-auto-goto-ignores
6765                                               'undownloaded)
6766                                           gnus-newsgroup-undownloaded)))
6767                             (when (gnus-data-unread-p (car data))
6768                               (setq result (car data)
6769                                     data nil)))
6770                           (setq data (cdr data)))
6771                         result)
6772                     (car data)))
6773         (goto-char (gnus-data-pos result))
6774         (gnus-data-number result)))))
6775
6776 (defun gnus-summary-find-prev (&optional unread article)
6777   (let* ((eobp (eobp))
6778          (article (or article (gnus-summary-article-number)))
6779          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6780          result)
6781     (when (and (not eobp)
6782                (or (not gnus-summary-check-current)
6783                    (not unread)
6784                    (not (gnus-data-unread-p (car data)))))
6785       (setq data (cdr data)))
6786     (when (setq result
6787                 (if unread
6788                     (progn
6789                       (while data
6790                         (unless (memq (gnus-data-number (car data))
6791                                       (cond
6792                                        ((eq gnus-auto-goto-ignores
6793                                             'always-undownloaded)
6794                                         gnus-newsgroup-undownloaded)
6795                                        (gnus-plugged
6796                                         nil)
6797                                        ((eq gnus-auto-goto-ignores
6798                                             'unfetched)
6799                                         gnus-newsgroup-unfetched)
6800                                        ((eq gnus-auto-goto-ignores
6801                                             'undownloaded)
6802                                         gnus-newsgroup-undownloaded)))
6803                           (when (gnus-data-unread-p (car data))
6804                             (setq result (car data)
6805                                   data nil)))
6806                         (setq data (cdr data)))
6807                       result)
6808                   (car data)))
6809       (goto-char (gnus-data-pos result))
6810       (gnus-data-number result))))
6811
6812 (defun gnus-summary-find-subject (subject &optional unread backward article)
6813   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6814          (article (or article (gnus-summary-article-number)))
6815          (articles (gnus-data-list backward))
6816          (arts (gnus-data-find-list article articles))
6817          result)
6818     (when (or (not gnus-summary-check-current)
6819               (not unread)
6820               (not (gnus-data-unread-p (car arts))))
6821       (setq arts (cdr arts)))
6822     (while arts
6823       (and (or (not unread)
6824                (gnus-data-unread-p (car arts)))
6825            (vectorp (gnus-data-header (car arts)))
6826            (gnus-subject-equal
6827             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6828            (setq result (car arts)
6829                  arts nil))
6830       (setq arts (cdr arts)))
6831     (and result
6832          (goto-char (gnus-data-pos result))
6833          (gnus-data-number result))))
6834
6835 (defun gnus-summary-search-forward (&optional unread subject backward)
6836   "Search forward for an article.
6837 If UNREAD, look for unread articles.  If SUBJECT, look for
6838 articles with that subject.  If BACKWARD, search backward instead."
6839   (cond (subject (gnus-summary-find-subject subject unread backward))
6840         (backward (gnus-summary-find-prev unread))
6841         (t (gnus-summary-find-next unread))))
6842
6843 (defun gnus-recenter (&optional n)
6844   "Center point in window and redisplay frame.
6845 Also do horizontal recentering."
6846   (interactive "P")
6847   (when (and gnus-auto-center-summary
6848              (not (eq gnus-auto-center-summary 'vertical)))
6849     (gnus-horizontal-recenter))
6850   (if (fboundp 'recenter-top-bottom)
6851       (recenter-top-bottom n)
6852     (recenter n)))
6853
6854 (put 'gnus-recenter 'isearch-scroll t)
6855
6856 (defun gnus-forward-line-ignore-invisible (n)
6857   "Move N lines forward (backward if N is negative).
6858 Like forward-line, but skip over (and don't count) invisible lines."
6859   (let (done)
6860     (while (and (> n 0) (not done))
6861       ;; If the following character is currently invisible,
6862       ;; skip all characters with that same `invisible' property value.
6863       (while (gnus-invisible-p (point))
6864         (goto-char (gnus-next-char-property-change (point))))
6865       (forward-line 1)
6866       (if (eobp)
6867           (setq done t)
6868         (setq n (1- n))))
6869     (while (and (< n 0) (not done))
6870       (forward-line -1)
6871       (if (bobp) (setq done t)
6872         (setq n (1+ n))
6873         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6874           (goto-char (gnus-previous-char-property-change (point))))))))
6875
6876 (defun gnus-summary-recenter ()
6877   "Center point in the summary window.
6878 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6879 displayed, no centering will be performed."
6880   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6881   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6882   (interactive)
6883   ;; The user has to want it.
6884   (when gnus-auto-center-summary
6885     (let* ((top (cond ((< (window-height) 4) 0)
6886                       ((< (window-height) 7) 1)
6887                       (t (if (numberp gnus-auto-center-summary)
6888                              gnus-auto-center-summary
6889                            (/ (1- (window-height)) 2)))))
6890            (height (1- (window-height)))
6891            (bottom (save-excursion
6892                      (goto-char (point-max))
6893                      (gnus-forward-line-ignore-invisible (- height))
6894                      (point)))
6895            (window (get-buffer-window (current-buffer))))
6896       (when (get-buffer-window gnus-article-buffer)
6897         ;; Only do recentering when the article buffer is displayed,
6898         ;; Set the window start to either `bottom', which is the biggest
6899         ;; possible valid number, or the second line from the top,
6900         ;; whichever is the least.
6901         (let ((top-pos (save-excursion
6902                          (gnus-forward-line-ignore-invisible (- top))
6903                          (point))))
6904           (if (> bottom top-pos)
6905               ;; Keep the second line from the top visible
6906               (set-window-start window top-pos)
6907             ;; Try to keep the bottom line visible; if it's partially
6908             ;; obscured, either scroll one more line to make it fully
6909             ;; visible, or revert to using TOP-POS.
6910             (save-excursion
6911               (goto-char (point-max))
6912               (gnus-forward-line-ignore-invisible -1)
6913               (let ((last-line-start (point)))
6914                 (goto-char bottom)
6915                 (set-window-start window (point) t)
6916                 (when (not (pos-visible-in-window-p last-line-start window))
6917                   (gnus-forward-line-ignore-invisible 1)
6918                   (set-window-start window (min (point) top-pos) t)))))))
6919       ;; Do horizontal recentering while we're at it.
6920       (when (and (get-buffer-window (current-buffer) t)
6921                  (not (eq gnus-auto-center-summary 'vertical)))
6922         (let ((selected (selected-window)))
6923           (select-window (get-buffer-window (current-buffer) t))
6924           (gnus-summary-position-point)
6925           (gnus-horizontal-recenter)
6926           (select-window selected))))))
6927
6928 (defun gnus-summary-jump-to-group (newsgroup)
6929   "Move point to NEWSGROUP in group mode buffer."
6930   ;; Keep update point of group mode buffer if visible.
6931   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6932       (save-window-excursion
6933         ;; Take care of tree window mode.
6934         (when (get-buffer-window gnus-group-buffer)
6935           (pop-to-buffer gnus-group-buffer))
6936         (gnus-group-jump-to-group newsgroup))
6937     (save-excursion
6938       ;; Take care of tree window mode.
6939       (if (get-buffer-window gnus-group-buffer 0)
6940           (pop-to-buffer gnus-group-buffer)
6941         (set-buffer gnus-group-buffer))
6942       (gnus-group-jump-to-group newsgroup))))
6943
6944 ;; This function returns a list of article numbers based on the
6945 ;; difference between the ranges of read articles in this group and
6946 ;; the range of active articles.
6947 (defun gnus-list-of-unread-articles (group)
6948   (let* ((read (gnus-info-read (gnus-get-info group)))
6949          (active (or (gnus-active group) (gnus-activate-group group)))
6950          (last (or (cdr active)
6951                    (error "Group %s couldn't be activated " group)))
6952          (bottom (if gnus-newsgroup-maximum-articles
6953                      (max (car active)
6954                           (- last gnus-newsgroup-maximum-articles -1))
6955                    (car active)))
6956          first nlast unread)
6957     ;; If none are read, then all are unread.
6958     (if (not read)
6959         (setq first bottom)
6960       ;; If the range of read articles is a single range, then the
6961       ;; first unread article is the article after the last read
6962       ;; article.  Sounds logical, doesn't it?
6963       (if (and (not (listp (cdr read)))
6964                (or (< (car read) bottom)
6965                    (progn (setq read (list read))
6966                           nil)))
6967           (setq first (max bottom (1+ (cdr read))))
6968         ;; `read' is a list of ranges.
6969         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6970                                   (caar read)))
6971                   1)
6972           (setq first bottom))
6973         (while read
6974           (when first
6975             (while (< first nlast)
6976               (setq unread (cons first unread)
6977                     first (1+ first))))
6978           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6979           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6980           (setq read (cdr read)))))
6981     ;; And add the last unread articles.
6982     (while (<= first last)
6983       (setq unread (cons first unread)
6984             first (1+ first)))
6985     ;; Return the list of unread articles.
6986     (delq 0 (nreverse unread))))
6987
6988 (defun gnus-list-of-read-articles (group)
6989   "Return a list of unread, unticked and non-dormant articles."
6990   (let* ((info (gnus-get-info group))
6991          (marked (gnus-info-marks info))
6992          (active (gnus-active group)))
6993     (and info active
6994          (gnus-list-range-difference
6995           (gnus-list-range-difference
6996            (gnus-sorted-complement
6997             (gnus-uncompress-range
6998              (if gnus-newsgroup-maximum-articles
6999                  (cons (max (car active)
7000                             (- (cdr active)
7001                                gnus-newsgroup-maximum-articles
7002                                -1))
7003                        (cdr active))
7004                active))
7005             (gnus-list-of-unread-articles group))
7006            (cdr (assq 'dormant marked)))
7007           (cdr (assq 'tick marked))))))
7008
7009 ;; This function returns a sequence of article numbers based on the
7010 ;; difference between the ranges of read articles in this group and
7011 ;; the range of active articles.
7012 (defun gnus-sequence-of-unread-articles (group)
7013   (let* ((read (gnus-info-read (gnus-get-info group)))
7014          (active (or (gnus-active group) (gnus-activate-group group)))
7015          (last (cdr active))
7016          (bottom (if gnus-newsgroup-maximum-articles
7017                      (max (car active)
7018                           (- last gnus-newsgroup-maximum-articles -1))
7019                    (car active)))
7020          first nlast unread)
7021     ;; If none are read, then all are unread.
7022     (if (not read)
7023         (setq first bottom)
7024       ;; If the range of read articles is a single range, then the
7025       ;; first unread article is the article after the last read
7026       ;; article.  Sounds logical, doesn't it?
7027       (if (and (not (listp (cdr read)))
7028                (or (< (car read) bottom)
7029                    (progn (setq read (list read))
7030                           nil)))
7031           (setq first (max bottom (1+ (cdr read))))
7032         ;; `read' is a list of ranges.
7033         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
7034                                   (caar read)))
7035                   1)
7036           (setq first bottom))
7037         (while read
7038           (when first
7039             (push (cons first nlast) unread))
7040           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
7041           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
7042           (setq read (cdr read)))))
7043     ;; And add the last unread articles.
7044     (cond ((not (and first last))
7045            nil)
7046           ((< first last)
7047            (push (cons first last) unread))
7048           ((= first last)
7049            (push first unread)))
7050     ;; Return the sequence of unread articles.
7051     (delq 0 (nreverse unread))))
7052
7053 ;; Various summary commands
7054
7055 (defun gnus-summary-select-article-buffer ()
7056   "Reconfigure windows to show the article buffer.
7057 If `gnus-widen-article-window' is set, show only the article
7058 buffer."
7059   (interactive)
7060   (if (not (gnus-buffer-live-p gnus-article-buffer))
7061       (error "There is no article buffer for this summary buffer")
7062     (unless (get-buffer-window gnus-article-buffer)
7063       (gnus-summary-show-article))
7064     (gnus-configure-windows
7065      (if gnus-widen-article-window
7066          'only-article
7067        'article)
7068      t)
7069     (select-window (get-buffer-window gnus-article-buffer))))
7070
7071 (defun gnus-summary-universal-argument (arg)
7072   "Perform any operation on all articles that are process/prefixed."
7073   (interactive "P")
7074   (let ((articles (gnus-summary-work-articles arg))
7075         func article)
7076     (if (eq
7077          (setq
7078           func
7079           (key-binding
7080            (read-key-sequence
7081             (substitute-command-keys
7082              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
7083          'undefined)
7084         (gnus-error 1 "Undefined key")
7085       (save-excursion
7086         (while articles
7087           (gnus-summary-goto-subject (setq article (pop articles)))
7088           (let (gnus-newsgroup-processable)
7089             (command-execute func))
7090           (gnus-summary-remove-process-mark article)))))
7091   (gnus-summary-position-point))
7092
7093 (defun gnus-summary-toggle-truncation (&optional arg)
7094   "Toggle truncation of summary lines.
7095 With ARG, turn line truncation on if ARG is positive."
7096   (interactive "P")
7097   (setq truncate-lines
7098         (if (null arg) (not truncate-lines)
7099           (> (prefix-numeric-value arg) 0)))
7100   (redraw-display))
7101
7102 (defun gnus-summary-find-for-reselect ()
7103   "Return the number of an article to stay on across a reselect.
7104 The current article is considered, then following articles, then previous
7105 articles.  An article is sought which is not canceled and isn't a temporary
7106 insertion from another group.  If there's no such then return a dummy 0."
7107   (let (found)
7108     (dolist (rev '(nil t))
7109       (unless found      ; don't demand the reverse list if we don't need it
7110         (let ((data (gnus-data-find-list
7111                      (gnus-summary-article-number) (gnus-data-list rev))))
7112           (while (and data (not found))
7113             (if (and (< 0 (gnus-data-number (car data)))
7114                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7115                 (setq found (gnus-data-number (car data))))
7116             (setq data (cdr data))))))
7117     (or found 0)))
7118
7119 (defun gnus-summary-reselect-current-group (&optional all rescan)
7120   "Exit and then reselect the current newsgroup.
7121 The prefix argument ALL means to select all articles."
7122   (interactive "P")
7123   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7124     (error "Ephemeral groups can't be reselected"))
7125   (let ((current-subject (gnus-summary-find-for-reselect))
7126         (group gnus-newsgroup-name))
7127     (setq gnus-newsgroup-begin nil)
7128     (gnus-summary-exit nil 'leave-hidden)
7129     ;; We have to adjust the point of group mode buffer because
7130     ;; point was moved to the next unread newsgroup by exiting.
7131     (gnus-summary-jump-to-group group)
7132     (when rescan
7133       (save-excursion
7134         (gnus-group-get-new-news-this-group 1)))
7135     (gnus-group-read-group all t)
7136     (gnus-summary-goto-subject current-subject nil t)))
7137
7138 (defun gnus-summary-rescan-group (&optional all)
7139   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7140   (interactive "P")
7141   (let ((config gnus-current-window-configuration))
7142     (gnus-summary-reselect-current-group all t)
7143     (gnus-configure-windows config)
7144     (when (eq config 'article)
7145       (gnus-summary-select-article))))
7146
7147 (defun gnus-summary-update-info (&optional non-destructive)
7148   (save-excursion
7149     (let ((group gnus-newsgroup-name))
7150       (when group
7151         (when gnus-newsgroup-kill-headers
7152           (setq gnus-newsgroup-killed
7153                 (gnus-compress-sequence
7154                  (gnus-sorted-union
7155                   (gnus-list-range-intersection
7156                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7157                   gnus-newsgroup-unreads)
7158                  t)))
7159         (unless (listp (cdr gnus-newsgroup-killed))
7160           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7161         (let ((headers gnus-newsgroup-headers)
7162               (ephemeral-p (gnus-ephemeral-group-p group))
7163               info)
7164           (unless ephemeral-p
7165             (setq info (copy-sequence (gnus-get-info group))
7166                   info (delq (gnus-info-params info) info)))
7167           ;; Set the new ranges of read articles.
7168           (with-current-buffer gnus-group-buffer
7169             (gnus-undo-force-boundary))
7170           (gnus-update-read-articles
7171            group (gnus-sorted-union
7172                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7173           ;; Set the current article marks.
7174           (let ((gnus-newsgroup-scored
7175                  (if (and (not gnus-save-score)
7176                           (not non-destructive))
7177                      nil
7178                    gnus-newsgroup-scored)))
7179             (save-excursion
7180               (gnus-update-marks)))
7181           ;; Do the cross-ref thing.
7182           (when gnus-use-cross-reference
7183             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7184           ;; Do not switch windows but change the buffer to work.
7185           (set-buffer gnus-group-buffer)
7186           (unless ephemeral-p
7187             (gnus-group-update-group
7188              group nil
7189              (equal info
7190                     (setq info (copy-sequence (gnus-get-info group))
7191                           info (delq (gnus-info-params info) info))))))))))
7192
7193 (defun gnus-summary-save-newsrc (&optional force)
7194   "Save the current number of read/marked articles in the dribble buffer.
7195 The dribble buffer will then be saved.
7196 If FORCE (the prefix), also save the .newsrc file(s)."
7197   (interactive "P")
7198   (gnus-summary-update-info t)
7199   (if force
7200       (gnus-save-newsrc-file)
7201     (gnus-dribble-save)))
7202
7203 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7204
7205 (defun gnus-summary-exit (&optional temporary leave-hidden)
7206   "Exit reading current newsgroup, and then return to group selection mode.
7207 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7208   (interactive)
7209   (gnus-set-global-variables)
7210   (when (gnus-buffer-live-p gnus-article-buffer)
7211     (with-current-buffer gnus-article-buffer
7212       (mm-destroy-parts gnus-article-mime-handles)
7213       ;; Set it to nil for safety reason.
7214       (setq gnus-article-mime-handle-alist nil)
7215       (setq gnus-article-mime-handles nil)))
7216   (gnus-kill-save-kill-buffer)
7217   (gnus-async-halt-prefetch)
7218   (let* ((group gnus-newsgroup-name)
7219          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7220          (gnus-group-is-exiting-p t)
7221          (article-buffer gnus-article-buffer)
7222          (original-article-buffer gnus-original-article-buffer)
7223          (mode major-mode)
7224          (group-point nil)
7225          (buf (current-buffer))
7226          ;; `gnus-single-article-buffer' is nil buffer-locally in
7227          ;; ephemeral group of which summary buffer will be killed,
7228          ;; but the global value may be non-nil.
7229          (single-article-buffer gnus-single-article-buffer))
7230     (unless quit-config
7231       ;; Do adaptive scoring, and possibly save score files.
7232       (when gnus-newsgroup-adaptive
7233         (gnus-score-adaptive))
7234       (when gnus-use-scoring
7235         (gnus-score-save)))
7236     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7237     (when gnus-use-cache
7238       (gnus-cache-possibly-remove-articles)
7239       (gnus-cache-save-buffers))
7240     (gnus-async-prefetch-remove-group group)
7241     (when gnus-suppress-duplicates
7242       (gnus-dup-enter-articles))
7243     (when gnus-use-trees
7244       (gnus-tree-close group))
7245     (when gnus-use-cache
7246       (gnus-cache-write-active))
7247     ;; Remove entries for this group.
7248     (nnmail-purge-split-history (gnus-group-real-name group))
7249     ;; Make all changes in this group permanent.
7250     (unless quit-config
7251       (gnus-run-hooks 'gnus-exit-group-hook)
7252       (gnus-summary-update-info))
7253     (gnus-close-group group)
7254     ;; Make sure where we were, and go to next newsgroup.
7255     (set-buffer gnus-group-buffer)
7256     (unless quit-config
7257       (gnus-group-jump-to-group group))
7258     (gnus-run-hooks 'gnus-summary-exit-hook)
7259     (unless (or quit-config
7260                 (not gnus-summary-next-group-on-exit)
7261                 ;; If this group has disappeared from the summary
7262                 ;; buffer, don't skip forwards.
7263                 (not (string= group (gnus-group-group-name))))
7264       (gnus-group-next-unread-group 1))
7265     (setq group-point (point))
7266     (if temporary
7267         nil                             ;Nothing to do.
7268       (set-buffer buf)
7269       (if (not gnus-kill-summary-on-exit)
7270           (progn
7271             (gnus-deaden-summary)
7272             (setq mode nil))
7273         (when (get-buffer gnus-article-buffer)
7274           (bury-buffer gnus-article-buffer))
7275         ;; Return to group mode buffer.
7276         (when (eq mode 'gnus-summary-mode)
7277           (gnus-kill-buffer buf)))
7278
7279       (setq gnus-current-select-method gnus-select-method)
7280       (set-buffer gnus-group-buffer)
7281       (if quit-config
7282           (gnus-handle-ephemeral-exit quit-config)
7283         (goto-char group-point)
7284         ;; If gnus-group-buffer is already displayed, make sure we also move
7285         ;; the cursor in the window that displays it.
7286         (let ((win (get-buffer-window (current-buffer) 0)))
7287           (if win (set-window-point win (point))))
7288         (unless leave-hidden
7289           (gnus-configure-windows 'group 'force)))
7290
7291       ;; If we have several article buffers, we kill them at exit.
7292       (unless single-article-buffer
7293         (when (gnus-buffer-live-p article-buffer)
7294           (with-current-buffer article-buffer
7295             ;; Don't kill sticky article buffers
7296             (unless (eq major-mode 'gnus-sticky-article-mode)
7297               (gnus-kill-buffer article-buffer)
7298               (setq gnus-article-current nil))))
7299         (gnus-kill-buffer original-article-buffer))
7300
7301       ;; Clear the current group name.
7302       (unless quit-config
7303         (setq gnus-newsgroup-name nil)))))
7304
7305 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7306 (defun gnus-summary-exit-no-update (&optional no-questions)
7307   "Quit reading current newsgroup without updating read article info."
7308   (interactive)
7309   (let* ((group gnus-newsgroup-name)
7310          (gnus-group-is-exiting-p t)
7311          (gnus-group-is-exiting-without-update-p t)
7312          (quit-config (gnus-group-quit-config group)))
7313     (when (or no-questions
7314               gnus-expert-user
7315               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7316       (gnus-async-halt-prefetch)
7317       (run-hooks 'gnus-summary-prepare-exit-hook)
7318       (when (gnus-buffer-live-p gnus-article-buffer)
7319         (with-current-buffer gnus-article-buffer
7320           (gnus-article-stop-animations)
7321           (mm-destroy-parts gnus-article-mime-handles)
7322           ;; Set it to nil for safety reason.
7323           (setq gnus-article-mime-handle-alist nil)
7324           (setq gnus-article-mime-handles nil)))
7325       ;; If we have several article buffers, we kill them at exit.
7326       (unless gnus-single-article-buffer
7327         (gnus-kill-buffer gnus-article-buffer)
7328         (gnus-kill-buffer gnus-original-article-buffer)
7329         (setq gnus-article-current nil))
7330       ;; Return to the group buffer.
7331       (gnus-configure-windows 'group 'force)
7332       (if (not gnus-kill-summary-on-exit)
7333           (gnus-deaden-summary)
7334         (gnus-close-group group)
7335         (gnus-kill-buffer gnus-summary-buffer))
7336       (unless gnus-single-article-buffer
7337         (setq gnus-article-current nil))
7338       (when gnus-use-trees
7339         (gnus-tree-close group))
7340       (gnus-async-prefetch-remove-group group)
7341       (when (get-buffer gnus-article-buffer)
7342         (bury-buffer gnus-article-buffer))
7343       ;; Clear the current group name.
7344       (setq gnus-newsgroup-name nil)
7345       (unless (gnus-ephemeral-group-p group)
7346         (gnus-group-update-group group nil t))
7347       (when (equal (gnus-group-group-name) group)
7348         (gnus-group-next-unread-group 1))
7349       (when quit-config
7350         (gnus-handle-ephemeral-exit quit-config)))))
7351
7352 (defun gnus-handle-ephemeral-exit (quit-config)
7353   "Handle movement when leaving an ephemeral group.
7354 The state which existed when entering the ephemeral is reset."
7355   (if (not (buffer-name (car quit-config)))
7356       (gnus-configure-windows 'group 'force)
7357     (set-buffer (car quit-config))
7358     (unless (eq (cdr quit-config) 'group)
7359       (setq gnus-current-select-method
7360             (gnus-find-method-for-group gnus-newsgroup-name)))
7361     (cond ((eq major-mode 'gnus-summary-mode)
7362            (gnus-set-global-variables))
7363           ((eq major-mode 'gnus-article-mode)
7364            (save-current-buffer
7365              ;; The `gnus-summary-buffer' variable may point
7366              ;; to the old summary buffer when using a single
7367              ;; article buffer.
7368              (unless (gnus-buffer-live-p gnus-summary-buffer)
7369                (set-buffer gnus-group-buffer))
7370              (set-buffer gnus-summary-buffer)
7371              (gnus-set-global-variables))))
7372     (if (or (eq (cdr quit-config) 'article)
7373             (eq (cdr quit-config) 'pick))
7374         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7375             (gnus-configure-windows 'pick 'force)
7376           (gnus-configure-windows (cdr quit-config) 'force))
7377       (gnus-configure-windows (cdr quit-config) 'force))
7378     (when (eq major-mode 'gnus-summary-mode)
7379       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7380                                                      next-unread-noselect))
7381           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7382                                   'next-noselect)
7383                               (gnus-summary-next-subject 1 nil t))
7384                              ((eq gnus-auto-select-on-ephemeral-exit
7385                                   'next-unread-noselect)
7386                               (gnus-summary-next-subject 1 t t))))
7387             ;; Hide the article buffer which displays the article different
7388             ;; from the one that the cursor points to in the summary buffer.
7389             (gnus-configure-windows 'summary 'force))
7390         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7391                (gnus-summary-next-subject 1))
7392               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7393                (gnus-summary-next-subject 1 t))))
7394       (gnus-summary-recenter)
7395       (gnus-summary-position-point))))
7396
7397 ;;; Dead summaries.
7398
7399 (defvar gnus-dead-summary-mode-map
7400   (let ((map (make-keymap)))
7401     (suppress-keymap map)
7402     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7403     (dolist (key '("\C-d" "\r" "\177" [delete]))
7404       (define-key map key 'gnus-summary-wake-up-the-dead))
7405     (dolist (key '("q" "Q"))
7406       (define-key map key 'bury-buffer))
7407     map))
7408
7409 (define-minor-mode gnus-dead-summary-mode
7410   "Minor mode for Gnus summary buffers."
7411   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7412   (unless (derived-mode-p 'gnus-summary-mode)
7413     (setq gnus-dead-summary-mode nil)))
7414
7415 (defun gnus-deaden-summary ()
7416   "Make the current summary buffer into a dead summary buffer."
7417   ;; Kill any previous dead summary buffer.
7418   (when (and gnus-dead-summary
7419              (buffer-name gnus-dead-summary))
7420     (with-current-buffer gnus-dead-summary
7421       (when gnus-dead-summary-mode
7422         (kill-buffer (current-buffer)))))
7423   ;; Make this the current dead summary.
7424   (setq gnus-dead-summary (current-buffer))
7425   (gnus-dead-summary-mode 1)
7426   (let ((name (buffer-name)))
7427     (when (string-match "Summary" name)
7428       (rename-buffer
7429        (concat (substring name 0 (match-beginning 0)) "Dead "
7430                (substring name (match-beginning 0)))
7431        t)
7432       (bury-buffer))))
7433
7434 (defun gnus-kill-or-deaden-summary (buffer)
7435   "Kill or deaden the summary BUFFER."
7436   (save-excursion
7437     (when (and (buffer-name buffer)
7438                (not gnus-single-article-buffer))
7439       (with-current-buffer buffer
7440         (gnus-kill-buffer gnus-article-buffer)
7441         (gnus-kill-buffer gnus-original-article-buffer)))
7442     (cond
7443      ;; Kill the buffer.
7444      (gnus-kill-summary-on-exit
7445       (when (and gnus-use-trees
7446                  (gnus-buffer-exists-p buffer))
7447         (with-current-buffer buffer
7448           (gnus-tree-close gnus-newsgroup-name)))
7449       (gnus-kill-buffer buffer))
7450      ;; Deaden the buffer.
7451      ((gnus-buffer-exists-p buffer)
7452       (with-current-buffer buffer
7453         (gnus-deaden-summary))))))
7454
7455 (defun gnus-summary-wake-up-the-dead (&rest args)
7456   "Wake up the dead summary buffer."
7457   (interactive)
7458   (gnus-dead-summary-mode -1)
7459   (let ((name (buffer-name)))
7460     (when (string-match "Dead " name)
7461       (rename-buffer
7462        (concat (substring name 0 (match-beginning 0))
7463                (substring name (match-end 0)))
7464        t)))
7465   (gnus-message 3 "This dead summary is now alive again"))
7466
7467 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7468 (defun gnus-summary-describe-group (&optional force)
7469   "Describe the current newsgroup."
7470   (interactive "P")
7471   (gnus-group-describe-group force gnus-newsgroup-name))
7472
7473 (defun gnus-summary-describe-briefly ()
7474   "Describe summary mode commands briefly."
7475   (interactive)
7476   (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")))
7477
7478 ;; Walking around group mode buffer from summary mode.
7479
7480 (defun gnus-summary-next-group (&optional no-article target-group backward)
7481   "Exit current newsgroup and then select next unread newsgroup.
7482 If prefix argument NO-ARTICLE is non-nil, no article is selected
7483 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7484 previous group instead."
7485   (interactive "P")
7486   ;; Stop pre-fetching.
7487   (gnus-async-halt-prefetch)
7488   (let ((current-group gnus-newsgroup-name)
7489         (current-buffer (current-buffer))
7490         entered)
7491     ;; First we semi-exit this group to update Xrefs and all variables.
7492     ;; We can't do a real exit, because the window conf must remain
7493     ;; the same in case the user is prompted for info, and we don't
7494     ;; want the window conf to change before that...
7495     (gnus-summary-exit t)
7496     (while (not entered)
7497       ;; Then we find what group we are supposed to enter.
7498       (set-buffer gnus-group-buffer)
7499       (gnus-group-jump-to-group current-group)
7500       (setq target-group
7501             (or target-group
7502                 (if (eq gnus-keep-same-level 'best)
7503                     (gnus-summary-best-group gnus-newsgroup-name)
7504                   (gnus-summary-search-group backward gnus-keep-same-level))))
7505       (if (not target-group)
7506           ;; There are no further groups, so we return to the group
7507           ;; buffer.
7508           (progn
7509             (gnus-message 5 "Returning to the group buffer")
7510             (setq entered t)
7511             (when (gnus-buffer-live-p current-buffer)
7512               (set-buffer current-buffer)
7513               (gnus-summary-exit))
7514             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7515         ;; We try to enter the target group.
7516         (gnus-group-jump-to-group target-group)
7517         (let ((unreads (gnus-group-group-unread)))
7518           (if (and (or (eq t unreads)
7519                        (and unreads (not (zerop unreads))))
7520                    (gnus-summary-read-group
7521                     target-group nil no-article
7522                     (and (buffer-name current-buffer) current-buffer)
7523                     nil backward))
7524               (setq entered t)
7525             (setq current-group target-group
7526                   target-group nil)))))))
7527
7528 (defun gnus-summary-prev-group (&optional no-article)
7529   "Exit current newsgroup and then select previous unread newsgroup.
7530 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7531   (interactive "P")
7532   (gnus-summary-next-group no-article nil t))
7533
7534 ;; Walking around summary lines.
7535
7536 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7537   "Go to the first subject satisfying any non-nil constraint.
7538 If UNREAD is non-nil, the article should be unread.
7539 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7540 If UNSEEN is non-nil, the article should be unseen as well as unread.
7541 Returns the article selected or nil if there are no matching articles."
7542   (interactive "P")
7543   (cond
7544    ;; Empty summary.
7545    ((null gnus-newsgroup-data)
7546     (gnus-message 3 "No articles in the group")
7547     nil)
7548    ;; Pick the first article.
7549    ((not (or unread undownloaded unseen))
7550     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7551     (gnus-data-number (car gnus-newsgroup-data)))
7552    ;; Find the first unread article.
7553    (t
7554     (let ((data gnus-newsgroup-data))
7555       (while (and data
7556                   (let ((num (gnus-data-number (car data))))
7557                     (or (memq num gnus-newsgroup-unfetched)
7558                         (not (or (and unread
7559                                       (memq num gnus-newsgroup-unreads))
7560                                  (and undownloaded
7561                                       (memq num gnus-newsgroup-undownloaded))
7562                                  (and unseen
7563                                       (memq num gnus-newsgroup-unseen)
7564                                       (memq num gnus-newsgroup-unreads)))))))
7565         (setq data (cdr data)))
7566       (prog1
7567           (if data
7568               (progn
7569                 (goto-char (gnus-data-pos (car data)))
7570                 (gnus-data-number (car data)))
7571             (gnus-message 3 "No more%s articles"
7572                           (let* ((r (when unread " unread"))
7573                                  (d (when undownloaded " undownloaded"))
7574                                  (s (when unseen " unseen"))
7575                                  (l (delq nil (list r d s))))
7576                             (cond ((= 3 (length l))
7577                                    (concat r "," d ", or" s))
7578                                   ((= 2 (length l))
7579                                    (concat (car l) ", or" (cadr l)))
7580                                   ((= 1 (length l))
7581                                    (car l))
7582                                   (t
7583                                    ""))))
7584             nil
7585             )
7586         (gnus-summary-position-point))))))
7587
7588 (defun gnus-summary-next-subject (n &optional unread dont-display)
7589   "Go to next N'th summary line.
7590 If N is negative, go to the previous N'th subject line.
7591 If UNREAD is non-nil, only unread articles are selected.
7592 The difference between N and the actual number of steps taken is
7593 returned."
7594   (interactive "p")
7595   (let ((backward (< n 0))
7596         (n (abs n)))
7597     (while (and (> n 0)
7598                 (if backward
7599                     (gnus-summary-find-prev unread)
7600                   (gnus-summary-find-next unread)))
7601       (unless (zerop (setq n (1- n)))
7602         (gnus-summary-show-thread)))
7603     (when (/= 0 n)
7604       (gnus-message 7 "No more%s articles"
7605                     (if unread " unread" "")))
7606     (unless dont-display
7607       (gnus-summary-recenter)
7608       (gnus-summary-position-point))
7609     n))
7610
7611 (defun gnus-summary-next-unread-subject (n)
7612   "Go to next N'th unread summary line."
7613   (interactive "p")
7614   (gnus-summary-next-subject n t))
7615
7616 (defun gnus-summary-prev-subject (n &optional unread)
7617   "Go to previous N'th summary line.
7618 If optional argument UNREAD is non-nil, only unread article is selected."
7619   (interactive "p")
7620   (gnus-summary-next-subject (- n) unread))
7621
7622 (defun gnus-summary-prev-unread-subject (n)
7623   "Go to previous N'th unread summary line."
7624   (interactive "p")
7625   (gnus-summary-next-subject (- n) t))
7626
7627 (defun gnus-summary-goto-subjects (articles)
7628   "Insert the subject header for ARTICLES in the current buffer."
7629   (save-excursion
7630     (dolist (article articles)
7631       (gnus-summary-goto-subject article t)))
7632   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7633   (gnus-summary-position-point))
7634
7635 (defun gnus-summary-goto-subject (article &optional force silent)
7636   "Go to the subject line of ARTICLE.
7637 If FORCE, also allow jumping to articles not currently shown."
7638   (interactive "nArticle number: ")
7639   (unless (numberp article)
7640     (error "Article %s is not a number" article))
7641   (let ((b (point))
7642         (data (gnus-data-find article)))
7643     ;; We read in the article if we have to.
7644     (and (not data)
7645          force
7646          (gnus-summary-insert-subject
7647           article
7648           (if (or (numberp force) (vectorp force)) force)
7649           t)
7650          (setq data (gnus-data-find article)))
7651     (goto-char b)
7652     (if (not data)
7653         (progn
7654           (unless silent
7655             (gnus-message 3 "Can't find article %d" article))
7656           nil)
7657       (let ((pt (gnus-data-pos data)))
7658         (goto-char pt)
7659         (gnus-summary-set-article-display-arrow pt))
7660       (gnus-summary-position-point)
7661       article)))
7662
7663 ;; Walking around summary lines with displaying articles.
7664
7665 (defun gnus-summary-expand-window (&optional arg)
7666   "Make the summary buffer take up the entire Emacs frame.
7667 Given a prefix, will force an `article' buffer configuration."
7668   (interactive "P")
7669   (if arg
7670       (gnus-configure-windows 'article 'force)
7671     (gnus-configure-windows 'summary 'force)))
7672
7673 (defun gnus-summary-display-article (article &optional all-header)
7674   "Display ARTICLE in article buffer."
7675   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7676                (with-current-buffer gnus-article-buffer
7677                  (eq major-mode 'gnus-article-mode)))
7678     (gnus-article-setup-buffer))
7679   (gnus-set-global-variables)
7680   (with-current-buffer gnus-article-buffer
7681     (setq gnus-article-charset gnus-newsgroup-charset)
7682     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7683     (mm-enable-multibyte))
7684   (if (null article)
7685       nil
7686     (prog1
7687         (if gnus-summary-display-article-function
7688             (funcall gnus-summary-display-article-function article all-header)
7689           (gnus-article-prepare article all-header))
7690       (gnus-run-hooks 'gnus-select-article-hook)
7691       (when (and gnus-current-article
7692                  (not (zerop gnus-current-article)))
7693         (gnus-summary-goto-subject gnus-current-article))
7694       (gnus-summary-recenter)
7695       (when (and gnus-use-trees gnus-show-threads)
7696         (gnus-possibly-generate-tree article)
7697         (gnus-highlight-selected-tree article))
7698       ;; Successfully display article.
7699       (gnus-article-set-window-start
7700        (cdr (assq article gnus-newsgroup-bookmarks))))))
7701
7702 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7703   "Select the current article.
7704 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7705 non-nil, the article will be re-fetched even if it already present in
7706 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7707 be displayed."
7708   ;; Make sure we are in the summary buffer to work around bbdb bug.
7709   (unless (eq major-mode 'gnus-summary-mode)
7710     (set-buffer gnus-summary-buffer))
7711   (let ((article (or article (gnus-summary-article-number)))
7712         (all-headers (not (not all-headers))) ;Must be t or nil.
7713         gnus-summary-display-article-function)
7714     (and (not pseudo)
7715          (gnus-summary-article-pseudo-p article)
7716          (error "This is a pseudo-article"))
7717     (with-current-buffer gnus-summary-buffer
7718       (if (or (and gnus-single-article-buffer
7719                    (or (null gnus-current-article)
7720                        (null gnus-article-current)
7721                        (null (get-buffer gnus-article-buffer))
7722                        (not (eq article (cdr gnus-article-current)))
7723                        (not (equal (car gnus-article-current)
7724                                    gnus-newsgroup-name))
7725                        (not (get-buffer gnus-original-article-buffer))))
7726               (and (not gnus-single-article-buffer)
7727                    (or (null gnus-current-article)
7728                        (not (get-buffer gnus-original-article-buffer))
7729                        (not (eq gnus-current-article article))))
7730               force)
7731           ;; The requested article is different from the current article.
7732           (progn
7733             (gnus-summary-display-article article all-headers)
7734             (when (gnus-buffer-live-p gnus-article-buffer)
7735               (with-current-buffer gnus-article-buffer
7736                 (if (not gnus-article-decoded-p) ;; a local variable
7737                     (mm-disable-multibyte))))
7738             (gnus-article-set-window-start
7739              (cdr (assq article gnus-newsgroup-bookmarks)))
7740             article)
7741         'old))))
7742
7743 (defun gnus-summary-force-verify-and-decrypt ()
7744   "Display buttons for signed/encrypted parts and verify/decrypt them."
7745   (interactive)
7746   (let ((mm-verify-option 'known)
7747         (mm-decrypt-option 'known)
7748         (gnus-article-emulate-mime t)
7749         (gnus-buttonized-mime-types (append (list "multipart/signed"
7750                                                   "multipart/encrypted")
7751                                             gnus-buttonized-mime-types)))
7752     (gnus-summary-select-article nil 'force)))
7753
7754 (defun gnus-summary-set-current-mark (&optional current-mark)
7755   "Obsolete function."
7756   nil)
7757
7758 (defun gnus-summary-next-article (&optional unread subject backward push)
7759   "Select the next article.
7760 If UNREAD, only unread articles are selected.
7761 If SUBJECT, only articles with SUBJECT are selected.
7762 If BACKWARD, the previous article is selected instead of the next."
7763   (interactive "P")
7764   ;; Make sure we are in the summary buffer.
7765   (unless (eq major-mode 'gnus-summary-mode)
7766     (set-buffer gnus-summary-buffer))
7767   (cond
7768    ;; Is there such an article?
7769    ((and (gnus-summary-search-forward unread subject backward)
7770          (or (gnus-summary-display-article (gnus-summary-article-number))
7771              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7772     (gnus-summary-position-point))
7773    ;; If not, we try the first unread, if that is wanted.
7774    ((and subject
7775          gnus-auto-select-same
7776          (gnus-summary-first-unread-article))
7777     (gnus-summary-position-point)
7778     (gnus-message 6 "Wrapped"))
7779    ;; Try to get next/previous article not displayed in this group.
7780    ((and gnus-auto-extend-newsgroup
7781          (not unread) (not subject))
7782     (gnus-summary-goto-article
7783      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7784      nil (count-lines (point-min) (point))))
7785    ;; Go to next/previous group.
7786    (t
7787     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7788       (gnus-summary-jump-to-group gnus-newsgroup-name))
7789     (let ((cmd (if (featurep 'xemacs)
7790                    last-command-char
7791                  last-command-event))
7792           (point
7793            (with-current-buffer gnus-group-buffer
7794              (point)))
7795           (current-summary (current-buffer))
7796           (group
7797            (if (eq gnus-keep-same-level 'best)
7798                (gnus-summary-best-group gnus-newsgroup-name)
7799              (gnus-summary-search-group backward gnus-keep-same-level))))
7800       ;; Select next unread newsgroup automagically.
7801       (cond
7802        ((or (not gnus-auto-select-next)
7803             (not cmd))
7804         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7805        ((or (eq gnus-auto-select-next 'quietly)
7806             (and (eq gnus-auto-select-next 'slightly-quietly)
7807                  push)
7808             (and (eq gnus-auto-select-next 'almost-quietly)
7809                  (gnus-summary-last-article-p)))
7810         ;; Select quietly.
7811         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7812             (gnus-summary-exit)
7813           (gnus-message 7 "No more%s articles (%s)..."
7814                         (if unread " unread" "")
7815                         (if group (concat "selecting " group)
7816                           "exiting"))
7817           (gnus-summary-next-group nil group backward)))
7818        (t
7819         (when (gnus-key-press-event-p last-input-event)
7820           ;; Somehow or other, we may now have selected a different
7821           ;; window.  Make point go back to the summary buffer.
7822           (when (eq current-summary (current-buffer))
7823             ;; FIXME: This burps when get-buffer-window returns nil.
7824             (select-window (get-buffer-window current-summary 0)))
7825           (gnus-summary-walk-group-buffer
7826            gnus-newsgroup-name cmd unread backward point))))))))
7827
7828 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7829   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7830                       (?\C-p (gnus-group-prev-unread-group 1))))
7831         (cursor-in-echo-area t)
7832         keve key group ended prompt)
7833     (with-current-buffer gnus-group-buffer
7834       (goto-char start)
7835       (setq group
7836             (if (eq gnus-keep-same-level 'best)
7837                 (gnus-summary-best-group gnus-newsgroup-name)
7838               (gnus-summary-search-group backward gnus-keep-same-level))))
7839     (while (not ended)
7840       (setq prompt
7841             (format
7842              "No more%s articles%s " (if unread " unread" "")
7843              (if (and group
7844                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7845                  (format " (Type %s for %s [%s])"
7846                          (single-key-description cmd)
7847                          (gnus-group-decoded-name group)
7848                          (gnus-group-unread group))
7849                (format " (Type %s to exit %s)"
7850                        (single-key-description cmd)
7851                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7852       ;; Confirm auto selection.
7853       (setq key (car (setq keve (gnus-read-event-char prompt)))
7854             ended t)
7855       (cond
7856        ((assq key keystrokes)
7857         (let ((obuf (current-buffer)))
7858           (switch-to-buffer gnus-group-buffer)
7859           (when group
7860             (gnus-group-jump-to-group group))
7861           (eval (cadr (assq key keystrokes)))
7862           (setq group (gnus-group-group-name))
7863           (switch-to-buffer obuf))
7864         (setq ended nil))
7865        ((equal key cmd)
7866         (if (or (not group)
7867                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7868             (gnus-summary-exit)
7869           (gnus-summary-next-group nil group backward)))
7870        (t
7871         (push (cdr keve) unread-command-events))))))
7872
7873 (defun gnus-summary-next-unread-article ()
7874   "Select unread article after current one."
7875   (interactive)
7876   (gnus-summary-next-article
7877    (or (not (eq gnus-summary-goto-unread 'never))
7878        (gnus-summary-last-article-p (gnus-summary-article-number)))
7879    (and gnus-auto-select-same
7880         (gnus-summary-article-subject))))
7881
7882 (defun gnus-summary-prev-article (&optional unread subject)
7883   "Select the article before the current one.
7884 If UNREAD is non-nil, only unread articles are selected."
7885   (interactive "P")
7886   (gnus-summary-next-article unread subject t))
7887
7888 (defun gnus-summary-prev-unread-article ()
7889   "Select unread article before current one."
7890   (interactive)
7891   (gnus-summary-prev-article
7892    (or (not (eq gnus-summary-goto-unread 'never))
7893        (gnus-summary-first-article-p (gnus-summary-article-number)))
7894    (and gnus-auto-select-same
7895         (gnus-summary-article-subject))))
7896
7897 (defun gnus-summary-next-page (&optional lines circular stop)
7898   "Show next page of the selected article.
7899 If at the end of the current article, select the next article.
7900 LINES says how many lines should be scrolled up.
7901
7902 If CIRCULAR is non-nil, go to the start of the article instead of
7903 selecting the next article when reaching the end of the current
7904 article.
7905
7906 If STOP is non-nil, just stop when reaching the end of the message.
7907
7908 Also see the variable `gnus-article-skip-boring'."
7909   (interactive "P")
7910   (setq gnus-summary-buffer (current-buffer))
7911   (gnus-set-global-variables)
7912   (let ((article (gnus-summary-article-number))
7913         (article-window (get-buffer-window gnus-article-buffer t))
7914         endp)
7915     ;; If the buffer is empty, we have no article.
7916     (unless article
7917       (error "No article to select"))
7918     (gnus-configure-windows 'article)
7919     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7920         (if (and (eq gnus-summary-goto-unread 'never)
7921                  (not (gnus-summary-last-article-p article)))
7922             (gnus-summary-next-article)
7923           (gnus-summary-next-unread-article))
7924       (if (or (null gnus-current-article)
7925               (null gnus-article-current)
7926               (/= article (cdr gnus-article-current))
7927               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7928           ;; Selected subject is different from current article's.
7929           (gnus-summary-display-article article)
7930         (when article-window
7931           (gnus-eval-in-buffer-window gnus-article-buffer
7932             (setq endp (or (gnus-article-next-page lines)
7933                            (gnus-article-only-boring-p))))
7934           (when endp
7935             (cond ((or stop gnus-summary-stop-at-end-of-message)
7936                    (gnus-message 3 "End of message"))
7937                   (circular
7938                    (gnus-summary-beginning-of-article))
7939                   (lines
7940                    (gnus-message 3 "End of message"))
7941                   ((null lines)
7942                    (if (and (eq gnus-summary-goto-unread 'never)
7943                             (not (gnus-summary-last-article-p article)))
7944                        (gnus-summary-next-article)
7945                      (gnus-summary-next-unread-article))))))))
7946     (gnus-summary-recenter)
7947     (gnus-summary-position-point)))
7948
7949 (defun gnus-summary-prev-page (&optional lines move)
7950   "Show previous page of selected article.
7951 Argument LINES specifies lines to be scrolled down.
7952 If MOVE, move to the previous unread article if point is at
7953 the beginning of the buffer."
7954   (interactive "P")
7955   (let ((article (gnus-summary-article-number))
7956         (article-window (get-buffer-window gnus-article-buffer t))
7957         endp)
7958     (gnus-configure-windows 'article)
7959     (if (or (null gnus-current-article)
7960             (null gnus-article-current)
7961             (/= article (cdr gnus-article-current))
7962             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7963         ;; Selected subject is different from current article's.
7964         (gnus-summary-display-article article)
7965       (gnus-summary-recenter)
7966       (when article-window
7967         (gnus-eval-in-buffer-window gnus-article-buffer
7968           (setq endp (gnus-article-prev-page lines)))
7969         (when (and move endp)
7970           (cond (lines
7971                  (gnus-message 3 "Beginning of message"))
7972                 ((null lines)
7973                  (if (and (eq gnus-summary-goto-unread 'never)
7974                           (not (gnus-summary-first-article-p article)))
7975                      (gnus-summary-prev-article)
7976                    (gnus-summary-prev-unread-article))))))))
7977   (gnus-summary-position-point))
7978
7979 (defun gnus-summary-prev-page-or-article (&optional lines)
7980   "Show previous page of selected article.
7981 Argument LINES specifies lines to be scrolled down.
7982 If at the beginning of the article, go to the next article."
7983   (interactive "P")
7984   (gnus-summary-prev-page lines t))
7985
7986 (defun gnus-summary-scroll-up (lines)
7987   "Scroll up (or down) one line current article.
7988 Argument LINES specifies lines to be scrolled up (or down if negative).
7989 If no article is selected, then the current article will be selected first."
7990   (interactive "p")
7991   (gnus-configure-windows 'article)
7992   (gnus-summary-show-thread)
7993   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7994     (gnus-eval-in-buffer-window gnus-article-buffer
7995       (cond ((> lines 0)
7996              (when (gnus-article-next-page lines)
7997                (gnus-message 3 "End of message")))
7998             ((< lines 0)
7999              (gnus-article-prev-page (- lines))))))
8000   (gnus-summary-recenter)
8001   (gnus-summary-position-point))
8002
8003 (defun gnus-summary-scroll-down (lines)
8004   "Scroll down (or up) one line current article.
8005 Argument LINES specifies lines to be scrolled down (or up if negative).
8006 If no article is selected, then the current article will be selected first."
8007   (interactive "p")
8008   (gnus-summary-scroll-up (- lines)))
8009
8010 (defun gnus-summary-next-same-subject ()
8011   "Select next article which has the same subject as current one."
8012   (interactive)
8013   (gnus-summary-next-article nil (gnus-summary-article-subject)))
8014
8015 (defun gnus-summary-prev-same-subject ()
8016   "Select previous article which has the same subject as current one."
8017   (interactive)
8018   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
8019
8020 (defun gnus-summary-next-unread-same-subject ()
8021   "Select next unread article which has the same subject as current one."
8022   (interactive)
8023   (gnus-summary-next-article t (gnus-summary-article-subject)))
8024
8025 (defun gnus-summary-prev-unread-same-subject ()
8026   "Select previous unread article which has the same subject as current one."
8027   (interactive)
8028   (gnus-summary-prev-article t (gnus-summary-article-subject)))
8029
8030 (defun gnus-summary-first-unread-article ()
8031   "Select the first unread article.
8032 Return nil if there are no unread articles."
8033   (interactive)
8034   (prog1
8035       (when (gnus-summary-first-subject t)
8036         (gnus-summary-show-thread)
8037         (gnus-summary-first-subject t)
8038         (gnus-summary-display-article (gnus-summary-article-number)))
8039     (gnus-summary-position-point)))
8040
8041 (defun gnus-summary-first-unread-subject ()
8042   "Place the point on the subject line of the first unread article.
8043 Return nil if there are no unread articles."
8044   (interactive)
8045   (prog1
8046       (when (gnus-summary-first-subject t)
8047         (gnus-summary-show-thread)
8048         (gnus-summary-first-subject t))
8049     (gnus-summary-position-point)))
8050
8051 (defun gnus-summary-first-unseen-subject ()
8052   "Place the point on the subject line of the first unseen article.
8053 Return nil if there are no unseen articles."
8054   (interactive)
8055   (prog1
8056       (when (gnus-summary-first-subject nil nil t)
8057         (gnus-summary-show-thread)
8058         (gnus-summary-first-subject nil nil t))
8059     (gnus-summary-position-point)))
8060
8061 (defun gnus-summary-first-unseen-or-unread-subject ()
8062   "Place the point on the subject line of the first unseen and unread article.
8063 If all article have been seen, on the subject line of the first unread
8064 article."
8065   (interactive)
8066   (prog1
8067       (unless (when (gnus-summary-first-subject nil nil t)
8068                 (gnus-summary-show-thread)
8069                 (gnus-summary-first-subject nil nil t))
8070         (when (gnus-summary-first-subject t)
8071           (gnus-summary-show-thread)
8072           (gnus-summary-first-subject t)))
8073     (gnus-summary-position-point)))
8074
8075 (defun gnus-summary-first-article ()
8076   "Select the first article.
8077 Return nil if there are no articles."
8078   (interactive)
8079   (prog1
8080       (when (gnus-summary-first-subject)
8081         (gnus-summary-show-thread)
8082         (gnus-summary-first-subject)
8083         (gnus-summary-display-article (gnus-summary-article-number)))
8084     (gnus-summary-position-point)))
8085
8086 (defun gnus-summary-best-unread-article (&optional arg)
8087   "Select the unread article with the highest score.
8088 If given a prefix argument, select the next unread article that has a
8089 score higher than the default score."
8090   (interactive "P")
8091   (let ((article (if arg
8092                      (gnus-summary-better-unread-subject)
8093                    (gnus-summary-best-unread-subject))))
8094     (if article
8095         (gnus-summary-goto-article article)
8096       (error "No unread articles"))))
8097
8098 (defun gnus-summary-best-unread-subject ()
8099   "Select the unread subject with the highest score."
8100   (interactive)
8101   (let ((best -1000000)
8102         (data gnus-newsgroup-data)
8103         article score)
8104     (while data
8105       (and (gnus-data-unread-p (car data))
8106            (> (setq score
8107                     (gnus-summary-article-score (gnus-data-number (car data))))
8108               best)
8109            (setq best score
8110                  article (gnus-data-number (car data))))
8111       (setq data (cdr data)))
8112     (when article
8113       (gnus-summary-goto-subject article))
8114     (gnus-summary-position-point)
8115     article))
8116
8117 (defun gnus-summary-better-unread-subject ()
8118   "Select the first unread subject that has a score over the default score."
8119   (interactive)
8120   (let ((data gnus-newsgroup-data)
8121         article score)
8122     (while (and (setq article (gnus-data-number (car data)))
8123                 (or (gnus-data-read-p (car data))
8124                     (not (> (gnus-summary-article-score article)
8125                             gnus-summary-default-score))))
8126       (setq data (cdr data)))
8127     (when article
8128       (gnus-summary-goto-subject article))
8129     (gnus-summary-position-point)
8130     article))
8131
8132 (defun gnus-summary-last-subject ()
8133   "Go to the last displayed subject line in the group."
8134   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8135     (when article
8136       (gnus-summary-goto-subject article))))
8137
8138 (defun gnus-summary-goto-article (article &optional all-headers force)
8139   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8140 If ALL-HEADERS is non-nil, no header lines are hidden.
8141 If FORCE, go to the article even if it isn't displayed.  If FORCE
8142 is a number, it is the line the article is to be displayed on."
8143   (interactive
8144    (list
8145     (gnus-completing-read
8146      "Article number or Message-ID"
8147      (mapcar 'int-to-string gnus-newsgroup-limit))
8148     current-prefix-arg
8149     t))
8150   (prog1
8151       (if (and (stringp article)
8152                (string-match "@\\|%40" article))
8153           (gnus-summary-refer-article article)
8154         (when (stringp article)
8155           (setq article (string-to-number article)))
8156         (if (gnus-summary-goto-subject article force)
8157             (gnus-summary-display-article article all-headers)
8158           (gnus-message 4 "Couldn't go to article %s" article) nil))
8159     (gnus-summary-position-point)))
8160
8161 (defun gnus-summary-goto-last-article ()
8162   "Go to the previously read article."
8163   (interactive)
8164   (prog1
8165       (when gnus-last-article
8166         (gnus-summary-goto-article gnus-last-article nil t))
8167     (gnus-summary-position-point)))
8168
8169 (defun gnus-summary-pop-article (number)
8170   "Pop one article off the history and go to the previous.
8171 NUMBER articles will be popped off."
8172   (interactive "p")
8173   (let (to)
8174     (setq gnus-newsgroup-history
8175           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8176     (if to
8177         (gnus-summary-goto-article (car to) nil t)
8178       (error "Article history empty")))
8179   (gnus-summary-position-point))
8180
8181 ;; Summary commands and functions for limiting the summary buffer.
8182
8183 (defun gnus-summary-limit-to-articles (n)
8184   "Limit the summary buffer to the next N articles.
8185 If not given a prefix, use the process marked articles instead."
8186   (interactive "P")
8187   (prog1
8188       (let ((articles (gnus-summary-work-articles n)))
8189         (setq gnus-newsgroup-processable nil)
8190         (gnus-summary-limit articles))
8191     (gnus-summary-position-point)))
8192
8193 (defun gnus-summary-pop-limit (&optional total)
8194   "Restore the previous limit.
8195 If given a prefix, remove all limits."
8196   (interactive "P")
8197   (when total
8198     (setq gnus-newsgroup-limits
8199           (list (mapcar (lambda (h) (mail-header-number h))
8200                         gnus-newsgroup-headers))))
8201   (unless gnus-newsgroup-limits
8202     (error "No limit to pop"))
8203   (prog1
8204       (gnus-summary-limit nil 'pop)
8205     (gnus-summary-position-point)))
8206
8207 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8208   "Limit the summary buffer to articles that have subjects that match a regexp.
8209 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8210   (interactive
8211    (list (read-string (if current-prefix-arg
8212                           "Exclude subject (regexp): "
8213                         "Limit to subject (regexp): "))
8214          nil current-prefix-arg))
8215   (unless header
8216     (setq header "subject"))
8217   (when (not (equal "" subject))
8218     (prog1
8219         (let ((articles (gnus-summary-find-matching
8220                          (or header "subject") subject 'all nil nil
8221                          not-matching)))
8222           (unless articles
8223             (error "Found no matches for \"%s\"" subject))
8224           (gnus-summary-limit articles))
8225       (gnus-summary-position-point))))
8226
8227 (defun gnus-summary-limit-to-author (from &optional not-matching)
8228   "Limit the summary buffer to articles that have authors that match a regexp.
8229 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8230   (interactive
8231    (list (read-string (if current-prefix-arg
8232                           "Exclude author (regexp): "
8233                         "Limit to author (regexp): "))
8234          current-prefix-arg))
8235   (gnus-summary-limit-to-subject from "from" not-matching))
8236
8237 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8238   "Limit the summary buffer to articles with the given RECIPIENT.
8239
8240 If NOT-MATCHING, exclude RECIPIENT.
8241
8242 To and Cc headers are checked.  You need to include them in
8243 `nnmail-extra-headers'."
8244   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8245   (interactive
8246    (list (read-string (format "%s recipient (regexp): "
8247                               (if current-prefix-arg "Exclude" "Limit to")))
8248          current-prefix-arg))
8249   (when (not (equal "" recipient))
8250     (prog1 (let* ((to
8251                    (if (memq 'To nnmail-extra-headers)
8252                        (gnus-summary-find-matching
8253                         (cons 'extra 'To) recipient 'all nil nil
8254                         not-matching)
8255                      (gnus-message
8256                       1 "`To' isn't present in `nnmail-extra-headers'")
8257                      (sit-for 1)
8258                      nil))
8259                   (cc
8260                    (if (memq 'Cc nnmail-extra-headers)
8261                        (gnus-summary-find-matching
8262                         (cons 'extra 'Cc) recipient 'all nil nil
8263                         not-matching)
8264                      (gnus-message
8265                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8266                      (sit-for 1)
8267                      nil))
8268                   (articles
8269                    (if not-matching
8270                        ;; We need the numbers that are in both lists:
8271                        (mapcar (lambda (a)
8272                                  (and (memq a to) a))
8273                                cc)
8274                      (nconc to cc))))
8275              (unless articles
8276                (error "Found no matches for \"%s\"" recipient))
8277              (gnus-summary-limit articles))
8278       (gnus-summary-position-point))))
8279
8280 (defun gnus-summary-limit-to-address (address &optional not-matching)
8281   "Limit the summary buffer to articles with the given ADDRESS.
8282
8283 If NOT-MATCHING, exclude ADDRESS.
8284
8285 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8286 in `nnmail-extra-headers'."
8287   (interactive
8288    (list (read-string (format "%s address (regexp): "
8289                               (if current-prefix-arg "Exclude" "Limit to")))
8290          current-prefix-arg))
8291   (when (not (equal "" address))
8292     (prog1 (let* ((to
8293                    (if (memq 'To nnmail-extra-headers)
8294                        (gnus-summary-find-matching
8295                         (cons 'extra 'To) address 'all nil nil
8296                         not-matching)
8297                      (gnus-message
8298                       1 "`To' isn't present in `nnmail-extra-headers'")
8299                      (sit-for 1)
8300                      t))
8301                   (cc
8302                    (if (memq 'Cc nnmail-extra-headers)
8303                        (gnus-summary-find-matching
8304                         (cons 'extra 'Cc) address 'all nil nil
8305                         not-matching)
8306                      (gnus-message
8307                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8308                      (sit-for 1)
8309                      t))
8310                   (from
8311                    (gnus-summary-find-matching "from" address
8312                                                'all nil nil not-matching))
8313                   (articles
8314                    (if not-matching
8315                        ;; We need the numbers that are in all lists:
8316                        (if (eq cc t)
8317                            (if (eq to t)
8318                                from
8319                              (mapcar (lambda (a) (car (memq a from))) to))
8320                          (if (eq to t)
8321                              (mapcar (lambda (a) (car (memq a from))) cc)
8322                            (mapcar (lambda (a) (car (memq a from)))
8323                                    (mapcar (lambda (a) (car (memq a to)))
8324                                            cc))))
8325                      (nconc (if (eq to t) nil to)
8326                             (if (eq cc t) nil cc)
8327                             from))))
8328              (unless articles
8329                (error "Found no matches for \"%s\"" address))
8330              (gnus-summary-limit articles))
8331       (gnus-summary-position-point))))
8332
8333 (defun gnus-summary-limit-strange-charsets-predicate (header)
8334   (when (fboundp 'char-charset)
8335     (let ((string (concat (mail-header-subject header)
8336                           (mail-header-from header)))
8337           charset found)
8338       (dotimes (i (1- (length string)))
8339         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8340         (when (string-match "unicode\\|big\\|japanese" charset)
8341           (setq found t)))
8342       found)))
8343
8344 (defun gnus-summary-limit-to-predicate (predicate)
8345   "Limit to articles where PREDICATE returns non-nil.
8346 PREDICATE will be called with the header structures of the
8347 articles."
8348   (let ((articles nil)
8349         (case-fold-search t))
8350     (dolist (header gnus-newsgroup-headers)
8351       (when (funcall predicate header)
8352         (push (mail-header-number header) articles)))
8353     (gnus-summary-limit (nreverse articles))))
8354
8355 (defun gnus-summary-limit-to-age (age &optional younger-p)
8356   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8357 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8358 articles that are younger than AGE days."
8359   (interactive
8360    (let ((younger current-prefix-arg)
8361          (days-got nil)
8362          days)
8363      (while (not days-got)
8364        (setq days (if younger
8365                       (read-string "Limit to articles younger than (in days, older when negative): ")
8366                     (read-string
8367                      "Limit to articles older than (in days, younger when negative): ")))
8368        (when (> (length days) 0)
8369          (setq days (read days)))
8370        (if (numberp days)
8371            (progn
8372              (setq days-got t)
8373              (when (< days 0)
8374                (setq younger (not younger))
8375                (setq days (* days -1))))
8376          (message "Please enter a number.")
8377          (sleep-for 1)))
8378      (list days younger)))
8379   (prog1
8380       (let ((data gnus-newsgroup-data)
8381             (cutoff (days-to-time age))
8382             articles d date is-younger)
8383         (while (setq d (pop data))
8384           (when (and (vectorp (gnus-data-header d))
8385                      (setq date (mail-header-date (gnus-data-header d))))
8386             (setq is-younger (time-less-p
8387                               (time-since (gnus-date-get-time date))
8388                               cutoff))
8389             (when (if younger-p
8390                       is-younger
8391                     (not is-younger))
8392               (push (gnus-data-number d) articles))))
8393         (gnus-summary-limit (nreverse articles)))
8394     (gnus-summary-position-point)))
8395
8396 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8397   "Limit the summary buffer to articles that match an 'extra' header."
8398   (interactive
8399    (let ((header
8400           (intern
8401            (gnus-completing-read
8402             (if current-prefix-arg
8403                 "Exclude extra header"
8404               "Limit extra header")
8405             (mapcar 'symbol-name gnus-extra-headers)
8406             t nil nil
8407             (symbol-name (car gnus-extra-headers))))))
8408      (list header
8409            (read-string (format "%s header %s (regexp): "
8410                                 (if current-prefix-arg "Exclude" "Limit to")
8411                                 header))
8412            current-prefix-arg)))
8413   (when (not (equal "" regexp))
8414     (prog1
8415         (let ((articles (gnus-summary-find-matching
8416                          (cons 'extra header) regexp 'all nil nil
8417                          not-matching)))
8418           (unless articles
8419             (error "Found no matches for \"%s\"" regexp))
8420           (gnus-summary-limit articles))
8421       (gnus-summary-position-point))))
8422
8423 (defun gnus-summary-limit-to-display-predicate ()
8424   "Limit the summary buffer to the predicated in the `display' group parameter."
8425   (interactive)
8426   (unless gnus-newsgroup-display
8427     (error "There is no `display' group parameter"))
8428   (let (articles)
8429     (dolist (gnus-number gnus-newsgroup-articles)
8430       (when (funcall gnus-newsgroup-display)
8431         (push gnus-number articles)))
8432     (gnus-summary-limit articles))
8433   (gnus-summary-position-point))
8434
8435 (defun gnus-summary-limit-to-unread (&optional all)
8436   "Limit the summary buffer to articles that are not marked as read.
8437 If ALL is non-nil, limit strictly to unread articles."
8438   (interactive "P")
8439   (if all
8440       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8441     (gnus-summary-limit-to-marks
8442      ;; Concat all the marks that say that an article is read and have
8443      ;; those removed.
8444      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8445            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8446            gnus-low-score-mark gnus-expirable-mark
8447            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8448            gnus-duplicate-mark)
8449      'reverse)))
8450
8451 (defun gnus-summary-limit-to-headers (match &optional reverse)
8452   "Limit the summary buffer to articles that have headers that match MATCH.
8453 If REVERSE (the prefix), limit to articles that don't match."
8454   (interactive "sMatch headers (regexp): \nP")
8455   (gnus-summary-limit-to-bodies match reverse t))
8456
8457 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8458   "Limit the summary buffer to articles that have bodies that match MATCH.
8459 If REVERSE (the prefix), limit to articles that don't match."
8460   (interactive "sMatch body (regexp): \nP")
8461   (let ((articles nil)
8462         (gnus-select-article-hook nil)  ;Disable hook.
8463         (gnus-article-prepare-hook nil)
8464         (gnus-use-article-prefetch nil)
8465         (gnus-keep-backlog nil)
8466         (gnus-break-pages nil)
8467         (gnus-summary-display-arrow nil)
8468         (gnus-updated-mode-lines nil)
8469         (gnus-auto-center-summary nil)
8470         (gnus-display-mime-function nil))
8471     (dolist (data gnus-newsgroup-data)
8472       (let (gnus-mark-article-hook)
8473         (gnus-summary-select-article t t nil (gnus-data-number data)))
8474       (with-current-buffer gnus-article-buffer
8475         (article-goto-body)
8476         (let* ((case-fold-search t)
8477                (found (if headersp
8478                           (re-search-backward match nil t)
8479                         (re-search-forward match nil t))))
8480           (when (or (and found
8481                          (not reverse))
8482                     (and (not found)
8483                          reverse))
8484             (push (gnus-data-number data) articles)))))
8485     (if (not articles)
8486         (message "No messages matched")
8487       (gnus-summary-limit articles)))
8488   (gnus-summary-position-point))
8489
8490 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8491   "Limit the summary buffer to articles that aren't part on any thread.
8492 If THREADSP (the prefix), limit to articles that are in threads."
8493   (interactive "P")
8494   (let ((articles nil)
8495         thread-articles
8496         threads)
8497     (dolist (thread gnus-newsgroup-threads)
8498       (if (stringp (car thread))
8499           (dolist (thread (cdr thread))
8500             (push thread threads))
8501         (push thread threads)))
8502     (dolist (thread threads)
8503       (setq thread-articles (gnus-articles-in-thread thread))
8504       (when (or (and threadsp
8505                      (> (length thread-articles) 1))
8506                 (and (not threadsp)
8507                      (= (length thread-articles) 1)))
8508         (setq articles (nconc thread-articles articles))))
8509     (if (not articles)
8510         (message "No messages matched")
8511       (gnus-summary-limit articles))
8512     (gnus-summary-position-point)))
8513
8514 (defun gnus-summary-limit-to-replied (&optional unreplied)
8515   "Limit the summary buffer to replied articles.
8516 If UNREPLIED (the prefix), limit to unreplied articles."
8517   (interactive "P")
8518   (if unreplied
8519       (gnus-summary-limit
8520        (gnus-set-difference gnus-newsgroup-articles
8521         gnus-newsgroup-replied))
8522     (gnus-summary-limit gnus-newsgroup-replied))
8523   (gnus-summary-position-point))
8524
8525 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8526   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8527 If REVERSE, limit the summary buffer to articles that are marked
8528 with MARKS.  MARKS can either be a string of marks or a list of marks.
8529 Returns how many articles were removed."
8530   (interactive "sMarks: ")
8531   (gnus-summary-limit-to-marks marks t))
8532
8533 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8534   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8535 If REVERSE (the prefix), limit the summary buffer to articles that are
8536 not marked with MARKS.  MARKS can either be a string of marks or a
8537 list of marks.
8538 Returns how many articles were removed."
8539   (interactive "sMarks: \nP")
8540   (prog1
8541       (let ((data gnus-newsgroup-data)
8542             (marks (if (listp marks) marks
8543                      (append marks nil))) ; Transform to list.
8544             articles)
8545         (while data
8546           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8547                   (memq (gnus-data-mark (car data)) marks))
8548             (push (gnus-data-number (car data)) articles))
8549           (setq data (cdr data)))
8550         (gnus-summary-limit articles))
8551     (gnus-summary-position-point)))
8552
8553 (defun gnus-summary-limit-to-score (score)
8554   "Limit to articles with score at or above SCORE."
8555   (interactive "NLimit to articles with score of at least: ")
8556   (let ((data gnus-newsgroup-data)
8557         articles)
8558     (while data
8559       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8560                 score)
8561         (push (gnus-data-number (car data)) articles))
8562       (setq data (cdr data)))
8563     (prog1
8564         (gnus-summary-limit articles)
8565       (gnus-summary-position-point))))
8566
8567 (defun gnus-summary-limit-to-unseen ()
8568   "Limit to unseen articles."
8569   (interactive)
8570   (prog1
8571       (gnus-summary-limit gnus-newsgroup-unseen)
8572     (gnus-summary-position-point)))
8573
8574 (defun gnus-summary-limit-include-thread (id)
8575   "Display all the hidden articles that is in the thread with ID in it.
8576 When called interactively, ID is the Message-ID of the current
8577 article."
8578   (interactive (list (mail-header-id (gnus-summary-article-header))))
8579   (let ((articles (gnus-articles-in-thread
8580                    (gnus-id-to-thread (gnus-root-id id))))
8581         ;;we REALLY want the whole thread---this prevents cut-threads
8582         ;;from removing the thread we want to include.
8583         (gnus-fetch-old-headers nil)
8584         (gnus-build-sparse-threads nil))
8585     (prog1
8586         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8587       (gnus-summary-limit-include-matching-articles
8588        "subject"
8589        (regexp-quote (gnus-simplify-subject-re
8590                       (mail-header-subject (gnus-id-to-header id)))))
8591       (gnus-summary-position-point))))
8592
8593 (defun gnus-summary-limit-include-matching-articles (header regexp)
8594   "Display all the hidden articles that have HEADERs that match REGEXP."
8595   (interactive (list (read-string "Match on header: ")
8596                      (read-string "Regexp: ")))
8597   (let ((articles (gnus-find-matching-articles header regexp)))
8598     (prog1
8599         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8600       (gnus-summary-position-point))))
8601
8602 (defun gnus-summary-insert-dormant-articles ()
8603   "Insert all the dormant articles for this group into the current buffer."
8604   (interactive)
8605   (let ((gnus-verbose (max 6 gnus-verbose)))
8606     (if (not gnus-newsgroup-dormant)
8607         (gnus-message 3 "No dormant articles for this group")
8608       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8609
8610 (defun gnus-summary-insert-ticked-articles ()
8611   "Insert ticked articles for this group into the current buffer."
8612   (interactive)
8613   (let ((gnus-verbose (max 6 gnus-verbose)))
8614     (if (not gnus-newsgroup-marked)
8615         (gnus-message 3 "No ticked articles for this group")
8616       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8617
8618 (defun gnus-summary-limit-include-dormant ()
8619   "Display all the hidden articles that are marked as dormant.
8620 Note that this command only works on a subset of the articles currently
8621 fetched for this group."
8622   (interactive)
8623   (unless gnus-newsgroup-dormant
8624     (error "There are no dormant articles in this group"))
8625   (prog1
8626       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8627     (gnus-summary-position-point)))
8628
8629 (defun gnus-summary-include-articles (articles)
8630   "Fetch the headers for ARTICLES and then display the summary lines."
8631   (let ((gnus-inhibit-demon t)
8632         (gnus-agent nil)
8633         (gnus-read-all-available-headers t))
8634     (setq gnus-newsgroup-headers
8635           (gnus-merge
8636            'list gnus-newsgroup-headers
8637            (gnus-fetch-headers articles nil t)
8638            'gnus-article-sort-by-number))
8639     (setq gnus-newsgroup-articles
8640           (gnus-sorted-nunion gnus-newsgroup-articles articles))
8641     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8642
8643 (defun gnus-summary-limit-exclude-dormant ()
8644   "Hide all dormant articles."
8645   (interactive)
8646   (prog1
8647       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8648     (gnus-summary-position-point)))
8649
8650 (defun gnus-summary-limit-exclude-childless-dormant ()
8651   "Hide all dormant articles that have no children."
8652   (interactive)
8653   (let ((data (gnus-data-list t))
8654         articles d children)
8655     ;; Find all articles that are either not dormant or have
8656     ;; children.
8657     (while (setq d (pop data))
8658       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8659                 (and (setq children
8660                            (gnus-article-children (gnus-data-number d)))
8661                      (let (found)
8662                        (while children
8663                          (when (memq (car children) articles)
8664                            (setq children nil
8665                                  found t))
8666                          (pop children))
8667                        found)))
8668         (push (gnus-data-number d) articles)))
8669     ;; Do the limiting.
8670     (prog1
8671         (gnus-summary-limit articles)
8672       (gnus-summary-position-point))))
8673
8674 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8675   "Mark all unread excluded articles as read.
8676 If ALL, mark even excluded ticked and dormants as read."
8677   (interactive "P")
8678   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8679   (let ((articles (gnus-sorted-ndifference
8680                    (sort
8681                     (mapcar (lambda (h) (mail-header-number h))
8682                             gnus-newsgroup-headers)
8683                     '<)
8684                    gnus-newsgroup-limit))
8685         article)
8686     (setq gnus-newsgroup-unreads
8687           (gnus-sorted-intersection gnus-newsgroup-unreads
8688                                     gnus-newsgroup-limit))
8689     (if all
8690         (setq gnus-newsgroup-dormant nil
8691               gnus-newsgroup-marked nil
8692               gnus-newsgroup-reads
8693               (nconc
8694                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8695                gnus-newsgroup-reads))
8696       (while (setq article (pop articles))
8697         (unless (or (memq article gnus-newsgroup-dormant)
8698                     (memq article gnus-newsgroup-marked))
8699           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8700
8701 (defun gnus-summary-limit (articles &optional pop)
8702   (if pop
8703       ;; We pop the previous limit off the stack and use that.
8704       (setq articles (car gnus-newsgroup-limits)
8705             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8706     ;; We use the new limit, so we push the old limit on the stack.
8707     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8708   ;; Set the limit.
8709   (setq gnus-newsgroup-limit articles)
8710   (let ((total (length gnus-newsgroup-data))
8711         (data (gnus-data-find-list (gnus-summary-article-number)))
8712         (gnus-summary-mark-below nil)   ; Inhibit this.
8713         found)
8714     ;; This will do all the work of generating the new summary buffer
8715     ;; according to the new limit.
8716     (gnus-summary-prepare)
8717     ;; Hide any threads, possibly.
8718     (gnus-summary-maybe-hide-threads)
8719     ;; Try to return to the article you were at, or one in the
8720     ;; neighborhood.
8721     (when data
8722       ;; We try to find some article after the current one.
8723       (while data
8724         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8725           (setq data nil
8726                 found t))
8727         (setq data (cdr data))))
8728     (unless found
8729       ;; If there is no data, that means that we were after the last
8730       ;; article.  The same goes when we can't find any articles
8731       ;; after the current one.
8732       (goto-char (point-max))
8733       (gnus-summary-find-prev))
8734     (gnus-set-mode-line 'summary)
8735     ;; We return how many articles were removed from the summary
8736     ;; buffer as a result of the new limit.
8737     (- total (length gnus-newsgroup-data))))
8738
8739 (defsubst gnus-invisible-cut-children (threads)
8740   (let ((num 0))
8741     (while threads
8742       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8743         (incf num))
8744       (pop threads))
8745     (< num 2)))
8746
8747 (defsubst gnus-cut-thread (thread)
8748   "Go forwards in the thread until we find an article that we want to display."
8749   (when (or (eq gnus-fetch-old-headers 'some)
8750             (eq gnus-fetch-old-headers 'invisible)
8751             (numberp gnus-fetch-old-headers)
8752             (eq gnus-build-sparse-threads 'some)
8753             (eq gnus-build-sparse-threads 'more))
8754     ;; Deal with old-fetched headers and sparse threads.
8755     (while (and
8756             thread
8757             (or
8758              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8759              (gnus-summary-article-ancient-p
8760               (mail-header-number (car thread))))
8761             (if (or (<= (length (cdr thread)) 1)
8762                     (eq gnus-fetch-old-headers 'invisible))
8763                 (setq gnus-newsgroup-limit
8764                       (delq (mail-header-number (car thread))
8765                             gnus-newsgroup-limit)
8766                       thread (cadr thread))
8767               (when (gnus-invisible-cut-children (cdr thread))
8768                 (let ((th (cdr thread)))
8769                   (while th
8770                     (if (memq (mail-header-number (caar th))
8771                               gnus-newsgroup-limit)
8772                         (setq thread (car th)
8773                               th nil)
8774                       (setq th (cdr th))))))))))
8775   thread)
8776
8777 (defun gnus-cut-threads (threads)
8778   "Cut off all uninteresting articles from the beginning of THREADS."
8779   (when (or (eq gnus-fetch-old-headers 'some)
8780             (eq gnus-fetch-old-headers 'invisible)
8781             (numberp gnus-fetch-old-headers)
8782             (eq gnus-build-sparse-threads 'some)
8783             (eq gnus-build-sparse-threads 'more))
8784     (let ((th threads))
8785       (while th
8786         (setcar th (gnus-cut-thread (car th)))
8787         (setq th (cdr th)))))
8788   ;; Remove nixed out threads.
8789   (delq nil threads))
8790
8791 (defun gnus-summary-initial-limit (&optional show-if-empty)
8792   "Figure out what the initial limit is supposed to be on group entry.
8793 This entails weeding out unwanted dormants, low-scored articles,
8794 fetch-old-headers verbiage, and so on."
8795   ;; Most groups have nothing to remove.
8796   (unless (or gnus-inhibit-limiting
8797               (and (null gnus-newsgroup-dormant)
8798                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8799                    (not (eq gnus-fetch-old-headers 'some))
8800                    (not (numberp gnus-fetch-old-headers))
8801                    (not (eq gnus-fetch-old-headers 'invisible))
8802                    (null gnus-summary-expunge-below)
8803                    (not (eq gnus-build-sparse-threads 'some))
8804                    (not (eq gnus-build-sparse-threads 'more))
8805                    (null gnus-thread-expunge-below)))
8806     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8807     (setq gnus-newsgroup-limit nil)
8808     (mapatoms
8809      (lambda (node)
8810        (unless (car (symbol-value node))
8811          ;; These threads have no parents -- they are roots.
8812          (let ((nodes (cdr (symbol-value node)))
8813                thread)
8814            (while nodes
8815              (if (and gnus-thread-expunge-below
8816                       (< (gnus-thread-total-score (car nodes))
8817                          gnus-thread-expunge-below))
8818                  (gnus-expunge-thread (pop nodes))
8819                (setq thread (pop nodes))
8820                (gnus-summary-limit-children thread))))))
8821      gnus-newsgroup-dependencies)
8822     ;; If this limitation resulted in an empty group, we might
8823     ;; pop the previous limit and use it instead.
8824     (when (and (not gnus-newsgroup-limit)
8825                show-if-empty)
8826       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8827     gnus-newsgroup-limit))
8828
8829 (defun gnus-summary-limit-children (thread)
8830   "Return 1 if this subthread is visible and 0 if it is not."
8831   ;; First we get the number of visible children to this thread.  This
8832   ;; is done by recursing down the thread using this function, so this
8833   ;; will really go down to a leaf article first, before slowly
8834   ;; working its way up towards the root.
8835   (when thread
8836     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8837            (children
8838            (if (cdr thread)
8839                (apply '+ (mapcar 'gnus-summary-limit-children
8840                                  (cdr thread)))
8841              0))
8842            (number (mail-header-number (car thread)))
8843            score)
8844       (if (and
8845            (not (memq number gnus-newsgroup-marked))
8846            (or
8847             ;; If this article is dormant and has absolutely no visible
8848             ;; children, then this article isn't visible.
8849             (and (memq number gnus-newsgroup-dormant)
8850                  (zerop children))
8851             ;; If this is "fetch-old-headered" and there is no
8852             ;; visible children, then we don't want this article.
8853             (and (or (eq gnus-fetch-old-headers 'some)
8854                      (numberp gnus-fetch-old-headers))
8855                  (gnus-summary-article-ancient-p number)
8856                  (zerop children))
8857             ;; If this is "fetch-old-headered" and `invisible', then
8858             ;; we don't want this article.
8859             (and (eq gnus-fetch-old-headers 'invisible)
8860                  (gnus-summary-article-ancient-p number))
8861             ;; If this is a sparsely inserted article with no children,
8862             ;; we don't want it.
8863             (and (eq gnus-build-sparse-threads 'some)
8864                  (gnus-summary-article-sparse-p number)
8865                  (zerop children))
8866             ;; If we use expunging, and this article is really
8867             ;; low-scored, then we don't want this article.
8868             (when (and gnus-summary-expunge-below
8869                        (< (setq score
8870                                 (or (cdr (assq number gnus-newsgroup-scored))
8871                                     gnus-summary-default-score))
8872                           gnus-summary-expunge-below))
8873               ;; We increase the expunge-tally here, but that has
8874               ;; nothing to do with the limits, really.
8875               (incf gnus-newsgroup-expunged-tally)
8876               ;; We also mark as read here, if that's wanted.
8877               (when (and gnus-summary-mark-below
8878                          (< score gnus-summary-mark-below))
8879                 (setq gnus-newsgroup-unreads
8880                       (delq number gnus-newsgroup-unreads))
8881                 (if gnus-newsgroup-auto-expire
8882                     (push number gnus-newsgroup-expirable)
8883                   (push (cons number gnus-low-score-mark)
8884                         gnus-newsgroup-reads)))
8885               t)
8886             ;; Do the `display' group parameter.
8887             (and gnus-newsgroup-display
8888                  (let ((gnus-number number))
8889                    (not (funcall gnus-newsgroup-display))))))
8890           ;; Nope, invisible article.
8891           0
8892         ;; Ok, this article is to be visible, so we add it to the limit
8893         ;; and return 1.
8894         (push number gnus-newsgroup-limit)
8895         1))))
8896
8897 (defun gnus-expunge-thread (thread)
8898   "Mark all articles in THREAD as read."
8899   (let* ((number (mail-header-number (car thread))))
8900     (incf gnus-newsgroup-expunged-tally)
8901     ;; We also mark as read here, if that's wanted.
8902     (setq gnus-newsgroup-unreads
8903           (delq number gnus-newsgroup-unreads))
8904     (if gnus-newsgroup-auto-expire
8905         (push number gnus-newsgroup-expirable)
8906       (push (cons number gnus-low-score-mark)
8907             gnus-newsgroup-reads)))
8908   ;; Go recursively through all subthreads.
8909   (mapcar 'gnus-expunge-thread (cdr thread)))
8910
8911 ;; Summary article oriented commands
8912
8913 (defun gnus-summary-refer-parent-article (n)
8914   "Refer parent article N times.
8915 If N is negative, go to ancestor -N instead.
8916 The difference between N and the number of articles fetched is returned."
8917   (interactive "p")
8918   (let ((skip 1)
8919         error header ref)
8920     (when (not (natnump n))
8921       (setq skip (abs n)
8922             n 1))
8923     (while (and (> n 0)
8924                 (not error))
8925       (setq header (gnus-summary-article-header))
8926       (if (and (eq (mail-header-number header)
8927                    (cdr gnus-article-current))
8928                (equal gnus-newsgroup-name
8929                       (car gnus-article-current)))
8930           ;; If we try to find the parent of the currently
8931           ;; displayed article, then we take a look at the actual
8932           ;; References header, since this is slightly more
8933           ;; reliable than the References field we got from the
8934           ;; server.
8935           (with-current-buffer gnus-original-article-buffer
8936             (nnheader-narrow-to-headers)
8937             (unless (setq ref (message-fetch-field "references"))
8938               (when (setq ref (message-fetch-field "in-reply-to"))
8939                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8940             (widen))
8941         (setq ref
8942               ;; It's not the current article, so we take a bet on
8943               ;; the value we got from the server.
8944               (mail-header-references header)))
8945       (if (and ref
8946                (not (equal ref "")))
8947           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8948             (gnus-message 1 "Couldn't find parent"))
8949         (gnus-message 1 "No references in article %d"
8950                       (gnus-summary-article-number))
8951         (setq error t))
8952       (decf n))
8953     (gnus-summary-position-point)
8954     n))
8955
8956 (defun gnus-summary-refer-references ()
8957   "Fetch all articles mentioned in the References header.
8958 Return the number of articles fetched."
8959   (interactive)
8960   (let ((ref (mail-header-references (gnus-summary-article-header)))
8961         (current (gnus-summary-article-number))
8962         (n 0))
8963     (if (or (not ref)
8964             (equal ref ""))
8965         (error "No References in the current article")
8966       ;; For each Message-ID in the References header...
8967       (while (string-match "<[^>]*>" ref)
8968         (incf n)
8969         ;; ... fetch that article.
8970         (gnus-summary-refer-article
8971          (prog1 (match-string 0 ref)
8972            (setq ref (substring ref (match-end 0))))))
8973       (gnus-summary-goto-subject current)
8974       (gnus-summary-position-point)
8975       n)))
8976
8977 (defun gnus-delete-duplicate-headers (headers)
8978   ;; First remove leading duplicates.
8979   (while (and (> (length headers) 1)
8980               (= (mail-header-number (car headers))
8981                  (mail-header-number (cadr headers))))
8982     (pop headers))
8983   ;; Then the rest.
8984   (let ((result headers))
8985     (while (> (length headers) 1)
8986       (if (= (mail-header-number (car headers))
8987              (mail-header-number (cadr headers)))
8988           (setcdr headers (cddr headers))
8989         (pop headers)))
8990     result))
8991
8992 (defun gnus-summary-refer-thread (&optional limit)
8993   "Fetch all articles in the current thread. For backends that
8994 know how to search for threads (currently only 'nnimap) a
8995 non-numeric prefix arg will use nnir to search the entire
8996 server; without a prefix arg only the current group is
8997 searched. If the variable `gnus-refer-thread-use-nnir' is
8998 non-nil the prefix arg has the reverse meaning. If no
8999 backend-specific 'request-thread function is available fetch
9000 LIMIT (the numerical prefix) old headers. If LIMIT is
9001 non-numeric or nil fetch the number specified by the
9002 `gnus-refer-thread-limit' variable."
9003   (interactive "P")
9004   (gnus-warp-to-article)
9005   (let* ((header (gnus-summary-article-header))
9006          (id (mail-header-id header))
9007          (gnus-inhibit-demon t)
9008          (gnus-summary-ignore-duplicates t)
9009          (gnus-read-all-available-headers t)
9010          (gnus-refer-thread-use-nnir
9011           (if (and (not (null limit)) (listp limit))
9012               (not gnus-refer-thread-use-nnir) gnus-refer-thread-use-nnir))
9013          (new-headers
9014           (if (gnus-check-backend-function
9015                'request-thread gnus-newsgroup-name)
9016               (gnus-request-thread header gnus-newsgroup-name)
9017             (let* ((limit (if (numberp limit) (prefix-numeric-value limit)
9018                             gnus-refer-thread-limit))
9019                    (last (if (numberp limit)
9020                              (min (+ (mail-header-number header)
9021                                      limit)
9022                                   gnus-newsgroup-highest)
9023                            gnus-newsgroup-highest))
9024                    (subject (gnus-simplify-subject
9025                              (mail-header-subject header)))
9026                    (refs (split-string (or (mail-header-references header)
9027                                            "")))
9028                    (gnus-parse-headers-hook
9029                     `(lambda () (goto-char (point-min))
9030                       (keep-lines
9031                        (regexp-opt ',(append refs (list id subject)))))))
9032               (gnus-fetch-headers (list last) (if (numberp limit)
9033                                                   (* 2 limit) limit) t))))
9034          article-ids)
9035     (when (listp new-headers)
9036       (dolist (header new-headers)
9037         (push (mail-header-number header) article-ids)
9038         (when (member (mail-header-number header) gnus-newsgroup-unselected)
9039           (push (mail-header-number header) gnus-newsgroup-unreads)
9040           (setq gnus-newsgroup-unselected
9041                 (delete (mail-header-number header)
9042                         gnus-newsgroup-unselected))))
9043       (setq gnus-newsgroup-headers
9044             (gnus-delete-duplicate-headers
9045              (gnus-merge
9046               'list gnus-newsgroup-headers new-headers
9047               'gnus-article-sort-by-number)))
9048       (setq gnus-newsgroup-articles
9049             (gnus-sorted-nunion gnus-newsgroup-articles (nreverse article-ids)))
9050       (gnus-summary-limit-include-thread id)))
9051   (gnus-summary-show-thread))
9052
9053 (defun gnus-summary-refer-article (message-id)
9054   "Fetch an article specified by MESSAGE-ID."
9055   (interactive "sMessage-ID: ")
9056   (gnus-warp-to-article)
9057   (when (and (stringp message-id)
9058              (not (zerop (length message-id))))
9059     (setq message-id (gnus-replace-in-string message-id " " ""))
9060     ;; Construct the correct Message-ID if necessary.
9061     ;; Suggested by tale@pawl.rpi.edu.
9062     (unless (string-match "^<" message-id)
9063       (setq message-id (concat "<" message-id)))
9064     (unless (string-match ">$" message-id)
9065       (setq message-id (concat message-id ">")))
9066     ;; People often post MIDs from URLs, so unhex it:
9067     (unless (string-match "@" message-id)
9068       (setq message-id (gnus-url-unhex-string message-id)))
9069     (let* ((header (gnus-id-to-header message-id))
9070            (sparse (and header
9071                         (gnus-summary-article-sparse-p
9072                          (mail-header-number header))
9073                         (memq (mail-header-number header)
9074                               gnus-newsgroup-limit)))
9075            number)
9076       (cond
9077        ;; If the article is present in the buffer we just go to it.
9078        ((and header
9079              (or (not (gnus-summary-article-sparse-p
9080                        (mail-header-number header)))
9081                  sparse))
9082         (prog1
9083             (gnus-summary-goto-article
9084              (mail-header-number header) nil t)
9085           (when sparse
9086             (gnus-summary-update-article (mail-header-number header)))))
9087        (t
9088         ;; We fetch the article.
9089         (catch 'found
9090           (dolist (gnus-override-method (gnus-refer-article-methods))
9091             (when (and (gnus-check-server gnus-override-method)
9092                        ;; Fetch the header,
9093                        (setq number (gnus-summary-insert-subject message-id)))
9094               ;; and display the article.
9095               (gnus-summary-select-article nil nil nil number)
9096               (throw 'found t)))
9097           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
9098
9099 (defun gnus-refer-article-methods ()
9100   "Return a list of referable methods."
9101   (cond
9102    ;; No method, so we default to current and native.
9103    ((null gnus-refer-article-method)
9104     (list gnus-current-select-method gnus-select-method))
9105    ;; Current.
9106    ((eq 'current gnus-refer-article-method)
9107     (list gnus-current-select-method))
9108    ;; List of select methods.
9109    ((not (and (symbolp (car gnus-refer-article-method))
9110               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9111     (let (out)
9112       (dolist (method gnus-refer-article-method)
9113         (push (if (eq 'current method)
9114                   gnus-current-select-method
9115                 (if (eq 'nnir (car method))
9116                     (list
9117                      'nnir
9118                      (or (cadr method)
9119                          (gnus-method-to-server gnus-current-select-method)))
9120                   method))
9121               out))
9122       (nreverse out)))
9123    ;; One single select method.
9124    (t
9125     (list gnus-refer-article-method))))
9126
9127 (defun gnus-summary-edit-parameters ()
9128   "Edit the group parameters of the current group."
9129   (interactive)
9130   (gnus-group-edit-group gnus-newsgroup-name 'params))
9131
9132 (defun gnus-summary-customize-parameters ()
9133   "Customize the group parameters of the current group."
9134   (interactive)
9135   (gnus-group-customize gnus-newsgroup-name))
9136
9137 (defun gnus-summary-enter-digest-group (&optional force)
9138   "Enter an nndoc group based on the current article.
9139 If FORCE, force a digest interpretation.  If not, try to guess
9140 what the document format is.
9141
9142 To control what happens when you exit the group, see the
9143 `gnus-auto-select-on-ephemeral-exit' variable."
9144   (interactive "P")
9145   (let ((conf gnus-current-window-configuration))
9146     (save-window-excursion
9147       (save-excursion
9148         (let (gnus-article-prepare-hook
9149               gnus-display-mime-function
9150               gnus-break-pages)
9151           (gnus-summary-select-article))))
9152     (setq gnus-current-window-configuration conf)
9153     (let* ((name (format "%s-%d"
9154                          (gnus-group-prefixed-name
9155                           gnus-newsgroup-name (list 'nndoc ""))
9156                          (with-current-buffer gnus-summary-buffer
9157                            gnus-current-article)))
9158            (ogroup gnus-newsgroup-name)
9159            (params (append (gnus-info-params (gnus-get-info ogroup))
9160                            (list (cons 'to-group ogroup))
9161                            (list (cons 'parent-group ogroup))
9162                            (list (cons 'save-article-group ogroup))))
9163            (case-fold-search t)
9164            (buf (current-buffer))
9165            dig to-address)
9166       (with-current-buffer gnus-original-article-buffer
9167         ;; Have the digest group inherit the main mail address of
9168         ;; the parent article.
9169         (when (setq to-address (or (gnus-fetch-field "reply-to")
9170                                    (gnus-fetch-field "from")))
9171           (setq params
9172                 (append
9173                  (list (cons 'to-address
9174                              (funcall gnus-decode-encoded-address-function
9175                                       to-address))))))
9176         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9177         (insert-buffer-substring gnus-original-article-buffer)
9178         ;; Remove lines that may lead nndoc to misinterpret the
9179         ;; document type.
9180         (narrow-to-region
9181          (goto-char (point-min))
9182          (or (search-forward "\n\n" nil t) (point)))
9183         (goto-char (point-min))
9184         (delete-matching-lines "^Path:\\|^From ")
9185         (widen))
9186       (unwind-protect
9187           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
9188                     (gnus-newsgroup-ephemeral-ignored-charsets
9189                      gnus-newsgroup-ignored-charsets))
9190                 (gnus-group-read-ephemeral-group
9191                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9192                               (nndoc-article-type
9193                                ,(if force 'mbox 'guess)))
9194                  t nil nil nil
9195                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9196                                                         "ADAPT")))))
9197               ;; Make all postings to this group go to the parent group.
9198               (nconc (gnus-info-params (gnus-get-info name))
9199                      params)
9200             ;; Couldn't select this doc group.
9201             (switch-to-buffer buf)
9202             (gnus-set-global-variables)
9203             (gnus-configure-windows 'summary)
9204             (gnus-message 3 "Article couldn't be entered?"))
9205         (kill-buffer dig)))))
9206
9207 (defun gnus-summary-read-document (n)
9208   "Open a new group based on the current article(s).
9209 This will allow you to read digests and other similar
9210 documents as newsgroups.
9211 Obeys the standard process/prefix convention."
9212   (interactive "P")
9213   (let* ((ogroup gnus-newsgroup-name)
9214          (params (append (gnus-info-params (gnus-get-info ogroup))
9215                          (list (cons 'to-group ogroup))))
9216          group egroup groups vgroup)
9217     (dolist (article (gnus-summary-work-articles n))
9218       (setq group (format "%s-%d" gnus-newsgroup-name article))
9219       (gnus-summary-remove-process-mark article)
9220       (when (gnus-summary-display-article article)
9221         (save-excursion ;;What for?
9222           (with-temp-buffer
9223             (insert-buffer-substring gnus-original-article-buffer)
9224             ;; Remove some headers that may lead nndoc to make
9225             ;; the wrong guess.
9226             (message-narrow-to-head)
9227             (goto-char (point-min))
9228             (delete-matching-lines "^Path:\\|^From ")
9229             (widen)
9230             (if (setq egroup
9231                       (gnus-group-read-ephemeral-group
9232                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9233                                      (nndoc-article-type guess))
9234                        t nil t))
9235                 (progn
9236                   ;; Make all postings to this group go to the parent group.
9237                   (nconc (gnus-info-params (gnus-get-info egroup))
9238                          params)
9239                   (push egroup groups))
9240               ;; Couldn't select this doc group.
9241               (gnus-error 3 "Article couldn't be entered"))))))
9242     ;; Now we have selected all the documents.
9243     (cond
9244      ((not groups)
9245       (error "None of the articles could be interpreted as documents"))
9246      ((gnus-group-read-ephemeral-group
9247        (setq vgroup (format
9248                      "nnvirtual:%s-%s" gnus-newsgroup-name
9249                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9250        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9251        t
9252        (cons (current-buffer) 'summary)))
9253      (t
9254       (error "Couldn't select virtual nndoc group")))))
9255
9256 (defun gnus-summary-widget-forward (arg)
9257   "Move point to the next field or button in the article.
9258 With optional ARG, move across that many fields."
9259   (interactive "p")
9260   (gnus-summary-select-article)
9261   (gnus-configure-windows 'article)
9262   (select-window (gnus-get-buffer-window gnus-article-buffer))
9263   (widget-forward arg))
9264
9265 (defun gnus-summary-isearch-article (&optional regexp-p)
9266   "Do incremental search forward on the current article.
9267 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9268   (interactive "P")
9269   (gnus-summary-select-article)
9270   (gnus-configure-windows 'article)
9271   (gnus-eval-in-buffer-window gnus-article-buffer
9272     (save-restriction
9273       (widen)
9274       (isearch-forward regexp-p))))
9275
9276 (defun gnus-summary-repeat-search-article-forward ()
9277   "Repeat the previous search forwards."
9278   (interactive)
9279   (unless gnus-last-search-regexp
9280     (error "No previous search"))
9281   (gnus-summary-search-article-forward gnus-last-search-regexp))
9282
9283 (defun gnus-summary-repeat-search-article-backward ()
9284   "Repeat the previous search backwards."
9285   (interactive)
9286   (unless gnus-last-search-regexp
9287     (error "No previous search"))
9288   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9289
9290 (defun gnus-summary-search-article-forward (regexp &optional backward)
9291   "Search for an article containing REGEXP forward.
9292 If BACKWARD, search backward instead."
9293   (interactive
9294    (list (read-string
9295           (format "Search article %s (regexp%s): "
9296                   (if current-prefix-arg "backward" "forward")
9297                   (if gnus-last-search-regexp
9298                       (concat ", default " gnus-last-search-regexp)
9299                     "")))
9300          current-prefix-arg))
9301   (if (string-equal regexp "")
9302       (setq regexp (or gnus-last-search-regexp ""))
9303     (setq gnus-last-search-regexp regexp)
9304     (setq gnus-article-before-search gnus-current-article))
9305   ;; Intentionally set gnus-last-article.
9306   (setq gnus-last-article gnus-article-before-search)
9307   (let ((gnus-last-article gnus-last-article))
9308     (if (gnus-summary-search-article regexp backward)
9309         (gnus-summary-show-thread)
9310       (signal 'search-failed (list regexp)))))
9311
9312 (defun gnus-summary-search-article-backward (regexp)
9313   "Search for an article containing REGEXP backward."
9314   (interactive
9315    (list (read-string
9316           (format "Search article backward (regexp%s): "
9317                   (if gnus-last-search-regexp
9318                       (concat ", default " gnus-last-search-regexp)
9319                     "")))))
9320   (gnus-summary-search-article-forward regexp 'backward))
9321
9322 (defun gnus-summary-search-article (regexp &optional backward)
9323   "Search for an article containing REGEXP.
9324 Optional argument BACKWARD means do search for backward.
9325 `gnus-select-article-hook' is not called during the search."
9326   ;; We have to require this here to make sure that the following
9327   ;; dynamic binding isn't shadowed by autoloading.
9328   (require 'gnus-async)
9329   (require 'gnus-art)
9330   (let ((gnus-select-article-hook nil)  ;Disable hook.
9331         (gnus-article-prepare-hook nil)
9332         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9333         (gnus-use-article-prefetch nil)
9334         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9335         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9336         (gnus-visual nil)
9337         (gnus-keep-backlog nil)
9338         (gnus-break-pages nil)
9339         (gnus-summary-display-arrow nil)
9340         (gnus-updated-mode-lines nil)
9341         (gnus-auto-center-summary nil)
9342         (sum (current-buffer))
9343         (gnus-display-mime-function nil)
9344         (found nil)
9345         point)
9346     (gnus-save-hidden-threads
9347       (gnus-summary-select-article)
9348       (set-buffer gnus-article-buffer)
9349       (goto-char (window-point (get-buffer-window (current-buffer))))
9350       (when backward
9351         (forward-line -1))
9352       (while (not found)
9353         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9354         (if (if backward
9355                 (re-search-backward regexp nil t)
9356               (re-search-forward regexp nil t))
9357             ;; We found the regexp.
9358             (progn
9359               (setq found 'found)
9360               (beginning-of-line)
9361               (set-window-start
9362                (get-buffer-window (current-buffer))
9363                (point))
9364               (forward-line 1)
9365               (set-window-point
9366                (get-buffer-window (current-buffer))
9367                (point))
9368               (set-buffer sum)
9369               (setq point (point)))
9370           ;; We didn't find it, so we go to the next article.
9371           (set-buffer sum)
9372           (setq found 'not)
9373           (while (eq found 'not)
9374             (if (not (if backward (gnus-summary-find-prev)
9375                        (gnus-summary-find-next)))
9376                 ;; No more articles.
9377                 (setq found t)
9378               ;; Select the next article and adjust point.
9379               (unless (gnus-summary-article-sparse-p
9380                        (gnus-summary-article-number))
9381                 (setq found nil)
9382                 (gnus-summary-select-article)
9383                 (set-buffer gnus-article-buffer)
9384                 (widen)
9385                 (goto-char (if backward (point-max) (point-min))))))))
9386       (gnus-message 7 ""))
9387     ;; Return whether we found the regexp.
9388     (when (eq found 'found)
9389       (goto-char point)
9390       (gnus-summary-show-thread)
9391       (gnus-summary-goto-subject gnus-current-article)
9392       (gnus-summary-position-point)
9393       t)))
9394
9395 (defun gnus-find-matching-articles (header regexp)
9396   "Return a list of all articles that match REGEXP on HEADER.
9397 This search includes all articles in the current group that Gnus has
9398 fetched headers for, whether they are displayed or not."
9399   (let ((articles nil)
9400         ;; Can't eta-reduce because it's a macro.
9401         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9402         (case-fold-search t))
9403     (dolist (header gnus-newsgroup-headers)
9404       (when (string-match regexp (funcall func header))
9405         (push (mail-header-number header) articles)))
9406     (nreverse articles)))
9407
9408 (defun gnus-summary-find-matching (header regexp &optional backward unread
9409                                           not-case-fold not-matching)
9410   "Return a list of all articles that match REGEXP on HEADER.
9411 The search stars on the current article and goes forwards unless
9412 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9413 If UNREAD is non-nil, only unread articles will
9414 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9415 in the comparisons. If NOT-MATCHING, return a list of all articles that
9416 not match REGEXP on HEADER."
9417   (let ((case-fold-search (not not-case-fold))
9418         articles d func)
9419     (if (consp header)
9420         (if (eq (car header) 'extra)
9421             (setq func
9422                   `(lambda (h)
9423                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9424                          "")))
9425           (error "%s is an invalid header" header))
9426       (unless (fboundp (intern (concat "mail-header-" header)))
9427         (error "%s is not a valid header" header))
9428       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9429     (dolist (d (if (eq backward 'all)
9430                    gnus-newsgroup-data
9431                  (gnus-data-find-list
9432                   (gnus-summary-article-number)
9433                   (gnus-data-list backward))))
9434       (when (and (or (not unread)       ; We want all articles...
9435                      (gnus-data-unread-p d)) ; Or just unreads.
9436                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9437                  (if not-matching
9438                      (not (string-match
9439                            regexp
9440                            (funcall func (gnus-data-header d))))
9441                    (string-match regexp
9442                                  (funcall func (gnus-data-header d)))))
9443         (push (gnus-data-number d) articles))) ; Success!
9444     (nreverse articles)))
9445
9446 (defun gnus-summary-execute-command (header regexp command &optional backward)
9447   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9448 If HEADER is an empty string (or nil), the match is done on the entire
9449 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9450   (interactive
9451    (list (let ((completion-ignore-case t))
9452            (gnus-completing-read
9453             "Header name"
9454             (mapcar 'symbol-name
9455                     (append
9456                      '(Number Subject From Lines Date
9457                        Message-ID Xref References Body)
9458                      gnus-extra-headers))
9459             'require-match))
9460          (read-string "Regexp: ")
9461          (read-key-sequence "Command: ")
9462          current-prefix-arg))
9463   (when (equal header "Body")
9464     (setq header ""))
9465   ;; Hidden thread subtrees must be searched as well.
9466   (gnus-summary-show-all-threads)
9467   ;; We don't want to change current point nor window configuration.
9468   (save-excursion
9469     (save-window-excursion
9470       (let (gnus-visual
9471             gnus-treat-strip-trailing-blank-lines
9472             gnus-treat-strip-leading-blank-lines
9473             gnus-treat-strip-multiple-blank-lines
9474             gnus-treat-hide-boring-headers
9475             gnus-treat-fold-newsgroups
9476             gnus-article-prepare-hook)
9477         (gnus-message 6 "Executing %s..." (key-description command))
9478         ;; We'd like to execute COMMAND interactively so as to give arguments.
9479         (gnus-execute header regexp
9480                       `(call-interactively ',(key-binding command))
9481                       backward)
9482         (gnus-message 6 "Executing %s...done" (key-description command))))))
9483
9484 (defun gnus-summary-beginning-of-article ()
9485   "Scroll the article back to the beginning."
9486   (interactive)
9487   (gnus-summary-select-article)
9488   (gnus-configure-windows 'article)
9489   (gnus-eval-in-buffer-window gnus-article-buffer
9490     (widen)
9491     (goto-char (point-min))
9492     (when gnus-break-pages
9493       (gnus-narrow-to-page))))
9494
9495 (defun gnus-summary-end-of-article ()
9496   "Scroll to the end of the article."
9497   (interactive)
9498   (gnus-summary-select-article)
9499   (gnus-configure-windows 'article)
9500   (gnus-eval-in-buffer-window gnus-article-buffer
9501     (widen)
9502     (goto-char (point-max))
9503     (recenter -3)
9504     (when gnus-break-pages
9505       (gnus-narrow-to-page))))
9506
9507 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9508   "Truncate to LEN and quote all \"(\"'s in STRING."
9509   (gnus-replace-in-string (if (and len (> (length string) len))
9510                               (substring string 0 len)
9511                             string)
9512                           "[()]" "\\\\\\&"))
9513
9514 (defun gnus-summary-print-article (&optional filename n)
9515   "Generate and print a PostScript image of the process-marked (mail) articles.
9516
9517 If used interactively, print the current article if none are
9518 process-marked.  With prefix arg, prompt the user for the name of the
9519 file to save in.
9520
9521 When used from Lisp, accept two optional args FILENAME and N.  N means
9522 to print the next N articles.  If N is negative, print the N previous
9523 articles.  If N is nil and articles have been marked with the process
9524 mark, print these instead.
9525
9526 If the optional first argument FILENAME is nil, send the image to the
9527 printer.  If FILENAME is a string, save the PostScript image in a file with
9528 that name.  If FILENAME is a number, prompt the user for the name of the file
9529 to save in."
9530   (interactive (list (ps-print-preprint current-prefix-arg)))
9531   (dolist (article (gnus-summary-work-articles n))
9532     (gnus-summary-select-article nil nil 'pseudo article)
9533     (gnus-eval-in-buffer-window gnus-article-buffer
9534       (gnus-print-buffer))
9535     (gnus-summary-remove-process-mark article))
9536   (ps-despool filename))
9537
9538 (defun gnus-print-buffer ()
9539   (let ((ps-left-header
9540          (list
9541           (concat "("
9542                   (gnus-summary-print-truncate-and-quote
9543                    (mail-header-subject gnus-current-headers)
9544                    66) ")")
9545           (concat "("
9546                   (gnus-summary-print-truncate-and-quote
9547                    (mail-header-from gnus-current-headers)
9548                    45) ")")))
9549         (ps-right-header
9550          (list
9551           "/pagenumberstring load"
9552           (concat "("
9553                   (mail-header-date gnus-current-headers) ")"))))
9554     (gnus-run-hooks 'gnus-ps-print-hook)
9555     (save-excursion
9556       (if ps-print-color-p
9557           (ps-spool-buffer-with-faces)
9558         (ps-spool-buffer)))))
9559
9560 (defun gnus-summary-show-complete-article ()
9561   "Show a complete version of the current article.
9562 This is only useful if you're looking at a partial version of the
9563 article currently."
9564   (interactive)
9565   (let ((gnus-keep-backlog nil)
9566         (gnus-use-cache nil)
9567         (gnus-agent nil)
9568         (variable (intern
9569                    (format "%s-fetch-partial-articles"
9570                            (car (gnus-find-method-for-group
9571                                  gnus-newsgroup-name)))
9572                    obarray))
9573         old-val)
9574     (unwind-protect
9575         (progn
9576           (setq old-val (symbol-value variable))
9577           (set variable nil)
9578           (gnus-flush-original-article-buffer)
9579           (gnus-summary-show-article))
9580       (set variable old-val))))
9581
9582 (defun gnus-summary-show-article (&optional arg)
9583   "Force redisplaying of the current article.
9584 If ARG (the prefix) is a number, show the article with the charset
9585 defined in `gnus-summary-show-article-charset-alist', or the charset
9586 input.
9587 If ARG (the prefix) is non-nil and not a number, show the article,
9588 but without running any of the article treatment functions
9589 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9590 C-u g', show the raw article."
9591   (interactive "P")
9592   (cond
9593    ((numberp arg)
9594     (gnus-summary-show-article t)
9595     (let ((gnus-newsgroup-charset
9596            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9597                (mm-read-coding-system
9598                 "View as charset: " ;; actually it is coding system.
9599                 (with-current-buffer gnus-article-buffer
9600                   (mm-detect-coding-region (point) (point-max))))))
9601           (gnus-newsgroup-ignored-charsets 'gnus-all))
9602       (gnus-summary-select-article nil 'force)
9603       (let ((deps gnus-newsgroup-dependencies)
9604             head header lines)
9605         (with-current-buffer gnus-original-article-buffer
9606           (save-restriction
9607             (message-narrow-to-head)
9608             (setq head (buffer-string))
9609             (goto-char (point-min))
9610             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9611               (goto-char (point-max))
9612               (widen)
9613               (setq lines (1- (count-lines (point) (point-max))))))
9614           (with-temp-buffer
9615             (insert (format "211 %d Article retrieved.\n"
9616                             (cdr gnus-article-current)))
9617             (insert head)
9618             (if lines (insert (format "Lines: %d\n" lines)))
9619             (insert ".\n")
9620             (let ((nntp-server-buffer (current-buffer)))
9621               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9622         (gnus-data-set-header
9623          (gnus-data-find (cdr gnus-article-current))
9624          header)
9625         (gnus-summary-update-article-line
9626          (cdr gnus-article-current) header)
9627         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9628           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9629    ((not arg)
9630     ;; Select the article the normal way.
9631     (if (eq mm-text-html-renderer 'shr)
9632         (progn
9633           (require 'shr)
9634           (let ((shr-ignore-cache t))
9635             (gnus-summary-select-article nil 'force)))
9636       (gnus-summary-select-article nil 'force)))
9637    ((equal arg '(16))
9638     ;; C-u C-u g
9639     (let ((gnus-inhibit-article-treatments t))
9640       (gnus-summary-select-article nil 'force)))
9641    (t
9642     ;; We have to require this here to make sure that the following
9643     ;; dynamic binding isn't shadowed by autoloading.
9644     (require 'gnus-async)
9645     (require 'gnus-art)
9646     ;; Bind the article treatment functions to nil.
9647     (let ((gnus-have-all-headers t)
9648           gnus-article-prepare-hook
9649           gnus-article-decode-hook
9650           gnus-display-mime-function
9651           gnus-break-pages)
9652       ;; Destroy any MIME parts.
9653       (when (gnus-buffer-live-p gnus-article-buffer)
9654         (with-current-buffer gnus-article-buffer
9655           (gnus-article-stop-animations)
9656           (mm-destroy-parts gnus-article-mime-handles)
9657           ;; Set it to nil for safety reason.
9658           (setq gnus-article-mime-handle-alist nil)
9659           (setq gnus-article-mime-handles nil)))
9660       (gnus-summary-select-article nil 'force))))
9661   (gnus-summary-goto-subject gnus-current-article)
9662   (gnus-summary-position-point))
9663
9664 (defun gnus-summary-show-raw-article ()
9665   "Show the raw article without any article massaging functions being run."
9666   (interactive)
9667   (gnus-summary-show-article t))
9668
9669 (defun gnus-summary-verbose-headers (&optional arg)
9670   "Toggle permanent full header display.
9671 If ARG is a positive number, turn header display on.
9672 If ARG is a negative number, turn header display off."
9673   (interactive "P")
9674   (setq gnus-show-all-headers
9675         (cond ((or (not (numberp arg))
9676                    (zerop arg))
9677                (not gnus-show-all-headers))
9678               ((natnump arg)
9679                t)))
9680   (gnus-summary-show-article))
9681
9682 (defun gnus-summary-toggle-header (&optional arg)
9683   "Show the headers if they are hidden, or hide them if they are shown.
9684 If ARG is a positive number, show the entire header.
9685 If ARG is a negative number, hide the unwanted header lines."
9686   (interactive "P")
9687   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9688                      (get-buffer-window gnus-article-buffer t))))
9689     (with-current-buffer gnus-article-buffer
9690       (widen)
9691       (article-narrow-to-head)
9692       (let* ((inhibit-read-only t)
9693              (inhibit-point-motion-hooks t)
9694              (hidden (if (numberp arg)
9695                          (>= arg 0)
9696                        (or (not (looking-at "[^ \t\n]+:"))
9697                            (gnus-article-hidden-text-p 'headers))))
9698              s e)
9699         (delete-region (point-min) (point-max))
9700         (with-current-buffer gnus-original-article-buffer
9701           (goto-char (setq s (point-min)))
9702           (setq e (if (search-forward "\n\n" nil t)
9703                       (1- (point))
9704                     (point-max))))
9705         (insert-buffer-substring gnus-original-article-buffer s e)
9706         (run-hooks 'gnus-article-decode-hook)
9707         (if hidden
9708             (let ((gnus-treat-hide-headers nil)
9709                   (gnus-treat-hide-boring-headers nil))
9710               (gnus-delete-wash-type 'headers)
9711               (gnus-treat-article 'head))
9712           (gnus-treat-article 'head))
9713         (widen)
9714         (if window
9715             (set-window-start window (goto-char (point-min))))
9716         (if gnus-break-pages
9717             (gnus-narrow-to-page)
9718           (when (gnus-visual-p 'page-marker)
9719             (let ((inhibit-read-only t))
9720               (gnus-remove-text-with-property 'gnus-prev)
9721               (gnus-remove-text-with-property 'gnus-next))))
9722         (gnus-set-mode-line 'article)))))
9723
9724 (defun gnus-summary-show-all-headers ()
9725   "Make all header lines visible."
9726   (interactive)
9727   (gnus-summary-toggle-header 1))
9728
9729 (defun gnus-summary-caesar-message (&optional arg)
9730   "Caesar rotate the current article by 13.
9731 With a non-numerical prefix, also rotate headers.  A numerical
9732 prefix specifies how many places to rotate each letter forward."
9733   (interactive "P")
9734   (gnus-summary-select-article)
9735   (let ((mail-header-separator ""))
9736     (gnus-eval-in-buffer-window gnus-article-buffer
9737       (save-restriction
9738         (widen)
9739         (let ((start (window-start))
9740               (inhibit-read-only t))
9741           (if (equal arg '(4))
9742               (message-caesar-buffer-body nil t)
9743             (message-caesar-buffer-body arg))
9744           (set-window-start (get-buffer-window (current-buffer)) start)))))
9745   ;; Create buttons and stuff...
9746   (gnus-treat-article nil))
9747
9748 (declare-function idna-to-unicode "ext:idna" (str))
9749
9750 (defun gnus-summary-idna-message (&optional arg)
9751   "Decode IDNA encoded domain names in the current articles.
9752 IDNA encoded domain names looks like `xn--bar'.  If a string
9753 remain unencoded after running this function, it is likely an
9754 invalid IDNA string (`xn--bar' is invalid).
9755
9756 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9757 installed for this command to work."
9758   (interactive "P")
9759   (if (not (and (condition-case nil (require 'idna)
9760                   (file-error))
9761                 (mm-coding-system-p 'utf-8)
9762                 (executable-find (symbol-value 'idna-program))))
9763       (gnus-message
9764        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9765     (gnus-summary-select-article)
9766     (let ((mail-header-separator ""))
9767       (gnus-eval-in-buffer-window gnus-article-buffer
9768         (save-restriction
9769           (widen)
9770           (let ((start (window-start))
9771                 buffer-read-only)
9772             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9773               (replace-match (idna-to-unicode (match-string 1))))
9774             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9775
9776 (defun gnus-summary-morse-message (&optional arg)
9777   "Morse decode the current article."
9778   (interactive "P")
9779   (gnus-summary-select-article)
9780   (let ((mail-header-separator ""))
9781     (gnus-eval-in-buffer-window gnus-article-buffer
9782       (save-excursion
9783         (save-restriction
9784           (widen)
9785           (let ((pos (window-start))
9786                 (inhibit-read-only t))
9787             (goto-char (point-min))
9788             (when (message-goto-body)
9789               (gnus-narrow-to-body))
9790             (goto-char (point-min))
9791             (while (search-forward "·" (point-max) t)
9792               (replace-match "."))
9793             (unmorse-region (point-min) (point-max))
9794             (widen)
9795             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9796
9797 (defun gnus-summary-stop-page-breaking ()
9798   "Stop page breaking in the current article."
9799   (interactive)
9800   (gnus-summary-select-article)
9801   (gnus-eval-in-buffer-window gnus-article-buffer
9802     (widen)
9803     (when (gnus-visual-p 'page-marker)
9804       (let ((inhibit-read-only t))
9805         (gnus-remove-text-with-property 'gnus-prev)
9806         (gnus-remove-text-with-property 'gnus-next))
9807       (setq gnus-page-broken nil))))
9808
9809 (defun gnus-summary-move-article (&optional n to-newsgroup
9810                                             select-method action)
9811   "Move the current article to a different newsgroup.
9812 If N is a positive number, move the N next articles.
9813 If N is a negative number, move the N previous articles.
9814 If N is nil and any articles have been marked with the process mark,
9815 move those articles instead.
9816 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9817 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9818 re-spool using this method.
9819
9820 When called interactively with TO-NEWSGROUP being nil, the value of
9821 the variable `gnus-move-split-methods' is used for finding a default
9822 for the target newsgroup.
9823
9824 For this function to work, both the current newsgroup and the
9825 newsgroup that you want to move to have to support the `request-move'
9826 and `request-accept' functions.
9827
9828 ACTION can be either `move' (the default), `crosspost' or `copy'."
9829   (interactive "P")
9830   (unless action
9831     (setq action 'move))
9832   ;; Check whether the source group supports the required functions.
9833   (cond ((and (eq action 'move)
9834               (not (gnus-check-backend-function
9835                     'request-move-article gnus-newsgroup-name)))
9836          (error "The current group does not support article moving"))
9837         ((and (eq action 'crosspost)
9838               (not (gnus-check-backend-function
9839                     'request-replace-article gnus-newsgroup-name)))
9840          (error "The current group does not support article editing")))
9841   (let ((articles (gnus-summary-work-articles n))
9842         (prefix (if (gnus-check-backend-function
9843                      'request-move-article gnus-newsgroup-name)
9844                     (funcall gnus-move-group-prefix-function
9845                              gnus-newsgroup-name)
9846                   ""))
9847         (names '((move "Move" "Moving")
9848                  (copy "Copy" "Copying")
9849                  (crosspost "Crosspost" "Crossposting")))
9850         (copy-buf (save-excursion
9851                     (nnheader-set-temp-buffer " *copy article*")))
9852         art-group to-method new-xref article to-groups
9853         articles-to-update-marks encoded)
9854     (unless (assq action names)
9855       (error "Unknown action %s" action))
9856     ;; Read the newsgroup name.
9857     (when (and (not to-newsgroup)
9858                (not select-method))
9859       (if (and gnus-move-split-methods
9860                (not
9861                 (and (memq gnus-current-article articles)
9862                      (gnus-buffer-live-p gnus-original-article-buffer))))
9863           ;; When `gnus-move-split-methods' is non-nil, we have to
9864           ;; select an article to give `gnus-read-move-group-name' an
9865           ;; opportunity to suggest an appropriate default.  However,
9866           ;; we needn't render or mark the article.
9867           (let ((gnus-display-mime-function nil)
9868                 (gnus-article-prepare-hook nil)
9869                 (gnus-mark-article-hook nil))
9870             (gnus-summary-select-article nil nil nil (car articles))))
9871       (setq to-newsgroup (gnus-read-move-group-name
9872                           (cadr (assq action names))
9873                           (symbol-value
9874                            (intern (format "gnus-current-%s-group" action)))
9875                           articles prefix)
9876             encoded to-newsgroup
9877             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9878       (set (intern (format "gnus-current-%s-group" action))
9879            (mm-decode-coding-string
9880             to-newsgroup
9881             (gnus-group-name-charset to-method to-newsgroup))))
9882     (unless to-method
9883       (setq to-method (or select-method
9884                           (gnus-server-to-method
9885                            (gnus-group-method to-newsgroup)))))
9886     (setq to-newsgroup
9887           (or encoded
9888               (and to-newsgroup
9889                    (mm-encode-coding-string
9890                     to-newsgroup
9891                     (gnus-group-name-charset to-method to-newsgroup)))))
9892     ;; Check the method we are to move this article to...
9893     (unless (gnus-check-backend-function
9894              'request-accept-article (car to-method))
9895       (error "%s does not support article copying" (car to-method)))
9896     (unless (gnus-check-server to-method)
9897       (error "Can't open server %s" (car to-method)))
9898     (gnus-message 6 "%s to %s: %s..."
9899                   (caddr (assq action names))
9900                   (or (car select-method)
9901                       (gnus-group-decoded-name to-newsgroup))
9902                   articles)
9903     (while articles
9904       (setq article (pop articles))
9905       ;; Set any marks that may have changed in the summary buffer.
9906       (when gnus-preserve-marks
9907         (gnus-summary-push-marks-to-backend article))
9908       (setq
9909        art-group
9910        (cond
9911         ;; Move the article.
9912         ((eq action 'move)
9913          ;; Remove this article from future suppression.
9914          (gnus-dup-unsuppress-article article)
9915          (let* ((from-method (gnus-find-method-for-group
9916                               gnus-newsgroup-name))
9917                 (to-method (or select-method
9918                                (gnus-find-method-for-group to-newsgroup)))
9919                 (move-is-internal (gnus-server-equal from-method to-method)))
9920            (gnus-request-move-article
9921             article                     ; Article to move
9922             gnus-newsgroup-name         ; From newsgroup
9923             (nth 1 (gnus-find-method-for-group
9924                     gnus-newsgroup-name)) ; Server
9925             (list 'gnus-request-accept-article
9926                   to-newsgroup (list 'quote select-method)
9927                   (not articles) t)     ; Accept form
9928             (not articles)              ; Only save nov last time
9929             (and move-is-internal
9930                  to-newsgroup           ; Not respooling
9931                                         ; Is this move internal?
9932                  (gnus-group-real-name to-newsgroup)))))
9933         ;; Copy the article.
9934         ((eq action 'copy)
9935          (with-current-buffer copy-buf
9936            (when (gnus-request-article-this-buffer article
9937                                                    gnus-newsgroup-name)
9938              (save-restriction
9939                (nnheader-narrow-to-headers)
9940                (dolist (hdr gnus-copy-article-ignored-headers)
9941                  (message-remove-header hdr t)))
9942              (gnus-request-accept-article
9943               to-newsgroup select-method (not articles) t))))
9944         ;; Crosspost the article.
9945         ((eq action 'crosspost)
9946          (let ((xref (message-tokenize-header
9947                       (mail-header-xref (gnus-summary-article-header
9948                                          article))
9949                       " ")))
9950            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9951                                   ":" (number-to-string article)))
9952            (unless xref
9953              (setq xref (list (system-name))))
9954            (setq new-xref
9955                  (concat
9956                   (mapconcat 'identity
9957                              (delete "Xref:" (delete new-xref xref))
9958                              " ")
9959                   " " new-xref))
9960            (with-current-buffer copy-buf
9961              ;; First put the article in the destination group.
9962              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9963              (when (consp (setq art-group
9964                                 (gnus-request-accept-article
9965                                  to-newsgroup select-method (not articles)
9966                                  t)))
9967                (setq new-xref (concat new-xref " " (car art-group)
9968                                       ":"
9969                                       (number-to-string (cdr art-group))))
9970                ;; Now we have the new Xrefs header, so we insert
9971                ;; it and replace the new article.
9972                (nnheader-replace-header "Xref" new-xref)
9973                (gnus-request-replace-article
9974                 (cdr art-group) to-newsgroup (current-buffer) t)
9975                art-group))))))
9976       (cond
9977        ((not art-group)
9978         (gnus-message 1 "Couldn't %s article %s: %s"
9979                       (cadr (assq action names)) article
9980                       (nnheader-get-report (car to-method))))
9981        ((eq art-group 'junk)
9982         (when (eq action 'move)
9983           (gnus-summary-mark-article article gnus-canceled-mark)
9984           (gnus-message 4 "Deleted article %s" article)
9985           ;; run the delete hook
9986           (run-hook-with-args 'gnus-summary-article-delete-hook
9987                               action
9988                               (gnus-data-header
9989                                (assoc article (gnus-data-list nil)))
9990                               gnus-newsgroup-name nil
9991                               select-method)))
9992        (t
9993         (let* ((pto-group (gnus-group-prefixed-name
9994                            (car art-group) to-method))
9995                (info (gnus-get-info pto-group))
9996                (to-group (gnus-info-group info))
9997                to-marks)
9998           ;; Update the group that has been moved to.
9999           (when (and info
10000                      (memq action '(move copy)))
10001             (unless (member to-group to-groups)
10002               (push to-group to-groups))
10003
10004             (when (and (not (memq article gnus-newsgroup-unreads))
10005                        (cdr art-group))
10006               (push 'read to-marks)
10007               (gnus-info-set-read
10008                info (gnus-add-to-range (gnus-info-read info)
10009                                        (list (cdr art-group)))))
10010
10011             ;; See whether the article is to be put in the cache.
10012             (let* ((expirable (gnus-group-auto-expirable-p to-group))
10013                    (marks (if expirable
10014                               gnus-article-mark-lists
10015                             (delete '(expirable . expire)
10016                                     (copy-sequence
10017                                      gnus-article-mark-lists))))
10018                    (to-article (cdr art-group)))
10019
10020               ;; Enter the article into the cache in the new group,
10021               ;; if that is required.
10022               (when (and to-article
10023                          gnus-use-cache)
10024                 (gnus-cache-possibly-enter-article
10025                  to-group to-article
10026                  (memq article gnus-newsgroup-marked)
10027                  (memq article gnus-newsgroup-dormant)
10028                  (memq article gnus-newsgroup-unreads)))
10029
10030               (when (and gnus-preserve-marks
10031                          to-article)
10032                 ;; Copy any marks over to the new group.
10033                 (when (and (equal to-group gnus-newsgroup-name)
10034                            (not (memq article gnus-newsgroup-unreads)))
10035                   ;; Mark this article as read in this group.
10036                   (push (cons to-article gnus-read-mark)
10037                         gnus-newsgroup-reads)
10038                   ;; Increase the active status of this group.
10039                   (setcdr (gnus-active to-group) to-article)
10040                   (setcdr gnus-newsgroup-active to-article))
10041
10042                 (while marks
10043                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10044                     (when (memq article (symbol-value
10045                                          (intern (format "gnus-newsgroup-%s"
10046                                                          (caar marks)))))
10047                       (push (cdar marks) to-marks)
10048                       ;; If the other group is the same as this group,
10049                       ;; then we have to add the mark to the list.
10050                       (when (equal to-group gnus-newsgroup-name)
10051                         (set (intern (format "gnus-newsgroup-%s"
10052                                              (caar marks)))
10053                              (cons to-article
10054                                    (symbol-value
10055                                     (intern (format "gnus-newsgroup-%s"
10056                                                     (caar marks)))))))
10057                       ;; Copy the marks to other group.
10058                       (gnus-add-marked-articles
10059                        to-group (cdar marks) (list to-article) info)))
10060                   (setq marks (cdr marks)))
10061
10062                 (when (and expirable
10063                            gnus-mark-copied-or-moved-articles-as-expirable
10064                            (not (memq 'expire to-marks)))
10065                   ;; Mark this article as expirable.
10066                   (push 'expire to-marks)
10067                   (when (equal to-group gnus-newsgroup-name)
10068                     (push to-article gnus-newsgroup-expirable))
10069                   ;; Copy the expirable mark to other group.
10070                   (gnus-add-marked-articles
10071                    to-group 'expire (list to-article) info))
10072
10073                 (when (and to-marks
10074                            (or gnus-propagate-marks
10075                                (gnus-method-option-p
10076                                 (gnus-find-method-for-group to-group)
10077                                 'server-marks)))
10078                   (gnus-request-set-mark
10079                    to-group (list (list (list to-article) 'add to-marks)))))
10080
10081               (gnus-dribble-enter
10082                (concat "(gnus-group-set-info '"
10083                        (gnus-prin1-to-string (gnus-get-info to-group))
10084                        ")")
10085                (concat "^(gnus-group-set-info '(\""
10086                        (regexp-quote to-group) "\""))))
10087
10088           ;; Update the Xref header in this article to point to
10089           ;; the new crossposted article we have just created.
10090           (when (eq action 'crosspost)
10091             (with-current-buffer copy-buf
10092               (gnus-request-article-this-buffer article gnus-newsgroup-name)
10093               (nnheader-replace-header "Xref" new-xref)
10094               (gnus-request-replace-article
10095                article gnus-newsgroup-name (current-buffer) t)))
10096
10097           ;; run the move/copy/crosspost/respool hook
10098           (run-hook-with-args 'gnus-summary-article-move-hook
10099                               action
10100                               (gnus-data-header
10101                                (assoc article (gnus-data-list nil)))
10102                               gnus-newsgroup-name
10103                               to-newsgroup
10104                               select-method))
10105
10106         ;;;!!!Why is this necessary?
10107         (set-buffer gnus-summary-buffer)
10108
10109         (when (eq action 'move)
10110           (save-excursion
10111             (gnus-summary-goto-subject article)
10112             (gnus-summary-mark-article article gnus-canceled-mark)))))
10113       (push article articles-to-update-marks))
10114
10115     (save-excursion
10116       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
10117     ;; Re-activate all groups that have been moved to.
10118     (with-current-buffer gnus-group-buffer
10119       (let ((gnus-group-marked to-groups))
10120         (gnus-group-get-new-news-this-group nil t)))
10121
10122     (gnus-kill-buffer copy-buf)
10123     (gnus-summary-position-point)
10124     (gnus-set-mode-line 'summary)))
10125
10126 (defun gnus-summary-push-marks-to-backend (article)
10127   (let ((set nil)
10128         (marks gnus-article-mark-lists))
10129     (unless (memq article gnus-newsgroup-unreads)
10130       (push 'read set))
10131     (while marks
10132       (when (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10133                  (memq article (symbol-value
10134                                 (intern (format "gnus-newsgroup-%s"
10135                                                 (caar marks))))))
10136         (push (cdar marks) set))
10137       (pop marks))
10138     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)))))
10139
10140 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10141   "Copy the current article to some other group.
10142 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10143 When called interactively, if TO-NEWSGROUP is nil, use the value of
10144 the variable `gnus-move-split-methods' for finding a default target
10145 newsgroup.
10146 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10147 re-spool using this method."
10148   (interactive "P")
10149   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10150
10151 (defun gnus-summary-crosspost-article (&optional n)
10152   "Crosspost the current article to some other group."
10153   (interactive "P")
10154   (gnus-summary-move-article n nil nil 'crosspost))
10155
10156 (defcustom gnus-summary-respool-default-method nil
10157   "Default method type for respooling an article.
10158 If nil, use to the current newsgroup method."
10159   :type 'symbol
10160   :group 'gnus-summary-mail)
10161
10162 (defun gnus-summary-respool-article (&optional n method)
10163   "Respool the current article.
10164 The article will be squeezed through the mail spooling process again,
10165 which means that it will be put in some mail newsgroup or other
10166 depending on `nnmail-split-methods'.
10167 If N is a positive number, respool the N next articles.
10168 If N is a negative number, respool the N previous articles.
10169 If N is nil and any articles have been marked with the process mark,
10170 respool those articles instead.
10171
10172 Respooling can be done both from mail groups and \"real\" newsgroups.
10173 In the former case, the articles in question will be moved from the
10174 current group into whatever groups they are destined to.  In the
10175 latter case, they will be copied into the relevant groups."
10176   (interactive
10177    (list current-prefix-arg
10178          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10179                 (methname
10180                  (symbol-name (or gnus-summary-respool-default-method
10181                                   (car (gnus-find-method-for-group
10182                                         gnus-newsgroup-name)))))
10183                 (method
10184                  (gnus-completing-read
10185                   "Backend to use when respooling"
10186                   methods t nil 'gnus-mail-method-history methname))
10187                 ms)
10188            (cond
10189             ((zerop (length (setq ms (gnus-servers-using-backend
10190                                       (intern method)))))
10191              (list (intern method) ""))
10192             ((= 1 (length ms))
10193              (car ms))
10194             (t
10195              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10196                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10197                            ms-alist))))))))
10198   (unless method
10199     (error "No method given for respooling"))
10200   (if (assoc (symbol-name
10201               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10202              (gnus-methods-using 'respool))
10203       (gnus-summary-move-article n nil method)
10204     (gnus-summary-copy-article n nil method)))
10205
10206 (defun gnus-summary-import-article (file &optional edit)
10207   "Import an arbitrary file into a mail newsgroup."
10208   (interactive "fImport file: \nP")
10209   (let ((group gnus-newsgroup-name)
10210         (now (current-time))
10211         atts lines group-art)
10212     (unless (gnus-check-backend-function 'request-accept-article group)
10213       (error "%s does not support article importing" group))
10214     (or (file-readable-p file)
10215         (not (file-regular-p file))
10216         (error "Can't read %s" file))
10217     (with-current-buffer (gnus-get-buffer-create " *import file*")
10218       (erase-buffer)
10219       (nnheader-insert-file-contents file)
10220       (goto-char (point-min))
10221       (if (nnheader-article-p)
10222           (save-restriction
10223             (goto-char (point-min))
10224             (search-forward "\n\n" nil t)
10225             (narrow-to-region (point-min) (1- (point)))
10226             (goto-char (point-min))
10227             (unless (re-search-forward "^date:" nil t)
10228               (goto-char (point-max))
10229               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10230        ;; This doesn't look like an article, so we fudge some headers.
10231         (setq atts (file-attributes file)
10232               lines (count-lines (point-min) (point-max)))
10233         (insert "From: " (read-string "From: ") "\n"
10234                 "Subject: " (read-string "Subject: ") "\n"
10235                 "Date: " (message-make-date (nth 5 atts)) "\n"
10236                 "Message-ID: " (message-make-message-id) "\n"
10237                 "Lines: " (int-to-string lines) "\n"
10238                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10239       (setq group-art (gnus-request-accept-article group nil t))
10240       (kill-buffer (current-buffer)))
10241     (setq gnus-newsgroup-active (gnus-activate-group group))
10242     (forward-line 1)
10243     (gnus-summary-goto-article (cdr group-art) nil t)
10244     (when edit
10245       (gnus-summary-edit-article))))
10246
10247 (defun gnus-summary-create-article ()
10248   "Create an article in a mail newsgroup."
10249   (interactive)
10250   (let ((group gnus-newsgroup-name)
10251         (now (current-time))
10252         group-art)
10253     (unless (gnus-check-backend-function 'request-accept-article group)
10254       (error "%s does not support article importing" group))
10255     (with-current-buffer (gnus-get-buffer-create " *import file*")
10256       (erase-buffer)
10257       (goto-char (point-min))
10258       ;; This doesn't look like an article, so we fudge some headers.
10259       (insert "From: " (read-string "From: ") "\n"
10260               "Subject: " (read-string "Subject: ") "\n"
10261               "Date: " (message-make-date now) "\n"
10262               "Message-ID: " (message-make-message-id) "\n")
10263       (setq group-art (gnus-request-accept-article group nil t))
10264       (kill-buffer (current-buffer)))
10265     (setq gnus-newsgroup-active (gnus-activate-group group))
10266     (forward-line 1)
10267     (gnus-summary-goto-article (cdr group-art) nil t)
10268     (gnus-summary-edit-article)))
10269
10270 (defun gnus-summary-article-posted-p ()
10271   "Say whether the current (mail) article is available from news as well.
10272 This will be the case if the article has both been mailed and posted."
10273   (interactive)
10274   (let ((id (mail-header-references (gnus-summary-article-header)))
10275         (gnus-override-method (car (gnus-refer-article-methods))))
10276     (if (gnus-request-head id "")
10277         (gnus-message 2 "The current message was found on %s"
10278                       gnus-override-method)
10279       (gnus-message 2 "The current message couldn't be found on %s"
10280                     gnus-override-method)
10281       nil)))
10282
10283 (defun gnus-summary-expire-articles (&optional now)
10284   "Expire all articles that are marked as expirable in the current group."
10285   (interactive)
10286   (when (and (not gnus-group-is-exiting-without-update-p)
10287              (gnus-check-backend-function
10288               'request-expire-articles gnus-newsgroup-name))
10289     ;; This backend supports expiry.
10290     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10291            (expirable (if total
10292                           (progn
10293                             ;; We need to update the info for
10294                             ;; this group for `gnus-list-of-read-articles'
10295                             ;; to give us the right answer.
10296                             (gnus-run-hooks 'gnus-exit-group-hook)
10297                             (gnus-summary-update-info)
10298                             (gnus-list-of-read-articles gnus-newsgroup-name))
10299                         (setq gnus-newsgroup-expirable
10300                               (sort gnus-newsgroup-expirable '<))))
10301            (expiry-wait (if now 'immediate
10302                           (gnus-group-find-parameter
10303                            gnus-newsgroup-name 'expiry-wait)))
10304            (nnmail-expiry-target
10305             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10306                 nnmail-expiry-target))
10307            es)
10308       (when expirable
10309         ;; There are expirable articles in this group, so we run them
10310         ;; through the expiry process.
10311         (gnus-message 6 "Expiring articles...")
10312         (when (gnus-check-group gnus-newsgroup-name)
10313           ;; The list of articles that weren't expired is returned.
10314           (save-excursion
10315             (if expiry-wait
10316                 (let ((nnmail-expiry-wait-function nil)
10317                       (nnmail-expiry-wait expiry-wait))
10318                   (setq es (gnus-request-expire-articles
10319                             expirable gnus-newsgroup-name)))
10320               (setq es (gnus-request-expire-articles
10321                         expirable gnus-newsgroup-name)))
10322             (unless total
10323               (setq gnus-newsgroup-expirable es))
10324             ;; We go through the old list of expirable, and mark all
10325             ;; really expired articles as nonexistent.
10326             (unless (eq es expirable) ;If nothing was expired, we don't mark.
10327               (let ((gnus-use-cache nil))
10328                 (dolist (article expirable)
10329                   (when (and (not (memq article es))
10330                              (gnus-data-find article))
10331                     (gnus-summary-mark-article article gnus-canceled-mark)
10332                     (run-hook-with-args 'gnus-summary-article-expire-hook
10333                                         'delete
10334                                         (gnus-data-header
10335                                          (assoc article (gnus-data-list nil)))
10336                                         gnus-newsgroup-name
10337                                         nil
10338                                         nil)))))))
10339         (gnus-message 6 "Expiring articles...done")))))
10340
10341 (defun gnus-summary-expire-articles-now ()
10342   "Expunge all expirable articles in the current group.
10343 This means that *all* articles that are marked as expirable will be
10344 deleted forever, right now."
10345   (interactive)
10346   (or gnus-expert-user
10347       (gnus-yes-or-no-p
10348        "Are you really, really sure you want to delete all expirable messages? ")
10349       (error "Phew!"))
10350   (gnus-summary-expire-articles t))
10351
10352 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10353 (defun gnus-summary-delete-article (&optional n)
10354   "Delete the N next (mail) articles.
10355 This command actually deletes articles.  This is not a marking
10356 command.  The article will disappear forever from your life, never to
10357 return.
10358
10359 If N is negative, delete backwards.
10360 If N is nil and articles have been marked with the process mark,
10361 delete these instead.
10362
10363 If `gnus-novice-user' is non-nil you will be asked for
10364 confirmation before the articles are deleted."
10365   (interactive "P")
10366   (unless (gnus-check-backend-function 'request-expire-articles
10367                                        gnus-newsgroup-name)
10368     (error "The current newsgroup does not support article deletion"))
10369   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10370     (error "Couldn't open server"))
10371   ;; Compute the list of articles to delete.
10372   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10373         (nnmail-expiry-target 'delete)
10374         not-deleted)
10375     (if (and gnus-novice-user
10376              (not (gnus-yes-or-no-p
10377                    (format "Do you really want to delete %s forever? "
10378                            (if (> (length articles) 1)
10379                                (format "these %s articles" (length articles))
10380                              "this article")))))
10381         ()
10382       ;; Delete the articles.
10383       (setq not-deleted (gnus-request-expire-articles
10384                          articles gnus-newsgroup-name 'force))
10385       (save-excursion
10386         (while articles
10387           (gnus-summary-remove-process-mark (car articles))
10388           ;; The backend might not have been able to delete the article
10389           ;; after all.
10390           (unless (memq (car articles) not-deleted)
10391             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10392             (let* ((article (car articles))
10393                    (ghead  (gnus-data-header
10394                             (assoc article (gnus-data-list nil)))))
10395               (run-hook-with-args 'gnus-summary-article-delete-hook
10396                                   'delete ghead gnus-newsgroup-name nil
10397                                   nil)))
10398           (setq articles (cdr articles))))
10399       (when not-deleted
10400         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10401     (gnus-summary-position-point)
10402     (gnus-set-mode-line 'summary)
10403     not-deleted))
10404
10405 (defun gnus-summary-edit-article (&optional arg)
10406   "Edit the current article.
10407 This will have permanent effect only in mail groups.
10408 If ARG is nil, edit the decoded articles.
10409 If ARG is 1, edit the raw articles.
10410 If ARG is 2, edit the raw articles even in read-only groups.
10411 If ARG is 3, edit the articles with the current handles.
10412 Otherwise, allow editing of articles even in read-only
10413 groups."
10414   (interactive "P")
10415   (let (force raw current-handles)
10416     (cond
10417      ((null arg))
10418      ((eq arg 1)
10419       (setq raw t))
10420      ((eq arg 2)
10421       (setq raw t
10422             force t))
10423      ((eq arg 3)
10424       (setq current-handles
10425             (and (gnus-buffer-live-p gnus-article-buffer)
10426                  (with-current-buffer gnus-article-buffer
10427                    (prog1
10428                        gnus-article-mime-handles
10429                      (setq gnus-article-mime-handles nil))))))
10430      (t
10431       (setq force t)))
10432     (when (and raw (not force)
10433                (member gnus-newsgroup-name '("nndraft:delayed"
10434                                              "nndraft:drafts"
10435                                              "nndraft:queue")))
10436       (error "Can't edit the raw article in group %s"
10437              gnus-newsgroup-name))
10438     (with-current-buffer gnus-summary-buffer
10439       (let ((mail-parse-charset gnus-newsgroup-charset)
10440             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10441         (gnus-set-global-variables)
10442         (when (and (not force)
10443                    (gnus-group-read-only-p))
10444           (error "The current newsgroup does not support article editing"))
10445         (gnus-summary-show-article t)
10446         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10447           (with-current-buffer gnus-article-buffer
10448             (mm-enable-multibyte)))
10449         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10450             (setq raw t))
10451         (gnus-article-edit-article
10452          (if raw 'ignore
10453            `(lambda ()
10454               (let ((mbl mml-buffer-list))
10455                 (setq mml-buffer-list nil)
10456                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10457                   (mime-to-mml ,'current-handles))
10458                 (let ((mbl1 mml-buffer-list))
10459                   (setq mml-buffer-list mbl)
10460                   (set (make-local-variable 'mml-buffer-list) mbl1))
10461                 (gnus-make-local-hook 'kill-buffer-hook)
10462                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10463          `(lambda (no-highlight)
10464             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10465                   (message-options message-options)
10466                   (message-options-set-recipient)
10467                   (mail-parse-ignored-charsets
10468                    ',gnus-newsgroup-ignored-charsets)
10469                   (rfc2047-header-encoding-alist
10470                    ',(let ((charset (gnus-group-name-charset
10471                                      (gnus-find-method-for-group
10472                                       gnus-newsgroup-name)
10473                                      gnus-newsgroup-name)))
10474                        (append (list (cons "Newsgroups" charset)
10475                                      (cons "Followup-To" charset)
10476                                      (cons "Xref" charset))
10477                                rfc2047-header-encoding-alist))))
10478               ,(if (not raw) '(progn
10479                                 (mml-to-mime)
10480                                 (mml-destroy-buffers)
10481                                 (remove-hook 'kill-buffer-hook
10482                                              'mml-destroy-buffers t)
10483                                 (kill-local-variable 'mml-buffer-list)))
10484               (gnus-summary-edit-article-done
10485                ,(or (mail-header-references gnus-current-headers) "")
10486                ,(gnus-group-read-only-p)
10487                ,gnus-summary-buffer no-highlight))))))))
10488
10489 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10490
10491 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10492                                                  no-highlight)
10493   "Make edits to the current article permanent."
10494   (interactive)
10495   (save-excursion
10496     ;; The buffer restriction contains the entire article if it exists.
10497     (when (article-goto-body)
10498       (let ((lines (count-lines (point) (point-max)))
10499             (length (- (point-max) (point)))
10500             (case-fold-search t)
10501             (body (copy-marker (point))))
10502         (goto-char (point-min))
10503         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10504           (delete-region (match-beginning 1) (match-end 1))
10505           (insert (number-to-string length)))
10506         (goto-char (point-min))
10507         (when (re-search-forward
10508                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10509           (delete-region (match-beginning 1) (match-end 1))
10510           (insert (number-to-string length)))
10511         (goto-char (point-min))
10512         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10513           (delete-region (match-beginning 1) (match-end 1))
10514           (insert (number-to-string lines))))))
10515   ;; Replace the article.
10516   (let ((buf (current-buffer))
10517         (article (cdr gnus-article-current))
10518         replace-result)
10519     (with-temp-buffer
10520       (insert-buffer-substring buf)
10521       (if (and (not read-only)
10522                (not (setq replace-result
10523                           (gnus-request-replace-article
10524                            article (car gnus-article-current)
10525                            (current-buffer) t))))
10526           (error "Couldn't replace article")
10527         ;; If we got a number back, then that's the new article number
10528         ;; for this article.  Otherwise, the article number didn't change.
10529         (when (numberp replace-result)
10530           (with-current-buffer gnus-summary-buffer
10531             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10532             (gnus-summary-limit gnus-newsgroup-limit)
10533             (setq article replace-result)
10534             (gnus-summary-goto-subject article t)))
10535         ;; Update the summary buffer.
10536         (if (and references
10537                  (equal (message-tokenize-header references " ")
10538                         (message-tokenize-header
10539                          (or (message-fetch-field "references") "") " ")))
10540             ;; We only have to update this line.
10541             (save-excursion
10542               (save-restriction
10543                 (message-narrow-to-head)
10544                 (let ((head (buffer-substring-no-properties
10545                              (point-min) (point-max)))
10546                       header)
10547                   (with-temp-buffer
10548                     (insert (format "211 %d Article retrieved.\n" article))
10549                     (insert head)
10550                     (insert ".\n")
10551                     (let ((nntp-server-buffer (current-buffer)))
10552                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10553                     (with-current-buffer gnus-summary-buffer
10554                       (gnus-data-set-header (gnus-data-find article) header)
10555                       (gnus-summary-update-article-line article header)
10556                       (if (gnus-summary-goto-subject article nil t)
10557                           (gnus-summary-update-secondary-mark article)))))))
10558           ;; Update threads.
10559           (set-buffer (or buffer gnus-summary-buffer))
10560           (gnus-summary-update-article article)
10561           (if (gnus-summary-goto-subject article nil t)
10562               (gnus-summary-update-secondary-mark article)))
10563         ;; Prettify the article buffer again.
10564         (unless no-highlight
10565           (with-current-buffer gnus-article-buffer
10566             ;;!!! Fix this -- article should be rehighlighted.
10567             ;;(gnus-run-hooks 'gnus-article-display-hook)
10568             (set-buffer gnus-original-article-buffer)
10569             (gnus-request-article
10570              article (car gnus-article-current) (current-buffer))))
10571         ;; Prettify the summary buffer line.
10572         (when (gnus-visual-p 'summary-highlight 'highlight)
10573           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10574
10575 (defun gnus-summary-edit-wash (key)
10576   "Perform editing command KEY in the article buffer."
10577   (interactive
10578    (list
10579     (progn
10580       (message "%s" (concat (this-command-keys) "- "))
10581       (read-char))))
10582   (message "")
10583   (gnus-summary-edit-article)
10584   (execute-kbd-macro (concat (this-command-keys) key))
10585   (gnus-article-edit-done))
10586
10587 ;;; Respooling
10588
10589 (defun gnus-summary-respool-query (&optional silent trace)
10590   "Query where the respool algorithm would put this article."
10591   (interactive)
10592   (let (gnus-mark-article-hook)
10593     (gnus-summary-select-article)
10594     (with-current-buffer gnus-original-article-buffer
10595       (let ((groups (nnmail-article-group 'identity trace)))
10596         (unless silent
10597           (if groups
10598               (message "This message would go to %s"
10599                        (mapconcat 'car groups ", "))
10600             (message "This message would go to no groups"))
10601           groups)))))
10602
10603 (defun gnus-summary-respool-trace ()
10604   "Trace where the respool algorithm would put this article.
10605 Display a buffer showing all fancy splitting patterns which matched."
10606   (interactive)
10607   (gnus-summary-respool-query nil t))
10608
10609 ;; Summary marking commands.
10610
10611 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10612   "Mark articles which has the same subject as read, and then select the next.
10613 If UNMARK is positive, remove any kind of mark.
10614 If UNMARK is negative, tick articles."
10615   (interactive "P")
10616   (when unmark
10617     (setq unmark (prefix-numeric-value unmark)))
10618   (let ((count
10619          (gnus-summary-mark-same-subject
10620           (gnus-summary-article-subject) unmark)))
10621     ;; Select next unread article.  If auto-select-same mode, should
10622     ;; select the first unread article.
10623     (gnus-summary-next-article t (and gnus-auto-select-same
10624                                       (gnus-summary-article-subject)))
10625     (gnus-message 7 "%d article%s marked as %s"
10626                   count (if (= count 1) " is" "s are")
10627                   (if unmark "unread" "read"))))
10628
10629 (defun gnus-summary-kill-same-subject (&optional unmark)
10630   "Mark articles which has the same subject as read.
10631 If UNMARK is positive, remove any kind of mark.
10632 If UNMARK is negative, tick articles."
10633   (interactive "P")
10634   (when unmark
10635     (setq unmark (prefix-numeric-value unmark)))
10636   (let ((count
10637          (gnus-summary-mark-same-subject
10638           (gnus-summary-article-subject) unmark)))
10639     ;; If marked as read, go to next unread subject.
10640     (when (null unmark)
10641       ;; Go to next unread subject.
10642       (gnus-summary-next-subject 1 t))
10643     (gnus-message 7 "%d articles are marked as %s"
10644                   count (if unmark "unread" "read"))))
10645
10646 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10647   "Mark articles with same SUBJECT as read, and return marked number.
10648 If optional argument UNMARK is positive, remove any kinds of marks.
10649 If optional argument UNMARK is negative, mark articles as unread instead."
10650   (let ((count 1))
10651     (save-excursion
10652       (cond
10653        ((null unmark)                   ; Mark as read.
10654         (while (and
10655                 (progn
10656                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10657                   (gnus-summary-show-thread) t)
10658                 (gnus-summary-find-subject subject))
10659           (setq count (1+ count))))
10660        ((> unmark 0)                    ; Tick.
10661         (while (and
10662                 (progn
10663                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10664                   (gnus-summary-show-thread) t)
10665                 (gnus-summary-find-subject subject))
10666           (setq count (1+ count))))
10667        (t                               ; Mark as unread.
10668         (while (and
10669                 (progn
10670                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10671                   (gnus-summary-show-thread) t)
10672                 (gnus-summary-find-subject subject))
10673           (setq count (1+ count)))))
10674       (gnus-set-mode-line 'summary)
10675       ;; Return the number of marked articles.
10676       count)))
10677
10678 (defun gnus-summary-mark-as-processable (n &optional unmark)
10679   "Set the process mark on the next N articles.
10680 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10681 the process mark instead.  The difference between N and the actual
10682 number of articles marked is returned."
10683   (interactive "P")
10684   (if (and (null n) (gnus-region-active-p))
10685       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10686     (setq n (prefix-numeric-value n))
10687     (let ((backward (< n 0))
10688           (n (abs n)))
10689       (while (and
10690               (> n 0)
10691               (if unmark
10692                   (gnus-summary-remove-process-mark
10693                    (gnus-summary-article-number))
10694                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10695               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10696         (setq n (1- n)))
10697       (when (/= 0 n)
10698         (gnus-message 7 "No more articles"))
10699       (gnus-summary-recenter)
10700       (gnus-summary-position-point)
10701       n)))
10702
10703 (defun gnus-summary-unmark-as-processable (n)
10704   "Remove the process mark from the next N articles.
10705 If N is negative, unmark backward instead.  The difference between N and
10706 the actual number of articles unmarked is returned."
10707   (interactive "P")
10708   (gnus-summary-mark-as-processable n t))
10709
10710 (defun gnus-summary-unmark-all-processable ()
10711   "Remove the process mark from all articles."
10712   (interactive)
10713   (save-excursion
10714     (while gnus-newsgroup-processable
10715       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10716   (gnus-summary-position-point))
10717
10718 (defun gnus-summary-add-mark (article type)
10719   "Mark ARTICLE with a mark of TYPE."
10720   (let ((vtype (car (assq type gnus-article-mark-lists)))
10721         var)
10722     (if (not vtype)
10723         (error "No such mark type: %s" type)
10724       (setq var (intern (format "gnus-newsgroup-%s" type)))
10725       (set var (cons article (symbol-value var)))
10726       (if (memq type '(processable cached replied forwarded recent saved))
10727           (gnus-summary-update-secondary-mark article)
10728         ;;; !!! This is bogus.  We should find out what primary
10729         ;;; !!! mark we want to set.
10730         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10731
10732 (defun gnus-summary-mark-as-expirable (n)
10733   "Mark N articles forward as expirable.
10734 If N is negative, mark backward instead.  The difference between N and
10735 the actual number of articles marked is returned."
10736   (interactive "p")
10737   (gnus-summary-mark-forward n gnus-expirable-mark))
10738
10739 (defun gnus-summary-mark-as-spam (n)
10740   "Mark N articles forward as spam.
10741 If N is negative, mark backward instead.  The difference between N and
10742 the actual number of articles marked is returned."
10743   (interactive "p")
10744   (gnus-summary-mark-forward n gnus-spam-mark))
10745
10746 (defun gnus-summary-mark-article-as-replied (article)
10747   "Mark ARTICLE as replied to and update the summary line.
10748 ARTICLE can also be a list of articles."
10749   (interactive (list (gnus-summary-article-number)))
10750   (let ((articles (if (listp article) article (list article))))
10751     (dolist (article articles)
10752       (unless (numberp article)
10753         (error "%s is not a number" article))
10754       (push article gnus-newsgroup-replied)
10755       (let ((inhibit-read-only t))
10756         (when (gnus-summary-goto-subject article nil t)
10757           (gnus-summary-update-secondary-mark article))))))
10758
10759 (defun gnus-summary-mark-article-as-forwarded (article)
10760   "Mark ARTICLE as forwarded and update the summary line.
10761 ARTICLE can also be a list of articles."
10762   (let ((articles (if (listp article) article (list article))))
10763     (dolist (article articles)
10764       (push article gnus-newsgroup-forwarded)
10765       (let ((inhibit-read-only t))
10766         (when (gnus-summary-goto-subject article nil t)
10767           (gnus-summary-update-secondary-mark article))))))
10768
10769 (defun gnus-summary-set-bookmark (article)
10770   "Set a bookmark in current article."
10771   (interactive (list (gnus-summary-article-number)))
10772   (when (or (not (get-buffer gnus-article-buffer))
10773             (not gnus-current-article)
10774             (not gnus-article-current)
10775             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10776     (error "No current article selected"))
10777   ;; Remove old bookmark, if one exists.
10778   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10779   ;; Set the new bookmark, which is on the form
10780   ;; (article-number . line-number-in-body).
10781   (push
10782    (cons article
10783          (with-current-buffer gnus-article-buffer
10784            (count-lines
10785             (min (point)
10786                  (save-excursion
10787                    (article-goto-body)
10788                    (point)))
10789             (point))))
10790    gnus-newsgroup-bookmarks)
10791   (gnus-message 6 "A bookmark has been added to the current article."))
10792
10793 (defun gnus-summary-remove-bookmark (article)
10794   "Remove the bookmark from the current article."
10795   (interactive (list (gnus-summary-article-number)))
10796   ;; Remove old bookmark, if one exists.
10797   (if (not (assq article gnus-newsgroup-bookmarks))
10798       (gnus-message 6 "No bookmark in current article.")
10799     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10800     (gnus-message 6 "Removed bookmark.")))
10801
10802 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10803 (defun gnus-summary-mark-as-dormant (n)
10804   "Mark N articles forward as dormant.
10805 If N is negative, mark backward instead.  The difference between N and
10806 the actual number of articles marked is returned."
10807   (interactive "p")
10808   (gnus-summary-mark-forward n gnus-dormant-mark))
10809
10810 (defun gnus-summary-set-process-mark (article)
10811   "Set the process mark on ARTICLE and update the summary line."
10812   (setq gnus-newsgroup-processable
10813         (cons article
10814               (delq article gnus-newsgroup-processable)))
10815   (when (gnus-summary-goto-subject article)
10816     (gnus-summary-show-thread)
10817     (gnus-summary-goto-subject article)
10818     (gnus-summary-update-secondary-mark article)))
10819
10820 (defun gnus-summary-remove-process-mark (&rest articles)
10821   "Remove the process mark from ARTICLES and update the summary line."
10822   (dolist (article articles)
10823     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10824     (when (gnus-summary-goto-subject article)
10825       (gnus-summary-show-thread)
10826       (gnus-summary-goto-subject article)
10827       (gnus-summary-update-secondary-mark article)))
10828   t)
10829
10830 (defun gnus-summary-set-saved-mark (article)
10831   "Set the process mark on ARTICLE and update the summary line."
10832   (push article gnus-newsgroup-saved)
10833   (when (gnus-summary-goto-subject article)
10834     (gnus-summary-update-secondary-mark article)))
10835
10836 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10837   "Mark N articles as read forwards.
10838 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10839 The difference between N and the actual number of articles marked is
10840 returned.
10841 If NO-EXPIRE, auto-expiry will be inhibited."
10842   (interactive "p")
10843   (gnus-summary-show-thread)
10844   (let ((backward (< n 0))
10845         (gnus-summary-goto-unread
10846          (and gnus-summary-goto-unread
10847               (not (eq gnus-summary-goto-unread 'never))
10848               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10849                                     gnus-ticked-mark gnus-dormant-mark)))))
10850         (n (abs n))
10851         (mark (or mark gnus-del-mark)))
10852     (while (and (> n 0)
10853                 (gnus-summary-mark-article nil mark no-expire)
10854                 (zerop (gnus-summary-next-subject
10855                         (if backward -1 1)
10856                         (and gnus-summary-goto-unread
10857                              (not (eq gnus-summary-goto-unread 'never)))
10858                         t)))
10859       (setq n (1- n)))
10860     (when (/= 0 n)
10861       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10862     (gnus-summary-recenter)
10863     (gnus-summary-position-point)
10864     (gnus-set-mode-line 'summary)
10865     n))
10866
10867 (defun gnus-summary-mark-article-as-read (mark)
10868   "Mark the current article quickly as read with MARK."
10869   (let ((article (gnus-summary-article-number)))
10870     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10871     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10872     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10873     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10874     (push (cons article mark) gnus-newsgroup-reads)
10875     ;; Possibly remove from cache, if that is used.
10876     (when gnus-use-cache
10877       (gnus-cache-enter-remove-article article))
10878     ;; Allow the backend to change the mark.
10879     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10880     ;; Check for auto-expiry.
10881     (when (and gnus-newsgroup-auto-expire
10882                (memq mark gnus-auto-expirable-marks))
10883       (setq mark gnus-expirable-mark)
10884       ;; Let the backend know about the mark change.
10885       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10886       (push article gnus-newsgroup-expirable))
10887     ;; Set the mark in the buffer.
10888     (gnus-summary-update-mark mark 'unread)
10889     t))
10890
10891 (defun gnus-summary-mark-article-as-unread (mark)
10892   "Mark the current article quickly as unread with MARK."
10893   (let* ((article (gnus-summary-article-number))
10894          (old-mark (gnus-summary-article-mark article)))
10895     ;; Allow the backend to change the mark.
10896     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10897     (if (eq mark old-mark)
10898         t
10899       (if (<= article 0)
10900           (progn
10901             (gnus-error 1 "Can't mark negative article numbers")
10902             nil)
10903         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10904         (setq gnus-newsgroup-spam-marked
10905               (delq article gnus-newsgroup-spam-marked))
10906         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10907         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10908         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10909         (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10910         (cond ((= mark gnus-ticked-mark)
10911                (setq gnus-newsgroup-marked
10912                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10913                                               article)))
10914               ((= mark gnus-spam-mark)
10915                (setq gnus-newsgroup-spam-marked
10916                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10917                                               article)))
10918               ((= mark gnus-dormant-mark)
10919                (setq gnus-newsgroup-dormant
10920                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10921                                               article)))
10922               (t
10923                (setq gnus-newsgroup-unreads
10924                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10925                                               article))))
10926         (gnus-alist-pull article gnus-newsgroup-reads)
10927
10928         ;; See whether the article is to be put in the cache.
10929         (and gnus-use-cache
10930              (vectorp (gnus-summary-article-header article))
10931              (save-excursion
10932                (gnus-cache-possibly-enter-article
10933                 gnus-newsgroup-name article
10934                 (= mark gnus-ticked-mark)
10935                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10936
10937         ;; Fix the mark.
10938         (gnus-summary-update-mark mark 'unread)
10939         t))))
10940
10941 (defun gnus-summary-mark-article (&optional article mark no-expire)
10942   "Mark ARTICLE with MARK.  MARK can be any character.
10943 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10944 `??' (dormant) and `?E' (expirable).
10945 If MARK is nil, then the default character `?r' is used.
10946 If ARTICLE is nil, then the article on the current line will be
10947 marked.
10948 If NO-EXPIRE, auto-expiry will be inhibited."
10949   ;; The mark might be a string.
10950   (when (stringp mark)
10951     (setq mark (aref mark 0)))
10952   ;; If no mark is given, then we check auto-expiring.
10953   (when (null mark)
10954     (setq mark gnus-del-mark))
10955   (when (and (not no-expire)
10956              gnus-newsgroup-auto-expire
10957              (memq mark gnus-auto-expirable-marks))
10958     (setq mark gnus-expirable-mark))
10959   (let ((article (or article (gnus-summary-article-number)))
10960         (old-mark (gnus-summary-article-mark article)))
10961     ;; Allow the backend to change the mark.
10962     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10963     (if (eq mark old-mark)
10964         t
10965       (unless article
10966         (error "No article on current line"))
10967       (if (not (if (or (= mark gnus-unread-mark)
10968                        (= mark gnus-ticked-mark)
10969                        (= mark gnus-spam-mark)
10970                        (= mark gnus-dormant-mark))
10971                    (gnus-mark-article-as-unread article mark)
10972                  (gnus-mark-article-as-read article mark)))
10973           t
10974         ;; See whether the article is to be put in the cache.
10975         (and gnus-use-cache
10976              (not (= mark gnus-canceled-mark))
10977              (vectorp (gnus-summary-article-header article))
10978              (save-excursion
10979                (gnus-cache-possibly-enter-article
10980                 gnus-newsgroup-name article
10981                 (= mark gnus-ticked-mark)
10982                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10983
10984         (when (gnus-summary-goto-subject article nil t)
10985           (let ((inhibit-read-only t))
10986             (gnus-summary-show-thread)
10987             ;; Fix the mark.
10988             (gnus-summary-update-mark mark 'unread)
10989             t))))))
10990
10991 (defun gnus-summary-update-secondary-mark (article)
10992   "Update the secondary (read, process, cache) mark."
10993   (gnus-summary-update-mark
10994    (cond ((memq article gnus-newsgroup-processable)
10995           gnus-process-mark)
10996          ((memq article gnus-newsgroup-cached)
10997           gnus-cached-mark)
10998          ((memq article gnus-newsgroup-replied)
10999           gnus-replied-mark)
11000          ((memq article gnus-newsgroup-forwarded)
11001           gnus-forwarded-mark)
11002          ((memq article gnus-newsgroup-saved)
11003           gnus-saved-mark)
11004          ((memq article gnus-newsgroup-unseen)
11005           gnus-unseen-mark)
11006          (t gnus-no-mark))
11007    'replied)
11008   (when (gnus-visual-p 'summary-highlight 'highlight)
11009     (gnus-summary-highlight-line)
11010     (gnus-run-hooks 'gnus-summary-update-hook))
11011   t)
11012
11013 (defun gnus-summary-update-download-mark (article)
11014   "Update the download mark."
11015   (gnus-summary-update-mark
11016    (cond ((memq article gnus-newsgroup-undownloaded)
11017           gnus-undownloaded-mark)
11018          (gnus-newsgroup-agentized
11019           gnus-downloaded-mark)
11020          (t
11021           gnus-no-mark))
11022    'download)
11023   (gnus-summary-update-line t)
11024   t)
11025
11026 (defun gnus-summary-update-mark (mark type)
11027   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11028         (inhibit-read-only t))
11029     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
11030     (when forward
11031       (when (looking-at "\r")
11032         (incf forward))
11033       (when (<= (+ forward (point)) (point-max))
11034         ;; Go to the right position on the line.
11035         (goto-char (+ forward (point)))
11036         ;; Replace the old mark with the new mark.
11037         (let ((to-insert
11038                (mm-subst-char-in-string
11039                 (char-after) mark
11040                 (buffer-substring (point) (1+ (point))))))
11041           (delete-region (point) (1+ (point)))
11042           (insert to-insert))
11043         ;; Optionally update the marks by some user rule.
11044         (when (eq type 'unread)
11045           (gnus-data-set-mark
11046            (gnus-data-find (gnus-summary-article-number)) mark)
11047           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
11048
11049 (defun gnus-mark-article-as-read (article &optional mark)
11050   "Enter ARTICLE in the pertinent lists and remove it from others."
11051   ;; Make the article expirable.
11052   (let ((mark (or mark gnus-del-mark)))
11053     (setq gnus-newsgroup-expirable
11054           (if (= mark gnus-expirable-mark)
11055               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
11056             (delq article gnus-newsgroup-expirable)))
11057     ;; Remove from unread and marked lists.
11058     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11059     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11060     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
11061     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11062     (push (cons article mark) gnus-newsgroup-reads)
11063     ;; Possibly remove from cache, if that is used.
11064     (when gnus-use-cache
11065       (gnus-cache-enter-remove-article article))
11066     t))
11067
11068 (defun gnus-mark-article-as-unread (article &optional mark)
11069   "Enter ARTICLE in the pertinent lists and remove it from others."
11070   (let ((mark (or mark gnus-ticked-mark)))
11071     (if (<= article 0)
11072         (progn
11073           (gnus-error 1 "Can't mark negative article numbers")
11074           nil)
11075       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
11076             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
11077             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
11078             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
11079             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11080
11081       ;; Unsuppress duplicates?
11082       (when gnus-suppress-duplicates
11083         (gnus-dup-unsuppress-article article))
11084
11085       (cond ((= mark gnus-ticked-mark)
11086              (setq gnus-newsgroup-marked
11087                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
11088             ((= mark gnus-spam-mark)
11089              (setq gnus-newsgroup-spam-marked
11090                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
11091                                             article)))
11092             ((= mark gnus-dormant-mark)
11093              (setq gnus-newsgroup-dormant
11094                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
11095             (t
11096              (setq gnus-newsgroup-unreads
11097                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
11098       (gnus-alist-pull article gnus-newsgroup-reads)
11099       t)))
11100
11101 (defun gnus-summary-tick-article-forward (n)
11102   "Tick N articles forwards.
11103 If N is negative, tick backwards instead.
11104 The difference between N and the number of articles ticked is returned."
11105   (interactive "p")
11106   (gnus-summary-mark-forward n gnus-ticked-mark))
11107
11108 (defun gnus-summary-tick-article-backward (n)
11109   "Tick N articles backwards.
11110 The difference between N and the number of articles ticked is returned."
11111   (interactive "p")
11112   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11113
11114 (defun gnus-summary-tick-article (&optional article clear-mark)
11115   "Mark current article as unread.
11116 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11117 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11118   (interactive)
11119   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11120                                        gnus-ticked-mark)))
11121
11122 (defun gnus-summary-mark-as-read-forward (n)
11123   "Mark N articles as read forwards.
11124 If N is negative, mark backwards instead.
11125 The difference between N and the actual number of articles marked is
11126 returned."
11127   (interactive "p")
11128   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11129
11130 (defun gnus-summary-mark-as-read-backward (n)
11131   "Mark the N articles as read backwards.
11132 The difference between N and the actual number of articles marked is
11133 returned."
11134   (interactive "p")
11135   (gnus-summary-mark-forward
11136    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11137
11138 (defun gnus-summary-mark-as-read (&optional article mark)
11139   "Mark current article as read.
11140 ARTICLE specifies the article to be marked as read.
11141 MARK specifies a string to be inserted at the beginning of the line."
11142   (gnus-summary-mark-article article mark))
11143
11144 (defun gnus-summary-clear-mark-forward (n)
11145   "Clear marks from N articles forward.
11146 If N is negative, clear backward instead.
11147 The difference between N and the number of marks cleared is returned."
11148   (interactive "p")
11149   (gnus-summary-mark-forward n gnus-unread-mark))
11150
11151 (defun gnus-summary-clear-mark-backward (n)
11152   "Clear marks from N articles backward.
11153 The difference between N and the number of marks cleared is returned."
11154   (interactive "p")
11155   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11156
11157 (defun gnus-summary-mark-unread-as-read ()
11158   "Intended to be used by `gnus-mark-article-hook'."
11159   (when (memq gnus-current-article gnus-newsgroup-unreads)
11160     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11161
11162 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11163   "Intended to be used by `gnus-mark-article-hook'."
11164   (let ((mark (gnus-summary-article-mark)))
11165     (when (or (gnus-unread-mark-p mark)
11166               (gnus-read-mark-p mark))
11167       (gnus-summary-mark-article gnus-current-article
11168                                  (or new-mark gnus-read-mark)))))
11169
11170 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11171   "Intended to be used by `gnus-mark-article-hook'."
11172   (let ((mark (gnus-summary-article-mark)))
11173     (when (or (gnus-unread-mark-p mark)
11174               (gnus-read-mark-p mark))
11175       (gnus-summary-mark-article (gnus-summary-article-number)
11176                                  (or new-mark gnus-read-mark)))))
11177
11178 (defun gnus-summary-mark-unread-as-ticked ()
11179   "Intended to be used by `gnus-mark-article-hook'."
11180   (when (memq gnus-current-article gnus-newsgroup-unreads)
11181     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11182
11183 (defun gnus-summary-mark-region-as-read (point mark all)
11184   "Mark all unread articles between point and mark as read.
11185 If given a prefix, mark all articles between point and mark as read,
11186 even ticked and dormant ones."
11187   (interactive "r\nP")
11188   (save-excursion
11189     (let (article)
11190       (goto-char point)
11191       (beginning-of-line)
11192       (while (and
11193               (< (point) mark)
11194               (progn
11195                 (when (or all
11196                           (memq (setq article (gnus-summary-article-number))
11197                                 gnus-newsgroup-unreads))
11198                   (gnus-summary-mark-article article gnus-del-mark))
11199                 t)
11200               (gnus-summary-find-next))))))
11201
11202 (defun gnus-summary-mark-below (score mark)
11203   "Mark articles with score less than SCORE with MARK."
11204   (interactive "P\ncMark: ")
11205   (setq score (if score
11206                   (prefix-numeric-value score)
11207                 (or gnus-summary-default-score 0)))
11208   (with-current-buffer gnus-summary-buffer
11209     (goto-char (point-min))
11210     (while
11211         (progn
11212           (and (< (gnus-summary-article-score) score)
11213                (gnus-summary-mark-article nil mark))
11214           (gnus-summary-find-next)))))
11215
11216 (defun gnus-summary-kill-below (&optional score)
11217   "Mark articles with score below SCORE as read."
11218   (interactive "P")
11219   (gnus-summary-mark-below score gnus-killed-mark))
11220
11221 (defun gnus-summary-clear-above (&optional score)
11222   "Clear all marks from articles with score above SCORE."
11223   (interactive "P")
11224   (gnus-summary-mark-above score gnus-unread-mark))
11225
11226 (defun gnus-summary-tick-above (&optional score)
11227   "Tick all articles with score above SCORE."
11228   (interactive "P")
11229   (gnus-summary-mark-above score gnus-ticked-mark))
11230
11231 (defun gnus-summary-mark-above (score mark)
11232   "Mark articles with score over SCORE with MARK."
11233   (interactive "P\ncMark: ")
11234   (setq score (if score
11235                   (prefix-numeric-value score)
11236                 (or gnus-summary-default-score 0)))
11237   (with-current-buffer gnus-summary-buffer
11238     (goto-char (point-min))
11239     (while (and (progn
11240                   (when (> (gnus-summary-article-score) score)
11241                     (gnus-summary-mark-article nil mark))
11242                   t)
11243                 (gnus-summary-find-next)))))
11244
11245 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11246 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11247 (defun gnus-summary-limit-include-expunged (&optional no-error)
11248   "Display all the hidden articles that were expunged for low scores."
11249   (interactive)
11250   (let ((inhibit-read-only t))
11251     (let ((scored gnus-newsgroup-scored)
11252           headers h)
11253       (while scored
11254         (unless (gnus-summary-article-header (caar scored))
11255           (and (setq h (gnus-number-to-header (caar scored)))
11256                (< (cdar scored) gnus-summary-expunge-below)
11257                (push h headers)))
11258         (setq scored (cdr scored)))
11259       (if (not headers)
11260           (when (not no-error)
11261             (error "No expunged articles hidden"))
11262         (goto-char (point-min))
11263         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11264         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11265         (dolist (x headers)
11266           (push (mail-header-number x) gnus-newsgroup-limit))
11267         (gnus-summary-prepare-unthreaded (nreverse headers))
11268         (goto-char (point-min))
11269         (gnus-summary-position-point)
11270         t))))
11271
11272 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11273   "Mark all unread articles in this newsgroup as read.
11274 If prefix argument ALL is non-nil, ticked and dormant articles will
11275 also be marked as read.
11276 If QUIETLY is non-nil, no questions will be asked.
11277
11278 If TO-HERE is non-nil, it should be a point in the buffer.  All
11279 articles before (after, if REVERSE is set) this point will be marked
11280 as read.
11281
11282 Note that this function will only catch up the unread article
11283 in the current summary buffer limitation.
11284
11285 The number of articles marked as read is returned."
11286   (interactive "P")
11287   (prog1
11288       (save-excursion
11289         (when (or quietly
11290                   (not gnus-interactive-catchup) ;Without confirmation?
11291                   gnus-expert-user
11292                   (gnus-y-or-n-p
11293                    (if all
11294                        "Mark absolutely all articles as read? "
11295                      "Mark all unread articles as read? ")))
11296           (if (and not-mark
11297                    (not gnus-newsgroup-adaptive)
11298                    (not gnus-newsgroup-auto-expire)
11299                    (not gnus-suppress-duplicates)
11300                    (or (not gnus-use-cache)
11301                        (eq gnus-use-cache 'passive)))
11302               (progn
11303                 (when all
11304                   (setq gnus-newsgroup-marked nil
11305                         gnus-newsgroup-spam-marked nil
11306                         gnus-newsgroup-dormant nil))
11307                 (setq gnus-newsgroup-unreads
11308                       (gnus-sorted-nunion
11309                        (gnus-sorted-intersection gnus-newsgroup-unreads
11310                                                  gnus-newsgroup-downloadable)
11311                        (gnus-sorted-difference gnus-newsgroup-unfetched
11312                                                gnus-newsgroup-cached))))
11313             ;; We actually mark all articles as canceled, which we
11314             ;; have to do when using auto-expiry or adaptive scoring.
11315             (gnus-summary-show-all-threads)
11316             (if (and to-here reverse)
11317                 (progn
11318                   (goto-char to-here)
11319                   (gnus-summary-mark-current-read-and-unread-as-read
11320                    gnus-catchup-mark)
11321                   (while (gnus-summary-find-next (not all))
11322                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11323               (when (gnus-summary-first-subject (not all))
11324                 (while (and
11325                         (if to-here (< (point) to-here) t)
11326                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11327                         (gnus-summary-find-next (not all))))))
11328             (gnus-set-mode-line 'summary))
11329           t))
11330     (gnus-summary-position-point)))
11331
11332 (defun gnus-summary-catchup-to-here (&optional all)
11333   "Mark all unticked articles before the current one as read.
11334 If ALL is non-nil, also mark ticked and dormant articles as read."
11335   (interactive "P")
11336   (save-excursion
11337     (gnus-save-hidden-threads
11338       (let ((beg (point)))
11339         ;; We check that there are unread articles.
11340         (when (or all (gnus-summary-find-prev))
11341           (gnus-summary-catchup all t beg)))))
11342   (gnus-summary-position-point))
11343
11344 (defun gnus-summary-catchup-from-here (&optional all)
11345   "Mark all unticked articles after (and including) the current one as read.
11346 If ALL is non-nil, also mark ticked and dormant articles as read."
11347   (interactive "P")
11348   (save-excursion
11349     (gnus-save-hidden-threads
11350       (let ((beg (point)))
11351         ;; We check that there are unread articles.
11352         (when (or all (gnus-summary-find-next))
11353           (gnus-summary-catchup all t beg nil t)))))
11354   (gnus-summary-position-point))
11355
11356 (defun gnus-summary-catchup-all (&optional quietly)
11357   "Mark all articles in this newsgroup as read.
11358 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11359 instead, which marks only unread articles as read."
11360   (interactive "P")
11361   (gnus-summary-catchup t quietly))
11362
11363 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11364   "Mark all unread articles in this group as read, then exit.
11365 If prefix argument ALL is non-nil, all articles are marked as read.
11366 If QUIETLY is non-nil, no questions will be asked."
11367   (interactive "P")
11368   (when (gnus-summary-catchup all quietly nil 'fast)
11369     ;; Select next newsgroup or exit.
11370     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11371              (eq gnus-auto-select-next 'quietly))
11372         (gnus-summary-next-group nil)
11373       (gnus-summary-exit))))
11374
11375 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11376   "Mark all articles in this newsgroup as read, and then exit.
11377 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11378 instead, which marks only unread articles as read."
11379   (interactive "P")
11380   (gnus-summary-catchup-and-exit t quietly))
11381
11382 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11383   "Mark all articles in this group as read and select the next group.
11384 If given a prefix, mark all articles, unread as well as ticked, as
11385 read."
11386   (interactive "P")
11387   (save-excursion
11388     (gnus-summary-catchup all))
11389   (gnus-summary-next-group))
11390
11391 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11392   "Mark all articles in this group as read and select the previous group.
11393 If given a prefix, mark all articles, unread as well as ticked, as
11394 read."
11395   (interactive "P")
11396   (save-excursion
11397     (gnus-summary-catchup all))
11398   (gnus-summary-next-group nil nil t))
11399
11400 ;;;
11401 ;;; with article
11402 ;;;
11403
11404 (defmacro gnus-with-article (article &rest forms)
11405   "Select ARTICLE and perform FORMS in the original article buffer.
11406 Then replace the article with the result."
11407   `(progn
11408      ;; We don't want the article to be marked as read.
11409      (let (gnus-mark-article-hook)
11410        (gnus-summary-select-article t t nil ,article))
11411      (set-buffer gnus-original-article-buffer)
11412      ,@forms
11413      (if (not (gnus-check-backend-function
11414                'request-replace-article (car gnus-article-current)))
11415          (gnus-message 5 "Read-only group; not replacing")
11416        (unless (gnus-request-replace-article
11417                 ,article (car gnus-article-current)
11418                 (current-buffer) t)
11419          (error "Couldn't replace article")))
11420      ;; The cache and backlog have to be flushed somewhat.
11421      (when gnus-keep-backlog
11422        (gnus-backlog-remove-article
11423         (car gnus-article-current) (cdr gnus-article-current)))
11424      (when gnus-use-cache
11425        (gnus-cache-update-article
11426         (car gnus-article-current) (cdr gnus-article-current)))))
11427
11428 (put 'gnus-with-article 'lisp-indent-function 1)
11429 (put 'gnus-with-article 'edebug-form-spec '(form body))
11430
11431 ;; Thread-based commands.
11432
11433 (defun gnus-summary-articles-in-thread (&optional article)
11434   "Return a list of all articles in the current thread.
11435 If ARTICLE is non-nil, return all articles in the thread that starts
11436 with that article."
11437   (let* ((article (or article (gnus-summary-article-number)))
11438          (data (gnus-data-find-list article))
11439          (top-level (gnus-data-level (car data)))
11440          (top-subject
11441           (cond ((null gnus-thread-operation-ignore-subject)
11442                  (gnus-simplify-subject-re
11443                   (mail-header-subject (gnus-data-header (car data)))))
11444                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11445                  (gnus-simplify-subject-fuzzy
11446                   (mail-header-subject (gnus-data-header (car data)))))
11447                 (t nil)))
11448          (end-point (save-excursion
11449                       (goto-char (gnus-data-pos (car data)))
11450                       (if (gnus-summary-go-to-next-thread)
11451                           (point) (point-max))))
11452          articles)
11453     (while (and data
11454                 (< (gnus-data-pos (car data)) end-point))
11455       (when (or (not top-subject)
11456                 (string= top-subject
11457                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11458                              (gnus-simplify-subject-fuzzy
11459                               (mail-header-subject
11460                                (gnus-data-header (car data))))
11461                            (gnus-simplify-subject-re
11462                             (mail-header-subject
11463                              (gnus-data-header (car data)))))))
11464         (push (gnus-data-number (car data)) articles))
11465       (unless (and (setq data (cdr data))
11466                    (> (gnus-data-level (car data)) top-level))
11467         (setq data nil)))
11468     ;; Return the list of articles.
11469     (nreverse articles)))
11470
11471 (defun gnus-summary-rethread-current ()
11472   "Rethread the thread the current article is part of."
11473   (interactive)
11474   (let* ((gnus-show-threads t)
11475          (article (gnus-summary-article-number))
11476          (id (mail-header-id (gnus-summary-article-header)))
11477          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11478     (unless id
11479       (error "No article on the current line"))
11480     (gnus-rebuild-thread id)
11481     (gnus-summary-goto-subject article)))
11482
11483 (defun gnus-summary-reparent-thread ()
11484   "Make the current article child of the marked (or previous) article.
11485
11486 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11487 is non-nil or the Subject: of both articles are the same."
11488   (interactive)
11489   (unless (not (gnus-group-read-only-p))
11490     (error "The current newsgroup does not support article editing"))
11491   (unless (<= (length gnus-newsgroup-processable) 1)
11492     (error "No more than one article may be marked"))
11493   (let ((child (gnus-summary-article-number))
11494         ;; First grab the marked article, otherwise one line up.
11495         (parent (if (not (null gnus-newsgroup-processable))
11496                     (car gnus-newsgroup-processable)
11497                   (save-excursion
11498                     (if (eq (forward-line -1) 0)
11499                         (gnus-summary-article-number)
11500                       (error "Beginning of summary buffer"))))))
11501     (gnus-summary-reparent-children parent (list child))))
11502
11503 (defun gnus-summary-reparent-children (parent children)
11504   "Make PARENT the parent of CHILDREN.
11505 When called interactively, PARENT is the current article and CHILDREN
11506 are the process-marked articles."
11507   (interactive
11508    (list (gnus-summary-article-number)
11509          (gnus-summary-work-articles nil)))
11510   (dolist (child children)
11511     (save-window-excursion
11512       (let ((gnus-article-buffer " *reparent*"))
11513         (unless (not (eq parent child))
11514           (error "An article may not be self-referential"))
11515         (let ((message-id (mail-header-id
11516                            (gnus-summary-article-header parent))))
11517           (unless (and message-id (not (equal message-id "")))
11518             (error "No message-id in desired parent"))
11519           (gnus-with-article child
11520             (save-restriction
11521               (goto-char (point-min))
11522               (message-narrow-to-head)
11523               (if (re-search-forward "^References: " nil t)
11524                   (progn
11525                     (re-search-forward "^[^ \t]" nil t)
11526                     (forward-line -1)
11527                     (end-of-line)
11528                     (insert " " message-id))
11529                 (insert "References: " message-id "\n"))))
11530           (set-buffer gnus-summary-buffer)
11531           (gnus-summary-unmark-all-processable)
11532           (gnus-summary-update-article child)
11533           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11534             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11535           (gnus-summary-rethread-current)
11536           (gnus-message 3 "Article %d is now the child of article %d"
11537                         child parent))))))
11538
11539 (defun gnus-summary-toggle-threads (&optional arg)
11540   "Toggle showing conversation threads.
11541 If ARG is positive number, turn showing conversation threads on."
11542   (interactive "P")
11543   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11544     (setq gnus-show-threads
11545           (if (null arg) (not gnus-show-threads)
11546             (> (prefix-numeric-value arg) 0)))
11547     (gnus-summary-prepare)
11548     (gnus-summary-goto-subject current)
11549     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11550     (gnus-summary-position-point)))
11551
11552 (eval-and-compile
11553   (if (fboundp 'remove-overlays)
11554       (defalias 'gnus-remove-overlays 'remove-overlays)
11555     (defun gnus-remove-overlays (beg end name val)
11556       "Clear BEG and END of overlays whose property NAME has value VAL.
11557 For compatibility with XEmacs."
11558       (dolist (ov (gnus-overlays-in beg end))
11559         (when (eq (gnus-overlay-get ov name) val)
11560           (gnus-delete-overlay ov))))))
11561
11562 (defun gnus-summary-show-all-threads ()
11563   "Show all threads."
11564   (interactive)
11565   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11566   (gnus-summary-position-point))
11567
11568 (defsubst gnus-summary--inv (p)
11569   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11570
11571 (defun gnus-summary-show-thread ()
11572   "Show thread subtrees.
11573 Returns nil if no thread was there to be shown."
11574   (interactive)
11575   (let* ((orig (point))
11576          (end (point-at-eol))
11577          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11578          ;; Leave point at bol
11579          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11580          (eoi (when end
11581                 (if (fboundp 'next-single-char-property-change)
11582                     (or (next-single-char-property-change end 'invisible)
11583                         (point-max))
11584                   (while (progn
11585                            (end-of-line 2)
11586                            (and (not (eobp))
11587                                 (eq (get-char-property (point) 'invisible)
11588                                     'gnus-sum))))
11589                   (point)))))
11590     (when eoi
11591       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11592       (goto-char orig)
11593       (gnus-summary-position-point)
11594       eoi)))
11595
11596 (defun gnus-summary-maybe-hide-threads ()
11597   "If requested, hide the threads that should be hidden."
11598   (when (and gnus-show-threads
11599              gnus-thread-hide-subtree)
11600     (gnus-summary-hide-all-threads
11601      (if (or (consp gnus-thread-hide-subtree)
11602              (functionp gnus-thread-hide-subtree))
11603          (gnus-make-predicate gnus-thread-hide-subtree)
11604        nil))))
11605
11606 ;;; Hiding predicates.
11607
11608 (defun gnus-article-unread-p (header)
11609   (memq (mail-header-number header) gnus-newsgroup-unreads))
11610
11611 (defun gnus-article-unseen-p (header)
11612   (memq (mail-header-number header) gnus-newsgroup-unseen))
11613
11614 (defun gnus-map-articles (predicate articles)
11615   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11616   (apply 'gnus-or (mapcar predicate
11617                           (mapcar (lambda (number)
11618                                     (gnus-summary-article-header number))
11619                                   articles))))
11620
11621 (defun gnus-summary-hide-all-threads (&optional predicate)
11622   "Hide all thread subtrees.
11623 If PREDICATE is supplied, threads that satisfy this predicate
11624 will not be hidden."
11625   (interactive)
11626   (save-excursion
11627     (goto-char (point-min))
11628     (let ((end nil)
11629           (count 0))
11630       (while (not end)
11631         (incf count)
11632         (when (zerop (mod count 1000))
11633           (message "Hiding all threads... %d" count))
11634         (when (or (not predicate)
11635                   (gnus-map-articles
11636                    predicate (gnus-summary-article-children)))
11637             (gnus-summary-hide-thread))
11638         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11639   (gnus-summary-position-point))
11640
11641 (defun gnus-summary-hide-thread ()
11642   "Hide thread subtrees.
11643 If PREDICATE is supplied, threads that satisfy this predicate
11644 will not be hidden.
11645 Returns nil if no threads were there to be hidden."
11646   (interactive)
11647   (let ((start (point))
11648         (starteol (line-end-position))
11649         (article (gnus-summary-article-number)))
11650     (goto-char start)
11651     ;; Go forward until either the buffer ends or the subthread ends.
11652     (when (and (not (eobp))
11653                (or (zerop (gnus-summary-next-thread 1 t))
11654                    (goto-char (point-max))))
11655       (if (and (> (point) start)
11656                ;; FIXME: this should actually search for a non-invisible \n.
11657                (search-backward "\n" start t))
11658           (progn
11659             (when (> (point) starteol)
11660               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11661               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11662                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11663                 (gnus-overlay-put ol 'evaporate t)))
11664             (gnus-summary-goto-subject article)
11665             (when (> start (point))
11666               (message "Hiding the thread moved us backwards, aborting!")
11667               (goto-char (point-max))))
11668         (goto-char start)
11669         nil))))
11670
11671 (defun gnus-summary-go-to-next-thread (&optional previous)
11672   "Go to the same level (or less) next thread.
11673 If PREVIOUS is non-nil, go to previous thread instead.
11674 Return the article number moved to, or nil if moving was impossible."
11675   (let ((level (gnus-summary-thread-level))
11676         (way (if previous -1 1))
11677         (beg (point)))
11678     (forward-line way)
11679     (while (and (not (eobp))
11680                 (< level (gnus-summary-thread-level)))
11681       (forward-line way))
11682     (if (eobp)
11683         (progn
11684           (goto-char beg)
11685           nil)
11686       (setq beg (point))
11687       (prog1
11688           (gnus-summary-article-number)
11689         (goto-char beg)))))
11690
11691 (defun gnus-summary-next-thread (n &optional silent)
11692   "Go to the same level next N'th thread.
11693 If N is negative, search backward instead.
11694 Returns the difference between N and the number of skips actually
11695 done.
11696
11697 If SILENT, don't output messages."
11698   (interactive "p")
11699   (let ((backward (< n 0))
11700         (n (abs n)))
11701     (while (and (> n 0)
11702                 (gnus-summary-go-to-next-thread backward))
11703       (decf n))
11704     (unless silent
11705       (gnus-summary-position-point))
11706     (when (and (not silent) (/= 0 n))
11707       (gnus-message 7 "No more threads"))
11708     n))
11709
11710 (defun gnus-summary-prev-thread (n)
11711   "Go to the same level previous N'th thread.
11712 Returns the difference between N and the number of skips actually
11713 done."
11714   (interactive "p")
11715   (gnus-summary-next-thread (- n)))
11716
11717 (defun gnus-summary-go-down-thread ()
11718   "Go down one level in the current thread."
11719   (let ((children (gnus-summary-article-children)))
11720     (when children
11721       (gnus-summary-goto-subject (car children)))))
11722
11723 (defun gnus-summary-go-up-thread ()
11724   "Go up one level in the current thread."
11725   (let ((parent (gnus-summary-article-parent)))
11726     (when parent
11727       (gnus-summary-goto-subject parent))))
11728
11729 (defun gnus-summary-down-thread (n)
11730   "Go down thread N steps.
11731 If N is negative, go up instead.
11732 Returns the difference between N and how many steps down that were
11733 taken."
11734   (interactive "p")
11735   (let ((up (< n 0))
11736         (n (abs n)))
11737     (while (and (> n 0)
11738                 (if up (gnus-summary-go-up-thread)
11739                   (gnus-summary-go-down-thread)))
11740       (setq n (1- n)))
11741     (gnus-summary-position-point)
11742     (when (/= 0 n)
11743       (gnus-message 7 "Can't go further"))
11744     n))
11745
11746 (defun gnus-summary-up-thread (n)
11747   "Go up thread N steps.
11748 If N is negative, go down instead.
11749 Returns the difference between N and how many steps down that were
11750 taken."
11751   (interactive "p")
11752   (gnus-summary-down-thread (- n)))
11753
11754 (defun gnus-summary-top-thread ()
11755   "Go to the top of the thread."
11756   (interactive)
11757   (while (gnus-summary-go-up-thread))
11758   (gnus-summary-article-number))
11759
11760 (defun gnus-summary-expire-thread ()
11761   "Mark articles under current thread as expired."
11762   (interactive)
11763   (gnus-summary-kill-thread 0))
11764
11765 (defun gnus-summary-kill-thread (&optional unmark)
11766   "Mark articles under current thread as read.
11767 If the prefix argument is positive, remove any kinds of marks.
11768 If the prefix argument is zero, mark thread as expired.
11769 If the prefix argument is negative, tick articles instead."
11770   (interactive "P")
11771   (when unmark
11772     (setq unmark (prefix-numeric-value unmark)))
11773   (let ((articles (gnus-summary-articles-in-thread))
11774         (hide (or (null unmark) (= unmark 0))))
11775     (save-excursion
11776       ;; Expand the thread.
11777       (gnus-summary-show-thread)
11778       ;; Mark all the articles.
11779       (while articles
11780         (gnus-summary-goto-subject (car articles))
11781         (cond ((null unmark)
11782                (gnus-summary-mark-article-as-read gnus-killed-mark))
11783               ((> unmark 0)
11784                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11785               ((= unmark 0)
11786                (gnus-summary-mark-article nil gnus-expirable-mark))
11787               (t
11788                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11789         (setq articles (cdr articles))))
11790     ;; Hide killed subtrees when hide is true.
11791     (and hide
11792          gnus-thread-hide-killed
11793          (gnus-summary-hide-thread))
11794     ;; If hide is t, go to next unread subject.
11795     (when hide
11796       ;; Go to next unread subject.
11797       (gnus-summary-next-subject 1 t)))
11798   (gnus-set-mode-line 'summary))
11799
11800 ;; Summary sorting commands
11801
11802 (defun gnus-summary-sort-by-number (&optional reverse)
11803   "Sort the summary buffer by article number.
11804 Argument REVERSE means reverse order."
11805   (interactive "P")
11806   (gnus-summary-sort 'number reverse))
11807
11808 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11809   "Sort the summary buffer by most recent article number.
11810 Argument REVERSE means reverse order."
11811   (interactive "P")
11812   (gnus-summary-sort 'most-recent-number reverse))
11813
11814 (defun gnus-summary-sort-by-random (&optional reverse)
11815   "Randomize the order in the summary buffer.
11816 Argument REVERSE means to randomize in reverse order."
11817   (interactive "P")
11818   (gnus-summary-sort 'random reverse))
11819
11820 (defun gnus-summary-sort-by-author (&optional reverse)
11821   "Sort the summary buffer by author name alphabetically.
11822 If `case-fold-search' is non-nil, case of letters is ignored.
11823 Argument REVERSE means reverse order."
11824   (interactive "P")
11825   (gnus-summary-sort 'author reverse))
11826
11827 (defun gnus-summary-sort-by-recipient (&optional reverse)
11828   "Sort the summary buffer by recipient name alphabetically.
11829 If `case-fold-search' is non-nil, case of letters is ignored.
11830 Argument REVERSE means reverse order."
11831   (interactive "P")
11832   (gnus-summary-sort 'recipient reverse))
11833
11834 (defun gnus-summary-sort-by-subject (&optional reverse)
11835   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11836 If `case-fold-search' is non-nil, case of letters is ignored.
11837 Argument REVERSE means reverse order."
11838   (interactive "P")
11839   (gnus-summary-sort 'subject reverse))
11840
11841 (defun gnus-summary-sort-by-date (&optional reverse)
11842   "Sort the summary buffer by date.
11843 Argument REVERSE means reverse order."
11844   (interactive "P")
11845   (gnus-summary-sort 'date reverse))
11846
11847 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11848   "Sort the summary buffer by most recent date.
11849 Argument REVERSE means reverse order."
11850   (interactive "P")
11851   (gnus-summary-sort 'most-recent-date reverse))
11852
11853 (defun gnus-summary-sort-by-score (&optional reverse)
11854   "Sort the summary buffer by score.
11855 Argument REVERSE means reverse order."
11856   (interactive "P")
11857   (gnus-summary-sort 'score reverse))
11858
11859 (defun gnus-summary-sort-by-lines (&optional reverse)
11860   "Sort the summary buffer by the number of lines.
11861 Argument REVERSE means reverse order."
11862   (interactive "P")
11863   (gnus-summary-sort 'lines reverse))
11864
11865 (defun gnus-summary-sort-by-chars (&optional reverse)
11866   "Sort the summary buffer by article length.
11867 Argument REVERSE means reverse order."
11868   (interactive "P")
11869   (gnus-summary-sort 'chars reverse))
11870
11871 (defun gnus-summary-sort-by-original (&optional reverse)
11872   "Sort the summary buffer using the default sorting method.
11873 Argument REVERSE means reverse order."
11874   (interactive "P")
11875   (let* ((inhibit-read-only t)
11876          (gnus-summary-prepare-hook nil))
11877     ;; We do the sorting by regenerating the threads.
11878     (gnus-summary-prepare)
11879     ;; Hide subthreads if needed.
11880     (gnus-summary-maybe-hide-threads)))
11881
11882 (defun gnus-summary-sort (predicate reverse)
11883   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11884   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11885          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11886          (gnus-thread-sort-functions
11887           (if (not reverse)
11888               thread
11889             `(lambda (t1 t2)
11890                (,thread t2 t1))))
11891          (gnus-sort-gathered-threads-function
11892           gnus-thread-sort-functions)
11893          (gnus-article-sort-functions
11894           (if (not reverse)
11895               article
11896             `(lambda (t1 t2)
11897                (,article t2 t1))))
11898          (inhibit-read-only t)
11899          (gnus-summary-prepare-hook nil))
11900     ;; We do the sorting by regenerating the threads.
11901     (gnus-summary-prepare)
11902     ;; Hide subthreads if needed.
11903     (gnus-summary-maybe-hide-threads)))
11904
11905 ;; Summary saving commands.
11906
11907 (defun gnus-summary-save-article (&optional n not-saved)
11908   "Save the current article using the default saver function.
11909 If N is a positive number, save the N next articles.
11910 If N is a negative number, save the N previous articles.
11911 If N is nil and any articles have been marked with the process mark,
11912 save those articles instead.
11913 The variable `gnus-default-article-saver' specifies the saver function.
11914
11915 If the optional second argument NOT-SAVED is non-nil, articles saved
11916 will not be marked as saved."
11917   (interactive "P")
11918   (require 'gnus-art)
11919   (let* ((articles (gnus-summary-work-articles n))
11920          (save-buffer (save-excursion
11921                         (nnheader-set-temp-buffer " *Gnus Save*")))
11922          (num (length articles))
11923          ;; Whether to save decoded articles or raw articles.
11924          (decode (when gnus-article-save-coding-system
11925                    (get gnus-default-article-saver :decode)))
11926          ;; When saving many articles in a single file, use the other
11927          ;; function to save articles other than the first one.
11928          (saver2 (get gnus-default-article-saver :function))
11929          (gnus-prompt-before-saving (if saver2
11930                                         t
11931                                       gnus-prompt-before-saving))
11932          (gnus-default-article-saver gnus-default-article-saver)
11933          header file)
11934     (dolist (article articles)
11935       (setq header (gnus-summary-article-header article))
11936       (if (not (vectorp header))
11937           ;; This is a pseudo-article.
11938           (if (assq 'name header)
11939               (gnus-copy-file (cdr (assq 'name header)))
11940             (gnus-message 1 "Article %d is unsavable" article))
11941         ;; This is a real article.
11942         (save-window-excursion
11943           (gnus-summary-select-article decode decode nil article)
11944           (gnus-summary-goto-subject article))
11945         (with-current-buffer save-buffer
11946           (erase-buffer)
11947           (insert-buffer-substring (if decode
11948                                        gnus-article-buffer
11949                                      gnus-original-article-buffer)))
11950         (setq file (gnus-article-save save-buffer file num))
11951         (gnus-summary-remove-process-mark article)
11952         (unless not-saved
11953           (gnus-summary-set-saved-mark article)))
11954       (when saver2
11955         (setq gnus-default-article-saver saver2
11956               saver2 nil)))
11957     (gnus-kill-buffer save-buffer)
11958     (gnus-summary-position-point)
11959     (gnus-set-mode-line 'summary)
11960     n))
11961
11962 (defun gnus-summary-pipe-output (&optional n sym)
11963   "Pipe the current article to a subprocess.
11964 If N is a positive number, pipe the N next articles.
11965 If N is a negative number, pipe the N previous articles.
11966 If N is nil and any articles have been marked with the process mark,
11967 pipe those articles instead.
11968 The default command to which articles are piped is specified by the
11969 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11970 will be prompted for the command.
11971
11972 The properties `:decode' and `:headers' that are put to the function
11973 symbol `gnus-summary-save-in-pipe' control whether this function
11974 decodes articles and what headers to keep (see the doc string for the
11975 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11976 is neither omitted nor the symbol `r', force including all headers
11977 regardless of the `:headers' property.  If it is the symbol `r',
11978 articles that are not decoded and include all headers will be piped
11979 no matter what the properties `:decode' and `:headers' are."
11980   (interactive (gnus-interactive "P\ny"))
11981   (require 'gnus-art)
11982   (let* ((articles (gnus-summary-work-articles n))
11983          (result-buffer "*Shell Command Output*")
11984          (all-headers (not (memq sym '(nil r))))
11985          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11986          (raw (eq sym 'r))
11987          (headers (get 'gnus-summary-save-in-pipe :headers))
11988          command result)
11989     (unless (numberp (car articles))
11990       (error "No article to pipe"))
11991     (setq command (gnus-read-shell-command
11992                    (concat "Shell command on "
11993                            (if (cdr articles)
11994                                (format "these %d articles" (length articles))
11995                              "this article")
11996                            ": ")
11997                    gnus-summary-pipe-output-default-command))
11998     (when (string-equal command "")
11999       (error "A command is required"))
12000     (when all-headers
12001       (put 'gnus-summary-save-in-pipe :headers nil))
12002     (unwind-protect
12003         (while articles
12004           (gnus-summary-goto-subject (pop articles))
12005           (save-window-excursion (gnus-summary-save-in-pipe command raw))
12006           (when (and (get-buffer result-buffer)
12007                      (not (zerop (buffer-size (get-buffer result-buffer)))))
12008             (setq result (concat result (with-current-buffer result-buffer
12009                                           (buffer-string))))))
12010       (put 'gnus-summary-save-in-pipe :headers headers))
12011     (unless (zerop (length result))
12012       (if (with-current-buffer (get-buffer-create result-buffer)
12013             (erase-buffer)
12014             (insert result)
12015             (prog1
12016                 (and (= (count-lines (point-min) (point)) 1)
12017                      (progn
12018                        (end-of-line 0)
12019                        (<= (current-column)
12020                            (window-width (minibuffer-window)))))
12021               (goto-char (point-min))))
12022           (message "%s" (substring result 0 -1))
12023         (message nil)
12024         (gnus-configure-windows 'pipe)))))
12025
12026 (defun gnus-summary-save-article-mail (&optional arg)
12027   "Append the current article to a Unix mail box file.
12028 If N is a positive number, save the N next articles.
12029 If N is a negative number, save the N previous articles.
12030 If N is nil and any articles have been marked with the process mark,
12031 save those articles instead."
12032   (interactive "P")
12033   (require 'gnus-art)
12034   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12035     (gnus-summary-save-article arg)))
12036
12037 (defun gnus-summary-save-article-rmail (&optional arg)
12038   "Append the current article to an rmail file.
12039 If N is a positive number, save the N next articles.
12040 If N is a negative number, save the N previous articles.
12041 If N is nil and any articles have been marked with the process mark,
12042 save those articles instead."
12043   (interactive "P")
12044   (require 'gnus-art)
12045   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12046     (gnus-summary-save-article arg)))
12047
12048 (defun gnus-summary-save-article-file (&optional arg)
12049   "Append the current article to a file.
12050 If N is a positive number, save the N next articles.
12051 If N is a negative number, save the N previous articles.
12052 If N is nil and any articles have been marked with the process mark,
12053 save those articles instead."
12054   (interactive "P")
12055   (require 'gnus-art)
12056   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12057     (gnus-summary-save-article arg)))
12058
12059 (defun gnus-summary-write-article-file (&optional arg)
12060   "Write the current article to a file, deleting the previous file.
12061 If N is a positive number, save the N next articles.
12062 If N is a negative number, save the N previous articles.
12063 If N is nil and any articles have been marked with the process mark,
12064 save those articles instead."
12065   (interactive "P")
12066   (require 'gnus-art)
12067   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
12068     (gnus-summary-save-article arg)))
12069
12070 (defun gnus-summary-save-article-body-file (&optional arg)
12071   "Append the current article body to a file.
12072 If N is a positive number, save the N next articles.
12073 If N is a negative number, save the N previous articles.
12074 If N is nil and any articles have been marked with the process mark,
12075 save those articles instead."
12076   (interactive "P")
12077   (require 'gnus-art)
12078   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12079     (gnus-summary-save-article arg)))
12080
12081 (defun gnus-summary-write-article-body-file (&optional arg)
12082   "Write the current article body to a file, deleting the previous file.
12083 If N is a positive number, save the N next articles.
12084 If N is a negative number, save the N previous articles.
12085 If N is nil and any articles have been marked with the process mark,
12086 save those articles instead."
12087   (interactive "P")
12088   (require 'gnus-art)
12089   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
12090     (gnus-summary-save-article arg)))
12091
12092 (defun gnus-summary-muttprint (&optional arg)
12093   "Print the current article using Muttprint.
12094 If N is a positive number, save the N next articles.
12095 If N is a negative number, save the N previous articles.
12096 If N is nil and any articles have been marked with the process mark,
12097 save those articles instead."
12098   (interactive "P")
12099   (require 'gnus-art)
12100   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
12101     (gnus-summary-save-article arg t)))
12102
12103 (defun gnus-summary-pipe-message (program)
12104   "Pipe the current article through PROGRAM."
12105   (interactive "sProgram: ")
12106   (gnus-summary-select-article)
12107   (let ((mail-header-separator ""))
12108     (gnus-eval-in-buffer-window gnus-article-buffer
12109       (save-restriction
12110         (widen)
12111         (let ((start (window-start))
12112               (inhibit-read-only t))
12113           (message-pipe-buffer-body program)
12114           (set-window-start (get-buffer-window (current-buffer)) start))))))
12115
12116 (defun gnus-get-split-value (methods)
12117   "Return a value based on the split METHODS."
12118   (let (split-name method result match)
12119     (when methods
12120       (with-current-buffer gnus-original-article-buffer
12121         (save-restriction
12122           (nnheader-narrow-to-headers)
12123           (while (and methods (not split-name))
12124             (goto-char (point-min))
12125             (setq method (pop methods))
12126             (setq match (car method))
12127             (when (cond
12128                    ((stringp match)
12129                     ;; Regular expression.
12130                     (ignore-errors
12131                       (re-search-forward match nil t)))
12132                    ((functionp match)
12133                     ;; Function.
12134                     (save-restriction
12135                       (widen)
12136                       (setq result (funcall match gnus-newsgroup-name))))
12137                    ((consp match)
12138                     ;; Form.
12139                     (save-restriction
12140                       (widen)
12141                       (setq result (eval match)))))
12142               (setq split-name (cdr method))
12143               (cond ((stringp result)
12144                      (push (expand-file-name
12145                             result gnus-article-save-directory)
12146                            split-name))
12147                     ((consp result)
12148                      (setq split-name (append result split-name)))))))))
12149     (nreverse split-name)))
12150
12151 (defun gnus-valid-move-group-p (group)
12152   (and (symbolp group)
12153        (boundp group)
12154        (symbol-name group)
12155        (symbol-value group)
12156        (gnus-get-function (gnus-find-method-for-group
12157                            (symbol-name group)) 'request-accept-article t)))
12158
12159 (defun gnus-read-move-group-name (prompt default articles prefix)
12160   "Read a group name."
12161   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12162          (minibuffer-confirm-incomplete nil) ; XEmacs
12163          (prom
12164           (format "%s %s to"
12165                   prompt
12166                   (if (> (length articles) 1)
12167                       (format "these %d articles" (length articles))
12168                     "this article")))
12169          (to-newsgroup
12170           (cond
12171            ((null split-name)
12172             (gnus-group-completing-read
12173              prom
12174              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12175              nil prefix nil default))
12176            ((= 1 (length split-name))
12177             (gnus-group-completing-read
12178              prom
12179              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12180              nil prefix 'gnus-group-history (car split-name)))
12181            (t
12182             (gnus-completing-read
12183              prom (nreverse split-name) nil nil 'gnus-group-history))))
12184          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12185          encoded)
12186     (when to-newsgroup
12187       (if (or (string= to-newsgroup "")
12188               (string= to-newsgroup prefix))
12189           (setq to-newsgroup default))
12190       (unless to-newsgroup
12191         (error "No group name entered"))
12192       (setq encoded (mm-encode-coding-string
12193                      to-newsgroup
12194                      (gnus-group-name-charset to-method to-newsgroup)))
12195       (or (gnus-active encoded)
12196           (gnus-activate-group encoded nil nil to-method)
12197           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12198                                      to-newsgroup))
12199               (or (and (gnus-request-create-group encoded to-method)
12200                        (gnus-activate-group encoded nil nil to-method)
12201                        (gnus-subscribe-group encoded))
12202                   (error "Couldn't create group %s" to-newsgroup)))
12203           (error "No such group: %s" to-newsgroup))
12204       encoded)))
12205
12206 (defvar gnus-summary-save-parts-counter)
12207 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12208
12209 (defun gnus-summary-save-parts (type dir n &optional reverse)
12210   "Save parts matching TYPE to DIR.
12211 If REVERSE, save parts that do not match TYPE."
12212   (interactive
12213    (list (read-string "Save parts of type: "
12214                       (or (car gnus-summary-save-parts-type-history)
12215                           gnus-summary-save-parts-default-mime)
12216                       'gnus-summary-save-parts-type-history)
12217          (setq gnus-summary-save-parts-last-directory
12218                (read-directory-name "Save to directory: "
12219                                     gnus-summary-save-parts-last-directory
12220                                     nil t))
12221          current-prefix-arg))
12222   (gnus-summary-iterate n
12223     (let ((gnus-display-mime-function nil)
12224           gnus-article-prepare-hook
12225           gnus-article-decode-hook
12226           gnus-display-mime-function
12227           gnus-break-pages
12228           (gnus-inhibit-treatment t))
12229       (gnus-summary-select-article))
12230     (with-current-buffer gnus-article-buffer
12231       (let ((handles (or gnus-article-mime-handles
12232                          (mm-dissect-buffer nil gnus-article-loose-mime)
12233                          (and gnus-article-emulate-mime
12234                               (mm-uu-dissect))))
12235             (gnus-summary-save-parts-counter 1))
12236         (when handles
12237           (gnus-summary-save-parts-1 type dir handles reverse)
12238           (unless gnus-article-mime-handles ;; Don't destroy this case.
12239             (mm-destroy-parts handles)))))))
12240
12241 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12242   (if (stringp (car handle))
12243       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12244               (cdr handle))
12245     (when (if reverse
12246               (not (string-match type (mm-handle-media-type handle)))
12247             (string-match type (mm-handle-media-type handle)))
12248       (let ((file (expand-file-name
12249                    (gnus-map-function
12250                     mm-file-name-rewrite-functions
12251                     (file-name-nondirectory
12252                      (or
12253                       (mm-handle-filename handle)
12254                       (format "%s.%d.%d" gnus-newsgroup-name
12255                               (cdr gnus-article-current)
12256                               gnus-summary-save-parts-counter))))
12257                    dir)))
12258         (incf gnus-summary-save-parts-counter)
12259         (unless (file-exists-p file)
12260           (mm-save-part-to-file handle file))))))
12261
12262 ;; Summary extract commands
12263
12264 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12265   (let ((inhibit-read-only t)
12266         (article (gnus-summary-article-number))
12267         after-article b e)
12268     (unless (gnus-summary-goto-subject article)
12269       (error "No such article: %d" article))
12270     (gnus-summary-position-point)
12271     ;; If all commands are to be bunched up on one line, we collect
12272     ;; them here.
12273     (unless gnus-view-pseudos-separately
12274       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12275             files action)
12276         (while ps
12277           (setq action (cdr (assq 'action (car ps))))
12278           (setq files (list (cdr (assq 'name (car ps)))))
12279           (while (and ps (cdr ps)
12280                       (string= (or action "1")
12281                                (or (cdr (assq 'action (cadr ps))) "2")))
12282             (push (cdr (assq 'name (cadr ps))) files)
12283             (setcdr ps (cddr ps)))
12284           (when files
12285             (when (not (string-match "%s" action))
12286               (push " " files))
12287             (push " " files)
12288             (when (assq 'execute (car ps))
12289               (setcdr (assq 'execute (car ps))
12290                       (funcall (if (string-match "%s" action)
12291                                    'format 'concat)
12292                                action
12293                                (mapconcat
12294                                 (lambda (f)
12295                                   (if (equal f " ")
12296                                       f
12297                                     (shell-quote-argument f)))
12298                                 files " ")))))
12299           (setq ps (cdr ps)))))
12300     (if (and gnus-view-pseudos (not not-view))
12301         (while pslist
12302           (when (assq 'execute (car pslist))
12303             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12304                                   (eq gnus-view-pseudos 'not-confirm)))
12305           (setq pslist (cdr pslist)))
12306       (save-excursion
12307         (while pslist
12308           (setq after-article (or (cdr (assq 'article (car pslist)))
12309                                   (gnus-summary-article-number)))
12310           (gnus-summary-goto-subject after-article)
12311           (forward-line 1)
12312           (setq b (point))
12313           (insert "    " (file-name-nondirectory
12314                           (cdr (assq 'name (car pslist))))
12315                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12316           (setq e (point))
12317           (forward-line -1)             ; back to `b'
12318           (gnus-add-text-properties
12319            b (1- e) (list 'gnus-number gnus-reffed-article-number
12320                           gnus-mouse-face-prop gnus-mouse-face))
12321           (gnus-data-enter
12322            after-article gnus-reffed-article-number
12323            gnus-unread-mark b (car pslist) 0 (- e b))
12324           (setq gnus-newsgroup-unreads
12325                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12326                                          gnus-reffed-article-number))
12327           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12328           (setq pslist (cdr pslist)))))))
12329
12330 (defun gnus-pseudos< (p1 p2)
12331   (let ((c1 (cdr (assq 'action p1)))
12332         (c2 (cdr (assq 'action p2))))
12333     (and c1 c2 (string< c1 c2))))
12334
12335 (defun gnus-request-pseudo-article (props)
12336   (cond ((assq 'execute props)
12337          (gnus-execute-command (cdr (assq 'execute props)))))
12338   (let ((gnus-current-article (gnus-summary-article-number)))
12339     (gnus-run-hooks 'gnus-mark-article-hook)))
12340
12341 (defun gnus-execute-command (command &optional automatic)
12342   (save-excursion
12343     (gnus-article-setup-buffer)
12344     (set-buffer gnus-article-buffer)
12345     (setq buffer-read-only nil)
12346     (let ((command (if automatic command
12347                      (read-string "Command: " (cons command 0)))))
12348       (erase-buffer)
12349       (insert "$ " command "\n\n")
12350       (if gnus-view-pseudo-asynchronously
12351           (start-process "gnus-execute" (current-buffer) shell-file-name
12352                          shell-command-switch command)
12353         (call-process shell-file-name nil t nil
12354                       shell-command-switch command)))))
12355
12356 ;; Summary kill commands.
12357
12358 (defun gnus-summary-edit-global-kill (article)
12359   "Edit the \"global\" kill file."
12360   (interactive (list (gnus-summary-article-number)))
12361   (gnus-group-edit-global-kill article))
12362
12363 (defun gnus-summary-edit-local-kill ()
12364   "Edit a local kill file applied to the current newsgroup."
12365   (interactive)
12366   (setq gnus-current-headers (gnus-summary-article-header))
12367   (gnus-group-edit-local-kill
12368    (gnus-summary-article-number) gnus-newsgroup-name))
12369
12370 ;;; Header reading.
12371
12372 (defun gnus-read-header (id &optional header)
12373   "Read the headers of article ID and enter them into the Gnus system."
12374   (let ((group gnus-newsgroup-name)
12375         (gnus-override-method
12376          (or
12377           gnus-override-method
12378           (and (gnus-news-group-p gnus-newsgroup-name)
12379                (car (gnus-refer-article-methods)))))
12380         where)
12381     ;; First we check to see whether the header in question is already
12382     ;; fetched.
12383     (if (stringp id)
12384         ;; This is a Message-ID.
12385         (setq header (or header (gnus-id-to-header id)))
12386       ;; This is an article number.
12387       (setq header (or header (gnus-summary-article-header id))))
12388     (if (and header
12389              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12390         ;; We have found the header.
12391         header
12392       ;; We have to really fetch the header to this article.
12393       (with-current-buffer nntp-server-buffer
12394         (when (setq where (gnus-request-head id group))
12395           (nnheader-fold-continuation-lines)
12396           (goto-char (point-max))
12397           (insert ".\n")
12398           (goto-char (point-min))
12399           (insert "211 ")
12400           (princ (cond
12401                   ((numberp id) id)
12402                   ((cdr where) (cdr where))
12403                   (header (mail-header-number header))
12404                   (t gnus-reffed-article-number))
12405                  (current-buffer))
12406           (insert " Article retrieved.\n"))
12407         (if (or (not where)
12408                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12409             ()                          ; Malformed head.
12410           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12411             (when (and (stringp id)
12412                        (or
12413                         (not (string= (gnus-group-real-name group)
12414                                       (car where)))
12415                         (not (gnus-server-equal gnus-override-method
12416                                                 (gnus-group-method group)))))
12417               ;; If we fetched by Message-ID and the article came from
12418               ;; a different group (or server), we fudge some bogus
12419               ;; article numbers for this article.
12420               (mail-header-set-number header gnus-reffed-article-number))
12421             (with-current-buffer gnus-summary-buffer
12422               (decf gnus-reffed-article-number)
12423               (gnus-remove-header (mail-header-number header))
12424               (push header gnus-newsgroup-headers)
12425               (setq gnus-current-headers header)
12426               (push (mail-header-number header) gnus-newsgroup-limit)))
12427           header)))))
12428
12429 (defun gnus-remove-header (number)
12430   "Remove header NUMBER from `gnus-newsgroup-headers'."
12431   (if (and gnus-newsgroup-headers
12432            (= number (mail-header-number (car gnus-newsgroup-headers))))
12433       (pop gnus-newsgroup-headers)
12434     (let ((headers gnus-newsgroup-headers))
12435       (while (and (cdr headers)
12436                   (not (= number (mail-header-number (cadr headers)))))
12437         (pop headers))
12438       (when (cdr headers)
12439         (setcdr headers (cddr headers))))))
12440
12441 ;;;
12442 ;;; summary highlights
12443 ;;;
12444
12445 (defun gnus-highlight-selected-summary ()
12446   "Highlight selected article in summary buffer."
12447   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12448   (when gnus-summary-selected-face
12449     (save-excursion
12450       (let* ((beg (point-at-bol))
12451              (end (point-at-eol))
12452              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12453              (from (if (get-text-property beg gnus-mouse-face-prop)
12454                        beg
12455                      (or (next-single-property-change
12456                           beg gnus-mouse-face-prop nil end)
12457                          beg)))
12458              (to
12459               (if (= from end)
12460                   (- from 2)
12461                 (or (next-single-property-change
12462                      from gnus-mouse-face-prop nil end)
12463                     end))))
12464         ;; If no mouse-face prop on line we will have to = from = end,
12465         ;; so we highlight the entire line instead.
12466         (when (= (+ to 2) from)
12467           (setq from beg)
12468           (setq to end))
12469         (if gnus-newsgroup-selected-overlay
12470             ;; Move old overlay.
12471             (gnus-move-overlay
12472              gnus-newsgroup-selected-overlay from to (current-buffer))
12473           ;; Create new overlay.
12474           (gnus-overlay-put
12475            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12476            'face gnus-summary-selected-face))))))
12477
12478 (defvar gnus-summary-highlight-line-cached nil)
12479 (defvar gnus-summary-highlight-line-trigger nil)
12480
12481 (defun gnus-summary-highlight-line-0 ()
12482   (if (and (eq gnus-summary-highlight-line-trigger
12483                gnus-summary-highlight)
12484            gnus-summary-highlight-line-cached)
12485       gnus-summary-highlight-line-cached
12486     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12487           gnus-summary-highlight-line-cached
12488           (let* ((cond (list 'cond))
12489                  (c cond)
12490                  (list gnus-summary-highlight))
12491             (while list
12492               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12493                               nil))
12494               (setq c (cdr c)
12495                     list (cdr list)))
12496             (gnus-byte-compile (list 'lambda nil cond))))))
12497
12498 (defun gnus-summary-highlight-line ()
12499   "Highlight current line according to `gnus-summary-highlight'."
12500   (let* ((beg (point-at-bol))
12501          (article (or (gnus-summary-article-number) gnus-current-article))
12502          (score (or (cdr (assq article
12503                                gnus-newsgroup-scored))
12504                     gnus-summary-default-score 0))
12505          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12506          (inhibit-read-only t)
12507          (default gnus-summary-default-score)
12508          (default-high gnus-summary-default-high-score)
12509          (default-low gnus-summary-default-low-score)
12510          (uncached (and gnus-summary-use-undownloaded-faces
12511                         (memq article gnus-newsgroup-undownloaded)
12512                         (not (memq article gnus-newsgroup-cached)))))
12513     (let ((face (funcall (gnus-summary-highlight-line-0))))
12514       (unless (eq face (get-text-property beg 'face))
12515         (gnus-put-text-property-excluding-characters-with-faces
12516          beg (point-at-eol) 'face
12517          (setq face (if (boundp face) (symbol-value face) face)))
12518         (when gnus-summary-highlight-line-function
12519           (funcall gnus-summary-highlight-line-function article face))))))
12520
12521 (defun gnus-update-read-articles (group unread &optional compute)
12522   "Update the list of read articles in GROUP.
12523 UNREAD is a sorted list."
12524   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12525         (info (gnus-get-info group))
12526         (prev 1)
12527         read)
12528     (if (or (not info) (not active))
12529         ;; There is no info on this group if it was, in fact,
12530         ;; killed.  Gnus stores no information on killed groups, so
12531         ;; there's nothing to be done.
12532         ;; One could store the information somewhere temporarily,
12533         ;; perhaps...  Hmmm...
12534         ()
12535       ;; Remove any negative articles numbers.
12536       (while (and unread (< (car unread) 0))
12537         (setq unread (cdr unread)))
12538       ;; Remove any expired article numbers
12539       (while (and unread (< (car unread) (car active)))
12540         (setq unread (cdr unread)))
12541       ;; Compute the ranges of read articles by looking at the list of
12542       ;; unread articles.
12543       (while unread
12544         (when (/= (car unread) prev)
12545           (push (if (= prev (1- (car unread))) prev
12546                   (cons prev (1- (car unread))))
12547                 read))
12548         (setq prev (1+ (car unread)))
12549         (setq unread (cdr unread)))
12550       (when (<= prev (cdr active))
12551         (push (cons prev (cdr active)) read))
12552       (setq read (if (> (length read) 1) (nreverse read) read))
12553       (if compute
12554           read
12555         (save-excursion
12556           (let (setmarkundo)
12557             ;; Propagate the read marks to the backend.
12558             (when (and (or gnus-propagate-marks
12559                            (gnus-method-option-p
12560                             (gnus-find-method-for-group group)
12561                             'server-marks))
12562                        (gnus-check-backend-function 'request-set-mark group))
12563               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12564                     (add (gnus-remove-from-range read (gnus-info-read info))))
12565                 (when (or add del)
12566                   (unless (gnus-check-group group)
12567                     (error "Can't open server for %s" group))
12568                   (gnus-request-set-mark
12569                    group (delq nil (list (if add (list add 'add '(read)))
12570                                          (if del (list del 'del '(read))))))
12571                   (setq setmarkundo
12572                         `(gnus-request-set-mark
12573                           ,group
12574                           ',(delq nil (list
12575                                        (if del (list del 'add '(read)))
12576                                        (if add (list add 'del '(read))))))))))
12577             (set-buffer gnus-group-buffer)
12578             (gnus-undo-register
12579               `(progn
12580                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12581                  (gnus-info-set-read ',info ',(gnus-info-read info))
12582                  (gnus-get-unread-articles-in-group ',info
12583                                                     (gnus-active ,group))
12584                  (gnus-group-update-group ,group t)
12585                  ,setmarkundo))))
12586         ;; Enter this list into the group info.
12587         (gnus-info-set-read info read)
12588         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12589         (gnus-get-unread-articles-in-group info (gnus-active group))
12590         t))))
12591
12592 (defun gnus-offer-save-summaries ()
12593   "Offer to save all active summary buffers."
12594   (let (buffers)
12595     ;; Go through all buffers and find all summaries.
12596     (dolist (buffer (buffer-list))
12597       (when (and (setq buffer (buffer-name buffer))
12598                  (string-match "Summary" buffer)
12599                  (with-current-buffer buffer
12600                    ;; We check that this is, indeed, a summary buffer.
12601                    (and (eq major-mode 'gnus-summary-mode)
12602                         ;; Also make sure this isn't bogus.
12603                         gnus-newsgroup-prepared
12604                         ;; Also make sure that this isn't a
12605                         ;; dead summary buffer.
12606                         (not gnus-dead-summary-mode))))
12607         (push buffer buffers)))
12608     ;; Go through all these summary buffers and offer to save them.
12609     (when buffers
12610       (save-excursion
12611         (if (eq gnus-interactive-exit 'quiet)
12612             (dolist (buffer buffers)
12613               (switch-to-buffer buffer)
12614               (gnus-summary-exit))
12615           (map-y-or-n-p
12616            "Update summary buffer %s? "
12617            (lambda (buf)
12618              (switch-to-buffer buf)
12619              (gnus-summary-exit))
12620            buffers))))))
12621
12622 (defun gnus-summary-setup-default-charset ()
12623   "Setup newsgroup default charset."
12624   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12625       (setq gnus-newsgroup-charset nil)
12626     (let* ((ignored-charsets
12627             (or gnus-newsgroup-ephemeral-ignored-charsets
12628                 (append
12629                  (and gnus-newsgroup-name
12630                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12631                  gnus-newsgroup-ignored-charsets))))
12632       (setq gnus-newsgroup-charset
12633             (or gnus-newsgroup-ephemeral-charset
12634                 (and gnus-newsgroup-name
12635                      (gnus-parameter-charset gnus-newsgroup-name))
12636                 gnus-default-charset))
12637       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12638            ignored-charsets))))
12639
12640 ;;;
12641 ;;; Mime Commands
12642 ;;;
12643
12644 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12645   "Display the current article buffer fully MIME-buttonized.
12646 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12647 treated as multipart/mixed."
12648   (interactive "P")
12649   (require 'gnus-art)
12650   (let ((gnus-unbuttonized-mime-types nil)
12651         (gnus-mime-display-multipart-as-mixed show-all-parts))
12652     (gnus-summary-show-article)))
12653
12654 (defun gnus-summary-repair-multipart (article)
12655   "Add a Content-Type header to a multipart article without one."
12656   (interactive (list (gnus-summary-article-number)))
12657   (gnus-with-article article
12658     (message-narrow-to-head)
12659     (message-remove-header "Mime-Version")
12660     (goto-char (point-max))
12661     (insert "Mime-Version: 1.0\n")
12662     (widen)
12663     (when (search-forward "\n--" nil t)
12664       (let ((separator (buffer-substring (point) (point-at-eol))))
12665         (message-narrow-to-head)
12666         (message-remove-header "Content-Type")
12667         (goto-char (point-max))
12668         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12669                         separator))
12670         (widen))))
12671   (let (gnus-mark-article-hook)
12672     (gnus-summary-select-article t t nil article)))
12673
12674 (defun gnus-summary-toggle-display-buttonized ()
12675   "Toggle the buttonizing of the article buffer."
12676   (interactive)
12677   (require 'gnus-art)
12678   (if (setq gnus-inhibit-mime-unbuttonizing
12679             (not gnus-inhibit-mime-unbuttonizing))
12680       (let ((gnus-unbuttonized-mime-types nil))
12681         (gnus-summary-show-article))
12682     (gnus-summary-show-article)))
12683
12684 ;;;
12685 ;;; Generic summary marking commands
12686 ;;;
12687
12688 (defvar gnus-summary-marking-alist
12689   '((read gnus-del-mark "d")
12690     (unread gnus-unread-mark "u")
12691     (ticked gnus-ticked-mark "!")
12692     (dormant gnus-dormant-mark "?")
12693     (expirable gnus-expirable-mark "e"))
12694   "An alist of names/marks/keystrokes.")
12695
12696 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12697 (defvar gnus-summary-mark-map)
12698
12699 (defun gnus-summary-make-all-marking-commands ()
12700   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12701   (dolist (elem gnus-summary-marking-alist)
12702     (apply 'gnus-summary-make-marking-command elem)))
12703
12704 (defun gnus-summary-make-marking-command (name mark keystroke)
12705   (let ((map (make-sparse-keymap)))
12706     (define-key gnus-summary-generic-mark-map keystroke map)
12707     (dolist (lway `((next "next" next nil "n")
12708                     (next-unread "next unread" next t "N")
12709                     (prev "previous" prev nil "p")
12710                     (prev-unread "previous unread" prev t "P")
12711                     (nomove "" nil nil ,keystroke)))
12712       (let ((func (gnus-summary-make-marking-command-1
12713                    mark (car lway) lway name)))
12714         (setq func (eval func))
12715         (define-key map (nth 4 lway) func)))))
12716
12717 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12718   `(defun ,(intern
12719             (format "gnus-summary-put-mark-as-%s%s"
12720                     name (if (eq way 'nomove)
12721                              ""
12722                            (concat "-" (symbol-name way)))))
12723      (n)
12724      ,(format
12725        "Mark the current article as %s%s.
12726 If N, the prefix, then repeat N times.
12727 If N is negative, move in reverse order.
12728 The difference between N and the actual number of articles marked is
12729 returned."
12730        name (cadr lway))
12731      (interactive "p")
12732      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12733
12734 (defun gnus-summary-generic-mark (n mark move unread)
12735   "Mark N articles with MARK."
12736   (unless (eq major-mode 'gnus-summary-mode)
12737     (error "This command can only be used in the summary buffer"))
12738   (gnus-summary-show-thread)
12739   (let ((nummove
12740          (cond
12741           ((eq move 'next) 1)
12742           ((eq move 'prev) -1)
12743           (t 0))))
12744     (if (zerop nummove)
12745         (setq n 1)
12746       (when (< n 0)
12747         (setq n (abs n)
12748               nummove (* -1 nummove))))
12749     (while (and (> n 0)
12750                 (gnus-summary-mark-article nil mark)
12751                 (zerop (gnus-summary-next-subject nummove unread t)))
12752       (setq n (1- n)))
12753     (when (/= 0 n)
12754       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12755     (gnus-summary-recenter)
12756     (gnus-summary-position-point)
12757     (gnus-set-mode-line 'summary)
12758     n))
12759
12760 (defun gnus-summary-insert-articles (articles)
12761   (when (setq articles
12762               (gnus-sorted-difference articles
12763                                       (mapcar (lambda (h)
12764                                                 (mail-header-number h))
12765                                               gnus-newsgroup-headers)))
12766     (setq gnus-newsgroup-headers
12767           (gnus-merge 'list
12768                       gnus-newsgroup-headers
12769                       (gnus-fetch-headers articles)
12770                       'gnus-article-sort-by-number))
12771     (setq gnus-newsgroup-articles
12772           (gnus-sorted-nunion gnus-newsgroup-articles articles))
12773     ;; Suppress duplicates?
12774     (when gnus-suppress-duplicates
12775       (gnus-dup-suppress-articles))
12776
12777     (if (and gnus-fetch-old-headers
12778              (eq gnus-headers-retrieved-by 'nov))
12779         ;; We might want to build some more threads first.
12780         (if (eq gnus-fetch-old-headers 'invisible)
12781             (gnus-build-all-threads)
12782           (gnus-build-old-threads))
12783       ;; Mark the inserted articles that are unread as unread.
12784       (setq gnus-newsgroup-unreads
12785             (gnus-sorted-nunion
12786              gnus-newsgroup-unreads
12787              (gnus-sorted-nintersection
12788               (gnus-list-of-unread-articles gnus-newsgroup-name)
12789               articles)))
12790       ;; Mark the inserted articles as selected so that the information
12791       ;; of the marks having been changed by a user may be updated when
12792       ;; exiting this group.  See `gnus-summary-update-info'.
12793       (dolist (art articles)
12794         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12795     ;; Let the Gnus agent mark articles as read.
12796     (when gnus-agent
12797       (gnus-agent-get-undownloaded-list))
12798     ;; Remove list identifiers from subject
12799     (gnus-summary-remove-list-identifiers)
12800     ;; First and last article in this newsgroup.
12801     (when gnus-newsgroup-headers
12802       (setq gnus-newsgroup-begin
12803             (mail-header-number (car gnus-newsgroup-headers))
12804             gnus-newsgroup-end
12805             (mail-header-number
12806              (gnus-last-element gnus-newsgroup-headers))))
12807     (when gnus-use-scoring
12808       (gnus-possibly-score-headers))))
12809
12810 (defun gnus-summary-insert-old-articles (&optional all)
12811   "Insert all old articles in this group.
12812 If ALL is non-nil, already read articles become readable.
12813 If ALL is a number, fetch this number of articles."
12814   (interactive "P")
12815   (prog1
12816       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12817             older len)
12818         (setq older
12819               ;; Some nntp servers lie about their active range.  When
12820               ;; this happens, the active range can be in the millions.
12821               ;; Use a compressed range to avoid creating a huge list.
12822               (gnus-range-difference (list gnus-newsgroup-active) old))
12823         (setq len (gnus-range-length older))
12824         (cond
12825          ((null older) nil)
12826          ((numberp all)
12827           (if (< all len)
12828               (let ((older-range (nreverse older)))
12829                 (setq older nil)
12830
12831                 (while (> all 0)
12832                   (let* ((r (pop older-range))
12833                          (min (if (numberp r) r (car r)))
12834                          (max (if (numberp r) r (cdr r))))
12835                     (while (and (<= min max)
12836                                 (> all 0))
12837                       (push max older)
12838                       (setq all (1- all)
12839                             max (1- max))))))
12840             (setq older (gnus-uncompress-range older))))
12841          (all
12842           (setq older (gnus-uncompress-range older)))
12843          (t
12844           (when (and (numberp gnus-large-newsgroup)
12845                    (> len gnus-large-newsgroup))
12846               (let* ((cursor-in-echo-area nil)
12847                      (initial (gnus-parameter-large-newsgroup-initial
12848                                gnus-newsgroup-name))
12849                      (input
12850                       (read-string
12851                        (format
12852                         "How many articles from %s (%s %d): "
12853                         (gnus-group-decoded-name gnus-newsgroup-name)
12854                         (if initial "max" "default")
12855                         len)
12856                        (if initial
12857                            (cons (number-to-string initial)
12858                                  0)))))
12859                 (unless (string-match "^[ \t]*$" input)
12860                   (setq all (string-to-number input))
12861                   (if (< all len)
12862                       (let ((older-range (nreverse older)))
12863                         (setq older nil)
12864
12865                         (while (> all 0)
12866                           (let* ((r (pop older-range))
12867                                  (min (if (numberp r) r (car r)))
12868                                  (max (if (numberp r) r (cdr r))))
12869                             (while (and (<= min max)
12870                                         (> all 0))
12871                               (push max older)
12872                               (setq all (1- all)
12873                                     max (1- max))))))))))
12874           (setq older (gnus-uncompress-range older))))
12875         (if (not older)
12876             (message "No old news.")
12877           (gnus-summary-insert-articles older)
12878           (gnus-summary-limit (gnus-sorted-nunion old older))))
12879     (gnus-summary-position-point)))
12880
12881 (defun gnus-summary-insert-new-articles ()
12882   "Insert all new articles in this group."
12883   (interactive)
12884   (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12885         (old-high gnus-newsgroup-highest)
12886         (nnmail-fetched-sources (list t))
12887         (new-active (gnus-activate-group gnus-newsgroup-name 'scan))
12888         i new)
12889     (unless new-active
12890       (error "Couldn't fetch new data"))
12891     (setq gnus-newsgroup-active (gnus-copy-sequence new-active))
12892     (setq i (cdr gnus-newsgroup-active)
12893           gnus-newsgroup-highest i)
12894     (while (> i old-high)
12895       (push i new)
12896       (decf i))
12897     (if (not new)
12898         (message "No gnus is bad news")
12899       (gnus-summary-insert-articles new)
12900       (setq gnus-newsgroup-unreads
12901             (gnus-sorted-nunion gnus-newsgroup-unreads new))
12902       (gnus-summary-limit (gnus-sorted-nunion old new))))
12903   (gnus-summary-position-point))
12904
12905 ;;; Bookmark support for Gnus.
12906 (declare-function bookmark-make-record-default
12907                   "bookmark" (&optional no-file no-context posn))
12908 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12909 (declare-function bookmark-default-handler "bookmark" (bmk))
12910 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12911 (defvar bookmark-yank-point)
12912 (defvar bookmark-current-buffer)
12913
12914 (defun gnus-summary-bookmark-make-record ()
12915   "Make a bookmark entry for a Gnus summary buffer."
12916   (let (pos buf)
12917     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12918       (save-restriction              ; FIXME is it necessary to widen?
12919         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12920       (setq buf "art") ; We are recording bookmark from article buffer.
12921       (setq bookmark-yank-point (point))
12922       (setq bookmark-current-buffer (current-buffer))
12923       (gnus-article-show-summary))      ; Go back in summary buffer.
12924     ;; We are now recording bookmark from summary buffer.
12925     (unless buf (setq buf "sum"))
12926     (let* ((subject (elt (gnus-summary-article-header) 1))
12927            (grp     (car gnus-article-current))
12928            (art     (cdr gnus-article-current))
12929            (head    (gnus-summary-article-header art))
12930            (id      (mail-header-id head)))
12931       `(,subject
12932         ,@(condition-case nil
12933               (bookmark-make-record-default 'no-file 'no-context pos)
12934             (wrong-number-of-arguments
12935              (bookmark-make-record-default 'point-only)))
12936         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12937         (group . ,grp) (article . ,art)
12938         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12939
12940 ;;;###autoload
12941 (defun gnus-summary-bookmark-jump (bookmark)
12942   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12943 BOOKMARK is a bookmark name or a bookmark record."
12944   (let ((group    (bookmark-prop-get bookmark 'group))
12945         (article  (bookmark-prop-get bookmark 'article))
12946         (id       (bookmark-prop-get bookmark 'message-id))
12947         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12948     (gnus-fetch-group group (list article))
12949     (gnus-summary-insert-cached-articles)
12950     (gnus-summary-goto-article id nil 'force)
12951     ;; FIXME we have to wait article buffer is ready (only large buffer)
12952     ;; Is there a better solution to know that?
12953     ;; If we don't wait `bookmark-default-handler' will have no chance
12954     ;; to set position. However there is no error, just wrong pos.
12955     (sit-for 1)
12956     (when (string= buf "Gnus-art")
12957       (other-window 1))
12958     (bookmark-default-handler
12959      `(""
12960        (buffer . ,(current-buffer))
12961        . ,(bookmark-get-bookmark-record bookmark)))))
12962
12963 (gnus-summary-make-all-marking-commands)
12964
12965 (gnus-ems-redefine)
12966
12967 (provide 'gnus-sum)
12968
12969 (run-hooks 'gnus-sum-load-hook)
12970
12971 ;; Local Variables:
12972 ;; coding: iso-8859-1
12973 ;; End:
12974
12975 ;;; gnus-sum.el ends here