New command to ASCII-fy the article buffer, based on org-entities.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; For Emacs <22.2 and XEmacs.
29 (eval-and-compile
30   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31 (eval-when-compile
32   (require 'cl))
33 (eval-when-compile
34   (when (featurep 'xemacs)
35     (require 'easy-mmode))) ; for `define-minor-mode'
36
37 (defvar tool-bar-mode)
38 (defvar gnus-tmp-header)
39
40 (require 'gnus)
41 (require 'gnus-group)
42 (require 'gnus-spec)
43 (require 'gnus-range)
44 (require 'gnus-int)
45 (require 'gnus-undo)
46 (require 'gnus-util)
47 (require 'gmm-utils)
48 (require 'mm-decode)
49 (require 'nnoo)
50
51 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
52 (autoload 'gnus-cache-write-active "gnus-cache")
53 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
54 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
55 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
56 (autoload 'mm-uu-dissect "mm-uu")
57 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
58   "Deuglify broken Outlook (Express) articles and redisplay."
59   t)
60 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
61 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
62 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
63
64 (defcustom gnus-kill-summary-on-exit t
65   "*If non-nil, kill the summary buffer when you exit from it.
66 If nil, the summary will become a \"*Dead Summary*\" buffer, and
67 it will be killed sometime later."
68   :group 'gnus-summary-exit
69   :type 'boolean)
70
71 (defcustom gnus-summary-next-group-on-exit t
72   "If non-nil, go to the next unread newsgroup on summary exit.
73 See `gnus-group-goto-unread'."
74   :link '(custom-manual "(gnus)Group Maneuvering")
75   :group 'gnus-summary-exit
76   :version "23.1" ;; No Gnus
77   :type 'boolean)
78
79 (defcustom gnus-summary-stop-at-end-of-message nil
80   "If non-nil, don't select the next message when using `SPC'."
81   :link '(custom-manual "(gnus)Group Maneuvering")
82   :group 'gnus-summary-maneuvering
83   :version "24.1"
84   :type 'boolean)
85
86 (defcustom gnus-fetch-old-headers nil
87   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
88 If an unread article in the group refers to an older, already
89 read (or just marked as read) article, the old article will not
90 normally be displayed in the Summary buffer.  If this variable is
91 t, Gnus will attempt to grab the headers to the old articles, and
92 thereby build complete threads.  If it has the value `some', all
93 old headers will be fetched but only enough headers to connect
94 otherwise loose threads will be displayed.  This variable can
95 also be a number.  In that case, no more than that number of old
96 headers will be fetched.  If it has the value `invisible', all
97 old headers will be fetched, but none will be displayed.
98
99 The server has to support NOV for any of this to work.
100
101 This feature can seriously impact performance it ignores all
102 locally cached header entries.  Setting it to t for groups for a
103 server that doesn't expire articles (such as news.gmane.org),
104 leads to very slow summary generation."
105   :group 'gnus-thread
106   :type '(choice (const :tag "off" nil)
107                  (const :tag "on" t)
108                  (const some)
109                  (const invisible)
110                  number
111                  (sexp :menu-tag "other" t)))
112
113 (defcustom gnus-refer-thread-limit 500
114   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
115 If t, fetch all the available old headers."
116   :group 'gnus-thread
117   :type '(choice number
118                  (sexp :menu-tag "other" t)))
119
120 (defcustom gnus-summary-make-false-root 'adopt
121   "*nil means that Gnus won't gather loose threads.
122 If the root of a thread has expired or been read in a previous
123 session, the information necessary to build a complete thread has been
124 lost.  Instead of having many small sub-threads from this original thread
125 scattered all over the summary buffer, Gnus can gather them.
126
127 If non-nil, Gnus will try to gather all loose sub-threads from an
128 original thread into one large thread.
129
130 If this variable is non-nil, it should be one of `none', `adopt',
131 `dummy' or `empty'.
132
133 If this variable is `none', Gnus will not make a false root, but just
134 present the sub-threads after another.
135 If this variable is `dummy', Gnus will create a dummy root that will
136 have all the sub-threads as children.
137 If this variable is `adopt', Gnus will make one of the \"children\"
138 the parent and mark all the step-children as such.
139 If this variable is `empty', the \"children\" are printed with empty
140 subject fields.  (Or rather, they will be printed with a string
141 given by the `gnus-summary-same-subject' variable.)"
142   :group 'gnus-thread
143   :type '(choice (const :tag "off" nil)
144                  (const none)
145                  (const dummy)
146                  (const adopt)
147                  (const empty)))
148
149 (defcustom gnus-summary-make-false-root-always nil
150   "Always make a false dummy root."
151   :version "22.1"
152   :group 'gnus-thread
153   :type 'boolean)
154
155 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
156   "*A regexp to match subjects to be excluded from loose thread gathering.
157 As loose thread gathering is done on subjects only, that means that
158 there can be many false gatherings performed.  By rooting out certain
159 common subjects, gathering might become saner."
160   :group 'gnus-thread
161   :type 'regexp)
162
163 (defcustom gnus-summary-gather-subject-limit nil
164   "*Maximum length of subject comparisons when gathering loose threads.
165 Use nil to compare full subjects.  Setting this variable to a low
166 number will help gather threads that have been corrupted by
167 newsreaders chopping off subject lines, but it might also mean that
168 unrelated articles that have subject that happen to begin with the
169 same few characters will be incorrectly gathered.
170
171 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
172 comparing subjects."
173   :group 'gnus-thread
174   :type '(choice (const :tag "off" nil)
175                  (const fuzzy)
176                  (sexp :menu-tag "on" t)))
177
178 (defcustom gnus-simplify-subject-functions nil
179   "List of functions taking a string argument that simplify subjects.
180 The functions are applied recursively.
181
182 Useful functions to put in this list include:
183 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
184 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
185   :group 'gnus-thread
186   :type '(repeat function))
187
188 (defcustom gnus-simplify-ignored-prefixes nil
189   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
190   :group 'gnus-thread
191   :type '(choice (const :tag "off" nil)
192                  regexp))
193
194 (defcustom gnus-build-sparse-threads nil
195   "*If non-nil, fill in the gaps in threads.
196 If `some', only fill in the gaps that are needed to tie loose threads
197 together.  If `more', fill in all leaf nodes that Gnus can find.  If
198 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
199   :group 'gnus-thread
200   :type '(choice (const :tag "off" nil)
201                  (const some)
202                  (const more)
203                  (sexp :menu-tag "all" t)))
204
205 (defcustom gnus-summary-thread-gathering-function
206   'gnus-gather-threads-by-subject
207   "*Function used for gathering loose threads.
208 There are two pre-defined functions: `gnus-gather-threads-by-subject',
209 which only takes Subjects into consideration; and
210 `gnus-gather-threads-by-references', which compared the References
211 headers of the articles to find matches."
212   :group 'gnus-thread
213   :type '(radio (function-item gnus-gather-threads-by-subject)
214                 (function-item gnus-gather-threads-by-references)
215                 (function :tag "other")))
216
217 (defcustom gnus-summary-same-subject ""
218   "*String indicating that the current article has the same subject as the previous.
219 This variable will only be used if the value of
220 `gnus-summary-make-false-root' is `empty'."
221   :group 'gnus-summary-format
222   :type 'string)
223
224 (defcustom gnus-summary-goto-unread nil
225   "*If t, many commands will go to the next unread article.
226 This applies to marking commands as well as other commands that
227 \"naturally\" select the next article, like, for instance, `SPC' at
228 the end of an article.
229
230 If nil, the marking commands do NOT go to the next unread article
231 \(they go to the next article instead).  If `never', commands that
232 usually go to the next unread article, will go to the next article,
233 whether it is read or not."
234   :version "24.1"
235   :group 'gnus-summary-marks
236   :link '(custom-manual "(gnus)Setting Marks")
237   :type '(choice (const :tag "off" nil)
238                  (const never)
239                  (sexp :menu-tag "on" t)))
240
241 (defcustom gnus-summary-default-score 0
242   "*Default article score level.
243 All scores generated by the score files will be added to this score.
244 If this variable is nil, scoring will be disabled."
245   :group 'gnus-score-default
246   :type '(choice (const :tag "disable")
247                  integer))
248
249 (defcustom gnus-summary-default-high-score 0
250   "*Default threshold for a high scored article.
251 An article will be highlighted as high scored if its score is greater
252 than this score."
253   :version "22.1"
254   :group 'gnus-score-default
255   :type 'integer)
256
257 (defcustom gnus-summary-default-low-score 0
258   "*Default threshold for a low scored article.
259 An article will be highlighted as low scored if its score is smaller
260 than this score."
261   :version "22.1"
262   :group 'gnus-score-default
263   :type 'integer)
264
265 (defcustom gnus-summary-zcore-fuzz 0
266   "*Fuzziness factor for the zcore in the summary buffer.
267 Articles with scores closer than this to `gnus-summary-default-score'
268 will not be marked."
269   :group 'gnus-summary-format
270   :type 'integer)
271
272 (defcustom gnus-simplify-subject-fuzzy-regexp nil
273   "*Strings to be removed when doing fuzzy matches.
274 This can either be a regular expression or list of regular expressions
275 that will be removed from subject strings if fuzzy subject
276 simplification is selected."
277   :group 'gnus-thread
278   :type '(repeat regexp))
279
280 (defcustom gnus-show-threads t
281   "*If non-nil, display threads in summary mode."
282   :group 'gnus-thread
283   :type 'boolean)
284
285 (defcustom gnus-thread-hide-subtree nil
286   "*If non-nil, hide all threads initially.
287 This can be a predicate specifier which says which threads to hide.
288 If threads are hidden, you have to run the command
289 `gnus-summary-show-thread' by hand or select an article."
290   :group 'gnus-thread
291   :type '(radio (sexp :format "Non-nil\n"
292                       :match (lambda (widget value)
293                                (not (or (consp value) (functionp value))))
294                       :value t)
295                 (const nil)
296                 (sexp :tag "Predicate specifier")))
297
298 (defcustom gnus-thread-hide-killed t
299   "*If non-nil, hide killed threads automatically."
300   :group 'gnus-thread
301   :type 'boolean)
302
303 (defcustom gnus-thread-ignore-subject t
304   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
305 If nil, articles that have different subjects from their parents will
306 start separate threads."
307   :group 'gnus-thread
308   :type 'boolean)
309
310 (defcustom gnus-thread-operation-ignore-subject t
311   "*If non-nil, subjects will be ignored when doing thread commands.
312 This affects commands like `gnus-summary-kill-thread' and
313 `gnus-summary-lower-thread'.
314
315 If this variable is nil, articles in the same thread with different
316 subjects will not be included in the operation in question.  If this
317 variable is `fuzzy', only articles that have subjects that are fuzzily
318 equal will be included."
319   :group 'gnus-thread
320   :type '(choice (const :tag "off" nil)
321                  (const fuzzy)
322                  (sexp :tag "on" t)))
323
324 (defcustom gnus-thread-indent-level 4
325   "*Number that says how much each sub-thread should be indented."
326   :group 'gnus-thread
327   :type 'integer)
328
329 (defcustom gnus-auto-extend-newsgroup t
330   "*If non-nil, extend newsgroup forward and backward when requested."
331   :group 'gnus-summary-choose
332   :type 'boolean)
333
334 (defcustom gnus-auto-select-first t
335   "If non-nil, select an article on group entry.
336 An article is selected automatically when entering a group
337 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
338 `gnus-summary-catchup-and-goto-next-group'.
339
340 Which article is selected is controlled by the variable
341 `gnus-auto-select-subject'.
342
343 If you want to prevent automatic selection of articles in some
344 newsgroups, set the variable to nil in `gnus-select-group-hook'."
345   ;; Commands include...
346   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
347   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
348   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
349   :group 'gnus-group-select
350   :type '(choice (const :tag "none" nil)
351                  (sexp :menu-tag "first" t)))
352
353 (defcustom gnus-auto-select-subject 'unseen-or-unread
354   "*Says what subject to place under point when entering a group.
355
356 This variable can either be the symbols `first' (place point on the
357 first subject), `unread' (place point on the subject line of the first
358 unread article), `best' (place point on the subject line of the
359 higest-scored article), `unseen' (place point on the subject line of
360 the first unseen article), `unseen-or-unread' (place point on the subject
361 line of the first unseen article or, if all article have been seen, on the
362 subject line of the first unread article), or a function to be called to
363 place point on some subject line."
364   :version "24.1"
365   :group 'gnus-group-select
366   :type '(choice (const best)
367                  (const unread)
368                  (const first)
369                  (const unseen)
370                  (const unseen-or-unread)))
371
372 (defcustom gnus-auto-select-next t
373   "*If non-nil, offer to go to the next group from the end of the previous.
374 If the value is t and the next newsgroup is empty, Gnus will exit
375 summary mode and go back to group mode.  If the value is neither nil
376 nor t, Gnus will select the following unread newsgroup.  In
377 particular, if the value is the symbol `quietly', the next unread
378 newsgroup will be selected without any confirmation, and if it is
379 `almost-quietly', the next group will be selected without any
380 confirmation if you are located on the last article in the group.
381 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
382 will go to the next group without confirmation."
383   :group 'gnus-summary-maneuvering
384   :type '(choice (const :tag "off" nil)
385                  (const quietly)
386                  (const almost-quietly)
387                  (const slightly-quietly)
388                  (sexp :menu-tag "on" t)))
389
390 (defcustom gnus-auto-select-same nil
391   "*If non-nil, select the next article with the same subject.
392 If there are no more articles with the same subject, go to
393 the first unread article."
394   :group 'gnus-summary-maneuvering
395   :type 'boolean)
396
397 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
398   "What article should be selected after exiting an ephemeral group.
399 Valid values include:
400
401 `next'
402   Select the next article.
403 `next-unread'
404   Select the next unread article.
405 `next-noselect'
406   Move the cursor to the next article.  This is the default.
407 `next-unread-noselect'
408   Move the cursor to the next unread article.
409
410 If it has any other value or there is no next (unread) article, the
411 article selected before entering to the ephemeral group will appear."
412   :version "23.1" ;; No Gnus
413   :group 'gnus-summary-maneuvering
414   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
415                  (const next) (const next-unread)
416                  (const next-noselect) (const next-unread-noselect)
417                  (sexp :tag "other" :value nil)))
418
419 (defcustom gnus-auto-goto-ignores 'unfetched
420   "*Says how to handle unfetched articles when maneuvering.
421
422 This variable can either be the symbols nil (maneuver to any
423 article), `undownloaded' (maneuvering while unplugged ignores articles
424 that have not been fetched), `always-undownloaded' (maneuvering always
425 ignores articles that have not been fetched), `unfetched' (maneuvering
426 ignores articles whose headers have not been fetched).
427
428 NOTE: The list of unfetched articles will always be nil when plugged
429 and, when unplugged, a subset of the undownloaded article list."
430   :version "22.1"
431   :group 'gnus-summary-maneuvering
432   :type '(choice (const :tag "None" nil)
433                  (const :tag "Undownloaded when unplugged" undownloaded)
434                  (const :tag "Undownloaded" always-undownloaded)
435                  (const :tag "Unfetched" unfetched)))
436
437 (defcustom gnus-summary-check-current nil
438   "*If non-nil, consider the current article when moving.
439 The \"unread\" movement commands will stay on the same line if the
440 current article is unread."
441   :group 'gnus-summary-maneuvering
442   :type 'boolean)
443
444 (defcustom gnus-auto-center-summary 2
445   "*If non-nil, always center the current summary buffer.
446 In particular, if `vertical' do only vertical recentering.  If non-nil
447 and non-`vertical', do both horizontal and vertical recentering."
448   :group 'gnus-summary-maneuvering
449   :type '(choice (const :tag "none" nil)
450                  (const vertical)
451                  (integer :tag "height")
452                  (sexp :menu-tag "both" t)))
453
454 (defcustom gnus-auto-center-group t
455   "If non-nil, always center the group buffer."
456   :group 'gnus-summary-maneuvering
457   :type 'boolean)
458
459 (defcustom gnus-show-all-headers nil
460   "*If non-nil, don't hide any headers."
461   :group 'gnus-article-hiding
462   :group 'gnus-article-headers
463   :type 'boolean)
464
465 (defcustom gnus-summary-ignore-duplicates nil
466   "*If non-nil, ignore articles with identical Message-ID headers."
467   :group 'gnus-summary
468   :type 'boolean)
469
470 (defcustom gnus-single-article-buffer nil
471   "*If non-nil, display all articles in the same buffer.
472 If nil, each group will get its own article buffer."
473   :version "24.1"
474   :group 'gnus-article-various
475   :type 'boolean)
476
477 (defcustom gnus-widen-article-window nil
478   "If non-nil, selecting the article buffer will display only the article buffer."
479   :version "24.1"
480   :group 'gnus-article-various
481   :type 'boolean)
482
483 (defcustom gnus-break-pages t
484   "*If non-nil, do page breaking on articles.
485 The page delimiter is specified by the `gnus-page-delimiter'
486 variable."
487   :group 'gnus-article-various
488   :type 'boolean)
489
490 (defcustom gnus-move-split-methods nil
491   "*Variable used to suggest where articles are to be moved to.
492 It uses the same syntax as the `gnus-split-methods' variable.
493 However, whereas `gnus-split-methods' specifies file names as targets,
494 this variable specifies group names."
495   :group 'gnus-summary-mail
496   :type '(repeat (choice (list :value (fun) function)
497                          (cons :value ("" "") regexp (repeat string))
498                          (sexp :value nil))))
499
500 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
501   "Function used to compute default prefix for article move/copy/etc prompts.
502 The function should take one argument, a group name, and return a
503 string with the suggested prefix."
504   :group 'gnus-summary-mail
505   :type 'function)
506
507 ;; FIXME: Although the custom type is `character' for the following variables,
508 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
509
510 (defcustom gnus-unread-mark ?           ;Whitespace
511   "*Mark used for unread articles."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-ticked-mark ?!
516   "*Mark used for ticked articles."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-dormant-mark ??
521   "*Mark used for dormant articles."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-del-mark ?r
526   "*Mark used for del'd articles."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-read-mark ?R
531   "*Mark used for read articles."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-expirable-mark ?E
536   "*Mark used for expirable articles."
537   :group 'gnus-summary-marks
538   :type 'character)
539
540 (defcustom gnus-killed-mark ?K
541   "*Mark used for killed articles."
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-spam-mark ?$
546   "*Mark used for spam articles."
547   :version "22.1"
548   :group 'gnus-summary-marks
549   :type 'character)
550
551 (defcustom gnus-kill-file-mark ?X
552   "*Mark used for articles killed by kill files."
553   :group 'gnus-summary-marks
554   :type 'character)
555
556 (defcustom gnus-low-score-mark ?Y
557   "*Mark used for articles with a low score."
558   :group 'gnus-summary-marks
559   :type 'character)
560
561 (defcustom gnus-catchup-mark ?C
562   "*Mark used for articles that are caught up."
563   :group 'gnus-summary-marks
564   :type 'character)
565
566 (defcustom gnus-replied-mark ?A
567   "*Mark used for articles that have been replied to."
568   :group 'gnus-summary-marks
569   :type 'character)
570
571 (defcustom gnus-forwarded-mark ?F
572   "*Mark used for articles that have been forwarded."
573   :version "22.1"
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-recent-mark ?N
578   "*Mark used for articles that are recent."
579   :version "22.1"
580   :group 'gnus-summary-marks
581   :type 'character)
582
583 (defcustom gnus-cached-mark ?*
584   "*Mark used for articles that are in the cache."
585   :group 'gnus-summary-marks
586   :type 'character)
587
588 (defcustom gnus-saved-mark ?S
589   "*Mark used for articles that have been saved."
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-unseen-mark ?.
594   "*Mark used for articles that haven't been seen."
595   :version "22.1"
596   :group 'gnus-summary-marks
597   :type 'character)
598
599 (defcustom gnus-no-mark ?               ;Whitespace
600   "*Mark used for articles that have no other secondary mark."
601   :version "22.1"
602   :group 'gnus-summary-marks
603   :type 'character)
604
605 (defcustom gnus-ancient-mark ?O
606   "*Mark used for ancient articles."
607   :group 'gnus-summary-marks
608   :type 'character)
609
610 (defcustom gnus-sparse-mark ?Q
611   "*Mark used for sparsely reffed articles."
612   :group 'gnus-summary-marks
613   :type 'character)
614
615 (defcustom gnus-canceled-mark ?G
616   "*Mark used for canceled articles."
617   :group 'gnus-summary-marks
618   :type 'character)
619
620 (defcustom gnus-duplicate-mark ?M
621   "*Mark used for duplicate articles."
622   :group 'gnus-summary-marks
623   :type 'character)
624
625 (defcustom gnus-undownloaded-mark ?-
626   "*Mark used for articles that weren't downloaded."
627   :version "22.1"
628   :group 'gnus-summary-marks
629   :type 'character)
630
631 (defcustom gnus-downloaded-mark ?+
632   "*Mark used for articles that were downloaded."
633   :group 'gnus-summary-marks
634   :type 'character)
635
636 (defcustom gnus-downloadable-mark ?%
637   "*Mark used for articles that are to be downloaded."
638   :group 'gnus-summary-marks
639   :type 'character)
640
641 (defcustom gnus-unsendable-mark ?=
642   "*Mark used for articles that won't be sent."
643   :group 'gnus-summary-marks
644   :type 'character)
645
646 (defcustom gnus-score-over-mark ?+
647   "*Score mark used for articles with high scores."
648   :group 'gnus-summary-marks
649   :type 'character)
650
651 (defcustom gnus-score-below-mark ?-
652   "*Score mark used for articles with low scores."
653   :group 'gnus-summary-marks
654   :type 'character)
655
656 (defcustom gnus-empty-thread-mark ?     ;Whitespace
657   "*There is no thread under the article."
658   :group 'gnus-summary-marks
659   :type 'character)
660
661 (defcustom gnus-not-empty-thread-mark ?=
662   "*There is a thread under the article."
663   :group 'gnus-summary-marks
664   :type 'character)
665
666 (defcustom gnus-view-pseudo-asynchronously nil
667   "*If non-nil, Gnus will view pseudo-articles asynchronously."
668   :group 'gnus-extract-view
669   :type 'boolean)
670
671 (defcustom gnus-auto-expirable-marks
672   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
673         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
674         gnus-duplicate-mark)
675   "*The list of marks converted into expiration if a group is auto-expirable."
676   :version "24.1"
677   :group 'gnus-summary
678   :type '(repeat character))
679
680 (defcustom gnus-inhibit-user-auto-expire t
681   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
682   :version "21.1"
683   :group 'gnus-summary
684   :type 'boolean)
685
686 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
687   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
688 If nil, the expirable marks will be unchanged except that the marks
689 will be removed when copying or moving articles to a group that has
690 not turned auto-expire on.  If non-nil, articles that have been read
691 will be marked as expirable when being copied or moved to a group in
692 which auto-expire is turned on."
693   :version "23.2"
694   :type 'boolean
695   :group 'gnus-summary-marks)
696
697 (defcustom gnus-view-pseudos nil
698   "*If `automatic', pseudo-articles will be viewed automatically.
699 If `not-confirm', pseudos will be viewed automatically, and the user
700 will not be asked to confirm the command."
701   :group 'gnus-extract-view
702   :type '(choice (const :tag "off" nil)
703                  (const automatic)
704                  (const not-confirm)))
705
706 (defcustom gnus-view-pseudos-separately t
707   "*If non-nil, one pseudo-article will be created for each file to be viewed.
708 If nil, all files that use the same viewing command will be given as a
709 list of parameters to that command."
710   :group 'gnus-extract-view
711   :type 'boolean)
712
713 (defcustom gnus-insert-pseudo-articles t
714   "*If non-nil, insert pseudo-articles when decoding articles."
715   :group 'gnus-extract-view
716   :type 'boolean)
717
718 (defcustom gnus-summary-dummy-line-format
719   "   %(:                             :%) %S\n"
720   "*The format specification for the dummy roots in the summary buffer.
721 It works along the same lines as a normal formatting string,
722 with some simple extensions.
723
724 %S  The subject
725
726 General format specifiers can also be used.
727 See `(gnus)Formatting Variables'."
728   :link '(custom-manual "(gnus)Formatting Variables")
729   :group 'gnus-threading
730   :type 'string)
731
732 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
733   "*The format specification for the summary mode line.
734 It works along the same lines as a normal formatting string,
735 with some simple extensions:
736
737 %G  Group name
738 %p  Unprefixed group name
739 %A  Current article number
740 %z  Current article score
741 %V  Gnus version
742 %U  Number of unread articles in the group
743 %e  Number of unselected articles in the group
744 %Z  A string with unread/unselected article counts
745 %g  Shortish group name
746 %S  Subject of the current article
747 %u  User-defined spec
748 %s  Current score file name
749 %d  Number of dormant articles
750 %r  Number of articles that have been marked as read in this session
751 %E  Number of articles expunged by the score files"
752   :group 'gnus-summary-format
753   :type 'string)
754
755 (defcustom gnus-list-identifiers nil
756   "Regexp that matches list identifiers to be removed from subject.
757 This can also be a list of regexps."
758   :version "21.1"
759   :group 'gnus-summary-format
760   :group 'gnus-article-hiding
761   :type '(choice (const :tag "none" nil)
762                  (regexp :value ".*")
763                  (repeat :value (".*") regexp)))
764
765 (defcustom gnus-summary-mark-below 0
766   "*Mark all articles with a score below this variable as read.
767 This variable is local to each summary buffer and usually set by the
768 score file."
769   :group 'gnus-score-default
770   :type 'integer)
771
772 (defun gnus-widget-reversible-match (widget value)
773   "Ignoring WIDGET, convert VALUE to internal form.
774 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
775   ;; (debug value)
776   (or (symbolp value)
777       (and (listp value)
778            (eq (length value) 2)
779            (eq (nth 0 value) 'not)
780            (symbolp (nth 1 value)))))
781
782 (defun gnus-widget-reversible-to-internal (widget value)
783   "Ignoring WIDGET, convert VALUE to internal form.
784 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
785 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
786   ;; (debug value)
787   (if (atom value)
788       (list value nil)
789     (list (nth 1 value) t)))
790
791 (defun gnus-widget-reversible-to-external (widget value)
792   "Ignoring WIDGET, convert VALUE to external form.
793 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
794 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
795   ;; (debug value)
796   (if (nth 1 value)
797       (list 'not (nth 0 value))
798     (nth 0 value)))
799
800 (define-widget 'gnus-widget-reversible 'group
801   "A `group' that convert values."
802   :match 'gnus-widget-reversible-match
803   :value-to-internal 'gnus-widget-reversible-to-internal
804   :value-to-external 'gnus-widget-reversible-to-external)
805
806 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
807   "*List of functions used for sorting articles in the summary buffer.
808
809 Each function takes two articles and returns non-nil if the first
810 article should be sorted before the other.  If you use more than one
811 function, the primary sort function should be the last.  You should
812 probably always include `gnus-article-sort-by-number' in the list of
813 sorting functions -- preferably first.  Also note that sorting by date
814 is often much slower than sorting by number, and the sorting order is
815 very similar.  (Sorting by date means sorting by the time the message
816 was sent, sorting by number means sorting by arrival time.)
817
818 Each item can also be a list `(not F)' where F is a function;
819 this reverses the sort order.
820
821 Ready-made functions include `gnus-article-sort-by-number',
822 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
823 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
824 and `gnus-article-sort-by-score'.
825
826 When threading is turned on, the variable `gnus-thread-sort-functions'
827 controls how articles are sorted."
828   :group 'gnus-summary-sort
829   :type '(repeat (gnus-widget-reversible
830                   (choice (function-item gnus-article-sort-by-number)
831                           (function-item gnus-article-sort-by-author)
832                           (function-item gnus-article-sort-by-subject)
833                           (function-item gnus-article-sort-by-date)
834                           (function-item gnus-article-sort-by-score)
835                           (function-item gnus-article-sort-by-random)
836                           (function :tag "other"))
837                   (boolean :tag "Reverse order"))))
838
839
840 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
841   "*List of functions used for sorting threads in the summary buffer.
842 By default, threads are sorted by article number.
843
844 Each function takes two threads and returns non-nil if the first
845 thread should be sorted before the other.  If you use more than one
846 function, the primary sort function should be the last.  You should
847 probably always include `gnus-thread-sort-by-number' in the list of
848 sorting functions -- preferably first.  Also note that sorting by date
849 is often much slower than sorting by number, and the sorting order is
850 very similar.  (Sorting by date means sorting by the time the message
851 was sent, sorting by number means sorting by arrival time.)
852
853 Each list item can also be a list `(not F)' where F is a
854 function; this specifies reversed sort order.
855
856 Ready-made functions include `gnus-thread-sort-by-number',
857 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
858 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
859 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
860 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
861 and `gnus-thread-sort-by-total-score' (see
862 `gnus-thread-score-function').
863
864 When threading is turned off, the variable
865 `gnus-article-sort-functions' controls how articles are sorted."
866   :group 'gnus-summary-sort
867   :type '(repeat
868           (gnus-widget-reversible
869            (choice (function-item gnus-thread-sort-by-number)
870                    (function-item gnus-thread-sort-by-author)
871                    (function-item gnus-thread-sort-by-recipient)
872                    (function-item gnus-thread-sort-by-subject)
873                    (function-item gnus-thread-sort-by-date)
874                    (function-item gnus-thread-sort-by-score)
875                    (function-item gnus-thread-sort-by-most-recent-number)
876                    (function-item gnus-thread-sort-by-most-recent-date)
877                    (function-item gnus-thread-sort-by-random)
878                    (function-item gnus-thread-sort-by-total-score)
879                    (function :tag "other"))
880            (boolean :tag "Reverse order"))))
881
882 (defcustom gnus-thread-score-function '+
883   "*Function used for calculating the total score of a thread.
884
885 The function is called with the scores of the article and each
886 subthread and should then return the score of the thread.
887
888 Some functions you can use are `+', `max', or `min'."
889   :group 'gnus-summary-sort
890   :type 'function)
891
892 (defcustom gnus-summary-expunge-below nil
893   "All articles that have a score less than this variable will be expunged.
894 This variable is local to the summary buffers."
895   :group 'gnus-score-default
896   :type '(choice (const :tag "off" nil)
897                  integer))
898
899 (defcustom gnus-thread-expunge-below nil
900   "All threads that have a total score less than this variable will be expunged.
901 See `gnus-thread-score-function' for en explanation of what a
902 \"thread score\" is.
903
904 This variable is local to the summary buffers."
905   :group 'gnus-threading
906   :group 'gnus-score-default
907   :type '(choice (const :tag "off" nil)
908                  integer))
909
910 (defcustom gnus-summary-mode-hook nil
911   "*A hook for Gnus summary mode.
912 This hook is run before any variables are set in the summary buffer."
913   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
914   :group 'gnus-summary-various
915   :type 'hook)
916
917 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
918 (when (featurep 'xemacs)
919   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
920   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
921   (add-hook 'gnus-summary-mode-hook
922             'gnus-xmas-switch-horizontal-scrollbar-off))
923
924 (defcustom gnus-summary-menu-hook nil
925   "*Hook run after the creation of the summary mode menu."
926   :group 'gnus-summary-visual
927   :type 'hook)
928
929 (defcustom gnus-summary-exit-hook nil
930   "*A hook called on exit from the summary buffer.
931 It will be called with point in the group buffer."
932   :group 'gnus-summary-exit
933   :type 'hook)
934
935 (defcustom gnus-summary-prepare-hook nil
936   "*A hook called after the summary buffer has been generated.
937 If you want to modify the summary buffer, you can use this hook."
938   :group 'gnus-summary-various
939   :type 'hook)
940
941 (defcustom gnus-summary-prepared-hook nil
942   "*A hook called as the last thing after the summary buffer has been generated."
943   :group 'gnus-summary-various
944   :type 'hook)
945
946 (defcustom gnus-summary-generate-hook nil
947   "*A hook run just before generating the summary buffer.
948 This hook is commonly used to customize threading variables and the
949 like."
950   :group 'gnus-summary-various
951   :type 'hook)
952
953 (defcustom gnus-select-group-hook nil
954   "*A hook called when a newsgroup is selected.
955
956 If you'd like to simplify subjects like the
957 `gnus-summary-next-same-subject' command does, you can use the
958 following hook:
959
960  (add-hook gnus-select-group-hook
961            (lambda ()
962              (mapcar (lambda (header)
963                        (mail-header-set-subject
964                         header
965                         (gnus-simplify-subject
966                          (mail-header-subject header) 're-only)))
967                      gnus-newsgroup-headers)))"
968   :group 'gnus-group-select
969   :type 'hook)
970
971 (defcustom gnus-select-article-hook nil
972   "*A hook called when an article is selected."
973   :group 'gnus-summary-choose
974   :options '(gnus-agent-fetch-selected-article)
975   :type 'hook)
976
977 (defcustom gnus-visual-mark-article-hook
978   (list 'gnus-highlight-selected-summary)
979   "*Hook run after selecting an article in the summary buffer.
980 It is meant to be used for highlighting the article in some way.  It
981 is not run if `gnus-visual' is nil."
982   :group 'gnus-summary-visual
983   :type 'hook)
984
985 (defcustom gnus-parse-headers-hook nil
986   "*A hook called before parsing the headers."
987   :group 'gnus-various
988   :type 'hook)
989
990 (defcustom gnus-exit-group-hook nil
991   "*A hook called when exiting summary mode.
992 This hook is not called from the non-updating exit commands like `Q'."
993   :group 'gnus-various
994   :type 'hook)
995
996 (defcustom gnus-summary-update-hook nil
997   "*A hook called when a summary line is changed.
998 The hook will not be called if `gnus-visual' is nil.
999
1000 The default function `gnus-summary-highlight-line' will
1001 highlight the line according to the `gnus-summary-highlight'
1002 variable."
1003   :group 'gnus-summary-visual
1004   :type 'hook)
1005
1006 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1007   "*A hook called when an article is selected for the first time.
1008 The hook is intended to mark an article as read (or unread)
1009 automatically when it is selected."
1010   :group 'gnus-summary-choose
1011   :type 'hook)
1012
1013 (defcustom gnus-group-no-more-groups-hook nil
1014   "*A hook run when returning to group mode having no more (unread) groups."
1015   :group 'gnus-group-select
1016   :type 'hook)
1017
1018 (defcustom gnus-ps-print-hook nil
1019   "*A hook run before ps-printing something from Gnus."
1020   :group 'gnus-summary
1021   :type 'hook)
1022
1023 (defcustom gnus-summary-article-move-hook nil
1024   "*A hook called after an article is moved, copied, respooled, or crossposted."
1025   :version "22.1"
1026   :group 'gnus-summary
1027   :type 'hook)
1028
1029 (defcustom gnus-summary-article-delete-hook nil
1030   "*A hook called after an article is deleted."
1031   :version "22.1"
1032   :group 'gnus-summary
1033   :type 'hook)
1034
1035 (defcustom gnus-summary-article-expire-hook nil
1036   "*A hook called after an article is expired."
1037   :version "22.1"
1038   :group 'gnus-summary
1039   :type 'hook)
1040
1041 (defcustom gnus-summary-display-arrow
1042   (and (fboundp 'display-graphic-p)
1043        (display-graphic-p))
1044   "*If non-nil, display an arrow highlighting the current article."
1045   :version "22.1"
1046   :group 'gnus-summary
1047   :type 'boolean)
1048
1049 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1050   "Face used for highlighting the current article in the summary buffer."
1051   :group 'gnus-summary-visual
1052   :type 'face)
1053
1054 (defvar gnus-tmp-downloaded nil)
1055
1056 (defcustom gnus-summary-highlight
1057   '(((eq mark gnus-canceled-mark)
1058      . gnus-summary-cancelled)
1059     ((and uncached (> score default-high))
1060      . gnus-summary-high-undownloaded)
1061     ((and uncached (< score default-low))
1062      . gnus-summary-low-undownloaded)
1063     (uncached
1064      . gnus-summary-normal-undownloaded)
1065     ((and (> score default-high)
1066           (or (eq mark gnus-dormant-mark)
1067               (eq mark gnus-ticked-mark)))
1068      . gnus-summary-high-ticked)
1069     ((and (< score default-low)
1070           (or (eq mark gnus-dormant-mark)
1071               (eq mark gnus-ticked-mark)))
1072      . gnus-summary-low-ticked)
1073     ((or (eq mark gnus-dormant-mark)
1074          (eq mark gnus-ticked-mark))
1075      . gnus-summary-normal-ticked)
1076     ((and (> score default-high) (eq mark gnus-ancient-mark))
1077      . gnus-summary-high-ancient)
1078     ((and (< score default-low) (eq mark gnus-ancient-mark))
1079      . gnus-summary-low-ancient)
1080     ((eq mark gnus-ancient-mark)
1081      . gnus-summary-normal-ancient)
1082     ((and (> score default-high) (eq mark gnus-unread-mark))
1083      . gnus-summary-high-unread)
1084     ((and (< score default-low) (eq mark gnus-unread-mark))
1085      . gnus-summary-low-unread)
1086     ((eq mark gnus-unread-mark)
1087      . gnus-summary-normal-unread)
1088     ((> score default-high)
1089      . gnus-summary-high-read)
1090     ((< score default-low)
1091      . gnus-summary-low-read)
1092     (t
1093      . gnus-summary-normal-read))
1094   "*Controls the highlighting of summary buffer lines.
1095
1096 A list of (FORM . FACE) pairs.  When deciding how a particular
1097 summary line should be displayed, each form is evaluated.  The content
1098 of the face field after the first true form is used.  You can change
1099 how those summary lines are displayed, by editing the face field.
1100
1101 You can use the following variables in the FORM field.
1102
1103 score:        The article's score.
1104 default:      The default article score.
1105 default-high: The default score for high scored articles.
1106 default-low:  The default score for low scored articles.
1107 below:        The score below which articles are automatically marked as read.
1108 mark:         The article's mark.
1109 uncached:     Non-nil if the article is uncached."
1110   :group 'gnus-summary-visual
1111   :type '(repeat (cons (sexp :tag "Form" nil)
1112                        face)))
1113 (put 'gnus-summary-highlight 'risky-local-variable t)
1114
1115 (defcustom gnus-alter-header-function nil
1116   "Function called to allow alteration of article header structures.
1117 The function is called with one parameter, the article header vector,
1118 which it may alter in any way."
1119   :type '(choice (const :tag "None" nil)
1120                  function)
1121   :group 'gnus-summary)
1122
1123 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1124   "Function used to decode a string with encoded words.")
1125
1126 (defvar gnus-decode-encoded-address-function
1127   'mail-decode-encoded-address-string
1128   "Function used to decode addresses with encoded words.")
1129
1130 (defcustom gnus-extra-headers '(To Newsgroups)
1131   "*Extra headers to parse."
1132   :version "21.1"
1133   :group 'gnus-summary
1134   :type '(repeat symbol))
1135
1136 (defcustom gnus-ignored-from-addresses
1137   (and user-mail-address
1138        (not (string= user-mail-address ""))
1139        (regexp-quote user-mail-address))
1140   "*From headers that may be suppressed in favor of To headers.
1141 This can be a regexp or a list of regexps."
1142   :version "21.1"
1143   :group 'gnus-summary
1144   :type '(choice regexp
1145                  (repeat :tag "Regexp List" regexp)))
1146
1147 (defsubst gnus-ignored-from-addresses ()
1148   (gmm-regexp-concat gnus-ignored-from-addresses))
1149
1150 (defcustom gnus-summary-to-prefix "-> "
1151   "*String prefixed to the To field in the summary line when
1152 using `gnus-ignored-from-addresses'."
1153   :version "22.1"
1154   :group 'gnus-summary
1155   :type 'string)
1156
1157 (defcustom gnus-summary-newsgroup-prefix "=> "
1158   "*String prefixed to the Newsgroup field in the summary
1159 line when using `gnus-ignored-from-addresses'."
1160   :version "22.1"
1161   :group 'gnus-summary
1162   :type 'string)
1163
1164 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1165   "List of charsets that should be ignored.
1166 When these charsets are used in the \"charset\" parameter, the
1167 default charset will be used instead."
1168   :version "21.1"
1169   :type '(repeat symbol)
1170   :group 'gnus-charset)
1171
1172 (defcustom gnus-newsgroup-maximum-articles nil
1173   "The maximum number of articles a newsgroup.
1174 If this is a number, old articles in a newsgroup exceeding this number
1175 are silently ignored.  If it is nil, no article is ignored.  Note that
1176 setting this variable to a number might prevent you from reading very
1177 old articles."
1178   :group 'gnus-group-select
1179   :version "22.2"
1180   :type '(choice (const :tag "No limit" nil)
1181                  integer))
1182
1183 (gnus-define-group-parameter
1184  ignored-charsets
1185  :type list
1186  :function-document
1187  "Return the ignored charsets of GROUP."
1188  :variable gnus-group-ignored-charsets-alist
1189  :variable-default
1190  '(("alt\\.chinese\\.text" iso-8859-1))
1191  :variable-document
1192  "Alist of regexps (to match group names) and charsets that should be ignored.
1193 When these charsets are used in the \"charset\" parameter, the
1194 default charset will be used instead."
1195  :variable-group gnus-charset
1196  :variable-type '(repeat (cons (regexp :tag "Group")
1197                                (repeat symbol)))
1198  :parameter-type '(choice :tag "Ignored charsets"
1199                           :value nil
1200                           (repeat (symbol)))
1201  :parameter-document       "\
1202 List of charsets that should be ignored.
1203
1204 When these charsets are used in the \"charset\" parameter, the
1205 default charset will be used instead.")
1206
1207 (defcustom gnus-group-highlight-words-alist nil
1208   "Alist of group regexps and highlight regexps.
1209 This variable uses the same syntax as `gnus-emphasis-alist'."
1210   :version "21.1"
1211   :type '(repeat (cons (regexp :tag "Group")
1212                        (repeat (list (regexp :tag "Highlight regexp")
1213                                      (number :tag "Group for entire word" 0)
1214                                      (number :tag "Group for displayed part" 0)
1215                                      (symbol :tag "Face"
1216                                              gnus-emphasis-highlight-words)))))
1217   :group 'gnus-summary-visual)
1218
1219 (defcustom gnus-summary-show-article-charset-alist
1220   nil
1221   "Alist of number and charset.
1222 The article will be shown with the charset corresponding to the
1223 numbered argument.
1224 For example: ((1 . cn-gb-2312) (2 . big5))."
1225   :version "21.1"
1226   :type '(repeat (cons (number :tag "Argument" 1)
1227                        (symbol :tag "Charset")))
1228   :group 'gnus-charset)
1229
1230 (defcustom gnus-preserve-marks t
1231   "Whether marks are preserved when moving, copying and respooling messages."
1232   :version "21.1"
1233   :type 'boolean
1234   :group 'gnus-summary-marks)
1235
1236 (defcustom gnus-propagate-marks t
1237   "If non-nil, do not propagate marks to the backends."
1238   :version "23.1" ;; No Gnus
1239   :type 'boolean
1240   :group 'gnus-summary-marks)
1241
1242 (defcustom gnus-alter-articles-to-read-function nil
1243   "Function to be called to alter the list of articles to be selected."
1244   :type '(choice (const nil) function)
1245   :group 'gnus-summary)
1246
1247 (defcustom gnus-orphan-score nil
1248   "*All orphans get this score added.  Set in the score file."
1249   :group 'gnus-score-default
1250   :type '(choice (const nil)
1251                  integer))
1252
1253 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1254   "*A regexp to match MIME parts when saving multiple parts of a
1255 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1256 This regexp will be used by default when prompting the user for which
1257 type of files to save."
1258   :group 'gnus-summary
1259   :type 'regexp)
1260
1261 (defcustom gnus-read-all-available-headers nil
1262   "Whether Gnus should parse all headers made available to it.
1263 This is mostly relevant for slow back ends where the user may
1264 wish to widen the summary buffer to include all headers
1265 that were fetched."
1266   :version "22.1"
1267   :group 'gnus-summary
1268   :type '(choice boolean regexp))
1269
1270 (defcustom gnus-summary-pipe-output-default-command nil
1271   "Command (and optional arguments) used to pipe article to subprocess.
1272 This will be used as the default command if it is non-nil.  The value
1273 will be updated if you modify it when executing the command
1274 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1275   :version "23.1" ;; No Gnus
1276   :group 'gnus-summary
1277   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1278
1279 (defcustom gnus-summary-muttprint-program "muttprint"
1280   "Command (and optional arguments) used to run Muttprint.
1281 The value will be updated if you modify it when executing the command
1282 `gnus-summary-muttprint'."
1283   :version "22.1"
1284   :group 'gnus-summary
1285   :type 'string)
1286
1287 (defcustom gnus-article-loose-mime t
1288   "If non-nil, don't require MIME-Version header.
1289 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1290 supply the MIME-Version header or deliberately strip it from the mail.
1291 If non-nil (the default), Gnus will treat some articles as MIME
1292 even if the MIME-Version header is missing."
1293   :version "22.1"
1294   :type 'boolean
1295   :group 'gnus-article-mime)
1296
1297 (defcustom gnus-article-emulate-mime t
1298   "If non-nil, use MIME emulation for uuencode and the like.
1299 This means that Gnus will search message bodies for text that look
1300 like uuencoded bits, yEncoded bits, and so on, and present that using
1301 the normal Gnus MIME machinery."
1302   :version "22.1"
1303   :type 'boolean
1304   :group 'gnus-article-mime)
1305
1306 ;;; Internal variables
1307
1308 (defvar gnus-summary-display-cache nil)
1309 (defvar gnus-article-mime-handles nil)
1310 (defvar gnus-article-decoded-p nil)
1311 (defvar gnus-article-charset nil)
1312 (defvar gnus-article-ignored-charsets nil)
1313 (defvar gnus-scores-exclude-files nil)
1314 (defvar gnus-page-broken nil)
1315
1316 (defvar gnus-original-article nil)
1317 (defvar gnus-article-internal-prepare-hook nil)
1318 (defvar gnus-newsgroup-process-stack nil)
1319
1320 (defvar gnus-thread-indent-array nil)
1321 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1322 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1323   "Function called to sort the articles within a thread after it has been gathered together.")
1324
1325 (defvar gnus-summary-save-parts-type-history nil)
1326 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1327
1328 ;; Avoid highlighting in kill files.
1329 (defvar gnus-summary-inhibit-highlight nil)
1330 (defvar gnus-newsgroup-selected-overlay nil)
1331 (defvar gnus-inhibit-limiting nil)
1332 (defvar gnus-newsgroup-adaptive-score-file nil)
1333 (defvar gnus-current-score-file nil)
1334 (defvar gnus-current-move-group nil)
1335 (defvar gnus-current-copy-group nil)
1336 (defvar gnus-current-crosspost-group nil)
1337 (defvar gnus-newsgroup-display nil)
1338
1339 (defvar gnus-newsgroup-dependencies nil)
1340 (defvar gnus-newsgroup-adaptive nil)
1341 (defvar gnus-summary-display-article-function nil)
1342 (defvar gnus-summary-highlight-line-function nil
1343   "Function called after highlighting a summary line.")
1344
1345 (defvar gnus-summary-line-format-alist
1346   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1347     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1348     (?s gnus-tmp-subject-or-nil ?s)
1349     (?n gnus-tmp-name ?s)
1350     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1351         ?s)
1352     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1353             gnus-tmp-from) ?s)
1354     (?F gnus-tmp-from ?s)
1355     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1356     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1357     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1358     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1359     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1360     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1361     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1362     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1363     (?L gnus-tmp-lines ?s)
1364     (?O gnus-tmp-downloaded ?c)
1365     (?I gnus-tmp-indentation ?s)
1366     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1367     (?R gnus-tmp-replied ?c)
1368     (?\[ gnus-tmp-opening-bracket ?c)
1369     (?\] gnus-tmp-closing-bracket ?c)
1370     (?\> (make-string gnus-tmp-level ? ) ?s)
1371     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1372     (?i gnus-tmp-score ?d)
1373     (?z gnus-tmp-score-char ?c)
1374     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1375     (?U gnus-tmp-unread ?c)
1376     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1377         ?s)
1378     (?t (gnus-summary-number-of-articles-in-thread
1379          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1380         ?d)
1381     (?e (gnus-summary-number-of-articles-in-thread
1382          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1383         ?c)
1384     (?u gnus-tmp-user-defined ?s)
1385     (?P (gnus-pick-line-number) ?d)
1386     (?B gnus-tmp-thread-tree-header-string ?s)
1387     (user-date (gnus-user-date
1388                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1389   "An alist of format specifications that can appear in summary lines.
1390 These are paired with what variables they correspond with, along with
1391 the type of the variable (string, integer, character, etc).")
1392
1393 (defvar gnus-summary-dummy-line-format-alist
1394   `((?S gnus-tmp-subject ?s)
1395     (?N gnus-tmp-number ?d)
1396     (?u gnus-tmp-user-defined ?s)))
1397
1398 (defvar gnus-summary-mode-line-format-alist
1399   `((?G gnus-tmp-group-name ?s)
1400     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1401     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1402     (?A gnus-tmp-article-number ?d)
1403     (?Z gnus-tmp-unread-and-unselected ?s)
1404     (?V gnus-version ?s)
1405     (?U gnus-tmp-unread-and-unticked ?d)
1406     (?S gnus-tmp-subject ?s)
1407     (?e gnus-tmp-unselected ?d)
1408     (?u gnus-tmp-user-defined ?s)
1409     (?d (length gnus-newsgroup-dormant) ?d)
1410     (?t (length gnus-newsgroup-marked) ?d)
1411     (?h (length gnus-newsgroup-spam-marked) ?d)
1412     (?r (length gnus-newsgroup-reads) ?d)
1413     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1414     (?E gnus-newsgroup-expunged-tally ?d)
1415     (?s (gnus-current-score-file-nondirectory) ?s)))
1416
1417 ;; This is here rather than in gnus-art for compilation reasons.
1418 (defvar gnus-article-mode-line-format-alist
1419   (nconc '((?w (gnus-article-wash-status) ?s)
1420            (?m (gnus-article-mime-part-status) ?s))
1421          gnus-summary-mode-line-format-alist))
1422
1423 (defvar gnus-last-search-regexp nil
1424   "Default regexp for article search command.")
1425
1426 (defvar gnus-last-shell-command nil
1427   "Default shell command on article.")
1428
1429 (defvar gnus-newsgroup-agentized nil
1430   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1431 (defvar gnus-newsgroup-begin nil)
1432 (defvar gnus-newsgroup-end nil)
1433 (defvar gnus-newsgroup-last-rmail nil)
1434 (defvar gnus-newsgroup-last-mail nil)
1435 (defvar gnus-newsgroup-last-folder nil)
1436 (defvar gnus-newsgroup-last-file nil)
1437 (defvar gnus-newsgroup-last-directory nil)
1438 (defvar gnus-newsgroup-auto-expire nil)
1439 (defvar gnus-newsgroup-active nil)
1440 (defvar gnus-newsgroup-highest nil)
1441
1442 (defvar gnus-newsgroup-data nil)
1443 (defvar gnus-newsgroup-data-reverse nil)
1444 (defvar gnus-newsgroup-limit nil)
1445 (defvar gnus-newsgroup-limits nil)
1446 (defvar gnus-summary-use-undownloaded-faces nil)
1447
1448 (defvar gnus-newsgroup-unreads nil
1449   "Sorted list of unread articles in the current newsgroup.")
1450
1451 (defvar gnus-newsgroup-unselected nil
1452   "Sorted list of unselected unread articles in the current newsgroup.")
1453
1454 (defvar gnus-newsgroup-reads nil
1455   "Alist of read articles and article marks in the current newsgroup.")
1456
1457 (defvar gnus-newsgroup-expunged-tally nil)
1458
1459 (defvar gnus-newsgroup-marked nil
1460   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1461
1462 (defvar gnus-newsgroup-spam-marked nil
1463   "List of ranges of articles that have been marked as spam.")
1464
1465 (defvar gnus-newsgroup-killed nil
1466   "List of ranges of articles that have been through the scoring process.")
1467
1468 (defvar gnus-newsgroup-cached nil
1469   "Sorted list of articles that come from the article cache.")
1470
1471 (defvar gnus-newsgroup-saved nil
1472   "List of articles that have been saved.")
1473
1474 (defvar gnus-newsgroup-kill-headers nil)
1475
1476 (defvar gnus-newsgroup-replied nil
1477   "List of articles that have been replied to in the current newsgroup.")
1478
1479 (defvar gnus-newsgroup-forwarded nil
1480   "List of articles that have been forwarded in the current newsgroup.")
1481
1482 (defvar gnus-newsgroup-recent nil
1483   "List of articles that have are recent in the current newsgroup.")
1484
1485 (defvar gnus-newsgroup-expirable nil
1486   "Sorted list of articles in the current newsgroup that can be expired.")
1487
1488 (defvar gnus-newsgroup-processable nil
1489   "List of articles in the current newsgroup that can be processed.")
1490
1491 (defvar gnus-newsgroup-downloadable nil
1492   "Sorted list of articles in the current newsgroup that can be processed.")
1493
1494 (defvar gnus-newsgroup-unfetched nil
1495   "Sorted list of articles in the current newsgroup whose headers have
1496 not been fetched into the agent.
1497
1498 This list will always be a subset of gnus-newsgroup-undownloaded.")
1499
1500 (defvar gnus-newsgroup-undownloaded nil
1501   "List of articles in the current newsgroup that haven't been downloaded.")
1502
1503 (defvar gnus-newsgroup-unsendable nil
1504   "List of articles in the current newsgroup that won't be sent.")
1505
1506 (defvar gnus-newsgroup-bookmarks nil
1507   "List of articles in the current newsgroup that have bookmarks.")
1508
1509 (defvar gnus-newsgroup-dormant nil
1510   "Sorted list of dormant articles in the current newsgroup.")
1511
1512 (defvar gnus-newsgroup-unseen nil
1513   "List of unseen articles in the current newsgroup.")
1514
1515 (defvar gnus-newsgroup-seen nil
1516   "Range of seen articles in the current newsgroup.")
1517
1518 (defvar gnus-newsgroup-articles nil
1519   "List of articles in the current newsgroup.")
1520
1521 (defvar gnus-newsgroup-scored nil
1522   "List of scored articles in the current newsgroup.")
1523
1524 (defvar gnus-newsgroup-headers nil
1525   "List of article headers in the current newsgroup.")
1526
1527 (defvar gnus-newsgroup-threads nil)
1528
1529 (defvar gnus-newsgroup-prepared nil
1530   "Whether the current group has been prepared properly.")
1531
1532 (defvar gnus-newsgroup-ancient nil
1533   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1534
1535 (defvar gnus-newsgroup-sparse nil)
1536
1537 (defvar gnus-current-article nil)
1538 (defvar gnus-article-current nil)
1539 (defvar gnus-current-headers nil)
1540 (defvar gnus-have-all-headers nil)
1541 (defvar gnus-last-article nil)
1542 (defvar gnus-newsgroup-history nil)
1543 (defvar gnus-newsgroup-charset nil)
1544 (defvar gnus-newsgroup-ephemeral-charset nil)
1545 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1546
1547 (defvar gnus-article-before-search nil)
1548
1549 (defvar gnus-summary-local-variables
1550   '(gnus-newsgroup-name
1551
1552     ;; Marks lists
1553     gnus-newsgroup-unreads
1554     gnus-newsgroup-unselected
1555     gnus-newsgroup-marked
1556     gnus-newsgroup-spam-marked
1557     gnus-newsgroup-reads
1558     gnus-newsgroup-saved
1559     gnus-newsgroup-replied
1560     gnus-newsgroup-forwarded
1561     gnus-newsgroup-recent
1562     gnus-newsgroup-expirable
1563     gnus-newsgroup-killed
1564     gnus-newsgroup-unseen
1565     gnus-newsgroup-seen
1566     gnus-newsgroup-cached
1567     gnus-newsgroup-downloadable
1568     gnus-newsgroup-undownloaded
1569     gnus-newsgroup-unsendable
1570
1571     gnus-newsgroup-begin gnus-newsgroup-end
1572     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1573     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1574     gnus-newsgroup-last-directory
1575     gnus-newsgroup-auto-expire
1576     gnus-newsgroup-processable
1577     gnus-newsgroup-unfetched
1578     gnus-newsgroup-articles
1579     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1580     gnus-newsgroup-headers gnus-newsgroup-threads
1581     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1582     gnus-current-article gnus-current-headers gnus-have-all-headers
1583     gnus-last-article gnus-article-internal-prepare-hook
1584     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1585     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1586     gnus-thread-expunge-below
1587     gnus-score-alist gnus-current-score-file
1588     (gnus-summary-expunge-below . global)
1589     (gnus-summary-mark-below . global)
1590     (gnus-orphan-score . global)
1591     gnus-newsgroup-active gnus-scores-exclude-files
1592     gnus-newsgroup-highest
1593     gnus-newsgroup-history gnus-newsgroup-ancient
1594     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1595     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1596     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1597     (gnus-newsgroup-expunged-tally . 0)
1598     gnus-cache-removable-articles
1599     gnus-newsgroup-data gnus-newsgroup-data-reverse
1600     gnus-newsgroup-limit gnus-newsgroup-limits
1601     gnus-newsgroup-charset gnus-newsgroup-display
1602     gnus-summary-use-undownloaded-faces)
1603   "Variables that are buffer-local to the summary buffers.")
1604
1605 (defvar gnus-newsgroup-variables nil
1606   "A list of variables that have separate values in different newsgroups.
1607 A list of newsgroup (summary buffer) local variables, or cons of
1608 variables and their default expressions to be evalled (when the default
1609 values are not nil), that should be made global while the summary buffer
1610 is active.
1611
1612 Note: The default expressions will be evaluated (using function `eval')
1613 before assignment to the local variable rather than just assigned to it.
1614 If the default expression is the symbol `global', that symbol will not
1615 be evaluated but the global value of the local variable will be used
1616 instead.
1617
1618 These variables can be used to set variables in the group parameters
1619 while still allowing them to affect operations done in other buffers.
1620 For example:
1621
1622 \(setq gnus-newsgroup-variables
1623      '(message-use-followup-to
1624        (gnus-visible-headers .
1625          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1626 ")
1627
1628 (eval-when-compile
1629   ;; Bind features so that require will believe that gnus-sum has
1630   ;; already been loaded (avoids infinite recursion)
1631   (let ((features (cons 'gnus-sum features)))
1632     (require 'gnus-art)))
1633
1634 ;; MIME stuff.
1635
1636 (defvar gnus-decode-encoded-word-methods
1637   '(mail-decode-encoded-word-string)
1638   "List of methods used to decode encoded words.
1639
1640 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1641 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1642 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1643 whose names match REGEXP.
1644
1645 For example:
1646 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1647  mail-decode-encoded-word-string
1648  (\"chinese\" . rfc1843-decode-string))")
1649
1650 (defvar gnus-decode-encoded-word-methods-cache nil)
1651
1652 (defun gnus-multi-decode-encoded-word-string (string)
1653   "Apply the functions from `gnus-encoded-word-methods' that match."
1654   (unless (and gnus-decode-encoded-word-methods-cache
1655                (eq gnus-newsgroup-name
1656                    (car gnus-decode-encoded-word-methods-cache)))
1657     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1658     (dolist (method gnus-decode-encoded-word-methods)
1659       (if (symbolp method)
1660           (nconc gnus-decode-encoded-word-methods-cache (list method))
1661         (if (and gnus-newsgroup-name
1662                  (string-match (car method) gnus-newsgroup-name))
1663             (nconc gnus-decode-encoded-word-methods-cache
1664                    (list (cdr method)))))))
1665   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1666     (setq string (funcall method string))))
1667
1668 ;; Subject simplification.
1669
1670 (defun gnus-simplify-whitespace (str)
1671   "Remove excessive whitespace from STR."
1672   ;; Multiple spaces.
1673   (while (string-match "[ \t][ \t]+" str)
1674     (setq str (concat (substring str 0 (match-beginning 0))
1675                         " "
1676                         (substring str (match-end 0)))))
1677   ;; Leading spaces.
1678   (when (string-match "^[ \t]+" str)
1679     (setq str (substring str (match-end 0))))
1680   ;; Trailing spaces.
1681   (when (string-match "[ \t]+$" str)
1682     (setq str (substring str 0 (match-beginning 0))))
1683   str)
1684
1685 (defun gnus-simplify-all-whitespace (str)
1686   "Remove all whitespace from STR."
1687   (while (string-match "[ \t\n]+" str)
1688     (setq str (replace-match "" nil nil str)))
1689   str)
1690
1691 (defsubst gnus-simplify-subject-re (subject)
1692   "Remove \"Re:\" from subject lines."
1693   (if (string-match message-subject-re-regexp subject)
1694       (substring subject (match-end 0))
1695     subject))
1696
1697 (defun gnus-simplify-subject (subject &optional re-only)
1698   "Remove `Re:' and words in parentheses.
1699 If RE-ONLY is non-nil, strip leading `Re:'s only."
1700   (let ((case-fold-search t))           ;Ignore case.
1701     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1702     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1703       (setq subject (substring subject (match-end 0))))
1704     ;; Remove uninteresting prefixes.
1705     (when (and (not re-only)
1706                gnus-simplify-ignored-prefixes
1707                (string-match gnus-simplify-ignored-prefixes subject))
1708       (setq subject (substring subject (match-end 0))))
1709     ;; Remove words in parentheses from end.
1710     (unless re-only
1711       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1712         (setq subject (substring subject 0 (match-beginning 0)))))
1713     ;; Return subject string.
1714     subject))
1715
1716 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1717 ;; all whitespace.
1718 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1719   (goto-char (point-min))
1720   (while (re-search-forward regexp nil t)
1721     (replace-match (or newtext ""))))
1722
1723 (defun gnus-simplify-buffer-fuzzy ()
1724   "Simplify string in the buffer fuzzily.
1725 The string in the accessible portion of the current buffer is simplified.
1726 It is assumed to be a single-line subject.
1727 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1728 matter is removed.  Additional things can be deleted by setting
1729 `gnus-simplify-subject-fuzzy-regexp'."
1730   (let ((case-fold-search t)
1731         (modified-tick))
1732     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1733
1734     (while (not (eq modified-tick (buffer-modified-tick)))
1735       (setq modified-tick (buffer-modified-tick))
1736       (cond
1737        ((listp gnus-simplify-subject-fuzzy-regexp)
1738         (mapc 'gnus-simplify-buffer-fuzzy-step
1739               gnus-simplify-subject-fuzzy-regexp))
1740        (gnus-simplify-subject-fuzzy-regexp
1741         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1742       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1743       (gnus-simplify-buffer-fuzzy-step
1744        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1745       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1746
1747     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1748     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1749     (gnus-simplify-buffer-fuzzy-step " $")
1750     (gnus-simplify-buffer-fuzzy-step "^ +")))
1751
1752 (defun gnus-simplify-subject-fuzzy (subject)
1753   "Simplify a subject string fuzzily.
1754 See `gnus-simplify-buffer-fuzzy' for details."
1755   (save-excursion
1756     (gnus-set-work-buffer)
1757     (let ((case-fold-search t))
1758       ;; Remove uninteresting prefixes.
1759       (when (and gnus-simplify-ignored-prefixes
1760                  (string-match gnus-simplify-ignored-prefixes subject))
1761         (setq subject (substring subject (match-end 0))))
1762       (insert subject)
1763       (inline (gnus-simplify-buffer-fuzzy))
1764       (buffer-string))))
1765
1766 (defsubst gnus-simplify-subject-fully (subject)
1767   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1768   (cond
1769    (gnus-simplify-subject-functions
1770     (gnus-map-function gnus-simplify-subject-functions subject))
1771    ((null gnus-summary-gather-subject-limit)
1772     (gnus-simplify-subject-re subject))
1773    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1774     (gnus-simplify-subject-fuzzy subject))
1775    ((numberp gnus-summary-gather-subject-limit)
1776     (truncate-string-to-width (gnus-simplify-subject-re subject)
1777                               gnus-summary-gather-subject-limit))
1778    (t
1779     subject)))
1780
1781 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1782   "Check whether two subjects are equal.
1783 If optional argument SIMPLE-FIRST is t, first argument is already
1784 simplified."
1785   (cond
1786    ((null simple-first)
1787     (equal (gnus-simplify-subject-fully s1)
1788            (gnus-simplify-subject-fully s2)))
1789    (t
1790     (equal s1
1791            (gnus-simplify-subject-fully s2)))))
1792
1793 (defun gnus-summary-bubble-group ()
1794   "Increase the score of the current group.
1795 This is a handy function to add to `gnus-summary-exit-hook' to
1796 increase the score of each group you read."
1797   (gnus-group-add-score gnus-newsgroup-name))
1798
1799 \f
1800 ;;;
1801 ;;; Gnus summary mode
1802 ;;;
1803
1804 (put 'gnus-summary-mode 'mode-class 'special)
1805
1806 (defvar gnus-article-commands-menu)
1807
1808 ;; Non-orthogonal keys
1809
1810 (gnus-define-keys gnus-summary-mode-map
1811   " " gnus-summary-next-page
1812   "\177" gnus-summary-prev-page
1813   [delete] gnus-summary-prev-page
1814   [backspace] gnus-summary-prev-page
1815   "\r" gnus-summary-scroll-up
1816   "\M-\r" gnus-summary-scroll-down
1817   "n" gnus-summary-next-unread-article
1818   "p" gnus-summary-prev-unread-article
1819   "N" gnus-summary-next-article
1820   "P" gnus-summary-prev-article
1821   "\M-\C-n" gnus-summary-next-same-subject
1822   "\M-\C-p" gnus-summary-prev-same-subject
1823   "\M-n" gnus-summary-next-unread-subject
1824   "\M-p" gnus-summary-prev-unread-subject
1825   "." gnus-summary-first-unread-article
1826   "," gnus-summary-best-unread-article
1827   "\M-s" gnus-summary-search-article-forward
1828   "\M-r" gnus-summary-search-article-backward
1829   "\M-S" gnus-summary-repeat-search-article-forward
1830   "\M-R" gnus-summary-repeat-search-article-backward
1831   "<" gnus-summary-beginning-of-article
1832   ">" gnus-summary-end-of-article
1833   "j" gnus-summary-goto-article
1834   "^" gnus-summary-refer-parent-article
1835   "\M-^" gnus-summary-refer-article
1836   "u" gnus-summary-tick-article-forward
1837   "!" gnus-summary-tick-article-forward
1838   "U" gnus-summary-tick-article-backward
1839   "d" gnus-summary-mark-as-read-forward
1840   "D" gnus-summary-mark-as-read-backward
1841   "E" gnus-summary-mark-as-expirable
1842   "\M-u" gnus-summary-clear-mark-forward
1843   "\M-U" gnus-summary-clear-mark-backward
1844   "k" gnus-summary-kill-same-subject-and-select
1845   "\C-k" gnus-summary-kill-same-subject
1846   "\M-\C-k" gnus-summary-kill-thread
1847   "\M-\C-l" gnus-summary-lower-thread
1848   "e" gnus-summary-edit-article
1849   "#" gnus-summary-mark-as-processable
1850   "\M-#" gnus-summary-unmark-as-processable
1851   "\M-\C-t" gnus-summary-toggle-threads
1852   "\M-\C-s" gnus-summary-show-thread
1853   "\M-\C-h" gnus-summary-hide-thread
1854   "\M-\C-f" gnus-summary-next-thread
1855   "\M-\C-b" gnus-summary-prev-thread
1856   [(meta down)] gnus-summary-next-thread
1857   [(meta up)] gnus-summary-prev-thread
1858   "\M-\C-u" gnus-summary-up-thread
1859   "\M-\C-d" gnus-summary-down-thread
1860   "&" gnus-summary-execute-command
1861   "c" gnus-summary-catchup-and-exit
1862   "\C-w" gnus-summary-mark-region-as-read
1863   "\C-t" gnus-summary-toggle-truncation
1864   "?" gnus-summary-mark-as-dormant
1865   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1866   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1867   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1868   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1869   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1870   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1871   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1872   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1873   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1874   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1875   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1876   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1877   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1878   "=" gnus-summary-expand-window
1879   "\C-x\C-s" gnus-summary-reselect-current-group
1880   "\M-g" gnus-summary-rescan-group
1881   "\C-c\C-r" gnus-summary-caesar-message
1882   "f" gnus-summary-followup
1883   "F" gnus-summary-followup-with-original
1884   "C" gnus-summary-cancel-article
1885   "r" gnus-summary-reply
1886   "R" gnus-summary-reply-with-original
1887   "\C-c\C-f" gnus-summary-mail-forward
1888   "o" gnus-summary-save-article
1889   "\C-o" gnus-summary-save-article-mail
1890   "|" gnus-summary-pipe-output
1891   "\M-k" gnus-summary-edit-local-kill
1892   "\M-K" gnus-summary-edit-global-kill
1893   ;; "V" gnus-version
1894   "\C-c\C-d" gnus-summary-describe-group
1895   "q" gnus-summary-exit
1896   "Q" gnus-summary-exit-no-update
1897   "\C-c\C-i" gnus-info-find-node
1898   gnus-mouse-2 gnus-mouse-pick-article
1899   [follow-link] mouse-face
1900   "m" gnus-summary-mail-other-window
1901   "a" gnus-summary-post-news
1902   "x" gnus-summary-limit-to-unread
1903   "s" gnus-summary-isearch-article
1904   "t" gnus-summary-toggle-header
1905   "g" gnus-summary-show-article
1906   "l" gnus-summary-goto-last-article
1907   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1908   "\C-d" gnus-summary-enter-digest-group
1909   "\M-\C-d" gnus-summary-read-document
1910   "\M-\C-e" gnus-summary-edit-parameters
1911   "\M-\C-a" gnus-summary-customize-parameters
1912   "\C-c\C-b" gnus-bug
1913   "*" gnus-cache-enter-article
1914   "\M-*" gnus-cache-remove-article
1915   "\M-&" gnus-summary-universal-argument
1916   "\C-l" gnus-recenter
1917   "I" gnus-summary-increase-score
1918   "L" gnus-summary-lower-score
1919   "\M-i" gnus-symbolic-argument
1920   "h" gnus-summary-select-article-buffer
1921
1922   "b" gnus-article-view-part
1923   "\M-t" gnus-summary-toggle-display-buttonized
1924
1925   "V" gnus-summary-score-map
1926   "X" gnus-uu-extract-map
1927   "S" gnus-summary-send-map)
1928
1929 ;; Sort of orthogonal keymap
1930 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1931   "t" gnus-summary-tick-article-forward
1932   "!" gnus-summary-tick-article-forward
1933   "d" gnus-summary-mark-as-read-forward
1934   "r" gnus-summary-mark-as-read-forward
1935   "c" gnus-summary-clear-mark-forward
1936   " " gnus-summary-clear-mark-forward
1937   "e" gnus-summary-mark-as-expirable
1938   "x" gnus-summary-mark-as-expirable
1939   "?" gnus-summary-mark-as-dormant
1940   "b" gnus-summary-set-bookmark
1941   "B" gnus-summary-remove-bookmark
1942   "#" gnus-summary-mark-as-processable
1943   "\M-#" gnus-summary-unmark-as-processable
1944   "S" gnus-summary-limit-include-expunged
1945   "C" gnus-summary-catchup
1946   "H" gnus-summary-catchup-to-here
1947   "h" gnus-summary-catchup-from-here
1948   "\C-c" gnus-summary-catchup-all
1949   "k" gnus-summary-kill-same-subject-and-select
1950   "K" gnus-summary-kill-same-subject
1951   "P" gnus-uu-mark-map)
1952
1953 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1954   "c" gnus-summary-clear-above
1955   "u" gnus-summary-tick-above
1956   "m" gnus-summary-mark-above
1957   "k" gnus-summary-kill-below)
1958
1959 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1960   "/" gnus-summary-limit-to-subject
1961   "n" gnus-summary-limit-to-articles
1962   "b" gnus-summary-limit-to-bodies
1963   "h" gnus-summary-limit-to-headers
1964   "w" gnus-summary-pop-limit
1965   "s" gnus-summary-limit-to-subject
1966   "a" gnus-summary-limit-to-author
1967   "u" gnus-summary-limit-to-unread
1968   "m" gnus-summary-limit-to-marks
1969   "M" gnus-summary-limit-exclude-marks
1970   "v" gnus-summary-limit-to-score
1971   "*" gnus-summary-limit-include-cached
1972   "D" gnus-summary-limit-include-dormant
1973   "T" gnus-summary-limit-include-thread
1974   "d" gnus-summary-limit-exclude-dormant
1975   "t" gnus-summary-limit-to-age
1976   "." gnus-summary-limit-to-unseen
1977   "x" gnus-summary-limit-to-extra
1978   "p" gnus-summary-limit-to-display-predicate
1979   "E" gnus-summary-limit-include-expunged
1980   "c" gnus-summary-limit-exclude-childless-dormant
1981   "C" gnus-summary-limit-mark-excluded-as-read
1982   "o" gnus-summary-insert-old-articles
1983   "N" gnus-summary-insert-new-articles
1984   "S" gnus-summary-limit-to-singletons
1985   "r" gnus-summary-limit-to-replied
1986   "R" gnus-summary-limit-to-recipient
1987   "A" gnus-summary-limit-to-address)
1988
1989 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1990   "n" gnus-summary-next-unread-article
1991   "p" gnus-summary-prev-unread-article
1992   "N" gnus-summary-next-article
1993   "P" gnus-summary-prev-article
1994   "\C-n" gnus-summary-next-same-subject
1995   "\C-p" gnus-summary-prev-same-subject
1996   "\M-n" gnus-summary-next-unread-subject
1997   "\M-p" gnus-summary-prev-unread-subject
1998   "f" gnus-summary-first-unread-article
1999   "b" gnus-summary-best-unread-article
2000   "j" gnus-summary-goto-article
2001   "g" gnus-summary-goto-subject
2002   "l" gnus-summary-goto-last-article
2003   "o" gnus-summary-pop-article)
2004
2005 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2006   "k" gnus-summary-kill-thread
2007   "E" gnus-summary-expire-thread
2008   "l" gnus-summary-lower-thread
2009   "i" gnus-summary-raise-thread
2010   "T" gnus-summary-toggle-threads
2011   "t" gnus-summary-rethread-current
2012   "^" gnus-summary-reparent-thread
2013   "\M-^" gnus-summary-reparent-children
2014   "s" gnus-summary-show-thread
2015   "S" gnus-summary-show-all-threads
2016   "h" gnus-summary-hide-thread
2017   "H" gnus-summary-hide-all-threads
2018   "n" gnus-summary-next-thread
2019   "p" gnus-summary-prev-thread
2020   "u" gnus-summary-up-thread
2021   "o" gnus-summary-top-thread
2022   "d" gnus-summary-down-thread
2023   "#" gnus-uu-mark-thread
2024   "\M-#" gnus-uu-unmark-thread)
2025
2026 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2027   "g" gnus-summary-prepare
2028   "c" gnus-summary-insert-cached-articles
2029   "d" gnus-summary-insert-dormant-articles
2030   "t" gnus-summary-insert-ticked-articles)
2031
2032 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2033   "c" gnus-summary-catchup-and-exit
2034   "C" gnus-summary-catchup-all-and-exit
2035   "E" gnus-summary-exit-no-update
2036   "Q" gnus-summary-exit
2037   "Z" gnus-summary-exit
2038   "n" gnus-summary-catchup-and-goto-next-group
2039   "p" gnus-summary-catchup-and-goto-prev-group
2040   "R" gnus-summary-reselect-current-group
2041   "G" gnus-summary-rescan-group
2042   "N" gnus-summary-next-group
2043   "s" gnus-summary-save-newsrc
2044   "P" gnus-summary-prev-group)
2045
2046 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2047   " " gnus-summary-next-page
2048   "n" gnus-summary-next-page
2049   "\177" gnus-summary-prev-page
2050   [delete] gnus-summary-prev-page
2051   "p" gnus-summary-prev-page
2052   "\r" gnus-summary-scroll-up
2053   "\M-\r" gnus-summary-scroll-down
2054   "<" gnus-summary-beginning-of-article
2055   ">" gnus-summary-end-of-article
2056   "b" gnus-summary-beginning-of-article
2057   "e" gnus-summary-end-of-article
2058   "^" gnus-summary-refer-parent-article
2059   "r" gnus-summary-refer-parent-article
2060   "C" gnus-summary-show-complete-article
2061   "D" gnus-summary-enter-digest-group
2062   "R" gnus-summary-refer-references
2063   "T" gnus-summary-refer-thread
2064   "W" gnus-warp-to-article
2065   "g" gnus-summary-show-article
2066   "s" gnus-summary-isearch-article
2067   "P" gnus-summary-print-article
2068   "S" gnus-sticky-article
2069   "M" gnus-mailing-list-insinuate
2070   "t" gnus-article-babel)
2071
2072 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2073   "b" gnus-article-add-buttons
2074   "B" gnus-article-add-buttons-to-head
2075   "o" gnus-article-treat-overstrike
2076   "e" gnus-article-emphasize
2077   "w" gnus-article-fill-cited-article
2078   "Q" gnus-article-fill-long-lines
2079   "L" gnus-article-toggle-truncate-lines
2080   "C" gnus-article-capitalize-sentences
2081   "c" gnus-article-remove-cr
2082   "q" gnus-article-de-quoted-unreadable
2083   "6" gnus-article-de-base64-unreadable
2084   "Z" gnus-article-decode-HZ
2085   "A" gnus-article-treat-ansi-sequences
2086   "h" gnus-article-wash-html
2087   "u" gnus-article-unsplit-urls
2088   "s" gnus-summary-force-verify-and-decrypt
2089   "f" gnus-article-display-x-face
2090   "l" gnus-summary-stop-page-breaking
2091   "r" gnus-summary-caesar-message
2092   "m" gnus-summary-morse-message
2093   "t" gnus-summary-toggle-header
2094   "g" gnus-treat-smiley
2095   "v" gnus-summary-verbose-headers
2096   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2097   "p" gnus-article-verify-x-pgp-sig
2098   "d" gnus-article-treat-dumbquotes
2099   "U" gnus-article-treat-non-ascii
2100   "i" gnus-summary-idna-message)
2101
2102 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2103   ;; mnemonic: deuglif*Y*
2104   "u" gnus-article-outlook-unwrap-lines
2105   "a" gnus-article-outlook-repair-attribution
2106   "c" gnus-article-outlook-rearrange-citation
2107   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2108
2109 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2110   "a" gnus-article-hide
2111   "h" gnus-article-hide-headers
2112   "b" gnus-article-hide-boring-headers
2113   "s" gnus-article-hide-signature
2114   "c" gnus-article-hide-citation
2115   "C" gnus-article-hide-citation-in-followups
2116   "l" gnus-article-hide-list-identifiers
2117   "B" gnus-article-strip-banner
2118   "P" gnus-article-hide-pem
2119   "\C-c" gnus-article-hide-citation-maybe)
2120
2121 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2122   "a" gnus-article-highlight
2123   "h" gnus-article-highlight-headers
2124   "c" gnus-article-highlight-citation
2125   "s" gnus-article-highlight-signature)
2126
2127 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2128   "f" gnus-article-treat-fold-headers
2129   "u" gnus-article-treat-unfold-headers
2130   "n" gnus-article-treat-fold-newsgroups)
2131
2132 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2133   "x" gnus-article-display-x-face
2134   "d" gnus-article-display-face
2135   "s" gnus-treat-smiley
2136   "D" gnus-article-remove-images
2137   "W" gnus-html-show-images
2138   "f" gnus-treat-from-picon
2139   "m" gnus-treat-mail-picon
2140   "n" gnus-treat-newsgroups-picon
2141   "g" gnus-treat-from-gravatar
2142   "h" gnus-treat-mail-gravatar)
2143
2144 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2145   "w" gnus-article-decode-mime-words
2146   "c" gnus-article-decode-charset
2147   "v" gnus-mime-view-all-parts
2148   "b" gnus-article-view-part)
2149
2150 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2151   "z" gnus-article-date-ut
2152   "u" gnus-article-date-ut
2153   "l" gnus-article-date-local
2154   "p" gnus-article-date-english
2155   "e" gnus-article-date-lapsed
2156   "o" gnus-article-date-original
2157   "i" gnus-article-date-iso8601
2158   "s" gnus-article-date-user)
2159
2160 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2161   "t" gnus-article-remove-trailing-blank-lines
2162   "l" gnus-article-strip-leading-blank-lines
2163   "m" gnus-article-strip-multiple-blank-lines
2164   "a" gnus-article-strip-blank-lines
2165   "A" gnus-article-strip-all-blank-lines
2166   "s" gnus-article-strip-leading-space
2167   "e" gnus-article-strip-trailing-space
2168   "w" gnus-article-remove-leading-whitespace)
2169
2170 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2171   "v" gnus-version
2172   "d" gnus-summary-describe-group
2173   "h" gnus-summary-describe-briefly
2174   "i" gnus-info-find-node)
2175
2176 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2177   "e" gnus-summary-expire-articles
2178   "\M-\C-e" gnus-summary-expire-articles-now
2179   "\177" gnus-summary-delete-article
2180   [delete] gnus-summary-delete-article
2181   [backspace] gnus-summary-delete-article
2182   "m" gnus-summary-move-article
2183   "r" gnus-summary-respool-article
2184   "w" gnus-summary-edit-article
2185   "c" gnus-summary-copy-article
2186   "B" gnus-summary-crosspost-article
2187   "q" gnus-summary-respool-query
2188   "t" gnus-summary-respool-trace
2189   "i" gnus-summary-import-article
2190   "I" gnus-summary-create-article
2191   "p" gnus-summary-article-posted-p)
2192
2193 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2194   "o" gnus-summary-save-article
2195   "m" gnus-summary-save-article-mail
2196   "F" gnus-summary-write-article-file
2197   "r" gnus-summary-save-article-rmail
2198   "f" gnus-summary-save-article-file
2199   "b" gnus-summary-save-article-body-file
2200   "B" gnus-summary-write-article-body-file
2201   "h" gnus-summary-save-article-folder
2202   "v" gnus-summary-save-article-vm
2203   "p" gnus-summary-pipe-output
2204   "P" gnus-summary-muttprint)
2205
2206 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2207   "b" gnus-summary-display-buttonized
2208   "m" gnus-summary-repair-multipart
2209   "v" gnus-article-view-part
2210   "o" gnus-article-save-part
2211   "O" gnus-article-save-part-and-strip
2212   "r" gnus-article-replace-part
2213   "d" gnus-article-delete-part
2214   "t" gnus-article-view-part-as-type
2215   "j" gnus-article-jump-to-part
2216   "c" gnus-article-copy-part
2217   "C" gnus-article-view-part-as-charset
2218   "e" gnus-article-view-part-externally
2219   "H" gnus-article-browse-html-article
2220   "E" gnus-article-encrypt-body
2221   "i" gnus-article-inline-part
2222   "|" gnus-article-pipe-part)
2223
2224 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2225   "p" gnus-summary-mark-as-processable
2226   "u" gnus-summary-unmark-as-processable
2227   "U" gnus-summary-unmark-all-processable
2228   "v" gnus-uu-mark-over
2229   "s" gnus-uu-mark-series
2230   "r" gnus-uu-mark-region
2231   "g" gnus-uu-unmark-region
2232   "R" gnus-uu-mark-by-regexp
2233   "G" gnus-uu-unmark-by-regexp
2234   "t" gnus-uu-mark-thread
2235   "T" gnus-uu-unmark-thread
2236   "a" gnus-uu-mark-all
2237   "b" gnus-uu-mark-buffer
2238   "S" gnus-uu-mark-sparse
2239   "k" gnus-summary-kill-process-mark
2240   "y" gnus-summary-yank-process-mark
2241   "w" gnus-summary-save-process-mark
2242   "i" gnus-uu-invert-processable)
2243
2244 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2245   ;;"x" gnus-uu-extract-any
2246   "m" gnus-summary-save-parts
2247   "u" gnus-uu-decode-uu
2248   "U" gnus-uu-decode-uu-and-save
2249   "s" gnus-uu-decode-unshar
2250   "S" gnus-uu-decode-unshar-and-save
2251   "o" gnus-uu-decode-save
2252   "O" gnus-uu-decode-save
2253   "b" gnus-uu-decode-binhex
2254   "B" gnus-uu-decode-binhex
2255   "Y" gnus-uu-decode-yenc
2256   "p" gnus-uu-decode-postscript
2257   "P" gnus-uu-decode-postscript-and-save)
2258
2259 (gnus-define-keys
2260     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2261   "u" gnus-uu-decode-uu-view
2262   "U" gnus-uu-decode-uu-and-save-view
2263   "s" gnus-uu-decode-unshar-view
2264   "S" gnus-uu-decode-unshar-and-save-view
2265   "o" gnus-uu-decode-save-view
2266   "O" gnus-uu-decode-save-view
2267   "b" gnus-uu-decode-binhex-view
2268   "B" gnus-uu-decode-binhex-view
2269   "p" gnus-uu-decode-postscript-view
2270   "P" gnus-uu-decode-postscript-and-save-view)
2271
2272 (defvar gnus-article-post-menu nil)
2273
2274 (defconst gnus-summary-menu-maxlen 20)
2275
2276 (defun gnus-summary-menu-split (menu)
2277   ;; If we have lots of elements, divide them into groups of 20
2278   ;; and make a pane (or submenu) for each one.
2279   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2280       (let ((menu menu) sublists next
2281             (i 1))
2282         (while menu
2283           ;; Pull off the next gnus-summary-menu-maxlen elements
2284           ;; and make them the next element of sublist.
2285           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2286           (if next
2287               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2288                       nil))
2289           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2290                                              (aref (car (last menu)) 0)) menu)
2291                                sublists))
2292           (setq i (1+ i))
2293           (setq menu next))
2294         (nreverse sublists))
2295     ;; Few elements--put them all in one pane.
2296     menu))
2297
2298 (defun gnus-summary-make-menu-bar ()
2299   (gnus-turn-off-edit-menu 'summary)
2300
2301   (unless (boundp 'gnus-summary-misc-menu)
2302
2303     (easy-menu-define
2304       gnus-summary-kill-menu gnus-summary-mode-map ""
2305       (cons
2306        "Score"
2307        (nconc
2308         (list
2309          ["Customize" gnus-score-customize t])
2310         (gnus-make-score-map 'increase)
2311         (gnus-make-score-map 'lower)
2312         '(("Mark"
2313            ["Kill below" gnus-summary-kill-below t]
2314            ["Mark above" gnus-summary-mark-above t]
2315            ["Tick above" gnus-summary-tick-above t]
2316            ["Clear above" gnus-summary-clear-above t])
2317           ["Current score" gnus-summary-current-score t]
2318           ["Set score" gnus-summary-set-score t]
2319           ["Switch current score file..." gnus-score-change-score-file t]
2320           ["Set mark below..." gnus-score-set-mark-below t]
2321           ["Set expunge below..." gnus-score-set-expunge-below t]
2322           ["Edit current score file" gnus-score-edit-current-scores t]
2323           ["Edit score file..." gnus-score-edit-file t]
2324           ["Trace score" gnus-score-find-trace t]
2325           ["Find words" gnus-score-find-favourite-words t]
2326           ["Rescore buffer" gnus-summary-rescore t]
2327           ["Increase score..." gnus-summary-increase-score t]
2328           ["Lower score..." gnus-summary-lower-score t]))))
2329
2330     ;; Define both the Article menu in the summary buffer and the
2331     ;; equivalent Commands menu in the article buffer here for
2332     ;; consistency.
2333     (let ((innards
2334            `(("Hide"
2335               ["All" gnus-article-hide t]
2336               ["Headers" gnus-article-hide-headers t]
2337               ["Signature" gnus-article-hide-signature t]
2338               ["Citation" gnus-article-hide-citation t]
2339               ["List identifiers" gnus-article-hide-list-identifiers t]
2340               ["Banner" gnus-article-strip-banner t]
2341               ["Boring headers" gnus-article-hide-boring-headers t])
2342              ("Highlight"
2343               ["All" gnus-article-highlight t]
2344               ["Headers" gnus-article-highlight-headers t]
2345               ["Signature" gnus-article-highlight-signature t]
2346               ["Citation" gnus-article-highlight-citation t])
2347              ("MIME"
2348               ["Words" gnus-article-decode-mime-words t]
2349               ["Charset" gnus-article-decode-charset t]
2350               ["QP" gnus-article-de-quoted-unreadable t]
2351               ["Base64" gnus-article-de-base64-unreadable t]
2352               ["View MIME buttons" gnus-summary-display-buttonized t]
2353               ["View all" gnus-mime-view-all-parts t]
2354               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2355               ["Encrypt body" gnus-article-encrypt-body
2356                :active (not (gnus-group-read-only-p))
2357                ,@(if (featurep 'xemacs) nil
2358                    '(:help "Encrypt the message body on disk"))]
2359               ["Extract all parts..." gnus-summary-save-parts t]
2360               ("Multipart"
2361                ["Repair multipart" gnus-summary-repair-multipart t]
2362                ["Pipe part..." gnus-article-pipe-part t]
2363                ["Inline part" gnus-article-inline-part t]
2364                ["View part as type..." gnus-article-view-part-as-type t]
2365                ["Encrypt body" gnus-article-encrypt-body
2366                 :active (not (gnus-group-read-only-p))
2367                ,@(if (featurep 'xemacs) nil
2368                    '(:help "Encrypt the message body on disk"))]
2369                ["View part externally" gnus-article-view-part-externally t]
2370                ["View HTML parts in browser" gnus-article-browse-html-article t]
2371                ["View part with charset..." gnus-article-view-part-as-charset t]
2372                ["Copy part" gnus-article-copy-part t]
2373                ["Save part..." gnus-article-save-part t]
2374                ["View part" gnus-article-view-part t]))
2375              ("Date"
2376               ["Local" gnus-article-date-local t]
2377               ["ISO8601" gnus-article-date-iso8601 t]
2378               ["UT" gnus-article-date-ut t]
2379               ["Original" gnus-article-date-original t]
2380               ["Lapsed" gnus-article-date-lapsed t]
2381               ["User-defined" gnus-article-date-user t])
2382              ("Display"
2383               ["Remove images" gnus-article-remove-images t]
2384               ["Toggle smiley" gnus-treat-smiley t]
2385               ["Show X-Face" gnus-article-display-x-face t]
2386               ["Show picons in From" gnus-treat-from-picon t]
2387               ["Show picons in mail headers" gnus-treat-mail-picon t]
2388               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2389               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2390               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2391               ("View as different encoding"
2392                ,@(gnus-summary-menu-split
2393                   (mapcar
2394                    (lambda (cs)
2395                      ;; Since easymenu under Emacs doesn't allow
2396                      ;; lambda forms for menu commands, we should
2397                      ;; provide intern'ed function symbols.
2398                      (let ((command (intern (format "\
2399 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2400                        (fset command
2401                              `(lambda ()
2402                                 (interactive)
2403                                 (let ((gnus-summary-show-article-charset-alist
2404                                        '((1 . ,cs))))
2405                                   (gnus-summary-show-article 1))))
2406                        `[,(symbol-name cs) ,command t]))
2407                    (sort (if (fboundp 'coding-system-list)
2408                              (coding-system-list)
2409                            (mapcar 'car mm-mime-mule-charset-alist))
2410                          'string<)))))
2411              ("Washing"
2412               ("Remove Blanks"
2413                ["Leading" gnus-article-strip-leading-blank-lines t]
2414                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2415                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2416                ["All of the above" gnus-article-strip-blank-lines t]
2417                ["All" gnus-article-strip-all-blank-lines t]
2418                ["Leading space" gnus-article-strip-leading-space t]
2419                ["Trailing space" gnus-article-strip-trailing-space t]
2420                ["Leading space in headers"
2421                 gnus-article-remove-leading-whitespace t])
2422               ["Overstrike" gnus-article-treat-overstrike t]
2423               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2424               ["Non-ASCII" gnus-article-treat-non-ascii t]
2425               ["Emphasis" gnus-article-emphasize t]
2426               ["Word wrap" gnus-article-fill-cited-article t]
2427               ["Fill long lines" gnus-article-fill-long-lines t]
2428               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2429               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2430               ["Remove CR" gnus-article-remove-cr t]
2431               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2432               ["Base64" gnus-article-de-base64-unreadable t]
2433               ["Rot 13" gnus-summary-caesar-message
2434                ,@(if (featurep 'xemacs) '(t)
2435                    '(:help "\"Caesar rotate\" article by 13"))]
2436               ["De-IDNA" gnus-summary-idna-message t]
2437               ["Morse decode" gnus-summary-morse-message t]
2438               ["Unix pipe..." gnus-summary-pipe-message t]
2439               ["Add buttons" gnus-article-add-buttons t]
2440               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2441               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2442               ["Verbose header" gnus-summary-verbose-headers t]
2443               ["Toggle header" gnus-summary-toggle-header t]
2444               ["Unfold headers" gnus-article-treat-unfold-headers t]
2445               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2446               ["Html" gnus-article-wash-html t]
2447               ["Unsplit URLs" gnus-article-unsplit-urls t]
2448               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2449               ["Decode HZ" gnus-article-decode-HZ t]
2450               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2451               ("(Outlook) Deuglify"
2452                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2453                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2454                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2455                ["Full (Outlook) deuglify"
2456                 gnus-article-outlook-deuglify-article t])
2457               )
2458              ("Output"
2459               ["Save in default format..." gnus-summary-save-article
2460                ,@(if (featurep 'xemacs) '(t)
2461                    '(:help "Save article using default method"))]
2462               ["Save in file..." gnus-summary-save-article-file
2463                ,@(if (featurep 'xemacs) '(t)
2464                    '(:help "Save article in file"))]
2465               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2466               ["Save in MH folder..." gnus-summary-save-article-folder t]
2467               ["Save in VM folder..." gnus-summary-save-article-vm t]
2468               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2469               ["Save body in file..." gnus-summary-save-article-body-file t]
2470               ["Pipe through a filter..." gnus-summary-pipe-output t]
2471               ["Print with Muttprint..." gnus-summary-muttprint t]
2472               ["Print" gnus-summary-print-article
2473                ,@(if (featurep 'xemacs) '(t)
2474                    '(:help "Generate and print a PostScript image"))])
2475              ("Copy, move,... (Backend)"
2476               ,@(if (featurep 'xemacs) nil
2477                   '(:help "Copying, moving, expiring articles..."))
2478               ["Respool article..." gnus-summary-respool-article t]
2479               ["Move article..." gnus-summary-move-article
2480                (gnus-check-backend-function
2481                 'request-move-article gnus-newsgroup-name)]
2482               ["Copy article..." gnus-summary-copy-article t]
2483               ["Crosspost article..." gnus-summary-crosspost-article
2484                (gnus-check-backend-function
2485                 'request-replace-article gnus-newsgroup-name)]
2486               ["Import file..." gnus-summary-import-article
2487                (gnus-check-backend-function
2488                 'request-accept-article gnus-newsgroup-name)]
2489               ["Create article..." gnus-summary-create-article
2490                (gnus-check-backend-function
2491                 'request-accept-article gnus-newsgroup-name)]
2492               ["Check if posted" gnus-summary-article-posted-p t]
2493               ["Edit article" gnus-summary-edit-article
2494                (not (gnus-group-read-only-p))]
2495               ["Delete article" gnus-summary-delete-article
2496                (gnus-check-backend-function
2497                 'request-expire-articles gnus-newsgroup-name)]
2498               ["Query respool" gnus-summary-respool-query t]
2499               ["Trace respool" gnus-summary-respool-trace t]
2500               ["Delete expirable articles" gnus-summary-expire-articles-now
2501                (gnus-check-backend-function
2502                 'request-expire-articles gnus-newsgroup-name)])
2503              ("Extract"
2504               ["Uudecode" gnus-uu-decode-uu
2505                ,@(if (featurep 'xemacs) '(t)
2506                    '(:help "Decode uuencoded article(s)"))]
2507               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2508               ["Unshar" gnus-uu-decode-unshar t]
2509               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2510               ["Save" gnus-uu-decode-save t]
2511               ["Binhex" gnus-uu-decode-binhex t]
2512               ["Postscript" gnus-uu-decode-postscript t]
2513               ["All MIME parts" gnus-summary-save-parts t])
2514              ("Cache"
2515               ["Enter article" gnus-cache-enter-article t]
2516               ["Remove article" gnus-cache-remove-article t])
2517              ["Translate" gnus-article-babel t]
2518              ["Select article buffer" gnus-summary-select-article-buffer t]
2519              ["Make article buffer sticky" gnus-sticky-article t]
2520              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2521              ["Isearch article..." gnus-summary-isearch-article t]
2522              ["Beginning of the article" gnus-summary-beginning-of-article t]
2523              ["End of the article" gnus-summary-end-of-article t]
2524              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2525              ["Fetch referenced articles" gnus-summary-refer-references t]
2526              ["Fetch current thread" gnus-summary-refer-thread t]
2527              ["Fetch article with id..." gnus-summary-refer-article t]
2528              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2529              ["Redisplay" gnus-summary-show-article t]
2530              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2531       (easy-menu-define
2532         gnus-summary-article-menu gnus-summary-mode-map ""
2533         (cons "Article" innards))
2534
2535       (if (not (keymapp gnus-summary-article-menu))
2536           (easy-menu-define
2537             gnus-article-commands-menu gnus-article-mode-map ""
2538             (cons "Commands" innards))
2539         ;; in Emacs, don't share menu.
2540         (setq gnus-article-commands-menu
2541               (copy-keymap gnus-summary-article-menu))
2542         (define-key gnus-article-mode-map [menu-bar commands]
2543           (cons "Commands" gnus-article-commands-menu))))
2544
2545     (easy-menu-define
2546       gnus-summary-thread-menu gnus-summary-mode-map ""
2547       '("Threads"
2548         ["Find all messages in thread" gnus-summary-refer-thread t]
2549         ["Toggle threading" gnus-summary-toggle-threads t]
2550         ["Hide threads" gnus-summary-hide-all-threads t]
2551         ["Show threads" gnus-summary-show-all-threads t]
2552         ["Hide thread" gnus-summary-hide-thread t]
2553         ["Show thread" gnus-summary-show-thread t]
2554         ["Go to next thread" gnus-summary-next-thread t]
2555         ["Go to previous thread" gnus-summary-prev-thread t]
2556         ["Go down thread" gnus-summary-down-thread t]
2557         ["Go up thread" gnus-summary-up-thread t]
2558         ["Top of thread" gnus-summary-top-thread t]
2559         ["Mark thread as read" gnus-summary-kill-thread t]
2560         ["Mark thread as expired" gnus-summary-expire-thread t]
2561         ["Lower thread score" gnus-summary-lower-thread t]
2562         ["Raise thread score" gnus-summary-raise-thread t]
2563         ["Rethread current" gnus-summary-rethread-current t]))
2564
2565     (easy-menu-define
2566       gnus-summary-post-menu gnus-summary-mode-map ""
2567       `("Post"
2568         ["Send a message (mail or news)" gnus-summary-post-news
2569          ,@(if (featurep 'xemacs) '(t)
2570              '(:help "Compose a new message (mail or news)"))]
2571         ["Followup" gnus-summary-followup
2572          ,@(if (featurep 'xemacs) '(t)
2573              '(:help "Post followup to this article"))]
2574         ["Followup and yank" gnus-summary-followup-with-original
2575          ,@(if (featurep 'xemacs) '(t)
2576              '(:help "Post followup to this article, quoting its contents"))]
2577         ["Supersede article" gnus-summary-supersede-article t]
2578         ["Cancel article" gnus-summary-cancel-article
2579          ,@(if (featurep 'xemacs) '(t)
2580              '(:help "Cancel an article you posted"))]
2581         ["Reply" gnus-summary-reply t]
2582         ["Reply and yank" gnus-summary-reply-with-original t]
2583         ["Wide reply" gnus-summary-wide-reply t]
2584         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2585          ,@(if (featurep 'xemacs) '(t)
2586              '(:help "Mail a reply, quoting this article"))]
2587         ["Very wide reply" gnus-summary-very-wide-reply t]
2588         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2589          ,@(if (featurep 'xemacs) '(t)
2590              '(:help "Mail a very wide reply, quoting this article"))]
2591         ["Mail forward" gnus-summary-mail-forward t]
2592         ["Post forward" gnus-summary-post-forward t]
2593         ["Digest and mail" gnus-uu-digest-mail-forward t]
2594         ["Digest and post" gnus-uu-digest-post-forward t]
2595         ["Resend message" gnus-summary-resend-message t]
2596         ["Resend message edit" gnus-summary-resend-message-edit t]
2597         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2598         ["Send a mail" gnus-summary-mail-other-window t]
2599         ["Create a local message" gnus-summary-news-other-window t]
2600         ["Uuencode and post" gnus-uu-post-news
2601          ,@(if (featurep 'xemacs) '(t)
2602              '(:help "Post a uuencoded article"))]
2603         ["Followup via news" gnus-summary-followup-to-mail t]
2604         ["Followup via news and yank"
2605          gnus-summary-followup-to-mail-with-original t]
2606         ["Strip signature on reply"
2607          (lambda ()
2608            (interactive)
2609            (if (not (memq message-cite-function
2610                           '(message-cite-original-without-signature
2611                             message-cite-original)))
2612                ;; Stupid workaround for XEmacs not honoring :visible.
2613                (message "Can't toggle this value of `message-cite-function'")
2614              (setq message-cite-function
2615                    (if (eq message-cite-function
2616                            'message-cite-original-without-signature)
2617                        'message-cite-original
2618                      'message-cite-original-without-signature))))
2619          ;; XEmacs barfs on :visible.
2620          ,@(if (featurep 'xemacs) nil
2621              '(:visible (memq message-cite-function
2622                               '(message-cite-original-without-signature
2623                                 message-cite-original))))
2624          :style toggle
2625          :selected (eq message-cite-function
2626                        'message-cite-original-without-signature)
2627          ,@(if (featurep 'xemacs) nil
2628              '(:help "Strip signature from cited article when replying."))]
2629         ;;("Draft"
2630         ;;["Send" gnus-summary-send-draft t]
2631         ;;["Send bounced" gnus-resend-bounced-mail t])
2632         ))
2633
2634     (cond
2635      ((not (keymapp gnus-summary-post-menu))
2636       (setq gnus-article-post-menu gnus-summary-post-menu))
2637      ((not gnus-article-post-menu)
2638       ;; Don't share post menu.
2639       (setq gnus-article-post-menu
2640             (copy-keymap gnus-summary-post-menu))))
2641     (define-key gnus-article-mode-map [menu-bar post]
2642       (cons "Post" gnus-article-post-menu))
2643
2644     (easy-menu-define
2645       gnus-summary-misc-menu gnus-summary-mode-map ""
2646       `("Gnus"
2647         ("Mark Read"
2648          ["Mark as read" gnus-summary-mark-as-read-forward t]
2649          ["Mark same subject and select"
2650           gnus-summary-kill-same-subject-and-select t]
2651          ["Mark same subject" gnus-summary-kill-same-subject t]
2652          ["Catchup" gnus-summary-catchup
2653           ,@(if (featurep 'xemacs) '(t)
2654               '(:help "Mark unread articles in this group as read"))]
2655          ["Catchup all" gnus-summary-catchup-all t]
2656          ["Catchup to here" gnus-summary-catchup-to-here t]
2657          ["Catchup from here" gnus-summary-catchup-from-here t]
2658          ["Catchup region" gnus-summary-mark-region-as-read
2659           (gnus-mark-active-p)]
2660          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2661         ("Mark Various"
2662          ["Tick" gnus-summary-tick-article-forward t]
2663          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2664          ["Remove marks" gnus-summary-clear-mark-forward t]
2665          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2666          ["Set bookmark" gnus-summary-set-bookmark t]
2667          ["Remove bookmark" gnus-summary-remove-bookmark t])
2668         ("Limit to"
2669          ["Marks..." gnus-summary-limit-to-marks t]
2670          ["Subject..." gnus-summary-limit-to-subject t]
2671          ["Author..." gnus-summary-limit-to-author t]
2672          ["Recipient..." gnus-summary-limit-to-recipient t]
2673          ["Address..." gnus-summary-limit-to-address t]
2674          ["Age..." gnus-summary-limit-to-age t]
2675          ["Extra..." gnus-summary-limit-to-extra t]
2676          ["Score..." gnus-summary-limit-to-score t]
2677          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2678          ["Unread" gnus-summary-limit-to-unread t]
2679          ["Unseen" gnus-summary-limit-to-unseen t]
2680          ["Singletons" gnus-summary-limit-to-singletons t]
2681          ["Replied" gnus-summary-limit-to-replied t]
2682          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2683          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2684          ["Pop limit" gnus-summary-pop-limit t]
2685          ["Show dormant" gnus-summary-limit-include-dormant t]
2686          ["Hide childless dormant"
2687           gnus-summary-limit-exclude-childless-dormant t]
2688          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2689          ["Hide marked" gnus-summary-limit-exclude-marks t]
2690          ["Show expunged" gnus-summary-limit-include-expunged t])
2691         ("Process Mark"
2692          ["Set mark" gnus-summary-mark-as-processable t]
2693          ["Remove mark" gnus-summary-unmark-as-processable t]
2694          ["Remove all marks" gnus-summary-unmark-all-processable t]
2695          ["Invert marks" gnus-uu-invert-processable t]
2696          ["Mark above" gnus-uu-mark-over t]
2697          ["Mark series" gnus-uu-mark-series t]
2698          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2699          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2700          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2701          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2702          ["Mark all" gnus-uu-mark-all t]
2703          ["Mark buffer" gnus-uu-mark-buffer t]
2704          ["Mark sparse" gnus-uu-mark-sparse t]
2705          ["Mark thread" gnus-uu-mark-thread t]
2706          ["Unmark thread" gnus-uu-unmark-thread t]
2707          ("Process Mark Sets"
2708           ["Kill" gnus-summary-kill-process-mark t]
2709           ["Yank" gnus-summary-yank-process-mark
2710            gnus-newsgroup-process-stack]
2711           ["Save" gnus-summary-save-process-mark t]
2712           ["Run command on marked..." gnus-summary-universal-argument t]))
2713         ("Registry Marks")
2714         ("Scroll article"
2715          ["Page forward" gnus-summary-next-page
2716           ,@(if (featurep 'xemacs) '(t)
2717               '(:help "Show next page of article"))]
2718          ["Page backward" gnus-summary-prev-page
2719           ,@(if (featurep 'xemacs) '(t)
2720               '(:help "Show previous page of article"))]
2721          ["Line forward" gnus-summary-scroll-up t])
2722         ("Move"
2723          ["Next unread article" gnus-summary-next-unread-article t]
2724          ["Previous unread article" gnus-summary-prev-unread-article t]
2725          ["Next article" gnus-summary-next-article t]
2726          ["Previous article" gnus-summary-prev-article t]
2727          ["Next unread subject" gnus-summary-next-unread-subject t]
2728          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2729          ["Next article same subject" gnus-summary-next-same-subject t]
2730          ["Previous article same subject" gnus-summary-prev-same-subject t]
2731          ["First unread article" gnus-summary-first-unread-article t]
2732          ["Best unread article" gnus-summary-best-unread-article t]
2733          ["Go to subject number..." gnus-summary-goto-subject t]
2734          ["Go to article number..." gnus-summary-goto-article t]
2735          ["Go to the last article" gnus-summary-goto-last-article t]
2736          ["Pop article off history" gnus-summary-pop-article t])
2737         ("Sort"
2738          ["Sort by number" gnus-summary-sort-by-number t]
2739          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2740          ["Sort by author" gnus-summary-sort-by-author t]
2741          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2742          ["Sort by subject" gnus-summary-sort-by-subject t]
2743          ["Sort by date" gnus-summary-sort-by-date t]
2744          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2745          ["Sort by score" gnus-summary-sort-by-score t]
2746          ["Sort by lines" gnus-summary-sort-by-lines t]
2747          ["Sort by characters" gnus-summary-sort-by-chars t]
2748          ["Randomize" gnus-summary-sort-by-random t]
2749          ["Original sort" gnus-summary-sort-by-original t])
2750         ("Help"
2751          ["Describe group" gnus-summary-describe-group t]
2752          ["Read manual" gnus-info-find-node t])
2753         ("Modes"
2754          ["Pick and read" gnus-pick-mode t]
2755          ["Binary" gnus-binary-mode t])
2756         ("Regeneration"
2757          ["Regenerate" gnus-summary-prepare t]
2758          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2759          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2760          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2761          ["Toggle threading" gnus-summary-toggle-threads t])
2762         ["See old articles" gnus-summary-insert-old-articles t]
2763         ["See new articles" gnus-summary-insert-new-articles t]
2764         ["Filter articles..." gnus-summary-execute-command t]
2765         ["Run command on articles..." gnus-summary-universal-argument t]
2766         ["Search articles forward..." gnus-summary-search-article-forward t]
2767         ["Search articles backward..." gnus-summary-search-article-backward t]
2768         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2769         ["Expand window" gnus-summary-expand-window t]
2770         ["Expire expirable articles" gnus-summary-expire-articles
2771          (gnus-check-backend-function
2772           'request-expire-articles gnus-newsgroup-name)]
2773         ["Edit local kill file" gnus-summary-edit-local-kill t]
2774         ["Edit main kill file" gnus-summary-edit-global-kill t]
2775         ["Edit group parameters" gnus-summary-edit-parameters t]
2776         ["Customize group parameters" gnus-summary-customize-parameters t]
2777         ["Send a bug report" gnus-bug t]
2778         ("Exit"
2779          ["Catchup and exit" gnus-summary-catchup-and-exit
2780           ,@(if (featurep 'xemacs) '(t)
2781               '(:help "Mark unread articles in this group as read, then exit"))]
2782          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2783          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2784          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2785          ["Exit group" gnus-summary-exit
2786           ,@(if (featurep 'xemacs) '(t)
2787               '(:help "Exit current group, return to group selection mode"))]
2788          ["Exit group without updating" gnus-summary-exit-no-update t]
2789          ["Exit and goto next group" gnus-summary-next-group t]
2790          ["Exit and goto prev group" gnus-summary-prev-group t]
2791          ["Reselect group" gnus-summary-reselect-current-group t]
2792          ["Rescan group" gnus-summary-rescan-group t]
2793          ["Update dribble" gnus-summary-save-newsrc t])))
2794
2795     (gnus-run-hooks 'gnus-summary-menu-hook)))
2796
2797 (defvar gnus-summary-tool-bar-map nil)
2798
2799 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2800 ;; affect _new_ message buffers.  We might add a function that walks thru all
2801 ;; summary-mode buffers and force the update.
2802 (defun gnus-summary-tool-bar-update (&optional symbol value)
2803   "Update summary mode toolbar.
2804 Setter function for custom variables."
2805   (setq-default gnus-summary-tool-bar-map nil)
2806   (when symbol
2807     ;; When used as ":set" function:
2808     (set-default symbol value))
2809   (when (gnus-buffer-live-p gnus-summary-buffer)
2810     (with-current-buffer gnus-summary-buffer
2811       (gnus-summary-make-tool-bar))))
2812
2813 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2814                                      'gnus-summary-tool-bar-gnome
2815                                    'gnus-summary-tool-bar-retro)
2816   "Specifies the Gnus summary tool bar.
2817
2818 It can be either a list or a symbol refering to a list.  See
2819 `gmm-tool-bar-from-list' for the format of the list.  The
2820 default key map is `gnus-summary-mode-map'.
2821
2822 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2823 `gnus-summary-tool-bar-retro'."
2824   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2825                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2826                  (repeat :tag "User defined list" gmm-tool-bar-item)
2827                  (symbol))
2828   :version "23.1" ;; No Gnus
2829   :initialize 'custom-initialize-default
2830   :set 'gnus-summary-tool-bar-update
2831   :group 'gnus-summary)
2832
2833 (defcustom gnus-summary-tool-bar-gnome
2834   '((gnus-summary-post-news "mail/compose" nil)
2835     (gnus-summary-insert-new-articles "mail/inbox" nil
2836                                       :visible (or (not gnus-agent)
2837                                                    gnus-plugged))
2838     (gnus-summary-reply-with-original "mail/reply")
2839     (gnus-summary-reply "mail/reply" nil :visible nil)
2840     (gnus-summary-followup-with-original "mail/reply-all")
2841     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2842     (gnus-summary-mail-forward "mail/forward")
2843     (gnus-summary-save-article "mail/save")
2844     (gnus-summary-search-article-forward "search" nil :visible nil)
2845     (gnus-summary-print-article "print")
2846     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2847     ;; Some new commands that may need more suitable icons:
2848     (gnus-summary-save-newsrc "save" nil :visible nil)
2849     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2850     (gnus-summary-prev-article "left-arrow")
2851     (gnus-summary-next-article "right-arrow")
2852     (gnus-summary-next-page "next-page")
2853     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2854     ;;
2855     ;; Maybe some sort-by-... could be added:
2856     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2857     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2858     (gnus-summary-mark-as-expirable
2859      "delete" nil
2860      :visible (gnus-check-backend-function 'request-expire-articles
2861                                            gnus-newsgroup-name))
2862     (gnus-summary-mark-as-spam
2863      "mail/spam" t
2864      :visible (and (fboundp 'spam-group-ham-contents-p)
2865                    (spam-group-ham-contents-p gnus-newsgroup-name))
2866      :help "Mark as spam")
2867     (gnus-summary-mark-as-read-forward
2868      "mail/not-spam" nil
2869      :visible (and (fboundp 'spam-group-spam-contents-p)
2870                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2871     ;;
2872     (gnus-summary-exit "exit")
2873     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2874     (gnus-info-find-node "help"))
2875   "List of functions for the summary tool bar (GNOME style).
2876
2877 See `gmm-tool-bar-from-list' for the format of the list."
2878   :type '(repeat gmm-tool-bar-item)
2879   :version "23.1" ;; No Gnus
2880   :initialize 'custom-initialize-default
2881   :set 'gnus-summary-tool-bar-update
2882   :group 'gnus-summary)
2883
2884 (defcustom gnus-summary-tool-bar-retro
2885   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2886     (gnus-summary-next-unread-article "gnus/next-ur")
2887     (gnus-summary-post-news "gnus/post")
2888     (gnus-summary-followup-with-original "gnus/fuwo")
2889     (gnus-summary-followup "gnus/followup")
2890     (gnus-summary-reply-with-original "gnus/reply-wo")
2891     (gnus-summary-reply "gnus/reply")
2892     (gnus-summary-caesar-message "gnus/rot13")
2893     (gnus-uu-decode-uu "gnus/uu-decode")
2894     (gnus-summary-save-article-file "gnus/save-aif")
2895     (gnus-summary-save-article "gnus/save-art")
2896     (gnus-uu-post-news "gnus/uu-post")
2897     (gnus-summary-catchup "gnus/catchup")
2898     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2899     (gnus-summary-exit "gnus/exit-summ")
2900     ;; Some new command that may need more suitable icons:
2901     (gnus-summary-print-article "gnus/print" nil :visible nil)
2902     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2903     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2904     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2905     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2906     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2907     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2908     ;;
2909     (gnus-info-find-node "gnus/help" nil :visible nil))
2910   "List of functions for the summary tool bar (retro look).
2911
2912 See `gmm-tool-bar-from-list' for the format of the list."
2913   :type '(repeat gmm-tool-bar-item)
2914   :version "23.1" ;; No Gnus
2915   :initialize 'custom-initialize-default
2916   :set 'gnus-summary-tool-bar-update
2917   :group 'gnus-summary)
2918
2919 (defcustom gnus-summary-tool-bar-zap-list t
2920   "List of icon items from the global tool bar.
2921 These items are not displayed in the Gnus summary mode tool bar.
2922
2923 See `gmm-tool-bar-from-list' for the format of the list."
2924   :type 'gmm-tool-bar-zap-list
2925   :version "23.1" ;; No Gnus
2926   :initialize 'custom-initialize-default
2927   :set 'gnus-summary-tool-bar-update
2928   :group 'gnus-summary)
2929
2930 (defvar image-load-path)
2931 (defvar tool-bar-map)
2932
2933 (defun gnus-summary-make-tool-bar (&optional force)
2934   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2935 When FORCE, rebuild the tool bar."
2936   (when (and (not (featurep 'xemacs))
2937              (boundp 'tool-bar-mode)
2938              tool-bar-mode
2939              (or (not gnus-summary-tool-bar-map) force))
2940     (let* ((load-path
2941             (gmm-image-load-path-for-library "gnus"
2942                                              "mail/save.xpm"
2943                                              nil t))
2944            (image-load-path (cons (car load-path)
2945                                   (when (boundp 'image-load-path)
2946                                     image-load-path)))
2947            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2948                                         gnus-summary-tool-bar-zap-list
2949                                         'gnus-summary-mode-map)))
2950       (when map
2951         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2952         ;; uses it's value.
2953         (setq gnus-summary-tool-bar-map map))))
2954   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2955
2956 (defun gnus-score-set-default (var value)
2957   "A version of set that updates the GNU Emacs menu-bar."
2958   (set var value)
2959   ;; It is the message that forces the active status to be updated.
2960   (message ""))
2961
2962 (defun gnus-make-score-map (type)
2963   "Make a summary score map of type TYPE."
2964   (if t
2965       nil
2966     (let ((headers '(("author" "from" string)
2967                      ("subject" "subject" string)
2968                      ("article body" "body" string)
2969                      ("article head" "head" string)
2970                      ("xref" "xref" string)
2971                      ("extra header" "extra" string)
2972                      ("lines" "lines" number)
2973                      ("followups to author" "followup" string)))
2974           (types '((number ("less than" <)
2975                            ("greater than" >)
2976                            ("equal" =))
2977                    (string ("substring" s)
2978                            ("exact string" e)
2979                            ("fuzzy string" f)
2980                            ("regexp" r))))
2981           (perms '(("temporary" (current-time-string))
2982                    ("permanent" nil)
2983                    ("immediate" now)))
2984           header)
2985       (list
2986        (apply
2987         'nconc
2988         (list
2989          (if (eq type 'lower)
2990              "Lower score"
2991            "Increase score"))
2992         (let (outh)
2993           (while headers
2994             (setq header (car headers))
2995             (setq outh
2996                   (cons
2997                    (apply
2998                     'nconc
2999                     (list (car header))
3000                     (let ((ts (cdr (assoc (nth 2 header) types)))
3001                           outt)
3002                       (while ts
3003                         (setq outt
3004                               (cons
3005                                (apply
3006                                 'nconc
3007                                 (list (caar ts))
3008                                 (let ((ps perms)
3009                                       outp)
3010                                   (while ps
3011                                     (setq outp
3012                                           (cons
3013                                            (vector
3014                                             (caar ps)
3015                                             (list
3016                                              'gnus-summary-score-entry
3017                                              (nth 1 header)
3018                                              (if (or (string= (nth 1 header)
3019                                                               "head")
3020                                                      (string= (nth 1 header)
3021                                                               "body"))
3022                                                  ""
3023                                                (list 'gnus-summary-header
3024                                                      (nth 1 header)))
3025                                              (list 'quote (nth 1 (car ts)))
3026                                              (list 'gnus-score-delta-default
3027                                                    nil)
3028                                              (nth 1 (car ps))
3029                                              t)
3030                                             t)
3031                                            outp))
3032                                     (setq ps (cdr ps)))
3033                                   (list (nreverse outp))))
3034                                outt))
3035                         (setq ts (cdr ts)))
3036                       (list (nreverse outt))))
3037                    outh))
3038             (setq headers (cdr headers)))
3039           (list (nreverse outh))))))))
3040
3041
3042 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3043 (defvar bookmark-make-record-function)
3044 \f
3045
3046 (defun gnus-summary-mode (&optional group)
3047   "Major mode for reading articles.
3048
3049 All normal editing commands are switched off.
3050 \\<gnus-summary-mode-map>
3051 Each line in this buffer represents one article.  To read an
3052 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3053 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3054 respectively.
3055
3056 You can also post articles and send mail from this buffer.  To
3057 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3058 of an article, type `\\[gnus-summary-reply]'.
3059
3060 There are approx. one gazillion commands you can execute in this
3061 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3062
3063 The following commands are available:
3064
3065 \\{gnus-summary-mode-map}"
3066   (interactive)
3067   (kill-all-local-variables)
3068   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3069     (gnus-summary-make-local-variables))
3070   (gnus-summary-make-local-variables)
3071   (setq gnus-newsgroup-name group)
3072   (when (gnus-visual-p 'summary-menu 'menu)
3073     (gnus-summary-make-menu-bar)
3074     (gnus-summary-make-tool-bar))
3075   (gnus-make-thread-indent-array)
3076   (gnus-simplify-mode-line)
3077   (setq major-mode 'gnus-summary-mode)
3078   (setq mode-name "Summary")
3079   (use-local-map gnus-summary-mode-map)
3080   (buffer-disable-undo)
3081   (setq buffer-read-only t              ;Disable modification
3082         show-trailing-whitespace nil)
3083   (setq truncate-lines t)
3084   (add-to-invisibility-spec '(gnus-sum . t))
3085   (gnus-summary-set-display-table)
3086   (gnus-set-default-directory)
3087   (make-local-variable 'gnus-summary-line-format)
3088   (make-local-variable 'gnus-summary-line-format-spec)
3089   (make-local-variable 'gnus-summary-dummy-line-format)
3090   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3091   (make-local-variable 'gnus-summary-mark-positions)
3092   (gnus-make-local-hook 'pre-command-hook)
3093   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3094   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3095   (turn-on-gnus-mailing-list-mode)
3096   (mm-enable-multibyte)
3097   (set (make-local-variable 'bookmark-make-record-function)
3098        'gnus-summary-bookmark-make-record)
3099   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3100   (gnus-update-summary-mark-positions))
3101
3102 (defun gnus-summary-make-local-variables ()
3103   "Make all the local summary buffer variables."
3104   (let (global)
3105     (dolist (local gnus-summary-local-variables)
3106       (if (consp local)
3107           (progn
3108             (if (eq (cdr local) 'global)
3109                 ;; Copy the global value of the variable.
3110                 (setq global (symbol-value (car local)))
3111               ;; Use the value from the list.
3112               (setq global (eval (cdr local))))
3113             (set (make-local-variable (car local)) global))
3114         ;; Simple nil-valued local variable.
3115         (set (make-local-variable local) nil)))))
3116
3117 ;; Summary data functions.
3118
3119 (defmacro gnus-data-number (data)
3120   `(car ,data))
3121
3122 (defmacro gnus-data-set-number (data number)
3123   `(setcar ,data ,number))
3124
3125 (defmacro gnus-data-mark (data)
3126   `(nth 1 ,data))
3127
3128 (defmacro gnus-data-set-mark (data mark)
3129   `(setcar (nthcdr 1 ,data) ,mark))
3130
3131 (defmacro gnus-data-pos (data)
3132   `(nth 2 ,data))
3133
3134 (defmacro gnus-data-set-pos (data pos)
3135   `(setcar (nthcdr 2 ,data) ,pos))
3136
3137 (defmacro gnus-data-header (data)
3138   `(nth 3 ,data))
3139
3140 (defmacro gnus-data-set-header (data header)
3141   `(setf (nth 3 ,data) ,header))
3142
3143 (defmacro gnus-data-level (data)
3144   `(nth 4 ,data))
3145
3146 (defmacro gnus-data-unread-p (data)
3147   `(= (nth 1 ,data) gnus-unread-mark))
3148
3149 (defmacro gnus-data-read-p (data)
3150   `(/= (nth 1 ,data) gnus-unread-mark))
3151
3152 (defmacro gnus-data-pseudo-p (data)
3153   `(consp (nth 3 ,data)))
3154
3155 (defmacro gnus-data-find (number)
3156   `(assq ,number gnus-newsgroup-data))
3157
3158 (defmacro gnus-data-find-list (number &optional data)
3159   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3160      (memq (assq ,number bdata)
3161            bdata)))
3162
3163 (defmacro gnus-data-make (number mark pos header level)
3164   `(list ,number ,mark ,pos ,header ,level))
3165
3166 (defun gnus-data-enter (after-article number mark pos header level offset)
3167   (let ((data (gnus-data-find-list after-article)))
3168     (unless data
3169       (error "No such article: %d" after-article))
3170     (setcdr data (cons (gnus-data-make number mark pos header level)
3171                        (cdr data)))
3172     (setq gnus-newsgroup-data-reverse nil)
3173     (gnus-data-update-list (cddr data) offset)))
3174
3175 (defun gnus-data-enter-list (after-article list &optional offset)
3176   (when list
3177     (let ((data (and after-article (gnus-data-find-list after-article)))
3178           (ilist list))
3179       (if (not (or data
3180                    after-article))
3181           (let ((odata gnus-newsgroup-data))
3182             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3183             (when offset
3184               (gnus-data-update-list odata offset)))
3185         ;; Find the last element in the list to be spliced into the main
3186         ;; list.
3187         (setq list (last list))
3188         (if (not data)
3189             (progn
3190               (setcdr list gnus-newsgroup-data)
3191               (setq gnus-newsgroup-data ilist)
3192               (when offset
3193                 (gnus-data-update-list (cdr list) offset)))
3194           (setcdr list (cdr data))
3195           (setcdr data ilist)
3196           (when offset
3197             (gnus-data-update-list (cdr list) offset))))
3198       (setq gnus-newsgroup-data-reverse nil))))
3199
3200 (defun gnus-data-remove (article &optional offset)
3201   (let ((data gnus-newsgroup-data))
3202     (if (= (gnus-data-number (car data)) article)
3203         (progn
3204           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3205                 gnus-newsgroup-data-reverse nil)
3206           (when offset
3207             (gnus-data-update-list gnus-newsgroup-data offset)))
3208       (while (cdr data)
3209         (when (= (gnus-data-number (cadr data)) article)
3210           (setcdr data (cddr data))
3211           (when offset
3212             (gnus-data-update-list (cdr data) offset))
3213           (setq data nil
3214                 gnus-newsgroup-data-reverse nil))
3215         (setq data (cdr data))))))
3216
3217 (defmacro gnus-data-list (backward)
3218   `(if ,backward
3219        (or gnus-newsgroup-data-reverse
3220            (setq gnus-newsgroup-data-reverse
3221                  (reverse gnus-newsgroup-data)))
3222      gnus-newsgroup-data))
3223
3224 (defun gnus-data-update-list (data offset)
3225   "Add OFFSET to the POS of all data entries in DATA."
3226   (setq gnus-newsgroup-data-reverse nil)
3227   (while data
3228     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3229     (setq data (cdr data))))
3230
3231 (defun gnus-summary-article-pseudo-p (article)
3232   "Say whether this article is a pseudo article or not."
3233   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3234
3235 (defmacro gnus-summary-article-sparse-p (article)
3236   "Say whether this article is a sparse article or not."
3237   `(memq ,article gnus-newsgroup-sparse))
3238
3239 (defmacro gnus-summary-article-ancient-p (article)
3240   "Say whether this article is a sparse article or not."
3241   `(memq ,article gnus-newsgroup-ancient))
3242
3243 (defun gnus-article-parent-p (number)
3244   "Say whether this article is a parent or not."
3245   (let ((data (gnus-data-find-list number)))
3246     (and (cdr data)              ; There has to be an article after...
3247          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3248             (gnus-data-level (nth 1 data))))))
3249
3250 (defun gnus-article-children (number)
3251   "Return a list of all children to NUMBER."
3252   (let* ((data (gnus-data-find-list number))
3253          (level (gnus-data-level (car data)))
3254          children)
3255     (setq data (cdr data))
3256     (while (and data
3257                 (= (gnus-data-level (car data)) (1+ level)))
3258       (push (gnus-data-number (car data)) children)
3259       (setq data (cdr data)))
3260     children))
3261
3262 (defmacro gnus-summary-skip-intangible ()
3263   "If the current article is intangible, then jump to a different article."
3264   '(let ((to (get-text-property (point) 'gnus-intangible)))
3265      (and to (gnus-summary-goto-subject to))))
3266
3267 (defmacro gnus-summary-article-intangible-p ()
3268   "Say whether this article is intangible or not."
3269   '(get-text-property (point) 'gnus-intangible))
3270
3271 (defun gnus-article-read-p (article)
3272   "Say whether ARTICLE is read or not."
3273   (not (or (memq article gnus-newsgroup-marked)
3274            (memq article gnus-newsgroup-spam-marked)
3275            (memq article gnus-newsgroup-unreads)
3276            (memq article gnus-newsgroup-unselected)
3277            (memq article gnus-newsgroup-dormant))))
3278
3279 ;; Some summary mode macros.
3280
3281 (defmacro gnus-summary-article-number ()
3282   "The article number of the article on the current line.
3283 If there isn't an article number here, then we return the current
3284 article number."
3285   '(progn
3286      (gnus-summary-skip-intangible)
3287      (or (get-text-property (point) 'gnus-number)
3288          (gnus-summary-last-subject))))
3289
3290 (defmacro gnus-summary-article-header (&optional number)
3291   "Return the header of article NUMBER."
3292   `(gnus-data-header (gnus-data-find
3293                       ,(or number '(gnus-summary-article-number)))))
3294
3295 (defmacro gnus-summary-thread-level (&optional number)
3296   "Return the level of thread that starts with article NUMBER."
3297   `(if (and (eq gnus-summary-make-false-root 'dummy)
3298             (get-text-property (point) 'gnus-intangible))
3299        0
3300      (gnus-data-level (gnus-data-find
3301                        ,(or number '(gnus-summary-article-number))))))
3302
3303 (defmacro gnus-summary-article-mark (&optional number)
3304   "Return the mark of article NUMBER."
3305   `(gnus-data-mark (gnus-data-find
3306                     ,(or number '(gnus-summary-article-number)))))
3307
3308 (defmacro gnus-summary-article-pos (&optional number)
3309   "Return the position of the line of article NUMBER."
3310   `(gnus-data-pos (gnus-data-find
3311                    ,(or number '(gnus-summary-article-number)))))
3312
3313 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3314 (defmacro gnus-summary-article-subject (&optional number)
3315   "Return current subject string or nil if nothing."
3316   `(let ((headers
3317           ,(if number
3318                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3319              '(gnus-data-header (assq (gnus-summary-article-number)
3320                                       gnus-newsgroup-data)))))
3321      (and headers
3322           (vectorp headers)
3323           (mail-header-subject headers))))
3324
3325 (defmacro gnus-summary-article-score (&optional number)
3326   "Return current article score."
3327   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3328                   gnus-newsgroup-scored))
3329        gnus-summary-default-score 0))
3330
3331 (defun gnus-summary-article-children (&optional number)
3332   "Return a list of article numbers that are children of article NUMBER."
3333   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3334          (level (gnus-data-level (car data)))
3335          l children)
3336     (while (and (setq data (cdr data))
3337                 (> (setq l (gnus-data-level (car data))) level))
3338       (and (= (1+ level) l)
3339            (push (gnus-data-number (car data))
3340                  children)))
3341     (nreverse children)))
3342
3343 (defun gnus-summary-article-parent (&optional number)
3344   "Return the article number of the parent of article NUMBER."
3345   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3346                                     (gnus-data-list t)))
3347          (level (gnus-data-level (car data))))
3348     (if (zerop level)
3349         ()                              ; This is a root.
3350       ;; We search until we find an article with a level less than
3351       ;; this one.  That function has to be the parent.
3352       (while (and (setq data (cdr data))
3353                   (not (< (gnus-data-level (car data)) level))))
3354       (and data (gnus-data-number (car data))))))
3355
3356 (defun gnus-unread-mark-p (mark)
3357   "Say whether MARK is the unread mark."
3358   (= mark gnus-unread-mark))
3359
3360 (defun gnus-read-mark-p (mark)
3361   "Say whether MARK is one of the marks that mark as read.
3362 This is all marks except unread, ticked, dormant, and expirable."
3363   (not (or (= mark gnus-unread-mark)
3364            (= mark gnus-ticked-mark)
3365            (= mark gnus-spam-mark)
3366            (= mark gnus-dormant-mark)
3367            (= mark gnus-expirable-mark))))
3368
3369 (defmacro gnus-article-mark (number)
3370   "Return the MARK of article NUMBER.
3371 This macro should only be used when computing the mark the \"first\"
3372 time; i.e., when generating the summary lines.  After that,
3373 `gnus-summary-article-mark' should be used to examine the
3374 marks of articles."
3375   `(cond
3376     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3377     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3378     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3379     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3380     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3381     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3382     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3383     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3384            gnus-ancient-mark))))
3385
3386 ;; Saving hidden threads.
3387
3388 (defmacro gnus-save-hidden-threads (&rest forms)
3389   "Save hidden threads, eval FORMS, and restore the hidden threads."
3390   (let ((config (make-symbol "config")))
3391     `(let ((,config (gnus-hidden-threads-configuration)))
3392        (unwind-protect
3393            (save-excursion
3394              ,@forms)
3395          (gnus-restore-hidden-threads-configuration ,config)))))
3396 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3397 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3398
3399 (defun gnus-data-compute-positions ()
3400   "Compute the positions of all articles."
3401   (setq gnus-newsgroup-data-reverse nil)
3402   (let ((data gnus-newsgroup-data))
3403     (save-excursion
3404       (gnus-save-hidden-threads
3405         (gnus-summary-show-all-threads)
3406         (goto-char (point-min))
3407         (while data
3408           (while (get-text-property (point) 'gnus-intangible)
3409             (forward-line 1))
3410           (gnus-data-set-pos (car data) (+ (point) 3))
3411           (setq data (cdr data))
3412           (forward-line 1))))))
3413
3414 (defun gnus-hidden-threads-configuration ()
3415   "Return the current hidden threads configuration."
3416   (save-excursion
3417     (let (config)
3418       (goto-char (point-min))
3419       (while (not (eobp))
3420         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3421           (push (save-excursion (forward-line 0) (point)) config))
3422         (forward-line 1))
3423       config)))
3424
3425 (defun gnus-restore-hidden-threads-configuration (config)
3426   "Restore hidden threads configuration from CONFIG."
3427   (save-excursion
3428     (let (point (inhibit-read-only t))
3429       (while (setq point (pop config))
3430         (goto-char point)
3431         (gnus-summary-hide-thread)))))
3432
3433 ;; Various summary mode internalish functions.
3434
3435 (defun gnus-mouse-pick-article (e)
3436   (interactive "e")
3437   (mouse-set-point e)
3438   (gnus-summary-next-page nil t))
3439
3440 (defun gnus-summary-set-display-table ()
3441   "Change the display table.
3442 Odd characters have a tendency to mess
3443 up nicely formatted displays - we make all possible glyphs
3444 display only a single character."
3445
3446   ;; We start from the standard display table, if any.
3447   (let ((table (or (copy-sequence standard-display-table)
3448                    (make-display-table)))
3449         (i 32))
3450     ;; Nix out all the control chars...
3451     (while (>= (setq i (1- i)) 0)
3452       (gnus-put-display-table i [??] table))
3453    ;; ... but not newline and cr, of course.  (cr is necessary for the
3454     ;; selective display).
3455     (gnus-put-display-table ?\n nil table)
3456     (gnus-put-display-table ?\r nil table)
3457     ;; We keep TAB as well.
3458     (gnus-put-display-table ?\t nil table)
3459     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3460     ;; Emacs 23 (unicode), that are not set already.
3461     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3462                  160
3463                256)))
3464       (while (>= (setq i (1- i)) 127)
3465         ;; Only modify if the entry is nil.
3466         (unless (gnus-get-display-table i table)
3467           (gnus-put-display-table i [??] table))))
3468     (setq buffer-display-table table)))
3469
3470 (defun gnus-summary-set-article-display-arrow (pos)
3471   "Update the overlay arrow to point to line at position POS."
3472   (when gnus-summary-display-arrow
3473     (make-local-variable 'overlay-arrow-position)
3474     (make-local-variable 'overlay-arrow-string)
3475     (save-excursion
3476       (goto-char pos)
3477       (beginning-of-line)
3478       (unless overlay-arrow-position
3479         (setq overlay-arrow-position (make-marker)))
3480       (setq overlay-arrow-string "=>"
3481             overlay-arrow-position (set-marker overlay-arrow-position
3482                                                (point)
3483                                                (current-buffer))))))
3484
3485 (defun gnus-summary-setup-buffer (group)
3486   "Initialize summary buffer."
3487   (let ((buffer (gnus-summary-buffer-name group))
3488         (dead-name (concat "*Dead Summary "
3489                            (gnus-group-decoded-name group) "*")))
3490     ;; If a dead summary buffer exists, we kill it.
3491     (when (gnus-buffer-live-p dead-name)
3492       (gnus-kill-buffer dead-name))
3493     (if (get-buffer buffer)
3494         (progn
3495           (set-buffer buffer)
3496           (setq gnus-summary-buffer (current-buffer))
3497           (not gnus-newsgroup-prepared))
3498       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3499       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3500       (gnus-summary-mode group)
3501       (when (gnus-group-quit-config group)
3502         (set (make-local-variable 'gnus-single-article-buffer) nil))
3503       (make-local-variable 'gnus-article-buffer)
3504       (make-local-variable 'gnus-article-current)
3505       (make-local-variable 'gnus-original-article-buffer)
3506       (setq gnus-newsgroup-name group)
3507       ;; Set any local variables in the group parameters.
3508       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3509       t)))
3510
3511 (defun gnus-set-global-variables ()
3512   "Set the global equivalents of the buffer-local variables.
3513 They are set to the latest values they had.  These reflect the summary
3514 buffer that was in action when the last article was fetched."
3515   (when (eq major-mode 'gnus-summary-mode)
3516     (setq gnus-summary-buffer (current-buffer))
3517     (let ((name gnus-newsgroup-name)
3518           (marked gnus-newsgroup-marked)
3519           (spam gnus-newsgroup-spam-marked)
3520           (unread gnus-newsgroup-unreads)
3521           (headers gnus-current-headers)
3522           (data gnus-newsgroup-data)
3523           (summary gnus-summary-buffer)
3524           (article-buffer gnus-article-buffer)
3525           (original gnus-original-article-buffer)
3526           (gac gnus-article-current)
3527           (reffed gnus-reffed-article-number)
3528           (score-file gnus-current-score-file)
3529           (default-charset gnus-newsgroup-charset)
3530           vlist)
3531       (let ((locals gnus-newsgroup-variables))
3532         (while locals
3533           (if (consp (car locals))
3534               (push (eval (caar locals)) vlist)
3535             (push (eval (car locals)) vlist))
3536           (setq locals (cdr locals)))
3537         (setq vlist (nreverse vlist)))
3538       (with-current-buffer gnus-group-buffer
3539         (setq gnus-newsgroup-name name
3540               gnus-newsgroup-marked marked
3541               gnus-newsgroup-spam-marked spam
3542               gnus-newsgroup-unreads unread
3543               gnus-current-headers headers
3544               gnus-newsgroup-data data
3545               gnus-article-current gac
3546               gnus-summary-buffer summary
3547               gnus-article-buffer article-buffer
3548               gnus-original-article-buffer original
3549               gnus-reffed-article-number reffed
3550               gnus-current-score-file score-file
3551               gnus-newsgroup-charset default-charset)
3552         (let ((locals gnus-newsgroup-variables))
3553           (while locals
3554             (if (consp (car locals))
3555                 (set (caar locals) (pop vlist))
3556               (set (car locals) (pop vlist)))
3557             (setq locals (cdr locals))))
3558         ;; The article buffer also has local variables.
3559         (when (gnus-buffer-live-p gnus-article-buffer)
3560           (set-buffer gnus-article-buffer)
3561           (setq gnus-summary-buffer summary))))))
3562
3563 (defun gnus-summary-article-unread-p (article)
3564   "Say whether ARTICLE is unread or not."
3565   (memq article gnus-newsgroup-unreads))
3566
3567 (defun gnus-summary-first-article-p (&optional article)
3568   "Return whether ARTICLE is the first article in the buffer."
3569   (if (not (setq article (or article (gnus-summary-article-number))))
3570       nil
3571     (eq article (caar gnus-newsgroup-data))))
3572
3573 (defun gnus-summary-last-article-p (&optional article)
3574   "Return whether ARTICLE is the last article in the buffer."
3575   (if (not (setq article (or article (gnus-summary-article-number))))
3576       ;; All non-existent numbers are the last article.  :-)
3577       t
3578     (not (cdr (gnus-data-find-list article)))))
3579
3580 (defun gnus-make-thread-indent-array (&optional n)
3581   (when (or n
3582             (progn (setq n 200) nil)
3583             (null gnus-thread-indent-array)
3584             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3585     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3586           gnus-thread-indent-array-level gnus-thread-indent-level)
3587     (while (>= n 0)
3588       (aset gnus-thread-indent-array n
3589             (make-string (* n gnus-thread-indent-level) ? ))
3590       (setq n (1- n)))))
3591
3592 (defun gnus-update-summary-mark-positions ()
3593   "Compute where the summary marks are to go."
3594   (save-excursion
3595     (when (gnus-buffer-exists-p gnus-summary-buffer)
3596       (set-buffer gnus-summary-buffer))
3597     (let ((spec gnus-summary-line-format-spec)
3598           pos)
3599       (save-excursion
3600         (gnus-set-work-buffer)
3601         (let ((gnus-tmp-unread ?Z)
3602               (gnus-replied-mark ?Z)
3603               (gnus-score-below-mark ?Z)
3604               (gnus-score-over-mark ?Z)
3605               (gnus-undownloaded-mark ?Z)
3606               (gnus-summary-line-format-spec spec)
3607               (gnus-newsgroup-downloadable '(0))
3608               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3609               case-fold-search ignores)
3610           ;; Here, all marks are bound to Z.
3611           (gnus-summary-insert-line header
3612                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3613           (goto-char (point-min))
3614           ;; Memorize the positions of the same characters as dummy marks.
3615           (while (re-search-forward "[A-D]" nil t)
3616             (push (point) ignores))
3617           (erase-buffer)
3618           ;; We use A-D as dummy marks in order to know column positions
3619           ;; where marks should be inserted.
3620           (setq gnus-tmp-unread ?A
3621                 gnus-replied-mark ?B
3622                 gnus-score-below-mark ?C
3623                 gnus-score-over-mark ?C
3624                 gnus-undownloaded-mark ?D)
3625           (gnus-summary-insert-line header
3626                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3627           ;; Ignore characters which aren't dummy marks.
3628           (dolist (p ignores)
3629             (delete-region (goto-char (1- p)) p)
3630             (insert ?Z))
3631           (goto-char (point-min))
3632           (setq pos (list (cons 'unread
3633                                 (and (search-forward "A" nil t)
3634                                      (- (point) (point-min) 1)))))
3635           (goto-char (point-min))
3636           (push (cons 'replied (and (search-forward "B" nil t)
3637                                     (- (point) (point-min) 1)))
3638                 pos)
3639           (goto-char (point-min))
3640           (push (cons 'score (and (search-forward "C" nil t)
3641                                   (- (point) (point-min) 1)))
3642                 pos)
3643           (goto-char (point-min))
3644           (push (cons 'download (and (search-forward "D" nil t)
3645                                      (- (point) (point-min) 1)))
3646                 pos)))
3647       (setq gnus-summary-mark-positions pos))))
3648
3649 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3650   "Insert a dummy root in the summary buffer."
3651   (beginning-of-line)
3652   (gnus-add-text-properties
3653    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3654    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3655
3656 (defun gnus-summary-extract-address-component (from)
3657   (or (car (funcall gnus-extract-address-components from))
3658       from))
3659
3660 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3661   (let ((mail-parse-charset gnus-newsgroup-charset)
3662         (ignored-from-addresses (gnus-ignored-from-addresses))
3663         ; Is it really necessary to do this next part for each summary line?
3664         ; Luckily, doesn't seem to slow things down much.
3665         (mail-parse-ignored-charsets
3666          (with-current-buffer gnus-summary-buffer
3667            gnus-newsgroup-ignored-charsets)))
3668     (or
3669      (and ignored-from-addresses
3670           (string-match ignored-from-addresses gnus-tmp-from)
3671           (let ((extra-headers (mail-header-extra header))
3672                 to
3673                 newsgroups)
3674             (cond
3675              ((setq to (cdr (assq 'To extra-headers)))
3676               (concat gnus-summary-to-prefix
3677                       (inline
3678                         (gnus-summary-extract-address-component
3679                          (funcall gnus-decode-encoded-address-function to)))))
3680              ((setq newsgroups
3681                     (or
3682                      (cdr (assq 'Newsgroups extra-headers))
3683                      (and
3684                       (memq 'Newsgroups gnus-extra-headers)
3685                       (eq (car (gnus-find-method-for-group
3686                                 gnus-newsgroup-name)) 'nntp)
3687                       (gnus-group-real-name gnus-newsgroup-name))))
3688               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3689      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3690
3691 (defun gnus-summary-insert-line (gnus-tmp-header
3692                                  gnus-tmp-level gnus-tmp-current
3693                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3694                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3695                                  &optional gnus-tmp-dummy gnus-tmp-score
3696                                  gnus-tmp-process)
3697   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3698       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3699                                           gnus-tmp-level)))
3700   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3701          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3702          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3703          (gnus-tmp-score-char
3704           (if (or (null gnus-summary-default-score)
3705                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3706                       gnus-summary-zcore-fuzz))
3707               ?                         ;Whitespace
3708             (if (< gnus-tmp-score gnus-summary-default-score)
3709                 gnus-score-below-mark gnus-score-over-mark)))
3710          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3711          (gnus-tmp-replied
3712           (cond (gnus-tmp-process gnus-process-mark)
3713                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3714                  gnus-cached-mark)
3715                 (gnus-tmp-replied gnus-replied-mark)
3716                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3717                  gnus-forwarded-mark)
3718                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3719                  gnus-saved-mark)
3720                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3721                  gnus-recent-mark)
3722                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3723                  gnus-unseen-mark)
3724                 (t gnus-no-mark)))
3725          (gnus-tmp-downloaded
3726           (cond (undownloaded
3727                  gnus-undownloaded-mark)
3728                 (gnus-newsgroup-agentized
3729                  gnus-downloaded-mark)
3730                 (t
3731                  gnus-no-mark)))
3732          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3733          (gnus-tmp-name
3734           (cond
3735            ((string-match "<[^>]+> *$" gnus-tmp-from)
3736             (let ((beg (match-beginning 0)))
3737               (or (and (string-match "^\".+\"" gnus-tmp-from)
3738                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3739                   (substring gnus-tmp-from 0 beg))))
3740            ((string-match "(.+)" gnus-tmp-from)
3741             (substring gnus-tmp-from
3742                        (1+ (match-beginning 0)) (1- (match-end 0))))
3743            (t gnus-tmp-from)))
3744          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3745          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3746          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3747          (inhibit-read-only t))
3748     (when (string= gnus-tmp-name "")
3749       (setq gnus-tmp-name gnus-tmp-from))
3750     (unless (numberp gnus-tmp-lines)
3751       (setq gnus-tmp-lines -1))
3752     (if (= gnus-tmp-lines -1)
3753         (setq gnus-tmp-lines "?")
3754       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3755     (condition-case ()
3756         (gnus-put-text-property
3757          (point)
3758          (progn (eval gnus-summary-line-format-spec) (point))
3759          'gnus-number gnus-tmp-number)
3760       (error (gnus-message 5 "Error updating the summary line")))
3761     (when (gnus-visual-p 'summary-highlight 'highlight)
3762       (forward-line -1)
3763       (gnus-summary-highlight-line)
3764       (gnus-run-hooks 'gnus-summary-update-hook)
3765       (forward-line 1))))
3766
3767 (defun gnus-summary-update-line (&optional dont-update)
3768   "Update summary line after change."
3769   (when (and gnus-summary-default-score
3770              (not gnus-summary-inhibit-highlight))
3771     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3772            (article (gnus-summary-article-number))
3773            (score (gnus-summary-article-score article)))
3774       (unless dont-update
3775         (if (and gnus-summary-mark-below
3776                  (< (gnus-summary-article-score)
3777                     gnus-summary-mark-below))
3778             ;; This article has a low score, so we mark it as read.
3779             (when (memq article gnus-newsgroup-unreads)
3780               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3781           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3782             ;; This article was previously marked as read on account
3783             ;; of a low score, but now it has risen, so we mark it as
3784             ;; unread.
3785             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3786         (gnus-summary-update-mark
3787          (if (or (null gnus-summary-default-score)
3788                  (<= (abs (- score gnus-summary-default-score))
3789                      gnus-summary-zcore-fuzz))
3790              ?                          ;Whitespace
3791            (if (< score gnus-summary-default-score)
3792                gnus-score-below-mark gnus-score-over-mark))
3793          'score))
3794       ;; Do visual highlighting.
3795       (when (gnus-visual-p 'summary-highlight 'highlight)
3796         (gnus-summary-highlight-line)
3797         (gnus-run-hooks 'gnus-summary-update-hook)))))
3798
3799 (defvar gnus-tmp-new-adopts nil)
3800
3801 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3802   "Return the number of articles in THREAD.
3803 This may be 0 in some cases -- if none of the articles in
3804 the thread are to be displayed."
3805   (let* ((number
3806          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3807           (cond
3808            ((not (listp thread))
3809             1)
3810            ((and (consp thread) (cdr thread))
3811             (apply
3812              '+ 1 (mapcar
3813                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3814            ((null thread)
3815             1)
3816            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3817             1)
3818            (t 0))))
3819     (when (and level (zerop level) gnus-tmp-new-adopts)
3820       (incf number
3821             (apply '+ (mapcar
3822                        'gnus-summary-number-of-articles-in-thread
3823                        gnus-tmp-new-adopts))))
3824     (if char
3825         (if (> number 1) gnus-not-empty-thread-mark
3826           gnus-empty-thread-mark)
3827       number)))
3828
3829 (defsubst gnus-summary-line-message-size (head)
3830   "Return pretty-printed version of message size.
3831 This function is intended to be used in
3832 `gnus-summary-line-format-alist'."
3833   (let ((c (or (mail-header-chars head) -1)))
3834     (cond ((< c 0) "n/a")               ; chars not available
3835           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3836           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3837           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3838           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3839
3840
3841 (defun gnus-summary-set-local-parameters (group)
3842   "Go through the local params of GROUP and set all variable specs in that list."
3843   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3844                                         ; really variables.
3845     (dolist (elem (gnus-group-find-parameter group))
3846       (and (consp elem)                 ; Has to be a cons.
3847            (consp (cdr elem))           ; The cdr has to be a list.
3848            (symbolp (car elem))         ; Has to be a symbol in there.
3849            (not (memq (car elem) vars))
3850            (ignore-errors
3851              (push (car elem) vars)
3852              ;; Variables like `gnus-show-threads' that are globally
3853              ;; bound, if used as group parameters, need to get to be
3854              ;; buffer-local, whereas just parameters like `gcc-self',
3855              ;; `timestamp', etc. should not be bound as variables.
3856              (if (boundp (car elem))
3857                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3858                (eval (nth 1 elem))))))))
3859
3860 (defun gnus-summary-read-group (group &optional show-all no-article
3861                                       kill-buffer no-display backward
3862                                       select-articles)
3863   "Start reading news in newsgroup GROUP.
3864 If SHOW-ALL is non-nil, already read articles are also listed.
3865 If NO-ARTICLE is non-nil, no article is selected initially.
3866 If NO-DISPLAY, don't generate a summary buffer."
3867   (let (result)
3868     (while (and group
3869                 (null (setq result
3870                             (let ((gnus-auto-select-next nil))
3871                               (or (gnus-summary-read-group-1
3872                                    group show-all no-article
3873                                    kill-buffer no-display
3874                                    select-articles)
3875                                   (setq show-all nil
3876                                         select-articles nil)))))
3877                 (eq gnus-auto-select-next 'quietly))
3878       (set-buffer gnus-group-buffer)
3879       ;; The entry function called above goes to the next
3880       ;; group automatically, so we go two groups back
3881       ;; if we are searching for the previous group.
3882       (when backward
3883         (gnus-group-prev-unread-group 2))
3884       (if (not (equal group (gnus-group-group-name)))
3885           (setq group (gnus-group-group-name))
3886         (setq group nil)))
3887     result))
3888
3889 (defun gnus-summary-read-group-1 (group show-all no-article
3890                                         kill-buffer no-display
3891                                         &optional select-articles)
3892   ;; Killed foreign groups can't be entered.
3893   ;;  (when (and (not (gnus-group-native-p group))
3894   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3895   ;;    (error "Dead non-native groups can't be entered"))
3896   (gnus-message 5 "Retrieving newsgroup: %s..."
3897                 (gnus-group-decoded-name group))
3898   (let* ((new-group (gnus-summary-setup-buffer group))
3899          (quit-config (gnus-group-quit-config group))
3900          (did-select (and new-group (gnus-select-newsgroup
3901                                      group show-all select-articles))))
3902     (cond
3903      ;; This summary buffer exists already, so we just select it.
3904      ((not new-group)
3905       (gnus-set-global-variables)
3906       (when kill-buffer
3907         (gnus-kill-or-deaden-summary kill-buffer))
3908       (gnus-configure-windows 'summary 'force)
3909       (gnus-set-mode-line 'summary)
3910       (gnus-summary-position-point)
3911       (message "")
3912       t)
3913      ;; We couldn't select this group.
3914      ((null did-select)
3915       (when (and (eq major-mode 'gnus-summary-mode)
3916                  (not (equal (current-buffer) kill-buffer)))
3917         (kill-buffer (current-buffer))
3918         (if (not quit-config)
3919             (progn
3920               ;; Update the info -- marks might need to be removed,
3921               ;; for instance.
3922               (gnus-summary-update-info)
3923               (set-buffer gnus-group-buffer)
3924               (gnus-group-jump-to-group group)
3925               (gnus-group-next-unread-group 1))
3926           (gnus-handle-ephemeral-exit quit-config)))
3927       (let ((grpinfo (gnus-get-info group)))
3928         (if (null (gnus-info-read grpinfo))
3929             (gnus-message 3 "Group %s contains no messages"
3930                           (gnus-group-decoded-name group))
3931           (gnus-message 3 "Can't select group")))
3932       nil)
3933      ;; The user did a `C-g' while prompting for number of articles,
3934      ;; so we exit this group.
3935      ((eq did-select 'quit)
3936       (and (eq major-mode 'gnus-summary-mode)
3937            (not (equal (current-buffer) kill-buffer))
3938            (kill-buffer (current-buffer)))
3939       (when kill-buffer
3940         (gnus-kill-or-deaden-summary kill-buffer))
3941       (if (not quit-config)
3942           (progn
3943             (set-buffer gnus-group-buffer)
3944             (gnus-group-jump-to-group group)
3945             (gnus-configure-windows 'group 'force))
3946         (gnus-handle-ephemeral-exit quit-config))
3947       ;; Finally signal the quit.
3948       (signal 'quit nil))
3949      ;; The group was successfully selected.
3950      (t
3951       (gnus-set-global-variables)
3952       ;; Save the active value in effect when the group was entered.
3953       (setq gnus-newsgroup-active
3954             (gnus-copy-sequence
3955              (gnus-active gnus-newsgroup-name)))
3956       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
3957       ;; You can change the summary buffer in some way with this hook.
3958       (gnus-run-hooks 'gnus-select-group-hook)
3959       (when (memq 'summary (gnus-update-format-specifications
3960                             nil 'summary 'summary-mode 'summary-dummy))
3961         ;; The format specification for the summary line was updated,
3962         ;; so we need to update the mark positions as well.
3963         (gnus-update-summary-mark-positions))
3964       ;; Do score processing.
3965       (when gnus-use-scoring
3966         (gnus-possibly-score-headers))
3967       ;; Check whether to fill in the gaps in the threads.
3968       (when gnus-build-sparse-threads
3969         (gnus-build-sparse-threads))
3970       ;; Find the initial limit.
3971       (if show-all
3972           (let ((gnus-newsgroup-dormant nil))
3973             (gnus-summary-initial-limit show-all))
3974         (gnus-summary-initial-limit show-all))
3975       ;; Generate the summary buffer.
3976       (unless no-display
3977         (gnus-summary-prepare))
3978       (when gnus-use-trees
3979         (gnus-tree-open group)
3980         (setq gnus-summary-highlight-line-function
3981               'gnus-tree-highlight-article))
3982       ;; If the summary buffer is empty, but there are some low-scored
3983       ;; articles or some excluded dormants, we include these in the
3984       ;; buffer.
3985       (when (and (zerop (buffer-size))
3986                  (not no-display))
3987         (cond (gnus-newsgroup-dormant
3988                (gnus-summary-limit-include-dormant))
3989               ((and gnus-newsgroup-scored show-all)
3990                (gnus-summary-limit-include-expunged t))))
3991       ;; Function `gnus-apply-kill-file' must be called in this hook.
3992       (gnus-run-hooks 'gnus-apply-kill-hook)
3993       (if (and (zerop (buffer-size))
3994                (not no-display))
3995           (progn
3996             ;; This newsgroup is empty.
3997             (gnus-summary-catchup-and-exit nil t)
3998             (gnus-message 6 "No unread news")
3999             (when kill-buffer
4000               (gnus-kill-or-deaden-summary kill-buffer))
4001             ;; Return nil from this function.
4002             nil)
4003         ;; Hide conversation thread subtrees.  We cannot do this in
4004         ;; gnus-summary-prepare-hook since kill processing may not
4005         ;; work with hidden articles.
4006         (gnus-summary-maybe-hide-threads)
4007         (when kill-buffer
4008           (gnus-kill-or-deaden-summary kill-buffer))
4009         (gnus-summary-auto-select-subject)
4010         ;; Show first unread article if requested.
4011         (if (and (not no-article)
4012                  (not no-display)
4013                  gnus-newsgroup-unreads
4014                  gnus-auto-select-first)
4015             (progn
4016               (gnus-configure-windows 'summary)
4017               (let ((art (gnus-summary-article-number)))
4018                 (unless (and (not gnus-plugged)
4019                              (or (memq art gnus-newsgroup-undownloaded)
4020                                  (memq art gnus-newsgroup-downloadable)))
4021                   (gnus-summary-goto-article art))))
4022           ;; Don't select any articles.
4023           (gnus-summary-position-point)
4024           (gnus-configure-windows 'summary 'force)
4025           (gnus-set-mode-line 'summary))
4026         (when (and gnus-auto-center-group
4027                    (get-buffer-window gnus-group-buffer t))
4028           ;; Gotta use windows, because recenter does weird stuff if
4029           ;; the current buffer ain't the displayed window.
4030           (let ((owin (selected-window)))
4031             (select-window (get-buffer-window gnus-group-buffer t))
4032             (when (gnus-group-goto-group group)
4033               (recenter))
4034             (select-window owin)))
4035         ;; Mark this buffer as "prepared".
4036         (setq gnus-newsgroup-prepared t)
4037         (gnus-run-hooks 'gnus-summary-prepared-hook)
4038         (unless (gnus-ephemeral-group-p group)
4039           (gnus-group-update-group group))
4040         t)))))
4041
4042 (defun gnus-summary-auto-select-subject ()
4043   "Select the subject line on initial group entry."
4044   (goto-char (point-min))
4045   (cond
4046    ((eq gnus-auto-select-subject 'best)
4047     (gnus-summary-best-unread-subject))
4048    ((eq gnus-auto-select-subject 'unread)
4049     (gnus-summary-first-unread-subject))
4050    ((eq gnus-auto-select-subject 'unseen)
4051     (gnus-summary-first-unseen-subject))
4052    ((eq gnus-auto-select-subject 'unseen-or-unread)
4053     (gnus-summary-first-unseen-or-unread-subject))
4054    ((eq gnus-auto-select-subject 'first)
4055     ;; Do nothing.
4056     )
4057    ((functionp gnus-auto-select-subject)
4058     (funcall gnus-auto-select-subject))))
4059
4060 (defun gnus-summary-prepare ()
4061   "Generate the summary buffer."
4062   (interactive)
4063   (let ((inhibit-read-only t))
4064     (erase-buffer)
4065     (setq gnus-newsgroup-data nil
4066           gnus-newsgroup-data-reverse nil)
4067     (gnus-run-hooks 'gnus-summary-generate-hook)
4068     ;; Generate the buffer, either with threads or without.
4069     (when gnus-newsgroup-headers
4070       (gnus-summary-prepare-threads
4071        (if gnus-show-threads
4072            (gnus-sort-gathered-threads
4073             (funcall gnus-summary-thread-gathering-function
4074                      (gnus-sort-threads
4075                       (gnus-cut-threads (gnus-make-threads)))))
4076          ;; Unthreaded display.
4077          (gnus-sort-articles gnus-newsgroup-headers))))
4078     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4079     ;; Call hooks for modifying summary buffer.
4080     (goto-char (point-min))
4081     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4082
4083 (defsubst gnus-general-simplify-subject (subject)
4084   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4085   (setq subject
4086         (cond
4087          ;; Truncate the subject.
4088          (gnus-simplify-subject-functions
4089           (gnus-map-function gnus-simplify-subject-functions subject))
4090          ((numberp gnus-summary-gather-subject-limit)
4091           (setq subject (gnus-simplify-subject-re subject))
4092           (if (> (length subject) gnus-summary-gather-subject-limit)
4093               (substring subject 0 gnus-summary-gather-subject-limit)
4094             subject))
4095          ;; Fuzzily simplify it.
4096          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4097           (gnus-simplify-subject-fuzzy subject))
4098          ;; Just remove the leading "Re:".
4099          (t
4100           (gnus-simplify-subject-re subject))))
4101
4102   (if (and gnus-summary-gather-exclude-subject
4103            (string-match gnus-summary-gather-exclude-subject subject))
4104       nil                         ; This article shouldn't be gathered
4105     subject))
4106
4107 (defun gnus-summary-simplify-subject-query ()
4108   "Query where the respool algorithm would put this article."
4109   (interactive)
4110   (gnus-summary-select-article)
4111   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4112
4113 (defun gnus-gather-threads-by-subject (threads)
4114   "Gather threads by looking at Subject headers."
4115   (if (not gnus-summary-make-false-root)
4116       threads
4117     (let ((hashtb (gnus-make-hashtable 1024))
4118           (prev threads)
4119           (result threads)
4120           subject hthread whole-subject)
4121       (while threads
4122         (setq subject (gnus-general-simplify-subject
4123                        (setq whole-subject (mail-header-subject
4124                                             (caar threads)))))
4125         (when subject
4126           (if (setq hthread (gnus-gethash subject hashtb))
4127               (progn
4128                 ;; We enter a dummy root into the thread, if we
4129                 ;; haven't done that already.
4130                 (unless (stringp (caar hthread))
4131                   (setcar hthread (list whole-subject (car hthread))))
4132                 ;; We add this new gathered thread to this gathered
4133                 ;; thread.
4134                 (setcdr (car hthread)
4135                         (nconc (cdar hthread) (list (car threads))))
4136                 ;; Remove it from the list of threads.
4137                 (setcdr prev (cdr threads))
4138                 (setq threads prev))
4139             ;; Enter this thread into the hash table.
4140             (gnus-sethash subject
4141                           (if gnus-summary-make-false-root-always
4142                               (progn
4143                                 ;; If you want a dummy root above all
4144                                 ;; threads...
4145                                 (setcar threads (list whole-subject
4146                                                       (car threads)))
4147                                 threads)
4148                             threads)
4149                           hashtb)))
4150         (setq prev threads)
4151         (setq threads (cdr threads)))
4152       result)))
4153
4154 (defun gnus-gather-threads-by-references (threads)
4155   "Gather threads by looking at References headers."
4156   (let ((idhashtb (gnus-make-hashtable 1024))
4157         (thhashtb (gnus-make-hashtable 1024))
4158         (prev threads)
4159         (result threads)
4160         ids references id gthread gid entered ref)
4161     (while threads
4162       (when (setq references (mail-header-references (caar threads)))
4163         (setq id (mail-header-id (caar threads))
4164               ids (inline (gnus-split-references references))
4165               entered nil)
4166         (while (setq ref (pop ids))
4167           (setq ids (delete ref ids))
4168           (if (not (setq gid (gnus-gethash ref idhashtb)))
4169               (progn
4170                 (gnus-sethash ref id idhashtb)
4171                 (gnus-sethash id threads thhashtb))
4172             (setq gthread (gnus-gethash gid thhashtb))
4173             (unless entered
4174               ;; We enter a dummy root into the thread, if we
4175               ;; haven't done that already.
4176               (unless (stringp (caar gthread))
4177                 (setcar gthread (list (mail-header-subject (caar gthread))
4178                                       (car gthread))))
4179               ;; We add this new gathered thread to this gathered
4180               ;; thread.
4181               (setcdr (car gthread)
4182                       (nconc (cdar gthread) (list (car threads)))))
4183             ;; Add it into the thread hash table.
4184             (gnus-sethash id gthread thhashtb)
4185             (setq entered t)
4186             ;; Remove it from the list of threads.
4187             (setcdr prev (cdr threads))
4188             (setq threads prev))))
4189       (setq prev threads)
4190       (setq threads (cdr threads)))
4191     result))
4192
4193 (defun gnus-sort-gathered-threads (threads)
4194   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4195   (let ((result threads))
4196     (while threads
4197       (when (stringp (caar threads))
4198         (setcdr (car threads)
4199                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4200       (setq threads (cdr threads)))
4201     result))
4202
4203 (defun gnus-thread-loop-p (root thread)
4204   "Say whether ROOT is in THREAD."
4205   (let ((stack (list thread))
4206         (infloop 0)
4207         th)
4208     (while (setq thread (pop stack))
4209       (setq th (cdr thread))
4210       (while (and th
4211                   (not (eq (caar th) root)))
4212         (pop th))
4213       (if th
4214           ;; We have found a loop.
4215           (let (ref-dep)
4216             (setcdr thread (delq (car th) (cdr thread)))
4217             (if (boundp (setq ref-dep (intern "none"
4218                                               gnus-newsgroup-dependencies)))
4219                 (setcdr (symbol-value ref-dep)
4220                         (nconc (cdr (symbol-value ref-dep))
4221                                (list (car th))))
4222               (set ref-dep (list nil (car th))))
4223             (setq infloop 1
4224                   stack nil))
4225         ;; Push all the subthreads onto the stack.
4226         (push (cdr thread) stack)))
4227     infloop))
4228
4229 (defun gnus-make-threads ()
4230   "Go through the dependency hashtb and find the roots.  Return all threads."
4231   (let (threads)
4232     (while (catch 'infloop
4233              (mapatoms
4234               (lambda (refs)
4235                 ;; Deal with self-referencing References loops.
4236                 (when (and (car (symbol-value refs))
4237                            (not (zerop
4238                                  (apply
4239                                   '+
4240                                   (mapcar
4241                                    (lambda (thread)
4242                                      (gnus-thread-loop-p
4243                                       (car (symbol-value refs)) thread))
4244                                    (cdr (symbol-value refs)))))))
4245                   (setq threads nil)
4246                   (throw 'infloop t))
4247                 (unless (car (symbol-value refs))
4248                   ;; These threads do not refer back to any other
4249                   ;; articles, so they're roots.
4250                   (setq threads (append (cdr (symbol-value refs)) threads))))
4251               gnus-newsgroup-dependencies)))
4252     threads))
4253
4254 ;; Build the thread tree.
4255 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4256   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4257
4258 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4259 if it was already present.
4260
4261 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4262 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4263 Message-IDs will be renamed to a unique Message-ID before being
4264 entered.
4265
4266 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4267   (let* ((id (mail-header-id header))
4268          (id-dep (and id (intern id dependencies)))
4269          parent-id ref ref-dep ref-header replaced)
4270     ;; Enter this `header' in the `dependencies' table.
4271     (cond
4272      ((not id-dep)
4273       (setq header nil))
4274      ;; The first two cases do the normal part: enter a new `header'
4275      ;; in the `dependencies' table.
4276      ((not (boundp id-dep))
4277       (set id-dep (list header)))
4278      ((null (car (symbol-value id-dep)))
4279       (setcar (symbol-value id-dep) header))
4280
4281      ;; From here the `header' was already present in the
4282      ;; `dependencies' table.
4283      (force-new
4284       ;; Overrides an existing entry;
4285       ;; just set the header part of the entry.
4286       (setcar (symbol-value id-dep) header)
4287       (setq replaced t))
4288
4289      ;; Renames the existing `header' to a unique Message-ID.
4290      ((not gnus-summary-ignore-duplicates)
4291       ;; An article with this Message-ID has already been seen.
4292       ;; We rename the Message-ID.
4293       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4294            (list header))
4295       (mail-header-set-id header id))
4296
4297      ;; The last case ignores an existing entry, except it adds any
4298      ;; additional Xrefs (in case the two articles came from different
4299      ;; servers.
4300      ;; Also sets `header' to `nil' meaning that the `dependencies'
4301      ;; table was *not* modified.
4302      (t
4303       (mail-header-set-xref
4304        (car (symbol-value id-dep))
4305        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4306                    "")
4307                (or (mail-header-xref header) "")))
4308       (setq header nil)))
4309
4310     (when (and header (not replaced))
4311       ;; First check that we are not creating a References loop.
4312       (setq parent-id (gnus-parent-id (mail-header-references header)))
4313       (setq ref parent-id)
4314       (while (and ref
4315                   (setq ref-dep (intern-soft ref dependencies))
4316                   (boundp ref-dep)
4317                   (setq ref-header (car (symbol-value ref-dep))))
4318         (if (string= id ref)
4319             ;; Yuk!  This is a reference loop.  Make the article be a
4320             ;; root article.
4321             (progn
4322               (mail-header-set-references (car (symbol-value id-dep)) "none")
4323               (setq ref nil)
4324               (setq parent-id nil))
4325           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4326       (setq ref-dep (intern (or parent-id "none") dependencies))
4327       (if (boundp ref-dep)
4328           (setcdr (symbol-value ref-dep)
4329                   (nconc (cdr (symbol-value ref-dep))
4330                          (list (symbol-value id-dep))))
4331         (set ref-dep (list nil (symbol-value id-dep)))))
4332     header))
4333
4334 (defun gnus-extract-message-id-from-in-reply-to (string)
4335   (if (string-match "<[^>]+>" string)
4336       (substring string (match-beginning 0) (match-end 0))
4337     nil))
4338
4339 (defun gnus-build-sparse-threads ()
4340   (let ((headers gnus-newsgroup-headers)
4341         (mail-parse-charset gnus-newsgroup-charset)
4342         (gnus-summary-ignore-duplicates t)
4343         header references generation relations
4344         subject child end new-child date)
4345     ;; First we create an alist of generations/relations, where
4346     ;; generations is how much we trust the relation, and the relation
4347     ;; is parent/child.
4348     (gnus-message 7 "Making sparse threads...")
4349     (save-excursion
4350       (nnheader-set-temp-buffer " *gnus sparse threads*")
4351       (while (setq header (pop headers))
4352         (when (and (setq references (mail-header-references header))
4353                    (not (string= references "")))
4354           (insert references)
4355           (setq child (mail-header-id header)
4356                 subject (mail-header-subject header)
4357                 date (mail-header-date header)
4358                 generation 0)
4359           (while (search-backward ">" nil t)
4360             (setq end (1+ (point)))
4361             (when (search-backward "<" nil t)
4362               (setq new-child (buffer-substring (point) end))
4363               (push (list (incf generation)
4364                           child (setq child new-child)
4365                           subject date)
4366                     relations)))
4367           (when child
4368             (push (list (1+ generation) child nil subject) relations))
4369           (erase-buffer)))
4370       (kill-buffer (current-buffer)))
4371     ;; Sort over trustworthiness.
4372     (dolist (relation (sort relations 'car-less-than-car))
4373       (when (gnus-dependencies-add-header
4374              (make-full-mail-header
4375               gnus-reffed-article-number
4376               (nth 3 relation) "" (or (nth 4 relation) "")
4377               (nth 1 relation)
4378               (or (nth 2 relation) "") 0 0 "")
4379              gnus-newsgroup-dependencies nil)
4380         (push gnus-reffed-article-number gnus-newsgroup-limit)
4381         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4382         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4383               gnus-newsgroup-reads)
4384         (decf gnus-reffed-article-number)))
4385     (gnus-message 7 "Making sparse threads...done")))
4386
4387 (defun gnus-build-old-threads ()
4388   ;; Look at all the articles that refer back to old articles, and
4389   ;; fetch the headers for the articles that aren't there.  This will
4390   ;; build complete threads - if the roots haven't been expired by the
4391   ;; server, that is.
4392   (let ((mail-parse-charset gnus-newsgroup-charset)
4393         id heads)
4394     (mapatoms
4395      (lambda (refs)
4396        (when (not (car (symbol-value refs)))
4397          (setq heads (cdr (symbol-value refs)))
4398          (while heads
4399            (if (memq (mail-header-number (caar heads))
4400                      gnus-newsgroup-dormant)
4401                (setq heads (cdr heads))
4402              (setq id (symbol-name refs))
4403              (while (and (setq id (gnus-build-get-header id))
4404                          (not (car (gnus-id-to-thread id)))))
4405              (setq heads nil)))))
4406      gnus-newsgroup-dependencies)))
4407
4408 (defsubst gnus-remove-odd-characters (string)
4409   "Translate STRING into something that doesn't contain weird characters."
4410   (mm-subst-char-in-string
4411    ?\r ?\-
4412    (mm-subst-char-in-string ?\n ?\- string t) t))
4413
4414 ;; This function has to be called with point after the article number
4415 ;; on the beginning of the line.
4416 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4417   (let ((eol (point-at-eol))
4418         (buffer (current-buffer))
4419         header references in-reply-to)
4420
4421     ;; overview: [num subject from date id refs chars lines misc]
4422     (unwind-protect
4423         (let (x)
4424           (narrow-to-region (point) eol)
4425           (unless (eobp)
4426             (forward-char))
4427
4428           (setq header
4429                 (make-full-mail-header
4430                  number                 ; number
4431                  (condition-case ()     ; subject
4432                      (gnus-remove-odd-characters
4433                       (funcall gnus-decode-encoded-word-function
4434                                (setq x (nnheader-nov-field))))
4435                    (error x))
4436                  (condition-case ()     ; from
4437                      (gnus-remove-odd-characters
4438                       (funcall gnus-decode-encoded-address-function
4439                                (setq x (nnheader-nov-field))))
4440                    (error x))
4441                  (nnheader-nov-field)   ; date
4442                  (nnheader-nov-read-message-id number)  ; id
4443                  (setq references (nnheader-nov-field)) ; refs
4444                  (nnheader-nov-read-integer) ; chars
4445                  (nnheader-nov-read-integer) ; lines
4446                  (unless (eobp)
4447                    (if (looking-at "Xref: ")
4448                        (goto-char (match-end 0)))
4449                    (nnheader-nov-field)) ; Xref
4450                  (nnheader-nov-parse-extra)))) ; extra
4451
4452       (widen))
4453
4454     (when (and (string= references "")
4455                (setq in-reply-to (mail-header-extra header))
4456                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4457       (mail-header-set-references
4458        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4459
4460     (when gnus-alter-header-function
4461       (funcall gnus-alter-header-function header))
4462     (gnus-dependencies-add-header header dependencies force-new)))
4463
4464 (defun gnus-build-get-header (id)
4465   "Look through the buffer of NOV lines and find the header to ID.
4466 Enter this line into the dependencies hash table, and return
4467 the id of the parent article (if any)."
4468   (let ((deps gnus-newsgroup-dependencies)
4469         found header)
4470     (prog1
4471         (with-current-buffer nntp-server-buffer
4472           (let ((case-fold-search nil))
4473             (goto-char (point-min))
4474             (while (and (not found)
4475                         (search-forward id nil t))
4476               (beginning-of-line)
4477               (setq found (looking-at
4478                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4479                                    (regexp-quote id))))
4480               (or found (beginning-of-line 2)))
4481             (when found
4482               (beginning-of-line)
4483               (and
4484                (setq header (gnus-nov-parse-line
4485                              (read (current-buffer)) deps))
4486                (gnus-parent-id (mail-header-references header))))))
4487       (when header
4488         (let ((number (mail-header-number header)))
4489           (push number gnus-newsgroup-limit)
4490           (push header gnus-newsgroup-headers)
4491           (if (memq number gnus-newsgroup-unselected)
4492               (progn
4493                 (setq gnus-newsgroup-unreads
4494                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4495                                                number))
4496                 (setq gnus-newsgroup-unselected
4497                       (delq number gnus-newsgroup-unselected)))
4498             (push number gnus-newsgroup-ancient)))))))
4499
4500 (defun gnus-build-all-threads ()
4501   "Read all the headers."
4502   (let ((gnus-summary-ignore-duplicates t)
4503         (mail-parse-charset gnus-newsgroup-charset)
4504         (dependencies gnus-newsgroup-dependencies)
4505         header article)
4506     (with-current-buffer nntp-server-buffer
4507       (let ((case-fold-search nil))
4508         (goto-char (point-min))
4509         (while (not (eobp))
4510           (ignore-errors
4511             (setq article (read (current-buffer))
4512                   header (gnus-nov-parse-line article dependencies t)))
4513           (when header
4514             (with-current-buffer gnus-summary-buffer
4515               (push header gnus-newsgroup-headers)
4516               (if (memq (setq article (mail-header-number header))
4517                         gnus-newsgroup-unselected)
4518                   (progn
4519                     (setq gnus-newsgroup-unreads
4520                           (gnus-add-to-sorted-list
4521                            gnus-newsgroup-unreads article))
4522                     (setq gnus-newsgroup-unselected
4523                           (delq article gnus-newsgroup-unselected)))
4524                 (push article gnus-newsgroup-ancient)))
4525             (forward-line 1)))))))
4526
4527 (defun gnus-summary-update-article-line (article header)
4528   "Update the line for ARTICLE using HEADER."
4529   (let* ((id (mail-header-id header))
4530          (thread (gnus-id-to-thread id)))
4531     (unless thread
4532       (error "Article in no thread"))
4533     ;; Update the thread.
4534     (setcar thread header)
4535     (gnus-summary-goto-subject article)
4536     (let* ((datal (gnus-data-find-list article))
4537            (data (car datal))
4538            (inhibit-read-only t)
4539            (level (gnus-summary-thread-level)))
4540       (gnus-delete-line)
4541       (let ((inserted (- (point)
4542                          (progn
4543                            (gnus-summary-insert-line
4544                             header level nil
4545                             (memq article gnus-newsgroup-undownloaded)
4546                             (gnus-article-mark article)
4547                             (memq article gnus-newsgroup-replied)
4548                             (memq article gnus-newsgroup-expirable)
4549                             ;; Only insert the Subject string when it's different
4550                             ;; from the previous Subject string.
4551                             (if (and
4552                                  gnus-show-threads
4553                                  (gnus-subject-equal
4554                                   (condition-case ()
4555                                       (mail-header-subject
4556                                        (gnus-data-header
4557                                         (cadr
4558                                          (gnus-data-find-list
4559                                           article
4560                                           (gnus-data-list t)))))
4561                                     ;; Error on the side of excessive subjects.
4562                                     (error ""))
4563                                   (mail-header-subject header)))
4564                                 ""
4565                               (mail-header-subject header))
4566                             nil (cdr (assq article gnus-newsgroup-scored))
4567                             (memq article gnus-newsgroup-processable))
4568                            (point)))))
4569         (when (cdr datal)
4570           (gnus-data-update-list
4571            (cdr datal)
4572            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4573
4574 (defun gnus-summary-update-article (article &optional iheader)
4575   "Update ARTICLE in the summary buffer."
4576   (set-buffer gnus-summary-buffer)
4577   (let* ((header (gnus-summary-article-header article))
4578          (id (mail-header-id header))
4579          (data (gnus-data-find article))
4580          (thread (gnus-id-to-thread id))
4581          (references (mail-header-references header))
4582          (parent
4583           (gnus-id-to-thread
4584            (or (gnus-parent-id
4585                 (when (and references
4586                            (not (equal "" references)))
4587                   references))
4588                "none")))
4589          (inhibit-read-only t)
4590          (old (car thread)))
4591     (when thread
4592       (unless iheader
4593         (setcar thread nil)
4594         (when parent
4595           (delq thread parent)))
4596       (if (gnus-summary-insert-subject id header)
4597           ;; Set the (possibly) new article number in the data structure.
4598           (gnus-data-set-number data (gnus-id-to-article id))
4599         (setcar thread old)
4600         nil))))
4601
4602 (defun gnus-rebuild-thread (id &optional line)
4603   "Rebuild the thread containing ID.
4604 If LINE, insert the rebuilt thread starting on line LINE."
4605   (let ((inhibit-read-only t)
4606         old-pos current thread data)
4607     (if (not gnus-show-threads)
4608         (setq thread (list (car (gnus-id-to-thread id))))
4609       ;; Get the thread this article is part of.
4610       (setq thread (gnus-remove-thread id)))
4611     (setq old-pos (point-at-bol))
4612     (setq current (save-excursion
4613                     (and (re-search-backward "[\r\n]" nil t)
4614                          (gnus-summary-article-number))))
4615     ;; If this is a gathered thread, we have to go some re-gathering.
4616     (when (stringp (car thread))
4617       (let ((subject (car thread))
4618             roots thr)
4619         (setq thread (cdr thread))
4620         (while thread
4621           (unless (memq (setq thr (gnus-id-to-thread
4622                                    (gnus-root-id
4623                                     (mail-header-id (caar thread)))))
4624                         roots)
4625             (push thr roots))
4626           (setq thread (cdr thread)))
4627         ;; We now have all (unique) roots.
4628         (if (= (length roots) 1)
4629             ;; All the loose roots are now one solid root.
4630             (setq thread (car roots))
4631           (setq thread (cons subject (gnus-sort-threads roots))))))
4632     (let (threads)
4633       ;; We then insert this thread into the summary buffer.
4634       (when line
4635         (goto-char (point-min))
4636         (forward-line (1- line)))
4637       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4638         (if gnus-show-threads
4639             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4640           (gnus-summary-prepare-unthreaded thread))
4641         (setq data (nreverse gnus-newsgroup-data))
4642         (setq threads gnus-newsgroup-threads))
4643       ;; We splice the new data into the data structure.
4644       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4645       ;;!!! then we want to insert at the beginning of the buffer.
4646       ;;!!! That happens to be true with Gnus now, but that may
4647       ;;!!! change in the future.  Perhaps.
4648       (gnus-data-enter-list
4649        (if line nil current) data (- (point) old-pos))
4650       (setq gnus-newsgroup-threads
4651             (nconc threads gnus-newsgroup-threads))
4652       (gnus-data-compute-positions))))
4653
4654 (defun gnus-number-to-header (number)
4655   "Return the header for article NUMBER."
4656   (let ((headers gnus-newsgroup-headers))
4657     (while (and headers
4658                 (not (= number (mail-header-number (car headers)))))
4659       (pop headers))
4660     (when headers
4661       (car headers))))
4662
4663 (defun gnus-parent-headers (in-headers &optional generation)
4664   "Return the headers of the GENERATIONeth parent of HEADERS."
4665   (unless generation
4666     (setq generation 1))
4667   (let ((parent t)
4668         (headers in-headers)
4669         references)
4670     (while (and parent
4671                 (not (zerop generation))
4672                 (setq references (mail-header-references headers)))
4673       (setq headers (if (and references
4674                              (setq parent (gnus-parent-id references)))
4675                         (car (gnus-id-to-thread parent))
4676                       nil))
4677       (decf generation))
4678     (and (not (eq headers in-headers))
4679          headers)))
4680
4681 (defun gnus-id-to-thread (id)
4682   "Return the (sub-)thread where ID appears."
4683   (gnus-gethash id gnus-newsgroup-dependencies))
4684
4685 (defun gnus-id-to-article (id)
4686   "Return the article number of ID."
4687   (let ((thread (gnus-id-to-thread id)))
4688     (when (and thread
4689                (car thread))
4690       (mail-header-number (car thread)))))
4691
4692 (defun gnus-id-to-header (id)
4693   "Return the article headers of ID."
4694   (car (gnus-id-to-thread id)))
4695
4696 (defun gnus-article-displayed-root-p (article)
4697   "Say whether ARTICLE is a root(ish) article."
4698   (let ((level (gnus-summary-thread-level article))
4699         (refs (mail-header-references  (gnus-summary-article-header article)))
4700         particle)
4701     (cond
4702      ((null level) nil)
4703      ((zerop level) t)
4704      ((null refs) t)
4705      ((null (gnus-parent-id refs)) t)
4706      ((and (= 1 level)
4707            (null (setq particle (gnus-id-to-article
4708                                  (gnus-parent-id refs))))
4709            (null (gnus-summary-thread-level particle)))))))
4710
4711 (defun gnus-root-id (id)
4712   "Return the id of the root of the thread where ID appears."
4713   (let (last-id prev)
4714     (while (and id (setq prev (car (gnus-id-to-thread id))))
4715       (setq last-id id
4716             id (gnus-parent-id (mail-header-references prev))))
4717     last-id))
4718
4719 (defun gnus-articles-in-thread (thread)
4720   "Return the list of articles in THREAD."
4721   (cons (mail-header-number (car thread))
4722         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4723
4724 (defun gnus-remove-thread (id &optional dont-remove)
4725   "Remove the thread that has ID in it."
4726   (let (headers thread last-id)
4727     ;; First go up in this thread until we find the root.
4728     (setq last-id (gnus-root-id id)
4729           headers (message-flatten-list (gnus-id-to-thread last-id)))
4730     ;; We have now found the real root of this thread.  It might have
4731     ;; been gathered into some loose thread, so we have to search
4732     ;; through the threads to find the thread we wanted.
4733     (let ((threads gnus-newsgroup-threads)
4734           sub)
4735       (while threads
4736         (setq sub (car threads))
4737         (if (stringp (car sub))
4738             ;; This is a gathered thread, so we look at the roots
4739             ;; below it to find whether this article is in this
4740             ;; gathered root.
4741             (progn
4742               (setq sub (cdr sub))
4743               (while sub
4744                 (when (member (caar sub) headers)
4745                   (setq thread (car threads)
4746                         threads nil
4747                         sub nil))
4748                 (setq sub (cdr sub))))
4749           ;; It's an ordinary thread, so we check it.
4750           (when (eq (car sub) (car headers))
4751             (setq thread sub
4752                   threads nil)))
4753         (setq threads (cdr threads)))
4754       ;; If this article is in no thread, then it's a root.
4755       (if thread
4756           (unless dont-remove
4757             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4758         (setq thread (gnus-id-to-thread last-id)))
4759       (when thread
4760         (prog1
4761             thread                      ; We return this thread.
4762           (unless dont-remove
4763             (if (stringp (car thread))
4764                 (progn
4765                   ;; If we use dummy roots, then we have to remove the
4766                   ;; dummy root as well.
4767                   (when (eq gnus-summary-make-false-root 'dummy)
4768                     ;; We go to the dummy root by going to
4769                     ;; the first sub-"thread", and then one line up.
4770                     (gnus-summary-goto-article
4771                      (mail-header-number (caadr thread)))
4772                     (forward-line -1)
4773                     (gnus-delete-line)
4774                     (gnus-data-compute-positions))
4775                   (setq thread (cdr thread))
4776                   (while thread
4777                     (gnus-remove-thread-1 (car thread))
4778                     (setq thread (cdr thread))))
4779               (gnus-remove-thread-1 thread))))))))
4780
4781 (defun gnus-remove-thread-1 (thread)
4782   "Remove the thread THREAD recursively."
4783   (let ((number (mail-header-number (pop thread)))
4784         d)
4785     (setq thread (reverse thread))
4786     (while thread
4787       (gnus-remove-thread-1 (pop thread)))
4788     (when (setq d (gnus-data-find number))
4789       (goto-char (gnus-data-pos d))
4790       (gnus-summary-show-thread)
4791       (gnus-data-remove
4792        number
4793        (- (point-at-bol)
4794           (prog1
4795               (1+ (point-at-eol))
4796             (gnus-delete-line)))))))
4797
4798 (defun gnus-sort-threads-recursive (threads func)
4799   (sort (mapcar (lambda (thread)
4800                   (cons (car thread)
4801                         (and (cdr thread)
4802                              (gnus-sort-threads-recursive (cdr thread) func))))
4803                 threads) func))
4804
4805 (defun gnus-sort-threads-loop (threads func)
4806   (let* ((superthread (cons nil threads))
4807          (stack (list (cons superthread threads)))
4808          remaining-threads thread)
4809     (while stack
4810       (setq remaining-threads (cdr (car stack)))
4811       (if remaining-threads
4812           (progn (setq thread (car remaining-threads))
4813                  (setcdr (car stack) (cdr remaining-threads))
4814                  (if (cdr thread)
4815                      (push (cons thread (cdr thread)) stack)))
4816         (setq thread (caar stack))
4817         (setcdr thread (sort (cdr thread) func))
4818         (pop stack)))
4819     (cdr superthread)))
4820
4821 (defun gnus-sort-threads (threads)
4822   "Sort THREADS."
4823   (if (not gnus-thread-sort-functions)
4824       threads
4825     (gnus-message 8 "Sorting threads...")
4826     (prog1
4827         (condition-case nil
4828             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4829               (gnus-sort-threads-recursive
4830                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4831           ;; Even after binding max-lisp-eval-depth, the recursive
4832           ;; sorter might fail for very long threads.  In that case,
4833           ;; try using a (less well-tested) non-recursive sorter.
4834           (error (gnus-message 9 "Sorting threads with loop...")
4835                  (gnus-sort-threads-loop
4836                   threads (gnus-make-sort-function
4837                            gnus-thread-sort-functions))))
4838       (gnus-message 8 "Sorting threads...done"))))
4839
4840 (defun gnus-sort-articles (articles)
4841   "Sort ARTICLES."
4842   (when gnus-article-sort-functions
4843     (gnus-message 7 "Sorting articles...")
4844     (prog1
4845         (setq gnus-newsgroup-headers
4846               (sort articles (gnus-make-sort-function
4847                               gnus-article-sort-functions)))
4848       (gnus-message 7 "Sorting articles...done"))))
4849
4850 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4851 (defmacro gnus-thread-header (thread)
4852   "Return header of first article in THREAD.
4853 Note that THREAD must never, ever be anything else than a variable -
4854 using some other form will lead to serious barfage."
4855   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4856   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4857   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4858         (vector thread) 2))
4859
4860 (defsubst gnus-article-sort-by-number (h1 h2)
4861   "Sort articles by article number."
4862   (< (mail-header-number h1)
4863      (mail-header-number h2)))
4864
4865 (defun gnus-thread-sort-by-number (h1 h2)
4866   "Sort threads by root article number."
4867   (gnus-article-sort-by-number
4868    (gnus-thread-header h1) (gnus-thread-header h2)))
4869
4870 (defsubst gnus-article-sort-by-random (h1 h2)
4871   "Sort articles randomly."
4872   (zerop (random 2)))
4873
4874 (defun gnus-thread-sort-by-random (h1 h2)
4875   "Sort threads randomly."
4876   (gnus-article-sort-by-random
4877    (gnus-thread-header h1) (gnus-thread-header h2)))
4878
4879 (defsubst gnus-article-sort-by-lines (h1 h2)
4880   "Sort articles by article Lines header."
4881   (< (mail-header-lines h1)
4882      (mail-header-lines h2)))
4883
4884 (defun gnus-thread-sort-by-lines (h1 h2)
4885   "Sort threads by root article Lines header."
4886   (gnus-article-sort-by-lines
4887    (gnus-thread-header h1) (gnus-thread-header h2)))
4888
4889 (defsubst gnus-article-sort-by-chars (h1 h2)
4890   "Sort articles by octet length."
4891   (< (mail-header-chars h1)
4892      (mail-header-chars h2)))
4893
4894 (defun gnus-thread-sort-by-chars (h1 h2)
4895   "Sort threads by root article octet length."
4896   (gnus-article-sort-by-chars
4897    (gnus-thread-header h1) (gnus-thread-header h2)))
4898
4899 (defsubst gnus-article-sort-by-author (h1 h2)
4900   "Sort articles by root author."
4901   (gnus-string<
4902    (let ((extract (funcall
4903                    gnus-extract-address-components
4904                    (mail-header-from h1))))
4905      (or (car extract) (cadr extract) ""))
4906    (let ((extract (funcall
4907                    gnus-extract-address-components
4908                    (mail-header-from h2))))
4909      (or (car extract) (cadr extract) ""))))
4910
4911 (defun gnus-thread-sort-by-author (h1 h2)
4912   "Sort threads by root author."
4913   (gnus-article-sort-by-author
4914    (gnus-thread-header h1)  (gnus-thread-header h2)))
4915
4916 (defsubst gnus-article-sort-by-recipient (h1 h2)
4917   "Sort articles by recipient."
4918   (gnus-string<
4919    (let ((extract (funcall
4920                    gnus-extract-address-components
4921                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4922      (or (car extract) (cadr extract)))
4923    (let ((extract (funcall
4924                    gnus-extract-address-components
4925                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4926      (or (car extract) (cadr extract)))))
4927
4928 (defun gnus-thread-sort-by-recipient (h1 h2)
4929   "Sort threads by root recipient."
4930   (gnus-article-sort-by-recipient
4931    (gnus-thread-header h1) (gnus-thread-header h2)))
4932
4933 (defsubst gnus-article-sort-by-subject (h1 h2)
4934   "Sort articles by root subject."
4935   (gnus-string<
4936    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4937    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4938
4939 (defun gnus-thread-sort-by-subject (h1 h2)
4940   "Sort threads by root subject."
4941   (gnus-article-sort-by-subject
4942    (gnus-thread-header h1) (gnus-thread-header h2)))
4943
4944 (defsubst gnus-article-sort-by-date (h1 h2)
4945   "Sort articles by root article date."
4946   (time-less-p
4947    (gnus-date-get-time (mail-header-date h1))
4948    (gnus-date-get-time (mail-header-date h2))))
4949
4950 (defun gnus-thread-sort-by-date (h1 h2)
4951   "Sort threads by root article date."
4952   (gnus-article-sort-by-date
4953    (gnus-thread-header h1) (gnus-thread-header h2)))
4954
4955 (defsubst gnus-article-sort-by-score (h1 h2)
4956   "Sort articles by root article score.
4957 Unscored articles will be counted as having a score of zero."
4958   (> (or (cdr (assq (mail-header-number h1)
4959                     gnus-newsgroup-scored))
4960          gnus-summary-default-score 0)
4961      (or (cdr (assq (mail-header-number h2)
4962                     gnus-newsgroup-scored))
4963          gnus-summary-default-score 0)))
4964
4965 (defun gnus-thread-sort-by-score (h1 h2)
4966   "Sort threads by root article score."
4967   (gnus-article-sort-by-score
4968    (gnus-thread-header h1) (gnus-thread-header h2)))
4969
4970 (defun gnus-thread-sort-by-total-score (h1 h2)
4971   "Sort threads by the sum of all scores in the thread.
4972 Unscored articles will be counted as having a score of zero."
4973   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4974
4975 (defun gnus-thread-total-score (thread)
4976   ;; This function find the total score of THREAD.
4977   (cond
4978    ((null thread)
4979     0)
4980    ((consp thread)
4981     (if (stringp (car thread))
4982         (apply gnus-thread-score-function 0
4983                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4984       (gnus-thread-total-score-1 thread)))
4985    (t
4986     (gnus-thread-total-score-1 (list thread)))))
4987
4988 (defun gnus-article-sort-by-most-recent-number (h1 h2)
4989   "Sort articles by number."
4990   (gnus-article-sort-by-number h1 h2))
4991
4992 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4993   "Sort threads such that the thread with the most recently arrived article comes first."
4994   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4995
4996 (defun gnus-thread-highest-number (thread)
4997   "Return the highest article number in THREAD."
4998   (apply 'max (mapcar (lambda (header)
4999                         (mail-header-number header))
5000                       (message-flatten-list thread))))
5001
5002 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5003   "Sort articles by number."
5004   (gnus-article-sort-by-date h1 h2))
5005
5006 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5007   "Sort threads such that the thread with the most recently dated article comes first."
5008   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5009
5010 ; Since this is called not only to sort the top-level threads, but
5011 ; also in recursive sorts to order the articles within a thread, each
5012 ; article will be processed many times.  Thus it speeds things up
5013 ; quite a bit to use gnus-date-get-time, which caches the time value.
5014 (defun gnus-thread-latest-date (thread)
5015   "Return the highest article date in THREAD."
5016   (apply 'max
5017          (mapcar (lambda (header) (gnus-float-time
5018                                    (gnus-date-get-time
5019                                     (mail-header-date header))))
5020                  (message-flatten-list thread))))
5021
5022 (defun gnus-thread-total-score-1 (root)
5023   ;; This function find the total score of the thread below ROOT.
5024   (setq root (car root))
5025   (apply gnus-thread-score-function
5026          (or (append
5027               (mapcar 'gnus-thread-total-score
5028                       (cdr (gnus-id-to-thread (mail-header-id root))))
5029               (when (> (mail-header-number root) 0)
5030                 (list (or (cdr (assq (mail-header-number root)
5031                                      gnus-newsgroup-scored))
5032                           gnus-summary-default-score 0))))
5033              (list gnus-summary-default-score)
5034              '(0))))
5035
5036 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5037 (defvar gnus-tmp-prev-subject nil)
5038 (defvar gnus-tmp-false-parent nil)
5039 (defvar gnus-tmp-root-expunged nil)
5040 (defvar gnus-tmp-dummy-line nil)
5041
5042 (defun gnus-extra-header (type &optional header)
5043   "Return the extra header of TYPE."
5044   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5045       ""))
5046
5047 (defvar gnus-tmp-thread-tree-header-string "")
5048
5049 (defcustom gnus-sum-thread-tree-root "> "
5050   "With %B spec, used for the root of a thread.
5051 If nil, use subject instead."
5052   :version "22.1"
5053   :type '(radio (const :format "%v  " nil) string)
5054   :group 'gnus-thread)
5055
5056 (defcustom gnus-sum-thread-tree-false-root "> "
5057   "With %B spec, used for a false root of a thread.
5058 If nil, use subject instead."
5059   :version "22.1"
5060   :type '(radio (const :format "%v  " nil) string)
5061   :group 'gnus-thread)
5062
5063 (defcustom gnus-sum-thread-tree-single-indent ""
5064   "With %B spec, used for a thread with just one message.
5065 If nil, use subject instead."
5066   :version "22.1"
5067   :type '(radio (const :format "%v  " nil) string)
5068   :group 'gnus-thread)
5069
5070 (defcustom gnus-sum-thread-tree-vertical "| "
5071   "With %B spec, used for drawing a vertical line."
5072   :version "22.1"
5073   :type 'string
5074   :group 'gnus-thread)
5075
5076 (defcustom gnus-sum-thread-tree-indent "  "
5077   "With %B spec, used for indenting."
5078   :version "22.1"
5079   :type 'string
5080   :group 'gnus-thread)
5081
5082 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5083   "With %B spec, used for a leaf with brothers."
5084   :version "22.1"
5085   :type 'string
5086   :group 'gnus-thread)
5087
5088 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5089   "With %B spec, used for a leaf without brothers."
5090   :version "22.1"
5091   :type 'string
5092   :group 'gnus-thread)
5093
5094 (defcustom gnus-summary-display-while-building nil
5095   "If non-nil, show and update the summary buffer as it's being built.
5096 If the value is t, update the buffer after every line is inserted.  If
5097 the value is an integer (N), update the display every N lines."
5098   :version "22.1"
5099   :group 'gnus-thread
5100   :type '(choice (const :tag "off" nil)
5101                  number
5102                  (const :tag "frequently" t)))
5103
5104 (defun gnus-summary-prepare-threads (threads)
5105   "Prepare summary buffer from THREADS and indentation LEVEL.
5106 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5107 or a straight list of headers."
5108   (gnus-message 7 "Generating summary...")
5109
5110   (setq gnus-newsgroup-threads threads)
5111   (beginning-of-line)
5112
5113   (let ((gnus-tmp-level 0)
5114         (default-score (or gnus-summary-default-score 0))
5115         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5116         (building-line-count gnus-summary-display-while-building)
5117         (building-count (integerp gnus-summary-display-while-building))
5118         thread number subject stack state gnus-tmp-gathered beg-match
5119         new-roots gnus-tmp-new-adopts thread-end simp-subject
5120         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5121         gnus-tmp-replied gnus-tmp-subject-or-nil
5122         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5123         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5124         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5125         tree-stack)
5126
5127     (setq gnus-tmp-prev-subject nil
5128           gnus-tmp-thread-tree-header-string "")
5129
5130     (if (vectorp (car threads))
5131         ;; If this is a straight (sic) list of headers, then a
5132         ;; threaded summary display isn't required, so we just create
5133         ;; an unthreaded one.
5134         (gnus-summary-prepare-unthreaded threads)
5135
5136       ;; Do the threaded display.
5137
5138       (if gnus-summary-display-while-building
5139           (switch-to-buffer (buffer-name)))
5140       (while (or threads stack gnus-tmp-new-adopts new-roots)
5141
5142         (if (and (= gnus-tmp-level 0)
5143                  (or (not stack)
5144                      (= (caar stack) 0))
5145                  (not gnus-tmp-false-parent)
5146                  (or gnus-tmp-new-adopts new-roots))
5147             (if gnus-tmp-new-adopts
5148                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5149                       thread (list (car gnus-tmp-new-adopts))
5150                       gnus-tmp-header (caar thread)
5151                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5152               (when new-roots
5153                 (setq thread (list (car new-roots))
5154                       gnus-tmp-header (caar thread)
5155                       new-roots (cdr new-roots))))
5156
5157           (if threads
5158               ;; If there are some threads, we do them before the
5159               ;; threads on the stack.
5160               (setq thread threads
5161                     gnus-tmp-header (caar thread))
5162             ;; There were no current threads, so we pop something off
5163             ;; the stack.
5164             (setq state (car stack)
5165                   gnus-tmp-level (car state)
5166                   tree-stack (cadr state)
5167                   thread (caddr state)
5168                   stack (cdr stack)
5169                   gnus-tmp-header (caar thread))))
5170
5171         (setq gnus-tmp-false-parent nil)
5172         (setq gnus-tmp-root-expunged nil)
5173         (setq thread-end nil)
5174
5175         (if (stringp gnus-tmp-header)
5176             ;; The header is a dummy root.
5177             (cond
5178              ((eq gnus-summary-make-false-root 'adopt)
5179               ;; We let the first article adopt the rest.
5180               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5181                                                (cddar thread)))
5182               (setq gnus-tmp-gathered
5183                     (nconc (mapcar
5184                             (lambda (h) (mail-header-number (car h)))
5185                             (cddar thread))
5186                            gnus-tmp-gathered))
5187               (setq thread (cons (list (caar thread)
5188                                        (cadar thread))
5189                                  (cdr thread)))
5190               (setq gnus-tmp-level -1
5191                     gnus-tmp-false-parent t))
5192              ((eq gnus-summary-make-false-root 'empty)
5193               ;; We print adopted articles with empty subject fields.
5194               (setq gnus-tmp-gathered
5195                     (nconc (mapcar
5196                             (lambda (h) (mail-header-number (car h)))
5197                             (cddar thread))
5198                            gnus-tmp-gathered))
5199               (setq gnus-tmp-level -1))
5200              ((eq gnus-summary-make-false-root 'dummy)
5201               ;; We remember that we probably want to output a dummy
5202               ;; root.
5203               (setq gnus-tmp-dummy-line gnus-tmp-header)
5204               (setq gnus-tmp-prev-subject gnus-tmp-header))
5205              (t
5206               ;; We do not make a root for the gathered
5207               ;; sub-threads at all.
5208               (setq gnus-tmp-level -1)))
5209
5210           (setq number (mail-header-number gnus-tmp-header)
5211                 subject (mail-header-subject gnus-tmp-header)
5212                 simp-subject (gnus-simplify-subject-fully subject))
5213
5214           (cond
5215            ;; If the thread has changed subject, we might want to make
5216            ;; this subthread into a root.
5217            ((and (null gnus-thread-ignore-subject)
5218                  (not (zerop gnus-tmp-level))
5219                  gnus-tmp-prev-subject
5220                  (not (string= gnus-tmp-prev-subject simp-subject)))
5221             (setq new-roots (nconc new-roots (list (car thread)))
5222                   thread-end t
5223                   gnus-tmp-header nil))
5224            ;; If the article lies outside the current limit,
5225            ;; then we do not display it.
5226            ((not (memq number gnus-newsgroup-limit))
5227             (setq gnus-tmp-gathered
5228                   (nconc (mapcar
5229                           (lambda (h) (mail-header-number (car h)))
5230                           (cdar thread))
5231                          gnus-tmp-gathered))
5232             (setq gnus-tmp-new-adopts (if (cdar thread)
5233                                           (append gnus-tmp-new-adopts
5234                                                   (cdar thread))
5235                                         gnus-tmp-new-adopts)
5236                   thread-end t
5237                   gnus-tmp-header nil)
5238             (when (zerop gnus-tmp-level)
5239               (setq gnus-tmp-root-expunged t)))
5240            ;; Perhaps this article is to be marked as read?
5241            ((and gnus-summary-mark-below
5242                  (< (or (cdr (assq number gnus-newsgroup-scored))
5243                         default-score)
5244                     gnus-summary-mark-below)
5245                  ;; Don't touch sparse articles.
5246                  (not (gnus-summary-article-sparse-p number))
5247                  (not (gnus-summary-article-ancient-p number)))
5248             (setq gnus-newsgroup-unreads
5249                   (delq number gnus-newsgroup-unreads))
5250             (if gnus-newsgroup-auto-expire
5251                 (setq gnus-newsgroup-expirable
5252                       (gnus-add-to-sorted-list
5253                        gnus-newsgroup-expirable number))
5254               (push (cons number gnus-low-score-mark)
5255                     gnus-newsgroup-reads))))
5256
5257           (when gnus-tmp-header
5258             ;; We may have an old dummy line to output before this
5259             ;; article.
5260             (when (and gnus-tmp-dummy-line
5261                        (gnus-subject-equal
5262                         gnus-tmp-dummy-line
5263                         (mail-header-subject gnus-tmp-header)))
5264               (gnus-summary-insert-dummy-line
5265                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5266               (setq gnus-tmp-dummy-line nil))
5267
5268             ;; Compute the mark.
5269             (setq gnus-tmp-unread (gnus-article-mark number))
5270
5271             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5272                                   gnus-tmp-header gnus-tmp-level)
5273                   gnus-newsgroup-data)
5274
5275             ;; Actually insert the line.
5276             (setq
5277              gnus-tmp-subject-or-nil
5278              (cond
5279               ((and gnus-thread-ignore-subject
5280                     gnus-tmp-prev-subject
5281                     (not (string= gnus-tmp-prev-subject simp-subject)))
5282                subject)
5283               ((zerop gnus-tmp-level)
5284                (if (and (eq gnus-summary-make-false-root 'empty)
5285                         (memq number gnus-tmp-gathered)
5286                         gnus-tmp-prev-subject
5287                         (string= gnus-tmp-prev-subject simp-subject))
5288                    gnus-summary-same-subject
5289                  subject))
5290               (t gnus-summary-same-subject)))
5291             (if (and (eq gnus-summary-make-false-root 'adopt)
5292                      (= gnus-tmp-level 1)
5293                      (memq number gnus-tmp-gathered))
5294                 (setq gnus-tmp-opening-bracket ?\<
5295                       gnus-tmp-closing-bracket ?\>)
5296               (setq gnus-tmp-opening-bracket ?\[
5297                     gnus-tmp-closing-bracket ?\]))
5298             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5299                 (gnus-make-thread-indent-array
5300                  (max (* 2 (length gnus-thread-indent-array))
5301                       gnus-tmp-level)))
5302             (setq
5303              gnus-tmp-indentation
5304              (aref gnus-thread-indent-array gnus-tmp-level)
5305              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5306              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5307                                 gnus-summary-default-score 0)
5308              gnus-tmp-score-char
5309              (if (or (null gnus-summary-default-score)
5310                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5311                          gnus-summary-zcore-fuzz))
5312                  ?                      ;Whitespace
5313                (if (< gnus-tmp-score gnus-summary-default-score)
5314                    gnus-score-below-mark gnus-score-over-mark))
5315              gnus-tmp-replied
5316              (cond ((memq number gnus-newsgroup-processable)
5317                     gnus-process-mark)
5318                    ((memq number gnus-newsgroup-cached)
5319                     gnus-cached-mark)
5320                    ((memq number gnus-newsgroup-replied)
5321                     gnus-replied-mark)
5322                    ((memq number gnus-newsgroup-forwarded)
5323                     gnus-forwarded-mark)
5324                    ((memq number gnus-newsgroup-saved)
5325                     gnus-saved-mark)
5326                    ((memq number gnus-newsgroup-recent)
5327                     gnus-recent-mark)
5328                    ((memq number gnus-newsgroup-unseen)
5329                     gnus-unseen-mark)
5330                    (t gnus-no-mark))
5331              gnus-tmp-downloaded
5332              (cond ((memq number gnus-newsgroup-undownloaded)
5333                     gnus-undownloaded-mark)
5334                    (gnus-newsgroup-agentized
5335                     gnus-downloaded-mark)
5336                    (t
5337                     gnus-no-mark))
5338              gnus-tmp-from (mail-header-from gnus-tmp-header)
5339              gnus-tmp-name
5340              (cond
5341               ((string-match "<[^>]+> *$" gnus-tmp-from)
5342                (setq beg-match (match-beginning 0))
5343                (or (and (string-match "^\".+\"" gnus-tmp-from)
5344                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5345                    (substring gnus-tmp-from 0 beg-match)))
5346               ((string-match "(.+)" gnus-tmp-from)
5347                (substring gnus-tmp-from
5348                           (1+ (match-beginning 0)) (1- (match-end 0))))
5349               (t gnus-tmp-from))
5350
5351              ;; Do the %B string
5352              gnus-tmp-thread-tree-header-string
5353              (cond
5354               ((not gnus-show-threads) "")
5355               ((zerop gnus-tmp-level)
5356                (cond ((cdar thread)
5357                       (or gnus-sum-thread-tree-root subject))
5358                      (gnus-tmp-new-adopts
5359                       (or gnus-sum-thread-tree-false-root subject))
5360                      (t
5361                       (or gnus-sum-thread-tree-single-indent subject))))
5362               (t
5363                (concat (apply 'concat
5364                               (mapcar (lambda (item)
5365                                         (if (= item 1)
5366                                             gnus-sum-thread-tree-vertical
5367                                           gnus-sum-thread-tree-indent))
5368                                       (cdr (reverse tree-stack))))
5369                        (if (nth 1 thread)
5370                            gnus-sum-thread-tree-leaf-with-other
5371                          gnus-sum-thread-tree-single-leaf)))))
5372             (when (string= gnus-tmp-name "")
5373               (setq gnus-tmp-name gnus-tmp-from))
5374             (unless (numberp gnus-tmp-lines)
5375               (setq gnus-tmp-lines -1))
5376             (if (= gnus-tmp-lines -1)
5377                 (setq gnus-tmp-lines "?")
5378               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5379             (gnus-put-text-property
5380              (point)
5381              (progn (eval gnus-summary-line-format-spec) (point))
5382              'gnus-number number)
5383             (when gnus-visual-p
5384               (forward-line -1)
5385               (gnus-summary-highlight-line)
5386               (when gnus-summary-update-hook
5387                 (gnus-run-hooks 'gnus-summary-update-hook))
5388               (forward-line 1))
5389
5390             (setq gnus-tmp-prev-subject simp-subject)))
5391
5392         (when (nth 1 thread)
5393           (push (list (max 0 gnus-tmp-level)
5394                       (copy-sequence tree-stack)
5395                       (nthcdr 1 thread))
5396                 stack))
5397         (push (if (nth 1 thread) 1 0) tree-stack)
5398         (incf gnus-tmp-level)
5399         (setq threads (if thread-end nil (cdar thread)))
5400         (if gnus-summary-display-while-building
5401             (if building-count
5402                 (progn
5403                   ;; use a set frequency
5404                   (setq building-line-count (1- building-line-count))
5405                   (when (= building-line-count 0)
5406                     (sit-for 0)
5407                     (setq building-line-count
5408                           gnus-summary-display-while-building)))
5409               ;; always
5410               (sit-for 0)))
5411         (unless threads
5412           (setq gnus-tmp-level 0)))))
5413   (gnus-message 7 "Generating summary...done"))
5414
5415 (defun gnus-summary-prepare-unthreaded (headers)
5416   "Generate an unthreaded summary buffer based on HEADERS."
5417   (let (header number mark)
5418
5419     (beginning-of-line)
5420
5421     (while headers
5422       ;; We may have to root out some bad articles...
5423       (when (memq (setq number (mail-header-number
5424                                 (setq header (pop headers))))
5425                   gnus-newsgroup-limit)
5426         ;; Mark article as read when it has a low score.
5427         (when (and gnus-summary-mark-below
5428                    (< (or (cdr (assq number gnus-newsgroup-scored))
5429                           gnus-summary-default-score 0)
5430                       gnus-summary-mark-below)
5431                    (not (gnus-summary-article-ancient-p number)))
5432           (setq gnus-newsgroup-unreads
5433                 (delq number gnus-newsgroup-unreads))
5434           (if gnus-newsgroup-auto-expire
5435               (push number gnus-newsgroup-expirable)
5436             (push (cons number gnus-low-score-mark)
5437                   gnus-newsgroup-reads)))
5438
5439         (setq mark (gnus-article-mark number))
5440         (push (gnus-data-make number mark (1+ (point)) header 0)
5441               gnus-newsgroup-data)
5442         (gnus-summary-insert-line
5443          header 0 number
5444          (memq number gnus-newsgroup-undownloaded)
5445          mark (memq number gnus-newsgroup-replied)
5446          (memq number gnus-newsgroup-expirable)
5447          (mail-header-subject header) nil
5448          (cdr (assq number gnus-newsgroup-scored))
5449          (memq number gnus-newsgroup-processable))))))
5450
5451 (defun gnus-summary-remove-list-identifiers ()
5452   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5453   (let ((regexp (if (consp gnus-list-identifiers)
5454                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5455                   gnus-list-identifiers))
5456         changed subject)
5457     (when regexp
5458       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5459       (dolist (header gnus-newsgroup-headers)
5460         (setq subject (mail-header-subject header)
5461               changed nil)
5462         (while (string-match regexp subject)
5463           (setq subject
5464                 (concat (substring subject 0 (match-beginning 1))
5465                         (substring subject (match-end 0)))
5466                 changed t))
5467         (when changed
5468           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5469             (setq subject
5470                   (concat (substring subject 0 (match-beginning 1))
5471                           (substring subject (match-end 1)))))
5472           (mail-header-set-subject header subject))))))
5473
5474 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5475   "Fetch headers of ARTICLES."
5476   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5477     (gnus-message 5 "Fetching headers for %s..." name)
5478     (prog1
5479         (if (eq 'nov
5480                 (setq gnus-headers-retrieved-by
5481                       (gnus-retrieve-headers
5482                        articles gnus-newsgroup-name
5483                        (or limit
5484                            ;; We might want to fetch old headers, but
5485                            ;; not if there is only 1 article.
5486                            (and (or (and
5487                                      (not (eq gnus-fetch-old-headers 'some))
5488                                      (not (numberp gnus-fetch-old-headers)))
5489                                     (> (length articles) 1))
5490                                 gnus-fetch-old-headers)))))
5491             (gnus-get-newsgroup-headers-xover
5492              articles force-new dependencies gnus-newsgroup-name t)
5493           (gnus-get-newsgroup-headers dependencies force-new))
5494       (gnus-message 5 "Fetching headers for %s...done" name))))
5495
5496 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5497   "Select newsgroup GROUP.
5498 If READ-ALL is non-nil, all articles in the group are selected.
5499 If SELECT-ARTICLES, only select those articles from GROUP."
5500   (let* ((entry (gnus-group-entry group))
5501          ;;!!! Dirty hack; should be removed.
5502          (gnus-summary-ignore-duplicates
5503           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5504               t
5505             gnus-summary-ignore-duplicates))
5506          (info (nth 2 entry))
5507          charset articles fetched-articles cached)
5508
5509     (unless (gnus-check-server
5510              (set (make-local-variable 'gnus-current-select-method)
5511                   (gnus-find-method-for-group group)))
5512       (error "Couldn't open server"))
5513     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5514
5515     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5516         (gnus-activate-group group)     ; Or we can activate it...
5517         (progn                          ; Or we bug out.
5518           (when (equal major-mode 'gnus-summary-mode)
5519             (gnus-kill-buffer (current-buffer)))
5520           (error
5521            "Couldn't activate group %s: %s"
5522            (mm-decode-coding-string group charset)
5523            (mm-decode-coding-string (gnus-status-message group) charset))))
5524
5525     (unless (gnus-request-group group t)
5526       (when (equal major-mode 'gnus-summary-mode)
5527         (gnus-kill-buffer (current-buffer)))
5528       (error "Couldn't request group %s: %s"
5529              (mm-decode-coding-string group charset)
5530              (mm-decode-coding-string (gnus-status-message group) charset)))
5531
5532     (when gnus-agent
5533       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5534
5535       (setq gnus-summary-use-undownloaded-faces
5536             (gnus-agent-find-parameter
5537              group
5538              'agent-enable-undownloaded-faces)))
5539
5540     (setq gnus-newsgroup-name group
5541           gnus-newsgroup-unselected nil
5542           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5543
5544     (let ((display (gnus-group-find-parameter group 'display)))
5545       (setq gnus-newsgroup-display
5546             (cond
5547              ((not (zerop (or (car-safe read-all) 0)))
5548               ;; The user entered the group with C-u SPC/RET, let's show
5549               ;; all articles.
5550               'gnus-not-ignore)
5551              ((eq display 'all)
5552               'gnus-not-ignore)
5553              ((arrayp display)
5554               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5555              ((numberp display)
5556               ;; The following is probably the "correct" solution, but
5557               ;; it makes Gnus fetch all headers and then limit the
5558               ;; articles (which is slow), so instead we hack the
5559               ;; select-articles parameter instead. -- Simon Josefsson
5560               ;; <jas@kth.se>
5561               ;;
5562               ;; (gnus-byte-compile
5563               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5564               ;;                         display)))))
5565               (setq select-articles
5566                     (gnus-uncompress-range
5567                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5568                              (if (> tmp 0)
5569                                  tmp
5570                                1))
5571                            (cdr (gnus-active group)))))
5572               nil)
5573              (t
5574               nil))))
5575
5576     (gnus-summary-setup-default-charset)
5577
5578     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5579     (when (gnus-virtual-group-p group)
5580       (setq cached gnus-newsgroup-cached))
5581
5582     (setq gnus-newsgroup-unreads
5583           (gnus-sorted-ndifference
5584            (gnus-sorted-ndifference gnus-newsgroup-unreads
5585                                     gnus-newsgroup-marked)
5586            gnus-newsgroup-dormant))
5587
5588     (setq gnus-newsgroup-processable nil)
5589
5590     (gnus-update-read-articles group gnus-newsgroup-unreads)
5591
5592     ;; Adjust and set lists of article marks.
5593     (when info
5594       (gnus-adjust-marked-articles info))
5595     (if (setq articles select-articles)
5596         (setq gnus-newsgroup-unselected
5597               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5598       (setq articles (gnus-articles-to-read group read-all)))
5599
5600     (cond
5601      ((null articles)
5602       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5603       'quit)
5604      ((eq articles 0) nil)
5605      (t
5606       ;; Init the dependencies hash table.
5607       (setq gnus-newsgroup-dependencies
5608             (gnus-make-hashtable (length articles)))
5609       (gnus-set-global-variables)
5610       ;; Retrieve the headers and read them in.
5611
5612       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5613
5614       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5615       (when cached
5616         (setq gnus-newsgroup-cached cached))
5617
5618       ;; Suppress duplicates?
5619       (when gnus-suppress-duplicates
5620         (gnus-dup-suppress-articles))
5621
5622       ;; Set the initial limit.
5623       (setq gnus-newsgroup-limit (copy-sequence articles))
5624       ;; Remove canceled articles from the list of unread articles.
5625       (setq fetched-articles
5626             (mapcar (lambda (headers) (mail-header-number headers))
5627                     gnus-newsgroup-headers))
5628       (setq gnus-newsgroup-articles fetched-articles)
5629       (setq gnus-newsgroup-unreads
5630             (gnus-sorted-nintersection
5631              gnus-newsgroup-unreads fetched-articles))
5632       (gnus-compute-unseen-list)
5633
5634       ;; Removed marked articles that do not exist.
5635       (gnus-update-missing-marks
5636        (gnus-sorted-difference articles fetched-articles))
5637       ;; We might want to build some more threads first.
5638       (when (and gnus-fetch-old-headers
5639                  (eq gnus-headers-retrieved-by 'nov))
5640         (if (eq gnus-fetch-old-headers 'invisible)
5641             (gnus-build-all-threads)
5642           (gnus-build-old-threads)))
5643       ;; Let the Gnus agent mark articles as read.
5644       (when gnus-agent
5645         (gnus-agent-get-undownloaded-list))
5646       ;; Remove list identifiers from subject
5647       (when gnus-list-identifiers
5648         (gnus-summary-remove-list-identifiers))
5649       ;; Check whether auto-expire is to be done in this group.
5650       (setq gnus-newsgroup-auto-expire
5651             (gnus-group-auto-expirable-p group))
5652       ;; Set up the article buffer now, if necessary.
5653       (unless (and gnus-single-article-buffer
5654                    (equal gnus-article-buffer "*Article*"))
5655         (gnus-article-setup-buffer))
5656       ;; First and last article in this newsgroup.
5657       (when gnus-newsgroup-headers
5658         (setq gnus-newsgroup-begin
5659               (mail-header-number (car gnus-newsgroup-headers))
5660               gnus-newsgroup-end
5661               (mail-header-number
5662                (gnus-last-element gnus-newsgroup-headers))))
5663       ;; GROUP is successfully selected.
5664       (or gnus-newsgroup-headers t)))))
5665
5666 (defun gnus-compute-unseen-list ()
5667   ;; The `seen' marks are treated specially.
5668   (if (not gnus-newsgroup-seen)
5669       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5670     (setq gnus-newsgroup-unseen
5671           (gnus-inverse-list-range-intersection
5672            gnus-newsgroup-articles gnus-newsgroup-seen))))
5673
5674 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5675
5676 (defun gnus-summary-display-make-predicate (display)
5677   (require 'gnus-agent)
5678   (when (= (length display) 1)
5679     (setq display (car display)))
5680   (unless gnus-summary-display-cache
5681     (dolist (elem (append '((unread . unread)
5682                             (read . read)
5683                             (unseen . unseen))
5684                           gnus-article-mark-lists))
5685       (push (cons (cdr elem)
5686                   (gnus-byte-compile    ;Why bother?
5687                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5688             gnus-summary-display-cache)))
5689   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5690         (gnus-category-predicate-cache gnus-summary-display-cache))
5691     (gnus-get-predicate display)))
5692
5693 ;; Uses the dynamically bound `gnus-number' variable.
5694 (defvar gnus-number)
5695 (defun gnus-article-marked-p (type &optional article)
5696   (let ((article (or article gnus-number)))
5697     (cond
5698      ((eq type 'tick)
5699       (memq article gnus-newsgroup-marked))
5700      ((eq type 'spam)
5701       (memq article gnus-newsgroup-spam-marked))
5702      ((eq type 'unsend)
5703       (memq article gnus-newsgroup-unsendable))
5704      ((eq type 'undownload)
5705       (memq article gnus-newsgroup-undownloaded))
5706      ((eq type 'download)
5707       (memq article gnus-newsgroup-downloadable))
5708      ((eq type 'unread)
5709       (memq article gnus-newsgroup-unreads))
5710      ((eq type 'read)
5711       (memq article gnus-newsgroup-reads))
5712      ((eq type 'dormant)
5713       (memq article gnus-newsgroup-dormant) )
5714      ((eq type 'expire)
5715       (memq article gnus-newsgroup-expirable))
5716      ((eq type 'reply)
5717       (memq article gnus-newsgroup-replied))
5718      ((eq type 'killed)
5719       (memq article gnus-newsgroup-killed))
5720      ((eq type 'bookmark)
5721       (assq article gnus-newsgroup-bookmarks))
5722      ((eq type 'score)
5723       (assq article gnus-newsgroup-scored))
5724      ((eq type 'save)
5725       (memq article gnus-newsgroup-saved))
5726      ((eq type 'cache)
5727       (memq article gnus-newsgroup-cached))
5728      ((eq type 'forward)
5729       (memq article gnus-newsgroup-forwarded))
5730      ((eq type 'seen)
5731       (not (memq article gnus-newsgroup-unseen)))
5732      ((eq type 'recent)
5733       (memq article gnus-newsgroup-recent))
5734      (t t))))
5735
5736 (defun gnus-articles-to-read (group &optional read-all)
5737   "Find out what articles the user wants to read."
5738   (let* ((articles
5739           ;; Select all articles if `read-all' is non-nil, or if there
5740           ;; are no unread articles.
5741           (if (or read-all
5742                   (and (zerop (length gnus-newsgroup-marked))
5743                        (zerop (length gnus-newsgroup-unreads)))
5744                   ;; Fetch all if the predicate is non-nil.
5745                   gnus-newsgroup-display)
5746               ;; We want to select the headers for all the articles in
5747               ;; the group, so we select either all the active
5748               ;; articles in the group, or (if that's nil), the
5749               ;; articles in the cache.
5750               (or
5751                (if gnus-newsgroup-maximum-articles
5752                    (let ((active (gnus-active group)))
5753                      (gnus-uncompress-range
5754                       (cons (max (car active)
5755                                  (- (cdr active)
5756                                     gnus-newsgroup-maximum-articles
5757                                     -1))
5758                             (cdr active))))
5759                  (gnus-uncompress-range (gnus-active group)))
5760                (gnus-cache-articles-in-group group))
5761             ;; Select only the "normal" subset of articles.
5762             (gnus-sorted-nunion
5763              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5764              gnus-newsgroup-unreads)))
5765          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5766          (scored (length scored-list))
5767          (number (length articles))
5768          (marked (+ (length gnus-newsgroup-marked)
5769                     (length gnus-newsgroup-dormant)))
5770          (select
5771           (cond
5772            ((numberp read-all)
5773             read-all)
5774            ((numberp gnus-newsgroup-display)
5775             gnus-newsgroup-display)
5776            (t
5777             (condition-case ()
5778                 (cond
5779                  ((and (or (<= scored marked) (= scored number))
5780                        (numberp gnus-large-newsgroup)
5781                        (> number gnus-large-newsgroup))
5782                   (let* ((cursor-in-echo-area nil)
5783                          (initial (gnus-parameter-large-newsgroup-initial
5784                                    gnus-newsgroup-name))
5785                          (input
5786                           (read-string
5787                            (format
5788                             "How many articles from %s (%s %d): "
5789                             (gnus-group-decoded-name gnus-newsgroup-name)
5790                             (if initial "max" "default")
5791                             number)
5792                            (if initial
5793                                (cons (number-to-string initial)
5794                                      0)))))
5795                     (if (string-match "^[ \t]*$" input) number input)))
5796                  ((and (> scored marked) (< scored number)
5797                        (> (- scored number) 20))
5798                   (let ((input
5799                          (read-string
5800                           (format "%s %s (%d scored, %d total): "
5801                                   "How many articles from"
5802                                   (gnus-group-decoded-name group)
5803                                   scored number))))
5804                     (if (string-match "^[ \t]*$" input)
5805                         number input)))
5806                  (t number))
5807               (quit
5808                (message "Quit getting the articles to read")
5809                nil))))))
5810     (setq select (if (stringp select) (string-to-number select) select))
5811     (if (or (null select) (zerop select))
5812         select
5813       (if (and (not (zerop scored)) (<= (abs select) scored))
5814           (progn
5815             (setq articles (sort scored-list '<))
5816             (setq number (length articles)))
5817         (setq articles (copy-sequence articles)))
5818
5819       (when (< (abs select) number)
5820         (if (< select 0)
5821             ;; Select the N oldest articles.
5822             (setcdr (nthcdr (1- (abs select)) articles) nil)
5823           ;; Select the N most recent articles.
5824           (setq articles (nthcdr (- number select) articles))))
5825       (setq gnus-newsgroup-unselected
5826             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5827       (when gnus-alter-articles-to-read-function
5828         (setq articles
5829               (sort
5830                (funcall gnus-alter-articles-to-read-function
5831                         gnus-newsgroup-name articles)
5832                '<)))
5833       articles)))
5834
5835 (defun gnus-killed-articles (killed articles)
5836   (let (out)
5837     (while articles
5838       (when (inline (gnus-member-of-range (car articles) killed))
5839         (push (car articles) out))
5840       (setq articles (cdr articles)))
5841     out))
5842
5843 (defun gnus-uncompress-marks (marks)
5844   "Uncompress the mark ranges in MARKS."
5845   (let ((uncompressed '(score bookmark))
5846         out)
5847     (while marks
5848       (if (memq (caar marks) uncompressed)
5849           (push (car marks) out)
5850         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5851       (setq marks (cdr marks)))
5852     out))
5853
5854 (defun gnus-article-mark-to-type (mark)
5855   "Return the type of MARK."
5856   (or (cadr (assq mark gnus-article-special-mark-lists))
5857       'list))
5858
5859 (defun gnus-article-unpropagatable-p (mark)
5860   "Return whether MARK should be propagated to back end."
5861   (memq mark gnus-article-unpropagated-mark-lists))
5862
5863 (defun gnus-adjust-marked-articles (info)
5864   "Set all article lists and remove all marks that are no longer valid."
5865   (let* ((marked-lists (gnus-info-marks info))
5866          (active (gnus-active (gnus-info-group info)))
5867          (min (car active))
5868          (max (cdr active))
5869          (types gnus-article-mark-lists)
5870          marks var articles article mark mark-type
5871          bgn end)
5872     ;; Hack to avoid adjusting marks for imap.
5873     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5874               'nnimap)
5875       (setq min 1))
5876
5877     (dolist (marks marked-lists)
5878       (setq mark (car marks)
5879             mark-type (gnus-article-mark-to-type mark)
5880             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5881
5882       ;; We set the variable according to the type of the marks list,
5883       ;; and then adjust the marks to a subset of the active articles.
5884       (cond
5885        ;; Adjust "simple" lists - compressed yet unsorted
5886        ((eq mark-type 'list)
5887         ;; Simultaneously uncompress and clip to active range
5888         ;; See gnus-uncompress-range for a description of possible marks
5889         (let (l lh)
5890           (if (not (cadr marks))
5891               (set var nil)
5892             (setq articles (if (numberp (cddr marks))
5893                                (list (cdr marks))
5894                              (cdr marks))
5895                   lh (cons nil nil)
5896                   l lh)
5897
5898             (while (setq article (pop articles))
5899               (cond ((consp article)
5900                      (setq bgn (max (car article) min)
5901                            end (min (cdr article) max))
5902                      (while (<= bgn end)
5903                        (setq l (setcdr l (cons bgn nil))
5904                              bgn (1+ bgn))))
5905                     ((and (<= min article)
5906                           (>= max article))
5907                      (setq l (setcdr l (cons article nil))))))
5908             (set var (cdr lh)))))
5909        ;; Adjust assocs.
5910        ((eq mark-type 'tuple)
5911         (set var (setq articles (cdr marks)))
5912         (when (not (listp (cdr (symbol-value var))))
5913           (set var (list (symbol-value var))))
5914         (when (not (listp (cdr articles)))
5915           (setq articles (list articles)))
5916         (while articles
5917           (when (or (not (consp (setq article (pop articles))))
5918                     (< (car article) min)
5919                     (> (car article) max))
5920             (set var (delq article (symbol-value var))))))
5921        ;; Adjust ranges (sloppily).
5922        ((eq mark-type 'range)
5923         (cond
5924          ((eq mark 'seen)
5925           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5926           ;; It should be (seen (NUM1 . NUM2)).
5927           (when (numberp (cddr marks))
5928             (setcdr marks (list (cdr marks))))
5929           (setq articles (cdr marks))
5930           (while (and articles
5931                       (or (and (consp (car articles))
5932                                (> min (cdar articles)))
5933                           (and (numberp (car articles))
5934                                (> min (car articles)))))
5935             (pop articles))
5936           (set var articles))))))))
5937
5938 (defun gnus-update-missing-marks (missing)
5939   "Go through the list of MISSING articles and remove them from the mark lists."
5940   (when missing
5941     (let (var m)
5942       ;; Go through all types.
5943       (dolist (elem gnus-article-mark-lists)
5944         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5945           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5946           (when (symbol-value var)
5947             ;; This list has articles.  So we delete all missing
5948             ;; articles from it.
5949             (setq m missing)
5950             (while m
5951               (set var (delq (pop m) (symbol-value var))))))))))
5952
5953 (defun gnus-update-marks ()
5954   "Enter the various lists of marked articles into the newsgroup info list."
5955   (let ((types gnus-article-mark-lists)
5956         (info (gnus-get-info gnus-newsgroup-name))
5957         type list newmarked symbol delta-marks)
5958     (when info
5959       ;; Add all marks lists to the list of marks lists.
5960       (while (setq type (pop types))
5961         (setq list (symbol-value
5962                     (setq symbol
5963                           (intern (format "gnus-newsgroup-%s" (car type))))))
5964
5965         (when list
5966           ;; Get rid of the entries of the articles that have the
5967           ;; default score.
5968           (when (and (eq (cdr type) 'score)
5969                      gnus-save-score
5970                      list)
5971             (let* ((arts list)
5972                    (prev (cons nil list))
5973                    (all prev))
5974               (while arts
5975                 (if (or (not (consp (car arts)))
5976                         (= (cdar arts) gnus-summary-default-score))
5977                     (setcdr prev (cdr arts))
5978                   (setq prev arts))
5979                 (setq arts (cdr arts)))
5980               (setq list (cdr all)))))
5981
5982         (when (eq (cdr type) 'seen)
5983           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5984
5985         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5986           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5987
5988         (when (and (gnus-check-backend-function
5989                     'request-set-mark gnus-newsgroup-name)
5990                    (not (gnus-article-unpropagatable-p (cdr type))))
5991           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5992                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5993                  (add (gnus-remove-from-range
5994                        (gnus-copy-sequence list) old)))
5995             (when add
5996               (push (list add 'add (list (cdr type))) delta-marks))
5997             (when del
5998               ;; Don't delete marks from outside the active range.  This
5999               ;; shouldn't happen, but is a sanity check.
6000               (setq del (gnus-sorted-range-intersection
6001                          (gnus-active gnus-newsgroup-name) del))
6002               (push (list del 'del (list (cdr type))) delta-marks))))
6003
6004         (when list
6005           (push (cons (cdr type) list) newmarked)))
6006
6007       (when delta-marks
6008         (unless (gnus-check-group gnus-newsgroup-name)
6009           (error "Can't open server for %s" gnus-newsgroup-name))
6010         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6011
6012       ;; Enter these new marks into the info of the group.
6013       (if (nthcdr 3 info)
6014           (setcar (nthcdr 3 info) newmarked)
6015         ;; Add the marks lists to the end of the info.
6016         (when newmarked
6017           (setcdr (nthcdr 2 info) (list newmarked))))
6018
6019       ;; Cut off the end of the info if there's nothing else there.
6020       (let ((i 5))
6021         (while (and (> i 2)
6022                     (not (nth i info)))
6023           (when (nthcdr (decf i) info)
6024             (setcdr (nthcdr i info) nil)))))))
6025
6026 (defun gnus-set-mode-line (where)
6027   "Set the mode line of the article or summary buffers.
6028 If WHERE is `summary', the summary mode line format will be used."
6029   ;; Is this mode line one we keep updated?
6030   (when (and (memq where gnus-updated-mode-lines)
6031              (symbol-value
6032               (intern (format "gnus-%s-mode-line-format-spec" where))))
6033     (let (mode-string)
6034       ;; We evaluate this in the summary buffer since these
6035       ;; variables are buffer-local to that buffer.
6036       (with-current-buffer gnus-summary-buffer
6037         ;; We bind all these variables that are used in the `eval' form
6038         ;; below.
6039         (let* ((mformat (symbol-value
6040                          (intern
6041                           (format "gnus-%s-mode-line-format-spec" where))))
6042                (gnus-tmp-group-name (gnus-mode-string-quote
6043                                      (gnus-group-decoded-name
6044                                       gnus-newsgroup-name)))
6045                (gnus-tmp-article-number (or gnus-current-article 0))
6046                (gnus-tmp-unread gnus-newsgroup-unreads)
6047                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6048                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6049                (gnus-tmp-unread-and-unselected
6050                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6051                             (zerop gnus-tmp-unselected))
6052                        "")
6053                       ((zerop gnus-tmp-unselected)
6054                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6055                       (t (format "{%d(+%d) more}"
6056                                  gnus-tmp-unread-and-unticked
6057                                  gnus-tmp-unselected))))
6058                (gnus-tmp-subject
6059                 (if (and gnus-current-headers
6060                          (vectorp gnus-current-headers))
6061                     (gnus-mode-string-quote
6062                      (mail-header-subject gnus-current-headers))
6063                   ""))
6064                bufname-length max-len
6065                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6066           (setq mode-string (eval mformat))
6067           (setq bufname-length (if (string-match "%b" mode-string)
6068                                    (- (length
6069                                        (buffer-name
6070                                         (if (eq where 'summary)
6071                                             nil
6072                                           (get-buffer gnus-article-buffer))))
6073                                       2)
6074                                  0))
6075           (setq max-len (max 4 (if gnus-mode-non-string-length
6076                                    (- (window-width)
6077                                       gnus-mode-non-string-length
6078                                       bufname-length)
6079                                  (length mode-string))))
6080           ;; We might have to chop a bit of the string off...
6081           (when (> (length mode-string) max-len)
6082             (setq mode-string
6083                   (concat (truncate-string-to-width mode-string (- max-len 3))
6084                           "...")))))
6085       ;; Update the mode line.
6086       (setq mode-line-buffer-identification
6087             (gnus-mode-line-buffer-identification (list mode-string)))
6088       (set-buffer-modified-p t))))
6089
6090 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6091   "Go through the HEADERS list and add all Xrefs to a hash table.
6092 The resulting hash table is returned, or nil if no Xrefs were found."
6093   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6094          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6095          (xref-hashtb (gnus-make-hashtable))
6096          start group entry number xrefs header)
6097     (while headers
6098       (setq header (pop headers))
6099       (when (and (setq xrefs (mail-header-xref header))
6100                  (not (memq (setq number (mail-header-number header))
6101                             unreads)))
6102         (setq start 0)
6103         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6104           (setq start (match-end 0))
6105           (setq group (if prefix
6106                           (concat prefix (substring xrefs (match-beginning 1)
6107                                                     (match-end 1)))
6108                         (substring xrefs (match-beginning 1) (match-end 1))))
6109           (setq number
6110                 (string-to-number (substring xrefs (match-beginning 2)
6111                                           (match-end 2))))
6112           (if (setq entry (gnus-gethash group xref-hashtb))
6113               (setcdr entry (cons number (cdr entry)))
6114             (gnus-sethash group (cons number nil) xref-hashtb)))))
6115     (and start xref-hashtb)))
6116
6117 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6118   "Look through all the headers and mark the Xrefs as read."
6119   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6120         name info xref-hashtb idlist method nth4)
6121     (with-current-buffer gnus-group-buffer
6122       (when (setq xref-hashtb
6123                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6124         (mapatoms
6125          (lambda (group)
6126            (unless (string= from-newsgroup (setq name (symbol-name group)))
6127              (setq idlist (symbol-value group))
6128              ;; Dead groups are not updated.
6129              (and (prog1
6130                       (setq info (gnus-get-info name))
6131                     (when (stringp (setq nth4 (gnus-info-method info)))
6132                       (setq nth4 (gnus-server-to-method nth4))))
6133                   ;; Only do the xrefs if the group has the same
6134                   ;; select method as the group we have just read.
6135                   (or (gnus-methods-equal-p
6136                        nth4 (gnus-find-method-for-group from-newsgroup))
6137                       virtual
6138                       (equal nth4 (setq method (gnus-find-method-for-group
6139                                                 from-newsgroup)))
6140                       (and (equal (car nth4) (car method))
6141                            (equal (nth 1 nth4) (nth 1 method))))
6142                   gnus-use-cross-reference
6143                   (or (not (eq gnus-use-cross-reference t))
6144                       virtual
6145                       ;; Only do cross-references on subscribed
6146                       ;; groups, if that is what is wanted.
6147                       (<= (gnus-info-level info) gnus-level-subscribed))
6148                   (gnus-group-make-articles-read name idlist))))
6149          xref-hashtb)))))
6150
6151 (defun gnus-compute-read-articles (group articles)
6152   (let* ((entry (gnus-group-entry group))
6153          (info (nth 2 entry))
6154          (active (gnus-active group))
6155          ninfo)
6156     (when entry
6157       ;; First peel off all invalid article numbers.
6158       (when active
6159         (let ((ids articles)
6160               id first)
6161           (while (setq id (pop ids))
6162             (when (and first (> id (cdr active)))
6163               ;; We'll end up in this situation in one particular
6164               ;; obscure situation.  If you re-scan a group and get
6165               ;; a new article that is cross-posted to a different
6166               ;; group that has not been re-scanned, you might get
6167               ;; crossposted article that has a higher number than
6168               ;; Gnus believes possible.  So we re-activate this
6169               ;; group as well.  This might mean doing the
6170               ;; crossposting thingy will *increase* the number
6171               ;; of articles in some groups.  Tsk, tsk.
6172               (setq active (or (gnus-activate-group group) active)))
6173             (when (or (> id (cdr active))
6174                       (< id (car active)))
6175               (setq articles (delq id articles))))))
6176       ;; If the read list is nil, we init it.
6177       (if (and active
6178                (null (gnus-info-read info))
6179                (> (car active) 1))
6180           (setq ninfo (cons 1 (1- (car active))))
6181         (setq ninfo (gnus-info-read info)))
6182       ;; Then we add the read articles to the range.
6183       (gnus-add-to-range
6184        ninfo (setq articles (sort articles '<))))))
6185
6186 (defun gnus-group-make-articles-read (group articles)
6187   "Update the info of GROUP to say that ARTICLES are read."
6188   (let* ((num 0)
6189          (entry (gnus-group-entry group))
6190          (info (nth 2 entry))
6191          (active (gnus-active group))
6192          range)
6193     (if (not entry)
6194         ;; Group that Gnus doesn't know exists, but still allow the
6195         ;; backend to set marks.
6196         (gnus-request-set-mark
6197          group (list (list (gnus-compress-sequence (sort articles #'<))
6198                            'add '(read))))
6199       ;; Normal, subscribed groups.
6200       (setq range (gnus-compute-read-articles group articles))
6201       (with-current-buffer gnus-group-buffer
6202         (gnus-undo-register
6203           `(progn
6204              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6205              (gnus-info-set-read ',info ',(gnus-info-read info))
6206              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6207              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6208              (gnus-group-update-group ,group t))))
6209       ;; Add the read articles to the range.
6210       (gnus-info-set-read info range)
6211       (gnus-request-set-mark group (list (list range 'add '(read))))
6212       ;; Then we have to re-compute how many unread
6213       ;; articles there are in this group.
6214       (when active
6215         (cond
6216          ((not range)
6217           (setq num (- (1+ (cdr active)) (car active))))
6218          ((not (listp (cdr range)))
6219           (setq num (- (cdr active) (- (1+ (cdr range))
6220                                        (car range)))))
6221          (t
6222           (while range
6223             (if (numberp (car range))
6224                 (setq num (1+ num))
6225               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6226             (setq range (cdr range)))
6227           (setq num (- (cdr active) num))))
6228         ;; Update the number of unread articles.
6229         (setcar entry num)
6230         ;; Update the group buffer.
6231         (unless (gnus-ephemeral-group-p group)
6232           (gnus-group-update-group group t))))))
6233
6234 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6235   (let ((cur nntp-server-buffer)
6236         (dependencies
6237          (or dependencies
6238              (with-current-buffer gnus-summary-buffer
6239                gnus-newsgroup-dependencies)))
6240         headers id end ref number
6241         (mail-parse-charset gnus-newsgroup-charset)
6242         (mail-parse-ignored-charsets
6243          (save-current-buffer (condition-case nil
6244                                   (set-buffer gnus-summary-buffer)
6245                                 (error))
6246                               gnus-newsgroup-ignored-charsets)))
6247     (with-current-buffer nntp-server-buffer
6248       ;; Translate all TAB characters into SPACE characters.
6249       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6250       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6251       (ietf-drums-unfold-fws)
6252       (gnus-run-hooks 'gnus-parse-headers-hook)
6253       (let ((case-fold-search t)
6254             in-reply-to header p lines chars)
6255         (goto-char (point-min))
6256         ;; Search to the beginning of the next header.  Error messages
6257         ;; do not begin with 2 or 3.
6258         (while (re-search-forward "^[23][0-9]+ " nil t)
6259           (setq id nil
6260                 ref nil)
6261           ;; This implementation of this function, with nine
6262           ;; search-forwards instead of the one re-search-forward and
6263           ;; a case (which basically was the old function) is actually
6264           ;; about twice as fast, even though it looks messier.  You
6265           ;; can't have everything, I guess.  Speed and elegance
6266           ;; doesn't always go hand in hand.
6267           (setq
6268            header
6269            (vector
6270             ;; Number.
6271             (prog1
6272                 (setq number (read cur))
6273               (end-of-line)
6274               (setq p (point))
6275               (narrow-to-region (point)
6276                                 (or (and (search-forward "\n.\n" nil t)
6277                                          (- (point) 2))
6278                                     (point))))
6279             ;; Subject.
6280             (progn
6281               (goto-char p)
6282               (if (search-forward "\nsubject:" nil t)
6283                   (funcall gnus-decode-encoded-word-function
6284                            (nnheader-header-value))
6285                 "(none)"))
6286             ;; From.
6287             (progn
6288               (goto-char p)
6289               (if (search-forward "\nfrom:" nil t)
6290                   (funcall gnus-decode-encoded-address-function
6291                            (nnheader-header-value))
6292                 "(nobody)"))
6293             ;; Date.
6294             (progn
6295               (goto-char p)
6296               (if (search-forward "\ndate:" nil t)
6297                   (nnheader-header-value) ""))
6298             ;; Message-ID.
6299             (progn
6300               (goto-char p)
6301               (setq id (if (re-search-forward
6302                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6303                            ;; We do it this way to make sure the Message-ID
6304                            ;; is (somewhat) syntactically valid.
6305                            (buffer-substring (match-beginning 1)
6306                                              (match-end 1))
6307                          ;; If there was no message-id, we just fake one
6308                          ;; to make subsequent routines simpler.
6309                          (nnheader-generate-fake-message-id number))))
6310             ;; References.
6311             (progn
6312               (goto-char p)
6313               (if (search-forward "\nreferences:" nil t)
6314                   (progn
6315                     (setq end (point))
6316                     (prog1
6317                         (nnheader-header-value)
6318                       (setq ref
6319                             (buffer-substring
6320                              (progn
6321                                (end-of-line)
6322                                (search-backward ">" end t)
6323                                (1+ (point)))
6324                              (progn
6325                                (search-backward "<" end t)
6326                                (point))))))
6327                 ;; Get the references from the in-reply-to header if there
6328                 ;; were no references and the in-reply-to header looks
6329                 ;; promising.
6330                 (if (and (search-forward "\nin-reply-to:" nil t)
6331                          (setq in-reply-to (nnheader-header-value))
6332                          (string-match "<[^>]+>" in-reply-to))
6333                     (let (ref2)
6334                       (setq ref (substring in-reply-to (match-beginning 0)
6335                                            (match-end 0)))
6336                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6337                         (setq ref2 (substring in-reply-to (match-beginning 0)
6338                                               (match-end 0)))
6339                         (when (> (length ref2) (length ref))
6340                           (setq ref ref2)))
6341                       ref)
6342                   (setq ref nil))))
6343             ;; Chars.
6344             (progn
6345               (goto-char p)
6346               (if (search-forward "\nchars: " nil t)
6347                   (if (numberp (setq chars (ignore-errors (read cur))))
6348                       chars -1)
6349                 -1))
6350             ;; Lines.
6351             (progn
6352               (goto-char p)
6353               (if (search-forward "\nlines: " nil t)
6354                   (if (numberp (setq lines (ignore-errors (read cur))))
6355                       lines -1)
6356                 -1))
6357             ;; Xref.
6358             (progn
6359               (goto-char p)
6360               (and (search-forward "\nxref:" nil t)
6361                    (nnheader-header-value)))
6362             ;; Extra.
6363             (when gnus-extra-headers
6364               (let ((extra gnus-extra-headers)
6365                     out)
6366                 (while extra
6367                   (goto-char p)
6368                   (when (search-forward
6369                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6370                     (push (cons (car extra) (nnheader-header-value))
6371                           out))
6372                   (pop extra))
6373                 out))))
6374           (when (equal id ref)
6375             (setq ref nil))
6376
6377           (when gnus-alter-header-function
6378             (funcall gnus-alter-header-function header)
6379             (setq id (mail-header-id header)
6380                   ref (gnus-parent-id (mail-header-references header))))
6381
6382           (when (setq header
6383                       (gnus-dependencies-add-header
6384                        header dependencies force-new))
6385             (push header headers))
6386           (goto-char (point-max))
6387           (widen))
6388         (nreverse headers)))))
6389
6390 ;; Goes through the xover lines and returns a list of vectors
6391 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6392                                                   force-new dependencies
6393                                                   group also-fetch-heads)
6394   "Parse the news overview data in the server buffer.
6395 Return a list of headers that match SEQUENCE (see
6396 `nntp-retrieve-headers')."
6397   ;; Get the Xref when the users reads the articles since most/some
6398   ;; NNTP servers do not include Xrefs when using XOVER.
6399   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6400   (let ((mail-parse-charset gnus-newsgroup-charset)
6401         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6402         (cur nntp-server-buffer)
6403         (dependencies (or dependencies gnus-newsgroup-dependencies))
6404         (allp (cond
6405                ((eq gnus-read-all-available-headers t)
6406                 t)
6407                ((and (stringp gnus-read-all-available-headers)
6408                      group)
6409                 (string-match gnus-read-all-available-headers group))
6410                (t
6411                 nil)))
6412         number headers header)
6413     (with-current-buffer nntp-server-buffer
6414       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6415       ;; Allow the user to mangle the headers before parsing them.
6416       (gnus-run-hooks 'gnus-parse-headers-hook)
6417       (goto-char (point-min))
6418       (gnus-parse-without-error
6419         (while (and (or sequence allp)
6420                     (not (eobp)))
6421           (setq number (read cur))
6422           (when (not allp)
6423             (while (and sequence
6424                         (< (car sequence) number))
6425               (setq sequence (cdr sequence))))
6426           (when (and (or allp
6427                          (and sequence
6428                               (eq number (car sequence))))
6429                      (progn
6430                        (setq sequence (cdr sequence))
6431                        (setq header (inline
6432                                       (gnus-nov-parse-line
6433                                        number dependencies force-new)))))
6434             (push header headers))
6435           (forward-line 1)))
6436       ;; A common bug in inn is that if you have posted an article and
6437       ;; then retrieves the active file, it will answer correctly --
6438       ;; the new article is included.  However, a NOV entry for the
6439       ;; article may not have been generated yet, so this may fail.
6440       ;; We work around this problem by retrieving the last few
6441       ;; headers using HEAD.
6442       (if (or (not also-fetch-heads)
6443               (not sequence))
6444           ;; We (probably) got all the headers.
6445           (nreverse headers)
6446         (let ((gnus-nov-is-evil t))
6447           (nconc
6448            (nreverse headers)
6449            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6450              (gnus-get-newsgroup-headers))))))))
6451
6452 (defun gnus-article-get-xrefs ()
6453   "Fill in the Xref value in `gnus-current-headers', if necessary.
6454 This is meant to be called in `gnus-article-internal-prepare-hook'."
6455   (let ((headers (with-current-buffer gnus-summary-buffer
6456                    gnus-current-headers)))
6457     (or (not gnus-use-cross-reference)
6458         (not headers)
6459         (and (mail-header-xref headers)
6460              (not (string= (mail-header-xref headers) "")))
6461         (let ((case-fold-search t)
6462               xref)
6463           (save-restriction
6464             (nnheader-narrow-to-headers)
6465             (goto-char (point-min))
6466             (when (or (and (not (eobp))
6467                            (eq (downcase (char-after)) ?x)
6468                            (looking-at "Xref:"))
6469                       (search-forward "\nXref:" nil t))
6470               (goto-char (1+ (match-end 0)))
6471               (setq xref (buffer-substring (point) (point-at-eol)))
6472               (mail-header-set-xref headers xref)))))))
6473
6474 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6475   "Find article ID and insert the summary line for that article.
6476 OLD-HEADER can either be a header or a line number to insert
6477 the subject line on."
6478   (let* ((line (and (numberp old-header) old-header))
6479          (old-header (and (vectorp old-header) old-header))
6480          (header (cond ((and old-header use-old-header)
6481                         old-header)
6482                        ((and (numberp id)
6483                              (gnus-number-to-header id))
6484                         (gnus-number-to-header id))
6485                        (t
6486                         (gnus-read-header id))))
6487          (number (and (numberp id) id))
6488          d)
6489     (when header
6490       ;; Rebuild the thread that this article is part of and go to the
6491       ;; article we have fetched.
6492       (when (and (not gnus-show-threads)
6493                  old-header)
6494         (when (and number
6495                    (setq d (gnus-data-find (mail-header-number old-header))))
6496           (goto-char (gnus-data-pos d))
6497           (gnus-data-remove
6498            number
6499            (- (point-at-bol)
6500               (prog1
6501                   (1+ (point-at-eol))
6502                 (gnus-delete-line))))))
6503       ;; Remove list identifiers from subject.
6504       (when gnus-list-identifiers
6505         (let ((gnus-newsgroup-headers (list header)))
6506           (gnus-summary-remove-list-identifiers)))
6507       (when old-header
6508         (mail-header-set-number header (mail-header-number old-header)))
6509       (setq gnus-newsgroup-sparse
6510             (delq (setq number (mail-header-number header))
6511                   gnus-newsgroup-sparse))
6512       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6513       (push number gnus-newsgroup-limit)
6514       (gnus-rebuild-thread (mail-header-id header) line)
6515       (gnus-summary-goto-subject number nil t))
6516     (when (and (numberp number)
6517                (> number 0))
6518       ;; We have to update the boundaries even if we can't fetch the
6519       ;; article if ID is a number -- so that the next `P' or `N'
6520       ;; command will fetch the previous (or next) article even
6521       ;; if the one we tried to fetch this time has been canceled.
6522       (when (> number gnus-newsgroup-end)
6523         (setq gnus-newsgroup-end number))
6524       (when (< number gnus-newsgroup-begin)
6525         (setq gnus-newsgroup-begin number))
6526       (setq gnus-newsgroup-unselected
6527             (delq number gnus-newsgroup-unselected)))
6528     ;; Report back a success?
6529     (and header (mail-header-number header))))
6530
6531 ;;; Process/prefix in the summary buffer
6532
6533 (defun gnus-summary-work-articles (n)
6534   "Return a list of articles to be worked upon.
6535 The prefix argument, the list of process marked articles, and the
6536 current article will be taken into consideration."
6537   (with-current-buffer gnus-summary-buffer
6538     (cond
6539      (n
6540       ;; A numerical prefix has been given.
6541       (setq n (prefix-numeric-value n))
6542       (let ((backward (< n 0))
6543             (n (abs (prefix-numeric-value n)))
6544             articles article)
6545         (save-excursion
6546           (while
6547               (and (> n 0)
6548                    (push (setq article (gnus-summary-article-number))
6549                          articles)
6550                    (if backward
6551                        (gnus-summary-find-prev nil article)
6552                      (gnus-summary-find-next nil article)))
6553             (decf n)))
6554         (nreverse articles)))
6555      ((and (gnus-region-active-p) (mark))
6556       (message "region active")
6557       ;; Work on the region between point and mark.
6558       (let ((max (max (point) (mark)))
6559             articles article)
6560         (save-excursion
6561           (goto-char (min (point) (mark)))
6562           (while
6563               (and
6564                (push (setq article (gnus-summary-article-number)) articles)
6565                (gnus-summary-find-next nil article)
6566                (< (point) max)))
6567           (nreverse articles))))
6568      (gnus-newsgroup-processable
6569       ;; There are process-marked articles present.
6570       ;; Save current state.
6571       (gnus-summary-save-process-mark)
6572       ;; Return the list.
6573       (reverse gnus-newsgroup-processable))
6574      (t
6575       ;; Just return the current article.
6576       (list (gnus-summary-article-number))))))
6577
6578 (defmacro gnus-summary-iterate (arg &rest forms)
6579   "Iterate over the process/prefixed articles and do FORMS.
6580 ARG is the interactive prefix given to the command.  FORMS will be
6581 executed with point over the summary line of the articles."
6582   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6583     `(let ((,articles (gnus-summary-work-articles ,arg)))
6584        (while ,articles
6585          (gnus-summary-goto-subject (car ,articles))
6586          ,@forms
6587          (pop ,articles)))))
6588
6589 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6590 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6591
6592 (defun gnus-summary-save-process-mark ()
6593   "Push the current set of process marked articles on the stack."
6594   (interactive)
6595   (push (copy-sequence gnus-newsgroup-processable)
6596         gnus-newsgroup-process-stack))
6597
6598 (defun gnus-summary-kill-process-mark ()
6599   "Push the current set of process marked articles on the stack and unmark."
6600   (interactive)
6601   (gnus-summary-save-process-mark)
6602   (gnus-summary-unmark-all-processable))
6603
6604 (defun gnus-summary-yank-process-mark ()
6605   "Pop the last process mark state off the stack and restore it."
6606   (interactive)
6607   (unless gnus-newsgroup-process-stack
6608     (error "Empty mark stack"))
6609   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6610
6611 (defun gnus-summary-process-mark-set (set)
6612   "Make SET into the current process marked articles."
6613   (gnus-summary-unmark-all-processable)
6614   (mapc 'gnus-summary-set-process-mark set))
6615
6616 ;;; Searching and stuff
6617
6618 (defun gnus-summary-search-group (&optional backward use-level)
6619   "Search for next unread newsgroup.
6620 If optional argument BACKWARD is non-nil, search backward instead."
6621   (with-current-buffer gnus-group-buffer
6622     (when (gnus-group-search-forward
6623            backward nil (if use-level (gnus-group-group-level) nil))
6624       (gnus-group-group-name))))
6625
6626 (defun gnus-summary-best-group (&optional exclude-group)
6627   "Find the name of the best unread group.
6628 If EXCLUDE-GROUP, do not go to this group."
6629   (with-current-buffer gnus-group-buffer
6630     (save-excursion
6631       (gnus-group-best-unread-group exclude-group))))
6632
6633 (defun gnus-summary-find-next (&optional unread article backward)
6634   (if backward
6635       (gnus-summary-find-prev unread article)
6636     (let* ((dummy (gnus-summary-article-intangible-p))
6637            (article (or article (gnus-summary-article-number)))
6638            (data (gnus-data-find-list article))
6639            result)
6640       (when (and (not dummy)
6641                  (or (not gnus-summary-check-current)
6642                      (not unread)
6643                      (not (gnus-data-unread-p (car data)))))
6644         (setq data (cdr data)))
6645       (when (setq result
6646                   (if unread
6647                       (progn
6648                         (while data
6649                           (unless (memq (gnus-data-number (car data))
6650                                         (cond
6651                                          ((eq gnus-auto-goto-ignores
6652                                               'always-undownloaded)
6653                                           gnus-newsgroup-undownloaded)
6654                                          (gnus-plugged
6655                                           nil)
6656                                          ((eq gnus-auto-goto-ignores
6657                                               'unfetched)
6658                                           gnus-newsgroup-unfetched)
6659                                          ((eq gnus-auto-goto-ignores
6660                                               'undownloaded)
6661                                           gnus-newsgroup-undownloaded)))
6662                             (when (gnus-data-unread-p (car data))
6663                               (setq result (car data)
6664                                     data nil)))
6665                           (setq data (cdr data)))
6666                         result)
6667                     (car data)))
6668         (goto-char (gnus-data-pos result))
6669         (gnus-data-number result)))))
6670
6671 (defun gnus-summary-find-prev (&optional unread article)
6672   (let* ((eobp (eobp))
6673          (article (or article (gnus-summary-article-number)))
6674          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6675          result)
6676     (when (and (not eobp)
6677                (or (not gnus-summary-check-current)
6678                    (not unread)
6679                    (not (gnus-data-unread-p (car data)))))
6680       (setq data (cdr data)))
6681     (when (setq result
6682                 (if unread
6683                     (progn
6684                       (while data
6685                         (unless (memq (gnus-data-number (car data))
6686                                       (cond
6687                                        ((eq gnus-auto-goto-ignores
6688                                             'always-undownloaded)
6689                                         gnus-newsgroup-undownloaded)
6690                                        (gnus-plugged
6691                                         nil)
6692                                        ((eq gnus-auto-goto-ignores
6693                                             'unfetched)
6694                                         gnus-newsgroup-unfetched)
6695                                        ((eq gnus-auto-goto-ignores
6696                                             'undownloaded)
6697                                         gnus-newsgroup-undownloaded)))
6698                           (when (gnus-data-unread-p (car data))
6699                             (setq result (car data)
6700                                   data nil)))
6701                         (setq data (cdr data)))
6702                       result)
6703                   (car data)))
6704       (goto-char (gnus-data-pos result))
6705       (gnus-data-number result))))
6706
6707 (defun gnus-summary-find-subject (subject &optional unread backward article)
6708   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6709          (article (or article (gnus-summary-article-number)))
6710          (articles (gnus-data-list backward))
6711          (arts (gnus-data-find-list article articles))
6712          result)
6713     (when (or (not gnus-summary-check-current)
6714               (not unread)
6715               (not (gnus-data-unread-p (car arts))))
6716       (setq arts (cdr arts)))
6717     (while arts
6718       (and (or (not unread)
6719                (gnus-data-unread-p (car arts)))
6720            (vectorp (gnus-data-header (car arts)))
6721            (gnus-subject-equal
6722             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6723            (setq result (car arts)
6724                  arts nil))
6725       (setq arts (cdr arts)))
6726     (and result
6727          (goto-char (gnus-data-pos result))
6728          (gnus-data-number result))))
6729
6730 (defun gnus-summary-search-forward (&optional unread subject backward)
6731   "Search forward for an article.
6732 If UNREAD, look for unread articles.  If SUBJECT, look for
6733 articles with that subject.  If BACKWARD, search backward instead."
6734   (cond (subject (gnus-summary-find-subject subject unread backward))
6735         (backward (gnus-summary-find-prev unread))
6736         (t (gnus-summary-find-next unread))))
6737
6738 (defun gnus-recenter (&optional n)
6739   "Center point in window and redisplay frame.
6740 Also do horizontal recentering."
6741   (interactive "P")
6742   (when (and gnus-auto-center-summary
6743              (not (eq gnus-auto-center-summary 'vertical)))
6744     (gnus-horizontal-recenter))
6745   (if (fboundp 'recenter-top-bottom)
6746       (recenter-top-bottom n)
6747     (recenter n)))
6748
6749 (put 'gnus-recenter 'isearch-scroll t)
6750
6751 (defun gnus-forward-line-ignore-invisible (n)
6752   "Move N lines forward (backward if N is negative).
6753 Like forward-line, but skip over (and don't count) invisible lines."
6754   (let (done)
6755     (while (and (> n 0) (not done))
6756       ;; If the following character is currently invisible,
6757       ;; skip all characters with that same `invisible' property value.
6758       (while (gnus-invisible-p (point))
6759         (goto-char (gnus-next-char-property-change (point))))
6760       (forward-line 1)
6761       (if (eobp)
6762           (setq done t)
6763         (setq n (1- n))))
6764     (while (and (< n 0) (not done))
6765       (forward-line -1)
6766       (if (bobp) (setq done t)
6767         (setq n (1+ n))
6768         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6769           (goto-char (gnus-previous-char-property-change (point))))))))
6770
6771 (defun gnus-summary-recenter ()
6772   "Center point in the summary window.
6773 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6774 displayed, no centering will be performed."
6775   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6776   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6777   (interactive)
6778   ;; The user has to want it.
6779   (when gnus-auto-center-summary
6780     (let* ((top (cond ((< (window-height) 4) 0)
6781                       ((< (window-height) 7) 1)
6782                       (t (if (numberp gnus-auto-center-summary)
6783                              gnus-auto-center-summary
6784                            (/ (1- (window-height)) 2)))))
6785            (height (1- (window-height)))
6786            (bottom (save-excursion
6787                      (goto-char (point-max))
6788                      (gnus-forward-line-ignore-invisible (- height))
6789                      (point)))
6790            (window (get-buffer-window (current-buffer))))
6791       (when (get-buffer-window gnus-article-buffer)
6792         ;; Only do recentering when the article buffer is displayed,
6793         ;; Set the window start to either `bottom', which is the biggest
6794         ;; possible valid number, or the second line from the top,
6795         ;; whichever is the least.
6796         (let ((top-pos (save-excursion
6797                          (gnus-forward-line-ignore-invisible (- top))
6798                          (point))))
6799           (if (> bottom top-pos)
6800               ;; Keep the second line from the top visible
6801               (set-window-start window top-pos)
6802             ;; Try to keep the bottom line visible; if it's partially
6803             ;; obscured, either scroll one more line to make it fully
6804             ;; visible, or revert to using TOP-POS.
6805             (save-excursion
6806               (goto-char (point-max))
6807               (gnus-forward-line-ignore-invisible -1)
6808               (let ((last-line-start (point)))
6809                 (goto-char bottom)
6810                 (set-window-start window (point) t)
6811                 (when (not (pos-visible-in-window-p last-line-start window))
6812                   (gnus-forward-line-ignore-invisible 1)
6813                   (set-window-start window (min (point) top-pos) t)))))))
6814       ;; Do horizontal recentering while we're at it.
6815       (when (and (get-buffer-window (current-buffer) t)
6816                  (not (eq gnus-auto-center-summary 'vertical)))
6817         (let ((selected (selected-window)))
6818           (select-window (get-buffer-window (current-buffer) t))
6819           (gnus-summary-position-point)
6820           (gnus-horizontal-recenter)
6821           (select-window selected))))))
6822
6823 (defun gnus-summary-jump-to-group (newsgroup)
6824   "Move point to NEWSGROUP in group mode buffer."
6825   ;; Keep update point of group mode buffer if visible.
6826   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6827       (save-window-excursion
6828         ;; Take care of tree window mode.
6829         (when (get-buffer-window gnus-group-buffer)
6830           (pop-to-buffer gnus-group-buffer))
6831         (gnus-group-jump-to-group newsgroup))
6832     (save-excursion
6833       ;; Take care of tree window mode.
6834       (if (get-buffer-window gnus-group-buffer 0)
6835           (pop-to-buffer gnus-group-buffer)
6836         (set-buffer gnus-group-buffer))
6837       (gnus-group-jump-to-group newsgroup))))
6838
6839 ;; This function returns a list of article numbers based on the
6840 ;; difference between the ranges of read articles in this group and
6841 ;; the range of active articles.
6842 (defun gnus-list-of-unread-articles (group)
6843   (let* ((read (gnus-info-read (gnus-get-info group)))
6844          (active (or (gnus-active group) (gnus-activate-group group)))
6845          (last (or (cdr active)
6846                    (error "Group %s couldn't be activated " group)))
6847          (bottom (if gnus-newsgroup-maximum-articles
6848                      (max (car active)
6849                           (- last gnus-newsgroup-maximum-articles -1))
6850                    (car active)))
6851          first nlast unread)
6852     ;; If none are read, then all are unread.
6853     (if (not read)
6854         (setq first bottom)
6855       ;; If the range of read articles is a single range, then the
6856       ;; first unread article is the article after the last read
6857       ;; article.  Sounds logical, doesn't it?
6858       (if (and (not (listp (cdr read)))
6859                (or (< (car read) bottom)
6860                    (progn (setq read (list read))
6861                           nil)))
6862           (setq first (max bottom (1+ (cdr read))))
6863         ;; `read' is a list of ranges.
6864         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6865                                   (caar read)))
6866                   1)
6867           (setq first bottom))
6868         (while read
6869           (when first
6870             (while (< first nlast)
6871               (setq unread (cons first unread)
6872                     first (1+ first))))
6873           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6874           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6875           (setq read (cdr read)))))
6876     ;; And add the last unread articles.
6877     (while (<= first last)
6878       (setq unread (cons first unread)
6879             first (1+ first)))
6880     ;; Return the list of unread articles.
6881     (delq 0 (nreverse unread))))
6882
6883 (defun gnus-list-of-read-articles (group)
6884   "Return a list of unread, unticked and non-dormant articles."
6885   (let* ((info (gnus-get-info group))
6886          (marked (gnus-info-marks info))
6887          (active (gnus-active group)))
6888     (and info active
6889          (gnus-list-range-difference
6890           (gnus-list-range-difference
6891            (gnus-sorted-complement
6892             (gnus-uncompress-range
6893              (if gnus-newsgroup-maximum-articles
6894                  (cons (max (car active)
6895                             (- (cdr active)
6896                                gnus-newsgroup-maximum-articles
6897                                -1))
6898                        (cdr active))
6899                active))
6900             (gnus-list-of-unread-articles group))
6901            (cdr (assq 'dormant marked)))
6902           (cdr (assq 'tick marked))))))
6903
6904 ;; This function returns a sequence of article numbers based on the
6905 ;; difference between the ranges of read articles in this group and
6906 ;; the range of active articles.
6907 (defun gnus-sequence-of-unread-articles (group)
6908   (let* ((read (gnus-info-read (gnus-get-info group)))
6909          (active (or (gnus-active group) (gnus-activate-group group)))
6910          (last (cdr active))
6911          (bottom (if gnus-newsgroup-maximum-articles
6912                      (max (car active)
6913                           (- last gnus-newsgroup-maximum-articles -1))
6914                    (car active)))
6915          first nlast unread)
6916     ;; If none are read, then all are unread.
6917     (if (not read)
6918         (setq first bottom)
6919       ;; If the range of read articles is a single range, then the
6920       ;; first unread article is the article after the last read
6921       ;; article.  Sounds logical, doesn't it?
6922       (if (and (not (listp (cdr read)))
6923                (or (< (car read) bottom)
6924                    (progn (setq read (list read))
6925                           nil)))
6926           (setq first (max bottom (1+ (cdr read))))
6927         ;; `read' is a list of ranges.
6928         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6929                                   (caar read)))
6930                   1)
6931           (setq first bottom))
6932         (while read
6933           (when first
6934             (push (cons first nlast) unread))
6935           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6936           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6937           (setq read (cdr read)))))
6938     ;; And add the last unread articles.
6939     (cond ((not (and first last))
6940            nil)
6941           ((< first last)
6942            (push (cons first last) unread))
6943           ((= first last)
6944            (push first unread)))
6945     ;; Return the sequence of unread articles.
6946     (delq 0 (nreverse unread))))
6947
6948 ;; Various summary commands
6949
6950 (defun gnus-summary-select-article-buffer ()
6951   "Reconfigure windows to show the article buffer.
6952 If `gnus-widen-article-buffer' is set, show only the article
6953 buffer."
6954   (interactive)
6955   (if (not (gnus-buffer-live-p gnus-article-buffer))
6956       (error "There is no article buffer for this summary buffer")
6957     (unless (get-buffer-window gnus-article-buffer)
6958       (gnus-summary-show-article))
6959     (gnus-configure-windows
6960      (if gnus-widen-article-window
6961          'only-article
6962        'article)
6963      t)
6964     (select-window (get-buffer-window gnus-article-buffer))))
6965
6966 (defun gnus-summary-universal-argument (arg)
6967   "Perform any operation on all articles that are process/prefixed."
6968   (interactive "P")
6969   (let ((articles (gnus-summary-work-articles arg))
6970         func article)
6971     (if (eq
6972          (setq
6973           func
6974           (key-binding
6975            (read-key-sequence
6976             (substitute-command-keys
6977              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6978          'undefined)
6979         (gnus-error 1 "Undefined key")
6980       (save-excursion
6981         (while articles
6982           (gnus-summary-goto-subject (setq article (pop articles)))
6983           (let (gnus-newsgroup-processable)
6984             (command-execute func))
6985           (gnus-summary-remove-process-mark article)))))
6986   (gnus-summary-position-point))
6987
6988 (defun gnus-summary-toggle-truncation (&optional arg)
6989   "Toggle truncation of summary lines.
6990 With ARG, turn line truncation on if ARG is positive."
6991   (interactive "P")
6992   (setq truncate-lines
6993         (if (null arg) (not truncate-lines)
6994           (> (prefix-numeric-value arg) 0)))
6995   (redraw-display))
6996
6997 (defun gnus-summary-find-for-reselect ()
6998   "Return the number of an article to stay on across a reselect.
6999 The current article is considered, then following articles, then previous
7000 articles.  An article is sought which is not cancelled and isn't a temporary
7001 insertion from another group.  If there's no such then return a dummy 0."
7002   (let (found)
7003     (dolist (rev '(nil t))
7004       (unless found      ; don't demand the reverse list if we don't need it
7005         (let ((data (gnus-data-find-list
7006                      (gnus-summary-article-number) (gnus-data-list rev))))
7007           (while (and data (not found))
7008             (if (and (< 0 (gnus-data-number (car data)))
7009                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7010                 (setq found (gnus-data-number (car data))))
7011             (setq data (cdr data))))))
7012     (or found 0)))
7013
7014 (defun gnus-summary-reselect-current-group (&optional all rescan)
7015   "Exit and then reselect the current newsgroup.
7016 The prefix argument ALL means to select all articles."
7017   (interactive "P")
7018   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7019     (error "Ephemeral groups can't be reselected"))
7020   (let ((current-subject (gnus-summary-find-for-reselect))
7021         (group gnus-newsgroup-name))
7022     (setq gnus-newsgroup-begin nil)
7023     (gnus-summary-exit nil 'leave-hidden)
7024     ;; We have to adjust the point of group mode buffer because
7025     ;; point was moved to the next unread newsgroup by exiting.
7026     (gnus-summary-jump-to-group group)
7027     (when rescan
7028       (save-excursion
7029         (gnus-group-get-new-news-this-group 1)))
7030     (gnus-group-read-group all t)
7031     (gnus-summary-goto-subject current-subject nil t)))
7032
7033 (defun gnus-summary-rescan-group (&optional all)
7034   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7035   (interactive "P")
7036   (let ((config gnus-current-window-configuration))
7037     (gnus-summary-reselect-current-group all t)
7038     (gnus-configure-windows config)
7039     (when (eq config 'article)
7040       (gnus-summary-select-article))))
7041
7042 (defun gnus-summary-update-info (&optional non-destructive)
7043   (save-excursion
7044     (let ((group gnus-newsgroup-name))
7045       (when group
7046         (when gnus-newsgroup-kill-headers
7047           (setq gnus-newsgroup-killed
7048                 (gnus-compress-sequence
7049                  (gnus-sorted-union
7050                   (gnus-list-range-intersection
7051                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7052                   gnus-newsgroup-unreads)
7053                  t)))
7054         (unless (listp (cdr gnus-newsgroup-killed))
7055           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7056         (let ((headers gnus-newsgroup-headers))
7057           ;; Set the new ranges of read articles.
7058           (with-current-buffer gnus-group-buffer
7059             (gnus-undo-force-boundary))
7060           (gnus-update-read-articles
7061            group (gnus-sorted-union
7062                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7063           ;; Set the current article marks.
7064           (let ((gnus-newsgroup-scored
7065                  (if (and (not gnus-save-score)
7066                           (not non-destructive))
7067                      nil
7068                    gnus-newsgroup-scored)))
7069             (save-excursion
7070               (gnus-update-marks)))
7071           ;; Do the cross-ref thing.
7072           (when gnus-use-cross-reference
7073             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7074           ;; Do not switch windows but change the buffer to work.
7075           (set-buffer gnus-group-buffer)
7076           (unless (gnus-ephemeral-group-p group)
7077             (gnus-group-update-group group)))))))
7078
7079 (defun gnus-summary-save-newsrc (&optional force)
7080   "Save the current number of read/marked articles in the dribble buffer.
7081 The dribble buffer will then be saved.
7082 If FORCE (the prefix), also save the .newsrc file(s)."
7083   (interactive "P")
7084   (gnus-summary-update-info t)
7085   (if force
7086       (gnus-save-newsrc-file)
7087     (gnus-dribble-save)))
7088
7089 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7090
7091 (defun gnus-summary-exit (&optional temporary leave-hidden)
7092   "Exit reading current newsgroup, and then return to group selection mode.
7093 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7094   (interactive)
7095   (gnus-set-global-variables)
7096   (when (gnus-buffer-live-p gnus-article-buffer)
7097     (with-current-buffer gnus-article-buffer
7098       (mm-destroy-parts gnus-article-mime-handles)
7099       ;; Set it to nil for safety reason.
7100       (setq gnus-article-mime-handle-alist nil)
7101       (setq gnus-article-mime-handles nil)))
7102   (gnus-kill-save-kill-buffer)
7103   (gnus-async-halt-prefetch)
7104   (let* ((group gnus-newsgroup-name)
7105          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7106          (gnus-group-is-exiting-p t)
7107          (mode major-mode)
7108          (group-point nil)
7109          (buf (current-buffer)))
7110     (unless quit-config
7111       ;; Do adaptive scoring, and possibly save score files.
7112       (when gnus-newsgroup-adaptive
7113         (gnus-score-adaptive))
7114       (when gnus-use-scoring
7115         (gnus-score-save)))
7116     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7117     (when gnus-use-cache
7118       (gnus-cache-possibly-remove-articles)
7119       (gnus-cache-save-buffers))
7120     (gnus-async-prefetch-remove-group group)
7121     (when gnus-suppress-duplicates
7122       (gnus-dup-enter-articles))
7123     (when gnus-use-trees
7124       (gnus-tree-close group))
7125     (when gnus-use-cache
7126       (gnus-cache-write-active))
7127     ;; Remove entries for this group.
7128     (nnmail-purge-split-history (gnus-group-real-name group))
7129     ;; Make all changes in this group permanent.
7130     (unless quit-config
7131       (gnus-run-hooks 'gnus-exit-group-hook)
7132       (gnus-summary-update-info))
7133     (gnus-close-group group)
7134     ;; Make sure where we were, and go to next newsgroup.
7135     (set-buffer gnus-group-buffer)
7136     (unless quit-config
7137       (gnus-group-jump-to-group group))
7138     (gnus-run-hooks 'gnus-summary-exit-hook)
7139     (unless (or quit-config
7140                 (not gnus-summary-next-group-on-exit)
7141                 ;; If this group has disappeared from the summary
7142                 ;; buffer, don't skip forwards.
7143                 (not (string= group (gnus-group-group-name))))
7144       (gnus-group-next-unread-group 1))
7145     (setq group-point (point))
7146     (if temporary
7147         nil                             ;Nothing to do.
7148       (set-buffer buf)
7149       (if (not gnus-kill-summary-on-exit)
7150           (progn
7151             (gnus-deaden-summary)
7152             (setq mode nil))
7153         (when (get-buffer gnus-article-buffer)
7154           (bury-buffer gnus-article-buffer))
7155         ;; Return to group mode buffer.
7156         (when (eq mode 'gnus-summary-mode)
7157           (gnus-kill-buffer buf)))
7158
7159       ;; If we have several article buffers, we kill them at exit.
7160       (unless gnus-single-article-buffer
7161         (when (gnus-buffer-live-p gnus-article-buffer)
7162           (with-current-buffer gnus-article-buffer
7163             ;; Don't kill sticky article buffers
7164             (unless (eq major-mode 'gnus-sticky-article-mode)
7165               (gnus-kill-buffer gnus-article-buffer)
7166               (setq gnus-article-current nil))))
7167         (gnus-kill-buffer gnus-original-article-buffer))
7168
7169       (setq gnus-current-select-method gnus-select-method)
7170       (set-buffer gnus-group-buffer)
7171       (if quit-config
7172           (gnus-handle-ephemeral-exit quit-config)
7173         (goto-char group-point)
7174         ;; If gnus-group-buffer is already displayed, make sure we also move
7175         ;; the cursor in the window that displays it.
7176         (let ((win (get-buffer-window (current-buffer) 0)))
7177           (if win (set-window-point win (point))))
7178         (unless leave-hidden
7179           (gnus-configure-windows 'group 'force)))
7180       ;; Clear the current group name.
7181       (unless quit-config
7182         (setq gnus-newsgroup-name nil)))))
7183
7184 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7185 (defun gnus-summary-exit-no-update (&optional no-questions)
7186   "Quit reading current newsgroup without updating read article info."
7187   (interactive)
7188   (let* ((group gnus-newsgroup-name)
7189          (gnus-group-is-exiting-p t)
7190          (gnus-group-is-exiting-without-update-p t)
7191          (quit-config (gnus-group-quit-config group)))
7192     (when (or no-questions
7193               gnus-expert-user
7194               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7195       (gnus-async-halt-prefetch)
7196       (run-hooks 'gnus-summary-prepare-exit-hook)
7197       (when (gnus-buffer-live-p gnus-article-buffer)
7198         (with-current-buffer gnus-article-buffer
7199           (mm-destroy-parts gnus-article-mime-handles)
7200           ;; Set it to nil for safety reason.
7201           (setq gnus-article-mime-handle-alist nil)
7202           (setq gnus-article-mime-handles nil)))
7203       ;; If we have several article buffers, we kill them at exit.
7204       (unless gnus-single-article-buffer
7205         (gnus-kill-buffer gnus-article-buffer)
7206         (gnus-kill-buffer gnus-original-article-buffer)
7207         (setq gnus-article-current nil))
7208       (if (not gnus-kill-summary-on-exit)
7209           (gnus-deaden-summary)
7210         (gnus-close-group group)
7211         (gnus-kill-buffer gnus-summary-buffer))
7212       (unless gnus-single-article-buffer
7213         (setq gnus-article-current nil))
7214       (when gnus-use-trees
7215         (gnus-tree-close group))
7216       (gnus-async-prefetch-remove-group group)
7217       (when (get-buffer gnus-article-buffer)
7218         (bury-buffer gnus-article-buffer))
7219       ;; Return to the group buffer.
7220       (gnus-configure-windows 'group 'force)
7221       ;; Clear the current group name.
7222       (setq gnus-newsgroup-name nil)
7223       (unless (gnus-ephemeral-group-p group)
7224         (gnus-group-update-group group))
7225       (when (equal (gnus-group-group-name) group)
7226         (gnus-group-next-unread-group 1))
7227       (when quit-config
7228         (gnus-handle-ephemeral-exit quit-config)))))
7229
7230 (defun gnus-handle-ephemeral-exit (quit-config)
7231   "Handle movement when leaving an ephemeral group.
7232 The state which existed when entering the ephemeral is reset."
7233   (if (not (buffer-name (car quit-config)))
7234       (gnus-configure-windows 'group 'force)
7235     (set-buffer (car quit-config))
7236     (cond ((eq major-mode 'gnus-summary-mode)
7237            (gnus-set-global-variables))
7238           ((eq major-mode 'gnus-article-mode)
7239            (save-current-buffer
7240              ;; The `gnus-summary-buffer' variable may point
7241              ;; to the old summary buffer when using a single
7242              ;; article buffer.
7243              (unless (gnus-buffer-live-p gnus-summary-buffer)
7244                (set-buffer gnus-group-buffer))
7245              (set-buffer gnus-summary-buffer)
7246              (gnus-set-global-variables))))
7247     (if (or (eq (cdr quit-config) 'article)
7248             (eq (cdr quit-config) 'pick))
7249         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7250             (gnus-configure-windows 'pick 'force)
7251           (gnus-configure-windows (cdr quit-config) 'force))
7252       (gnus-configure-windows (cdr quit-config) 'force))
7253     (when (eq major-mode 'gnus-summary-mode)
7254       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7255                                                      next-unread-noselect))
7256           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7257                                   'next-noselect)
7258                               (gnus-summary-next-subject 1 nil t))
7259                              ((eq gnus-auto-select-on-ephemeral-exit
7260                                   'next-unread-noselect)
7261                               (gnus-summary-next-subject 1 t t))))
7262             ;; Hide the article buffer which displays the article different
7263             ;; from the one that the cursor points to in the summary buffer.
7264             (gnus-configure-windows 'summary 'force))
7265         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7266                (gnus-summary-next-subject 1))
7267               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7268                (gnus-summary-next-subject 1 t))))
7269       (gnus-summary-recenter)
7270       (gnus-summary-position-point))))
7271
7272 ;;; Dead summaries.
7273
7274 (defvar gnus-dead-summary-mode-map
7275   (let ((map (make-keymap)))
7276     (suppress-keymap map)
7277     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7278     (dolist (key '("\C-d" "\r" "\177" [delete]))
7279       (define-key map key 'gnus-summary-wake-up-the-dead))
7280     (dolist (key '("q" "Q"))
7281       (define-key map key 'bury-buffer))
7282     map))
7283
7284 (define-minor-mode gnus-dead-summary-mode
7285   "Minor mode for Gnus summary buffers."
7286   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7287   (unless (derived-mode-p 'gnus-summary-mode)
7288     (setq gnus-dead-summary-mode nil)))
7289
7290 (defun gnus-deaden-summary ()
7291   "Make the current summary buffer into a dead summary buffer."
7292   ;; Kill any previous dead summary buffer.
7293   (when (and gnus-dead-summary
7294              (buffer-name gnus-dead-summary))
7295     (with-current-buffer gnus-dead-summary
7296       (when gnus-dead-summary-mode
7297         (kill-buffer (current-buffer)))))
7298   ;; Make this the current dead summary.
7299   (setq gnus-dead-summary (current-buffer))
7300   (gnus-dead-summary-mode 1)
7301   (let ((name (buffer-name)))
7302     (when (string-match "Summary" name)
7303       (rename-buffer
7304        (concat (substring name 0 (match-beginning 0)) "Dead "
7305                (substring name (match-beginning 0)))
7306        t)
7307       (bury-buffer))))
7308
7309 (defun gnus-kill-or-deaden-summary (buffer)
7310   "Kill or deaden the summary BUFFER."
7311   (save-excursion
7312     (when (and (buffer-name buffer)
7313                (not gnus-single-article-buffer))
7314       (with-current-buffer buffer
7315         (gnus-kill-buffer gnus-article-buffer)
7316         (gnus-kill-buffer gnus-original-article-buffer)))
7317     (cond
7318      ;; Kill the buffer.
7319      (gnus-kill-summary-on-exit
7320       (when (and gnus-use-trees
7321                  (gnus-buffer-exists-p buffer))
7322         (with-current-buffer buffer
7323           (gnus-tree-close gnus-newsgroup-name)))
7324       (gnus-kill-buffer buffer))
7325      ;; Deaden the buffer.
7326      ((gnus-buffer-exists-p buffer)
7327       (with-current-buffer buffer
7328         (gnus-deaden-summary))))))
7329
7330 (defun gnus-summary-wake-up-the-dead (&rest args)
7331   "Wake up the dead summary buffer."
7332   (interactive)
7333   (gnus-dead-summary-mode -1)
7334   (let ((name (buffer-name)))
7335     (when (string-match "Dead " name)
7336       (rename-buffer
7337        (concat (substring name 0 (match-beginning 0))
7338                (substring name (match-end 0)))
7339        t)))
7340   (gnus-message 3 "This dead summary is now alive again"))
7341
7342 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7343 (defun gnus-summary-describe-group (&optional force)
7344   "Describe the current newsgroup."
7345   (interactive "P")
7346   (gnus-group-describe-group force gnus-newsgroup-name))
7347
7348 (defun gnus-summary-describe-briefly ()
7349   "Describe summary mode commands briefly."
7350   (interactive)
7351   (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")))
7352
7353 ;; Walking around group mode buffer from summary mode.
7354
7355 (defun gnus-summary-next-group (&optional no-article target-group backward)
7356   "Exit current newsgroup and then select next unread newsgroup.
7357 If prefix argument NO-ARTICLE is non-nil, no article is selected
7358 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7359 previous group instead."
7360   (interactive "P")
7361   ;; Stop pre-fetching.
7362   (gnus-async-halt-prefetch)
7363   (let ((current-group gnus-newsgroup-name)
7364         (current-buffer (current-buffer))
7365         entered)
7366     ;; First we semi-exit this group to update Xrefs and all variables.
7367     ;; We can't do a real exit, because the window conf must remain
7368     ;; the same in case the user is prompted for info, and we don't
7369     ;; want the window conf to change before that...
7370     (gnus-summary-exit t)
7371     (while (not entered)
7372       ;; Then we find what group we are supposed to enter.
7373       (set-buffer gnus-group-buffer)
7374       (gnus-group-jump-to-group current-group)
7375       (setq target-group
7376             (or target-group
7377                 (if (eq gnus-keep-same-level 'best)
7378                     (gnus-summary-best-group gnus-newsgroup-name)
7379                   (gnus-summary-search-group backward gnus-keep-same-level))))
7380       (if (not target-group)
7381           ;; There are no further groups, so we return to the group
7382           ;; buffer.
7383           (progn
7384             (gnus-message 5 "Returning to the group buffer")
7385             (setq entered t)
7386             (when (gnus-buffer-live-p current-buffer)
7387               (set-buffer current-buffer)
7388               (gnus-summary-exit))
7389             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7390         ;; We try to enter the target group.
7391         (gnus-group-jump-to-group target-group)
7392         (let ((unreads (gnus-group-group-unread)))
7393           (if (and (or (eq t unreads)
7394                        (and unreads (not (zerop unreads))))
7395                    (gnus-summary-read-group
7396                     target-group nil no-article
7397                     (and (buffer-name current-buffer) current-buffer)
7398                     nil backward))
7399               (setq entered t)
7400             (setq current-group target-group
7401                   target-group nil)))))))
7402
7403 (defun gnus-summary-prev-group (&optional no-article)
7404   "Exit current newsgroup and then select previous unread newsgroup.
7405 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7406   (interactive "P")
7407   (gnus-summary-next-group no-article nil t))
7408
7409 ;; Walking around summary lines.
7410
7411 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7412   "Go to the first subject satisfying any non-nil constraint.
7413 If UNREAD is non-nil, the article should be unread.
7414 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7415 If UNSEEN is non-nil, the article should be unseen as well as unread.
7416 Returns the article selected or nil if there are no matching articles."
7417   (interactive "P")
7418   (cond
7419    ;; Empty summary.
7420    ((null gnus-newsgroup-data)
7421     (gnus-message 3 "No articles in the group")
7422     nil)
7423    ;; Pick the first article.
7424    ((not (or unread undownloaded unseen))
7425     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7426     (gnus-data-number (car gnus-newsgroup-data)))
7427    ;; Find the first unread article.
7428    (t
7429     (let ((data gnus-newsgroup-data))
7430       (while (and data
7431                   (let ((num (gnus-data-number (car data))))
7432                     (or (memq num gnus-newsgroup-unfetched)
7433                         (not (or (and unread
7434                                       (memq num gnus-newsgroup-unreads))
7435                                  (and undownloaded
7436                                       (memq num gnus-newsgroup-undownloaded))
7437                                  (and unseen
7438                                       (memq num gnus-newsgroup-unseen)
7439                                       (memq num gnus-newsgroup-unreads)))))))
7440         (setq data (cdr data)))
7441       (prog1
7442           (if data
7443               (progn
7444                 (goto-char (gnus-data-pos (car data)))
7445                 (gnus-data-number (car data)))
7446             (gnus-message 3 "No more%s articles"
7447                           (let* ((r (when unread " unread"))
7448                                  (d (when undownloaded " undownloaded"))
7449                                  (s (when unseen " unseen"))
7450                                  (l (delq nil (list r d s))))
7451                             (cond ((= 3 (length l))
7452                                    (concat r "," d ", or" s))
7453                                   ((= 2 (length l))
7454                                    (concat (car l) ", or" (cadr l)))
7455                                   ((= 1 (length l))
7456                                    (car l))
7457                                   (t
7458                                    ""))))
7459             nil
7460             )
7461         (gnus-summary-position-point))))))
7462
7463 (defun gnus-summary-next-subject (n &optional unread dont-display)
7464   "Go to next N'th summary line.
7465 If N is negative, go to the previous N'th subject line.
7466 If UNREAD is non-nil, only unread articles are selected.
7467 The difference between N and the actual number of steps taken is
7468 returned."
7469   (interactive "p")
7470   (let ((backward (< n 0))
7471         (n (abs n)))
7472     (while (and (> n 0)
7473                 (if backward
7474                     (gnus-summary-find-prev unread)
7475                   (gnus-summary-find-next unread)))
7476       (unless (zerop (setq n (1- n)))
7477         (gnus-summary-show-thread)))
7478     (when (/= 0 n)
7479       (gnus-message 7 "No more%s articles"
7480                     (if unread " unread" "")))
7481     (unless dont-display
7482       (gnus-summary-recenter)
7483       (gnus-summary-position-point))
7484     n))
7485
7486 (defun gnus-summary-next-unread-subject (n)
7487   "Go to next N'th unread summary line."
7488   (interactive "p")
7489   (gnus-summary-next-subject n t))
7490
7491 (defun gnus-summary-prev-subject (n &optional unread)
7492   "Go to previous N'th summary line.
7493 If optional argument UNREAD is non-nil, only unread article is selected."
7494   (interactive "p")
7495   (gnus-summary-next-subject (- n) unread))
7496
7497 (defun gnus-summary-prev-unread-subject (n)
7498   "Go to previous N'th unread summary line."
7499   (interactive "p")
7500   (gnus-summary-next-subject (- n) t))
7501
7502 (defun gnus-summary-goto-subjects (articles)
7503   "Insert the subject header for ARTICLES in the current buffer."
7504   (save-excursion
7505     (dolist (article articles)
7506       (gnus-summary-goto-subject article t)))
7507   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7508   (gnus-summary-position-point))
7509
7510 (defun gnus-summary-goto-subject (article &optional force silent)
7511   "Go to the subject line of ARTICLE.
7512 If FORCE, also allow jumping to articles not currently shown."
7513   (interactive "nArticle number: ")
7514   (unless (numberp article)
7515     (error "Article %s is not a number" article))
7516   (let ((b (point))
7517         (data (gnus-data-find article)))
7518     ;; We read in the article if we have to.
7519     (and (not data)
7520          force
7521          (gnus-summary-insert-subject
7522           article
7523           (if (or (numberp force) (vectorp force)) force)
7524           t)
7525          (setq data (gnus-data-find article)))
7526     (goto-char b)
7527     (if (not data)
7528         (progn
7529           (unless silent
7530             (gnus-message 3 "Can't find article %d" article))
7531           nil)
7532       (let ((pt (gnus-data-pos data)))
7533         (goto-char pt)
7534         (gnus-summary-set-article-display-arrow pt))
7535       (gnus-summary-position-point)
7536       article)))
7537
7538 ;; Walking around summary lines with displaying articles.
7539
7540 (defun gnus-summary-expand-window (&optional arg)
7541   "Make the summary buffer take up the entire Emacs frame.
7542 Given a prefix, will force an `article' buffer configuration."
7543   (interactive "P")
7544   (if arg
7545       (gnus-configure-windows 'article 'force)
7546     (gnus-configure-windows 'summary 'force)))
7547
7548 (defun gnus-summary-display-article (article &optional all-header)
7549   "Display ARTICLE in article buffer."
7550   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7551                (with-current-buffer gnus-article-buffer
7552                  (eq major-mode 'gnus-article-mode)))
7553     (gnus-article-setup-buffer))
7554   (gnus-set-global-variables)
7555   (with-current-buffer gnus-article-buffer
7556     (setq gnus-article-charset gnus-newsgroup-charset)
7557     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7558     (mm-enable-multibyte))
7559   (if (null article)
7560       nil
7561     (prog1
7562         (if gnus-summary-display-article-function
7563             (funcall gnus-summary-display-article-function article all-header)
7564           (gnus-article-prepare article all-header))
7565       (gnus-run-hooks 'gnus-select-article-hook)
7566       (when (and gnus-current-article
7567                  (not (zerop gnus-current-article)))
7568         (gnus-summary-goto-subject gnus-current-article))
7569       (gnus-summary-recenter)
7570       (when (and gnus-use-trees gnus-show-threads)
7571         (gnus-possibly-generate-tree article)
7572         (gnus-highlight-selected-tree article))
7573       ;; Successfully display article.
7574       (gnus-article-set-window-start
7575        (cdr (assq article gnus-newsgroup-bookmarks))))))
7576
7577 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7578   "Select the current article.
7579 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7580 non-nil, the article will be re-fetched even if it already present in
7581 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7582 be displayed."
7583   ;; Make sure we are in the summary buffer to work around bbdb bug.
7584   (unless (eq major-mode 'gnus-summary-mode)
7585     (set-buffer gnus-summary-buffer))
7586   (let ((article (or article (gnus-summary-article-number)))
7587         (all-headers (not (not all-headers))) ;Must be t or nil.
7588         gnus-summary-display-article-function)
7589     (and (not pseudo)
7590          (gnus-summary-article-pseudo-p article)
7591          (error "This is a pseudo-article"))
7592     (with-current-buffer gnus-summary-buffer
7593       (if (or (and gnus-single-article-buffer
7594                    (or (null gnus-current-article)
7595                        (null gnus-article-current)
7596                        (null (get-buffer gnus-article-buffer))
7597                        (not (eq article (cdr gnus-article-current)))
7598                        (not (equal (car gnus-article-current)
7599                                    gnus-newsgroup-name))
7600                        (not (get-buffer gnus-original-article-buffer))))
7601               (and (not gnus-single-article-buffer)
7602                    (or (null gnus-current-article)
7603                        (not (get-buffer gnus-original-article-buffer))
7604                        (not (eq gnus-current-article article))))
7605               force)
7606           ;; The requested article is different from the current article.
7607           (progn
7608             (gnus-summary-display-article article all-headers)
7609             (when (gnus-buffer-live-p gnus-article-buffer)
7610               (with-current-buffer gnus-article-buffer
7611                 (if (not gnus-article-decoded-p) ;; a local variable
7612                     (mm-disable-multibyte))))
7613             (gnus-article-set-window-start
7614              (cdr (assq article gnus-newsgroup-bookmarks)))
7615             article)
7616         'old))))
7617
7618 (defun gnus-summary-force-verify-and-decrypt ()
7619   "Display buttons for signed/encrypted parts and verify/decrypt them."
7620   (interactive)
7621   (let ((mm-verify-option 'known)
7622         (mm-decrypt-option 'known)
7623         (gnus-article-emulate-mime t)
7624         (gnus-buttonized-mime-types (append (list "multipart/signed"
7625                                                   "multipart/encrypted")
7626                                             gnus-buttonized-mime-types)))
7627     (gnus-summary-select-article nil 'force)))
7628
7629 (defun gnus-summary-set-current-mark (&optional current-mark)
7630   "Obsolete function."
7631   nil)
7632
7633 (defun gnus-summary-next-article (&optional unread subject backward push)
7634   "Select the next article.
7635 If UNREAD, only unread articles are selected.
7636 If SUBJECT, only articles with SUBJECT are selected.
7637 If BACKWARD, the previous article is selected instead of the next."
7638   (interactive "P")
7639   ;; Make sure we are in the summary buffer.
7640   (unless (eq major-mode 'gnus-summary-mode)
7641     (set-buffer gnus-summary-buffer))
7642   (cond
7643    ;; Is there such an article?
7644    ((and (gnus-summary-search-forward unread subject backward)
7645          (or (gnus-summary-display-article (gnus-summary-article-number))
7646              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7647     (gnus-summary-position-point))
7648    ;; If not, we try the first unread, if that is wanted.
7649    ((and subject
7650          gnus-auto-select-same
7651          (gnus-summary-first-unread-article))
7652     (gnus-summary-position-point)
7653     (gnus-message 6 "Wrapped"))
7654    ;; Try to get next/previous article not displayed in this group.
7655    ((and gnus-auto-extend-newsgroup
7656          (not unread) (not subject))
7657     (gnus-summary-goto-article
7658      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7659      nil (count-lines (point-min) (point))))
7660    ;; Go to next/previous group.
7661    (t
7662     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7663       (gnus-summary-jump-to-group gnus-newsgroup-name))
7664     (let ((cmd (if (featurep 'xemacs)
7665                    last-command-char
7666                  last-command-event))
7667           (point
7668            (with-current-buffer gnus-group-buffer
7669              (point)))
7670           (group
7671            (if (eq gnus-keep-same-level 'best)
7672                (gnus-summary-best-group gnus-newsgroup-name)
7673              (gnus-summary-search-group backward gnus-keep-same-level))))
7674       ;; For some reason, the group window gets selected.  We change
7675       ;; it back.
7676       (select-window (get-buffer-window (current-buffer)))
7677       ;; Select next unread newsgroup automagically.
7678       (cond
7679        ((or (not gnus-auto-select-next)
7680             (not cmd))
7681         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7682        ((or (eq gnus-auto-select-next 'quietly)
7683             (and (eq gnus-auto-select-next 'slightly-quietly)
7684                  push)
7685             (and (eq gnus-auto-select-next 'almost-quietly)
7686                  (gnus-summary-last-article-p)))
7687         ;; Select quietly.
7688         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7689             (gnus-summary-exit)
7690           (gnus-message 7 "No more%s articles (%s)..."
7691                         (if unread " unread" "")
7692                         (if group (concat "selecting " group)
7693                           "exiting"))
7694           (gnus-summary-next-group nil group backward)))
7695        (t
7696         (when (gnus-key-press-event-p last-input-event)
7697           (gnus-summary-walk-group-buffer
7698            gnus-newsgroup-name cmd unread backward point))))))))
7699
7700 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7701   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7702                       (?\C-p (gnus-group-prev-unread-group 1))))
7703         (cursor-in-echo-area t)
7704         keve key group ended prompt)
7705     (with-current-buffer gnus-group-buffer
7706       (goto-char start)
7707       (setq group
7708             (if (eq gnus-keep-same-level 'best)
7709                 (gnus-summary-best-group gnus-newsgroup-name)
7710               (gnus-summary-search-group backward gnus-keep-same-level))))
7711     (while (not ended)
7712       (setq prompt
7713             (format
7714              "No more%s articles%s " (if unread " unread" "")
7715              (if (and group
7716                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7717                  (format " (Type %s for %s [%s])"
7718                          (single-key-description cmd)
7719                          (gnus-group-decoded-name group)
7720                          (gnus-group-unread group))
7721                (format " (Type %s to exit %s)"
7722                        (single-key-description cmd)
7723                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7724       ;; Confirm auto selection.
7725       (setq key (car (setq keve (gnus-read-event-char prompt)))
7726             ended t)
7727       (cond
7728        ((assq key keystrokes)
7729         (let ((obuf (current-buffer)))
7730           (switch-to-buffer gnus-group-buffer)
7731           (when group
7732             (gnus-group-jump-to-group group))
7733           (eval (cadr (assq key keystrokes)))
7734           (setq group (gnus-group-group-name))
7735           (switch-to-buffer obuf))
7736         (setq ended nil))
7737        ((equal key cmd)
7738         (if (or (not group)
7739                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7740             (gnus-summary-exit)
7741           (gnus-summary-next-group nil group backward)))
7742        (t
7743         (push (cdr keve) unread-command-events))))))
7744
7745 (defun gnus-summary-next-unread-article ()
7746   "Select unread article after current one."
7747   (interactive)
7748   (gnus-summary-next-article
7749    (or (not (eq gnus-summary-goto-unread 'never))
7750        (gnus-summary-last-article-p (gnus-summary-article-number)))
7751    (and gnus-auto-select-same
7752         (gnus-summary-article-subject))))
7753
7754 (defun gnus-summary-prev-article (&optional unread subject)
7755   "Select the article before the current one.
7756 If UNREAD is non-nil, only unread articles are selected."
7757   (interactive "P")
7758   (gnus-summary-next-article unread subject t))
7759
7760 (defun gnus-summary-prev-unread-article ()
7761   "Select unread article before current one."
7762   (interactive)
7763   (gnus-summary-prev-article
7764    (or (not (eq gnus-summary-goto-unread 'never))
7765        (gnus-summary-first-article-p (gnus-summary-article-number)))
7766    (and gnus-auto-select-same
7767         (gnus-summary-article-subject))))
7768
7769 (defun gnus-summary-next-page (&optional lines circular stop)
7770   "Show next page of the selected article.
7771 If at the end of the current article, select the next article.
7772 LINES says how many lines should be scrolled up.
7773
7774 If CIRCULAR is non-nil, go to the start of the article instead of
7775 selecting the next article when reaching the end of the current
7776 article.
7777
7778 If STOP is non-nil, just stop when reaching the end of the message.
7779
7780 Also see the variable `gnus-article-skip-boring'."
7781   (interactive "P")
7782   (setq gnus-summary-buffer (current-buffer))
7783   (gnus-set-global-variables)
7784   (let ((article (gnus-summary-article-number))
7785         (article-window (get-buffer-window gnus-article-buffer t))
7786         endp)
7787     ;; If the buffer is empty, we have no article.
7788     (unless article
7789       (error "No article to select"))
7790     (gnus-configure-windows 'article)
7791     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7792         (if (and (eq gnus-summary-goto-unread 'never)
7793                  (not (gnus-summary-last-article-p article)))
7794             (gnus-summary-next-article)
7795           (gnus-summary-next-unread-article))
7796       (if (or (null gnus-current-article)
7797               (null gnus-article-current)
7798               (/= article (cdr gnus-article-current))
7799               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7800           ;; Selected subject is different from current article's.
7801           (gnus-summary-display-article article)
7802         (when article-window
7803           (gnus-eval-in-buffer-window gnus-article-buffer
7804             (setq endp (or (gnus-article-next-page lines)
7805                            (gnus-article-only-boring-p))))
7806           (when endp
7807             (cond ((or stop gnus-summary-stop-at-end-of-message)
7808                    (gnus-message 3 "End of message"))
7809                   (circular
7810                    (gnus-summary-beginning-of-article))
7811                   (lines
7812                    (gnus-message 3 "End of message"))
7813                   ((null lines)
7814                    (if (and (eq gnus-summary-goto-unread 'never)
7815                             (not (gnus-summary-last-article-p article)))
7816                        (gnus-summary-next-article)
7817                      (gnus-summary-next-unread-article))))))))
7818     (gnus-summary-recenter)
7819     (gnus-summary-position-point)))
7820
7821 (defun gnus-summary-prev-page (&optional lines move)
7822   "Show previous page of selected article.
7823 Argument LINES specifies lines to be scrolled down.
7824 If MOVE, move to the previous unread article if point is at
7825 the beginning of the buffer."
7826   (interactive "P")
7827   (let ((article (gnus-summary-article-number))
7828         (article-window (get-buffer-window gnus-article-buffer t))
7829         endp)
7830     (gnus-configure-windows 'article)
7831     (if (or (null gnus-current-article)
7832             (null gnus-article-current)
7833             (/= article (cdr gnus-article-current))
7834             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7835         ;; Selected subject is different from current article's.
7836         (gnus-summary-display-article article)
7837       (gnus-summary-recenter)
7838       (when article-window
7839         (gnus-eval-in-buffer-window gnus-article-buffer
7840           (setq endp (gnus-article-prev-page lines)))
7841         (when (and move endp)
7842           (cond (lines
7843                  (gnus-message 3 "Beginning of message"))
7844                 ((null lines)
7845                  (if (and (eq gnus-summary-goto-unread 'never)
7846                           (not (gnus-summary-first-article-p article)))
7847                      (gnus-summary-prev-article)
7848                    (gnus-summary-prev-unread-article))))))))
7849   (gnus-summary-position-point))
7850
7851 (defun gnus-summary-prev-page-or-article (&optional lines)
7852   "Show previous page of selected article.
7853 Argument LINES specifies lines to be scrolled down.
7854 If at the beginning of the article, go to the next article."
7855   (interactive "P")
7856   (gnus-summary-prev-page lines t))
7857
7858 (defun gnus-summary-scroll-up (lines)
7859   "Scroll up (or down) one line current article.
7860 Argument LINES specifies lines to be scrolled up (or down if negative).
7861 If no article is selected, then the current article will be selected first."
7862   (interactive "p")
7863   (gnus-configure-windows 'article)
7864   (gnus-summary-show-thread)
7865   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7866     (gnus-eval-in-buffer-window gnus-article-buffer
7867       (cond ((> lines 0)
7868              (when (gnus-article-next-page lines)
7869                (gnus-message 3 "End of message")))
7870             ((< lines 0)
7871              (gnus-article-prev-page (- lines))))))
7872   (gnus-summary-recenter)
7873   (gnus-summary-position-point))
7874
7875 (defun gnus-summary-scroll-down (lines)
7876   "Scroll down (or up) one line current article.
7877 Argument LINES specifies lines to be scrolled down (or up if negative).
7878 If no article is selected, then the current article will be selected first."
7879   (interactive "p")
7880   (gnus-summary-scroll-up (- lines)))
7881
7882 (defun gnus-summary-next-same-subject ()
7883   "Select next article which has the same subject as current one."
7884   (interactive)
7885   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7886
7887 (defun gnus-summary-prev-same-subject ()
7888   "Select previous article which has the same subject as current one."
7889   (interactive)
7890   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7891
7892 (defun gnus-summary-next-unread-same-subject ()
7893   "Select next unread article which has the same subject as current one."
7894   (interactive)
7895   (gnus-summary-next-article t (gnus-summary-article-subject)))
7896
7897 (defun gnus-summary-prev-unread-same-subject ()
7898   "Select previous unread article which has the same subject as current one."
7899   (interactive)
7900   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7901
7902 (defun gnus-summary-first-unread-article ()
7903   "Select the first unread article.
7904 Return nil if there are no unread articles."
7905   (interactive)
7906   (prog1
7907       (when (gnus-summary-first-subject t)
7908         (gnus-summary-show-thread)
7909         (gnus-summary-first-subject t)
7910         (gnus-summary-display-article (gnus-summary-article-number)))
7911     (gnus-summary-position-point)))
7912
7913 (defun gnus-summary-first-unread-subject ()
7914   "Place the point on the subject line of the first unread article.
7915 Return nil if there are no unread articles."
7916   (interactive)
7917   (prog1
7918       (when (gnus-summary-first-subject t)
7919         (gnus-summary-show-thread)
7920         (gnus-summary-first-subject t))
7921     (gnus-summary-position-point)))
7922
7923 (defun gnus-summary-first-unseen-subject ()
7924   "Place the point on the subject line of the first unseen article.
7925 Return nil if there are no unseen articles."
7926   (interactive)
7927   (prog1
7928       (when (gnus-summary-first-subject nil nil t)
7929         (gnus-summary-show-thread)
7930         (gnus-summary-first-subject nil nil t))
7931     (gnus-summary-position-point)))
7932
7933 (defun gnus-summary-first-unseen-or-unread-subject ()
7934   "Place the point on the subject line of the first unseen and unread article.
7935 If all article have been seen, on the subject line of the first unread
7936 article."
7937   (interactive)
7938   (prog1
7939       (unless (when (gnus-summary-first-subject nil nil t)
7940                 (gnus-summary-show-thread)
7941                 (gnus-summary-first-subject nil nil t))
7942         (when (gnus-summary-first-subject t)
7943           (gnus-summary-show-thread)
7944           (gnus-summary-first-subject t)))
7945     (gnus-summary-position-point)))
7946
7947 (defun gnus-summary-first-article ()
7948   "Select the first article.
7949 Return nil if there are no articles."
7950   (interactive)
7951   (prog1
7952       (when (gnus-summary-first-subject)
7953         (gnus-summary-show-thread)
7954         (gnus-summary-first-subject)
7955         (gnus-summary-display-article (gnus-summary-article-number)))
7956     (gnus-summary-position-point)))
7957
7958 (defun gnus-summary-best-unread-article (&optional arg)
7959   "Select the unread article with the highest score.
7960 If given a prefix argument, select the next unread article that has a
7961 score higher than the default score."
7962   (interactive "P")
7963   (let ((article (if arg
7964                      (gnus-summary-better-unread-subject)
7965                    (gnus-summary-best-unread-subject))))
7966     (if article
7967         (gnus-summary-goto-article article)
7968       (error "No unread articles"))))
7969
7970 (defun gnus-summary-best-unread-subject ()
7971   "Select the unread subject with the highest score."
7972   (interactive)
7973   (let ((best -1000000)
7974         (data gnus-newsgroup-data)
7975         article score)
7976     (while data
7977       (and (gnus-data-unread-p (car data))
7978            (> (setq score
7979                     (gnus-summary-article-score (gnus-data-number (car data))))
7980               best)
7981            (setq best score
7982                  article (gnus-data-number (car data))))
7983       (setq data (cdr data)))
7984     (when article
7985       (gnus-summary-goto-subject article))
7986     (gnus-summary-position-point)
7987     article))
7988
7989 (defun gnus-summary-better-unread-subject ()
7990   "Select the first unread subject that has a score over the default score."
7991   (interactive)
7992   (let ((data gnus-newsgroup-data)
7993         article score)
7994     (while (and (setq article (gnus-data-number (car data)))
7995                 (or (gnus-data-read-p (car data))
7996                     (not (> (gnus-summary-article-score article)
7997                             gnus-summary-default-score))))
7998       (setq data (cdr data)))
7999     (when article
8000       (gnus-summary-goto-subject article))
8001     (gnus-summary-position-point)
8002     article))
8003
8004 (defun gnus-summary-last-subject ()
8005   "Go to the last displayed subject line in the group."
8006   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8007     (when article
8008       (gnus-summary-goto-subject article))))
8009
8010 (defun gnus-summary-goto-article (article &optional all-headers force)
8011   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8012 If ALL-HEADERS is non-nil, no header lines are hidden.
8013 If FORCE, go to the article even if it isn't displayed.  If FORCE
8014 is a number, it is the line the article is to be displayed on."
8015   (interactive
8016    (list
8017     (gnus-completing-read
8018      "Article number or Message-ID"
8019      (mapcar 'int-to-string gnus-newsgroup-limit))
8020     current-prefix-arg
8021     t))
8022   (prog1
8023       (if (and (stringp article)
8024                (string-match "@\\|%40" article))
8025           (gnus-summary-refer-article article)
8026         (when (stringp article)
8027           (setq article (string-to-number article)))
8028         (if (gnus-summary-goto-subject article force)
8029             (gnus-summary-display-article article all-headers)
8030           (gnus-message 4 "Couldn't go to article %s" article) nil))
8031     (gnus-summary-position-point)))
8032
8033 (defun gnus-summary-goto-last-article ()
8034   "Go to the previously read article."
8035   (interactive)
8036   (prog1
8037       (when gnus-last-article
8038         (gnus-summary-goto-article gnus-last-article nil t))
8039     (gnus-summary-position-point)))
8040
8041 (defun gnus-summary-pop-article (number)
8042   "Pop one article off the history and go to the previous.
8043 NUMBER articles will be popped off."
8044   (interactive "p")
8045   (let (to)
8046     (setq gnus-newsgroup-history
8047           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8048     (if to
8049         (gnus-summary-goto-article (car to) nil t)
8050       (error "Article history empty")))
8051   (gnus-summary-position-point))
8052
8053 ;; Summary commands and functions for limiting the summary buffer.
8054
8055 (defun gnus-summary-limit-to-articles (n)
8056   "Limit the summary buffer to the next N articles.
8057 If not given a prefix, use the process marked articles instead."
8058   (interactive "P")
8059   (prog1
8060       (let ((articles (gnus-summary-work-articles n)))
8061         (setq gnus-newsgroup-processable nil)
8062         (gnus-summary-limit articles))
8063     (gnus-summary-position-point)))
8064
8065 (defun gnus-summary-pop-limit (&optional total)
8066   "Restore the previous limit.
8067 If given a prefix, remove all limits."
8068   (interactive "P")
8069   (when total
8070     (setq gnus-newsgroup-limits
8071           (list (mapcar (lambda (h) (mail-header-number h))
8072                         gnus-newsgroup-headers))))
8073   (unless gnus-newsgroup-limits
8074     (error "No limit to pop"))
8075   (prog1
8076       (gnus-summary-limit nil 'pop)
8077     (gnus-summary-position-point)))
8078
8079 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8080   "Limit the summary buffer to articles that have subjects that match a regexp.
8081 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8082   (interactive
8083    (list (read-string (if current-prefix-arg
8084                           "Exclude subject (regexp): "
8085                         "Limit to subject (regexp): "))
8086          nil current-prefix-arg))
8087   (unless header
8088     (setq header "subject"))
8089   (when (not (equal "" subject))
8090     (prog1
8091         (let ((articles (gnus-summary-find-matching
8092                          (or header "subject") subject 'all nil nil
8093                          not-matching)))
8094           (unless articles
8095             (error "Found no matches for \"%s\"" subject))
8096           (gnus-summary-limit articles))
8097       (gnus-summary-position-point))))
8098
8099 (defun gnus-summary-limit-to-author (from &optional not-matching)
8100   "Limit the summary buffer to articles that have authors that match a regexp.
8101 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8102   (interactive
8103    (list (read-string (if current-prefix-arg
8104                           "Exclude author (regexp): "
8105                         "Limit to author (regexp): "))
8106          current-prefix-arg))
8107   (gnus-summary-limit-to-subject from "from" not-matching))
8108
8109 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8110   "Limit the summary buffer to articles with the given RECIPIENT.
8111
8112 If NOT-MATCHING, exclude RECIPIENT.
8113
8114 To and Cc headers are checked.  You need to include them in
8115 `nnmail-extra-headers'."
8116   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8117   (interactive
8118    (list (read-string (format "%s recipient (regexp): "
8119                               (if current-prefix-arg "Exclude" "Limit to")))
8120          current-prefix-arg))
8121   (when (not (equal "" recipient))
8122     (prog1 (let* ((to
8123                    (if (memq 'To nnmail-extra-headers)
8124                        (gnus-summary-find-matching
8125                         (cons 'extra 'To) recipient 'all nil nil
8126                         not-matching)
8127                      (gnus-message
8128                       1 "`To' isn't present in `nnmail-extra-headers'")
8129                      (sit-for 1)
8130                      nil))
8131                   (cc
8132                    (if (memq 'Cc nnmail-extra-headers)
8133                        (gnus-summary-find-matching
8134                         (cons 'extra 'Cc) recipient 'all nil nil
8135                         not-matching)
8136                      (gnus-message
8137                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8138                      (sit-for 1)
8139                      nil))
8140                   (articles
8141                    (if not-matching
8142                        ;; We need the numbers that are in both lists:
8143                        (mapcar (lambda (a)
8144                                  (and (memq a to) a))
8145                                cc)
8146                      (nconc to cc))))
8147              (unless articles
8148                (error "Found no matches for \"%s\"" recipient))
8149              (gnus-summary-limit articles))
8150       (gnus-summary-position-point))))
8151
8152 (defun gnus-summary-limit-to-address (address &optional not-matching)
8153   "Limit the summary buffer to articles with the given ADDRESS.
8154
8155 If NOT-MATCHING, exclude ADDRESS.
8156
8157 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8158 in `nnmail-extra-headers'."
8159   (interactive
8160    (list (read-string (format "%s address (regexp): "
8161                               (if current-prefix-arg "Exclude" "Limit to")))
8162          current-prefix-arg))
8163   (when (not (equal "" address))
8164     (prog1 (let* ((to
8165                    (if (memq 'To nnmail-extra-headers)
8166                        (gnus-summary-find-matching
8167                         (cons 'extra 'To) address 'all nil nil
8168                         not-matching)
8169                      (gnus-message
8170                       1 "`To' isn't present in `nnmail-extra-headers'")
8171                      (sit-for 1)
8172                      t))
8173                   (cc
8174                    (if (memq 'Cc nnmail-extra-headers)
8175                        (gnus-summary-find-matching
8176                         (cons 'extra 'Cc) address 'all nil nil
8177                         not-matching)
8178                      (gnus-message
8179                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8180                      (sit-for 1)
8181                      t))
8182                   (from
8183                    (gnus-summary-find-matching "from" address
8184                                                'all nil nil not-matching))
8185                   (articles
8186                    (if not-matching
8187                        ;; We need the numbers that are in all lists:
8188                        (if (eq cc t)
8189                            (if (eq to t)
8190                                from
8191                              (mapcar (lambda (a) (car (memq a from))) to))
8192                          (if (eq to t)
8193                              (mapcar (lambda (a) (car (memq a from))) cc)
8194                            (mapcar (lambda (a) (car (memq a from)))
8195                                    (mapcar (lambda (a) (car (memq a to)))
8196                                            cc))))
8197                      (nconc (if (eq to t) nil to)
8198                             (if (eq cc t) nil cc)
8199                             from))))
8200              (unless articles
8201                (error "Found no matches for \"%s\"" address))
8202              (gnus-summary-limit articles))
8203       (gnus-summary-position-point))))
8204
8205 (defun gnus-summary-limit-strange-charsets-predicate (header)
8206   (when (fboundp 'char-charset)
8207     (let ((string (concat (mail-header-subject header)
8208                           (mail-header-from header)))
8209           charset found)
8210       (dotimes (i (1- (length string)))
8211         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8212         (when (string-match "unicode\\|big\\|japanese" charset)
8213           (setq found t)))
8214       found)))
8215
8216 (defun gnus-summary-limit-to-predicate (predicate)
8217   "Limit to articles where PREDICATE returns non-nil.
8218 PREDICATE will be called with the header structures of the
8219 articles."
8220   (let ((articles nil)
8221         (case-fold-search t))
8222     (dolist (header gnus-newsgroup-headers)
8223       (when (funcall predicate header)
8224         (push (mail-header-number header) articles)))
8225     (gnus-summary-limit (nreverse articles))))
8226
8227 (defun gnus-summary-limit-to-age (age &optional younger-p)
8228   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8229 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8230 articles that are younger than AGE days."
8231   (interactive
8232    (let ((younger current-prefix-arg)
8233          (days-got nil)
8234          days)
8235      (while (not days-got)
8236        (setq days (if younger
8237                       (read-string "Limit to articles younger than (in days, older when negative): ")
8238                     (read-string
8239                      "Limit to articles older than (in days, younger when negative): ")))
8240        (when (> (length days) 0)
8241          (setq days (read days)))
8242        (if (numberp days)
8243            (progn
8244              (setq days-got t)
8245              (when (< days 0)
8246                (setq younger (not younger))
8247                (setq days (* days -1))))
8248          (message "Please enter a number.")
8249          (sleep-for 1)))
8250      (list days younger)))
8251   (prog1
8252       (let ((data gnus-newsgroup-data)
8253             (cutoff (days-to-time age))
8254             articles d date is-younger)
8255         (while (setq d (pop data))
8256           (when (and (vectorp (gnus-data-header d))
8257                      (setq date (mail-header-date (gnus-data-header d))))
8258             (setq is-younger (time-less-p
8259                               (time-since (gnus-date-get-time date))
8260                               cutoff))
8261             (when (if younger-p
8262                       is-younger
8263                     (not is-younger))
8264               (push (gnus-data-number d) articles))))
8265         (gnus-summary-limit (nreverse articles)))
8266     (gnus-summary-position-point)))
8267
8268 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8269   "Limit the summary buffer to articles that match an 'extra' header."
8270   (interactive
8271    (let ((header
8272           (intern
8273            (gnus-completing-read
8274             (if current-prefix-arg
8275                 "Exclude extra header"
8276               "Limit extra header")
8277             (mapcar 'symbol-name gnus-extra-headers)
8278             t nil nil
8279             (symbol-name (car gnus-extra-headers))))))
8280      (list header
8281            (read-string (format "%s header %s (regexp): "
8282                                 (if current-prefix-arg "Exclude" "Limit to")
8283                                 header))
8284            current-prefix-arg)))
8285   (when (not (equal "" regexp))
8286     (prog1
8287         (let ((articles (gnus-summary-find-matching
8288                          (cons 'extra header) regexp 'all nil nil
8289                          not-matching)))
8290           (unless articles
8291             (error "Found no matches for \"%s\"" regexp))
8292           (gnus-summary-limit articles))
8293       (gnus-summary-position-point))))
8294
8295 (defun gnus-summary-limit-to-display-predicate ()
8296   "Limit the summary buffer to the predicated in the `display' group parameter."
8297   (interactive)
8298   (unless gnus-newsgroup-display
8299     (error "There is no `display' group parameter"))
8300   (let (articles)
8301     (dolist (gnus-number gnus-newsgroup-articles)
8302       (when (funcall gnus-newsgroup-display)
8303         (push gnus-number articles)))
8304     (gnus-summary-limit articles))
8305   (gnus-summary-position-point))
8306
8307 (defun gnus-summary-limit-to-unread (&optional all)
8308   "Limit the summary buffer to articles that are not marked as read.
8309 If ALL is non-nil, limit strictly to unread articles."
8310   (interactive "P")
8311   (if all
8312       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8313     (gnus-summary-limit-to-marks
8314      ;; Concat all the marks that say that an article is read and have
8315      ;; those removed.
8316      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8317            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8318            gnus-low-score-mark gnus-expirable-mark
8319            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8320            gnus-duplicate-mark)
8321      'reverse)))
8322
8323 (defun gnus-summary-limit-to-headers (match &optional reverse)
8324   "Limit the summary buffer to articles that have headers that match MATCH.
8325 If REVERSE (the prefix), limit to articles that don't match."
8326   (interactive "sMatch headers (regexp): \nP")
8327   (gnus-summary-limit-to-bodies match reverse t))
8328
8329 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8330   "Limit the summary buffer to articles that have bodies that match MATCH.
8331 If REVERSE (the prefix), limit to articles that don't match."
8332   (interactive "sMatch body (regexp): \nP")
8333   (let ((articles nil)
8334         (gnus-select-article-hook nil)  ;Disable hook.
8335         (gnus-article-prepare-hook nil)
8336         (gnus-use-article-prefetch nil)
8337         (gnus-keep-backlog nil)
8338         (gnus-break-pages nil)
8339         (gnus-summary-display-arrow nil)
8340         (gnus-updated-mode-lines nil)
8341         (gnus-auto-center-summary nil)
8342         (gnus-display-mime-function nil))
8343     (dolist (data gnus-newsgroup-data)
8344       (let (gnus-mark-article-hook)
8345         (gnus-summary-select-article t t nil (gnus-data-number data)))
8346       (with-current-buffer gnus-article-buffer
8347         (article-goto-body)
8348         (let* ((case-fold-search t)
8349                (found (if headersp
8350                           (re-search-backward match nil t)
8351                         (re-search-forward match nil t))))
8352           (when (or (and found
8353                          (not reverse))
8354                     (and (not found)
8355                          reverse))
8356             (push (gnus-data-number data) articles)))))
8357     (if (not articles)
8358         (message "No messages matched")
8359       (gnus-summary-limit articles)))
8360   (gnus-summary-position-point))
8361
8362 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8363   "Limit the summary buffer to articles that aren't part on any thread.
8364 If THREADSP (the prefix), limit to articles that are in threads."
8365   (interactive "P")
8366   (let ((articles nil)
8367         thread-articles
8368         threads)
8369     (dolist (thread gnus-newsgroup-threads)
8370       (if (stringp (car thread))
8371           (dolist (thread (cdr thread))
8372             (push thread threads))
8373         (push thread threads)))
8374     (dolist (thread threads)
8375       (setq thread-articles (gnus-articles-in-thread thread))
8376       (when (or (and threadsp
8377                      (> (length thread-articles) 1))
8378                 (and (not threadsp)
8379                      (= (length thread-articles) 1)))
8380         (setq articles (nconc thread-articles articles))))
8381     (if (not articles)
8382         (message "No messages matched")
8383       (gnus-summary-limit articles))
8384     (gnus-summary-position-point)))
8385
8386 (defun gnus-summary-limit-to-replied (&optional unreplied)
8387   "Limit the summary buffer to replied articles.
8388 If UNREPLIED (the prefix), limit to unreplied articles."
8389   (interactive "P")
8390   (if unreplied
8391       (gnus-summary-limit
8392        (gnus-set-difference gnus-newsgroup-articles
8393         gnus-newsgroup-replied))
8394     (gnus-summary-limit gnus-newsgroup-replied))
8395   (gnus-summary-position-point))
8396
8397 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8398   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8399 If REVERSE, limit the summary buffer to articles that are marked
8400 with MARKS.  MARKS can either be a string of marks or a list of marks.
8401 Returns how many articles were removed."
8402   (interactive "sMarks: ")
8403   (gnus-summary-limit-to-marks marks t))
8404
8405 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8406   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8407 If REVERSE (the prefix), limit the summary buffer to articles that are
8408 not marked with MARKS.  MARKS can either be a string of marks or a
8409 list of marks.
8410 Returns how many articles were removed."
8411   (interactive "sMarks: \nP")
8412   (prog1
8413       (let ((data gnus-newsgroup-data)
8414             (marks (if (listp marks) marks
8415                      (append marks nil))) ; Transform to list.
8416             articles)
8417         (while data
8418           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8419                   (memq (gnus-data-mark (car data)) marks))
8420             (push (gnus-data-number (car data)) articles))
8421           (setq data (cdr data)))
8422         (gnus-summary-limit articles))
8423     (gnus-summary-position-point)))
8424
8425 (defun gnus-summary-limit-to-score (score)
8426   "Limit to articles with score at or above SCORE."
8427   (interactive "NLimit to articles with score of at least: ")
8428   (let ((data gnus-newsgroup-data)
8429         articles)
8430     (while data
8431       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8432                 score)
8433         (push (gnus-data-number (car data)) articles))
8434       (setq data (cdr data)))
8435     (prog1
8436         (gnus-summary-limit articles)
8437       (gnus-summary-position-point))))
8438
8439 (defun gnus-summary-limit-to-unseen ()
8440   "Limit to unseen articles."
8441   (interactive)
8442   (prog1
8443       (gnus-summary-limit gnus-newsgroup-unseen)
8444     (gnus-summary-position-point)))
8445
8446 (defun gnus-summary-limit-include-thread (id)
8447   "Display all the hidden articles that is in the thread with ID in it.
8448 When called interactively, ID is the Message-ID of the current
8449 article."
8450   (interactive (list (mail-header-id (gnus-summary-article-header))))
8451   (let ((articles (gnus-articles-in-thread
8452                    (gnus-id-to-thread (gnus-root-id id))))
8453         ;;we REALLY want the whole thread---this prevents cut-threads
8454         ;;from removing the thread we want to include.
8455         (gnus-fetch-old-headers nil)
8456         (gnus-build-sparse-threads nil))
8457     (prog1
8458         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8459       (gnus-summary-limit-include-matching-articles
8460        "subject"
8461        (regexp-quote (gnus-simplify-subject-re
8462                       (mail-header-subject (gnus-id-to-header id)))))
8463       (gnus-summary-position-point))))
8464
8465 (defun gnus-summary-limit-include-matching-articles (header regexp)
8466   "Display all the hidden articles that have HEADERs that match REGEXP."
8467   (interactive (list (read-string "Match on header: ")
8468                      (read-string "Regexp: ")))
8469   (let ((articles (gnus-find-matching-articles header regexp)))
8470     (prog1
8471         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8472       (gnus-summary-position-point))))
8473
8474 (defun gnus-summary-insert-dormant-articles ()
8475   "Insert all the dormant articles for this group into the current buffer."
8476   (interactive)
8477   (let ((gnus-verbose (max 6 gnus-verbose)))
8478     (if (not gnus-newsgroup-dormant)
8479         (gnus-message 3 "No dormant articles for this group")
8480       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8481
8482 (defun gnus-summary-insert-ticked-articles ()
8483   "Insert ticked articles for this group into the current buffer."
8484   (interactive)
8485   (let ((gnus-verbose (max 6 gnus-verbose)))
8486     (if (not gnus-newsgroup-marked)
8487         (gnus-message 3 "No ticked articles for this group")
8488       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8489
8490 (defun gnus-summary-limit-include-dormant ()
8491   "Display all the hidden articles that are marked as dormant.
8492 Note that this command only works on a subset of the articles currently
8493 fetched for this group."
8494   (interactive)
8495   (unless gnus-newsgroup-dormant
8496     (error "There are no dormant articles in this group"))
8497   (prog1
8498       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8499     (gnus-summary-position-point)))
8500
8501 (defun gnus-summary-limit-exclude-dormant ()
8502   "Hide all dormant articles."
8503   (interactive)
8504   (prog1
8505       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8506     (gnus-summary-position-point)))
8507
8508 (defun gnus-summary-limit-exclude-childless-dormant ()
8509   "Hide all dormant articles that have no children."
8510   (interactive)
8511   (let ((data (gnus-data-list t))
8512         articles d children)
8513     ;; Find all articles that are either not dormant or have
8514     ;; children.
8515     (while (setq d (pop data))
8516       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8517                 (and (setq children
8518                            (gnus-article-children (gnus-data-number d)))
8519                      (let (found)
8520                        (while children
8521                          (when (memq (car children) articles)
8522                            (setq children nil
8523                                  found t))
8524                          (pop children))
8525                        found)))
8526         (push (gnus-data-number d) articles)))
8527     ;; Do the limiting.
8528     (prog1
8529         (gnus-summary-limit articles)
8530       (gnus-summary-position-point))))
8531
8532 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8533   "Mark all unread excluded articles as read.
8534 If ALL, mark even excluded ticked and dormants as read."
8535   (interactive "P")
8536   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8537   (let ((articles (gnus-sorted-ndifference
8538                    (sort
8539                     (mapcar (lambda (h) (mail-header-number h))
8540                             gnus-newsgroup-headers)
8541                     '<)
8542                    gnus-newsgroup-limit))
8543         article)
8544     (setq gnus-newsgroup-unreads
8545           (gnus-sorted-intersection gnus-newsgroup-unreads
8546                                     gnus-newsgroup-limit))
8547     (if all
8548         (setq gnus-newsgroup-dormant nil
8549               gnus-newsgroup-marked nil
8550               gnus-newsgroup-reads
8551               (nconc
8552                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8553                gnus-newsgroup-reads))
8554       (while (setq article (pop articles))
8555         (unless (or (memq article gnus-newsgroup-dormant)
8556                     (memq article gnus-newsgroup-marked))
8557           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8558
8559 (defun gnus-summary-limit (articles &optional pop)
8560   (if pop
8561       ;; We pop the previous limit off the stack and use that.
8562       (setq articles (car gnus-newsgroup-limits)
8563             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8564     ;; We use the new limit, so we push the old limit on the stack.
8565     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8566   ;; Set the limit.
8567   (setq gnus-newsgroup-limit articles)
8568   (let ((total (length gnus-newsgroup-data))
8569         (data (gnus-data-find-list (gnus-summary-article-number)))
8570         (gnus-summary-mark-below nil)   ; Inhibit this.
8571         found)
8572     ;; This will do all the work of generating the new summary buffer
8573     ;; according to the new limit.
8574     (gnus-summary-prepare)
8575     ;; Hide any threads, possibly.
8576     (gnus-summary-maybe-hide-threads)
8577     ;; Try to return to the article you were at, or one in the
8578     ;; neighborhood.
8579     (when data
8580       ;; We try to find some article after the current one.
8581       (while data
8582         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8583           (setq data nil
8584                 found t))
8585         (setq data (cdr data))))
8586     (unless found
8587       ;; If there is no data, that means that we were after the last
8588       ;; article.  The same goes when we can't find any articles
8589       ;; after the current one.
8590       (goto-char (point-max))
8591       (gnus-summary-find-prev))
8592     (gnus-set-mode-line 'summary)
8593     ;; We return how many articles were removed from the summary
8594     ;; buffer as a result of the new limit.
8595     (- total (length gnus-newsgroup-data))))
8596
8597 (defsubst gnus-invisible-cut-children (threads)
8598   (let ((num 0))
8599     (while threads
8600       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8601         (incf num))
8602       (pop threads))
8603     (< num 2)))
8604
8605 (defsubst gnus-cut-thread (thread)
8606   "Go forwards in the thread until we find an article that we want to display."
8607   (when (or (eq gnus-fetch-old-headers 'some)
8608             (eq gnus-fetch-old-headers 'invisible)
8609             (numberp gnus-fetch-old-headers)
8610             (eq gnus-build-sparse-threads 'some)
8611             (eq gnus-build-sparse-threads 'more))
8612     ;; Deal with old-fetched headers and sparse threads.
8613     (while (and
8614             thread
8615             (or
8616              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8617              (gnus-summary-article-ancient-p
8618               (mail-header-number (car thread))))
8619             (if (or (<= (length (cdr thread)) 1)
8620                     (eq gnus-fetch-old-headers 'invisible))
8621                 (setq gnus-newsgroup-limit
8622                       (delq (mail-header-number (car thread))
8623                             gnus-newsgroup-limit)
8624                       thread (cadr thread))
8625               (when (gnus-invisible-cut-children (cdr thread))
8626                 (let ((th (cdr thread)))
8627                   (while th
8628                     (if (memq (mail-header-number (caar th))
8629                               gnus-newsgroup-limit)
8630                         (setq thread (car th)
8631                               th nil)
8632                       (setq th (cdr th))))))))))
8633   thread)
8634
8635 (defun gnus-cut-threads (threads)
8636   "Cut off all uninteresting articles from the beginning of THREADS."
8637   (when (or (eq gnus-fetch-old-headers 'some)
8638             (eq gnus-fetch-old-headers 'invisible)
8639             (numberp gnus-fetch-old-headers)
8640             (eq gnus-build-sparse-threads 'some)
8641             (eq gnus-build-sparse-threads 'more))
8642     (let ((th threads))
8643       (while th
8644         (setcar th (gnus-cut-thread (car th)))
8645         (setq th (cdr th)))))
8646   ;; Remove nixed out threads.
8647   (delq nil threads))
8648
8649 (defun gnus-summary-initial-limit (&optional show-if-empty)
8650   "Figure out what the initial limit is supposed to be on group entry.
8651 This entails weeding out unwanted dormants, low-scored articles,
8652 fetch-old-headers verbiage, and so on."
8653   ;; Most groups have nothing to remove.
8654   (unless (or gnus-inhibit-limiting
8655               (and (null gnus-newsgroup-dormant)
8656                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8657                    (not (eq gnus-fetch-old-headers 'some))
8658                    (not (numberp gnus-fetch-old-headers))
8659                    (not (eq gnus-fetch-old-headers 'invisible))
8660                    (null gnus-summary-expunge-below)
8661                    (not (eq gnus-build-sparse-threads 'some))
8662                    (not (eq gnus-build-sparse-threads 'more))
8663                    (null gnus-thread-expunge-below)))
8664     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8665     (setq gnus-newsgroup-limit nil)
8666     (mapatoms
8667      (lambda (node)
8668        (unless (car (symbol-value node))
8669          ;; These threads have no parents -- they are roots.
8670          (let ((nodes (cdr (symbol-value node)))
8671                thread)
8672            (while nodes
8673              (if (and gnus-thread-expunge-below
8674                       (< (gnus-thread-total-score (car nodes))
8675                          gnus-thread-expunge-below))
8676                  (gnus-expunge-thread (pop nodes))
8677                (setq thread (pop nodes))
8678                (gnus-summary-limit-children thread))))))
8679      gnus-newsgroup-dependencies)
8680     ;; If this limitation resulted in an empty group, we might
8681     ;; pop the previous limit and use it instead.
8682     (when (and (not gnus-newsgroup-limit)
8683                show-if-empty)
8684       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8685     gnus-newsgroup-limit))
8686
8687 (defun gnus-summary-limit-children (thread)
8688   "Return 1 if this subthread is visible and 0 if it is not."
8689   ;; First we get the number of visible children to this thread.  This
8690   ;; is done by recursing down the thread using this function, so this
8691   ;; will really go down to a leaf article first, before slowly
8692   ;; working its way up towards the root.
8693   (when thread
8694     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8695            (children
8696            (if (cdr thread)
8697                (apply '+ (mapcar 'gnus-summary-limit-children
8698                                  (cdr thread)))
8699              0))
8700            (number (mail-header-number (car thread)))
8701            score)
8702       (if (and
8703            (not (memq number gnus-newsgroup-marked))
8704            (or
8705             ;; If this article is dormant and has absolutely no visible
8706             ;; children, then this article isn't visible.
8707             (and (memq number gnus-newsgroup-dormant)
8708                  (zerop children))
8709             ;; If this is "fetch-old-headered" and there is no
8710             ;; visible children, then we don't want this article.
8711             (and (or (eq gnus-fetch-old-headers 'some)
8712                      (numberp gnus-fetch-old-headers))
8713                  (gnus-summary-article-ancient-p number)
8714                  (zerop children))
8715             ;; If this is "fetch-old-headered" and `invisible', then
8716             ;; we don't want this article.
8717             (and (eq gnus-fetch-old-headers 'invisible)
8718                  (gnus-summary-article-ancient-p number))
8719             ;; If this is a sparsely inserted article with no children,
8720             ;; we don't want it.
8721             (and (eq gnus-build-sparse-threads 'some)
8722                  (gnus-summary-article-sparse-p number)
8723                  (zerop children))
8724             ;; If we use expunging, and this article is really
8725             ;; low-scored, then we don't want this article.
8726             (when (and gnus-summary-expunge-below
8727                        (< (setq score
8728                                 (or (cdr (assq number gnus-newsgroup-scored))
8729                                     gnus-summary-default-score))
8730                           gnus-summary-expunge-below))
8731               ;; We increase the expunge-tally here, but that has
8732               ;; nothing to do with the limits, really.
8733               (incf gnus-newsgroup-expunged-tally)
8734               ;; We also mark as read here, if that's wanted.
8735               (when (and gnus-summary-mark-below
8736                          (< score gnus-summary-mark-below))
8737                 (setq gnus-newsgroup-unreads
8738                       (delq number gnus-newsgroup-unreads))
8739                 (if gnus-newsgroup-auto-expire
8740                     (push number gnus-newsgroup-expirable)
8741                   (push (cons number gnus-low-score-mark)
8742                         gnus-newsgroup-reads)))
8743               t)
8744             ;; Do the `display' group parameter.
8745             (and gnus-newsgroup-display
8746                  (let ((gnus-number number))
8747                    (not (funcall gnus-newsgroup-display))))))
8748           ;; Nope, invisible article.
8749           0
8750         ;; Ok, this article is to be visible, so we add it to the limit
8751         ;; and return 1.
8752         (push number gnus-newsgroup-limit)
8753         1))))
8754
8755 (defun gnus-expunge-thread (thread)
8756   "Mark all articles in THREAD as read."
8757   (let* ((number (mail-header-number (car thread))))
8758     (incf gnus-newsgroup-expunged-tally)
8759     ;; We also mark as read here, if that's wanted.
8760     (setq gnus-newsgroup-unreads
8761           (delq number gnus-newsgroup-unreads))
8762     (if gnus-newsgroup-auto-expire
8763         (push number gnus-newsgroup-expirable)
8764       (push (cons number gnus-low-score-mark)
8765             gnus-newsgroup-reads)))
8766   ;; Go recursively through all subthreads.
8767   (mapcar 'gnus-expunge-thread (cdr thread)))
8768
8769 ;; Summary article oriented commands
8770
8771 (defun gnus-summary-refer-parent-article (n)
8772   "Refer parent article N times.
8773 If N is negative, go to ancestor -N instead.
8774 The difference between N and the number of articles fetched is returned."
8775   (interactive "p")
8776   (let ((skip 1)
8777         error header ref)
8778     (when (not (natnump n))
8779       (setq skip (abs n)
8780             n 1))
8781     (while (and (> n 0)
8782                 (not error))
8783       (setq header (gnus-summary-article-header))
8784       (if (and (eq (mail-header-number header)
8785                    (cdr gnus-article-current))
8786                (equal gnus-newsgroup-name
8787                       (car gnus-article-current)))
8788           ;; If we try to find the parent of the currently
8789           ;; displayed article, then we take a look at the actual
8790           ;; References header, since this is slightly more
8791           ;; reliable than the References field we got from the
8792           ;; server.
8793           (with-current-buffer gnus-original-article-buffer
8794             (nnheader-narrow-to-headers)
8795             (unless (setq ref (message-fetch-field "references"))
8796               (when (setq ref (message-fetch-field "in-reply-to"))
8797                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8798             (widen))
8799         (setq ref
8800               ;; It's not the current article, so we take a bet on
8801               ;; the value we got from the server.
8802               (mail-header-references header)))
8803       (if (and ref
8804                (not (equal ref "")))
8805           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8806             (gnus-message 1 "Couldn't find parent"))
8807         (gnus-message 1 "No references in article %d"
8808                       (gnus-summary-article-number))
8809         (setq error t))
8810       (decf n))
8811     (gnus-summary-position-point)
8812     n))
8813
8814 (defun gnus-summary-refer-references ()
8815   "Fetch all articles mentioned in the References header.
8816 Return the number of articles fetched."
8817   (interactive)
8818   (let ((ref (mail-header-references (gnus-summary-article-header)))
8819         (current (gnus-summary-article-number))
8820         (n 0))
8821     (if (or (not ref)
8822             (equal ref ""))
8823         (error "No References in the current article")
8824       ;; For each Message-ID in the References header...
8825       (while (string-match "<[^>]*>" ref)
8826         (incf n)
8827         ;; ... fetch that article.
8828         (gnus-summary-refer-article
8829          (prog1 (match-string 0 ref)
8830            (setq ref (substring ref (match-end 0))))))
8831       (gnus-summary-goto-subject current)
8832       (gnus-summary-position-point)
8833       n)))
8834
8835 (defun gnus-summary-refer-thread (&optional limit)
8836   "Fetch all articles in the current thread.
8837 If no backend-specific 'request-thread function is available
8838 fetch LIMIT (the numerical prefix) old headers. If LIMIT is nil
8839 fetch what's specified by the `gnus-refer-thread-limit'
8840 variable."
8841   (interactive "P")
8842   (gnus-warp-to-article)
8843   (let ((id (mail-header-id (gnus-summary-article-header)))
8844         (gnus-inhibit-demon t)
8845         (gnus-agent nil)
8846         (gnus-summary-ignore-duplicates t)
8847         (gnus-read-all-available-headers t)
8848         (limit (if limit (prefix-numeric-value limit)
8849                  gnus-refer-thread-limit)))
8850     (setq gnus-newsgroup-headers
8851           (gnus-merge
8852            'list gnus-newsgroup-headers
8853            (if (gnus-check-backend-function
8854                 'request-thread gnus-newsgroup-name)
8855                (gnus-request-thread id)
8856              (let* ((last (if (numberp limit)
8857                               (min (+ (mail-header-number
8858                                        (gnus-summary-article-header))
8859                                       limit)
8860                                    gnus-newsgroup-highest)
8861                             gnus-newsgroup-highest))
8862                     (subject (gnus-simplify-subject
8863                               (mail-header-subject
8864                                (gnus-summary-article-header))))
8865                     (refs (split-string (or (mail-header-references
8866                                              (gnus-summary-article-header))
8867                                             "")))
8868                     (gnus-parse-headers-hook
8869                      (lambda () (goto-char (point-min))
8870                        (keep-lines
8871                         (regexp-opt (append refs (list id subject)))))))
8872                (gnus-fetch-headers (list last) (if (numberp limit)
8873                                                    (* 2 limit) limit) t)))
8874            'gnus-article-sort-by-number))
8875     (gnus-summary-limit-include-thread id)))
8876
8877 (defun gnus-summary-refer-article (message-id)
8878   "Fetch an article specified by MESSAGE-ID."
8879   (interactive "sMessage-ID: ")
8880   (when (and (stringp message-id)
8881              (not (zerop (length message-id))))
8882     (setq message-id (gnus-replace-in-string message-id " " ""))
8883     ;; Construct the correct Message-ID if necessary.
8884     ;; Suggested by tale@pawl.rpi.edu.
8885     (unless (string-match "^<" message-id)
8886       (setq message-id (concat "<" message-id)))
8887     (unless (string-match ">$" message-id)
8888       (setq message-id (concat message-id ">")))
8889     ;; People often post MIDs from URLs, so unhex it:
8890     (unless (string-match "@" message-id)
8891       (setq message-id (gnus-url-unhex-string message-id)))
8892     (let* ((header (gnus-id-to-header message-id))
8893            (sparse (and header
8894                         (gnus-summary-article-sparse-p
8895                          (mail-header-number header))
8896                         (memq (mail-header-number header)
8897                               gnus-newsgroup-limit)))
8898            number)
8899       (cond
8900        ;; If the article is present in the buffer we just go to it.
8901        ((and header
8902              (or (not (gnus-summary-article-sparse-p
8903                        (mail-header-number header)))
8904                  sparse))
8905         (prog1
8906             (gnus-summary-goto-article
8907              (mail-header-number header) nil t)
8908           (when sparse
8909             (gnus-summary-update-article (mail-header-number header)))))
8910        (t
8911         ;; We fetch the article.
8912         (catch 'found
8913           (dolist (gnus-override-method (gnus-refer-article-methods))
8914             (when (and (gnus-check-server gnus-override-method)
8915                        ;; Fetch the header,
8916                        (setq number (gnus-summary-insert-subject message-id)))
8917               ;; and display the article.
8918               (gnus-summary-select-article nil nil nil number)
8919               (throw 'found t)))
8920           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8921
8922 (defun gnus-refer-article-methods ()
8923   "Return a list of referable methods."
8924   (cond
8925    ;; No method, so we default to current and native.
8926    ((null gnus-refer-article-method)
8927     (list gnus-current-select-method gnus-select-method))
8928    ;; Current.
8929    ((eq 'current gnus-refer-article-method)
8930     (list gnus-current-select-method))
8931    ;; List of select methods.
8932    ((not (and (symbolp (car gnus-refer-article-method))
8933               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8934     (let (out)
8935       (dolist (method gnus-refer-article-method)
8936         (push (if (eq 'current method)
8937                   gnus-current-select-method
8938                 method)
8939               out))
8940       (nreverse out)))
8941    ;; One single select method.
8942    (t
8943     (list gnus-refer-article-method))))
8944
8945 (defun gnus-summary-edit-parameters ()
8946   "Edit the group parameters of the current group."
8947   (interactive)
8948   (gnus-group-edit-group gnus-newsgroup-name 'params))
8949
8950 (defun gnus-summary-customize-parameters ()
8951   "Customize the group parameters of the current group."
8952   (interactive)
8953   (gnus-group-customize gnus-newsgroup-name))
8954
8955 (defun gnus-summary-enter-digest-group (&optional force)
8956   "Enter an nndoc group based on the current article.
8957 If FORCE, force a digest interpretation.  If not, try
8958 to guess what the document format is."
8959   (interactive "P")
8960   (let ((conf gnus-current-window-configuration))
8961     (save-window-excursion
8962       (save-excursion
8963         (let (gnus-article-prepare-hook
8964               gnus-display-mime-function
8965               gnus-break-pages)
8966           (gnus-summary-select-article))))
8967     (setq gnus-current-window-configuration conf)
8968     (let* ((name (format "%s-%d"
8969                          (gnus-group-prefixed-name
8970                           gnus-newsgroup-name (list 'nndoc ""))
8971                          (with-current-buffer gnus-summary-buffer
8972                            gnus-current-article)))
8973            (ogroup gnus-newsgroup-name)
8974            (params (append (gnus-info-params (gnus-get-info ogroup))
8975                            (list (cons 'to-group ogroup))
8976                            (list (cons 'parent-group ogroup))
8977                            (list (cons 'save-article-group ogroup))))
8978            (case-fold-search t)
8979            (buf (current-buffer))
8980            dig to-address)
8981       (with-current-buffer gnus-original-article-buffer
8982         ;; Have the digest group inherit the main mail address of
8983         ;; the parent article.
8984         (when (setq to-address (or (gnus-fetch-field "reply-to")
8985                                    (gnus-fetch-field "from")))
8986           (setq params
8987                 (append
8988                  (list (cons 'to-address
8989                              (funcall gnus-decode-encoded-address-function
8990                                       to-address))))))
8991         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8992         (insert-buffer-substring gnus-original-article-buffer)
8993         ;; Remove lines that may lead nndoc to misinterpret the
8994         ;; document type.
8995         (narrow-to-region
8996          (goto-char (point-min))
8997          (or (search-forward "\n\n" nil t) (point)))
8998         (goto-char (point-min))
8999         (delete-matching-lines "^Path:\\|^From ")
9000         (widen))
9001       (unwind-protect
9002           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
9003                     (gnus-newsgroup-ephemeral-ignored-charsets
9004                      gnus-newsgroup-ignored-charsets))
9005                 (gnus-group-read-ephemeral-group
9006                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9007                               (nndoc-article-type
9008                                ,(if force 'mbox 'guess)))
9009                  t nil nil nil
9010                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9011                                                         "ADAPT")))))
9012               ;; Make all postings to this group go to the parent group.
9013               (nconc (gnus-info-params (gnus-get-info name))
9014                      params)
9015             ;; Couldn't select this doc group.
9016             (switch-to-buffer buf)
9017             (gnus-set-global-variables)
9018             (gnus-configure-windows 'summary)
9019             (gnus-message 3 "Article couldn't be entered?"))
9020         (kill-buffer dig)))))
9021
9022 (defun gnus-summary-read-document (n)
9023   "Open a new group based on the current article(s).
9024 This will allow you to read digests and other similar
9025 documents as newsgroups.
9026 Obeys the standard process/prefix convention."
9027   (interactive "P")
9028   (let* ((ogroup gnus-newsgroup-name)
9029          (params (append (gnus-info-params (gnus-get-info ogroup))
9030                          (list (cons 'to-group ogroup))))
9031          group egroup groups vgroup)
9032     (dolist (article (gnus-summary-work-articles n))
9033       (setq group (format "%s-%d" gnus-newsgroup-name article))
9034       (gnus-summary-remove-process-mark article)
9035       (when (gnus-summary-display-article article)
9036         (save-excursion ;;What for?
9037           (with-temp-buffer
9038             (insert-buffer-substring gnus-original-article-buffer)
9039             ;; Remove some headers that may lead nndoc to make
9040             ;; the wrong guess.
9041             (message-narrow-to-head)
9042             (goto-char (point-min))
9043             (delete-matching-lines "^Path:\\|^From ")
9044             (widen)
9045             (if (setq egroup
9046                       (gnus-group-read-ephemeral-group
9047                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9048                                      (nndoc-article-type guess))
9049                        t nil t))
9050                 (progn
9051                   ;; Make all postings to this group go to the parent group.
9052                   (nconc (gnus-info-params (gnus-get-info egroup))
9053                          params)
9054                   (push egroup groups))
9055               ;; Couldn't select this doc group.
9056               (gnus-error 3 "Article couldn't be entered"))))))
9057     ;; Now we have selected all the documents.
9058     (cond
9059      ((not groups)
9060       (error "None of the articles could be interpreted as documents"))
9061      ((gnus-group-read-ephemeral-group
9062        (setq vgroup (format
9063                      "nnvirtual:%s-%s" gnus-newsgroup-name
9064                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9065        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9066        t
9067        (cons (current-buffer) 'summary)))
9068      (t
9069       (error "Couldn't select virtual nndoc group")))))
9070
9071 (defun gnus-summary-isearch-article (&optional regexp-p)
9072   "Do incremental search forward on the current article.
9073 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9074   (interactive "P")
9075   (gnus-summary-select-article)
9076   (gnus-configure-windows 'article)
9077   (gnus-eval-in-buffer-window gnus-article-buffer
9078     (save-restriction
9079       (widen)
9080       (isearch-forward regexp-p))))
9081
9082 (defun gnus-summary-repeat-search-article-forward ()
9083   "Repeat the previous search forwards."
9084   (interactive)
9085   (unless gnus-last-search-regexp
9086     (error "No previous search"))
9087   (gnus-summary-search-article-forward gnus-last-search-regexp))
9088
9089 (defun gnus-summary-repeat-search-article-backward ()
9090   "Repeat the previous search backwards."
9091   (interactive)
9092   (unless gnus-last-search-regexp
9093     (error "No previous search"))
9094   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9095
9096 (defun gnus-summary-search-article-forward (regexp &optional backward)
9097   "Search for an article containing REGEXP forward.
9098 If BACKWARD, search backward instead."
9099   (interactive
9100    (list (read-string
9101           (format "Search article %s (regexp%s): "
9102                   (if current-prefix-arg "backward" "forward")
9103                   (if gnus-last-search-regexp
9104                       (concat ", default " gnus-last-search-regexp)
9105                     "")))
9106          current-prefix-arg))
9107   (if (string-equal regexp "")
9108       (setq regexp (or gnus-last-search-regexp ""))
9109     (setq gnus-last-search-regexp regexp)
9110     (setq gnus-article-before-search gnus-current-article))
9111   ;; Intentionally set gnus-last-article.
9112   (setq gnus-last-article gnus-article-before-search)
9113   (let ((gnus-last-article gnus-last-article))
9114     (if (gnus-summary-search-article regexp backward)
9115         (gnus-summary-show-thread)
9116       (signal 'search-failed (list regexp)))))
9117
9118 (defun gnus-summary-search-article-backward (regexp)
9119   "Search for an article containing REGEXP backward."
9120   (interactive
9121    (list (read-string
9122           (format "Search article backward (regexp%s): "
9123                   (if gnus-last-search-regexp
9124                       (concat ", default " gnus-last-search-regexp)
9125                     "")))))
9126   (gnus-summary-search-article-forward regexp 'backward))
9127
9128 (defun gnus-summary-search-article (regexp &optional backward)
9129   "Search for an article containing REGEXP.
9130 Optional argument BACKWARD means do search for backward.
9131 `gnus-select-article-hook' is not called during the search."
9132   ;; We have to require this here to make sure that the following
9133   ;; dynamic binding isn't shadowed by autoloading.
9134   (require 'gnus-async)
9135   (require 'gnus-art)
9136   (let ((gnus-select-article-hook nil)  ;Disable hook.
9137         (gnus-article-prepare-hook nil)
9138         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9139         (gnus-use-article-prefetch nil)
9140         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9141         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9142         (gnus-visual nil)
9143         (gnus-keep-backlog nil)
9144         (gnus-break-pages nil)
9145         (gnus-summary-display-arrow nil)
9146         (gnus-updated-mode-lines nil)
9147         (gnus-auto-center-summary nil)
9148         (sum (current-buffer))
9149         (gnus-display-mime-function nil)
9150         (found nil)
9151         point)
9152     (gnus-save-hidden-threads
9153       (gnus-summary-select-article)
9154       (set-buffer gnus-article-buffer)
9155       (goto-char (window-point (get-buffer-window (current-buffer))))
9156       (when backward
9157         (forward-line -1))
9158       (while (not found)
9159         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9160         (if (if backward
9161                 (re-search-backward regexp nil t)
9162               (re-search-forward regexp nil t))
9163             ;; We found the regexp.
9164             (progn
9165               (setq found 'found)
9166               (beginning-of-line)
9167               (set-window-start
9168                (get-buffer-window (current-buffer))
9169                (point))
9170               (forward-line 1)
9171               (set-window-point
9172                (get-buffer-window (current-buffer))
9173                (point))
9174               (set-buffer sum)
9175               (setq point (point)))
9176           ;; We didn't find it, so we go to the next article.
9177           (set-buffer sum)
9178           (setq found 'not)
9179           (while (eq found 'not)
9180             (if (not (if backward (gnus-summary-find-prev)
9181                        (gnus-summary-find-next)))
9182                 ;; No more articles.
9183                 (setq found t)
9184               ;; Select the next article and adjust point.
9185               (unless (gnus-summary-article-sparse-p
9186                        (gnus-summary-article-number))
9187                 (setq found nil)
9188                 (gnus-summary-select-article)
9189                 (set-buffer gnus-article-buffer)
9190                 (widen)
9191                 (goto-char (if backward (point-max) (point-min))))))))
9192       (gnus-message 7 ""))
9193     ;; Return whether we found the regexp.
9194     (when (eq found 'found)
9195       (goto-char point)
9196       (gnus-summary-show-thread)
9197       (gnus-summary-goto-subject gnus-current-article)
9198       (gnus-summary-position-point)
9199       t)))
9200
9201 (defun gnus-find-matching-articles (header regexp)
9202   "Return a list of all articles that match REGEXP on HEADER.
9203 This search includes all articles in the current group that Gnus has
9204 fetched headers for, whether they are displayed or not."
9205   (let ((articles nil)
9206         ;; Can't eta-reduce because it's a macro.
9207         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9208         (case-fold-search t))
9209     (dolist (header gnus-newsgroup-headers)
9210       (when (string-match regexp (funcall func header))
9211         (push (mail-header-number header) articles)))
9212     (nreverse articles)))
9213
9214 (defun gnus-summary-find-matching (header regexp &optional backward unread
9215                                           not-case-fold not-matching)
9216   "Return a list of all articles that match REGEXP on HEADER.
9217 The search stars on the current article and goes forwards unless
9218 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9219 If UNREAD is non-nil, only unread articles will
9220 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9221 in the comparisons. If NOT-MATCHING, return a list of all articles that
9222 not match REGEXP on HEADER."
9223   (let ((case-fold-search (not not-case-fold))
9224         articles d func)
9225     (if (consp header)
9226         (if (eq (car header) 'extra)
9227             (setq func
9228                   `(lambda (h)
9229                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9230                          "")))
9231           (error "%s is an invalid header" header))
9232       (unless (fboundp (intern (concat "mail-header-" header)))
9233         (error "%s is not a valid header" header))
9234       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9235     (dolist (d (if (eq backward 'all)
9236                    gnus-newsgroup-data
9237                  (gnus-data-find-list
9238                   (gnus-summary-article-number)
9239                   (gnus-data-list backward))))
9240       (when (and (or (not unread)       ; We want all articles...
9241                      (gnus-data-unread-p d)) ; Or just unreads.
9242                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9243                  (if not-matching
9244                      (not (string-match
9245                            regexp
9246                            (funcall func (gnus-data-header d))))
9247                    (string-match regexp
9248                                  (funcall func (gnus-data-header d)))))
9249         (push (gnus-data-number d) articles))) ; Success!
9250     (nreverse articles)))
9251
9252 (defun gnus-summary-execute-command (header regexp command &optional backward)
9253   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9254 If HEADER is an empty string (or nil), the match is done on the entire
9255 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9256   (interactive
9257    (list (let ((completion-ignore-case t))
9258            (gnus-completing-read
9259             "Header name"
9260             (mapcar 'symbol-name
9261                     (append
9262                      '(Number Subject From Lines Date
9263                        Message-ID Xref References Body)
9264                      gnus-extra-headers))
9265             'require-match))
9266          (read-string "Regexp: ")
9267          (read-key-sequence "Command: ")
9268          current-prefix-arg))
9269   (when (equal header "Body")
9270     (setq header ""))
9271   ;; Hidden thread subtrees must be searched as well.
9272   (gnus-summary-show-all-threads)
9273   ;; We don't want to change current point nor window configuration.
9274   (save-excursion
9275     (save-window-excursion
9276       (let (gnus-visual
9277             gnus-treat-strip-trailing-blank-lines
9278             gnus-treat-strip-leading-blank-lines
9279             gnus-treat-strip-multiple-blank-lines
9280             gnus-treat-hide-boring-headers
9281             gnus-treat-fold-newsgroups
9282             gnus-article-prepare-hook)
9283         (gnus-message 6 "Executing %s..." (key-description command))
9284         ;; We'd like to execute COMMAND interactively so as to give arguments.
9285         (gnus-execute header regexp
9286                       `(call-interactively ',(key-binding command))
9287                       backward)
9288         (gnus-message 6 "Executing %s...done" (key-description command))))))
9289
9290 (defun gnus-summary-beginning-of-article ()
9291   "Scroll the article back to the beginning."
9292   (interactive)
9293   (gnus-summary-select-article)
9294   (gnus-configure-windows 'article)
9295   (gnus-eval-in-buffer-window gnus-article-buffer
9296     (widen)
9297     (goto-char (point-min))
9298     (when gnus-break-pages
9299       (gnus-narrow-to-page))))
9300
9301 (defun gnus-summary-end-of-article ()
9302   "Scroll to the end of the article."
9303   (interactive)
9304   (gnus-summary-select-article)
9305   (gnus-configure-windows 'article)
9306   (gnus-eval-in-buffer-window gnus-article-buffer
9307     (widen)
9308     (goto-char (point-max))
9309     (recenter -3)
9310     (when gnus-break-pages
9311       (gnus-narrow-to-page))))
9312
9313 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9314   "Truncate to LEN and quote all \"(\"'s in STRING."
9315   (gnus-replace-in-string (if (and len (> (length string) len))
9316                               (substring string 0 len)
9317                             string)
9318                           "[()]" "\\\\\\&"))
9319
9320 (defun gnus-summary-print-article (&optional filename n)
9321   "Generate and print a PostScript image of the process-marked (mail) articles.
9322
9323 If used interactively, print the current article if none are
9324 process-marked.  With prefix arg, prompt the user for the name of the
9325 file to save in.
9326
9327 When used from Lisp, accept two optional args FILENAME and N.  N means
9328 to print the next N articles.  If N is negative, print the N previous
9329 articles.  If N is nil and articles have been marked with the process
9330 mark, print these instead.
9331
9332 If the optional first argument FILENAME is nil, send the image to the
9333 printer.  If FILENAME is a string, save the PostScript image in a file with
9334 that name.  If FILENAME is a number, prompt the user for the name of the file
9335 to save in."
9336   (interactive (list (ps-print-preprint current-prefix-arg)))
9337   (dolist (article (gnus-summary-work-articles n))
9338     (gnus-summary-select-article nil nil 'pseudo article)
9339     (gnus-eval-in-buffer-window gnus-article-buffer
9340       (gnus-print-buffer))
9341     (gnus-summary-remove-process-mark article))
9342   (ps-despool filename))
9343
9344 (defun gnus-print-buffer ()
9345   (let ((ps-left-header
9346          (list
9347           (concat "("
9348                   (gnus-summary-print-truncate-and-quote
9349                    (mail-header-subject gnus-current-headers)
9350                    66) ")")
9351           (concat "("
9352                   (gnus-summary-print-truncate-and-quote
9353                    (mail-header-from gnus-current-headers)
9354                    45) ")")))
9355         (ps-right-header
9356          (list
9357           "/pagenumberstring load"
9358           (concat "("
9359                   (mail-header-date gnus-current-headers) ")"))))
9360     (gnus-run-hooks 'gnus-ps-print-hook)
9361     (save-excursion
9362       (if ps-print-color-p
9363           (ps-spool-buffer-with-faces)
9364         (ps-spool-buffer)))))
9365
9366 (defun gnus-summary-show-complete-article ()
9367   "Show a complete version of the current article.
9368 This is only useful if you're looking at a partial version of the
9369 article currently."
9370   (interactive)
9371   (let ((gnus-keep-backlog nil)
9372         (gnus-use-cache nil)
9373         (gnus-agent nil)
9374         (variable (intern
9375                    (format "%s-fetch-partial-articles"
9376                            (car (gnus-find-method-for-group
9377                                  gnus-newsgroup-name)))
9378                    obarray))
9379         old-val)
9380     (unwind-protect
9381         (progn
9382           (setq old-val (symbol-value variable))
9383           (set variable nil)
9384           (gnus-flush-original-article-buffer)
9385           (gnus-summary-show-article))
9386       (set variable old-val))))
9387
9388 (defun gnus-summary-show-article (&optional arg)
9389   "Force redisplaying of the current article.
9390 If ARG (the prefix) is a number, show the article with the charset
9391 defined in `gnus-summary-show-article-charset-alist', or the charset
9392 input.
9393 If ARG (the prefix) is non-nil and not a number, show the article,
9394 but without running any of the article treatment functions
9395 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9396 C-u g', show the raw article."
9397   (interactive "P")
9398   (cond
9399    ((numberp arg)
9400     (gnus-summary-show-article t)
9401     (let ((gnus-newsgroup-charset
9402            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9403                (mm-read-coding-system
9404                 "View as charset: " ;; actually it is coding system.
9405                 (with-current-buffer gnus-article-buffer
9406                   (mm-detect-coding-region (point) (point-max))))))
9407           (gnus-newsgroup-ignored-charsets 'gnus-all))
9408       (gnus-summary-select-article nil 'force)
9409       (let ((deps gnus-newsgroup-dependencies)
9410             head header lines)
9411         (with-current-buffer gnus-original-article-buffer
9412           (save-restriction
9413             (message-narrow-to-head)
9414             (setq head (buffer-string))
9415             (goto-char (point-min))
9416             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9417               (goto-char (point-max))
9418               (widen)
9419               (setq lines (1- (count-lines (point) (point-max))))))
9420           (with-temp-buffer
9421             (insert (format "211 %d Article retrieved.\n"
9422                             (cdr gnus-article-current)))
9423             (insert head)
9424             (if lines (insert (format "Lines: %d\n" lines)))
9425             (insert ".\n")
9426             (let ((nntp-server-buffer (current-buffer)))
9427               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9428         (gnus-data-set-header
9429          (gnus-data-find (cdr gnus-article-current))
9430          header)
9431         (gnus-summary-update-article-line
9432          (cdr gnus-article-current) header)
9433         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9434           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9435    ((not arg)
9436     ;; Select the article the normal way.
9437     (gnus-summary-select-article nil 'force))
9438    ((or (equal arg '(16))
9439         (eq arg t))
9440     ;; C-u C-u g
9441     ;; We have to require this here to make sure that the following
9442     ;; dynamic binding isn't shadowed by autoloading.
9443     (require 'gnus-async)
9444     (require 'gnus-art)
9445     ;; Bind the article treatment functions to nil.
9446     (let ((gnus-have-all-headers t)
9447           gnus-article-prepare-hook
9448           gnus-article-decode-hook
9449           gnus-display-mime-function
9450           gnus-break-pages)
9451       ;; Destroy any MIME parts.
9452       (when (gnus-buffer-live-p gnus-article-buffer)
9453         (with-current-buffer gnus-article-buffer
9454           (mm-destroy-parts gnus-article-mime-handles)
9455           ;; Set it to nil for safety reason.
9456           (setq gnus-article-mime-handle-alist nil)
9457           (setq gnus-article-mime-handles nil)))
9458       (gnus-summary-select-article nil 'force)))
9459    (t
9460     (let ((gnus-inhibit-article-treatments t))
9461       (gnus-summary-select-article nil 'force))))
9462   (gnus-summary-goto-subject gnus-current-article)
9463   (gnus-summary-position-point))
9464
9465 (defun gnus-summary-show-raw-article ()
9466   "Show the raw article without any article massaging functions being run."
9467   (interactive)
9468   (gnus-summary-show-article t))
9469
9470 (defun gnus-summary-verbose-headers (&optional arg)
9471   "Toggle permanent full header display.
9472 If ARG is a positive number, turn header display on.
9473 If ARG is a negative number, turn header display off."
9474   (interactive "P")
9475   (setq gnus-show-all-headers
9476         (cond ((or (not (numberp arg))
9477                    (zerop arg))
9478                (not gnus-show-all-headers))
9479               ((natnump arg)
9480                t)))
9481   (gnus-summary-show-article))
9482
9483 (defun gnus-summary-toggle-header (&optional arg)
9484   "Show the headers if they are hidden, or hide them if they are shown.
9485 If ARG is a positive number, show the entire header.
9486 If ARG is a negative number, hide the unwanted header lines."
9487   (interactive "P")
9488   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9489                      (get-buffer-window gnus-article-buffer t))))
9490     (with-current-buffer gnus-article-buffer
9491       (widen)
9492       (article-narrow-to-head)
9493       (let* ((inhibit-read-only t)
9494              (inhibit-point-motion-hooks t)
9495              (hidden (if (numberp arg)
9496                          (>= arg 0)
9497                        (or (not (looking-at "[^ \t\n]+:"))
9498                            (gnus-article-hidden-text-p 'headers))))
9499              s e)
9500         (delete-region (point-min) (point-max))
9501         (with-current-buffer gnus-original-article-buffer
9502           (goto-char (setq s (point-min)))
9503           (setq e (if (search-forward "\n\n" nil t)
9504                       (1- (point))
9505                     (point-max))))
9506         (insert-buffer-substring gnus-original-article-buffer s e)
9507         (run-hooks 'gnus-article-decode-hook)
9508         (if hidden
9509             (let ((gnus-treat-hide-headers nil)
9510                   (gnus-treat-hide-boring-headers nil))
9511               (gnus-delete-wash-type 'headers)
9512               (gnus-treat-article 'head))
9513           (gnus-treat-article 'head))
9514         (widen)
9515         (if window
9516             (set-window-start window (goto-char (point-min))))
9517         (if gnus-break-pages
9518             (gnus-narrow-to-page)
9519           (when (gnus-visual-p 'page-marker)
9520             (let ((inhibit-read-only t))
9521               (gnus-remove-text-with-property 'gnus-prev)
9522               (gnus-remove-text-with-property 'gnus-next))))
9523         (gnus-set-mode-line 'article)))))
9524
9525 (defun gnus-summary-show-all-headers ()
9526   "Make all header lines visible."
9527   (interactive)
9528   (gnus-summary-toggle-header 1))
9529
9530 (defun gnus-summary-caesar-message (&optional arg)
9531   "Caesar rotate the current article by 13.
9532 With a non-numerical prefix, also rotate headers.  A numerical
9533 prefix specifies how many places to rotate each letter forward."
9534   (interactive "P")
9535   (gnus-summary-select-article)
9536   (let ((mail-header-separator ""))
9537     (gnus-eval-in-buffer-window gnus-article-buffer
9538       (save-restriction
9539         (widen)
9540         (let ((start (window-start))
9541               (inhibit-read-only t))
9542           (if (equal arg '(4))
9543               (message-caesar-buffer-body nil t)
9544             (message-caesar-buffer-body arg))
9545           (set-window-start (get-buffer-window (current-buffer)) start)))))
9546   ;; Create buttons and stuff...
9547   (gnus-treat-article nil))
9548
9549 (declare-function idna-to-unicode "ext:idna" (str))
9550
9551 (defun gnus-summary-idna-message (&optional arg)
9552   "Decode IDNA encoded domain names in the current articles.
9553 IDNA encoded domain names looks like `xn--bar'.  If a string
9554 remain unencoded after running this function, it is likely an
9555 invalid IDNA string (`xn--bar' is invalid).
9556
9557 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9558 installed for this command to work."
9559   (interactive "P")
9560   (if (not (and (condition-case nil (require 'idna)
9561                   (file-error))
9562                 (mm-coding-system-p 'utf-8)
9563                 (executable-find (symbol-value 'idna-program))))
9564       (gnus-message
9565        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9566     (gnus-summary-select-article)
9567     (let ((mail-header-separator ""))
9568       (gnus-eval-in-buffer-window gnus-article-buffer
9569         (save-restriction
9570           (widen)
9571           (let ((start (window-start))
9572                 buffer-read-only)
9573             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9574               (replace-match (idna-to-unicode (match-string 1))))
9575             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9576
9577 (defun gnus-summary-morse-message (&optional arg)
9578   "Morse decode the current article."
9579   (interactive "P")
9580   (gnus-summary-select-article)
9581   (let ((mail-header-separator ""))
9582     (gnus-eval-in-buffer-window gnus-article-buffer
9583       (save-excursion
9584         (save-restriction
9585           (widen)
9586           (let ((pos (window-start))
9587                 (inhibit-read-only t))
9588             (goto-char (point-min))
9589             (when (message-goto-body)
9590               (gnus-narrow-to-body))
9591             (goto-char (point-min))
9592             (while (search-forward "·" (point-max) t)
9593               (replace-match "."))
9594             (unmorse-region (point-min) (point-max))
9595             (widen)
9596             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9597
9598 (defun gnus-summary-stop-page-breaking ()
9599   "Stop page breaking in the current article."
9600   (interactive)
9601   (gnus-summary-select-article)
9602   (gnus-eval-in-buffer-window gnus-article-buffer
9603     (widen)
9604     (when (gnus-visual-p 'page-marker)
9605       (let ((inhibit-read-only t))
9606         (gnus-remove-text-with-property 'gnus-prev)
9607         (gnus-remove-text-with-property 'gnus-next))
9608       (setq gnus-page-broken nil))))
9609
9610 (defun gnus-summary-move-article (&optional n to-newsgroup
9611                                             select-method action)
9612   "Move the current article to a different newsgroup.
9613 If N is a positive number, move the N next articles.
9614 If N is a negative number, move the N previous articles.
9615 If N is nil and any articles have been marked with the process mark,
9616 move those articles instead.
9617 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9618 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9619 re-spool using this method.
9620
9621 When called interactively with TO-NEWSGROUP being nil, the value of
9622 the variable `gnus-move-split-methods' is used for finding a default
9623 for the target newsgroup.
9624
9625 For this function to work, both the current newsgroup and the
9626 newsgroup that you want to move to have to support the `request-move'
9627 and `request-accept' functions.
9628
9629 ACTION can be either `move' (the default), `crosspost' or `copy'."
9630   (interactive "P")
9631   (unless action
9632     (setq action 'move))
9633   ;; Check whether the source group supports the required functions.
9634   (cond ((and (eq action 'move)
9635               (not (gnus-check-backend-function
9636                     'request-move-article gnus-newsgroup-name)))
9637          (error "The current group does not support article moving"))
9638         ((and (eq action 'crosspost)
9639               (not (gnus-check-backend-function
9640                     'request-replace-article gnus-newsgroup-name)))
9641          (error "The current group does not support article editing")))
9642   (let ((articles (gnus-summary-work-articles n))
9643         (prefix (if (gnus-check-backend-function
9644                      'request-move-article gnus-newsgroup-name)
9645                     (funcall gnus-move-group-prefix-function
9646                              gnus-newsgroup-name)
9647                   ""))
9648         (names '((move "Move" "Moving")
9649                  (copy "Copy" "Copying")
9650                  (crosspost "Crosspost" "Crossposting")))
9651         (copy-buf (save-excursion
9652                     (nnheader-set-temp-buffer " *copy article*")))
9653         art-group to-method new-xref article to-groups
9654         articles-to-update-marks encoded)
9655     (unless (assq action names)
9656       (error "Unknown action %s" action))
9657     ;; Read the newsgroup name.
9658     (when (and (not to-newsgroup)
9659                (not select-method))
9660       (if (and gnus-move-split-methods
9661                (not
9662                 (and (memq gnus-current-article articles)
9663                      (gnus-buffer-live-p gnus-original-article-buffer))))
9664           ;; When `gnus-move-split-methods' is non-nil, we have to
9665           ;; select an article to give `gnus-read-move-group-name' an
9666           ;; opportunity to suggest an appropriate default.  However,
9667           ;; we needn't render or mark the article.
9668           (let ((gnus-display-mime-function nil)
9669                 (gnus-article-prepare-hook nil)
9670                 (gnus-mark-article-hook nil))
9671             (gnus-summary-select-article nil nil nil (car articles))))
9672       (setq to-newsgroup (gnus-read-move-group-name
9673                           (cadr (assq action names))
9674                           (symbol-value
9675                            (intern (format "gnus-current-%s-group" action)))
9676                           articles prefix)
9677             encoded to-newsgroup
9678             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9679       (set (intern (format "gnus-current-%s-group" action))
9680            (mm-decode-coding-string
9681             to-newsgroup
9682             (gnus-group-name-charset to-method to-newsgroup))))
9683     (unless to-method
9684       (setq to-method (or select-method
9685                           (gnus-server-to-method
9686                            (gnus-group-method to-newsgroup)))))
9687     (setq to-newsgroup
9688           (or encoded
9689               (and to-newsgroup
9690                    (mm-encode-coding-string
9691                     to-newsgroup
9692                     (gnus-group-name-charset to-method to-newsgroup)))))
9693     ;; Check the method we are to move this article to...
9694     (unless (gnus-check-backend-function
9695              'request-accept-article (car to-method))
9696       (error "%s does not support article copying" (car to-method)))
9697     (unless (gnus-check-server to-method)
9698       (error "Can't open server %s" (car to-method)))
9699     (gnus-message 6 "%s to %s: %s..."
9700                   (caddr (assq action names))
9701                   (or (car select-method)
9702                       (gnus-group-decoded-name to-newsgroup))
9703                   articles)
9704     (while articles
9705       (setq article (pop articles))
9706       (setq
9707        art-group
9708        (cond
9709         ;; Move the article.
9710         ((eq action 'move)
9711          ;; Remove this article from future suppression.
9712          (gnus-dup-unsuppress-article article)
9713          (let* ((from-method (gnus-find-method-for-group
9714                               gnus-newsgroup-name))
9715                 (to-method (or select-method
9716                                (gnus-find-method-for-group to-newsgroup)))
9717                 (move-is-internal (gnus-server-equal from-method to-method)))
9718            (gnus-request-move-article
9719             article                     ; Article to move
9720             gnus-newsgroup-name         ; From newsgroup
9721             (nth 1 (gnus-find-method-for-group
9722                     gnus-newsgroup-name)) ; Server
9723             (list 'gnus-request-accept-article
9724                   to-newsgroup (list 'quote select-method)
9725                   (not articles) t)     ; Accept form
9726             (not articles)              ; Only save nov last time
9727             (and move-is-internal
9728                  to-newsgroup           ; Not respooling
9729                  (gnus-group-real-name to-newsgroup))))) ; Is this move internal?
9730         ;; Copy the article.
9731         ((eq action 'copy)
9732          (with-current-buffer copy-buf
9733            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9734              (save-restriction
9735                (nnheader-narrow-to-headers)
9736                (dolist (hdr gnus-copy-article-ignored-headers)
9737                  (message-remove-header hdr t)))
9738              (gnus-request-accept-article
9739               to-newsgroup select-method (not articles) t))))
9740         ;; Crosspost the article.
9741         ((eq action 'crosspost)
9742          (let ((xref (message-tokenize-header
9743                       (mail-header-xref (gnus-summary-article-header article))
9744                       " ")))
9745            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9746                                   ":" (number-to-string article)))
9747            (unless xref
9748              (setq xref (list (system-name))))
9749            (setq new-xref
9750                  (concat
9751                   (mapconcat 'identity
9752                              (delete "Xref:" (delete new-xref xref))
9753                              " ")
9754                   " " new-xref))
9755            (with-current-buffer copy-buf
9756              ;; First put the article in the destination group.
9757              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9758              (when (consp (setq art-group
9759                                 (gnus-request-accept-article
9760                                  to-newsgroup select-method (not articles) t)))
9761                (setq new-xref (concat new-xref " " (car art-group)
9762                                       ":"
9763                                       (number-to-string (cdr art-group))))
9764                ;; Now we have the new Xrefs header, so we insert
9765                ;; it and replace the new article.
9766                (nnheader-replace-header "Xref" new-xref)
9767                (gnus-request-replace-article
9768                 (cdr art-group) to-newsgroup (current-buffer) t)
9769                art-group))))))
9770       (cond
9771        ((not art-group)
9772         (gnus-message 1 "Couldn't %s article %s: %s"
9773                       (cadr (assq action names)) article
9774                       (nnheader-get-report (car to-method))))
9775        ((eq art-group 'junk)
9776         (when (eq action 'move)
9777           (gnus-summary-mark-article article gnus-canceled-mark)
9778           (gnus-message 4 "Deleted article %s" article)
9779           ;; run the delete hook
9780           (run-hook-with-args 'gnus-summary-article-delete-hook
9781                               action
9782                               (gnus-data-header
9783                                (assoc article (gnus-data-list nil)))
9784                               gnus-newsgroup-name nil
9785                               select-method)))
9786        (t
9787         (let* ((pto-group (gnus-group-prefixed-name
9788                            (car art-group) to-method))
9789                (info (gnus-get-info pto-group))
9790                (to-group (gnus-info-group info))
9791                to-marks)
9792           ;; Update the group that has been moved to.
9793           (when (and info
9794                      (memq action '(move copy)))
9795             (unless (member to-group to-groups)
9796               (push to-group to-groups))
9797
9798             (unless (memq article gnus-newsgroup-unreads)
9799               (push 'read to-marks)
9800               (gnus-info-set-read
9801                info (gnus-add-to-range (gnus-info-read info)
9802                                        (list (cdr art-group)))))
9803
9804             ;; See whether the article is to be put in the cache.
9805             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9806                    (marks (if expirable
9807                               gnus-article-mark-lists
9808                             (delete '(expirable . expire)
9809                                     (copy-sequence gnus-article-mark-lists))))
9810                    (to-article (cdr art-group)))
9811
9812               ;; Enter the article into the cache in the new group,
9813               ;; if that is required.
9814               (when gnus-use-cache
9815                 (gnus-cache-possibly-enter-article
9816                  to-group to-article
9817                  (memq article gnus-newsgroup-marked)
9818                  (memq article gnus-newsgroup-dormant)
9819                  (memq article gnus-newsgroup-unreads)))
9820
9821               (when gnus-preserve-marks
9822                 ;; Copy any marks over to the new group.
9823                 (when (and (equal to-group gnus-newsgroup-name)
9824                            (not (memq article gnus-newsgroup-unreads)))
9825                   ;; Mark this article as read in this group.
9826                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9827                   ;; Increase the active status of this group.
9828                   (setcdr (gnus-active to-group) to-article)
9829                   (setcdr gnus-newsgroup-active to-article))
9830
9831                 (while marks
9832                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9833                     (when (memq article (symbol-value
9834                                          (intern (format "gnus-newsgroup-%s"
9835                                                          (caar marks)))))
9836                       (push (cdar marks) to-marks)
9837                       ;; If the other group is the same as this group,
9838                       ;; then we have to add the mark to the list.
9839                       (when (equal to-group gnus-newsgroup-name)
9840                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9841                              (cons to-article
9842                                    (symbol-value
9843                                     (intern (format "gnus-newsgroup-%s"
9844                                                     (caar marks)))))))
9845                       ;; Copy the marks to other group.
9846                       (gnus-add-marked-articles
9847                        to-group (cdar marks) (list to-article) info)))
9848                   (setq marks (cdr marks)))
9849
9850                 (when (and expirable
9851                            gnus-mark-copied-or-moved-articles-as-expirable
9852                            (not (memq 'expire to-marks)))
9853                   ;; Mark this article as expirable.
9854                   (push 'expire to-marks)
9855                   (when (equal to-group gnus-newsgroup-name)
9856                     (push to-article gnus-newsgroup-expirable))
9857                   ;; Copy the expirable mark to other group.
9858                   (gnus-add-marked-articles
9859                    to-group 'expire (list to-article) info))
9860
9861                 (when to-marks
9862                   (gnus-request-set-mark
9863                    to-group (list (list (list to-article) 'add to-marks)))))
9864
9865               (gnus-dribble-enter
9866                (concat "(gnus-group-set-info '"
9867                        (gnus-prin1-to-string (gnus-get-info to-group))
9868                        ")"))))
9869
9870           ;; Update the Xref header in this article to point to
9871           ;; the new crossposted article we have just created.
9872           (when (eq action 'crosspost)
9873             (with-current-buffer copy-buf
9874               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9875               (nnheader-replace-header "Xref" new-xref)
9876               (gnus-request-replace-article
9877                article gnus-newsgroup-name (current-buffer) t)))
9878
9879           ;; run the move/copy/crosspost/respool hook
9880           (run-hook-with-args 'gnus-summary-article-move-hook
9881                               action
9882                               (gnus-data-header
9883                                (assoc article (gnus-data-list nil)))
9884                               gnus-newsgroup-name
9885                               to-newsgroup
9886                               select-method))
9887
9888         ;;;!!!Why is this necessary?
9889         (set-buffer gnus-summary-buffer)
9890
9891         (when (eq action 'move)
9892           (save-excursion
9893             (gnus-summary-goto-subject article)
9894             (gnus-summary-mark-article article gnus-canceled-mark)))))
9895       (push article articles-to-update-marks))
9896
9897     (save-excursion
9898       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9899     ;; Re-activate all groups that have been moved to.
9900     (with-current-buffer gnus-group-buffer
9901       (let ((gnus-group-marked to-groups))
9902         (gnus-group-get-new-news-this-group nil t)))
9903
9904     (gnus-kill-buffer copy-buf)
9905     (gnus-summary-position-point)
9906     (gnus-set-mode-line 'summary)))
9907
9908 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9909   "Copy the current article to some other group.
9910 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9911 When called interactively, if TO-NEWSGROUP is nil, use the value of
9912 the variable `gnus-move-split-methods' for finding a default target
9913 newsgroup.
9914 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9915 re-spool using this method."
9916   (interactive "P")
9917   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9918
9919 (defun gnus-summary-crosspost-article (&optional n)
9920   "Crosspost the current article to some other group."
9921   (interactive "P")
9922   (gnus-summary-move-article n nil nil 'crosspost))
9923
9924 (defcustom gnus-summary-respool-default-method nil
9925   "Default method type for respooling an article.
9926 If nil, use to the current newsgroup method."
9927   :type 'symbol
9928   :group 'gnus-summary-mail)
9929
9930 (defun gnus-summary-respool-article (&optional n method)
9931   "Respool the current article.
9932 The article will be squeezed through the mail spooling process again,
9933 which means that it will be put in some mail newsgroup or other
9934 depending on `nnmail-split-methods'.
9935 If N is a positive number, respool the N next articles.
9936 If N is a negative number, respool the N previous articles.
9937 If N is nil and any articles have been marked with the process mark,
9938 respool those articles instead.
9939
9940 Respooling can be done both from mail groups and \"real\" newsgroups.
9941 In the former case, the articles in question will be moved from the
9942 current group into whatever groups they are destined to.  In the
9943 latter case, they will be copied into the relevant groups."
9944   (interactive
9945    (list current-prefix-arg
9946          (let* ((methods (gnus-methods-using 'respool))
9947                 (methname
9948                  (symbol-name (or gnus-summary-respool-default-method
9949                                   (car (gnus-find-method-for-group
9950                                         gnus-newsgroup-name)))))
9951                 (method
9952                  (gnus-completing-read
9953                   "Backend to use when respooling"
9954                   methods t nil 'gnus-mail-method-history methname))
9955                 ms)
9956            (cond
9957             ((zerop (length (setq ms (gnus-servers-using-backend
9958                                       (intern method)))))
9959              (list (intern method) ""))
9960             ((= 1 (length ms))
9961              (car ms))
9962             (t
9963              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9964                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
9965                            ms-alist))))))))
9966   (unless method
9967     (error "No method given for respooling"))
9968   (if (assoc (symbol-name
9969               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9970              (gnus-methods-using 'respool))
9971       (gnus-summary-move-article n nil method)
9972     (gnus-summary-copy-article n nil method)))
9973
9974 (defun gnus-summary-import-article (file &optional edit)
9975   "Import an arbitrary file into a mail newsgroup."
9976   (interactive "fImport file: \nP")
9977   (let ((group gnus-newsgroup-name)
9978         (now (current-time))
9979         atts lines group-art)
9980     (unless (gnus-check-backend-function 'request-accept-article group)
9981       (error "%s does not support article importing" group))
9982     (or (file-readable-p file)
9983         (not (file-regular-p file))
9984         (error "Can't read %s" file))
9985     (with-current-buffer (gnus-get-buffer-create " *import file*")
9986       (erase-buffer)
9987       (nnheader-insert-file-contents file)
9988       (goto-char (point-min))
9989       (if (nnheader-article-p)
9990           (save-restriction
9991             (goto-char (point-min))
9992             (search-forward "\n\n" nil t)
9993             (narrow-to-region (point-min) (1- (point)))
9994             (goto-char (point-min))
9995             (unless (re-search-forward "^date:" nil t)
9996               (goto-char (point-max))
9997               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9998        ;; This doesn't look like an article, so we fudge some headers.
9999         (setq atts (file-attributes file)
10000               lines (count-lines (point-min) (point-max)))
10001         (insert "From: " (read-string "From: ") "\n"
10002                 "Subject: " (read-string "Subject: ") "\n"
10003                 "Date: " (message-make-date (nth 5 atts)) "\n"
10004                 "Message-ID: " (message-make-message-id) "\n"
10005                 "Lines: " (int-to-string lines) "\n"
10006                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10007       (setq group-art (gnus-request-accept-article group nil t))
10008       (kill-buffer (current-buffer)))
10009     (setq gnus-newsgroup-active (gnus-activate-group group))
10010     (forward-line 1)
10011     (gnus-summary-goto-article (cdr group-art) nil t)
10012     (when edit
10013       (gnus-summary-edit-article))))
10014
10015 (defun gnus-summary-create-article ()
10016   "Create an article in a mail newsgroup."
10017   (interactive)
10018   (let ((group gnus-newsgroup-name)
10019         (now (current-time))
10020         group-art)
10021     (unless (gnus-check-backend-function 'request-accept-article group)
10022       (error "%s does not support article importing" group))
10023     (with-current-buffer (gnus-get-buffer-create " *import file*")
10024       (erase-buffer)
10025       (goto-char (point-min))
10026       ;; This doesn't look like an article, so we fudge some headers.
10027       (insert "From: " (read-string "From: ") "\n"
10028               "Subject: " (read-string "Subject: ") "\n"
10029               "Date: " (message-make-date now) "\n"
10030               "Message-ID: " (message-make-message-id) "\n")
10031       (setq group-art (gnus-request-accept-article group nil t))
10032       (kill-buffer (current-buffer)))
10033     (setq gnus-newsgroup-active (gnus-activate-group group))
10034     (forward-line 1)
10035     (gnus-summary-goto-article (cdr group-art) nil t)
10036     (gnus-summary-edit-article)))
10037
10038 (defun gnus-summary-article-posted-p ()
10039   "Say whether the current (mail) article is available from news as well.
10040 This will be the case if the article has both been mailed and posted."
10041   (interactive)
10042   (let ((id (mail-header-references (gnus-summary-article-header)))
10043         (gnus-override-method (car (gnus-refer-article-methods))))
10044     (if (gnus-request-head id "")
10045         (gnus-message 2 "The current message was found on %s"
10046                       gnus-override-method)
10047       (gnus-message 2 "The current message couldn't be found on %s"
10048                     gnus-override-method)
10049       nil)))
10050
10051 (defun gnus-summary-expire-articles (&optional now)
10052   "Expire all articles that are marked as expirable in the current group."
10053   (interactive)
10054   (when (and (not gnus-group-is-exiting-without-update-p)
10055              (gnus-check-backend-function
10056               'request-expire-articles gnus-newsgroup-name))
10057     ;; This backend supports expiry.
10058     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10059            (expirable (if total
10060                           (progn
10061                             ;; We need to update the info for
10062                             ;; this group for `gnus-list-of-read-articles'
10063                             ;; to give us the right answer.
10064                             (gnus-run-hooks 'gnus-exit-group-hook)
10065                             (gnus-summary-update-info)
10066                             (gnus-list-of-read-articles gnus-newsgroup-name))
10067                         (setq gnus-newsgroup-expirable
10068                               (sort gnus-newsgroup-expirable '<))))
10069            (expiry-wait (if now 'immediate
10070                           (gnus-group-find-parameter
10071                            gnus-newsgroup-name 'expiry-wait)))
10072            (nnmail-expiry-target
10073             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10074                 nnmail-expiry-target))
10075            es)
10076       (when expirable
10077         ;; There are expirable articles in this group, so we run them
10078         ;; through the expiry process.
10079         (gnus-message 6 "Expiring articles...")
10080         (unless (gnus-check-group gnus-newsgroup-name)
10081           (error "Can't open server for %s" gnus-newsgroup-name))
10082         ;; The list of articles that weren't expired is returned.
10083         (save-excursion
10084           (if expiry-wait
10085               (let ((nnmail-expiry-wait-function nil)
10086                     (nnmail-expiry-wait expiry-wait))
10087                 (setq es (gnus-request-expire-articles
10088                           expirable gnus-newsgroup-name)))
10089             (setq es (gnus-request-expire-articles
10090                       expirable gnus-newsgroup-name)))
10091           (unless total
10092             (setq gnus-newsgroup-expirable es))
10093           ;; We go through the old list of expirable, and mark all
10094           ;; really expired articles as nonexistent.
10095           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10096             (let ((gnus-use-cache nil))
10097               (dolist (article expirable)
10098                 (when (and (not (memq article es))
10099                            (gnus-data-find article))
10100                   (gnus-summary-mark-article article gnus-canceled-mark)
10101                   (run-hook-with-args 'gnus-summary-article-expire-hook
10102                                       'delete
10103                                       (gnus-data-header
10104                                        (assoc article (gnus-data-list nil)))
10105                                       gnus-newsgroup-name
10106                                       nil
10107                                       nil))))))
10108         (gnus-message 6 "Expiring articles...done")))))
10109
10110 (defun gnus-summary-expire-articles-now ()
10111   "Expunge all expirable articles in the current group.
10112 This means that *all* articles that are marked as expirable will be
10113 deleted forever, right now."
10114   (interactive)
10115   (or gnus-expert-user
10116       (gnus-yes-or-no-p
10117        "Are you really, really sure you want to delete all expirable messages? ")
10118       (error "Phew!"))
10119   (gnus-summary-expire-articles t))
10120
10121 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10122 (defun gnus-summary-delete-article (&optional n)
10123   "Delete the N next (mail) articles.
10124 This command actually deletes articles.  This is not a marking
10125 command.  The article will disappear forever from your life, never to
10126 return.
10127
10128 If N is negative, delete backwards.
10129 If N is nil and articles have been marked with the process mark,
10130 delete these instead.
10131
10132 If `gnus-novice-user' is non-nil you will be asked for
10133 confirmation before the articles are deleted."
10134   (interactive "P")
10135   (unless (gnus-check-backend-function 'request-expire-articles
10136                                        gnus-newsgroup-name)
10137     (error "The current newsgroup does not support article deletion"))
10138   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10139     (error "Couldn't open server"))
10140   ;; Compute the list of articles to delete.
10141   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10142         (nnmail-expiry-target 'delete)
10143         not-deleted)
10144     (if (and gnus-novice-user
10145              (not (gnus-yes-or-no-p
10146                    (format "Do you really want to delete %s forever? "
10147                            (if (> (length articles) 1)
10148                                (format "these %s articles" (length articles))
10149                              "this article")))))
10150         ()
10151       ;; Delete the articles.
10152       (setq not-deleted (gnus-request-expire-articles
10153                          articles gnus-newsgroup-name 'force))
10154       (save-excursion
10155         (while articles
10156           (gnus-summary-remove-process-mark (car articles))
10157           ;; The backend might not have been able to delete the article
10158           ;; after all.
10159           (unless (memq (car articles) not-deleted)
10160             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10161           (let* ((article (car articles))
10162                  (ghead  (gnus-data-header
10163                           (assoc article (gnus-data-list nil)))))
10164             (run-hook-with-args 'gnus-summary-article-delete-hook
10165                                 'delete ghead gnus-newsgroup-name nil
10166                                 nil))
10167           (setq articles (cdr articles))))
10168       (when not-deleted
10169         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10170     (gnus-summary-position-point)
10171     (gnus-set-mode-line 'summary)
10172     not-deleted))
10173
10174 (defun gnus-summary-edit-article (&optional arg)
10175   "Edit the current article.
10176 This will have permanent effect only in mail groups.
10177 If ARG is nil, edit the decoded articles.
10178 If ARG is 1, edit the raw articles.
10179 If ARG is 2, edit the raw articles even in read-only groups.
10180 If ARG is 3, edit the articles with the current handles.
10181 Otherwise, allow editing of articles even in read-only
10182 groups."
10183   (interactive "P")
10184   (let (force raw current-handles)
10185     (cond
10186      ((null arg))
10187      ((eq arg 1)
10188       (setq raw t))
10189      ((eq arg 2)
10190       (setq raw t
10191             force t))
10192      ((eq arg 3)
10193       (setq current-handles
10194             (and (gnus-buffer-live-p gnus-article-buffer)
10195                  (with-current-buffer gnus-article-buffer
10196                    (prog1
10197                        gnus-article-mime-handles
10198                      (setq gnus-article-mime-handles nil))))))
10199      (t
10200       (setq force t)))
10201     (when (and raw (not force)
10202                (member gnus-newsgroup-name '("nndraft:delayed"
10203                                              "nndraft:drafts"
10204                                              "nndraft:queue")))
10205       (error "Can't edit the raw article in group %s"
10206              gnus-newsgroup-name))
10207     (with-current-buffer gnus-summary-buffer
10208       (let ((mail-parse-charset gnus-newsgroup-charset)
10209             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10210         (gnus-set-global-variables)
10211         (when (and (not force)
10212                    (gnus-group-read-only-p))
10213           (error "The current newsgroup does not support article editing"))
10214         (gnus-summary-show-article t)
10215         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10216           (with-current-buffer gnus-article-buffer
10217             (mm-enable-multibyte)))
10218         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10219             (setq raw t))
10220         (gnus-article-edit-article
10221          (if raw 'ignore
10222            `(lambda ()
10223               (let ((mbl mml-buffer-list))
10224                 (setq mml-buffer-list nil)
10225                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10226                   (mime-to-mml ,'current-handles))
10227                 (let ((mbl1 mml-buffer-list))
10228                   (setq mml-buffer-list mbl)
10229                   (set (make-local-variable 'mml-buffer-list) mbl1))
10230                 (gnus-make-local-hook 'kill-buffer-hook)
10231                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10232          `(lambda (no-highlight)
10233             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10234                   (message-options message-options)
10235                   (message-options-set-recipient)
10236                   (mail-parse-ignored-charsets
10237                    ',gnus-newsgroup-ignored-charsets)
10238                   (rfc2047-header-encoding-alist
10239                    ',(let ((charset (gnus-group-name-charset
10240                                      (gnus-find-method-for-group
10241                                       gnus-newsgroup-name)
10242                                      gnus-newsgroup-name)))
10243                        (append (list (cons "Newsgroups" charset)
10244                                      (cons "Followup-To" charset)
10245                                      (cons "Xref" charset))
10246                                rfc2047-header-encoding-alist))))
10247               ,(if (not raw) '(progn
10248                                 (mml-to-mime)
10249                                 (mml-destroy-buffers)
10250                                 (remove-hook 'kill-buffer-hook
10251                                              'mml-destroy-buffers t)
10252                                 (kill-local-variable 'mml-buffer-list)))
10253               (gnus-summary-edit-article-done
10254                ,(or (mail-header-references gnus-current-headers) "")
10255                ,(gnus-group-read-only-p)
10256                ,gnus-summary-buffer no-highlight))))))))
10257
10258 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10259
10260 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10261                                                  no-highlight)
10262   "Make edits to the current article permanent."
10263   (interactive)
10264   (save-excursion
10265     ;; The buffer restriction contains the entire article if it exists.
10266     (when (article-goto-body)
10267       (let ((lines (count-lines (point) (point-max)))
10268             (length (- (point-max) (point)))
10269             (case-fold-search t)
10270             (body (copy-marker (point))))
10271         (goto-char (point-min))
10272         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10273           (delete-region (match-beginning 1) (match-end 1))
10274           (insert (number-to-string length)))
10275         (goto-char (point-min))
10276         (when (re-search-forward
10277                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10278           (delete-region (match-beginning 1) (match-end 1))
10279           (insert (number-to-string length)))
10280         (goto-char (point-min))
10281         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10282           (delete-region (match-beginning 1) (match-end 1))
10283           (insert (number-to-string lines))))))
10284   ;; Replace the article.
10285   (let ((buf (current-buffer))
10286         (article (cdr gnus-article-current))
10287         replace-result)
10288     (with-temp-buffer
10289       (insert-buffer-substring buf)
10290       (if (and (not read-only)
10291                (not (setq replace-result
10292                           (gnus-request-replace-article
10293                            article (car gnus-article-current)
10294                            (current-buffer) t))))
10295           (error "Couldn't replace article")
10296         ;; If we got a number back, then that's the new article number
10297         ;; for this article.  Otherwise, the article number didn't change.
10298         (when (numberp replace-result)
10299           (with-current-buffer gnus-summary-buffer
10300             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10301             (gnus-summary-limit gnus-newsgroup-limit)
10302             (setq article replace-result)
10303             (gnus-summary-goto-subject article t)))
10304         ;; Update the summary buffer.
10305         (if (and references
10306                  (equal (message-tokenize-header references " ")
10307                         (message-tokenize-header
10308                          (or (message-fetch-field "references") "") " ")))
10309             ;; We only have to update this line.
10310             (save-excursion
10311               (save-restriction
10312                 (message-narrow-to-head)
10313                 (let ((head (buffer-substring-no-properties
10314                              (point-min) (point-max)))
10315                       header)
10316                   (with-temp-buffer
10317                     (insert (format "211 %d Article retrieved.\n" article))
10318                     (insert head)
10319                     (insert ".\n")
10320                     (let ((nntp-server-buffer (current-buffer)))
10321                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10322                     (with-current-buffer gnus-summary-buffer
10323                       (gnus-data-set-header (gnus-data-find article) header)
10324                       (gnus-summary-update-article-line article header)
10325                       (if (gnus-summary-goto-subject article nil t)
10326                           (gnus-summary-update-secondary-mark article)))))))
10327           ;; Update threads.
10328           (set-buffer (or buffer gnus-summary-buffer))
10329           (gnus-summary-update-article article)
10330           (if (gnus-summary-goto-subject article nil t)
10331               (gnus-summary-update-secondary-mark article)))
10332         ;; Prettify the article buffer again.
10333         (unless no-highlight
10334           (with-current-buffer gnus-article-buffer
10335             ;;!!! Fix this -- article should be rehighlighted.
10336             ;;(gnus-run-hooks 'gnus-article-display-hook)
10337             (set-buffer gnus-original-article-buffer)
10338             (gnus-request-article
10339              article (car gnus-article-current) (current-buffer))))
10340         ;; Prettify the summary buffer line.
10341         (when (gnus-visual-p 'summary-highlight 'highlight)
10342           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10343
10344 (defun gnus-summary-edit-wash (key)
10345   "Perform editing command KEY in the article buffer."
10346   (interactive
10347    (list
10348     (progn
10349       (message "%s" (concat (this-command-keys) "- "))
10350       (read-char))))
10351   (message "")
10352   (gnus-summary-edit-article)
10353   (execute-kbd-macro (concat (this-command-keys) key))
10354   (gnus-article-edit-done))
10355
10356 ;;; Respooling
10357
10358 (defun gnus-summary-respool-query (&optional silent trace)
10359   "Query where the respool algorithm would put this article."
10360   (interactive)
10361   (let (gnus-mark-article-hook)
10362     (gnus-summary-select-article)
10363     (with-current-buffer gnus-original-article-buffer
10364       (let ((groups (nnmail-article-group 'identity trace)))
10365         (unless silent
10366           (if groups
10367               (message "This message would go to %s"
10368                        (mapconcat 'car groups ", "))
10369             (message "This message would go to no groups"))
10370           groups)))))
10371
10372 (defun gnus-summary-respool-trace ()
10373   "Trace where the respool algorithm would put this article.
10374 Display a buffer showing all fancy splitting patterns which matched."
10375   (interactive)
10376   (gnus-summary-respool-query nil t))
10377
10378 ;; Summary marking commands.
10379
10380 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10381   "Mark articles which has the same subject as read, and then select the next.
10382 If UNMARK is positive, remove any kind of mark.
10383 If UNMARK is negative, tick articles."
10384   (interactive "P")
10385   (when unmark
10386     (setq unmark (prefix-numeric-value unmark)))
10387   (let ((count
10388          (gnus-summary-mark-same-subject
10389           (gnus-summary-article-subject) unmark)))
10390     ;; Select next unread article.  If auto-select-same mode, should
10391     ;; select the first unread article.
10392     (gnus-summary-next-article t (and gnus-auto-select-same
10393                                       (gnus-summary-article-subject)))
10394     (gnus-message 7 "%d article%s marked as %s"
10395                   count (if (= count 1) " is" "s are")
10396                   (if unmark "unread" "read"))))
10397
10398 (defun gnus-summary-kill-same-subject (&optional unmark)
10399   "Mark articles which has the same subject as read.
10400 If UNMARK is positive, remove any kind of mark.
10401 If UNMARK is negative, tick articles."
10402   (interactive "P")
10403   (when unmark
10404     (setq unmark (prefix-numeric-value unmark)))
10405   (let ((count
10406          (gnus-summary-mark-same-subject
10407           (gnus-summary-article-subject) unmark)))
10408     ;; If marked as read, go to next unread subject.
10409     (when (null unmark)
10410       ;; Go to next unread subject.
10411       (gnus-summary-next-subject 1 t))
10412     (gnus-message 7 "%d articles are marked as %s"
10413                   count (if unmark "unread" "read"))))
10414
10415 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10416   "Mark articles with same SUBJECT as read, and return marked number.
10417 If optional argument UNMARK is positive, remove any kinds of marks.
10418 If optional argument UNMARK is negative, mark articles as unread instead."
10419   (let ((count 1))
10420     (save-excursion
10421       (cond
10422        ((null unmark)                   ; Mark as read.
10423         (while (and
10424                 (progn
10425                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10426                   (gnus-summary-show-thread) t)
10427                 (gnus-summary-find-subject subject))
10428           (setq count (1+ count))))
10429        ((> unmark 0)                    ; Tick.
10430         (while (and
10431                 (progn
10432                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10433                   (gnus-summary-show-thread) t)
10434                 (gnus-summary-find-subject subject))
10435           (setq count (1+ count))))
10436        (t                               ; Mark as unread.
10437         (while (and
10438                 (progn
10439                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10440                   (gnus-summary-show-thread) t)
10441                 (gnus-summary-find-subject subject))
10442           (setq count (1+ count)))))
10443       (gnus-set-mode-line 'summary)
10444       ;; Return the number of marked articles.
10445       count)))
10446
10447 (defun gnus-summary-mark-as-processable (n &optional unmark)
10448   "Set the process mark on the next N articles.
10449 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10450 the process mark instead.  The difference between N and the actual
10451 number of articles marked is returned."
10452   (interactive "P")
10453   (if (and (null n) (gnus-region-active-p))
10454       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10455     (setq n (prefix-numeric-value n))
10456     (let ((backward (< n 0))
10457           (n (abs n)))
10458       (while (and
10459               (> n 0)
10460               (if unmark
10461                   (gnus-summary-remove-process-mark
10462                    (gnus-summary-article-number))
10463                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10464               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10465         (setq n (1- n)))
10466       (when (/= 0 n)
10467         (gnus-message 7 "No more articles"))
10468       (gnus-summary-recenter)
10469       (gnus-summary-position-point)
10470       n)))
10471
10472 (defun gnus-summary-unmark-as-processable (n)
10473   "Remove the process mark from the next N articles.
10474 If N is negative, unmark backward instead.  The difference between N and
10475 the actual number of articles unmarked is returned."
10476   (interactive "P")
10477   (gnus-summary-mark-as-processable n t))
10478
10479 (defun gnus-summary-unmark-all-processable ()
10480   "Remove the process mark from all articles."
10481   (interactive)
10482   (save-excursion
10483     (while gnus-newsgroup-processable
10484       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10485   (gnus-summary-position-point))
10486
10487 (defun gnus-summary-add-mark (article type)
10488   "Mark ARTICLE with a mark of TYPE."
10489   (let ((vtype (car (assq type gnus-article-mark-lists)))
10490         var)
10491     (if (not vtype)
10492         (error "No such mark type: %s" type)
10493       (setq var (intern (format "gnus-newsgroup-%s" type)))
10494       (set var (cons article (symbol-value var)))
10495       (if (memq type '(processable cached replied forwarded recent saved))
10496           (gnus-summary-update-secondary-mark article)
10497         ;;; !!! This is bogus.  We should find out what primary
10498         ;;; !!! mark we want to set.
10499         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10500
10501 (defun gnus-summary-mark-as-expirable (n)
10502   "Mark N articles forward as expirable.
10503 If N is negative, mark backward instead.  The difference between N and
10504 the actual number of articles marked is returned."
10505   (interactive "p")
10506   (gnus-summary-mark-forward n gnus-expirable-mark))
10507
10508 (defun gnus-summary-mark-as-spam (n)
10509   "Mark N articles forward as spam.
10510 If N is negative, mark backward instead.  The difference between N and
10511 the actual number of articles marked is returned."
10512   (interactive "p")
10513   (gnus-summary-mark-forward n gnus-spam-mark))
10514
10515 (defun gnus-summary-mark-article-as-replied (article)
10516   "Mark ARTICLE as replied to and update the summary line.
10517 ARTICLE can also be a list of articles."
10518   (interactive (list (gnus-summary-article-number)))
10519   (let ((articles (if (listp article) article (list article))))
10520     (dolist (article articles)
10521       (unless (numberp article)
10522         (error "%s is not a number" article))
10523       (push article gnus-newsgroup-replied)
10524       (let ((inhibit-read-only t))
10525         (when (gnus-summary-goto-subject article nil t)
10526           (gnus-summary-update-secondary-mark article))))))
10527
10528 (defun gnus-summary-mark-article-as-forwarded (article)
10529   "Mark ARTICLE as forwarded and update the summary line.
10530 ARTICLE can also be a list of articles."
10531   (let ((articles (if (listp article) article (list article))))
10532     (dolist (article articles)
10533       (push article gnus-newsgroup-forwarded)
10534       (let ((inhibit-read-only t))
10535         (when (gnus-summary-goto-subject article nil t)
10536           (gnus-summary-update-secondary-mark article))))))
10537
10538 (defun gnus-summary-set-bookmark (article)
10539   "Set a bookmark in current article."
10540   (interactive (list (gnus-summary-article-number)))
10541   (when (or (not (get-buffer gnus-article-buffer))
10542             (not gnus-current-article)
10543             (not gnus-article-current)
10544             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10545     (error "No current article selected"))
10546   ;; Remove old bookmark, if one exists.
10547   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10548   ;; Set the new bookmark, which is on the form
10549   ;; (article-number . line-number-in-body).
10550   (push
10551    (cons article
10552          (with-current-buffer gnus-article-buffer
10553            (count-lines
10554             (min (point)
10555                  (save-excursion
10556                    (article-goto-body)
10557                    (point)))
10558             (point))))
10559    gnus-newsgroup-bookmarks)
10560   (gnus-message 6 "A bookmark has been added to the current article."))
10561
10562 (defun gnus-summary-remove-bookmark (article)
10563   "Remove the bookmark from the current article."
10564   (interactive (list (gnus-summary-article-number)))
10565   ;; Remove old bookmark, if one exists.
10566   (if (not (assq article gnus-newsgroup-bookmarks))
10567       (gnus-message 6 "No bookmark in current article.")
10568     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10569     (gnus-message 6 "Removed bookmark.")))
10570
10571 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10572 (defun gnus-summary-mark-as-dormant (n)
10573   "Mark N articles forward as dormant.
10574 If N is negative, mark backward instead.  The difference between N and
10575 the actual number of articles marked is returned."
10576   (interactive "p")
10577   (gnus-summary-mark-forward n gnus-dormant-mark))
10578
10579 (defun gnus-summary-set-process-mark (article)
10580   "Set the process mark on ARTICLE and update the summary line."
10581   (setq gnus-newsgroup-processable
10582         (cons article
10583               (delq article gnus-newsgroup-processable)))
10584   (when (gnus-summary-goto-subject article)
10585     (gnus-summary-show-thread)
10586     (gnus-summary-goto-subject article)
10587     (gnus-summary-update-secondary-mark article)))
10588
10589 (defun gnus-summary-remove-process-mark (&rest articles)
10590   "Remove the process mark from ARTICLES and update the summary line."
10591   (dolist (article articles)
10592     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10593     (when (gnus-summary-goto-subject article)
10594       (gnus-summary-show-thread)
10595       (gnus-summary-goto-subject article)
10596       (gnus-summary-update-secondary-mark article)))
10597   t)
10598
10599 (defun gnus-summary-set-saved-mark (article)
10600   "Set the process mark on ARTICLE and update the summary line."
10601   (push article gnus-newsgroup-saved)
10602   (when (gnus-summary-goto-subject article)
10603     (gnus-summary-update-secondary-mark article)))
10604
10605 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10606   "Mark N articles as read forwards.
10607 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10608 The difference between N and the actual number of articles marked is
10609 returned.
10610 If NO-EXPIRE, auto-expiry will be inhibited."
10611   (interactive "p")
10612   (gnus-summary-show-thread)
10613   (let ((backward (< n 0))
10614         (gnus-summary-goto-unread
10615          (and gnus-summary-goto-unread
10616               (not (eq gnus-summary-goto-unread 'never))
10617               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10618                                     gnus-ticked-mark gnus-dormant-mark)))))
10619         (n (abs n))
10620         (mark (or mark gnus-del-mark)))
10621     (while (and (> n 0)
10622                 (gnus-summary-mark-article nil mark no-expire)
10623                 (zerop (gnus-summary-next-subject
10624                         (if backward -1 1)
10625                         (and gnus-summary-goto-unread
10626                              (not (eq gnus-summary-goto-unread 'never)))
10627                         t)))
10628       (setq n (1- n)))
10629     (when (/= 0 n)
10630       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10631     (gnus-summary-recenter)
10632     (gnus-summary-position-point)
10633     (gnus-set-mode-line 'summary)
10634     n))
10635
10636 (defun gnus-summary-mark-article-as-read (mark)
10637   "Mark the current article quickly as read with MARK."
10638   (let ((article (gnus-summary-article-number)))
10639     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10640     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10641     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10642     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10643     (push (cons article mark) gnus-newsgroup-reads)
10644     ;; Possibly remove from cache, if that is used.
10645     (when gnus-use-cache
10646       (gnus-cache-enter-remove-article article))
10647     ;; Allow the backend to change the mark.
10648     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10649     ;; Check for auto-expiry.
10650     (when (and gnus-newsgroup-auto-expire
10651                (memq mark gnus-auto-expirable-marks))
10652       (setq mark gnus-expirable-mark)
10653       ;; Let the backend know about the mark change.
10654       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10655       (push article gnus-newsgroup-expirable))
10656     ;; Set the mark in the buffer.
10657     (gnus-summary-update-mark mark 'unread)
10658     t))
10659
10660 (defun gnus-summary-mark-article-as-unread (mark)
10661   "Mark the current article quickly as unread with MARK."
10662   (let* ((article (gnus-summary-article-number))
10663          (old-mark (gnus-summary-article-mark article)))
10664     ;; Allow the backend to change the mark.
10665     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10666     (if (eq mark old-mark)
10667         t
10668       (if (<= article 0)
10669           (progn
10670             (gnus-error 1 "Can't mark negative article numbers")
10671             nil)
10672         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10673         (setq gnus-newsgroup-spam-marked
10674               (delq article gnus-newsgroup-spam-marked))
10675         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10676         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10677         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10678         (cond ((= mark gnus-ticked-mark)
10679                (setq gnus-newsgroup-marked
10680                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10681                                               article)))
10682               ((= mark gnus-spam-mark)
10683                (setq gnus-newsgroup-spam-marked
10684                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10685                                               article)))
10686               ((= mark gnus-dormant-mark)
10687                (setq gnus-newsgroup-dormant
10688                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10689                                               article)))
10690               (t
10691                (setq gnus-newsgroup-unreads
10692                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10693                                               article))))
10694         (gnus-alist-pull article gnus-newsgroup-reads)
10695
10696         ;; See whether the article is to be put in the cache.
10697         (and gnus-use-cache
10698              (vectorp (gnus-summary-article-header article))
10699              (save-excursion
10700                (gnus-cache-possibly-enter-article
10701                 gnus-newsgroup-name article
10702                 (= mark gnus-ticked-mark)
10703                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10704
10705         ;; Fix the mark.
10706         (gnus-summary-update-mark mark 'unread)
10707         t))))
10708
10709 (defun gnus-summary-mark-article (&optional article mark no-expire)
10710   "Mark ARTICLE with MARK.  MARK can be any character.
10711 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10712 `??' (dormant) and `?E' (expirable).
10713 If MARK is nil, then the default character `?r' is used.
10714 If ARTICLE is nil, then the article on the current line will be
10715 marked.
10716 If NO-EXPIRE, auto-expiry will be inhibited."
10717   ;; The mark might be a string.
10718   (when (stringp mark)
10719     (setq mark (aref mark 0)))
10720   ;; If no mark is given, then we check auto-expiring.
10721   (when (null mark)
10722     (setq mark gnus-del-mark))
10723   (when (and (not no-expire)
10724              gnus-newsgroup-auto-expire
10725              (memq mark gnus-auto-expirable-marks))
10726     (setq mark gnus-expirable-mark))
10727   (let ((article (or article (gnus-summary-article-number)))
10728         (old-mark (gnus-summary-article-mark article)))
10729     ;; Allow the backend to change the mark.
10730     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10731     (if (eq mark old-mark)
10732         t
10733       (unless article
10734         (error "No article on current line"))
10735       (if (not (if (or (= mark gnus-unread-mark)
10736                        (= mark gnus-ticked-mark)
10737                        (= mark gnus-spam-mark)
10738                        (= mark gnus-dormant-mark))
10739                    (gnus-mark-article-as-unread article mark)
10740                  (gnus-mark-article-as-read article mark)))
10741           t
10742         ;; See whether the article is to be put in the cache.
10743         (and gnus-use-cache
10744              (not (= mark gnus-canceled-mark))
10745              (vectorp (gnus-summary-article-header article))
10746              (save-excursion
10747                (gnus-cache-possibly-enter-article
10748                 gnus-newsgroup-name article
10749                 (= mark gnus-ticked-mark)
10750                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10751
10752         (when (gnus-summary-goto-subject article nil t)
10753           (let ((inhibit-read-only t))
10754             (gnus-summary-show-thread)
10755             ;; Fix the mark.
10756             (gnus-summary-update-mark mark 'unread)
10757             t))))))
10758
10759 (defun gnus-summary-update-secondary-mark (article)
10760   "Update the secondary (read, process, cache) mark."
10761   (gnus-summary-update-mark
10762    (cond ((memq article gnus-newsgroup-processable)
10763           gnus-process-mark)
10764          ((memq article gnus-newsgroup-cached)
10765           gnus-cached-mark)
10766          ((memq article gnus-newsgroup-replied)
10767           gnus-replied-mark)
10768          ((memq article gnus-newsgroup-forwarded)
10769           gnus-forwarded-mark)
10770          ((memq article gnus-newsgroup-saved)
10771           gnus-saved-mark)
10772          ((memq article gnus-newsgroup-recent)
10773           gnus-recent-mark)
10774          ((memq article gnus-newsgroup-unseen)
10775           gnus-unseen-mark)
10776          (t gnus-no-mark))
10777    'replied)
10778   (when (gnus-visual-p 'summary-highlight 'highlight)
10779     (gnus-summary-highlight-line)
10780     (gnus-run-hooks 'gnus-summary-update-hook))
10781   t)
10782
10783 (defun gnus-summary-update-download-mark (article)
10784   "Update the download mark."
10785   (gnus-summary-update-mark
10786    (cond ((memq article gnus-newsgroup-undownloaded)
10787           gnus-undownloaded-mark)
10788          (gnus-newsgroup-agentized
10789           gnus-downloaded-mark)
10790          (t
10791           gnus-no-mark))
10792    'download)
10793   (gnus-summary-update-line t)
10794   t)
10795
10796 (defun gnus-summary-update-mark (mark type)
10797   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10798         (inhibit-read-only t))
10799     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10800     (when forward
10801       (when (looking-at "\r")
10802         (incf forward))
10803       (when (<= (+ forward (point)) (point-max))
10804         ;; Go to the right position on the line.
10805         (goto-char (+ forward (point)))
10806         ;; Replace the old mark with the new mark.
10807         (let ((to-insert
10808                (mm-subst-char-in-string
10809                 (char-after) mark
10810                 (buffer-substring (point) (1+ (point))))))
10811           (delete-region (point) (1+ (point)))
10812           (insert to-insert))
10813         ;; Optionally update the marks by some user rule.
10814         (when (eq type 'unread)
10815           (gnus-data-set-mark
10816            (gnus-data-find (gnus-summary-article-number)) mark)
10817           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10818
10819 (defun gnus-mark-article-as-read (article &optional mark)
10820   "Enter ARTICLE in the pertinent lists and remove it from others."
10821   ;; Make the article expirable.
10822   (let ((mark (or mark gnus-del-mark)))
10823     (setq gnus-newsgroup-expirable
10824           (if (= mark gnus-expirable-mark)
10825               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10826             (delq article gnus-newsgroup-expirable)))
10827     ;; Remove from unread and marked lists.
10828     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10829     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10830     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10831     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10832     (push (cons article mark) gnus-newsgroup-reads)
10833     ;; Possibly remove from cache, if that is used.
10834     (when gnus-use-cache
10835       (gnus-cache-enter-remove-article article))
10836     t))
10837
10838 (defun gnus-mark-article-as-unread (article &optional mark)
10839   "Enter ARTICLE in the pertinent lists and remove it from others."
10840   (let ((mark (or mark gnus-ticked-mark)))
10841     (if (<= article 0)
10842         (progn
10843           (gnus-error 1 "Can't mark negative article numbers")
10844           nil)
10845       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10846             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10847             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10848             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10849             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10850
10851       ;; Unsuppress duplicates?
10852       (when gnus-suppress-duplicates
10853         (gnus-dup-unsuppress-article article))
10854
10855       (cond ((= mark gnus-ticked-mark)
10856              (setq gnus-newsgroup-marked
10857                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10858             ((= mark gnus-spam-mark)
10859              (setq gnus-newsgroup-spam-marked
10860                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10861                                             article)))
10862             ((= mark gnus-dormant-mark)
10863              (setq gnus-newsgroup-dormant
10864                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10865             (t
10866              (setq gnus-newsgroup-unreads
10867                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10868       (gnus-alist-pull article gnus-newsgroup-reads)
10869       t)))
10870
10871 (defun gnus-summary-tick-article-forward (n)
10872   "Tick N articles forwards.
10873 If N is negative, tick backwards instead.
10874 The difference between N and the number of articles ticked is returned."
10875   (interactive "p")
10876   (gnus-summary-mark-forward n gnus-ticked-mark))
10877
10878 (defun gnus-summary-tick-article-backward (n)
10879   "Tick N articles backwards.
10880 The difference between N and the number of articles ticked is returned."
10881   (interactive "p")
10882   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10883
10884 (defun gnus-summary-tick-article (&optional article clear-mark)
10885   "Mark current article as unread.
10886 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10887 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10888   (interactive)
10889   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10890                                        gnus-ticked-mark)))
10891
10892 (defun gnus-summary-mark-as-read-forward (n)
10893   "Mark N articles as read forwards.
10894 If N is negative, mark backwards instead.
10895 The difference between N and the actual number of articles marked is
10896 returned."
10897   (interactive "p")
10898   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10899
10900 (defun gnus-summary-mark-as-read-backward (n)
10901   "Mark the N articles as read backwards.
10902 The difference between N and the actual number of articles marked is
10903 returned."
10904   (interactive "p")
10905   (gnus-summary-mark-forward
10906    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10907
10908 (defun gnus-summary-mark-as-read (&optional article mark)
10909   "Mark current article as read.
10910 ARTICLE specifies the article to be marked as read.
10911 MARK specifies a string to be inserted at the beginning of the line."
10912   (gnus-summary-mark-article article mark))
10913
10914 (defun gnus-summary-clear-mark-forward (n)
10915   "Clear marks from N articles forward.
10916 If N is negative, clear backward instead.
10917 The difference between N and the number of marks cleared is returned."
10918   (interactive "p")
10919   (gnus-summary-mark-forward n gnus-unread-mark))
10920
10921 (defun gnus-summary-clear-mark-backward (n)
10922   "Clear marks from N articles backward.
10923 The difference between N and the number of marks cleared is returned."
10924   (interactive "p")
10925   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10926
10927 (defun gnus-summary-mark-unread-as-read ()
10928   "Intended to be used by `gnus-mark-article-hook'."
10929   (when (memq gnus-current-article gnus-newsgroup-unreads)
10930     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10931
10932 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10933   "Intended to be used by `gnus-mark-article-hook'."
10934   (let ((mark (gnus-summary-article-mark)))
10935     (when (or (gnus-unread-mark-p mark)
10936               (gnus-read-mark-p mark))
10937       (gnus-summary-mark-article gnus-current-article
10938                                  (or new-mark gnus-read-mark)))))
10939
10940 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10941   "Intended to be used by `gnus-mark-article-hook'."
10942   (let ((mark (gnus-summary-article-mark)))
10943     (when (or (gnus-unread-mark-p mark)
10944               (gnus-read-mark-p mark))
10945       (gnus-summary-mark-article (gnus-summary-article-number)
10946                                  (or new-mark gnus-read-mark)))))
10947
10948 (defun gnus-summary-mark-unread-as-ticked ()
10949   "Intended to be used by `gnus-mark-article-hook'."
10950   (when (memq gnus-current-article gnus-newsgroup-unreads)
10951     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10952
10953 (defun gnus-summary-mark-region-as-read (point mark all)
10954   "Mark all unread articles between point and mark as read.
10955 If given a prefix, mark all articles between point and mark as read,
10956 even ticked and dormant ones."
10957   (interactive "r\nP")
10958   (save-excursion
10959     (let (article)
10960       (goto-char point)
10961       (beginning-of-line)
10962       (while (and
10963               (< (point) mark)
10964               (progn
10965                 (when (or all
10966                           (memq (setq article (gnus-summary-article-number))
10967                                 gnus-newsgroup-unreads))
10968                   (gnus-summary-mark-article article gnus-del-mark))
10969                 t)
10970               (gnus-summary-find-next))))))
10971
10972 (defun gnus-summary-mark-below (score mark)
10973   "Mark articles with score less than SCORE with MARK."
10974   (interactive "P\ncMark: ")
10975   (setq score (if score
10976                   (prefix-numeric-value score)
10977                 (or gnus-summary-default-score 0)))
10978   (with-current-buffer gnus-summary-buffer
10979     (goto-char (point-min))
10980     (while
10981         (progn
10982           (and (< (gnus-summary-article-score) score)
10983                (gnus-summary-mark-article nil mark))
10984           (gnus-summary-find-next)))))
10985
10986 (defun gnus-summary-kill-below (&optional score)
10987   "Mark articles with score below SCORE as read."
10988   (interactive "P")
10989   (gnus-summary-mark-below score gnus-killed-mark))
10990
10991 (defun gnus-summary-clear-above (&optional score)
10992   "Clear all marks from articles with score above SCORE."
10993   (interactive "P")
10994   (gnus-summary-mark-above score gnus-unread-mark))
10995
10996 (defun gnus-summary-tick-above (&optional score)
10997   "Tick all articles with score above SCORE."
10998   (interactive "P")
10999   (gnus-summary-mark-above score gnus-ticked-mark))
11000
11001 (defun gnus-summary-mark-above (score mark)
11002   "Mark articles with score over SCORE with MARK."
11003   (interactive "P\ncMark: ")
11004   (setq score (if score
11005                   (prefix-numeric-value score)
11006                 (or gnus-summary-default-score 0)))
11007   (with-current-buffer gnus-summary-buffer
11008     (goto-char (point-min))
11009     (while (and (progn
11010                   (when (> (gnus-summary-article-score) score)
11011                     (gnus-summary-mark-article nil mark))
11012                   t)
11013                 (gnus-summary-find-next)))))
11014
11015 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11016 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11017 (defun gnus-summary-limit-include-expunged (&optional no-error)
11018   "Display all the hidden articles that were expunged for low scores."
11019   (interactive)
11020   (let ((inhibit-read-only t))
11021     (let ((scored gnus-newsgroup-scored)
11022           headers h)
11023       (while scored
11024         (unless (gnus-summary-article-header (caar scored))
11025           (and (setq h (gnus-number-to-header (caar scored)))
11026                (< (cdar scored) gnus-summary-expunge-below)
11027                (push h headers)))
11028         (setq scored (cdr scored)))
11029       (if (not headers)
11030           (when (not no-error)
11031             (error "No expunged articles hidden"))
11032         (goto-char (point-min))
11033         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11034         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11035         (dolist (x headers)
11036           (push (mail-header-number x) gnus-newsgroup-limit))
11037         (gnus-summary-prepare-unthreaded (nreverse headers))
11038         (goto-char (point-min))
11039         (gnus-summary-position-point)
11040         t))))
11041
11042 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11043   "Mark all unread articles in this newsgroup as read.
11044 If prefix argument ALL is non-nil, ticked and dormant articles will
11045 also be marked as read.
11046 If QUIETLY is non-nil, no questions will be asked.
11047
11048 If TO-HERE is non-nil, it should be a point in the buffer.  All
11049 articles before (after, if REVERSE is set) this point will be marked
11050 as read.
11051
11052 Note that this function will only catch up the unread article
11053 in the current summary buffer limitation.
11054
11055 The number of articles marked as read is returned."
11056   (interactive "P")
11057   (prog1
11058       (save-excursion
11059         (when (or quietly
11060                   (not gnus-interactive-catchup) ;Without confirmation?
11061                   gnus-expert-user
11062                   (gnus-y-or-n-p
11063                    (if all
11064                        "Mark absolutely all articles as read? "
11065                      "Mark all unread articles as read? ")))
11066           (if (and not-mark
11067                    (not gnus-newsgroup-adaptive)
11068                    (not gnus-newsgroup-auto-expire)
11069                    (not gnus-suppress-duplicates)
11070                    (or (not gnus-use-cache)
11071                        (eq gnus-use-cache 'passive)))
11072               (progn
11073                 (when all
11074                   (setq gnus-newsgroup-marked nil
11075                         gnus-newsgroup-spam-marked nil
11076                         gnus-newsgroup-dormant nil))
11077                 (setq gnus-newsgroup-unreads
11078                       (gnus-sorted-nunion
11079                        (gnus-sorted-intersection gnus-newsgroup-unreads
11080                                                  gnus-newsgroup-downloadable)
11081                        (gnus-sorted-difference gnus-newsgroup-unfetched
11082                                                gnus-newsgroup-cached))))
11083             ;; We actually mark all articles as canceled, which we
11084             ;; have to do when using auto-expiry or adaptive scoring.
11085             (gnus-summary-show-all-threads)
11086             (if (and to-here reverse)
11087                 (progn
11088                   (goto-char to-here)
11089                   (gnus-summary-mark-current-read-and-unread-as-read
11090                    gnus-catchup-mark)
11091                   (while (gnus-summary-find-next (not all))
11092                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11093               (when (gnus-summary-first-subject (not all))
11094                 (while (and
11095                         (if to-here (< (point) to-here) t)
11096                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11097                         (gnus-summary-find-next (not all))))))
11098             (gnus-set-mode-line 'summary))
11099           t))
11100     (gnus-summary-position-point)))
11101
11102 (defun gnus-summary-catchup-to-here (&optional all)
11103   "Mark all unticked articles before the current one as read.
11104 If ALL is non-nil, also mark ticked and dormant articles as read."
11105   (interactive "P")
11106   (save-excursion
11107     (gnus-save-hidden-threads
11108       (let ((beg (point)))
11109         ;; We check that there are unread articles.
11110         (when (or all (gnus-summary-find-prev))
11111           (gnus-summary-catchup all t beg)))))
11112   (gnus-summary-position-point))
11113
11114 (defun gnus-summary-catchup-from-here (&optional all)
11115   "Mark all unticked articles after (and including) the current one as read.
11116 If ALL is non-nil, also mark ticked and dormant articles as read."
11117   (interactive "P")
11118   (save-excursion
11119     (gnus-save-hidden-threads
11120       (let ((beg (point)))
11121         ;; We check that there are unread articles.
11122         (when (or all (gnus-summary-find-next))
11123           (gnus-summary-catchup all t beg nil t)))))
11124   (gnus-summary-position-point))
11125
11126 (defun gnus-summary-catchup-all (&optional quietly)
11127   "Mark all articles in this newsgroup as read.
11128 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11129 instead, which marks only unread articles as read."
11130   (interactive "P")
11131   (gnus-summary-catchup t quietly))
11132
11133 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11134   "Mark all unread articles in this group as read, then exit.
11135 If prefix argument ALL is non-nil, all articles are marked as read.
11136 If QUIETLY is non-nil, no questions will be asked."
11137   (interactive "P")
11138   (when (gnus-summary-catchup all quietly nil 'fast)
11139     ;; Select next newsgroup or exit.
11140     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11141              (eq gnus-auto-select-next 'quietly))
11142         (gnus-summary-next-group nil)
11143       (gnus-summary-exit))))
11144
11145 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11146   "Mark all articles in this newsgroup as read, and then exit.
11147 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11148 instead, which marks only unread articles as read."
11149   (interactive "P")
11150   (gnus-summary-catchup-and-exit t quietly))
11151
11152 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11153   "Mark all articles in this group as read and select the next group.
11154 If given a prefix, mark all articles, unread as well as ticked, as
11155 read."
11156   (interactive "P")
11157   (save-excursion
11158     (gnus-summary-catchup all))
11159   (gnus-summary-next-group))
11160
11161 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11162   "Mark all articles in this group as read and select the previous group.
11163 If given a prefix, mark all articles, unread as well as ticked, as
11164 read."
11165   (interactive "P")
11166   (save-excursion
11167     (gnus-summary-catchup all))
11168   (gnus-summary-next-group nil nil t))
11169
11170 ;;;
11171 ;;; with article
11172 ;;;
11173
11174 (defmacro gnus-with-article (article &rest forms)
11175   "Select ARTICLE and perform FORMS in the original article buffer.
11176 Then replace the article with the result."
11177   `(progn
11178      ;; We don't want the article to be marked as read.
11179      (let (gnus-mark-article-hook)
11180        (gnus-summary-select-article t t nil ,article))
11181      (set-buffer gnus-original-article-buffer)
11182      ,@forms
11183      (if (not (gnus-check-backend-function
11184                'request-replace-article (car gnus-article-current)))
11185          (gnus-message 5 "Read-only group; not replacing")
11186        (unless (gnus-request-replace-article
11187                 ,article (car gnus-article-current)
11188                 (current-buffer) t)
11189          (error "Couldn't replace article")))
11190      ;; The cache and backlog have to be flushed somewhat.
11191      (when gnus-keep-backlog
11192        (gnus-backlog-remove-article
11193         (car gnus-article-current) (cdr gnus-article-current)))
11194      (when gnus-use-cache
11195        (gnus-cache-update-article
11196         (car gnus-article-current) (cdr gnus-article-current)))))
11197
11198 (put 'gnus-with-article 'lisp-indent-function 1)
11199 (put 'gnus-with-article 'edebug-form-spec '(form body))
11200
11201 ;; Thread-based commands.
11202
11203 (defun gnus-summary-articles-in-thread (&optional article)
11204   "Return a list of all articles in the current thread.
11205 If ARTICLE is non-nil, return all articles in the thread that starts
11206 with that article."
11207   (let* ((article (or article (gnus-summary-article-number)))
11208          (data (gnus-data-find-list article))
11209          (top-level (gnus-data-level (car data)))
11210          (top-subject
11211           (cond ((null gnus-thread-operation-ignore-subject)
11212                  (gnus-simplify-subject-re
11213                   (mail-header-subject (gnus-data-header (car data)))))
11214                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11215                  (gnus-simplify-subject-fuzzy
11216                   (mail-header-subject (gnus-data-header (car data)))))
11217                 (t nil)))
11218          (end-point (save-excursion
11219                       (if (gnus-summary-go-to-next-thread)
11220                           (point) (point-max))))
11221          articles)
11222     (while (and data
11223                 (< (gnus-data-pos (car data)) end-point))
11224       (when (or (not top-subject)
11225                 (string= top-subject
11226                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11227                              (gnus-simplify-subject-fuzzy
11228                               (mail-header-subject
11229                                (gnus-data-header (car data))))
11230                            (gnus-simplify-subject-re
11231                             (mail-header-subject
11232                              (gnus-data-header (car data)))))))
11233         (push (gnus-data-number (car data)) articles))
11234       (unless (and (setq data (cdr data))
11235                    (> (gnus-data-level (car data)) top-level))
11236         (setq data nil)))
11237     ;; Return the list of articles.
11238     (nreverse articles)))
11239
11240 (defun gnus-summary-rethread-current ()
11241   "Rethread the thread the current article is part of."
11242   (interactive)
11243   (let* ((gnus-show-threads t)
11244          (article (gnus-summary-article-number))
11245          (id (mail-header-id (gnus-summary-article-header)))
11246          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11247     (unless id
11248       (error "No article on the current line"))
11249     (gnus-rebuild-thread id)
11250     (gnus-summary-goto-subject article)))
11251
11252 (defun gnus-summary-reparent-thread ()
11253   "Make the current article child of the marked (or previous) article.
11254
11255 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11256 is non-nil or the Subject: of both articles are the same."
11257   (interactive)
11258   (unless (not (gnus-group-read-only-p))
11259     (error "The current newsgroup does not support article editing"))
11260   (unless (<= (length gnus-newsgroup-processable) 1)
11261     (error "No more than one article may be marked"))
11262   (let ((child (gnus-summary-article-number))
11263         ;; First grab the marked article, otherwise one line up.
11264         (parent (if (not (null gnus-newsgroup-processable))
11265                     (car gnus-newsgroup-processable)
11266                   (save-excursion
11267                     (if (eq (forward-line -1) 0)
11268                         (gnus-summary-article-number)
11269                       (error "Beginning of summary buffer"))))))
11270     (gnus-summary-reparent-children parent (list child))))
11271
11272 (defun gnus-summary-reparent-children (parent children)
11273   "Make PARENT the parent of CHILDREN.
11274 When called interactively, PARENT is the current article and CHILDREN
11275 are the process-marked articles."
11276   (interactive
11277    (list (gnus-summary-article-number)
11278          (gnus-summary-work-articles nil)))
11279   (dolist (child children)
11280     (save-window-excursion
11281       (let ((gnus-article-buffer " *reparent*"))
11282         (unless (not (eq parent child))
11283           (error "An article may not be self-referential"))
11284         (let ((message-id (mail-header-id
11285                            (gnus-summary-article-header parent))))
11286           (unless (and message-id (not (equal message-id "")))
11287             (error "No message-id in desired parent"))
11288           (gnus-with-article child
11289             (save-restriction
11290               (goto-char (point-min))
11291               (message-narrow-to-head)
11292               (if (re-search-forward "^References: " nil t)
11293                   (progn
11294                     (re-search-forward "^[^ \t]" nil t)
11295                     (forward-line -1)
11296                     (end-of-line)
11297                     (insert " " message-id))
11298                 (insert "References: " message-id "\n"))))
11299           (set-buffer gnus-summary-buffer)
11300           (gnus-summary-unmark-all-processable)
11301           (gnus-summary-update-article child)
11302           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11303             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11304           (gnus-summary-rethread-current)
11305           (gnus-message 3 "Article %d is now the child of article %d"
11306                         child parent))))))
11307
11308 (defun gnus-summary-toggle-threads (&optional arg)
11309   "Toggle showing conversation threads.
11310 If ARG is positive number, turn showing conversation threads on."
11311   (interactive "P")
11312   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11313     (setq gnus-show-threads
11314           (if (null arg) (not gnus-show-threads)
11315             (> (prefix-numeric-value arg) 0)))
11316     (gnus-summary-prepare)
11317     (gnus-summary-goto-subject current)
11318     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11319     (gnus-summary-position-point)))
11320
11321 (eval-and-compile
11322   (if (fboundp 'remove-overlays)
11323       (defalias 'gnus-remove-overlays 'remove-overlays)
11324     (defun gnus-remove-overlays (beg end name val)
11325       "Clear BEG and END of overlays whose property NAME has value VAL.
11326 For compatibility with XEmacs."
11327       (dolist (ov (gnus-overlays-in beg end))
11328         (when (eq (gnus-overlay-get ov name) val)
11329           (gnus-delete-overlay ov))))))
11330
11331 (defun gnus-summary-show-all-threads ()
11332   "Show all threads."
11333   (interactive)
11334   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11335   (gnus-summary-position-point))
11336
11337 (defsubst gnus-summary--inv (p)
11338   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11339
11340 (defun gnus-summary-show-thread ()
11341   "Show thread subtrees.
11342 Returns nil if no thread was there to be shown."
11343   (interactive)
11344   (let* ((orig (point))
11345          (end (point-at-eol))
11346          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11347          ;; Leave point at bol
11348          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11349          (eoi (when end
11350                 (if (fboundp 'next-single-char-property-change)
11351                     (or (next-single-char-property-change end 'invisible)
11352                         (point-max))
11353                   (while (progn
11354                            (end-of-line 2)
11355                            (and (not (eobp))
11356                                 (eq (get-char-property (point) 'invisible)
11357                                     'gnus-sum))))
11358                   (point)))))
11359     (when eoi
11360       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11361       (goto-char orig)
11362       (gnus-summary-position-point)
11363       eoi)))
11364
11365 (defun gnus-summary-maybe-hide-threads ()
11366   "If requested, hide the threads that should be hidden."
11367   (when (and gnus-show-threads
11368              gnus-thread-hide-subtree)
11369     (gnus-summary-hide-all-threads
11370      (if (or (consp gnus-thread-hide-subtree)
11371              (functionp gnus-thread-hide-subtree))
11372          (gnus-make-predicate gnus-thread-hide-subtree)
11373        nil))))
11374
11375 ;;; Hiding predicates.
11376
11377 (defun gnus-article-unread-p (header)
11378   (memq (mail-header-number header) gnus-newsgroup-unreads))
11379
11380 (defun gnus-article-unseen-p (header)
11381   (memq (mail-header-number header) gnus-newsgroup-unseen))
11382
11383 (defun gnus-map-articles (predicate articles)
11384   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11385   (apply 'gnus-or (mapcar predicate
11386                           (mapcar (lambda (number)
11387                                     (gnus-summary-article-header number))
11388                                   articles))))
11389
11390 (defun gnus-summary-hide-all-threads (&optional predicate)
11391   "Hide all thread subtrees.
11392 If PREDICATE is supplied, threads that satisfy this predicate
11393 will not be hidden."
11394   (interactive)
11395   (save-excursion
11396     (goto-char (point-min))
11397     (let ((end nil))
11398       (while (not end)
11399         (when (or (not predicate)
11400                   (gnus-map-articles
11401                    predicate (gnus-summary-article-children)))
11402             (gnus-summary-hide-thread))
11403         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11404   (gnus-summary-position-point))
11405
11406 (defun gnus-summary-hide-thread ()
11407   "Hide thread subtrees.
11408 If PREDICATE is supplied, threads that satisfy this predicate
11409 will not be hidden.
11410 Returns nil if no threads were there to be hidden."
11411   (interactive)
11412   (let ((start (point))
11413         (starteol (line-end-position))
11414         (article (gnus-summary-article-number)))
11415     (goto-char start)
11416     ;; Go forward until either the buffer ends or the subthread ends.
11417     (when (and (not (eobp))
11418                (or (zerop (gnus-summary-next-thread 1 t))
11419                    (goto-char (point-max))))
11420       (if (and (> (point) start)
11421                ;; FIXME: this should actually search for a non-invisible \n.
11422                (search-backward "\n" start t))
11423           (progn
11424             (when (> (point) starteol)
11425               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11426               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11427                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11428                 (gnus-overlay-put ol 'evaporate t)))
11429             (gnus-summary-goto-subject article))
11430         (goto-char start)
11431         nil))))
11432
11433 (defun gnus-summary-go-to-next-thread (&optional previous)
11434   "Go to the same level (or less) next thread.
11435 If PREVIOUS is non-nil, go to previous thread instead.
11436 Return the article number moved to, or nil if moving was impossible."
11437   (let ((level (gnus-summary-thread-level))
11438         (way (if previous -1 1))
11439         (beg (point)))
11440     (forward-line way)
11441     (while (and (not (eobp))
11442                 (< level (gnus-summary-thread-level)))
11443       (forward-line way))
11444     (if (eobp)
11445         (progn
11446           (goto-char beg)
11447           nil)
11448       (setq beg (point))
11449       (prog1
11450           (gnus-summary-article-number)
11451         (goto-char beg)))))
11452
11453 (defun gnus-summary-next-thread (n &optional silent)
11454   "Go to the same level next N'th thread.
11455 If N is negative, search backward instead.
11456 Returns the difference between N and the number of skips actually
11457 done.
11458
11459 If SILENT, don't output messages."
11460   (interactive "p")
11461   (let ((backward (< n 0))
11462         (n (abs n)))
11463     (while (and (> n 0)
11464                 (gnus-summary-go-to-next-thread backward))
11465       (decf n))
11466     (unless silent
11467       (gnus-summary-position-point))
11468     (when (and (not silent) (/= 0 n))
11469       (gnus-message 7 "No more threads"))
11470     n))
11471
11472 (defun gnus-summary-prev-thread (n)
11473   "Go to the same level previous N'th thread.
11474 Returns the difference between N and the number of skips actually
11475 done."
11476   (interactive "p")
11477   (gnus-summary-next-thread (- n)))
11478
11479 (defun gnus-summary-go-down-thread ()
11480   "Go down one level in the current thread."
11481   (let ((children (gnus-summary-article-children)))
11482     (when children
11483       (gnus-summary-goto-subject (car children)))))
11484
11485 (defun gnus-summary-go-up-thread ()
11486   "Go up one level in the current thread."
11487   (let ((parent (gnus-summary-article-parent)))
11488     (when parent
11489       (gnus-summary-goto-subject parent))))
11490
11491 (defun gnus-summary-down-thread (n)
11492   "Go down thread N steps.
11493 If N is negative, go up instead.
11494 Returns the difference between N and how many steps down that were
11495 taken."
11496   (interactive "p")
11497   (let ((up (< n 0))
11498         (n (abs n)))
11499     (while (and (> n 0)
11500                 (if up (gnus-summary-go-up-thread)
11501                   (gnus-summary-go-down-thread)))
11502       (setq n (1- n)))
11503     (gnus-summary-position-point)
11504     (when (/= 0 n)
11505       (gnus-message 7 "Can't go further"))
11506     n))
11507
11508 (defun gnus-summary-up-thread (n)
11509   "Go up thread N steps.
11510 If N is negative, go down instead.
11511 Returns the difference between N and how many steps down that were
11512 taken."
11513   (interactive "p")
11514   (gnus-summary-down-thread (- n)))
11515
11516 (defun gnus-summary-top-thread ()
11517   "Go to the top of the thread."
11518   (interactive)
11519   (while (gnus-summary-go-up-thread))
11520   (gnus-summary-article-number))
11521
11522 (defun gnus-summary-expire-thread ()
11523   "Mark articles under current thread as expired."
11524   (interactive)
11525   (gnus-summary-kill-thread 0))
11526
11527 (defun gnus-summary-kill-thread (&optional unmark)
11528   "Mark articles under current thread as read.
11529 If the prefix argument is positive, remove any kinds of marks.
11530 If the prefix argument is zero, mark thread as expired.
11531 If the prefix argument is negative, tick articles instead."
11532   (interactive "P")
11533   (when unmark
11534     (setq unmark (prefix-numeric-value unmark)))
11535   (let ((articles (gnus-summary-articles-in-thread))
11536         (hide (or (null unmark) (= unmark 0))))
11537     (save-excursion
11538       ;; Expand the thread.
11539       (gnus-summary-show-thread)
11540       ;; Mark all the articles.
11541       (while articles
11542         (gnus-summary-goto-subject (car articles))
11543         (cond ((null unmark)
11544                (gnus-summary-mark-article-as-read gnus-killed-mark))
11545               ((> unmark 0)
11546                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11547               ((= unmark 0)
11548                (gnus-summary-mark-article nil gnus-expirable-mark))
11549               (t
11550                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11551         (setq articles (cdr articles))))
11552     ;; Hide killed subtrees when hide is true.
11553     (and hide
11554          gnus-thread-hide-killed
11555          (gnus-summary-hide-thread))
11556     ;; If hide is t, go to next unread subject.
11557     (when hide
11558       ;; Go to next unread subject.
11559       (gnus-summary-next-subject 1 t)))
11560   (gnus-set-mode-line 'summary))
11561
11562 ;; Summary sorting commands
11563
11564 (defun gnus-summary-sort-by-number (&optional reverse)
11565   "Sort the summary buffer by article number.
11566 Argument REVERSE means reverse order."
11567   (interactive "P")
11568   (gnus-summary-sort 'number reverse))
11569
11570 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11571   "Sort the summary buffer by most recent article number.
11572 Argument REVERSE means reverse order."
11573   (interactive "P")
11574   (gnus-summary-sort 'most-recent-number reverse))
11575
11576 (defun gnus-summary-sort-by-random (&optional reverse)
11577   "Randomize the order in the summary buffer.
11578 Argument REVERSE means to randomize in reverse order."
11579   (interactive "P")
11580   (gnus-summary-sort 'random reverse))
11581
11582 (defun gnus-summary-sort-by-author (&optional reverse)
11583   "Sort the summary buffer by author name alphabetically.
11584 If `case-fold-search' is non-nil, case of letters is ignored.
11585 Argument REVERSE means reverse order."
11586   (interactive "P")
11587   (gnus-summary-sort 'author reverse))
11588
11589 (defun gnus-summary-sort-by-recipient (&optional reverse)
11590   "Sort the summary buffer by recipient name alphabetically.
11591 If `case-fold-search' is non-nil, case of letters is ignored.
11592 Argument REVERSE means reverse order."
11593   (interactive "P")
11594   (gnus-summary-sort 'recipient reverse))
11595
11596 (defun gnus-summary-sort-by-subject (&optional reverse)
11597   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11598 If `case-fold-search' is non-nil, case of letters is ignored.
11599 Argument REVERSE means reverse order."
11600   (interactive "P")
11601   (gnus-summary-sort 'subject reverse))
11602
11603 (defun gnus-summary-sort-by-date (&optional reverse)
11604   "Sort the summary buffer by date.
11605 Argument REVERSE means reverse order."
11606   (interactive "P")
11607   (gnus-summary-sort 'date reverse))
11608
11609 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11610   "Sort the summary buffer by most recent date.
11611 Argument REVERSE means reverse order."
11612   (interactive "P")
11613   (gnus-summary-sort 'most-recent-date reverse))
11614
11615 (defun gnus-summary-sort-by-score (&optional reverse)
11616   "Sort the summary buffer by score.
11617 Argument REVERSE means reverse order."
11618   (interactive "P")
11619   (gnus-summary-sort 'score reverse))
11620
11621 (defun gnus-summary-sort-by-lines (&optional reverse)
11622   "Sort the summary buffer by the number of lines.
11623 Argument REVERSE means reverse order."
11624   (interactive "P")
11625   (gnus-summary-sort 'lines reverse))
11626
11627 (defun gnus-summary-sort-by-chars (&optional reverse)
11628   "Sort the summary buffer by article length.
11629 Argument REVERSE means reverse order."
11630   (interactive "P")
11631   (gnus-summary-sort 'chars reverse))
11632
11633 (defun gnus-summary-sort-by-original (&optional reverse)
11634   "Sort the summary buffer using the default sorting method.
11635 Argument REVERSE means reverse order."
11636   (interactive "P")
11637   (let* ((inhibit-read-only t)
11638          (gnus-summary-prepare-hook nil))
11639     ;; We do the sorting by regenerating the threads.
11640     (gnus-summary-prepare)
11641     ;; Hide subthreads if needed.
11642     (gnus-summary-maybe-hide-threads)))
11643
11644 (defun gnus-summary-sort (predicate reverse)
11645   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11646   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11647          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11648          (gnus-thread-sort-functions
11649           (if (not reverse)
11650               thread
11651             `(lambda (t1 t2)
11652                (,thread t2 t1))))
11653          (gnus-sort-gathered-threads-function
11654           gnus-thread-sort-functions)
11655          (gnus-article-sort-functions
11656           (if (not reverse)
11657               article
11658             `(lambda (t1 t2)
11659                (,article t2 t1))))
11660          (inhibit-read-only t)
11661          (gnus-summary-prepare-hook nil))
11662     ;; We do the sorting by regenerating the threads.
11663     (gnus-summary-prepare)
11664     ;; Hide subthreads if needed.
11665     (gnus-summary-maybe-hide-threads)))
11666
11667 ;; Summary saving commands.
11668
11669 (defun gnus-summary-save-article (&optional n not-saved)
11670   "Save the current article using the default saver function.
11671 If N is a positive number, save the N next articles.
11672 If N is a negative number, save the N previous articles.
11673 If N is nil and any articles have been marked with the process mark,
11674 save those articles instead.
11675 The variable `gnus-default-article-saver' specifies the saver function.
11676
11677 If the optional second argument NOT-SAVED is non-nil, articles saved
11678 will not be marked as saved."
11679   (interactive "P")
11680   (require 'gnus-art)
11681   (let* ((articles (gnus-summary-work-articles n))
11682          (save-buffer (save-excursion
11683                         (nnheader-set-temp-buffer " *Gnus Save*")))
11684          (num (length articles))
11685          ;; Whether to save decoded articles or raw articles.
11686          (decode (when gnus-article-save-coding-system
11687                    (get gnus-default-article-saver :decode)))
11688          ;; When saving many articles in a single file, use the other
11689          ;; function to save articles other than the first one.
11690          (saver2 (get gnus-default-article-saver :function))
11691          (gnus-prompt-before-saving (if saver2
11692                                         t
11693                                       gnus-prompt-before-saving))
11694          (gnus-default-article-saver gnus-default-article-saver)
11695          header file)
11696     (dolist (article articles)
11697       (setq header (gnus-summary-article-header article))
11698       (if (not (vectorp header))
11699           ;; This is a pseudo-article.
11700           (if (assq 'name header)
11701               (gnus-copy-file (cdr (assq 'name header)))
11702             (gnus-message 1 "Article %d is unsaveable" article))
11703         ;; This is a real article.
11704         (save-window-excursion
11705           (gnus-summary-select-article decode decode nil article)
11706           (gnus-summary-goto-subject article))
11707         (with-current-buffer save-buffer
11708           (erase-buffer)
11709           (insert-buffer-substring (if decode
11710                                        gnus-article-buffer
11711                                      gnus-original-article-buffer)))
11712         (setq file (gnus-article-save save-buffer file num))
11713         (gnus-summary-remove-process-mark article)
11714         (unless not-saved
11715           (gnus-summary-set-saved-mark article)))
11716       (when saver2
11717         (setq gnus-default-article-saver saver2
11718               saver2 nil)))
11719     (gnus-kill-buffer save-buffer)
11720     (gnus-summary-position-point)
11721     (gnus-set-mode-line 'summary)
11722     n))
11723
11724 (defun gnus-summary-pipe-output (&optional n sym)
11725   "Pipe the current article to a subprocess.
11726 If N is a positive number, pipe the N next articles.
11727 If N is a negative number, pipe the N previous articles.
11728 If N is nil and any articles have been marked with the process mark,
11729 pipe those articles instead.
11730 The default command to which articles are piped is specified by the
11731 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11732 will be prompted for the command.
11733
11734 The properties `:decode' and `:headers' that are put to the function
11735 symbol `gnus-summary-save-in-pipe' control whether this function
11736 decodes articles and what headers to keep (see the doc string for the
11737 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11738 is neither omitted nor the symbol `r', force including all headers
11739 regardless of the `:headers' property.  If it is the symbol `r',
11740 articles that are not decoded and include all headers will be piped
11741 no matter what the properties `:decode' and `:headers' are."
11742   (interactive (gnus-interactive "P\ny"))
11743   (require 'gnus-art)
11744   (let* ((articles (gnus-summary-work-articles n))
11745          (result-buffer "*Shell Command Output*")
11746          (all-headers (not (memq sym '(nil r))))
11747          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11748          (raw (eq sym 'r))
11749          (headers (get 'gnus-summary-save-in-pipe :headers))
11750          command result)
11751     (unless (numberp (car articles))
11752       (error "No article to pipe"))
11753     (setq command (gnus-read-shell-command
11754                    (concat "Shell command on "
11755                            (if (cdr articles)
11756                                (format "these %d articles" (length articles))
11757                              "this article")
11758                            ": ")
11759                    gnus-summary-pipe-output-default-command))
11760     (when (string-equal command "")
11761       (error "A command is required"))
11762     (when all-headers
11763       (put 'gnus-summary-save-in-pipe :headers nil))
11764     (unwind-protect
11765         (while articles
11766           (gnus-summary-goto-subject (pop articles))
11767           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11768           (when (and (get-buffer result-buffer)
11769                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11770             (setq result (concat result (with-current-buffer result-buffer
11771                                           (buffer-string))))))
11772       (put 'gnus-summary-save-in-pipe :headers headers))
11773     (unless (zerop (length result))
11774       (if (with-current-buffer (get-buffer-create result-buffer)
11775             (erase-buffer)
11776             (insert result)
11777             (prog1
11778                 (and (= (count-lines (point-min) (point)) 1)
11779                      (progn
11780                        (end-of-line 0)
11781                        (<= (current-column)
11782                            (window-width (minibuffer-window)))))
11783               (goto-char (point-min))))
11784           (message "%s" (substring result 0 -1))
11785         (message nil)
11786         (gnus-configure-windows 'pipe)))))
11787
11788 (defun gnus-summary-save-article-mail (&optional arg)
11789   "Append the current article to a Unix mail box file.
11790 If N is a positive number, save the N next articles.
11791 If N is a negative number, save the N previous articles.
11792 If N is nil and any articles have been marked with the process mark,
11793 save those articles instead."
11794   (interactive "P")
11795   (require 'gnus-art)
11796   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11797     (gnus-summary-save-article arg)))
11798
11799 (defun gnus-summary-save-article-rmail (&optional arg)
11800   "Append the current article to an rmail file.
11801 If N is a positive number, save the N next articles.
11802 If N is a negative number, save the N previous articles.
11803 If N is nil and any articles have been marked with the process mark,
11804 save those articles instead."
11805   (interactive "P")
11806   (require 'gnus-art)
11807   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11808     (gnus-summary-save-article arg)))
11809
11810 (defun gnus-summary-save-article-file (&optional arg)
11811   "Append the current article to a file.
11812 If N is a positive number, save the N next articles.
11813 If N is a negative number, save the N previous articles.
11814 If N is nil and any articles have been marked with the process mark,
11815 save those articles instead."
11816   (interactive "P")
11817   (require 'gnus-art)
11818   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11819     (gnus-summary-save-article arg)))
11820
11821 (defun gnus-summary-write-article-file (&optional arg)
11822   "Write the current article to a file, deleting the previous file.
11823 If N is a positive number, save the N next articles.
11824 If N is a negative number, save the N previous articles.
11825 If N is nil and any articles have been marked with the process mark,
11826 save those articles instead."
11827   (interactive "P")
11828   (require 'gnus-art)
11829   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11830     (gnus-summary-save-article arg)))
11831
11832 (defun gnus-summary-save-article-body-file (&optional arg)
11833   "Append the current article body to a file.
11834 If N is a positive number, save the N next articles.
11835 If N is a negative number, save the N previous articles.
11836 If N is nil and any articles have been marked with the process mark,
11837 save those articles instead."
11838   (interactive "P")
11839   (require 'gnus-art)
11840   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11841     (gnus-summary-save-article arg)))
11842
11843 (defun gnus-summary-write-article-body-file (&optional arg)
11844   "Write the current article body to a file, deleting the previous file.
11845 If N is a positive number, save the N next articles.
11846 If N is a negative number, save the N previous articles.
11847 If N is nil and any articles have been marked with the process mark,
11848 save those articles instead."
11849   (interactive "P")
11850   (require 'gnus-art)
11851   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11852     (gnus-summary-save-article arg)))
11853
11854 (defun gnus-summary-muttprint (&optional arg)
11855   "Print the current article using Muttprint.
11856 If N is a positive number, save the N next articles.
11857 If N is a negative number, save the N previous articles.
11858 If N is nil and any articles have been marked with the process mark,
11859 save those articles instead."
11860   (interactive "P")
11861   (require 'gnus-art)
11862   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11863     (gnus-summary-save-article arg t)))
11864
11865 (defun gnus-summary-pipe-message (program)
11866   "Pipe the current article through PROGRAM."
11867   (interactive "sProgram: ")
11868   (gnus-summary-select-article)
11869   (let ((mail-header-separator ""))
11870     (gnus-eval-in-buffer-window gnus-article-buffer
11871       (save-restriction
11872         (widen)
11873         (let ((start (window-start))
11874               (inhibit-read-only t))
11875           (message-pipe-buffer-body program)
11876           (set-window-start (get-buffer-window (current-buffer)) start))))))
11877
11878 (defun gnus-get-split-value (methods)
11879   "Return a value based on the split METHODS."
11880   (let (split-name method result match)
11881     (when methods
11882       (with-current-buffer gnus-original-article-buffer
11883         (save-restriction
11884           (nnheader-narrow-to-headers)
11885           (while (and methods (not split-name))
11886             (goto-char (point-min))
11887             (setq method (pop methods))
11888             (setq match (car method))
11889             (when (cond
11890                    ((stringp match)
11891                     ;; Regular expression.
11892                     (ignore-errors
11893                       (re-search-forward match nil t)))
11894                    ((functionp match)
11895                     ;; Function.
11896                     (save-restriction
11897                       (widen)
11898                       (setq result (funcall match gnus-newsgroup-name))))
11899                    ((consp match)
11900                     ;; Form.
11901                     (save-restriction
11902                       (widen)
11903                       (setq result (eval match)))))
11904               (setq split-name (cdr method))
11905               (cond ((stringp result)
11906                      (push (expand-file-name
11907                             result gnus-article-save-directory)
11908                            split-name))
11909                     ((consp result)
11910                      (setq split-name (append result split-name)))))))))
11911     (nreverse split-name)))
11912
11913 (defun gnus-valid-move-group-p (group)
11914   (and (symbolp group)
11915        (boundp group)
11916        (symbol-name group)
11917        (symbol-value group)
11918        (gnus-get-function (gnus-find-method-for-group
11919                            (symbol-name group)) 'request-accept-article t)))
11920
11921 (defun gnus-read-move-group-name (prompt default articles prefix)
11922   "Read a group name."
11923   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11924          (minibuffer-confirm-incomplete nil) ; XEmacs
11925          (prom
11926           (format "%s %s to"
11927                   prompt
11928                   (if (> (length articles) 1)
11929                       (format "these %d articles" (length articles))
11930                     "this article")))
11931          (to-newsgroup
11932           (cond
11933            ((null split-name)
11934             (gnus-group-completing-read
11935              prom
11936              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
11937              nil prefix nil default))
11938            ((= 1 (length split-name))
11939             (gnus-group-completing-read
11940              prom
11941              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
11942              nil prefix 'gnus-group-history (car split-name)))
11943            (t
11944             (gnus-completing-read
11945              prom (nreverse split-name) nil nil 'gnus-group-history))))
11946          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11947          encoded)
11948     (when to-newsgroup
11949       (if (or (string= to-newsgroup "")
11950               (string= to-newsgroup prefix))
11951           (setq to-newsgroup default))
11952       (unless to-newsgroup
11953         (error "No group name entered"))
11954       (setq encoded (mm-encode-coding-string
11955                      to-newsgroup
11956                      (gnus-group-name-charset to-method to-newsgroup)))
11957       (or (gnus-active encoded)
11958           (gnus-activate-group encoded nil nil to-method)
11959           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11960                                      to-newsgroup))
11961               (or (and (gnus-request-create-group encoded to-method)
11962                        (gnus-activate-group encoded nil nil to-method)
11963                        (gnus-subscribe-group encoded))
11964                   (error "Couldn't create group %s" to-newsgroup)))
11965           (error "No such group: %s" to-newsgroup))
11966       encoded)))
11967
11968 (defvar gnus-summary-save-parts-counter)
11969 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11970
11971 (defun gnus-summary-save-parts (type dir n &optional reverse)
11972   "Save parts matching TYPE to DIR.
11973 If REVERSE, save parts that do not match TYPE."
11974   (interactive
11975    (list (read-string "Save parts of type: "
11976                       (or (car gnus-summary-save-parts-type-history)
11977                           gnus-summary-save-parts-default-mime)
11978                       'gnus-summary-save-parts-type-history)
11979          (setq gnus-summary-save-parts-last-directory
11980                (read-file-name "Save to directory: "
11981                                gnus-summary-save-parts-last-directory
11982                                nil t))
11983          current-prefix-arg))
11984   (gnus-summary-iterate n
11985     (let ((gnus-display-mime-function nil)
11986           gnus-article-prepare-hook
11987           gnus-article-decode-hook
11988           gnus-display-mime-function
11989           gnus-break-pages
11990           (gnus-inhibit-treatment t))
11991       (gnus-summary-select-article))
11992     (with-current-buffer gnus-article-buffer
11993       (let ((handles (or gnus-article-mime-handles
11994                          (mm-dissect-buffer nil gnus-article-loose-mime)
11995                          (and gnus-article-emulate-mime
11996                               (mm-uu-dissect))))
11997             (gnus-summary-save-parts-counter 1))
11998         (when handles
11999           (gnus-summary-save-parts-1 type dir handles reverse)
12000           (unless gnus-article-mime-handles ;; Don't destroy this case.
12001             (mm-destroy-parts handles)))))))
12002
12003 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12004   (if (stringp (car handle))
12005       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12006               (cdr handle))
12007     (when (if reverse
12008               (not (string-match type (mm-handle-media-type handle)))
12009             (string-match type (mm-handle-media-type handle)))
12010       (let ((file (expand-file-name
12011                    (gnus-map-function
12012                     mm-file-name-rewrite-functions
12013                     (file-name-nondirectory
12014                      (or
12015                       (mail-content-type-get
12016                        (mm-handle-disposition handle) 'filename)
12017                       (mail-content-type-get
12018                        (mm-handle-type handle) 'name)
12019                       (format "%s.%d.%d" gnus-newsgroup-name
12020                               (cdr gnus-article-current)
12021                               gnus-summary-save-parts-counter))))
12022                    dir)))
12023         (incf gnus-summary-save-parts-counter)
12024         (unless (file-exists-p file)
12025           (mm-save-part-to-file handle file))))))
12026
12027 ;; Summary extract commands
12028
12029 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12030   (let ((inhibit-read-only t)
12031         (article (gnus-summary-article-number))
12032         after-article b e)
12033     (unless (gnus-summary-goto-subject article)
12034       (error "No such article: %d" article))
12035     (gnus-summary-position-point)
12036     ;; If all commands are to be bunched up on one line, we collect
12037     ;; them here.
12038     (unless gnus-view-pseudos-separately
12039       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12040             files action)
12041         (while ps
12042           (setq action (cdr (assq 'action (car ps))))
12043           (setq files (list (cdr (assq 'name (car ps)))))
12044           (while (and ps (cdr ps)
12045                       (string= (or action "1")
12046                                (or (cdr (assq 'action (cadr ps))) "2")))
12047             (push (cdr (assq 'name (cadr ps))) files)
12048             (setcdr ps (cddr ps)))
12049           (when files
12050             (when (not (string-match "%s" action))
12051               (push " " files))
12052             (push " " files)
12053             (when (assq 'execute (car ps))
12054               (setcdr (assq 'execute (car ps))
12055                       (funcall (if (string-match "%s" action)
12056                                    'format 'concat)
12057                                action
12058                                (mapconcat
12059                                 (lambda (f)
12060                                   (if (equal f " ")
12061                                       f
12062                                     (shell-quote-argument f)))
12063                                 files " ")))))
12064           (setq ps (cdr ps)))))
12065     (if (and gnus-view-pseudos (not not-view))
12066         (while pslist
12067           (when (assq 'execute (car pslist))
12068             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12069                                   (eq gnus-view-pseudos 'not-confirm)))
12070           (setq pslist (cdr pslist)))
12071       (save-excursion
12072         (while pslist
12073           (setq after-article (or (cdr (assq 'article (car pslist)))
12074                                   (gnus-summary-article-number)))
12075           (gnus-summary-goto-subject after-article)
12076           (forward-line 1)
12077           (setq b (point))
12078           (insert "    " (file-name-nondirectory
12079                           (cdr (assq 'name (car pslist))))
12080                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12081           (setq e (point))
12082           (forward-line -1)             ; back to `b'
12083           (gnus-add-text-properties
12084            b (1- e) (list 'gnus-number gnus-reffed-article-number
12085                           gnus-mouse-face-prop gnus-mouse-face))
12086           (gnus-data-enter
12087            after-article gnus-reffed-article-number
12088            gnus-unread-mark b (car pslist) 0 (- e b))
12089           (setq gnus-newsgroup-unreads
12090                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12091                                          gnus-reffed-article-number))
12092           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12093           (setq pslist (cdr pslist)))))))
12094
12095 (defun gnus-pseudos< (p1 p2)
12096   (let ((c1 (cdr (assq 'action p1)))
12097         (c2 (cdr (assq 'action p2))))
12098     (and c1 c2 (string< c1 c2))))
12099
12100 (defun gnus-request-pseudo-article (props)
12101   (cond ((assq 'execute props)
12102          (gnus-execute-command (cdr (assq 'execute props)))))
12103   (let ((gnus-current-article (gnus-summary-article-number)))
12104     (gnus-run-hooks 'gnus-mark-article-hook)))
12105
12106 (defun gnus-execute-command (command &optional automatic)
12107   (save-excursion
12108     (gnus-article-setup-buffer)
12109     (set-buffer gnus-article-buffer)
12110     (setq buffer-read-only nil)
12111     (let ((command (if automatic command
12112                      (read-string "Command: " (cons command 0)))))
12113       (erase-buffer)
12114       (insert "$ " command "\n\n")
12115       (if gnus-view-pseudo-asynchronously
12116           (start-process "gnus-execute" (current-buffer) shell-file-name
12117                          shell-command-switch command)
12118         (call-process shell-file-name nil t nil
12119                       shell-command-switch command)))))
12120
12121 ;; Summary kill commands.
12122
12123 (defun gnus-summary-edit-global-kill (article)
12124   "Edit the \"global\" kill file."
12125   (interactive (list (gnus-summary-article-number)))
12126   (gnus-group-edit-global-kill article))
12127
12128 (defun gnus-summary-edit-local-kill ()
12129   "Edit a local kill file applied to the current newsgroup."
12130   (interactive)
12131   (setq gnus-current-headers (gnus-summary-article-header))
12132   (gnus-group-edit-local-kill
12133    (gnus-summary-article-number) gnus-newsgroup-name))
12134
12135 ;;; Header reading.
12136
12137 (defun gnus-read-header (id &optional header)
12138   "Read the headers of article ID and enter them into the Gnus system."
12139   (let ((group gnus-newsgroup-name)
12140         (gnus-override-method
12141          (or
12142           gnus-override-method
12143           (and (gnus-news-group-p gnus-newsgroup-name)
12144                (car (gnus-refer-article-methods)))))
12145         where)
12146     ;; First we check to see whether the header in question is already
12147     ;; fetched.
12148     (if (stringp id)
12149         ;; This is a Message-ID.
12150         (setq header (or header (gnus-id-to-header id)))
12151       ;; This is an article number.
12152       (setq header (or header (gnus-summary-article-header id))))
12153     (if (and header
12154              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12155         ;; We have found the header.
12156         header
12157       ;; We have to really fetch the header to this article.
12158       (with-current-buffer nntp-server-buffer
12159         (when (setq where (gnus-request-head id group))
12160           (nnheader-fold-continuation-lines)
12161           (goto-char (point-max))
12162           (insert ".\n")
12163           (goto-char (point-min))
12164           (insert "211 ")
12165           (princ (cond
12166                   ((numberp id) id)
12167                   ((cdr where) (cdr where))
12168                   (header (mail-header-number header))
12169                   (t gnus-reffed-article-number))
12170                  (current-buffer))
12171           (insert " Article retrieved.\n"))
12172         (if (or (not where)
12173                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12174             ()                          ; Malformed head.
12175           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12176             (when (and (stringp id)
12177                        (or
12178                         (not (string= (gnus-group-real-name group)
12179                                       (car where)))
12180                         (not (gnus-server-equal gnus-override-method
12181                                                 (gnus-group-method group)))))
12182               ;; If we fetched by Message-ID and the article came from
12183               ;; a different group (or server), we fudge some bogus
12184               ;; article numbers for this article.
12185               (mail-header-set-number header gnus-reffed-article-number))
12186             (with-current-buffer gnus-summary-buffer
12187               (decf gnus-reffed-article-number)
12188               (gnus-remove-header (mail-header-number header))
12189               (push header gnus-newsgroup-headers)
12190               (setq gnus-current-headers header)
12191               (push (mail-header-number header) gnus-newsgroup-limit)))
12192           header)))))
12193
12194 (defun gnus-remove-header (number)
12195   "Remove header NUMBER from `gnus-newsgroup-headers'."
12196   (if (and gnus-newsgroup-headers
12197            (= number (mail-header-number (car gnus-newsgroup-headers))))
12198       (pop gnus-newsgroup-headers)
12199     (let ((headers gnus-newsgroup-headers))
12200       (while (and (cdr headers)
12201                   (not (= number (mail-header-number (cadr headers)))))
12202         (pop headers))
12203       (when (cdr headers)
12204         (setcdr headers (cddr headers))))))
12205
12206 ;;;
12207 ;;; summary highlights
12208 ;;;
12209
12210 (defun gnus-highlight-selected-summary ()
12211   "Highlight selected article in summary buffer."
12212   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12213   (when gnus-summary-selected-face
12214     (save-excursion
12215       (let* ((beg (point-at-bol))
12216              (end (point-at-eol))
12217              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12218              (from (if (get-text-property beg gnus-mouse-face-prop)
12219                        beg
12220                      (or (next-single-property-change
12221                           beg gnus-mouse-face-prop nil end)
12222                          beg)))
12223              (to
12224               (if (= from end)
12225                   (- from 2)
12226                 (or (next-single-property-change
12227                      from gnus-mouse-face-prop nil end)
12228                     end))))
12229         ;; If no mouse-face prop on line we will have to = from = end,
12230         ;; so we highlight the entire line instead.
12231         (when (= (+ to 2) from)
12232           (setq from beg)
12233           (setq to end))
12234         (if gnus-newsgroup-selected-overlay
12235             ;; Move old overlay.
12236             (gnus-move-overlay
12237              gnus-newsgroup-selected-overlay from to (current-buffer))
12238           ;; Create new overlay.
12239           (gnus-overlay-put
12240            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12241            'face gnus-summary-selected-face))))))
12242
12243 (defvar gnus-summary-highlight-line-cached nil)
12244 (defvar gnus-summary-highlight-line-trigger nil)
12245
12246 (defun gnus-summary-highlight-line-0 ()
12247   (if (and (eq gnus-summary-highlight-line-trigger
12248                gnus-summary-highlight)
12249            gnus-summary-highlight-line-cached)
12250       gnus-summary-highlight-line-cached
12251     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12252           gnus-summary-highlight-line-cached
12253           (let* ((cond (list 'cond))
12254                  (c cond)
12255                  (list gnus-summary-highlight))
12256             (while list
12257               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12258                               nil))
12259               (setq c (cdr c)
12260                     list (cdr list)))
12261             (gnus-byte-compile (list 'lambda nil cond))))))
12262
12263 (defun gnus-summary-highlight-line ()
12264   "Highlight current line according to `gnus-summary-highlight'."
12265   (let* ((beg (point-at-bol))
12266          (article (or (gnus-summary-article-number) gnus-current-article))
12267          (score (or (cdr (assq article
12268                                gnus-newsgroup-scored))
12269                     gnus-summary-default-score 0))
12270          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12271          (inhibit-read-only t)
12272          (default gnus-summary-default-score)
12273          (default-high gnus-summary-default-high-score)
12274          (default-low gnus-summary-default-low-score)
12275          (uncached (and gnus-summary-use-undownloaded-faces
12276                         (memq article gnus-newsgroup-undownloaded)
12277                         (not (memq article gnus-newsgroup-cached)))))
12278     (let ((face (funcall (gnus-summary-highlight-line-0))))
12279       (unless (eq face (get-text-property beg 'face))
12280         (gnus-put-text-property-excluding-characters-with-faces
12281          beg (point-at-eol) 'face
12282          (setq face (if (boundp face) (symbol-value face) face)))
12283         (when gnus-summary-highlight-line-function
12284           (funcall gnus-summary-highlight-line-function article face))))))
12285
12286 (defun gnus-update-read-articles (group unread &optional compute)
12287   "Update the list of read articles in GROUP.
12288 UNREAD is a sorted list."
12289   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12290         (info (gnus-get-info group))
12291         (prev 1)
12292         read)
12293     (if (or (not info) (not active))
12294         ;; There is no info on this group if it was, in fact,
12295         ;; killed.  Gnus stores no information on killed groups, so
12296         ;; there's nothing to be done.
12297         ;; One could store the information somewhere temporarily,
12298         ;; perhaps...  Hmmm...
12299         ()
12300       ;; Remove any negative articles numbers.
12301       (while (and unread (< (car unread) 0))
12302         (setq unread (cdr unread)))
12303       ;; Remove any expired article numbers
12304       (while (and unread (< (car unread) (car active)))
12305         (setq unread (cdr unread)))
12306       ;; Compute the ranges of read articles by looking at the list of
12307       ;; unread articles.
12308       (while unread
12309         (when (/= (car unread) prev)
12310           (push (if (= prev (1- (car unread))) prev
12311                   (cons prev (1- (car unread))))
12312                 read))
12313         (setq prev (1+ (car unread)))
12314         (setq unread (cdr unread)))
12315       (when (<= prev (cdr active))
12316         (push (cons prev (cdr active)) read))
12317       (setq read (if (> (length read) 1) (nreverse read) read))
12318       (if compute
12319           read
12320         (save-excursion
12321           (let (setmarkundo)
12322             ;; Propagate the read marks to the backend.
12323             (when (and gnus-propagate-marks
12324                        (gnus-check-backend-function 'request-set-mark group))
12325               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12326                     (add (gnus-remove-from-range read (gnus-info-read info))))
12327                 (when (or add del)
12328                   (unless (gnus-check-group group)
12329                     (error "Can't open server for %s" group))
12330                   (gnus-request-set-mark
12331                    group (delq nil (list (if add (list add 'add '(read)))
12332                                          (if del (list del 'del '(read))))))
12333                   (setq setmarkundo
12334                         `(gnus-request-set-mark
12335                           ,group
12336                           ',(delq nil (list
12337                                        (if del (list del 'add '(read)))
12338                                        (if add (list add 'del '(read))))))))))
12339             (set-buffer gnus-group-buffer)
12340             (gnus-undo-register
12341               `(progn
12342                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12343                  (gnus-info-set-read ',info ',(gnus-info-read info))
12344                  (gnus-get-unread-articles-in-group ',info
12345                                                     (gnus-active ,group))
12346                  (gnus-group-update-group ,group t)
12347                  ,setmarkundo))))
12348         ;; Enter this list into the group info.
12349         (gnus-info-set-read info read)
12350         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12351         (gnus-get-unread-articles-in-group info (gnus-active group))
12352         t))))
12353
12354 (defun gnus-offer-save-summaries ()
12355   "Offer to save all active summary buffers."
12356   (let (buffers)
12357     ;; Go through all buffers and find all summaries.
12358     (dolist (buffer (buffer-list))
12359       (when (and (setq buffer (buffer-name buffer))
12360                  (string-match "Summary" buffer)
12361                  (with-current-buffer buffer
12362                    ;; We check that this is, indeed, a summary buffer.
12363                    (and (eq major-mode 'gnus-summary-mode)
12364                         ;; Also make sure this isn't bogus.
12365                         gnus-newsgroup-prepared
12366                         ;; Also make sure that this isn't a
12367                         ;; dead summary buffer.
12368                         (not gnus-dead-summary-mode))))
12369         (push buffer buffers)))
12370     ;; Go through all these summary buffers and offer to save them.
12371     (when buffers
12372       (save-excursion
12373         (map-y-or-n-p
12374          "Update summary buffer %s? "
12375          (lambda (buf)
12376            (switch-to-buffer buf)
12377            (gnus-summary-exit))
12378          buffers)))))
12379
12380 (defun gnus-summary-setup-default-charset ()
12381   "Setup newsgroup default charset."
12382   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12383       (setq gnus-newsgroup-charset nil)
12384     (let* ((ignored-charsets
12385             (or gnus-newsgroup-ephemeral-ignored-charsets
12386                 (append
12387                  (and gnus-newsgroup-name
12388                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12389                  gnus-newsgroup-ignored-charsets))))
12390       (setq gnus-newsgroup-charset
12391             (or gnus-newsgroup-ephemeral-charset
12392                 (and gnus-newsgroup-name
12393                      (gnus-parameter-charset gnus-newsgroup-name))
12394                 gnus-default-charset))
12395       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12396            ignored-charsets))))
12397
12398 ;;;
12399 ;;; Mime Commands
12400 ;;;
12401
12402 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12403   "Display the current article buffer fully MIME-buttonized.
12404 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12405 treated as multipart/mixed."
12406   (interactive "P")
12407   (require 'gnus-art)
12408   (let ((gnus-unbuttonized-mime-types nil)
12409         (gnus-mime-display-multipart-as-mixed show-all-parts))
12410     (gnus-summary-show-article)))
12411
12412 (defun gnus-summary-repair-multipart (article)
12413   "Add a Content-Type header to a multipart article without one."
12414   (interactive (list (gnus-summary-article-number)))
12415   (gnus-with-article article
12416     (message-narrow-to-head)
12417     (message-remove-header "Mime-Version")
12418     (goto-char (point-max))
12419     (insert "Mime-Version: 1.0\n")
12420     (widen)
12421     (when (search-forward "\n--" nil t)
12422       (let ((separator (buffer-substring (point) (point-at-eol))))
12423         (message-narrow-to-head)
12424         (message-remove-header "Content-Type")
12425         (goto-char (point-max))
12426         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12427                         separator))
12428         (widen))))
12429   (let (gnus-mark-article-hook)
12430     (gnus-summary-select-article t t nil article)))
12431
12432 (defun gnus-summary-toggle-display-buttonized ()
12433   "Toggle the buttonizing of the article buffer."
12434   (interactive)
12435   (require 'gnus-art)
12436   (if (setq gnus-inhibit-mime-unbuttonizing
12437             (not gnus-inhibit-mime-unbuttonizing))
12438       (let ((gnus-unbuttonized-mime-types nil))
12439         (gnus-summary-show-article))
12440     (gnus-summary-show-article)))
12441
12442 ;;;
12443 ;;; Generic summary marking commands
12444 ;;;
12445
12446 (defvar gnus-summary-marking-alist
12447   '((read gnus-del-mark "d")
12448     (unread gnus-unread-mark "u")
12449     (ticked gnus-ticked-mark "!")
12450     (dormant gnus-dormant-mark "?")
12451     (expirable gnus-expirable-mark "e"))
12452   "An alist of names/marks/keystrokes.")
12453
12454 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12455 (defvar gnus-summary-mark-map)
12456
12457 (defun gnus-summary-make-all-marking-commands ()
12458   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12459   (dolist (elem gnus-summary-marking-alist)
12460     (apply 'gnus-summary-make-marking-command elem)))
12461
12462 (defun gnus-summary-make-marking-command (name mark keystroke)
12463   (let ((map (make-sparse-keymap)))
12464     (define-key gnus-summary-generic-mark-map keystroke map)
12465     (dolist (lway `((next "next" next nil "n")
12466                     (next-unread "next unread" next t "N")
12467                     (prev "previous" prev nil "p")
12468                     (prev-unread "previous unread" prev t "P")
12469                     (nomove "" nil nil ,keystroke)))
12470       (let ((func (gnus-summary-make-marking-command-1
12471                    mark (car lway) lway name)))
12472         (setq func (eval func))
12473         (define-key map (nth 4 lway) func)))))
12474
12475 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12476   `(defun ,(intern
12477             (format "gnus-summary-put-mark-as-%s%s"
12478                     name (if (eq way 'nomove)
12479                              ""
12480                            (concat "-" (symbol-name way)))))
12481      (n)
12482      ,(format
12483        "Mark the current article as %s%s.
12484 If N, the prefix, then repeat N times.
12485 If N is negative, move in reverse order.
12486 The difference between N and the actual number of articles marked is
12487 returned."
12488        name (cadr lway))
12489      (interactive "p")
12490      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12491
12492 (defun gnus-summary-generic-mark (n mark move unread)
12493   "Mark N articles with MARK."
12494   (unless (eq major-mode 'gnus-summary-mode)
12495     (error "This command can only be used in the summary buffer"))
12496   (gnus-summary-show-thread)
12497   (let ((nummove
12498          (cond
12499           ((eq move 'next) 1)
12500           ((eq move 'prev) -1)
12501           (t 0))))
12502     (if (zerop nummove)
12503         (setq n 1)
12504       (when (< n 0)
12505         (setq n (abs n)
12506               nummove (* -1 nummove))))
12507     (while (and (> n 0)
12508                 (gnus-summary-mark-article nil mark)
12509                 (zerop (gnus-summary-next-subject nummove unread t)))
12510       (setq n (1- n)))
12511     (when (/= 0 n)
12512       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12513     (gnus-summary-recenter)
12514     (gnus-summary-position-point)
12515     (gnus-set-mode-line 'summary)
12516     n))
12517
12518 (defun gnus-summary-insert-articles (articles)
12519   (when (setq articles
12520               (gnus-sorted-difference articles
12521                                       (mapcar (lambda (h)
12522                                                 (mail-header-number h))
12523                                               gnus-newsgroup-headers)))
12524     (setq gnus-newsgroup-headers
12525           (gnus-merge 'list
12526                       gnus-newsgroup-headers
12527                       (gnus-fetch-headers articles)
12528                       'gnus-article-sort-by-number))
12529     ;; Suppress duplicates?
12530     (when gnus-suppress-duplicates
12531       (gnus-dup-suppress-articles))
12532
12533     (if (and gnus-fetch-old-headers
12534              (eq gnus-headers-retrieved-by 'nov))
12535         ;; We might want to build some more threads first.
12536         (if (eq gnus-fetch-old-headers 'invisible)
12537             (gnus-build-all-threads)
12538           (gnus-build-old-threads))
12539       ;; Mark the inserted articles that are unread as unread.
12540       (setq gnus-newsgroup-unreads
12541             (gnus-sorted-nunion
12542              gnus-newsgroup-unreads
12543              (gnus-sorted-nintersection
12544               (gnus-list-of-unread-articles gnus-newsgroup-name)
12545               articles)))
12546       ;; Mark the inserted articles as selected so that the information
12547       ;; of the marks having been changed by a user may be updated when
12548       ;; exiting this group.  See `gnus-summary-update-info'.
12549       (dolist (art articles)
12550         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12551     ;; Let the Gnus agent mark articles as read.
12552     (when gnus-agent
12553       (gnus-agent-get-undownloaded-list))
12554     ;; Remove list identifiers from subject
12555     (when gnus-list-identifiers
12556       (gnus-summary-remove-list-identifiers))
12557     ;; First and last article in this newsgroup.
12558     (when gnus-newsgroup-headers
12559       (setq gnus-newsgroup-begin
12560             (mail-header-number (car gnus-newsgroup-headers))
12561             gnus-newsgroup-end
12562             (mail-header-number
12563              (gnus-last-element gnus-newsgroup-headers))))
12564     (when gnus-use-scoring
12565       (gnus-possibly-score-headers))))
12566
12567 (defun gnus-summary-insert-old-articles (&optional all)
12568   "Insert all old articles in this group.
12569 If ALL is non-nil, already read articles become readable.
12570 If ALL is a number, fetch this number of articles."
12571   (interactive "P")
12572   (prog1
12573       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12574             older len)
12575         (setq older
12576               ;; Some nntp servers lie about their active range.  When
12577               ;; this happens, the active range can be in the millions.
12578               ;; Use a compressed range to avoid creating a huge list.
12579               (gnus-range-difference (list gnus-newsgroup-active) old))
12580         (setq len (gnus-range-length older))
12581         (cond
12582          ((null older) nil)
12583          ((numberp all)
12584           (if (< all len)
12585               (let ((older-range (nreverse older)))
12586                 (setq older nil)
12587
12588                 (while (> all 0)
12589                   (let* ((r (pop older-range))
12590                          (min (if (numberp r) r (car r)))
12591                          (max (if (numberp r) r (cdr r))))
12592                     (while (and (<= min max)
12593                                 (> all 0))
12594                       (push max older)
12595                       (setq all (1- all)
12596                             max (1- max))))))
12597             (setq older (gnus-uncompress-range older))))
12598          (all
12599           (setq older (gnus-uncompress-range older)))
12600          (t
12601           (when (and (numberp gnus-large-newsgroup)
12602                    (> len gnus-large-newsgroup))
12603               (let* ((cursor-in-echo-area nil)
12604                      (initial (gnus-parameter-large-newsgroup-initial
12605                                gnus-newsgroup-name))
12606                      (input
12607                       (read-string
12608                        (format
12609                         "How many articles from %s (%s %d): "
12610                         (gnus-group-decoded-name gnus-newsgroup-name)
12611                         (if initial "max" "default")
12612                         len)
12613                        (if initial
12614                            (cons (number-to-string initial)
12615                                  0)))))
12616                 (unless (string-match "^[ \t]*$" input)
12617                   (setq all (string-to-number input))
12618                   (if (< all len)
12619                       (let ((older-range (nreverse older)))
12620                         (setq older nil)
12621
12622                         (while (> all 0)
12623                           (let* ((r (pop older-range))
12624                                  (min (if (numberp r) r (car r)))
12625                                  (max (if (numberp r) r (cdr r))))
12626                             (while (and (<= min max)
12627                                         (> all 0))
12628                               (push max older)
12629                               (setq all (1- all)
12630                                     max (1- max))))))))))
12631           (setq older (gnus-uncompress-range older))))
12632         (if (not older)
12633             (message "No old news.")
12634           (gnus-summary-insert-articles older)
12635           (gnus-summary-limit (gnus-sorted-nunion old older))))
12636     (gnus-summary-position-point)))
12637
12638 (defun gnus-summary-insert-new-articles ()
12639   "Insert all new articles in this group."
12640   (interactive)
12641   (prog1
12642       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12643             (old-high gnus-newsgroup-highest)
12644             (nnmail-fetched-sources (list t))
12645             i new)
12646         (setq gnus-newsgroup-active
12647               (gnus-copy-sequence
12648                (gnus-activate-group gnus-newsgroup-name 'scan)))
12649         (setq i (cdr gnus-newsgroup-active)
12650               gnus-newsgroup-highest i)
12651         (while (> i old-high)
12652           (push i new)
12653           (decf i))
12654         (if (not new)
12655             (message "No gnus is bad news")
12656           (gnus-summary-insert-articles new)
12657           (setq gnus-newsgroup-unreads
12658                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12659           (gnus-summary-limit (gnus-sorted-nunion old new))))
12660     (gnus-summary-position-point)))
12661
12662 ;;; Bookmark support for Gnus.
12663 (declare-function bookmark-make-record-default
12664                   "bookmark" (&optional no-file no-context posn))
12665 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12666 (declare-function bookmark-default-handler "bookmark" (bmk))
12667 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12668 (defvar bookmark-yank-point)
12669 (defvar bookmark-current-buffer)
12670
12671 (defun gnus-summary-bookmark-make-record ()
12672   "Make a bookmark entry for a Gnus summary buffer."
12673   (let (pos buf)
12674     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12675       (save-restriction              ; FIXME is it necessary to widen?
12676         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12677       (setq buf "art") ; We are recording bookmark from article buffer.
12678       (setq bookmark-yank-point (point))
12679       (setq bookmark-current-buffer (current-buffer))
12680       (gnus-article-show-summary))      ; Go back in summary buffer.
12681     ;; We are now recording bookmark from summary buffer.
12682     (unless buf (setq buf "sum"))
12683     (let* ((subject (elt (gnus-summary-article-header) 1))
12684            (grp     (car gnus-article-current))
12685            (art     (cdr gnus-article-current))
12686            (head    (gnus-summary-article-header art))
12687            (id      (mail-header-id head)))
12688       `(,subject
12689         ,@(condition-case nil
12690               (bookmark-make-record-default 'no-file 'no-context pos)
12691             (wrong-number-of-arguments
12692              (bookmark-make-record-default 'point-only)))
12693         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12694         (group . ,grp) (article . ,art)
12695         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12696
12697 ;;;###autoload
12698 (defun gnus-summary-bookmark-jump (bookmark)
12699   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12700 BOOKMARK is a bookmark name or a bookmark record."
12701   (let ((group    (bookmark-prop-get bookmark 'group))
12702         (article  (bookmark-prop-get bookmark 'article))
12703         (id       (bookmark-prop-get bookmark 'message-id))
12704         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12705     (gnus-fetch-group group (list article))
12706     (gnus-summary-insert-cached-articles)
12707     (gnus-summary-goto-article id nil 'force)
12708     ;; FIXME we have to wait article buffer is ready (only large buffer)
12709     ;; Is there a better solution to know that?
12710     ;; If we don't wait `bookmark-default-handler' will have no chance
12711     ;; to set position. However there is no error, just wrong pos.
12712     (sit-for 1)
12713     (when (string= buf "Gnus-art")
12714       (other-window 1))
12715     (bookmark-default-handler
12716      `(""
12717        (buffer . ,(current-buffer))
12718        . ,(bookmark-get-bookmark-record bookmark)))))
12719
12720 (gnus-summary-make-all-marking-commands)
12721
12722 (gnus-ems-redefine)
12723
12724 (provide 'gnus-sum)
12725
12726 (run-hooks 'gnus-sum-load-hook)
12727
12728 ;; Local Variables:
12729 ;; coding: iso-8859-1
12730 ;; End:
12731
12732 ;;; gnus-sum.el ends here