Commit Katsumi Yamaoka's patch.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
40
41 (defcustom gnus-kill-summary-on-exit t
42   "*If non-nil, kill the summary buffer when you exit from it.
43 If nil, the summary will become a \"*Dead Summary*\" buffer, and
44 it will be killed sometime later."
45   :group 'gnus-summary-exit
46   :type 'boolean)
47
48 (defcustom gnus-fetch-old-headers nil
49   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
50 If an unread article in the group refers to an older, already read (or
51 just marked as read) article, the old article will not normally be
52 displayed in the Summary buffer.  If this variable is non-nil, Gnus
53 will attempt to grab the headers to the old articles, and thereby
54 build complete threads.  If it has the value `some', only enough
55 headers to connect otherwise loose threads will be displayed.  This
56 variable can also be a number.  In that case, no more than that number
57 of old headers will be fetched.  If it has the value `invisible', all
58 old headers will be fetched, but none will be displayed.
59
60 The server has to support NOV for any of this to work."
61   :group 'gnus-thread
62   :type '(choice (const :tag "off" nil)
63                  (const some)
64                  number
65                  (sexp :menu-tag "other" t)))
66
67 (defcustom gnus-refer-thread-limit 200
68   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
69 If t, fetch all the available old headers."
70   :group 'gnus-thread
71   :type '(choice number
72                  (sexp :menu-tag "other" t)))
73
74 (defcustom gnus-summary-make-false-root 'adopt
75   "*nil means that Gnus won't gather loose threads.
76 If the root of a thread has expired or been read in a previous
77 session, the information necessary to build a complete thread has been
78 lost.  Instead of having many small sub-threads from this original thread
79 scattered all over the summary buffer, Gnus can gather them.
80
81 If non-nil, Gnus will try to gather all loose sub-threads from an
82 original thread into one large thread.
83
84 If this variable is non-nil, it should be one of `none', `adopt',
85 `dummy' or `empty'.
86
87 If this variable is `none', Gnus will not make a false root, but just
88 present the sub-threads after another.
89 If this variable is `dummy', Gnus will create a dummy root that will
90 have all the sub-threads as children.
91 If this variable is `adopt', Gnus will make one of the \"children\"
92 the parent and mark all the step-children as such.
93 If this variable is `empty', the \"children\" are printed with empty
94 subject fields.  (Or rather, they will be printed with a string
95 given by the `gnus-summary-same-subject' variable.)"
96   :group 'gnus-thread
97   :type '(choice (const :tag "off" nil)
98                  (const none)
99                  (const dummy)
100                  (const adopt)
101                  (const empty)))
102
103 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
104   "*A regexp to match subjects to be excluded from loose thread gathering.
105 As loose thread gathering is done on subjects only, that means that
106 there can be many false gatherings performed.  By rooting out certain
107 common subjects, gathering might become saner."
108   :group 'gnus-thread
109   :type 'regexp)
110
111 (defcustom gnus-summary-gather-subject-limit nil
112   "*Maximum length of subject comparisons when gathering loose threads.
113 Use nil to compare full subjects.  Setting this variable to a low
114 number will help gather threads that have been corrupted by
115 newsreaders chopping off subject lines, but it might also mean that
116 unrelated articles that have subject that happen to begin with the
117 same few characters will be incorrectly gathered.
118
119 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
120 comparing subjects."
121   :group 'gnus-thread
122   :type '(choice (const :tag "off" nil)
123                  (const fuzzy)
124                  (sexp :menu-tag "on" t)))
125
126 (defcustom gnus-simplify-subject-functions nil
127   "List of functions taking a string argument that simplify subjects.
128 The functions are applied recursively.
129
130 Useful functions to put in this list include: `gnus-simplify-subject-re',
131 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
132   :group 'gnus-thread
133   :type '(repeat function))
134
135 (defcustom gnus-simplify-ignored-prefixes nil
136   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
137   :group 'gnus-thread
138   :type '(choice (const :tag "off" nil)
139                  regexp))
140
141 (defcustom gnus-build-sparse-threads nil
142   "*If non-nil, fill in the gaps in threads.
143 If `some', only fill in the gaps that are needed to tie loose threads
144 together.  If `more', fill in all leaf nodes that Gnus can find.  If
145 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
146   :group 'gnus-thread
147   :type '(choice (const :tag "off" nil)
148                  (const some)
149                  (const more)
150                  (sexp :menu-tag "all" t)))
151
152 (defcustom gnus-summary-thread-gathering-function
153   'gnus-gather-threads-by-subject
154   "*Function used for gathering loose threads.
155 There are two pre-defined functions: `gnus-gather-threads-by-subject',
156 which only takes Subjects into consideration; and
157 `gnus-gather-threads-by-references', which compared the References
158 headers of the articles to find matches."
159   :group 'gnus-thread
160   :type '(radio (function-item gnus-gather-threads-by-subject)
161                 (function-item gnus-gather-threads-by-references)
162                 (function :tag "other")))
163
164 (defcustom gnus-summary-same-subject ""
165   "*String indicating that the current article has the same subject as the previous.
166 This variable will only be used if the value of
167 `gnus-summary-make-false-root' is `empty'."
168   :group 'gnus-summary-format
169   :type 'string)
170
171 (defcustom gnus-summary-goto-unread t
172   "*If t, many commands will go to the next unread article.
173 This applies to marking commands as well as other commands that
174 \"naturally\" select the next article, like, for instance, `SPC' at
175 the end of an article.
176
177 If nil, the marking commands do NOT go to the next unread article
178 (they go to the next article instead).  If `never', commands that
179 usually go to the next unread article, will go to the next article,
180 whether it is read or not."
181   :group 'gnus-summary-marks
182   :link '(custom-manual "(gnus)Setting Marks")
183   :type '(choice (const :tag "off" nil)
184                  (const never)
185                  (sexp :menu-tag "on" t)))
186
187 (defcustom gnus-summary-default-score 0
188   "*Default article score level.
189 All scores generated by the score files will be added to this score.
190 If this variable is nil, scoring will be disabled."
191   :group 'gnus-score-default
192   :type '(choice (const :tag "disable")
193                  integer))
194
195 (defcustom gnus-summary-zcore-fuzz 0
196   "*Fuzziness factor for the zcore in the summary buffer.
197 Articles with scores closer than this to `gnus-summary-default-score'
198 will not be marked."
199   :group 'gnus-summary-format
200   :type 'integer)
201
202 (defcustom gnus-simplify-subject-fuzzy-regexp nil
203   "*Strings to be removed when doing fuzzy matches.
204 This can either be a regular expression or list of regular expressions
205 that will be removed from subject strings if fuzzy subject
206 simplification is selected."
207   :group 'gnus-thread
208   :type '(repeat regexp))
209
210 (defcustom gnus-show-threads t
211   "*If non-nil, display threads in summary mode."
212   :group 'gnus-thread
213   :type 'boolean)
214
215 (defcustom gnus-thread-hide-subtree nil
216   "*If non-nil, hide all threads initially.
217 If threads are hidden, you have to run the command
218 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
219 to expose hidden threads."
220   :group 'gnus-thread
221   :type 'boolean)
222
223 (defcustom gnus-thread-hide-killed t
224   "*If non-nil, hide killed threads automatically."
225   :group 'gnus-thread
226   :type 'boolean)
227
228 (defcustom gnus-thread-ignore-subject t
229   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
230 If nil, articles that have different subjects from their parents will
231 start separate threads."
232   :group 'gnus-thread
233   :type 'boolean)
234
235 (defcustom gnus-thread-operation-ignore-subject t
236   "*If non-nil, subjects will be ignored when doing thread commands.
237 This affects commands like `gnus-summary-kill-thread' and
238 `gnus-summary-lower-thread'.
239
240 If this variable is nil, articles in the same thread with different
241 subjects will not be included in the operation in question.  If this
242 variable is `fuzzy', only articles that have subjects that are fuzzily
243 equal will be included."
244   :group 'gnus-thread
245   :type '(choice (const :tag "off" nil)
246                  (const fuzzy)
247                  (sexp :tag "on" t)))
248
249 (defcustom gnus-thread-indent-level 4
250   "*Number that says how much each sub-thread should be indented."
251   :group 'gnus-thread
252   :type 'integer)
253
254 (defcustom gnus-auto-extend-newsgroup t
255   "*If non-nil, extend newsgroup forward and backward when requested."
256   :group 'gnus-summary-choose
257   :type 'boolean)
258
259 (defcustom gnus-auto-select-first t
260   "*If nil, don't select the first unread article when entering a group.
261 If this variable is `best', select the highest-scored unread article
262 in the group.  If t, select the first unread article.
263
264 This variable can also be a function to place point on a likely
265 subject line.  Useful values include `gnus-summary-first-unread-subject',
266 `gnus-summary-first-unread-article' and
267 `gnus-summary-best-unread-article'.
268
269 If you want to prevent automatic selection of the first unread article
270 in some newsgroups, set the variable to nil in
271 `gnus-select-group-hook'."
272   :group 'gnus-group-select
273   :type '(choice (const :tag "none" nil)
274                  (const best)
275                  (sexp :menu-tag "first" t)
276                  (function-item gnus-summary-first-unread-subject)
277                  (function-item gnus-summary-first-unread-article)
278                  (function-item gnus-summary-best-unread-article)))
279
280 (defcustom gnus-auto-select-next t
281   "*If non-nil, offer to go to the next group from the end of the previous.
282 If the value is t and the next newsgroup is empty, Gnus will exit
283 summary mode and go back to group mode.  If the value is neither nil
284 nor t, Gnus will select the following unread newsgroup.  In
285 particular, if the value is the symbol `quietly', the next unread
286 newsgroup will be selected without any confirmation, and if it is
287 `almost-quietly', the next group will be selected without any
288 confirmation if you are located on the last article in the group.
289 Finally, if this variable is `slightly-quietly', the `Z n' command
290 will go to the next group without confirmation."
291   :group 'gnus-summary-maneuvering
292   :type '(choice (const :tag "off" nil)
293                  (const quietly)
294                  (const almost-quietly)
295                  (const slightly-quietly)
296                  (sexp :menu-tag "on" t)))
297
298 (defcustom gnus-auto-select-same nil
299   "*If non-nil, select the next article with the same subject.
300 If there are no more articles with the same subject, go to
301 the first unread article."
302   :group 'gnus-summary-maneuvering
303   :type 'boolean)
304
305 (defcustom gnus-summary-check-current nil
306   "*If non-nil, consider the current article when moving.
307 The \"unread\" movement commands will stay on the same line if the
308 current article is unread."
309   :group 'gnus-summary-maneuvering
310   :type 'boolean)
311
312 (defcustom gnus-auto-center-summary t
313   "*If non-nil, always center the current summary buffer.
314 In particular, if `vertical' do only vertical recentering.  If non-nil
315 and non-`vertical', do both horizontal and vertical recentering."
316   :group 'gnus-summary-maneuvering
317   :type '(choice (const :tag "none" nil)
318                  (const vertical)
319                  (integer :tag "height")
320                  (sexp :menu-tag "both" t)))
321
322 (defcustom gnus-show-all-headers nil
323   "*If non-nil, don't hide any headers."
324   :group 'gnus-article-hiding
325   :group 'gnus-article-headers
326   :type 'boolean)
327
328 (defcustom gnus-summary-ignore-duplicates nil
329   "*If non-nil, ignore articles with identical Message-ID headers."
330   :group 'gnus-summary
331   :type 'boolean)
332
333 (defcustom gnus-single-article-buffer t
334   "*If non-nil, display all articles in the same buffer.
335 If nil, each group will get its own article buffer."
336   :group 'gnus-article-various
337   :type 'boolean)
338
339 (defcustom gnus-break-pages t
340   "*If non-nil, do page breaking on articles.
341 The page delimiter is specified by the `gnus-page-delimiter'
342 variable."
343   :group 'gnus-article-various
344   :type 'boolean)
345
346 (defcustom gnus-move-split-methods nil
347   "*Variable used to suggest where articles are to be moved to.
348 It uses the same syntax as the `gnus-split-methods' variable."
349   :group 'gnus-summary-mail
350   :type '(repeat (choice (list :value (fun) function)
351                          (cons :value ("" "") regexp (repeat string))
352                          (sexp :value nil))))
353
354 (defcustom gnus-unread-mark ?  ;Whitespace
355   "*Mark used for unread articles."
356   :group 'gnus-summary-marks
357   :type 'character)
358
359 (defcustom gnus-ticked-mark ?!
360   "*Mark used for ticked articles."
361   :group 'gnus-summary-marks
362   :type 'character)
363
364 (defcustom gnus-dormant-mark ??
365   "*Mark used for dormant articles."
366   :group 'gnus-summary-marks
367   :type 'character)
368
369 (defcustom gnus-del-mark ?r
370   "*Mark used for del'd articles."
371   :group 'gnus-summary-marks
372   :type 'character)
373
374 (defcustom gnus-read-mark ?R
375   "*Mark used for read articles."
376   :group 'gnus-summary-marks
377   :type 'character)
378
379 (defcustom gnus-expirable-mark ?E
380   "*Mark used for expirable articles."
381   :group 'gnus-summary-marks
382   :type 'character)
383
384 (defcustom gnus-killed-mark ?K
385   "*Mark used for killed articles."
386   :group 'gnus-summary-marks
387   :type 'character)
388
389 (defcustom gnus-souped-mark ?F
390   "*Mark used for killed articles."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-kill-file-mark ?X
395   "*Mark used for articles killed by kill files."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-low-score-mark ?Y
400   "*Mark used for articles with a low score."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-catchup-mark ?C
405   "*Mark used for articles that are caught up."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-replied-mark ?A
410   "*Mark used for articles that have been replied to."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-cached-mark ?*
415   "*Mark used for articles that are in the cache."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-saved-mark ?S
420   "*Mark used for articles that have been saved to."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-ancient-mark ?O
425   "*Mark used for ancient articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-sparse-mark ?Q
430   "*Mark used for sparsely reffed articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-canceled-mark ?G
435   "*Mark used for canceled articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-duplicate-mark ?M
440   "*Mark used for duplicate articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-undownloaded-mark ?@
445   "*Mark used for articles that weren't downloaded."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-downloadable-mark ?%
450   "*Mark used for articles that are to be downloaded."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-unsendable-mark ?=
455   "*Mark used for articles that won't be sent."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-score-over-mark ?+
460   "*Score mark used for articles with high scores."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-score-below-mark ?-
465   "*Score mark used for articles with low scores."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-empty-thread-mark ?  ;Whitespace
470   "*There is no thread under the article."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-not-empty-thread-mark ?=
475   "*There is a thread under the article."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-view-pseudo-asynchronously nil
480   "*If non-nil, Gnus will view pseudo-articles asynchronously."
481   :group 'gnus-extract-view
482   :type 'boolean)
483
484 (defcustom gnus-auto-expirable-marks
485   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
486         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
487         gnus-souped-mark gnus-duplicate-mark)
488   "*The list of marks converted into expiration if a group is auto-expirable."
489   :group 'gnus-summary
490   :type '(repeat character))
491
492 (defcustom gnus-inhibit-user-auto-expire t
493   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
494   :group 'gnus-summary
495   :type 'boolean)
496
497 (defcustom gnus-view-pseudos nil
498   "*If `automatic', pseudo-articles will be viewed automatically.
499 If `not-confirm', pseudos will be viewed automatically, and the user
500 will not be asked to confirm the command."
501   :group 'gnus-extract-view
502   :type '(choice (const :tag "off" nil)
503                  (const automatic)
504                  (const not-confirm)))
505
506 (defcustom gnus-view-pseudos-separately t
507   "*If non-nil, one pseudo-article will be created for each file to be viewed.
508 If nil, all files that use the same viewing command will be given as a
509 list of parameters to that command."
510   :group 'gnus-extract-view
511   :type 'boolean)
512
513 (defcustom gnus-insert-pseudo-articles t
514   "*If non-nil, insert pseudo-articles when decoding articles."
515   :group 'gnus-extract-view
516   :type 'boolean)
517
518 (defcustom gnus-summary-dummy-line-format
519   "  %(:                          :%) %S\n"
520   "*The format specification for the dummy roots in the summary buffer.
521 It works along the same lines as a normal formatting string,
522 with some simple extensions.
523
524 %S  The subject"
525   :group 'gnus-threading
526   :type 'string)
527
528 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
529   "*The format specification for the summary mode line.
530 It works along the same lines as a normal formatting string,
531 with some simple extensions:
532
533 %G  Group name
534 %p  Unprefixed group name
535 %A  Current article number
536 %z  Current article score
537 %V  Gnus version
538 %U  Number of unread articles in the group
539 %e  Number of unselected articles in the group
540 %Z  A string with unread/unselected article counts
541 %g  Shortish group name
542 %S  Subject of the current article
543 %u  User-defined spec
544 %s  Current score file name
545 %d  Number of dormant articles
546 %r  Number of articles that have been marked as read in this session
547 %E  Number of articles expunged by the score files"
548   :group 'gnus-summary-format
549   :type 'string)
550
551 (defcustom gnus-list-identifiers nil
552   "Regexp that matches list identifiers to be removed from subject.
553 This can also be a list of regexps."
554   :group 'gnus-summary-format
555   :group 'gnus-article-hiding
556   :type '(choice (const :tag "none" nil)
557                  (regexp :value ".*")
558                  (repeat :value (".*") regexp)))
559
560 (defcustom gnus-summary-mark-below 0
561   "*Mark all articles with a score below this variable as read.
562 This variable is local to each summary buffer and usually set by the
563 score file."
564   :group 'gnus-score-default
565   :type 'integer)
566
567 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
568   "*List of functions used for sorting articles in the summary buffer.
569 This variable is only used when not using a threaded display."
570   :group 'gnus-summary-sort
571   :type '(repeat (choice (function-item gnus-article-sort-by-number)
572                          (function-item gnus-article-sort-by-author)
573                          (function-item gnus-article-sort-by-subject)
574                          (function-item gnus-article-sort-by-date)
575                          (function-item gnus-article-sort-by-score)
576                          (function :tag "other"))))
577
578 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
579   "*List of functions used for sorting threads in the summary buffer.
580 By default, threads are sorted by article number.
581
582 Each function takes two threads and return non-nil if the first thread
583 should be sorted before the other.  If you use more than one function,
584 the primary sort function should be the last.  You should probably
585 always include `gnus-thread-sort-by-number' in the list of sorting
586 functions -- preferably first.
587
588 Ready-made functions include `gnus-thread-sort-by-number',
589 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
590 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
591 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
592   :group 'gnus-summary-sort
593   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
594                          (function-item gnus-thread-sort-by-author)
595                          (function-item gnus-thread-sort-by-subject)
596                          (function-item gnus-thread-sort-by-date)
597                          (function-item gnus-thread-sort-by-score)
598                          (function-item gnus-thread-sort-by-total-score)
599                          (function :tag "other"))))
600
601 (defcustom gnus-thread-score-function '+
602   "*Function used for calculating the total score of a thread.
603
604 The function is called with the scores of the article and each
605 subthread and should then return the score of the thread.
606
607 Some functions you can use are `+', `max', or `min'."
608   :group 'gnus-summary-sort
609   :type 'function)
610
611 (defcustom gnus-summary-expunge-below nil
612   "All articles that have a score less than this variable will be expunged.
613 This variable is local to the summary buffers."
614   :group 'gnus-score-default
615   :type '(choice (const :tag "off" nil)
616                  integer))
617
618 (defcustom gnus-thread-expunge-below nil
619   "All threads that have a total score less than this variable will be expunged.
620 See `gnus-thread-score-function' for en explanation of what a
621 \"thread score\" is.
622
623 This variable is local to the summary buffers."
624   :group 'gnus-threading
625   :group 'gnus-score-default
626   :type '(choice (const :tag "off" nil)
627                  integer))
628
629 (defcustom gnus-summary-mode-hook nil
630   "*A hook for Gnus summary mode.
631 This hook is run before any variables are set in the summary buffer."
632   :group 'gnus-summary-various
633   :type 'hook)
634
635 (defcustom gnus-summary-menu-hook nil
636   "*Hook run after the creation of the summary mode menu."
637   :group 'gnus-summary-visual
638   :type 'hook)
639
640 (defcustom gnus-summary-exit-hook nil
641   "*A hook called on exit from the summary buffer.
642 It will be called with point in the group buffer."
643   :group 'gnus-summary-exit
644   :type 'hook)
645
646 (defcustom gnus-summary-prepare-hook nil
647   "*A hook called after the summary buffer has been generated.
648 If you want to modify the summary buffer, you can use this hook."
649   :group 'gnus-summary-various
650   :type 'hook)
651
652 (defcustom gnus-summary-prepared-hook nil
653   "*A hook called as the last thing after the summary buffer has been generated."
654   :group 'gnus-summary-various
655   :type 'hook)
656
657 (defcustom gnus-summary-generate-hook nil
658   "*A hook run just before generating the summary buffer.
659 This hook is commonly used to customize threading variables and the
660 like."
661   :group 'gnus-summary-various
662   :type 'hook)
663
664 (defcustom gnus-select-group-hook nil
665   "*A hook called when a newsgroup is selected.
666
667 If you'd like to simplify subjects like the
668 `gnus-summary-next-same-subject' command does, you can use the
669 following hook:
670
671  (setq gnus-select-group-hook
672       (list
673         (lambda ()
674           (mapcar (lambda (header)
675                      (mail-header-set-subject
676                       header
677                       (gnus-simplify-subject
678                        (mail-header-subject header) 're-only)))
679                   gnus-newsgroup-headers))))"
680   :group 'gnus-group-select
681   :type 'hook)
682
683 (defcustom gnus-select-article-hook nil
684   "*A hook called when an article is selected."
685   :group 'gnus-summary-choose
686   :type 'hook)
687
688 (defcustom gnus-visual-mark-article-hook
689   (list 'gnus-highlight-selected-summary)
690   "*Hook run after selecting an article in the summary buffer.
691 It is meant to be used for highlighting the article in some way.  It
692 is not run if `gnus-visual' is nil."
693   :group 'gnus-summary-visual
694   :type 'hook)
695
696 (defcustom gnus-parse-headers-hook nil
697   "*A hook called before parsing the headers."
698   :group 'gnus-various
699   :type 'hook)
700
701 (defcustom gnus-exit-group-hook nil
702   "*A hook called when exiting summary mode.
703 This hook is not called from the non-updating exit commands like `Q'."
704   :group 'gnus-various
705   :type 'hook)
706
707 (defcustom gnus-summary-update-hook
708   (list 'gnus-summary-highlight-line)
709   "*A hook called when a summary line is changed.
710 The hook will not be called if `gnus-visual' is nil.
711
712 The default function `gnus-summary-highlight-line' will
713 highlight the line according to the `gnus-summary-highlight'
714 variable."
715   :group 'gnus-summary-visual
716   :type 'hook)
717
718 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
719   "*A hook called when an article is selected for the first time.
720 The hook is intended to mark an article as read (or unread)
721 automatically when it is selected."
722   :group 'gnus-summary-choose
723   :type 'hook)
724
725 (defcustom gnus-group-no-more-groups-hook nil
726   "*A hook run when returning to group mode having no more (unread) groups."
727   :group 'gnus-group-select
728   :type 'hook)
729
730 (defcustom gnus-ps-print-hook nil
731   "*A hook run before ps-printing something from Gnus."
732   :group 'gnus-summary
733   :type 'hook)
734
735 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
736   "Face used for highlighting the current article in the summary buffer."
737   :group 'gnus-summary-visual
738   :type 'face)
739
740 (defcustom gnus-summary-highlight
741   '(((= mark gnus-canceled-mark)
742      . gnus-summary-cancelled-face)
743     ((and (> score default)
744           (or (= mark gnus-dormant-mark)
745               (= mark gnus-ticked-mark)))
746      . gnus-summary-high-ticked-face)
747     ((and (< score default)
748           (or (= mark gnus-dormant-mark)
749               (= mark gnus-ticked-mark)))
750      . gnus-summary-low-ticked-face)
751     ((or (= mark gnus-dormant-mark)
752          (= mark gnus-ticked-mark))
753      . gnus-summary-normal-ticked-face)
754     ((and (> score default) (= mark gnus-ancient-mark))
755      . gnus-summary-high-ancient-face)
756     ((and (< score default) (= mark gnus-ancient-mark))
757      . gnus-summary-low-ancient-face)
758     ((= mark gnus-ancient-mark)
759      . gnus-summary-normal-ancient-face)
760     ((and (> score default) (= mark gnus-unread-mark))
761      . gnus-summary-high-unread-face)
762     ((and (< score default) (= mark gnus-unread-mark))
763      . gnus-summary-low-unread-face)
764     ((= mark gnus-unread-mark)
765      . gnus-summary-normal-unread-face)
766     ((and (> score default) (memq mark (list gnus-downloadable-mark
767                                              gnus-undownloaded-mark)))
768      . gnus-summary-high-unread-face)
769     ((and (< score default) (memq mark (list gnus-downloadable-mark
770                                              gnus-undownloaded-mark)))
771      . gnus-summary-low-unread-face)
772     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
773      . gnus-summary-normal-unread-face)
774     ((> score default)
775      . gnus-summary-high-read-face)
776     ((< score default)
777      . gnus-summary-low-read-face)
778     (t
779      . gnus-summary-normal-read-face))
780   "*Controls the highlighting of summary buffer lines.
781
782 A list of (FORM . FACE) pairs.  When deciding how a a particular
783 summary line should be displayed, each form is evaluated.  The content
784 of the face field after the first true form is used.  You can change
785 how those summary lines are displayed, by editing the face field.
786
787 You can use the following variables in the FORM field.
788
789 score:   The articles score
790 default: The default article score.
791 below:   The score below which articles are automatically marked as read.
792 mark:    The articles mark."
793   :group 'gnus-summary-visual
794   :type '(repeat (cons (sexp :tag "Form" nil)
795                        face)))
796
797 (defcustom gnus-alter-header-function nil
798   "Function called to allow alteration of article header structures.
799 The function is called with one parameter, the article header vector,
800 which it may alter in any way.")
801
802 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
803   "Variable that says which function should be used to decode a string with encoded words.")
804
805 (defcustom gnus-extra-headers nil
806   "*Extra headers to parse."
807   :group 'gnus-summary
808   :type '(repeat symbol))
809
810 (defcustom gnus-ignored-from-addresses
811   (and user-mail-address (regexp-quote user-mail-address))
812   "*Regexp of From headers that may be suppressed in favor of To headers."
813   :group 'gnus-summary
814   :type 'regexp)
815
816 (defcustom gnus-group-charset-alist
817   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
818     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
819     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
820     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
821     ("^relcom\\>" koi8-r)
822     ("^fido7\\>" koi8-r)
823     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
824     ("^israel\\>" iso-8859-1)
825     ("^han\\>" euc-kr)
826     ("^alt.chinese.text.big5\\>" chinese-big5)
827     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
828     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
829     (".*" iso-8859-1))
830   "Alist of regexps (to match group names) and default charsets to be used when reading."
831   :type '(repeat (list (regexp :tag "Group")
832                        (symbol :tag "Charset")))
833   :group 'gnus-charset)
834
835 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
836   "List of charsets that should be ignored.
837 When these charsets are used in the \"charset\" parameter, the
838 default charset will be used instead."
839   :type '(repeat symbol)
840   :group 'gnus-charset)
841
842 (defcustom gnus-group-ignored-charsets-alist
843   '(("alt\\.chinese\\.text" iso-8859-1))
844   "Alist of regexps (to match group names) and charsets that should be ignored.
845 When these charsets are used in the \"charset\" parameter, the
846 default charset will be used instead."
847   :type '(repeat (cons (regexp :tag "Group")
848                        (repeat symbol)))
849   :group 'gnus-charset)
850
851 (defcustom gnus-group-highlight-words-alist nil
852   "Alist of group regexps and highlight regexps.
853 This variable uses the same syntax as `gnus-emphasis-alist'."
854   :type '(repeat (cons (regexp :tag "Group")
855                        (repeat (list (regexp :tag "Highlight regexp")
856                                      (number :tag "Group for entire word" 0)
857                                      (number :tag "Group for displayed part" 0)
858                                      (symbol :tag "Face"
859                                              gnus-emphasis-highlight-words)))))
860   :group 'gnus-summary-visual)
861
862 (defcustom gnus-summary-show-article-charset-alist
863   nil
864   "Alist of number and charset.
865 The article will be shown with the charset corresponding to the
866 numbered argument.
867 For example: ((1 . cn-gb-2312) (2 . big5))."
868   :type '(repeat (cons (number :tag "Argument" 1)
869                        (symbol :tag "Charset")))
870   :group 'gnus-charset)
871
872 (defcustom gnus-preserve-marks t
873   "Whether marks are preserved when moving, copying and respooling messages."
874   :type 'boolean
875   :group 'gnus-summary-marks)
876
877 (defcustom gnus-alter-articles-to-read-function nil
878   "Function to be called to alter the list of articles to be selected."
879   :type 'function
880   :group 'gnus-summary)
881
882 (defcustom gnus-orphan-score nil
883   "*All orphans get this score added.  Set in the score file."
884   :group 'gnus-score-default
885   :type '(choice (const nil)
886                  integer))
887
888 ;;; Internal variables
889
890 (defvar gnus-article-mime-handles nil)
891 (defvar gnus-article-decoded-p nil)
892 (defvar gnus-scores-exclude-files nil)
893 (defvar gnus-page-broken nil)
894 (defvar gnus-inhibit-mime-unbuttonizing nil)
895
896 (defvar gnus-original-article nil)
897 (defvar gnus-article-internal-prepare-hook nil)
898 (defvar gnus-newsgroup-process-stack nil)
899
900 (defvar gnus-thread-indent-array nil)
901 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
902 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
903   "Function called to sort the articles within a thread after it has been gathered together.")
904
905 ;; Avoid highlighting in kill files.
906 (defvar gnus-summary-inhibit-highlight nil)
907 (defvar gnus-newsgroup-selected-overlay nil)
908 (defvar gnus-inhibit-limiting nil)
909 (defvar gnus-newsgroup-adaptive-score-file nil)
910 (defvar gnus-current-score-file nil)
911 (defvar gnus-current-move-group nil)
912 (defvar gnus-current-copy-group nil)
913 (defvar gnus-current-crosspost-group nil)
914
915 (defvar gnus-newsgroup-dependencies nil)
916 (defvar gnus-newsgroup-adaptive nil)
917 (defvar gnus-summary-display-article-function nil)
918 (defvar gnus-summary-highlight-line-function nil
919   "Function called after highlighting a summary line.")
920
921 (defvar gnus-summary-line-format-alist
922   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
923     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
924     (?s gnus-tmp-subject-or-nil ?s)
925     (?n gnus-tmp-name ?s)
926     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
927         ?s)
928     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
929             gnus-tmp-from) ?s)
930     (?F gnus-tmp-from ?s)
931     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
932     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
933     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
934     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
935     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
936     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
937     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
938     (?L gnus-tmp-lines ?d)
939     (?I gnus-tmp-indentation ?s)
940     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
941     (?R gnus-tmp-replied ?c)
942     (?\[ gnus-tmp-opening-bracket ?c)
943     (?\] gnus-tmp-closing-bracket ?c)
944     (?\> (make-string gnus-tmp-level ? ) ?s)
945     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
946     (?i gnus-tmp-score ?d)
947     (?z gnus-tmp-score-char ?c)
948     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
949     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
950     (?U gnus-tmp-unread ?c)
951     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
952     (?t (gnus-summary-number-of-articles-in-thread
953          (and (boundp 'thread) (car thread)) gnus-tmp-level)
954         ?d)
955     (?e (gnus-summary-number-of-articles-in-thread
956          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
957         ?c)
958     (?u gnus-tmp-user-defined ?s)
959     (?P (gnus-pick-line-number) ?d))
960   "An alist of format specifications that can appear in summary lines.
961 These are paired with what variables they correspond with, along with
962 the type of the variable (string, integer, character, etc).")
963
964 (defvar gnus-summary-dummy-line-format-alist
965   `((?S gnus-tmp-subject ?s)
966     (?N gnus-tmp-number ?d)
967     (?u gnus-tmp-user-defined ?s)))
968
969 (defvar gnus-summary-mode-line-format-alist
970   `((?G gnus-tmp-group-name ?s)
971     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
972     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
973     (?A gnus-tmp-article-number ?d)
974     (?Z gnus-tmp-unread-and-unselected ?s)
975     (?V gnus-version ?s)
976     (?U gnus-tmp-unread-and-unticked ?d)
977     (?S gnus-tmp-subject ?s)
978     (?e gnus-tmp-unselected ?d)
979     (?u gnus-tmp-user-defined ?s)
980     (?d (length gnus-newsgroup-dormant) ?d)
981     (?t (length gnus-newsgroup-marked) ?d)
982     (?r (length gnus-newsgroup-reads) ?d)
983     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
984     (?E gnus-newsgroup-expunged-tally ?d)
985     (?s (gnus-current-score-file-nondirectory) ?s)))
986
987 (defvar gnus-last-search-regexp nil
988   "Default regexp for article search command.")
989
990 (defvar gnus-last-shell-command nil
991   "Default shell command on article.")
992
993 (defvar gnus-newsgroup-begin nil)
994 (defvar gnus-newsgroup-end nil)
995 (defvar gnus-newsgroup-last-rmail nil)
996 (defvar gnus-newsgroup-last-mail nil)
997 (defvar gnus-newsgroup-last-folder nil)
998 (defvar gnus-newsgroup-last-file nil)
999 (defvar gnus-newsgroup-auto-expire nil)
1000 (defvar gnus-newsgroup-active nil)
1001
1002 (defvar gnus-newsgroup-data nil)
1003 (defvar gnus-newsgroup-data-reverse nil)
1004 (defvar gnus-newsgroup-limit nil)
1005 (defvar gnus-newsgroup-limits nil)
1006
1007 (defvar gnus-newsgroup-unreads nil
1008   "List of unread articles in the current newsgroup.")
1009
1010 (defvar gnus-newsgroup-unselected nil
1011   "List of unselected unread articles in the current newsgroup.")
1012
1013 (defvar gnus-newsgroup-reads nil
1014   "Alist of read articles and article marks in the current newsgroup.")
1015
1016 (defvar gnus-newsgroup-expunged-tally nil)
1017
1018 (defvar gnus-newsgroup-marked nil
1019   "List of ticked articles in the current newsgroup (a subset of unread art).")
1020
1021 (defvar gnus-newsgroup-killed nil
1022   "List of ranges of articles that have been through the scoring process.")
1023
1024 (defvar gnus-newsgroup-cached nil
1025   "List of articles that come from the article cache.")
1026
1027 (defvar gnus-newsgroup-saved nil
1028   "List of articles that have been saved.")
1029
1030 (defvar gnus-newsgroup-kill-headers nil)
1031
1032 (defvar gnus-newsgroup-replied nil
1033   "List of articles that have been replied to in the current newsgroup.")
1034
1035 (defvar gnus-newsgroup-expirable nil
1036   "List of articles in the current newsgroup that can be expired.")
1037
1038 (defvar gnus-newsgroup-processable nil
1039   "List of articles in the current newsgroup that can be processed.")
1040
1041 (defvar gnus-newsgroup-downloadable nil
1042   "List of articles in the current newsgroup that can be processed.")
1043
1044 (defvar gnus-newsgroup-undownloaded nil
1045   "List of articles in the current newsgroup that haven't been downloaded..")
1046
1047 (defvar gnus-newsgroup-unsendable nil
1048   "List of articles in the current newsgroup that won't be sent.")
1049
1050 (defvar gnus-newsgroup-bookmarks nil
1051   "List of articles in the current newsgroup that have bookmarks.")
1052
1053 (defvar gnus-newsgroup-dormant nil
1054   "List of dormant articles in the current newsgroup.")
1055
1056 (defvar gnus-newsgroup-scored nil
1057   "List of scored articles in the current newsgroup.")
1058
1059 (defvar gnus-newsgroup-headers nil
1060   "List of article headers in the current newsgroup.")
1061
1062 (defvar gnus-newsgroup-threads nil)
1063
1064 (defvar gnus-newsgroup-prepared nil
1065   "Whether the current group has been prepared properly.")
1066
1067 (defvar gnus-newsgroup-ancient nil
1068   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1069
1070 (defvar gnus-newsgroup-sparse nil)
1071
1072 (defvar gnus-current-article nil)
1073 (defvar gnus-article-current nil)
1074 (defvar gnus-current-headers nil)
1075 (defvar gnus-have-all-headers nil)
1076 (defvar gnus-last-article nil)
1077 (defvar gnus-newsgroup-history nil)
1078 (defvar gnus-newsgroup-charset nil)
1079 (defvar gnus-newsgroup-ephemeral-charset nil)
1080 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1081
1082 (defconst gnus-summary-local-variables
1083   '(gnus-newsgroup-name
1084     gnus-newsgroup-begin gnus-newsgroup-end
1085     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1086     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1087     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1088     gnus-newsgroup-unselected gnus-newsgroup-marked
1089     gnus-newsgroup-reads gnus-newsgroup-saved
1090     gnus-newsgroup-replied gnus-newsgroup-expirable
1091     gnus-newsgroup-processable gnus-newsgroup-killed
1092     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1093     gnus-newsgroup-unsendable
1094     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1095     gnus-newsgroup-headers gnus-newsgroup-threads
1096     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1097     gnus-current-article gnus-current-headers gnus-have-all-headers
1098     gnus-last-article gnus-article-internal-prepare-hook
1099     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1100     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1101     gnus-thread-expunge-below
1102     gnus-score-alist gnus-current-score-file
1103     (gnus-summary-expunge-below . global)
1104     (gnus-summary-mark-below . global)
1105     (gnus-orphan-score . global)
1106     gnus-newsgroup-active gnus-scores-exclude-files
1107     gnus-newsgroup-history gnus-newsgroup-ancient
1108     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1109     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1110     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1111     (gnus-newsgroup-expunged-tally . 0)
1112     gnus-cache-removable-articles gnus-newsgroup-cached
1113     gnus-newsgroup-data gnus-newsgroup-data-reverse
1114     gnus-newsgroup-limit gnus-newsgroup-limits
1115     gnus-newsgroup-charset)
1116   "Variables that are buffer-local to the summary buffers.")
1117
1118 ;; Byte-compiler warning.
1119 (defvar gnus-article-mode-map)
1120
1121 ;; MIME stuff.
1122
1123 (defvar gnus-decode-encoded-word-methods
1124   '(mail-decode-encoded-word-string)
1125   "List of methods used to decode encoded words.
1126
1127 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1128 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1129 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1130 whose names match REGEXP.
1131
1132 For example:
1133 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1134  mail-decode-encoded-word-string
1135  (\"chinese\" . rfc1843-decode-string))")
1136
1137 (defvar gnus-decode-encoded-word-methods-cache nil)
1138
1139 (defun gnus-multi-decode-encoded-word-string (string)
1140   "Apply the functions from `gnus-encoded-word-methods' that match."
1141   (unless (and gnus-decode-encoded-word-methods-cache
1142                (eq gnus-newsgroup-name
1143                    (car gnus-decode-encoded-word-methods-cache)))
1144     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1145     (mapcar (lambda (x)
1146               (if (symbolp x)
1147                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1148                 (if (and gnus-newsgroup-name
1149                          (string-match (car x) gnus-newsgroup-name))
1150                     (nconc gnus-decode-encoded-word-methods-cache
1151                            (list (cdr x))))))
1152           gnus-decode-encoded-word-methods))
1153   (let ((xlist gnus-decode-encoded-word-methods-cache))
1154     (pop xlist)
1155     (while xlist
1156       (setq string (funcall (pop xlist) string))))
1157   string)
1158
1159 ;; Subject simplification.
1160
1161 (defun gnus-simplify-whitespace (str)
1162   "Remove excessive whitespace from STR."
1163   (let ((mystr str))
1164     ;; Multiple spaces.
1165     (while (string-match "[ \t][ \t]+" mystr)
1166       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1167                           " "
1168                           (substring mystr (match-end 0)))))
1169     ;; Leading spaces.
1170     (when (string-match "^[ \t]+" mystr)
1171       (setq mystr (substring mystr (match-end 0))))
1172     ;; Trailing spaces.
1173     (when (string-match "[ \t]+$" mystr)
1174       (setq mystr (substring mystr 0 (match-beginning 0))))
1175     mystr))
1176
1177 (defsubst gnus-simplify-subject-re (subject)
1178   "Remove \"Re:\" from subject lines."
1179   (if (string-match "^[Rr][Ee]: *" subject)
1180       (substring subject (match-end 0))
1181     subject))
1182
1183 (defun gnus-simplify-subject (subject &optional re-only)
1184   "Remove `Re:' and words in parentheses.
1185 If RE-ONLY is non-nil, strip leading `Re:'s only."
1186   (let ((case-fold-search t))           ;Ignore case.
1187     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1188     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1189       (setq subject (substring subject (match-end 0))))
1190     ;; Remove uninteresting prefixes.
1191     (when (and (not re-only)
1192                gnus-simplify-ignored-prefixes
1193                (string-match gnus-simplify-ignored-prefixes subject))
1194       (setq subject (substring subject (match-end 0))))
1195     ;; Remove words in parentheses from end.
1196     (unless re-only
1197       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1198         (setq subject (substring subject 0 (match-beginning 0)))))
1199     ;; Return subject string.
1200     subject))
1201
1202 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1203 ;; all whitespace.
1204 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1205   (goto-char (point-min))
1206   (while (re-search-forward regexp nil t)
1207     (replace-match (or newtext ""))))
1208
1209 (defun gnus-simplify-buffer-fuzzy ()
1210   "Simplify string in the buffer fuzzily.
1211 The string in the accessible portion of the current buffer is simplified.
1212 It is assumed to be a single-line subject.
1213 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1214 matter is removed.  Additional things can be deleted by setting
1215 `gnus-simplify-subject-fuzzy-regexp'."
1216   (let ((case-fold-search t)
1217         (modified-tick))
1218     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1219
1220     (while (not (eq modified-tick (buffer-modified-tick)))
1221       (setq modified-tick (buffer-modified-tick))
1222       (cond
1223        ((listp gnus-simplify-subject-fuzzy-regexp)
1224         (mapcar 'gnus-simplify-buffer-fuzzy-step
1225                 gnus-simplify-subject-fuzzy-regexp))
1226        (gnus-simplify-subject-fuzzy-regexp
1227         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1228       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1229       (gnus-simplify-buffer-fuzzy-step
1230        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1231       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1232
1233     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1234     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1235     (gnus-simplify-buffer-fuzzy-step " $")
1236     (gnus-simplify-buffer-fuzzy-step "^ +")))
1237
1238 (defun gnus-simplify-subject-fuzzy (subject)
1239   "Simplify a subject string fuzzily.
1240 See `gnus-simplify-buffer-fuzzy' for details."
1241   (save-excursion
1242     (gnus-set-work-buffer)
1243     (let ((case-fold-search t))
1244       ;; Remove uninteresting prefixes.
1245       (when (and gnus-simplify-ignored-prefixes
1246                  (string-match gnus-simplify-ignored-prefixes subject))
1247         (setq subject (substring subject (match-end 0))))
1248       (insert subject)
1249       (inline (gnus-simplify-buffer-fuzzy))
1250       (buffer-string))))
1251
1252 (defsubst gnus-simplify-subject-fully (subject)
1253   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1254   (cond
1255    (gnus-simplify-subject-functions
1256     (gnus-map-function gnus-simplify-subject-functions subject))
1257    ((null gnus-summary-gather-subject-limit)
1258     (gnus-simplify-subject-re subject))
1259    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1260     (gnus-simplify-subject-fuzzy subject))
1261    ((numberp gnus-summary-gather-subject-limit)
1262     (gnus-limit-string (gnus-simplify-subject-re subject)
1263                        gnus-summary-gather-subject-limit))
1264    (t
1265     subject)))
1266
1267 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1268   "Check whether two subjects are equal.
1269 If optional argument simple-first is t, first argument is already
1270 simplified."
1271   (cond
1272    ((null simple-first)
1273     (equal (gnus-simplify-subject-fully s1)
1274            (gnus-simplify-subject-fully s2)))
1275    (t
1276     (equal s1
1277            (gnus-simplify-subject-fully s2)))))
1278
1279 (defun gnus-summary-bubble-group ()
1280   "Increase the score of the current group.
1281 This is a handy function to add to `gnus-summary-exit-hook' to
1282 increase the score of each group you read."
1283   (gnus-group-add-score gnus-newsgroup-name))
1284
1285 \f
1286 ;;;
1287 ;;; Gnus summary mode
1288 ;;;
1289
1290 (put 'gnus-summary-mode 'mode-class 'special)
1291
1292 (when t
1293   ;; Non-orthogonal keys
1294
1295   (gnus-define-keys gnus-summary-mode-map
1296     " " gnus-summary-next-page
1297     "\177" gnus-summary-prev-page
1298     [delete] gnus-summary-prev-page
1299     [backspace] gnus-summary-prev-page
1300     "\r" gnus-summary-scroll-up
1301     "\M-\r" gnus-summary-scroll-down
1302     "n" gnus-summary-next-unread-article
1303     "p" gnus-summary-prev-unread-article
1304     "N" gnus-summary-next-article
1305     "P" gnus-summary-prev-article
1306     "\M-\C-n" gnus-summary-next-same-subject
1307     "\M-\C-p" gnus-summary-prev-same-subject
1308     "\M-n" gnus-summary-next-unread-subject
1309     "\M-p" gnus-summary-prev-unread-subject
1310     "." gnus-summary-first-unread-article
1311     "," gnus-summary-best-unread-article
1312     "\M-s" gnus-summary-search-article-forward
1313     "\M-r" gnus-summary-search-article-backward
1314     "<" gnus-summary-beginning-of-article
1315     ">" gnus-summary-end-of-article
1316     "j" gnus-summary-goto-article
1317     "^" gnus-summary-refer-parent-article
1318     "\M-^" gnus-summary-refer-article
1319     "u" gnus-summary-tick-article-forward
1320     "!" gnus-summary-tick-article-forward
1321     "U" gnus-summary-tick-article-backward
1322     "d" gnus-summary-mark-as-read-forward
1323     "D" gnus-summary-mark-as-read-backward
1324     "E" gnus-summary-mark-as-expirable
1325     "\M-u" gnus-summary-clear-mark-forward
1326     "\M-U" gnus-summary-clear-mark-backward
1327     "k" gnus-summary-kill-same-subject-and-select
1328     "\C-k" gnus-summary-kill-same-subject
1329     "\M-\C-k" gnus-summary-kill-thread
1330     "\M-\C-l" gnus-summary-lower-thread
1331     "e" gnus-summary-edit-article
1332     "#" gnus-summary-mark-as-processable
1333     "\M-#" gnus-summary-unmark-as-processable
1334     "\M-\C-t" gnus-summary-toggle-threads
1335     "\M-\C-s" gnus-summary-show-thread
1336     "\M-\C-h" gnus-summary-hide-thread
1337     "\M-\C-f" gnus-summary-next-thread
1338     "\M-\C-b" gnus-summary-prev-thread
1339     [(meta down)] gnus-summary-next-thread
1340     [(meta up)] gnus-summary-prev-thread
1341     "\M-\C-u" gnus-summary-up-thread
1342     "\M-\C-d" gnus-summary-down-thread
1343     "&" gnus-summary-execute-command
1344     "c" gnus-summary-catchup-and-exit
1345     "\C-w" gnus-summary-mark-region-as-read
1346     "\C-t" gnus-summary-toggle-truncation
1347     "?" gnus-summary-mark-as-dormant
1348     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1349     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1350     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1351     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1352     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1353     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1354     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1355     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1356     "=" gnus-summary-expand-window
1357     "\C-x\C-s" gnus-summary-reselect-current-group
1358     "\M-g" gnus-summary-rescan-group
1359     "w" gnus-summary-stop-page-breaking
1360     "\C-c\C-r" gnus-summary-caesar-message
1361     "f" gnus-summary-followup
1362     "F" gnus-summary-followup-with-original
1363     "C" gnus-summary-cancel-article
1364     "r" gnus-summary-reply
1365     "R" gnus-summary-reply-with-original
1366     "\C-c\C-f" gnus-summary-mail-forward
1367     "o" gnus-summary-save-article
1368     "\C-o" gnus-summary-save-article-mail
1369     "|" gnus-summary-pipe-output
1370     "\M-k" gnus-summary-edit-local-kill
1371     "\M-K" gnus-summary-edit-global-kill
1372     ;; "V" gnus-version
1373     "\C-c\C-d" gnus-summary-describe-group
1374     "q" gnus-summary-exit
1375     "Q" gnus-summary-exit-no-update
1376     "\C-c\C-i" gnus-info-find-node
1377     gnus-mouse-2 gnus-mouse-pick-article
1378     "m" gnus-summary-mail-other-window
1379     "a" gnus-summary-post-news
1380     "x" gnus-summary-limit-to-unread
1381     "s" gnus-summary-isearch-article
1382     "t" gnus-summary-toggle-header
1383     "g" gnus-summary-show-article
1384     "l" gnus-summary-goto-last-article
1385     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1386     "\C-d" gnus-summary-enter-digest-group
1387     "\M-\C-d" gnus-summary-read-document
1388     "\M-\C-e" gnus-summary-edit-parameters
1389     "\M-\C-a" gnus-summary-customize-parameters
1390     "\C-c\C-b" gnus-bug
1391     "*" gnus-cache-enter-article
1392     "\M-*" gnus-cache-remove-article
1393     "\M-&" gnus-summary-universal-argument
1394     "\C-l" gnus-recenter
1395     "I" gnus-summary-increase-score
1396     "L" gnus-summary-lower-score
1397     "\M-i" gnus-symbolic-argument
1398     "h" gnus-summary-select-article-buffer
1399
1400     "b" gnus-article-view-part
1401     "\M-t" gnus-summary-toggle-display-buttonized
1402
1403     "V" gnus-summary-score-map
1404     "X" gnus-uu-extract-map
1405     "S" gnus-summary-send-map)
1406
1407   ;; Sort of orthogonal keymap
1408   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1409     "t" gnus-summary-tick-article-forward
1410     "!" gnus-summary-tick-article-forward
1411     "d" gnus-summary-mark-as-read-forward
1412     "r" gnus-summary-mark-as-read-forward
1413     "c" gnus-summary-clear-mark-forward
1414     " " gnus-summary-clear-mark-forward
1415     "e" gnus-summary-mark-as-expirable
1416     "x" gnus-summary-mark-as-expirable
1417     "?" gnus-summary-mark-as-dormant
1418     "b" gnus-summary-set-bookmark
1419     "B" gnus-summary-remove-bookmark
1420     "#" gnus-summary-mark-as-processable
1421     "\M-#" gnus-summary-unmark-as-processable
1422     "S" gnus-summary-limit-include-expunged
1423     "C" gnus-summary-catchup
1424     "H" gnus-summary-catchup-to-here
1425     "\C-c" gnus-summary-catchup-all
1426     "k" gnus-summary-kill-same-subject-and-select
1427     "K" gnus-summary-kill-same-subject
1428     "P" gnus-uu-mark-map)
1429
1430   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1431     "c" gnus-summary-clear-above
1432     "u" gnus-summary-tick-above
1433     "m" gnus-summary-mark-above
1434     "k" gnus-summary-kill-below)
1435
1436   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1437     "/" gnus-summary-limit-to-subject
1438     "n" gnus-summary-limit-to-articles
1439     "w" gnus-summary-pop-limit
1440     "s" gnus-summary-limit-to-subject
1441     "a" gnus-summary-limit-to-author
1442     "u" gnus-summary-limit-to-unread
1443     "m" gnus-summary-limit-to-marks
1444     "M" gnus-summary-limit-exclude-marks
1445     "v" gnus-summary-limit-to-score
1446     "*" gnus-summary-limit-include-cached
1447     "D" gnus-summary-limit-include-dormant
1448     "T" gnus-summary-limit-include-thread
1449     "d" gnus-summary-limit-exclude-dormant
1450     "t" gnus-summary-limit-to-age
1451     "x" gnus-summary-limit-to-extra
1452     "E" gnus-summary-limit-include-expunged
1453     "c" gnus-summary-limit-exclude-childless-dormant
1454     "C" gnus-summary-limit-mark-excluded-as-read)
1455
1456   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1457     "n" gnus-summary-next-unread-article
1458     "p" gnus-summary-prev-unread-article
1459     "N" gnus-summary-next-article
1460     "P" gnus-summary-prev-article
1461     "\C-n" gnus-summary-next-same-subject
1462     "\C-p" gnus-summary-prev-same-subject
1463     "\M-n" gnus-summary-next-unread-subject
1464     "\M-p" gnus-summary-prev-unread-subject
1465     "f" gnus-summary-first-unread-article
1466     "b" gnus-summary-best-unread-article
1467     "j" gnus-summary-goto-article
1468     "g" gnus-summary-goto-subject
1469     "l" gnus-summary-goto-last-article
1470     "o" gnus-summary-pop-article)
1471
1472   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1473     "k" gnus-summary-kill-thread
1474     "l" gnus-summary-lower-thread
1475     "i" gnus-summary-raise-thread
1476     "T" gnus-summary-toggle-threads
1477     "t" gnus-summary-rethread-current
1478     "^" gnus-summary-reparent-thread
1479     "s" gnus-summary-show-thread
1480     "S" gnus-summary-show-all-threads
1481     "h" gnus-summary-hide-thread
1482     "H" gnus-summary-hide-all-threads
1483     "n" gnus-summary-next-thread
1484     "p" gnus-summary-prev-thread
1485     "u" gnus-summary-up-thread
1486     "o" gnus-summary-top-thread
1487     "d" gnus-summary-down-thread
1488     "#" gnus-uu-mark-thread
1489     "\M-#" gnus-uu-unmark-thread)
1490
1491   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1492     "g" gnus-summary-prepare
1493     "c" gnus-summary-insert-cached-articles)
1494
1495   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1496     "c" gnus-summary-catchup-and-exit
1497     "C" gnus-summary-catchup-all-and-exit
1498     "E" gnus-summary-exit-no-update
1499     "Q" gnus-summary-exit
1500     "Z" gnus-summary-exit
1501     "n" gnus-summary-catchup-and-goto-next-group
1502     "R" gnus-summary-reselect-current-group
1503     "G" gnus-summary-rescan-group
1504     "N" gnus-summary-next-group
1505     "s" gnus-summary-save-newsrc
1506     "P" gnus-summary-prev-group)
1507
1508   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1509     " " gnus-summary-next-page
1510     "n" gnus-summary-next-page
1511     "\177" gnus-summary-prev-page
1512     [delete] gnus-summary-prev-page
1513     "p" gnus-summary-prev-page
1514     "\r" gnus-summary-scroll-up
1515     "\M-\r" gnus-summary-scroll-down
1516     "<" gnus-summary-beginning-of-article
1517     ">" gnus-summary-end-of-article
1518     "b" gnus-summary-beginning-of-article
1519     "e" gnus-summary-end-of-article
1520     "^" gnus-summary-refer-parent-article
1521     "r" gnus-summary-refer-parent-article
1522     "D" gnus-summary-enter-digest-group
1523     "R" gnus-summary-refer-references
1524     "T" gnus-summary-refer-thread
1525     "g" gnus-summary-show-article
1526     "s" gnus-summary-isearch-article
1527     "P" gnus-summary-print-article
1528     "t" gnus-article-babel)
1529
1530   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1531     "b" gnus-article-add-buttons
1532     "B" gnus-article-add-buttons-to-head
1533     "o" gnus-article-treat-overstrike
1534     "e" gnus-article-emphasize
1535     "w" gnus-article-fill-cited-article
1536     "Q" gnus-article-fill-long-lines
1537     "C" gnus-article-capitalize-sentences
1538     "c" gnus-article-remove-cr
1539     "q" gnus-article-de-quoted-unreadable
1540     "6" gnus-article-de-base64-unreadable
1541     "Z" gnus-article-decode-HZ
1542     "h" gnus-article-wash-html
1543     "f" gnus-article-display-x-face
1544     "l" gnus-summary-stop-page-breaking
1545     "r" gnus-summary-caesar-message
1546     "t" gnus-summary-toggle-header
1547     "v" gnus-summary-verbose-headers
1548     "H" gnus-article-strip-headers-in-body
1549     "d" gnus-article-treat-dumbquotes)
1550
1551   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1552     "a" gnus-article-hide
1553     "h" gnus-article-hide-headers
1554     "b" gnus-article-hide-boring-headers
1555     "s" gnus-article-hide-signature
1556     "c" gnus-article-hide-citation
1557     "C" gnus-article-hide-citation-in-followups
1558     "l" gnus-article-hide-list-identifiers
1559     "p" gnus-article-hide-pgp
1560     "B" gnus-article-strip-banner
1561     "P" gnus-article-hide-pem
1562     "\C-c" gnus-article-hide-citation-maybe)
1563
1564   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1565     "a" gnus-article-highlight
1566     "h" gnus-article-highlight-headers
1567     "c" gnus-article-highlight-citation
1568     "s" gnus-article-highlight-signature)
1569
1570   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1571     "w" gnus-article-decode-mime-words
1572     "c" gnus-article-decode-charset
1573     "v" gnus-mime-view-all-parts
1574     "b" gnus-article-view-part)
1575
1576   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1577     "z" gnus-article-date-ut
1578     "u" gnus-article-date-ut
1579     "l" gnus-article-date-local
1580     "e" gnus-article-date-lapsed
1581     "o" gnus-article-date-original
1582     "i" gnus-article-date-iso8601
1583     "s" gnus-article-date-user)
1584
1585   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1586     "t" gnus-article-remove-trailing-blank-lines
1587     "l" gnus-article-strip-leading-blank-lines
1588     "m" gnus-article-strip-multiple-blank-lines
1589     "a" gnus-article-strip-blank-lines
1590     "A" gnus-article-strip-all-blank-lines
1591     "s" gnus-article-strip-leading-space
1592     "e" gnus-article-strip-trailing-space)
1593
1594   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1595     "v" gnus-version
1596     "f" gnus-summary-fetch-faq
1597     "d" gnus-summary-describe-group
1598     "h" gnus-summary-describe-briefly
1599     "i" gnus-info-find-node)
1600
1601   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1602     "e" gnus-summary-expire-articles
1603     "\M-\C-e" gnus-summary-expire-articles-now
1604     "\177" gnus-summary-delete-article
1605     [delete] gnus-summary-delete-article
1606     [backspace] gnus-summary-delete-article
1607     "m" gnus-summary-move-article
1608     "r" gnus-summary-respool-article
1609     "w" gnus-summary-edit-article
1610     "c" gnus-summary-copy-article
1611     "B" gnus-summary-crosspost-article
1612     "q" gnus-summary-respool-query
1613     "t" gnus-summary-respool-trace
1614     "i" gnus-summary-import-article
1615     "p" gnus-summary-article-posted-p)
1616
1617   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1618     "o" gnus-summary-save-article
1619     "m" gnus-summary-save-article-mail
1620     "F" gnus-summary-write-article-file
1621     "r" gnus-summary-save-article-rmail
1622     "f" gnus-summary-save-article-file
1623     "b" gnus-summary-save-article-body-file
1624     "h" gnus-summary-save-article-folder
1625     "v" gnus-summary-save-article-vm
1626     "p" gnus-summary-pipe-output
1627     "s" gnus-soup-add-article)
1628
1629   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1630     "b" gnus-summary-display-buttonized
1631     "m" gnus-summary-repair-multipart
1632     "v" gnus-article-view-part
1633     "o" gnus-article-save-part
1634     "c" gnus-article-copy-part
1635     "e" gnus-article-externalize-part
1636     "i" gnus-article-inline-part
1637     "|" gnus-article-pipe-part))
1638
1639 (defun gnus-summary-make-menu-bar ()
1640   (gnus-turn-off-edit-menu 'summary)
1641
1642   (unless (boundp 'gnus-summary-misc-menu)
1643
1644     (easy-menu-define
1645      gnus-summary-kill-menu gnus-summary-mode-map ""
1646      (cons
1647       "Score"
1648       (nconc
1649        (list
1650         ["Enter score..." gnus-summary-score-entry t]
1651         ["Customize" gnus-score-customize t])
1652        (gnus-make-score-map 'increase)
1653        (gnus-make-score-map 'lower)
1654        '(("Mark"
1655           ["Kill below" gnus-summary-kill-below t]
1656           ["Mark above" gnus-summary-mark-above t]
1657           ["Tick above" gnus-summary-tick-above t]
1658           ["Clear above" gnus-summary-clear-above t])
1659          ["Current score" gnus-summary-current-score t]
1660          ["Set score" gnus-summary-set-score t]
1661          ["Switch current score file..." gnus-score-change-score-file t]
1662          ["Set mark below..." gnus-score-set-mark-below t]
1663          ["Set expunge below..." gnus-score-set-expunge-below t]
1664          ["Edit current score file" gnus-score-edit-current-scores t]
1665          ["Edit score file" gnus-score-edit-file t]
1666          ["Trace score" gnus-score-find-trace t]
1667          ["Find words" gnus-score-find-favourite-words t]
1668          ["Rescore buffer" gnus-summary-rescore t]
1669          ["Increase score..." gnus-summary-increase-score t]
1670          ["Lower score..." gnus-summary-lower-score t]))))
1671
1672     ;; Define both the Article menu in the summary buffer and the equivalent
1673     ;; Commands menu in the article buffer here for consistency.
1674     (let ((innards
1675            '(("Hide"
1676               ["All" gnus-article-hide t]
1677               ["Headers" gnus-article-hide-headers t]
1678               ["Signature" gnus-article-hide-signature t]
1679               ["Citation" gnus-article-hide-citation t]
1680               ["List identifiers" gnus-article-hide-list-identifiers t]
1681               ["PGP" gnus-article-hide-pgp t]
1682               ["Banner" gnus-article-strip-banner t]
1683               ["Boring headers" gnus-article-hide-boring-headers t])
1684              ("Highlight"
1685               ["All" gnus-article-highlight t]
1686               ["Headers" gnus-article-highlight-headers t]
1687               ["Signature" gnus-article-highlight-signature t]
1688               ["Citation" gnus-article-highlight-citation t])
1689              ("MIME"
1690               ["Words" gnus-article-decode-mime-words t]
1691               ["Charset" gnus-article-decode-charset t]
1692               ["QP" gnus-article-de-quoted-unreadable t]
1693               ["Base64" gnus-article-de-base64-unreadable t]
1694               ["View all" gnus-mime-view-all-parts t])
1695              ("Date"
1696               ["Local" gnus-article-date-local t]
1697               ["ISO8601" gnus-article-date-iso8601 t]
1698               ["UT" gnus-article-date-ut t]
1699               ["Original" gnus-article-date-original t]
1700               ["Lapsed" gnus-article-date-lapsed t]
1701               ["User-defined" gnus-article-date-user t])
1702              ("Washing"
1703               ("Remove Blanks"
1704                ["Leading" gnus-article-strip-leading-blank-lines t]
1705                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1706                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1707                ["All of the above" gnus-article-strip-blank-lines t]
1708                ["All" gnus-article-strip-all-blank-lines t]
1709                ["Leading space" gnus-article-strip-leading-space t]
1710                ["Trailing space" gnus-article-strip-trailing-space t])
1711               ["Overstrike" gnus-article-treat-overstrike t]
1712               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1713               ["Emphasis" gnus-article-emphasize t]
1714               ["Word wrap" gnus-article-fill-cited-article t]
1715               ["Fill long lines" gnus-article-fill-long-lines t]
1716               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1717               ["CR" gnus-article-remove-cr t]
1718               ["Show X-Face" gnus-article-display-x-face t]
1719               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1720               ["Base64" gnus-article-de-base64-unreadable t]
1721               ["Rot 13" gnus-summary-caesar-message t]
1722               ["Unix pipe" gnus-summary-pipe-message t]
1723               ["Add buttons" gnus-article-add-buttons t]
1724               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1725               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1726               ["Verbose header" gnus-summary-verbose-headers t]
1727               ["Toggle header" gnus-summary-toggle-header t]
1728               ["Html" gnus-article-wash-html t]
1729               ["HZ" gnus-article-decode-HZ t])
1730              ("Output"
1731               ["Save in default format" gnus-summary-save-article t]
1732               ["Save in file" gnus-summary-save-article-file t]
1733               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1734               ["Save in MH folder" gnus-summary-save-article-folder t]
1735               ["Save in VM folder" gnus-summary-save-article-vm t]
1736               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1737               ["Save body in file" gnus-summary-save-article-body-file t]
1738               ["Pipe through a filter" gnus-summary-pipe-output t]
1739               ["Add to SOUP packet" gnus-soup-add-article t]
1740               ["Print" gnus-summary-print-article t])
1741              ("Backend"
1742               ["Respool article..." gnus-summary-respool-article t]
1743               ["Move article..." gnus-summary-move-article
1744                (gnus-check-backend-function
1745                 'request-move-article gnus-newsgroup-name)]
1746               ["Copy article..." gnus-summary-copy-article t]
1747               ["Crosspost article..." gnus-summary-crosspost-article
1748                (gnus-check-backend-function
1749                 'request-replace-article gnus-newsgroup-name)]
1750               ["Import file..." gnus-summary-import-article t]
1751               ["Check if posted" gnus-summary-article-posted-p t]
1752               ["Edit article" gnus-summary-edit-article
1753                (not (gnus-group-read-only-p))]
1754               ["Delete article" gnus-summary-delete-article
1755                (gnus-check-backend-function
1756                 'request-expire-articles gnus-newsgroup-name)]
1757               ["Query respool" gnus-summary-respool-query t]
1758               ["Trace respool" gnus-summary-respool-trace t]
1759               ["Delete expirable articles" gnus-summary-expire-articles-now
1760                (gnus-check-backend-function
1761                 'request-expire-articles gnus-newsgroup-name)])
1762              ("Extract"
1763               ["Uudecode" gnus-uu-decode-uu t]
1764               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1765               ["Unshar" gnus-uu-decode-unshar t]
1766               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1767               ["Save" gnus-uu-decode-save t]
1768               ["Binhex" gnus-uu-decode-binhex t]
1769               ["Postscript" gnus-uu-decode-postscript t])
1770              ("Cache"
1771               ["Enter article" gnus-cache-enter-article t]
1772               ["Remove article" gnus-cache-remove-article t])
1773              ["Translate" gnus-article-babel t]
1774              ["Select article buffer" gnus-summary-select-article-buffer t]
1775              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1776              ["Isearch article..." gnus-summary-isearch-article t]
1777              ["Beginning of the article" gnus-summary-beginning-of-article t]
1778              ["End of the article" gnus-summary-end-of-article t]
1779              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1780              ["Fetch referenced articles" gnus-summary-refer-references t]
1781              ["Fetch current thread" gnus-summary-refer-thread t]
1782              ["Fetch article with id..." gnus-summary-refer-article t]
1783              ["Redisplay" gnus-summary-show-article t])))
1784       (easy-menu-define
1785        gnus-summary-article-menu gnus-summary-mode-map ""
1786        (cons "Article" innards))
1787
1788       (easy-menu-define
1789        gnus-article-commands-menu gnus-article-mode-map ""
1790        (cons "Commands" innards)))
1791
1792     (easy-menu-define
1793      gnus-summary-thread-menu gnus-summary-mode-map ""
1794      '("Threads"
1795        ["Toggle threading" gnus-summary-toggle-threads t]
1796        ["Hide threads" gnus-summary-hide-all-threads t]
1797        ["Show threads" gnus-summary-show-all-threads t]
1798        ["Hide thread" gnus-summary-hide-thread t]
1799        ["Show thread" gnus-summary-show-thread t]
1800        ["Go to next thread" gnus-summary-next-thread t]
1801        ["Go to previous thread" gnus-summary-prev-thread t]
1802        ["Go down thread" gnus-summary-down-thread t]
1803        ["Go up thread" gnus-summary-up-thread t]
1804        ["Top of thread" gnus-summary-top-thread t]
1805        ["Mark thread as read" gnus-summary-kill-thread t]
1806        ["Lower thread score" gnus-summary-lower-thread t]
1807        ["Raise thread score" gnus-summary-raise-thread t]
1808        ["Rethread current" gnus-summary-rethread-current t]))
1809
1810     (easy-menu-define
1811      gnus-summary-post-menu gnus-summary-mode-map ""
1812      '("Post"
1813        ["Post an article" gnus-summary-post-news t]
1814        ["Followup" gnus-summary-followup t]
1815        ["Followup and yank" gnus-summary-followup-with-original t]
1816        ["Supersede article" gnus-summary-supersede-article t]
1817        ["Cancel article" gnus-summary-cancel-article t]
1818        ["Reply" gnus-summary-reply t]
1819        ["Reply and yank" gnus-summary-reply-with-original t]
1820        ["Wide reply" gnus-summary-wide-reply t]
1821        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1822        ["Mail forward" gnus-summary-mail-forward t]
1823        ["Post forward" gnus-summary-post-forward t]
1824        ["Digest and mail" gnus-uu-digest-mail-forward t]
1825        ["Digest and post" gnus-uu-digest-post-forward t]
1826        ["Resend message" gnus-summary-resend-message t]
1827        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1828        ["Send a mail" gnus-summary-mail-other-window t]
1829        ["Uuencode and post" gnus-uu-post-news t]
1830        ["Followup via news" gnus-summary-followup-to-mail t]
1831        ["Followup via news and yank"
1832         gnus-summary-followup-to-mail-with-original t]
1833        ;;("Draft"
1834        ;;["Send" gnus-summary-send-draft t]
1835        ;;["Send bounced" gnus-resend-bounced-mail t])
1836        ))
1837
1838     (easy-menu-define
1839      gnus-summary-misc-menu gnus-summary-mode-map ""
1840      '("Misc"
1841        ("Mark Read"
1842         ["Mark as read" gnus-summary-mark-as-read-forward t]
1843         ["Mark same subject and select"
1844          gnus-summary-kill-same-subject-and-select t]
1845         ["Mark same subject" gnus-summary-kill-same-subject t]
1846         ["Catchup" gnus-summary-catchup t]
1847         ["Catchup all" gnus-summary-catchup-all t]
1848         ["Catchup to here" gnus-summary-catchup-to-here t]
1849         ["Catchup region" gnus-summary-mark-region-as-read t]
1850         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1851        ("Mark Various"
1852         ["Tick" gnus-summary-tick-article-forward t]
1853         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1854         ["Remove marks" gnus-summary-clear-mark-forward t]
1855         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1856         ["Set bookmark" gnus-summary-set-bookmark t]
1857         ["Remove bookmark" gnus-summary-remove-bookmark t])
1858        ("Mark Limit"
1859         ["Marks..." gnus-summary-limit-to-marks t]
1860         ["Subject..." gnus-summary-limit-to-subject t]
1861         ["Author..." gnus-summary-limit-to-author t]
1862         ["Age..." gnus-summary-limit-to-age t]
1863         ["Extra..." gnus-summary-limit-to-extra t]
1864         ["Score" gnus-summary-limit-to-score t]
1865         ["Unread" gnus-summary-limit-to-unread t]
1866         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1867         ["Articles" gnus-summary-limit-to-articles t]
1868         ["Pop limit" gnus-summary-pop-limit t]
1869         ["Show dormant" gnus-summary-limit-include-dormant t]
1870         ["Hide childless dormant"
1871          gnus-summary-limit-exclude-childless-dormant t]
1872         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1873         ["Hide marked" gnus-summary-limit-exclude-marks t]
1874         ["Show expunged" gnus-summary-show-all-expunged t])
1875        ("Process Mark"
1876         ["Set mark" gnus-summary-mark-as-processable t]
1877         ["Remove mark" gnus-summary-unmark-as-processable t]
1878         ["Remove all marks" gnus-summary-unmark-all-processable t]
1879         ["Mark above" gnus-uu-mark-over t]
1880         ["Mark series" gnus-uu-mark-series t]
1881         ["Mark region" gnus-uu-mark-region t]
1882         ["Unmark region" gnus-uu-unmark-region t]
1883         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1884         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1885         ["Mark all" gnus-uu-mark-all t]
1886         ["Mark buffer" gnus-uu-mark-buffer t]
1887         ["Mark sparse" gnus-uu-mark-sparse t]
1888         ["Mark thread" gnus-uu-mark-thread t]
1889         ["Unmark thread" gnus-uu-unmark-thread t]
1890         ("Process Mark Sets"
1891          ["Kill" gnus-summary-kill-process-mark t]
1892          ["Yank" gnus-summary-yank-process-mark
1893           gnus-newsgroup-process-stack]
1894          ["Save" gnus-summary-save-process-mark t]))
1895        ("Scroll article"
1896         ["Page forward" gnus-summary-next-page t]
1897         ["Page backward" gnus-summary-prev-page t]
1898         ["Line forward" gnus-summary-scroll-up t])
1899        ("Move"
1900         ["Next unread article" gnus-summary-next-unread-article t]
1901         ["Previous unread article" gnus-summary-prev-unread-article t]
1902         ["Next article" gnus-summary-next-article t]
1903         ["Previous article" gnus-summary-prev-article t]
1904         ["Next unread subject" gnus-summary-next-unread-subject t]
1905         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1906         ["Next article same subject" gnus-summary-next-same-subject t]
1907         ["Previous article same subject" gnus-summary-prev-same-subject t]
1908         ["First unread article" gnus-summary-first-unread-article t]
1909         ["Best unread article" gnus-summary-best-unread-article t]
1910         ["Go to subject number..." gnus-summary-goto-subject t]
1911         ["Go to article number..." gnus-summary-goto-article t]
1912         ["Go to the last article" gnus-summary-goto-last-article t]
1913         ["Pop article off history" gnus-summary-pop-article t])
1914        ("Sort"
1915         ["Sort by number" gnus-summary-sort-by-number t]
1916         ["Sort by author" gnus-summary-sort-by-author t]
1917         ["Sort by subject" gnus-summary-sort-by-subject t]
1918         ["Sort by date" gnus-summary-sort-by-date t]
1919         ["Sort by score" gnus-summary-sort-by-score t]
1920         ["Sort by lines" gnus-summary-sort-by-lines t]
1921         ["Sort by characters" gnus-summary-sort-by-chars t])
1922        ("Help"
1923         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1924         ["Describe group" gnus-summary-describe-group t]
1925         ["Read manual" gnus-info-find-node t])
1926        ("Modes"
1927         ["Pick and read" gnus-pick-mode t]
1928         ["Binary" gnus-binary-mode t])
1929        ("Regeneration"
1930         ["Regenerate" gnus-summary-prepare t]
1931         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1932         ["Toggle threading" gnus-summary-toggle-threads t])
1933        ["Filter articles..." gnus-summary-execute-command t]
1934        ["Run command on subjects..." gnus-summary-universal-argument t]
1935        ["Search articles forward..." gnus-summary-search-article-forward t]
1936        ["Search articles backward..." gnus-summary-search-article-backward t]
1937        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1938        ["Expand window" gnus-summary-expand-window t]
1939        ["Expire expirable articles" gnus-summary-expire-articles
1940         (gnus-check-backend-function
1941          'request-expire-articles gnus-newsgroup-name)]
1942        ["Edit local kill file" gnus-summary-edit-local-kill t]
1943        ["Edit main kill file" gnus-summary-edit-global-kill t]
1944        ["Edit group parameters" gnus-summary-edit-parameters t]
1945        ["Customize group parameters" gnus-summary-customize-parameters t]
1946        ["Send a bug report" gnus-bug t]
1947        ("Exit"
1948         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1949         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1950         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1951         ["Exit group" gnus-summary-exit t]
1952         ["Exit group without updating" gnus-summary-exit-no-update t]
1953         ["Exit and goto next group" gnus-summary-next-group t]
1954         ["Exit and goto prev group" gnus-summary-prev-group t]
1955         ["Reselect group" gnus-summary-reselect-current-group t]
1956         ["Rescan group" gnus-summary-rescan-group t]
1957         ["Update dribble" gnus-summary-save-newsrc t])))
1958
1959     (gnus-run-hooks 'gnus-summary-menu-hook)))
1960
1961 (defun gnus-score-set-default (var value)
1962   "A version of set that updates the GNU Emacs menu-bar."
1963   (set var value)
1964   ;; It is the message that forces the active status to be updated.
1965   (message ""))
1966
1967 (defun gnus-make-score-map (type)
1968   "Make a summary score map of type TYPE."
1969   (if t
1970       nil
1971     (let ((headers '(("author" "from" string)
1972                      ("subject" "subject" string)
1973                      ("article body" "body" string)
1974                      ("article head" "head" string)
1975                      ("xref" "xref" string)
1976                      ("extra header" "extra" string)
1977                      ("lines" "lines" number)
1978                      ("followups to author" "followup" string)))
1979           (types '((number ("less than" <)
1980                            ("greater than" >)
1981                            ("equal" =))
1982                    (string ("substring" s)
1983                            ("exact string" e)
1984                            ("fuzzy string" f)
1985                            ("regexp" r))))
1986           (perms '(("temporary" (current-time-string))
1987                    ("permanent" nil)
1988                    ("immediate" now)))
1989           header)
1990       (list
1991        (apply
1992         'nconc
1993         (list
1994          (if (eq type 'lower)
1995              "Lower score"
1996            "Increase score"))
1997         (let (outh)
1998           (while headers
1999             (setq header (car headers))
2000             (setq outh
2001                   (cons
2002                    (apply
2003                     'nconc
2004                     (list (car header))
2005                     (let ((ts (cdr (assoc (nth 2 header) types)))
2006                           outt)
2007                       (while ts
2008                         (setq outt
2009                               (cons
2010                                (apply
2011                                 'nconc
2012                                 (list (caar ts))
2013                                 (let ((ps perms)
2014                                       outp)
2015                                   (while ps
2016                                     (setq outp
2017                                           (cons
2018                                            (vector
2019                                             (caar ps)
2020                                             (list
2021                                              'gnus-summary-score-entry
2022                                              (nth 1 header)
2023                                              (if (or (string= (nth 1 header)
2024                                                               "head")
2025                                                      (string= (nth 1 header)
2026                                                               "body"))
2027                                                  ""
2028                                                (list 'gnus-summary-header
2029                                                      (nth 1 header)))
2030                                              (list 'quote (nth 1 (car ts)))
2031                                              (list 'gnus-score-delta-default
2032                                                    nil)
2033                                              (nth 1 (car ps))
2034                                              t)
2035                                             t)
2036                                            outp))
2037                                     (setq ps (cdr ps)))
2038                                   (list (nreverse outp))))
2039                                outt))
2040                         (setq ts (cdr ts)))
2041                       (list (nreverse outt))))
2042                    outh))
2043             (setq headers (cdr headers)))
2044           (list (nreverse outh))))))))
2045
2046 \f
2047
2048 (defun gnus-summary-mode (&optional group)
2049   "Major mode for reading articles.
2050
2051 All normal editing commands are switched off.
2052 \\<gnus-summary-mode-map>
2053 Each line in this buffer represents one article.  To read an
2054 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2055 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2056 respectively.
2057
2058 You can also post articles and send mail from this buffer.  To
2059 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2060 of an article, type `\\[gnus-summary-reply]'.
2061
2062 There are approx. one gazillion commands you can execute in this
2063 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2064
2065 The following commands are available:
2066
2067 \\{gnus-summary-mode-map}"
2068   (interactive)
2069   (when (gnus-visual-p 'summary-menu 'menu)
2070     (gnus-summary-make-menu-bar))
2071   (kill-all-local-variables)
2072   (gnus-summary-make-local-variables)
2073   (gnus-make-thread-indent-array)
2074   (gnus-simplify-mode-line)
2075   (setq major-mode 'gnus-summary-mode)
2076   (setq mode-name "Summary")
2077   (make-local-variable 'minor-mode-alist)
2078   (use-local-map gnus-summary-mode-map)
2079   (buffer-disable-undo)
2080   (setq buffer-read-only t)             ;Disable modification
2081   (setq truncate-lines t)
2082   (setq selective-display t)
2083   (setq selective-display-ellipses t)   ;Display `...'
2084   (gnus-summary-set-display-table)
2085   (gnus-set-default-directory)
2086   (setq gnus-newsgroup-name group)
2087   (make-local-variable 'gnus-summary-line-format)
2088   (make-local-variable 'gnus-summary-line-format-spec)
2089   (make-local-variable 'gnus-summary-dummy-line-format)
2090   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2091   (make-local-variable 'gnus-summary-mark-positions)
2092   (make-local-hook 'pre-command-hook)
2093   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2094   (gnus-run-hooks 'gnus-summary-mode-hook)
2095   (mm-enable-multibyte)
2096   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2097   (gnus-update-summary-mark-positions))
2098
2099 (defun gnus-summary-make-local-variables ()
2100   "Make all the local summary buffer variables."
2101   (let (global)
2102     (dolist (local gnus-summary-local-variables)
2103       (if (consp local)
2104           (progn
2105             (if (eq (cdr local) 'global)
2106                 ;; Copy the global value of the variable.
2107                 (setq global (symbol-value (car local)))
2108               ;; Use the value from the list.
2109               (setq global (eval (cdr local))))
2110             (set (make-local-variable (car local)) global))
2111         ;; Simple nil-valued local variable.
2112         (set (make-local-variable local) nil)))))
2113
2114 (defun gnus-summary-clear-local-variables ()
2115   (let ((locals gnus-summary-local-variables))
2116     (while locals
2117       (if (consp (car locals))
2118           (and (vectorp (caar locals))
2119                (set (caar locals) nil))
2120         (and (vectorp (car locals))
2121              (set (car locals) nil)))
2122       (setq locals (cdr locals)))))
2123
2124 ;; Summary data functions.
2125
2126 (defmacro gnus-data-number (data)
2127   `(car ,data))
2128
2129 (defmacro gnus-data-set-number (data number)
2130   `(setcar ,data ,number))
2131
2132 (defmacro gnus-data-mark (data)
2133   `(nth 1 ,data))
2134
2135 (defmacro gnus-data-set-mark (data mark)
2136   `(setcar (nthcdr 1 ,data) ,mark))
2137
2138 (defmacro gnus-data-pos (data)
2139   `(nth 2 ,data))
2140
2141 (defmacro gnus-data-set-pos (data pos)
2142   `(setcar (nthcdr 2 ,data) ,pos))
2143
2144 (defmacro gnus-data-header (data)
2145   `(nth 3 ,data))
2146
2147 (defmacro gnus-data-set-header (data header)
2148   `(setf (nth 3 ,data) ,header))
2149
2150 (defmacro gnus-data-level (data)
2151   `(nth 4 ,data))
2152
2153 (defmacro gnus-data-unread-p (data)
2154   `(= (nth 1 ,data) gnus-unread-mark))
2155
2156 (defmacro gnus-data-read-p (data)
2157   `(/= (nth 1 ,data) gnus-unread-mark))
2158
2159 (defmacro gnus-data-pseudo-p (data)
2160   `(consp (nth 3 ,data)))
2161
2162 (defmacro gnus-data-find (number)
2163   `(assq ,number gnus-newsgroup-data))
2164
2165 (defmacro gnus-data-find-list (number &optional data)
2166   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2167      (memq (assq ,number bdata)
2168            bdata)))
2169
2170 (defmacro gnus-data-make (number mark pos header level)
2171   `(list ,number ,mark ,pos ,header ,level))
2172
2173 (defun gnus-data-enter (after-article number mark pos header level offset)
2174   (let ((data (gnus-data-find-list after-article)))
2175     (unless data
2176       (error "No such article: %d" after-article))
2177     (setcdr data (cons (gnus-data-make number mark pos header level)
2178                        (cdr data)))
2179     (setq gnus-newsgroup-data-reverse nil)
2180     (gnus-data-update-list (cddr data) offset)))
2181
2182 (defun gnus-data-enter-list (after-article list &optional offset)
2183   (when list
2184     (let ((data (and after-article (gnus-data-find-list after-article)))
2185           (ilist list))
2186       (if (not (or data
2187                    after-article))
2188           (let ((odata gnus-newsgroup-data))
2189             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2190             (when offset
2191               (gnus-data-update-list odata offset)))
2192         ;; Find the last element in the list to be spliced into the main
2193         ;; list.
2194         (while (cdr list)
2195           (setq list (cdr list)))
2196         (if (not data)
2197             (progn
2198               (setcdr list gnus-newsgroup-data)
2199               (setq gnus-newsgroup-data ilist)
2200               (when offset
2201                 (gnus-data-update-list (cdr list) offset)))
2202           (setcdr list (cdr data))
2203           (setcdr data ilist)
2204           (when offset
2205             (gnus-data-update-list (cdr list) offset))))
2206       (setq gnus-newsgroup-data-reverse nil))))
2207
2208 (defun gnus-data-remove (article &optional offset)
2209   (let ((data gnus-newsgroup-data))
2210     (if (= (gnus-data-number (car data)) article)
2211         (progn
2212           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2213                 gnus-newsgroup-data-reverse nil)
2214           (when offset
2215             (gnus-data-update-list gnus-newsgroup-data offset)))
2216       (while (cdr data)
2217         (when (= (gnus-data-number (cadr data)) article)
2218           (setcdr data (cddr data))
2219           (when offset
2220             (gnus-data-update-list (cdr data) offset))
2221           (setq data nil
2222                 gnus-newsgroup-data-reverse nil))
2223         (setq data (cdr data))))))
2224
2225 (defmacro gnus-data-list (backward)
2226   `(if ,backward
2227        (or gnus-newsgroup-data-reverse
2228            (setq gnus-newsgroup-data-reverse
2229                  (reverse gnus-newsgroup-data)))
2230      gnus-newsgroup-data))
2231
2232 (defun gnus-data-update-list (data offset)
2233   "Add OFFSET to the POS of all data entries in DATA."
2234   (setq gnus-newsgroup-data-reverse nil)
2235   (while data
2236     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2237     (setq data (cdr data))))
2238
2239 (defun gnus-summary-article-pseudo-p (article)
2240   "Say whether this article is a pseudo article or not."
2241   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2242
2243 (defmacro gnus-summary-article-sparse-p (article)
2244   "Say whether this article is a sparse article or not."
2245   `(memq ,article gnus-newsgroup-sparse))
2246
2247 (defmacro gnus-summary-article-ancient-p (article)
2248   "Say whether this article is a sparse article or not."
2249   `(memq ,article gnus-newsgroup-ancient))
2250
2251 (defun gnus-article-parent-p (number)
2252   "Say whether this article is a parent or not."
2253   (let ((data (gnus-data-find-list number)))
2254     (and (cdr data)                     ; There has to be an article after...
2255          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2256             (gnus-data-level (nth 1 data))))))
2257
2258 (defun gnus-article-children (number)
2259   "Return a list of all children to NUMBER."
2260   (let* ((data (gnus-data-find-list number))
2261          (level (gnus-data-level (car data)))
2262          children)
2263     (setq data (cdr data))
2264     (while (and data
2265                 (= (gnus-data-level (car data)) (1+ level)))
2266       (push (gnus-data-number (car data)) children)
2267       (setq data (cdr data)))
2268     children))
2269
2270 (defmacro gnus-summary-skip-intangible ()
2271   "If the current article is intangible, then jump to a different article."
2272   '(let ((to (get-text-property (point) 'gnus-intangible)))
2273      (and to (gnus-summary-goto-subject to))))
2274
2275 (defmacro gnus-summary-article-intangible-p ()
2276   "Say whether this article is intangible or not."
2277   '(get-text-property (point) 'gnus-intangible))
2278
2279 (defun gnus-article-read-p (article)
2280   "Say whether ARTICLE is read or not."
2281   (not (or (memq article gnus-newsgroup-marked)
2282            (memq article gnus-newsgroup-unreads)
2283            (memq article gnus-newsgroup-unselected)
2284            (memq article gnus-newsgroup-dormant))))
2285
2286 ;; Some summary mode macros.
2287
2288 (defmacro gnus-summary-article-number ()
2289   "The article number of the article on the current line.
2290 If there isn's an article number here, then we return the current
2291 article number."
2292   '(progn
2293      (gnus-summary-skip-intangible)
2294      (or (get-text-property (point) 'gnus-number)
2295          (gnus-summary-last-subject))))
2296
2297 (defmacro gnus-summary-article-header (&optional number)
2298   "Return the header of article NUMBER."
2299   `(gnus-data-header (gnus-data-find
2300                       ,(or number '(gnus-summary-article-number)))))
2301
2302 (defmacro gnus-summary-thread-level (&optional number)
2303   "Return the level of thread that starts with article NUMBER."
2304   `(if (and (eq gnus-summary-make-false-root 'dummy)
2305             (get-text-property (point) 'gnus-intangible))
2306        0
2307      (gnus-data-level (gnus-data-find
2308                        ,(or number '(gnus-summary-article-number))))))
2309
2310 (defmacro gnus-summary-article-mark (&optional number)
2311   "Return the mark of article NUMBER."
2312   `(gnus-data-mark (gnus-data-find
2313                     ,(or number '(gnus-summary-article-number)))))
2314
2315 (defmacro gnus-summary-article-pos (&optional number)
2316   "Return the position of the line of article NUMBER."
2317   `(gnus-data-pos (gnus-data-find
2318                    ,(or number '(gnus-summary-article-number)))))
2319
2320 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2321 (defmacro gnus-summary-article-subject (&optional number)
2322   "Return current subject string or nil if nothing."
2323   `(let ((headers
2324           ,(if number
2325                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2326              '(gnus-data-header (assq (gnus-summary-article-number)
2327                                       gnus-newsgroup-data)))))
2328      (and headers
2329           (vectorp headers)
2330           (mail-header-subject headers))))
2331
2332 (defmacro gnus-summary-article-score (&optional number)
2333   "Return current article score."
2334   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2335                   gnus-newsgroup-scored))
2336        gnus-summary-default-score 0))
2337
2338 (defun gnus-summary-article-children (&optional number)
2339   "Return a list of article numbers that are children of article NUMBER."
2340   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2341          (level (gnus-data-level (car data)))
2342          l children)
2343     (while (and (setq data (cdr data))
2344                 (> (setq l (gnus-data-level (car data))) level))
2345       (and (= (1+ level) l)
2346            (push (gnus-data-number (car data))
2347                  children)))
2348     (nreverse children)))
2349
2350 (defun gnus-summary-article-parent (&optional number)
2351   "Return the article number of the parent of article NUMBER."
2352   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2353                                     (gnus-data-list t)))
2354          (level (gnus-data-level (car data))))
2355     (if (zerop level)
2356         ()                              ; This is a root.
2357       ;; We search until we find an article with a level less than
2358       ;; this one.  That function has to be the parent.
2359       (while (and (setq data (cdr data))
2360                   (not (< (gnus-data-level (car data)) level))))
2361       (and data (gnus-data-number (car data))))))
2362
2363 (defun gnus-unread-mark-p (mark)
2364   "Say whether MARK is the unread mark."
2365   (= mark gnus-unread-mark))
2366
2367 (defun gnus-read-mark-p (mark)
2368   "Say whether MARK is one of the marks that mark as read.
2369 This is all marks except unread, ticked, dormant, and expirable."
2370   (not (or (= mark gnus-unread-mark)
2371            (= mark gnus-ticked-mark)
2372            (= mark gnus-dormant-mark)
2373            (= mark gnus-expirable-mark))))
2374
2375 (defmacro gnus-article-mark (number)
2376   "Return the MARK of article NUMBER.
2377 This macro should only be used when computing the mark the \"first\"
2378 time; i.e., when generating the summary lines.  After that,
2379 `gnus-summary-article-mark' should be used to examine the
2380 marks of articles."
2381   `(cond
2382     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2383     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2384     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2385     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2386     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2387     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2388     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2389     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2390            gnus-ancient-mark))))
2391
2392 ;; Saving hidden threads.
2393
2394 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2395 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2396
2397 (defmacro gnus-save-hidden-threads (&rest forms)
2398   "Save hidden threads, eval FORMS, and restore the hidden threads."
2399   (let ((config (make-symbol "config")))
2400     `(let ((,config (gnus-hidden-threads-configuration)))
2401        (unwind-protect
2402            (save-excursion
2403              ,@forms)
2404          (gnus-restore-hidden-threads-configuration ,config)))))
2405
2406 (defun gnus-data-compute-positions ()
2407   "Compute the positions of all articles."
2408   (setq gnus-newsgroup-data-reverse nil)
2409   (let ((data gnus-newsgroup-data))
2410     (save-excursion
2411       (gnus-save-hidden-threads
2412         (gnus-summary-show-all-threads)
2413         (goto-char (point-min))
2414         (while data
2415           (while (get-text-property (point) 'gnus-intangible)
2416             (forward-line 1))
2417           (gnus-data-set-pos (car data) (+ (point) 3))
2418           (setq data (cdr data))
2419           (forward-line 1))))))
2420
2421 (defun gnus-hidden-threads-configuration ()
2422   "Return the current hidden threads configuration."
2423   (save-excursion
2424     (let (config)
2425       (goto-char (point-min))
2426       (while (search-forward "\r" nil t)
2427         (push (1- (point)) config))
2428       config)))
2429
2430 (defun gnus-restore-hidden-threads-configuration (config)
2431   "Restore hidden threads configuration from CONFIG."
2432   (let (point buffer-read-only)
2433     (while (setq point (pop config))
2434       (when (and (< point (point-max))
2435                  (goto-char point)
2436                  (eq (char-after) ?\n))
2437         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2438
2439 ;; Various summary mode internalish functions.
2440
2441 (defun gnus-mouse-pick-article (e)
2442   (interactive "e")
2443   (mouse-set-point e)
2444   (gnus-summary-next-page nil t))
2445
2446 (defun gnus-summary-set-display-table ()
2447   "Change the display table.
2448 Odd characters have a tendency to mess
2449 up nicely formatted displays - we make all possible glyphs
2450 display only a single character."
2451
2452   ;; We start from the standard display table, if any.
2453   (let ((table (or (copy-sequence standard-display-table)
2454                    (make-display-table)))
2455         (i 32))
2456     ;; Nix out all the control chars...
2457     (while (>= (setq i (1- i)) 0)
2458       (aset table i [??]))
2459     ;; ... but not newline and cr, of course.  (cr is necessary for the
2460     ;; selective display).
2461     (aset table ?\n nil)
2462     (aset table ?\r nil)
2463     ;; We keep TAB as well.
2464     (aset table ?\t nil)
2465     ;; We nix out any glyphs over 126 that are not set already.
2466     (let ((i 256))
2467       (while (>= (setq i (1- i)) 127)
2468         ;; Only modify if the entry is nil.
2469         (unless (aref table i)
2470           (aset table i [??]))))
2471     (setq buffer-display-table table)))
2472
2473 (defun gnus-summary-setup-buffer (group)
2474   "Initialize summary buffer."
2475   (let ((buffer (concat "*Summary " group "*")))
2476     (if (get-buffer buffer)
2477         (progn
2478           (set-buffer buffer)
2479           (setq gnus-summary-buffer (current-buffer))
2480           (not gnus-newsgroup-prepared))
2481       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2482       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2483       (gnus-summary-mode group)
2484       (when gnus-carpal
2485         (gnus-carpal-setup-buffer 'summary))
2486       (unless gnus-single-article-buffer
2487         (make-local-variable 'gnus-article-buffer)
2488         (make-local-variable 'gnus-article-current)
2489         (make-local-variable 'gnus-original-article-buffer))
2490       (setq gnus-newsgroup-name group)
2491       t)))
2492
2493 (defun gnus-set-global-variables ()
2494   "Set the global equivalents of the buffer-local variables.
2495 They are set to the latest values they had.  These reflect the summary
2496 buffer that was in action when the last article was fetched."
2497   (when (eq major-mode 'gnus-summary-mode)
2498     (setq gnus-summary-buffer (current-buffer))
2499     (let ((name gnus-newsgroup-name)
2500           (marked gnus-newsgroup-marked)
2501           (unread gnus-newsgroup-unreads)
2502           (headers gnus-current-headers)
2503           (data gnus-newsgroup-data)
2504           (summary gnus-summary-buffer)
2505           (article-buffer gnus-article-buffer)
2506           (original gnus-original-article-buffer)
2507           (gac gnus-article-current)
2508           (reffed gnus-reffed-article-number)
2509           (score-file gnus-current-score-file)
2510           (default-charset gnus-newsgroup-charset))
2511       (save-excursion
2512         (set-buffer gnus-group-buffer)
2513         (setq gnus-newsgroup-name name
2514               gnus-newsgroup-marked marked
2515               gnus-newsgroup-unreads unread
2516               gnus-current-headers headers
2517               gnus-newsgroup-data data
2518               gnus-article-current gac
2519               gnus-summary-buffer summary
2520               gnus-article-buffer article-buffer
2521               gnus-original-article-buffer original
2522               gnus-reffed-article-number reffed
2523               gnus-current-score-file score-file
2524               gnus-newsgroup-charset default-charset)
2525         ;; The article buffer also has local variables.
2526         (when (gnus-buffer-live-p gnus-article-buffer)
2527           (set-buffer gnus-article-buffer)
2528           (setq gnus-summary-buffer summary))))))
2529
2530 (defun gnus-summary-article-unread-p (article)
2531   "Say whether ARTICLE is unread or not."
2532   (memq article gnus-newsgroup-unreads))
2533
2534 (defun gnus-summary-first-article-p (&optional article)
2535   "Return whether ARTICLE is the first article in the buffer."
2536   (if (not (setq article (or article (gnus-summary-article-number))))
2537       nil
2538     (eq article (caar gnus-newsgroup-data))))
2539
2540 (defun gnus-summary-last-article-p (&optional article)
2541   "Return whether ARTICLE is the last article in the buffer."
2542   (if (not (setq article (or article (gnus-summary-article-number))))
2543       ;; All non-existent numbers are the last article.  :-)
2544       t
2545     (not (cdr (gnus-data-find-list article)))))
2546
2547 (defun gnus-make-thread-indent-array ()
2548   (let ((n 200))
2549     (unless (and gnus-thread-indent-array
2550                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2551       (setq gnus-thread-indent-array (make-vector 201 "")
2552             gnus-thread-indent-array-level gnus-thread-indent-level)
2553       (while (>= n 0)
2554         (aset gnus-thread-indent-array n
2555               (make-string (* n gnus-thread-indent-level) ? ))
2556         (setq n (1- n))))))
2557
2558 (defun gnus-update-summary-mark-positions ()
2559   "Compute where the summary marks are to go."
2560   (save-excursion
2561     (when (gnus-buffer-exists-p gnus-summary-buffer)
2562       (set-buffer gnus-summary-buffer))
2563     (let ((gnus-replied-mark 129)
2564           (gnus-score-below-mark 130)
2565           (gnus-score-over-mark 130)
2566           (gnus-download-mark 131)
2567           (spec gnus-summary-line-format-spec)
2568           gnus-visual pos)
2569       (save-excursion
2570         (gnus-set-work-buffer)
2571         (let ((gnus-summary-line-format-spec spec)
2572               (gnus-newsgroup-downloadable '((0 . t))))
2573           (gnus-summary-insert-line
2574            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2575           (goto-char (point-min))
2576           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2577                                              (- (point) 2)))))
2578           (goto-char (point-min))
2579           (push (cons 'replied (and (search-forward "\201" nil t)
2580                                     (- (point) 2)))
2581                 pos)
2582           (goto-char (point-min))
2583           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2584                 pos)
2585           (goto-char (point-min))
2586           (push (cons 'download
2587                       (and (search-forward "\203" nil t) (- (point) 2)))
2588                 pos)))
2589       (setq gnus-summary-mark-positions pos))))
2590
2591 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2592   "Insert a dummy root in the summary buffer."
2593   (beginning-of-line)
2594   (gnus-add-text-properties
2595    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2596    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2597
2598 (defun gnus-summary-from-or-to-or-newsgroups (header)
2599   (let ((to (cdr (assq 'To (mail-header-extra header))))
2600         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2601         (mail-parse-charset gnus-newsgroup-charset)
2602         (mail-parse-ignored-charsets
2603          (save-excursion (set-buffer gnus-summary-buffer)
2604                          gnus-newsgroup-ignored-charsets)))
2605     (cond
2606      ((and to
2607            gnus-ignored-from-addresses
2608            (string-match gnus-ignored-from-addresses
2609                          (mail-header-from header)))
2610       (concat "-> "
2611               (or (car (funcall gnus-extract-address-components
2612                                 (funcall
2613                                  gnus-decode-encoded-word-function to)))
2614                   (funcall gnus-decode-encoded-word-function to))))
2615      ((and newsgroups
2616            gnus-ignored-from-addresses
2617            (string-match gnus-ignored-from-addresses
2618                          (mail-header-from header)))
2619       (concat "=> " newsgroups))
2620      (t
2621       (or (car (funcall gnus-extract-address-components
2622                         (mail-header-from header)))
2623           (mail-header-from header))))))
2624
2625 (defun gnus-summary-insert-line (gnus-tmp-header
2626                                  gnus-tmp-level gnus-tmp-current
2627                                  gnus-tmp-unread gnus-tmp-replied
2628                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2629                                  &optional gnus-tmp-dummy gnus-tmp-score
2630                                  gnus-tmp-process)
2631   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2632          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2633          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2634          (gnus-tmp-score-char
2635           (if (or (null gnus-summary-default-score)
2636                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2637                       gnus-summary-zcore-fuzz))
2638               ?  ;Whitespace
2639             (if (< gnus-tmp-score gnus-summary-default-score)
2640                 gnus-score-below-mark gnus-score-over-mark)))
2641          (gnus-tmp-replied
2642           (cond (gnus-tmp-process gnus-process-mark)
2643                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2644                  gnus-cached-mark)
2645                 (gnus-tmp-replied gnus-replied-mark)
2646                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2647                  gnus-saved-mark)
2648                 (t gnus-unread-mark)))
2649          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2650          (gnus-tmp-name
2651           (cond
2652            ((string-match "<[^>]+> *$" gnus-tmp-from)
2653             (let ((beg (match-beginning 0)))
2654               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2655                        (substring gnus-tmp-from (1+ (match-beginning 0))
2656                                   (1- (match-end 0))))
2657                   (substring gnus-tmp-from 0 beg))))
2658            ((string-match "(.+)" gnus-tmp-from)
2659             (substring gnus-tmp-from
2660                        (1+ (match-beginning 0)) (1- (match-end 0))))
2661            (t gnus-tmp-from)))
2662          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2663          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2664          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2665          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2666          (buffer-read-only nil))
2667     (when (string= gnus-tmp-name "")
2668       (setq gnus-tmp-name gnus-tmp-from))
2669     (unless (numberp gnus-tmp-lines)
2670       (setq gnus-tmp-lines 0))
2671     (gnus-put-text-property
2672      (point)
2673      (progn (eval gnus-summary-line-format-spec) (point))
2674      'gnus-number gnus-tmp-number)
2675     (when (gnus-visual-p 'summary-highlight 'highlight)
2676       (forward-line -1)
2677       (gnus-run-hooks 'gnus-summary-update-hook)
2678       (forward-line 1))))
2679
2680 (defun gnus-summary-update-line (&optional dont-update)
2681   "Update summary line after change."
2682   (when (and gnus-summary-default-score
2683              (not gnus-summary-inhibit-highlight))
2684     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2685            (article (gnus-summary-article-number))
2686            (score (gnus-summary-article-score article)))
2687       (unless dont-update
2688         (if (and gnus-summary-mark-below
2689                  (< (gnus-summary-article-score)
2690                     gnus-summary-mark-below))
2691             ;; This article has a low score, so we mark it as read.
2692             (when (memq article gnus-newsgroup-unreads)
2693               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2694           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2695             ;; This article was previously marked as read on account
2696             ;; of a low score, but now it has risen, so we mark it as
2697             ;; unread.
2698             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2699         (gnus-summary-update-mark
2700          (if (or (null gnus-summary-default-score)
2701                  (<= (abs (- score gnus-summary-default-score))
2702                      gnus-summary-zcore-fuzz))
2703              ?  ;Whitespace
2704            (if (< score gnus-summary-default-score)
2705                gnus-score-below-mark gnus-score-over-mark))
2706          'score))
2707       ;; Do visual highlighting.
2708       (when (gnus-visual-p 'summary-highlight 'highlight)
2709         (gnus-run-hooks 'gnus-summary-update-hook)))))
2710
2711 (defvar gnus-tmp-new-adopts nil)
2712
2713 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2714   "Return the number of articles in THREAD.
2715 This may be 0 in some cases -- if none of the articles in
2716 the thread are to be displayed."
2717   (let* ((number
2718           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2719           (cond
2720            ((not (listp thread))
2721             1)
2722            ((and (consp thread) (cdr thread))
2723             (apply
2724              '+ 1 (mapcar
2725                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2726            ((null thread)
2727             1)
2728            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2729             1)
2730            (t 0))))
2731     (when (and level (zerop level) gnus-tmp-new-adopts)
2732       (incf number
2733             (apply '+ (mapcar
2734                        'gnus-summary-number-of-articles-in-thread
2735                        gnus-tmp-new-adopts))))
2736     (if char
2737         (if (> number 1) gnus-not-empty-thread-mark
2738           gnus-empty-thread-mark)
2739       number)))
2740
2741 (defun gnus-summary-set-local-parameters (group)
2742   "Go through the local params of GROUP and set all variable specs in that list."
2743   (let ((params (gnus-group-find-parameter group))
2744         elem)
2745     (while params
2746       (setq elem (car params)
2747             params (cdr params))
2748       (and (consp elem)                 ; Has to be a cons.
2749            (consp (cdr elem))           ; The cdr has to be a list.
2750            (symbolp (car elem))         ; Has to be a symbol in there.
2751            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2752            (ignore-errors               ; So we set it.
2753              (make-local-variable (car elem))
2754              (set (car elem) (eval (nth 1 elem))))))))
2755
2756 (defun gnus-summary-read-group (group &optional show-all no-article
2757                                       kill-buffer no-display backward
2758                                       select-articles)
2759   "Start reading news in newsgroup GROUP.
2760 If SHOW-ALL is non-nil, already read articles are also listed.
2761 If NO-ARTICLE is non-nil, no article is selected initially.
2762 If NO-DISPLAY, don't generate a summary buffer."
2763   (let (result)
2764     (while (and group
2765                 (null (setq result
2766                             (let ((gnus-auto-select-next nil))
2767                               (or (gnus-summary-read-group-1
2768                                    group show-all no-article
2769                                    kill-buffer no-display
2770                                    select-articles)
2771                                   (setq show-all nil
2772                                         select-articles nil)))))
2773                 (eq gnus-auto-select-next 'quietly))
2774       (set-buffer gnus-group-buffer)
2775       ;; The entry function called above goes to the next
2776       ;; group automatically, so we go two groups back
2777       ;; if we are searching for the previous group.
2778       (when backward
2779         (gnus-group-prev-unread-group 2))
2780       (if (not (equal group (gnus-group-group-name)))
2781           (setq group (gnus-group-group-name))
2782         (setq group nil)))
2783     result))
2784
2785 (defun gnus-summary-read-group-1 (group show-all no-article
2786                                         kill-buffer no-display
2787                                         &optional select-articles)
2788   ;; Killed foreign groups can't be entered.
2789   (when (and (not (gnus-group-native-p group))
2790              (not (gnus-gethash group gnus-newsrc-hashtb)))
2791     (error "Dead non-native groups can't be entered"))
2792   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2793   (let* ((new-group (gnus-summary-setup-buffer group))
2794          (quit-config (gnus-group-quit-config group))
2795          (did-select (and new-group (gnus-select-newsgroup
2796                                      group show-all select-articles))))
2797     (cond
2798      ;; This summary buffer exists already, so we just select it.
2799      ((not new-group)
2800       (gnus-set-global-variables)
2801       (when kill-buffer
2802         (gnus-kill-or-deaden-summary kill-buffer))
2803       (gnus-configure-windows 'summary 'force)
2804       (gnus-set-mode-line 'summary)
2805       (gnus-summary-position-point)
2806       (message "")
2807       t)
2808      ;; We couldn't select this group.
2809      ((null did-select)
2810       (when (and (eq major-mode 'gnus-summary-mode)
2811                  (not (equal (current-buffer) kill-buffer)))
2812         (kill-buffer (current-buffer))
2813         (if (not quit-config)
2814             (progn
2815               ;; Update the info -- marks might need to be removed,
2816               ;; for instance.
2817               (gnus-summary-update-info)
2818               (set-buffer gnus-group-buffer)
2819               (gnus-group-jump-to-group group)
2820               (gnus-group-next-unread-group 1))
2821           (gnus-handle-ephemeral-exit quit-config)))
2822       (gnus-message 3 "Can't select group")
2823       nil)
2824      ;; The user did a `C-g' while prompting for number of articles,
2825      ;; so we exit this group.
2826      ((eq did-select 'quit)
2827       (and (eq major-mode 'gnus-summary-mode)
2828            (not (equal (current-buffer) kill-buffer))
2829            (kill-buffer (current-buffer)))
2830       (when kill-buffer
2831         (gnus-kill-or-deaden-summary kill-buffer))
2832       (if (not quit-config)
2833           (progn
2834             (set-buffer gnus-group-buffer)
2835             (gnus-group-jump-to-group group)
2836             (gnus-group-next-unread-group 1)
2837             (gnus-configure-windows 'group 'force))
2838         (gnus-handle-ephemeral-exit quit-config))
2839       ;; Finally signal the quit.
2840       (signal 'quit nil))
2841      ;; The group was successfully selected.
2842      (t
2843       (gnus-set-global-variables)
2844       ;; Save the active value in effect when the group was entered.
2845       (setq gnus-newsgroup-active
2846             (gnus-copy-sequence
2847              (gnus-active gnus-newsgroup-name)))
2848       ;; You can change the summary buffer in some way with this hook.
2849       (gnus-run-hooks 'gnus-select-group-hook)
2850       ;; Set any local variables in the group parameters.
2851       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2852       (gnus-update-format-specifications
2853        nil 'summary 'summary-mode 'summary-dummy)
2854       (gnus-update-summary-mark-positions)
2855       ;; Do score processing.
2856       (when gnus-use-scoring
2857         (gnus-possibly-score-headers))
2858       ;; Check whether to fill in the gaps in the threads.
2859       (when gnus-build-sparse-threads
2860         (gnus-build-sparse-threads))
2861       ;; Find the initial limit.
2862       (if gnus-show-threads
2863           (if show-all
2864               (let ((gnus-newsgroup-dormant nil))
2865                 (gnus-summary-initial-limit show-all))
2866             (gnus-summary-initial-limit show-all))
2867         ;; When untreaded, all articles are always shown.
2868         (setq gnus-newsgroup-limit
2869               (mapcar
2870                (lambda (header) (mail-header-number header))
2871                gnus-newsgroup-headers)))
2872       ;; Generate the summary buffer.
2873       (unless no-display
2874         (gnus-summary-prepare))
2875       (when gnus-use-trees
2876         (gnus-tree-open group)
2877         (setq gnus-summary-highlight-line-function
2878               'gnus-tree-highlight-article))
2879       ;; If the summary buffer is empty, but there are some low-scored
2880       ;; articles or some excluded dormants, we include these in the
2881       ;; buffer.
2882       (when (and (zerop (buffer-size))
2883                  (not no-display))
2884         (cond (gnus-newsgroup-dormant
2885                (gnus-summary-limit-include-dormant))
2886               ((and gnus-newsgroup-scored show-all)
2887                (gnus-summary-limit-include-expunged t))))
2888       ;; Function `gnus-apply-kill-file' must be called in this hook.
2889       (gnus-run-hooks 'gnus-apply-kill-hook)
2890       (if (and (zerop (buffer-size))
2891                (not no-display))
2892           (progn
2893             ;; This newsgroup is empty.
2894             (gnus-summary-catchup-and-exit nil t)
2895             (gnus-message 6 "No unread news")
2896             (when kill-buffer
2897               (gnus-kill-or-deaden-summary kill-buffer))
2898             ;; Return nil from this function.
2899             nil)
2900         ;; Hide conversation thread subtrees.  We cannot do this in
2901         ;; gnus-summary-prepare-hook since kill processing may not
2902         ;; work with hidden articles.
2903         (and gnus-show-threads
2904              gnus-thread-hide-subtree
2905              (gnus-summary-hide-all-threads))
2906         (when kill-buffer
2907           (gnus-kill-or-deaden-summary kill-buffer))
2908         ;; Show first unread article if requested.
2909         (if (and (not no-article)
2910                  (not no-display)
2911                  gnus-newsgroup-unreads
2912                  gnus-auto-select-first)
2913             (progn
2914               (gnus-configure-windows 'summary)
2915               (cond
2916                ((eq gnus-auto-select-first 'best)
2917                 (gnus-summary-best-unread-article))
2918                ((eq gnus-auto-select-first t)
2919                 (gnus-summary-first-unread-article))
2920                ((gnus-functionp gnus-auto-select-first)
2921                 (funcall gnus-auto-select-first))))
2922           ;; Don't select any articles, just move point to the first
2923           ;; article in the group.
2924           (goto-char (point-min))
2925           (gnus-summary-position-point)
2926           (gnus-configure-windows 'summary 'force)
2927           (gnus-set-mode-line 'summary))
2928         (when (get-buffer-window gnus-group-buffer t)
2929           ;; Gotta use windows, because recenter does weird stuff if
2930           ;; the current buffer ain't the displayed window.
2931           (let ((owin (selected-window)))
2932             (select-window (get-buffer-window gnus-group-buffer t))
2933             (when (gnus-group-goto-group group)
2934               (recenter))
2935             (select-window owin)))
2936         ;; Mark this buffer as "prepared".
2937         (setq gnus-newsgroup-prepared t)
2938         (gnus-run-hooks 'gnus-summary-prepared-hook)
2939         t)))))
2940
2941 (defun gnus-summary-prepare ()
2942   "Generate the summary buffer."
2943   (interactive)
2944   (let ((buffer-read-only nil))
2945     (erase-buffer)
2946     (setq gnus-newsgroup-data nil
2947           gnus-newsgroup-data-reverse nil)
2948     (gnus-run-hooks 'gnus-summary-generate-hook)
2949     ;; Generate the buffer, either with threads or without.
2950     (when gnus-newsgroup-headers
2951       (gnus-summary-prepare-threads
2952        (if gnus-show-threads
2953            (gnus-sort-gathered-threads
2954             (funcall gnus-summary-thread-gathering-function
2955                      (gnus-sort-threads
2956                       (gnus-cut-threads (gnus-make-threads)))))
2957          ;; Unthreaded display.
2958          (gnus-sort-articles gnus-newsgroup-headers))))
2959     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2960     ;; Call hooks for modifying summary buffer.
2961     (goto-char (point-min))
2962     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2963
2964 (defsubst gnus-general-simplify-subject (subject)
2965   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2966   (setq subject
2967         (cond
2968          ;; Truncate the subject.
2969          (gnus-simplify-subject-functions
2970           (gnus-map-function gnus-simplify-subject-functions subject))
2971          ((numberp gnus-summary-gather-subject-limit)
2972           (setq subject (gnus-simplify-subject-re subject))
2973           (if (> (length subject) gnus-summary-gather-subject-limit)
2974               (substring subject 0 gnus-summary-gather-subject-limit)
2975             subject))
2976          ;; Fuzzily simplify it.
2977          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2978           (gnus-simplify-subject-fuzzy subject))
2979          ;; Just remove the leading "Re:".
2980          (t
2981           (gnus-simplify-subject-re subject))))
2982
2983   (if (and gnus-summary-gather-exclude-subject
2984            (string-match gnus-summary-gather-exclude-subject subject))
2985       nil                               ; This article shouldn't be gathered
2986     subject))
2987
2988 (defun gnus-summary-simplify-subject-query ()
2989   "Query where the respool algorithm would put this article."
2990   (interactive)
2991   (gnus-summary-select-article)
2992   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2993
2994 (defun gnus-gather-threads-by-subject (threads)
2995   "Gather threads by looking at Subject headers."
2996   (if (not gnus-summary-make-false-root)
2997       threads
2998     (let ((hashtb (gnus-make-hashtable 1024))
2999           (prev threads)
3000           (result threads)
3001           subject hthread whole-subject)
3002       (while threads
3003         (setq subject (gnus-general-simplify-subject
3004                        (setq whole-subject (mail-header-subject
3005                                             (caar threads)))))
3006         (when subject
3007           (if (setq hthread (gnus-gethash subject hashtb))
3008               (progn
3009                 ;; We enter a dummy root into the thread, if we
3010                 ;; haven't done that already.
3011                 (unless (stringp (caar hthread))
3012                   (setcar hthread (list whole-subject (car hthread))))
3013                 ;; We add this new gathered thread to this gathered
3014                 ;; thread.
3015                 (setcdr (car hthread)
3016                         (nconc (cdar hthread) (list (car threads))))
3017                 ;; Remove it from the list of threads.
3018                 (setcdr prev (cdr threads))
3019                 (setq threads prev))
3020             ;; Enter this thread into the hash table.
3021             (gnus-sethash subject threads hashtb)))
3022         (setq prev threads)
3023         (setq threads (cdr threads)))
3024       result)))
3025
3026 (defun gnus-gather-threads-by-references (threads)
3027   "Gather threads by looking at References headers."
3028   (let ((idhashtb (gnus-make-hashtable 1024))
3029         (thhashtb (gnus-make-hashtable 1024))
3030         (prev threads)
3031         (result threads)
3032         ids references id gthread gid entered ref)
3033     (while threads
3034       (when (setq references (mail-header-references (caar threads)))
3035         (setq id (mail-header-id (caar threads))
3036               ids (gnus-split-references references)
3037               entered nil)
3038         (while (setq ref (pop ids))
3039           (setq ids (delete ref ids))
3040           (if (not (setq gid (gnus-gethash ref idhashtb)))
3041               (progn
3042                 (gnus-sethash ref id idhashtb)
3043                 (gnus-sethash id threads thhashtb))
3044             (setq gthread (gnus-gethash gid thhashtb))
3045             (unless entered
3046               ;; We enter a dummy root into the thread, if we
3047               ;; haven't done that already.
3048               (unless (stringp (caar gthread))
3049                 (setcar gthread (list (mail-header-subject (caar gthread))
3050                                       (car gthread))))
3051               ;; We add this new gathered thread to this gathered
3052               ;; thread.
3053               (setcdr (car gthread)
3054                       (nconc (cdar gthread) (list (car threads)))))
3055             ;; Add it into the thread hash table.
3056             (gnus-sethash id gthread thhashtb)
3057             (setq entered t)
3058             ;; Remove it from the list of threads.
3059             (setcdr prev (cdr threads))
3060             (setq threads prev))))
3061       (setq prev threads)
3062       (setq threads (cdr threads)))
3063     result))
3064
3065 (defun gnus-sort-gathered-threads (threads)
3066   "Sort subtreads inside each gathered thread by article number."
3067   (let ((result threads))
3068     (while threads
3069       (when (stringp (caar threads))
3070         (setcdr (car threads)
3071                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3072       (setq threads (cdr threads)))
3073     result))
3074
3075 (defun gnus-thread-loop-p (root thread)
3076   "Say whether ROOT is in THREAD."
3077   (let ((stack (list thread))
3078         (infloop 0)
3079         th)
3080     (while (setq thread (pop stack))
3081       (setq th (cdr thread))
3082       (while (and th
3083                   (not (eq (caar th) root)))
3084         (pop th))
3085       (if th
3086           ;; We have found a loop.
3087           (let (ref-dep)
3088             (setcdr thread (delq (car th) (cdr thread)))
3089             (if (boundp (setq ref-dep (intern "none"
3090                                               gnus-newsgroup-dependencies)))
3091                 (setcdr (symbol-value ref-dep)
3092                         (nconc (cdr (symbol-value ref-dep))
3093                                (list (car th))))
3094               (set ref-dep (list nil (car th))))
3095             (setq infloop 1
3096                   stack nil))
3097         ;; Push all the subthreads onto the stack.
3098         (push (cdr thread) stack)))
3099     infloop))
3100
3101 (defun gnus-make-threads ()
3102   "Go through the dependency hashtb and find the roots.  Return all threads."
3103   (let (threads)
3104     (while (catch 'infloop
3105              (mapatoms
3106               (lambda (refs)
3107                 ;; Deal with self-referencing References loops.
3108                 (when (and (car (symbol-value refs))
3109                            (not (zerop
3110                                  (apply
3111                                   '+
3112                                   (mapcar
3113                                    (lambda (thread)
3114                                      (gnus-thread-loop-p
3115                                       (car (symbol-value refs)) thread))
3116                                    (cdr (symbol-value refs)))))))
3117                   (setq threads nil)
3118                   (throw 'infloop t))
3119                 (unless (car (symbol-value refs))
3120                   ;; These threads do not refer back to any other articles,
3121                   ;; so they're roots.
3122                   (setq threads (append (cdr (symbol-value refs)) threads))))
3123               gnus-newsgroup-dependencies)))
3124     threads))
3125
3126 ;; Build the thread tree.
3127 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3128   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3129
3130 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3131 if it was already present.
3132
3133 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3134 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3135 Message-IDs will be renamed be renamed to a unique Message-ID before
3136 being entered.
3137
3138 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3139   (let* ((id (mail-header-id header))
3140          (id-dep (and id (intern id dependencies)))
3141          ref ref-dep ref-header)
3142     ;; Enter this `header' in the `dependencies' table.
3143     (cond
3144      ((not id-dep)
3145       (setq header nil))
3146      ;; The first two cases do the normal part: enter a new `header'
3147      ;; in the `dependencies' table.
3148      ((not (boundp id-dep))
3149       (set id-dep (list header)))
3150      ((null (car (symbol-value id-dep)))
3151       (setcar (symbol-value id-dep) header))
3152
3153      ;; From here the `header' was already present in the
3154      ;; `dependencies' table.
3155      (force-new
3156       ;; Overrides an existing entry;
3157       ;; just set the header part of the entry.
3158       (setcar (symbol-value id-dep) header))
3159
3160      ;; Renames the existing `header' to a unique Message-ID.
3161      ((not gnus-summary-ignore-duplicates)
3162       ;; An article with this Message-ID has already been seen.
3163       ;; We rename the Message-ID.
3164       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3165            (list header))
3166       (mail-header-set-id header id))
3167
3168      ;; The last case ignores an existing entry, except it adds any
3169      ;; additional Xrefs (in case the two articles came from different
3170      ;; servers.
3171      ;; Also sets `header' to `nil' meaning that the `dependencies'
3172      ;; table was *not* modified.
3173      (t
3174       (mail-header-set-xref
3175        (car (symbol-value id-dep))
3176        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3177                    "")
3178                (or (mail-header-xref header) "")))
3179       (setq header nil)))
3180
3181     (when header
3182       ;; First check if that we are not creating a References loop.
3183       (setq ref (gnus-parent-id (mail-header-references header)))
3184       (while (and ref
3185                   (setq ref-dep (intern-soft ref dependencies))
3186                   (boundp ref-dep)
3187                   (setq ref-header (car (symbol-value ref-dep))))
3188         (if (string= id ref)
3189             ;; Yuk!  This is a reference loop.  Make the article be a
3190             ;; root article.
3191             (progn
3192               (mail-header-set-references (car (symbol-value id-dep)) "none")
3193               (setq ref nil))
3194           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3195       (setq ref (gnus-parent-id (mail-header-references header)))
3196       (setq ref-dep (intern (or ref "none") dependencies))
3197       (if (boundp ref-dep)
3198           (setcdr (symbol-value ref-dep)
3199                   (nconc (cdr (symbol-value ref-dep))
3200                          (list (symbol-value id-dep))))
3201         (set ref-dep (list nil (symbol-value id-dep)))))
3202     header))
3203
3204 (defun gnus-build-sparse-threads ()
3205   (let ((headers gnus-newsgroup-headers)
3206         (mail-parse-charset gnus-newsgroup-charset)
3207         (gnus-summary-ignore-duplicates t)
3208         header references generation relations
3209         subject child end new-child date)
3210     ;; First we create an alist of generations/relations, where
3211     ;; generations is how much we trust the relation, and the relation
3212     ;; is parent/child.
3213     (gnus-message 7 "Making sparse threads...")
3214     (save-excursion
3215       (nnheader-set-temp-buffer " *gnus sparse threads*")
3216       (while (setq header (pop headers))
3217         (when (and (setq references (mail-header-references header))
3218                    (not (string= references "")))
3219           (insert references)
3220           (setq child (mail-header-id header)
3221                 subject (mail-header-subject header)
3222                 date (mail-header-date header)
3223                 generation 0)
3224           (while (search-backward ">" nil t)
3225             (setq end (1+ (point)))
3226             (when (search-backward "<" nil t)
3227               (setq new-child (buffer-substring (point) end))
3228               (push (list (incf generation)
3229                           child (setq child new-child)
3230                           subject date)
3231                     relations)))
3232           (when child
3233             (push (list (1+ generation) child nil subject) relations))
3234           (erase-buffer)))
3235       (kill-buffer (current-buffer)))
3236     ;; Sort over trustworthiness.
3237     (mapcar
3238      (lambda (relation)
3239        (when (gnus-dependencies-add-header
3240               (make-full-mail-header
3241                gnus-reffed-article-number
3242                (nth 3 relation) "" (or (nth 4 relation) "")
3243                (nth 1 relation)
3244                (or (nth 2 relation) "") 0 0 "")
3245               gnus-newsgroup-dependencies nil)
3246          (push gnus-reffed-article-number gnus-newsgroup-limit)
3247          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3248          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3249                gnus-newsgroup-reads)
3250          (decf gnus-reffed-article-number)))
3251      (sort relations 'car-less-than-car))
3252     (gnus-message 7 "Making sparse threads...done")))
3253
3254 (defun gnus-build-old-threads ()
3255   ;; Look at all the articles that refer back to old articles, and
3256   ;; fetch the headers for the articles that aren't there.  This will
3257   ;; build complete threads - if the roots haven't been expired by the
3258   ;; server, that is.
3259   (let ((mail-parse-charset gnus-newsgroup-charset)
3260         id heads)
3261     (mapatoms
3262      (lambda (refs)
3263        (when (not (car (symbol-value refs)))
3264          (setq heads (cdr (symbol-value refs)))
3265          (while heads
3266            (if (memq (mail-header-number (caar heads))
3267                      gnus-newsgroup-dormant)
3268                (setq heads (cdr heads))
3269              (setq id (symbol-name refs))
3270              (while (and (setq id (gnus-build-get-header id))
3271                          (not (car (gnus-id-to-thread id)))))
3272              (setq heads nil)))))
3273      gnus-newsgroup-dependencies)))
3274
3275 ;; This function has to be called with point after the article number
3276 ;; on the beginning of the line.
3277 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3278   (let ((eol (gnus-point-at-eol))
3279         (buffer (current-buffer))
3280         header)
3281
3282     ;; overview: [num subject from date id refs chars lines misc]
3283     (unwind-protect
3284         (progn
3285           (narrow-to-region (point) eol)
3286           (unless (eobp)
3287             (forward-char))
3288
3289           (setq header
3290                 (make-full-mail-header
3291                  number                 ; number
3292                  (funcall gnus-decode-encoded-word-function
3293                           (nnheader-nov-field)) ; subject
3294                  (funcall gnus-decode-encoded-word-function
3295                           (nnheader-nov-field)) ; from
3296                  (nnheader-nov-field)   ; date
3297                  (nnheader-nov-read-message-id) ; id
3298                  (nnheader-nov-field)   ; refs
3299                  (nnheader-nov-read-integer) ; chars
3300                  (nnheader-nov-read-integer) ; lines
3301                  (unless (eobp)
3302                    (nnheader-nov-field)) ; misc
3303                  (nnheader-nov-parse-extra)))) ; extra
3304
3305       (widen))
3306
3307     (when gnus-alter-header-function
3308       (funcall gnus-alter-header-function header))
3309     (gnus-dependencies-add-header header dependencies force-new)))
3310
3311 (defun gnus-build-get-header (id)
3312   "Look through the buffer of NOV lines and find the header to ID.
3313 Enter this line into the dependencies hash table, and return
3314 the id of the parent article (if any)."
3315   (let ((deps gnus-newsgroup-dependencies)
3316         found header)
3317     (prog1
3318         (save-excursion
3319           (set-buffer nntp-server-buffer)
3320           (let ((case-fold-search nil))
3321             (goto-char (point-min))
3322             (while (and (not found)
3323                         (search-forward id nil t))
3324               (beginning-of-line)
3325               (setq found (looking-at
3326                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3327                                    (regexp-quote id))))
3328               (or found (beginning-of-line 2)))
3329             (when found
3330               (beginning-of-line)
3331               (and
3332                (setq header (gnus-nov-parse-line
3333                              (read (current-buffer)) deps))
3334                (gnus-parent-id (mail-header-references header))))))
3335       (when header
3336         (let ((number (mail-header-number header)))
3337           (push number gnus-newsgroup-limit)
3338           (push header gnus-newsgroup-headers)
3339           (if (memq number gnus-newsgroup-unselected)
3340               (progn
3341                 (push number gnus-newsgroup-unreads)
3342                 (setq gnus-newsgroup-unselected
3343                       (delq number gnus-newsgroup-unselected)))
3344             (push number gnus-newsgroup-ancient)))))))
3345
3346 (defun gnus-build-all-threads ()
3347   "Read all the headers."
3348   (let ((gnus-summary-ignore-duplicates t)
3349         (mail-parse-charset gnus-newsgroup-charset)
3350         (dependencies gnus-newsgroup-dependencies)
3351         header article)
3352     (save-excursion
3353       (set-buffer nntp-server-buffer)
3354       (let ((case-fold-search nil))
3355         (goto-char (point-min))
3356         (while (not (eobp))
3357           (ignore-errors
3358             (setq article (read (current-buffer))
3359                   header (gnus-nov-parse-line article dependencies)))
3360           (when header
3361             (save-excursion
3362               (set-buffer gnus-summary-buffer)
3363               (push header gnus-newsgroup-headers)
3364               (if (memq (setq article (mail-header-number header))
3365                         gnus-newsgroup-unselected)
3366                   (progn
3367                     (push article gnus-newsgroup-unreads)
3368                     (setq gnus-newsgroup-unselected
3369                           (delq article gnus-newsgroup-unselected)))
3370                 (push article gnus-newsgroup-ancient)))
3371             (forward-line 1)))))))
3372
3373 (defun gnus-summary-update-article-line (article header)
3374   "Update the line for ARTICLE using HEADERS."
3375   (let* ((id (mail-header-id header))
3376          (thread (gnus-id-to-thread id)))
3377     (unless thread
3378       (error "Article in no thread"))
3379     ;; Update the thread.
3380     (setcar thread header)
3381     (gnus-summary-goto-subject article)
3382     (let* ((datal (gnus-data-find-list article))
3383            (data (car datal))
3384            (length (when (cdr datal)
3385                      (- (gnus-data-pos data)
3386                         (gnus-data-pos (cadr datal)))))
3387            (buffer-read-only nil)
3388            (level (gnus-summary-thread-level)))
3389       (gnus-delete-line)
3390       (gnus-summary-insert-line
3391        header level nil (gnus-article-mark article)
3392        (memq article gnus-newsgroup-replied)
3393        (memq article gnus-newsgroup-expirable)
3394        ;; Only insert the Subject string when it's different
3395        ;; from the previous Subject string.
3396        (if (and
3397             gnus-show-threads
3398             (gnus-subject-equal
3399              (condition-case ()
3400                  (mail-header-subject
3401                   (gnus-data-header
3402                    (cadr
3403                     (gnus-data-find-list
3404                      article
3405                      (gnus-data-list t)))))
3406                ;; Error on the side of excessive subjects.
3407                (error ""))
3408              (mail-header-subject header)))
3409            ""
3410          (mail-header-subject header))
3411        nil (cdr (assq article gnus-newsgroup-scored))
3412        (memq article gnus-newsgroup-processable))
3413       (when length
3414         (gnus-data-update-list
3415          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3416
3417 (defun gnus-summary-update-article (article &optional iheader)
3418   "Update ARTICLE in the summary buffer."
3419   (set-buffer gnus-summary-buffer)
3420   (let* ((header (gnus-summary-article-header article))
3421          (id (mail-header-id header))
3422          (data (gnus-data-find article))
3423          (thread (gnus-id-to-thread id))
3424          (references (mail-header-references header))
3425          (parent
3426           (gnus-id-to-thread
3427            (or (gnus-parent-id
3428                 (when (and references
3429                            (not (equal "" references)))
3430                   references))
3431                "none")))
3432          (buffer-read-only nil)
3433          (old (car thread)))
3434     (when thread
3435       (unless iheader
3436         (setcar thread nil)
3437         (when parent
3438           (delq thread parent)))
3439       (if (gnus-summary-insert-subject id header)
3440           ;; Set the (possibly) new article number in the data structure.
3441           (gnus-data-set-number data (gnus-id-to-article id))
3442         (setcar thread old)
3443         nil))))
3444
3445 (defun gnus-rebuild-thread (id &optional line)
3446   "Rebuild the thread containing ID.
3447 If LINE, insert the rebuilt thread starting on line LINE."
3448   (let ((buffer-read-only nil)
3449         old-pos current thread data)
3450     (if (not gnus-show-threads)
3451         (setq thread (list (car (gnus-id-to-thread id))))
3452       ;; Get the thread this article is part of.
3453       (setq thread (gnus-remove-thread id)))
3454     (setq old-pos (gnus-point-at-bol))
3455     (setq current (save-excursion
3456                     (and (zerop (forward-line -1))
3457                          (gnus-summary-article-number))))
3458     ;; If this is a gathered thread, we have to go some re-gathering.
3459     (when (stringp (car thread))
3460       (let ((subject (car thread))
3461             roots thr)
3462         (setq thread (cdr thread))
3463         (while thread
3464           (unless (memq (setq thr (gnus-id-to-thread
3465                                    (gnus-root-id
3466                                     (mail-header-id (caar thread)))))
3467                         roots)
3468             (push thr roots))
3469           (setq thread (cdr thread)))
3470         ;; We now have all (unique) roots.
3471         (if (= (length roots) 1)
3472             ;; All the loose roots are now one solid root.
3473             (setq thread (car roots))
3474           (setq thread (cons subject (gnus-sort-threads roots))))))
3475     (let (threads)
3476       ;; We then insert this thread into the summary buffer.
3477       (when line
3478         (goto-char (point-min))
3479         (forward-line (1- line)))
3480       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3481         (if gnus-show-threads
3482             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3483           (gnus-summary-prepare-unthreaded thread))
3484         (setq data (nreverse gnus-newsgroup-data))
3485         (setq threads gnus-newsgroup-threads))
3486       ;; We splice the new data into the data structure.
3487       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3488       ;;!!! then we want to insert at the beginning of the buffer.
3489       ;;!!! That happens to be true with Gnus now, but that may
3490       ;;!!! change in the future.  Perhaps.
3491       (gnus-data-enter-list
3492        (if line nil current) data (- (point) old-pos))
3493       (setq gnus-newsgroup-threads
3494             (nconc threads gnus-newsgroup-threads))
3495       (gnus-data-compute-positions))))
3496
3497 (defun gnus-number-to-header (number)
3498   "Return the header for article NUMBER."
3499   (let ((headers gnus-newsgroup-headers))
3500     (while (and headers
3501                 (not (= number (mail-header-number (car headers)))))
3502       (pop headers))
3503     (when headers
3504       (car headers))))
3505
3506 (defun gnus-parent-headers (in-headers &optional generation)
3507   "Return the headers of the GENERATIONeth parent of HEADERS."
3508   (unless generation
3509     (setq generation 1))
3510   (let ((parent t)
3511         (headers in-headers)
3512         references)
3513     (while (and parent
3514                 (not (zerop generation))
3515                 (setq references (mail-header-references headers)))
3516       (setq headers (if (and references
3517                              (setq parent (gnus-parent-id references)))
3518                         (car (gnus-id-to-thread parent))
3519                       nil))
3520       (decf generation))
3521     (and (not (eq headers in-headers))
3522          headers)))
3523
3524 (defun gnus-id-to-thread (id)
3525   "Return the (sub-)thread where ID appears."
3526   (gnus-gethash id gnus-newsgroup-dependencies))
3527
3528 (defun gnus-id-to-article (id)
3529   "Return the article number of ID."
3530   (let ((thread (gnus-id-to-thread id)))
3531     (when (and thread
3532                (car thread))
3533       (mail-header-number (car thread)))))
3534
3535 (defun gnus-id-to-header (id)
3536   "Return the article headers of ID."
3537   (car (gnus-id-to-thread id)))
3538
3539 (defun gnus-article-displayed-root-p (article)
3540   "Say whether ARTICLE is a root(ish) article."
3541   (let ((level (gnus-summary-thread-level article))
3542         (refs (mail-header-references  (gnus-summary-article-header article)))
3543         particle)
3544     (cond
3545      ((null level) nil)
3546      ((zerop level) t)
3547      ((null refs) t)
3548      ((null (gnus-parent-id refs)) t)
3549      ((and (= 1 level)
3550            (null (setq particle (gnus-id-to-article
3551                                  (gnus-parent-id refs))))
3552            (null (gnus-summary-thread-level particle)))))))
3553
3554 (defun gnus-root-id (id)
3555   "Return the id of the root of the thread where ID appears."
3556   (let (last-id prev)
3557     (while (and id (setq prev (car (gnus-id-to-thread id))))
3558       (setq last-id id
3559             id (gnus-parent-id (mail-header-references prev))))
3560     last-id))
3561
3562 (defun gnus-articles-in-thread (thread)
3563   "Return the list of articles in THREAD."
3564   (cons (mail-header-number (car thread))
3565         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3566
3567 (defun gnus-remove-thread (id &optional dont-remove)
3568   "Remove the thread that has ID in it."
3569   (let (headers thread last-id)
3570     ;; First go up in this thread until we find the root.
3571     (setq last-id (gnus-root-id id)
3572           headers (message-flatten-list (gnus-id-to-thread last-id)))
3573     ;; We have now found the real root of this thread.  It might have
3574     ;; been gathered into some loose thread, so we have to search
3575     ;; through the threads to find the thread we wanted.
3576     (let ((threads gnus-newsgroup-threads)
3577           sub)
3578       (while threads
3579         (setq sub (car threads))
3580         (if (stringp (car sub))
3581             ;; This is a gathered thread, so we look at the roots
3582             ;; below it to find whether this article is in this
3583             ;; gathered root.
3584             (progn
3585               (setq sub (cdr sub))
3586               (while sub
3587                 (when (member (caar sub) headers)
3588                   (setq thread (car threads)
3589                         threads nil
3590                         sub nil))
3591                 (setq sub (cdr sub))))
3592           ;; It's an ordinary thread, so we check it.
3593           (when (eq (car sub) (car headers))
3594             (setq thread sub
3595                   threads nil)))
3596         (setq threads (cdr threads)))
3597       ;; If this article is in no thread, then it's a root.
3598       (if thread
3599           (unless dont-remove
3600             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3601         (setq thread (gnus-id-to-thread last-id)))
3602       (when thread
3603         (prog1
3604             thread                      ; We return this thread.
3605           (unless dont-remove
3606             (if (stringp (car thread))
3607                 (progn
3608                   ;; If we use dummy roots, then we have to remove the
3609                   ;; dummy root as well.
3610                   (when (eq gnus-summary-make-false-root 'dummy)
3611                     ;; We go to the dummy root by going to
3612                     ;; the first sub-"thread", and then one line up.
3613                     (gnus-summary-goto-article
3614                      (mail-header-number (caadr thread)))
3615                     (forward-line -1)
3616                     (gnus-delete-line)
3617                     (gnus-data-compute-positions))
3618                   (setq thread (cdr thread))
3619                   (while thread
3620                     (gnus-remove-thread-1 (car thread))
3621                     (setq thread (cdr thread))))
3622               (gnus-remove-thread-1 thread))))))))
3623
3624 (defun gnus-remove-thread-1 (thread)
3625   "Remove the thread THREAD recursively."
3626   (let ((number (mail-header-number (pop thread)))
3627         d)
3628     (setq thread (reverse thread))
3629     (while thread
3630       (gnus-remove-thread-1 (pop thread)))
3631     (when (setq d (gnus-data-find number))
3632       (goto-char (gnus-data-pos d))
3633       (gnus-summary-show-thread)
3634       (gnus-data-remove
3635        number
3636        (- (gnus-point-at-bol)
3637           (prog1
3638               (1+ (gnus-point-at-eol))
3639             (gnus-delete-line)))))))
3640
3641 (defun gnus-sort-threads (threads)
3642   "Sort THREADS."
3643   (if (not gnus-thread-sort-functions)
3644       threads
3645     (gnus-message 8 "Sorting threads...")
3646     (prog1
3647         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3648       (gnus-message 8 "Sorting threads...done"))))
3649
3650 (defun gnus-sort-articles (articles)
3651   "Sort ARTICLES."
3652   (when gnus-article-sort-functions
3653     (gnus-message 7 "Sorting articles...")
3654     (prog1
3655         (setq gnus-newsgroup-headers
3656               (sort articles (gnus-make-sort-function
3657                               gnus-article-sort-functions)))
3658       (gnus-message 7 "Sorting articles...done"))))
3659
3660 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3661 (defmacro gnus-thread-header (thread)
3662   "Return header of first article in THREAD.
3663 Note that THREAD must never, ever be anything else than a variable -
3664 using some other form will lead to serious barfage."
3665   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3666   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3667   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3668         (vector thread) 2))
3669
3670 (defsubst gnus-article-sort-by-number (h1 h2)
3671   "Sort articles by article number."
3672   (< (mail-header-number h1)
3673      (mail-header-number h2)))
3674
3675 (defun gnus-thread-sort-by-number (h1 h2)
3676   "Sort threads by root article number."
3677   (gnus-article-sort-by-number
3678    (gnus-thread-header h1) (gnus-thread-header h2)))
3679
3680 (defsubst gnus-article-sort-by-lines (h1 h2)
3681   "Sort articles by article Lines header."
3682   (< (mail-header-lines h1)
3683      (mail-header-lines h2)))
3684
3685 (defun gnus-thread-sort-by-lines (h1 h2)
3686   "Sort threads by root article Lines header."
3687   (gnus-article-sort-by-lines
3688    (gnus-thread-header h1) (gnus-thread-header h2)))
3689
3690 (defsubst gnus-article-sort-by-chars (h1 h2)
3691   "Sort articles by octet length."
3692   (< (mail-header-chars h1)
3693      (mail-header-chars h2)))
3694
3695 (defun gnus-thread-sort-by-chars (h1 h2)
3696   "Sort threads by root article octet length."
3697   (gnus-article-sort-by-chars
3698    (gnus-thread-header h1) (gnus-thread-header h2)))
3699
3700 (defsubst gnus-article-sort-by-author (h1 h2)
3701   "Sort articles by root author."
3702   (string-lessp
3703    (let ((extract (funcall
3704                    gnus-extract-address-components
3705                    (mail-header-from h1))))
3706      (or (car extract) (cadr extract) ""))
3707    (let ((extract (funcall
3708                    gnus-extract-address-components
3709                    (mail-header-from h2))))
3710      (or (car extract) (cadr extract) ""))))
3711
3712 (defun gnus-thread-sort-by-author (h1 h2)
3713   "Sort threads by root author."
3714   (gnus-article-sort-by-author
3715    (gnus-thread-header h1)  (gnus-thread-header h2)))
3716
3717 (defsubst gnus-article-sort-by-subject (h1 h2)
3718   "Sort articles by root subject."
3719   (string-lessp
3720    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3721    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3722
3723 (defun gnus-thread-sort-by-subject (h1 h2)
3724   "Sort threads by root subject."
3725   (gnus-article-sort-by-subject
3726    (gnus-thread-header h1) (gnus-thread-header h2)))
3727
3728 (defsubst gnus-article-sort-by-date (h1 h2)
3729   "Sort articles by root article date."
3730   (time-less-p
3731    (gnus-date-get-time (mail-header-date h1))
3732    (gnus-date-get-time (mail-header-date h2))))
3733
3734 (defun gnus-thread-sort-by-date (h1 h2)
3735   "Sort threads by root article date."
3736   (gnus-article-sort-by-date
3737    (gnus-thread-header h1) (gnus-thread-header h2)))
3738
3739 (defsubst gnus-article-sort-by-score (h1 h2)
3740   "Sort articles by root article score.
3741 Unscored articles will be counted as having a score of zero."
3742   (> (or (cdr (assq (mail-header-number h1)
3743                     gnus-newsgroup-scored))
3744          gnus-summary-default-score 0)
3745      (or (cdr (assq (mail-header-number h2)
3746                     gnus-newsgroup-scored))
3747          gnus-summary-default-score 0)))
3748
3749 (defun gnus-thread-sort-by-score (h1 h2)
3750   "Sort threads by root article score."
3751   (gnus-article-sort-by-score
3752    (gnus-thread-header h1) (gnus-thread-header h2)))
3753
3754 (defun gnus-thread-sort-by-total-score (h1 h2)
3755   "Sort threads by the sum of all scores in the thread.
3756 Unscored articles will be counted as having a score of zero."
3757   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3758
3759 (defun gnus-thread-total-score (thread)
3760   ;; This function find the total score of THREAD.
3761   (cond ((null thread)
3762          0)
3763         ((consp thread)
3764          (if (stringp (car thread))
3765              (apply gnus-thread-score-function 0
3766                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3767            (gnus-thread-total-score-1 thread)))
3768         (t
3769          (gnus-thread-total-score-1 (list thread)))))
3770
3771 (defun gnus-thread-total-score-1 (root)
3772   ;; This function find the total score of the thread below ROOT.
3773   (setq root (car root))
3774   (apply gnus-thread-score-function
3775          (or (append
3776               (mapcar 'gnus-thread-total-score
3777                       (cdr (gnus-id-to-thread (mail-header-id root))))
3778               (when (> (mail-header-number root) 0)
3779                 (list (or (cdr (assq (mail-header-number root)
3780                                      gnus-newsgroup-scored))
3781                           gnus-summary-default-score 0))))
3782              (list gnus-summary-default-score)
3783              '(0))))
3784
3785 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3786 (defvar gnus-tmp-prev-subject nil)
3787 (defvar gnus-tmp-false-parent nil)
3788 (defvar gnus-tmp-root-expunged nil)
3789 (defvar gnus-tmp-dummy-line nil)
3790
3791 (defvar gnus-tmp-header)
3792 (defun gnus-extra-header (type &optional header)
3793   "Return the extra header of TYPE."
3794   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3795       ""))
3796
3797 (defun gnus-summary-prepare-threads (threads)
3798   "Prepare summary buffer from THREADS and indentation LEVEL.
3799 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3800 or a straight list of headers."
3801   (gnus-message 7 "Generating summary...")
3802
3803   (setq gnus-newsgroup-threads threads)
3804   (beginning-of-line)
3805
3806   (let ((gnus-tmp-level 0)
3807         (default-score (or gnus-summary-default-score 0))
3808         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3809         thread number subject stack state gnus-tmp-gathered beg-match
3810         new-roots gnus-tmp-new-adopts thread-end
3811         gnus-tmp-header gnus-tmp-unread
3812         gnus-tmp-replied gnus-tmp-subject-or-nil
3813         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3814         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3815         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3816
3817     (setq gnus-tmp-prev-subject nil)
3818
3819     (if (vectorp (car threads))
3820         ;; If this is a straight (sic) list of headers, then a
3821         ;; threaded summary display isn't required, so we just create
3822         ;; an unthreaded one.
3823         (gnus-summary-prepare-unthreaded threads)
3824
3825       ;; Do the threaded display.
3826
3827       (while (or threads stack gnus-tmp-new-adopts new-roots)
3828
3829         (if (and (= gnus-tmp-level 0)
3830                  (or (not stack)
3831                      (= (caar stack) 0))
3832                  (not gnus-tmp-false-parent)
3833                  (or gnus-tmp-new-adopts new-roots))
3834             (if gnus-tmp-new-adopts
3835                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3836                       thread (list (car gnus-tmp-new-adopts))
3837                       gnus-tmp-header (caar thread)
3838                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3839               (when new-roots
3840                 (setq thread (list (car new-roots))
3841                       gnus-tmp-header (caar thread)
3842                       new-roots (cdr new-roots))))
3843
3844           (if threads
3845               ;; If there are some threads, we do them before the
3846               ;; threads on the stack.
3847               (setq thread threads
3848                     gnus-tmp-header (caar thread))
3849             ;; There were no current threads, so we pop something off
3850             ;; the stack.
3851             (setq state (car stack)
3852                   gnus-tmp-level (car state)
3853                   thread (cdr state)
3854                   stack (cdr stack)
3855                   gnus-tmp-header (caar thread))))
3856
3857         (setq gnus-tmp-false-parent nil)
3858         (setq gnus-tmp-root-expunged nil)
3859         (setq thread-end nil)
3860
3861         (if (stringp gnus-tmp-header)
3862             ;; The header is a dummy root.
3863             (cond
3864              ((eq gnus-summary-make-false-root 'adopt)
3865               ;; We let the first article adopt the rest.
3866               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3867                                                (cddar thread)))
3868               (setq gnus-tmp-gathered
3869                     (nconc (mapcar
3870                             (lambda (h) (mail-header-number (car h)))
3871                             (cddar thread))
3872                            gnus-tmp-gathered))
3873               (setq thread (cons (list (caar thread)
3874                                        (cadar thread))
3875                                  (cdr thread)))
3876               (setq gnus-tmp-level -1
3877                     gnus-tmp-false-parent t))
3878              ((eq gnus-summary-make-false-root 'empty)
3879               ;; We print adopted articles with empty subject fields.
3880               (setq gnus-tmp-gathered
3881                     (nconc (mapcar
3882                             (lambda (h) (mail-header-number (car h)))
3883                             (cddar thread))
3884                            gnus-tmp-gathered))
3885               (setq gnus-tmp-level -1))
3886              ((eq gnus-summary-make-false-root 'dummy)
3887               ;; We remember that we probably want to output a dummy
3888               ;; root.
3889               (setq gnus-tmp-dummy-line gnus-tmp-header)
3890               (setq gnus-tmp-prev-subject gnus-tmp-header))
3891              (t
3892               ;; We do not make a root for the gathered
3893               ;; sub-threads at all.
3894               (setq gnus-tmp-level -1)))
3895
3896           (setq number (mail-header-number gnus-tmp-header)
3897                 subject (mail-header-subject gnus-tmp-header))
3898
3899           (cond
3900            ;; If the thread has changed subject, we might want to make
3901            ;; this subthread into a root.
3902            ((and (null gnus-thread-ignore-subject)
3903                  (not (zerop gnus-tmp-level))
3904                  gnus-tmp-prev-subject
3905                  (not (inline
3906                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3907             (setq new-roots (nconc new-roots (list (car thread)))
3908                   thread-end t
3909                   gnus-tmp-header nil))
3910            ;; If the article lies outside the current limit,
3911            ;; then we do not display it.
3912            ((not (memq number gnus-newsgroup-limit))
3913             (setq gnus-tmp-gathered
3914                   (nconc (mapcar
3915                           (lambda (h) (mail-header-number (car h)))
3916                           (cdar thread))
3917                          gnus-tmp-gathered))
3918             (setq gnus-tmp-new-adopts (if (cdar thread)
3919                                           (append gnus-tmp-new-adopts
3920                                                   (cdar thread))
3921                                         gnus-tmp-new-adopts)
3922                   thread-end t
3923                   gnus-tmp-header nil)
3924             (when (zerop gnus-tmp-level)
3925               (setq gnus-tmp-root-expunged t)))
3926            ;; Perhaps this article is to be marked as read?
3927            ((and gnus-summary-mark-below
3928                  (< (or (cdr (assq number gnus-newsgroup-scored))
3929                         default-score)
3930                     gnus-summary-mark-below)
3931                  ;; Don't touch sparse articles.
3932                  (not (gnus-summary-article-sparse-p number))
3933                  (not (gnus-summary-article-ancient-p number)))
3934             (setq gnus-newsgroup-unreads
3935                   (delq number gnus-newsgroup-unreads))
3936             (if gnus-newsgroup-auto-expire
3937                 (push number gnus-newsgroup-expirable)
3938               (push (cons number gnus-low-score-mark)
3939                     gnus-newsgroup-reads))))
3940
3941           (when gnus-tmp-header
3942             ;; We may have an old dummy line to output before this
3943             ;; article.
3944             (when (and gnus-tmp-dummy-line
3945                        (gnus-subject-equal
3946                         gnus-tmp-dummy-line
3947                         (mail-header-subject gnus-tmp-header)))
3948               (gnus-summary-insert-dummy-line
3949                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3950               (setq gnus-tmp-dummy-line nil))
3951
3952             ;; Compute the mark.
3953             (setq gnus-tmp-unread (gnus-article-mark number))
3954
3955             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3956                                   gnus-tmp-header gnus-tmp-level)
3957                   gnus-newsgroup-data)
3958
3959             ;; Actually insert the line.
3960             (setq
3961              gnus-tmp-subject-or-nil
3962              (cond
3963               ((and gnus-thread-ignore-subject
3964                     gnus-tmp-prev-subject
3965                     (not (inline (gnus-subject-equal
3966                                   gnus-tmp-prev-subject subject))))
3967                subject)
3968               ((zerop gnus-tmp-level)
3969                (if (and (eq gnus-summary-make-false-root 'empty)
3970                         (memq number gnus-tmp-gathered)
3971                         gnus-tmp-prev-subject
3972                         (inline (gnus-subject-equal
3973                                  gnus-tmp-prev-subject subject)))
3974                    gnus-summary-same-subject
3975                  subject))
3976               (t gnus-summary-same-subject)))
3977             (if (and (eq gnus-summary-make-false-root 'adopt)
3978                      (= gnus-tmp-level 1)
3979                      (memq number gnus-tmp-gathered))
3980                 (setq gnus-tmp-opening-bracket ?\<
3981                       gnus-tmp-closing-bracket ?\>)
3982               (setq gnus-tmp-opening-bracket ?\[
3983                     gnus-tmp-closing-bracket ?\]))
3984             (setq
3985              gnus-tmp-indentation
3986              (aref gnus-thread-indent-array gnus-tmp-level)
3987              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3988              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3989                                 gnus-summary-default-score 0)
3990              gnus-tmp-score-char
3991              (if (or (null gnus-summary-default-score)
3992                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3993                          gnus-summary-zcore-fuzz))
3994                  ?  ;Whitespace
3995                (if (< gnus-tmp-score gnus-summary-default-score)
3996                    gnus-score-below-mark gnus-score-over-mark))
3997              gnus-tmp-replied
3998              (cond ((memq number gnus-newsgroup-processable)
3999                     gnus-process-mark)
4000                    ((memq number gnus-newsgroup-cached)
4001                     gnus-cached-mark)
4002                    ((memq number gnus-newsgroup-replied)
4003                     gnus-replied-mark)
4004                    ((memq number gnus-newsgroup-saved)
4005                     gnus-saved-mark)
4006                    (t gnus-unread-mark))
4007              gnus-tmp-from (mail-header-from gnus-tmp-header)
4008              gnus-tmp-name
4009              (cond
4010               ((string-match "<[^>]+> *$" gnus-tmp-from)
4011                (setq beg-match (match-beginning 0))
4012                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4013                         (substring gnus-tmp-from (1+ (match-beginning 0))
4014                                    (1- (match-end 0))))
4015                    (substring gnus-tmp-from 0 beg-match)))
4016               ((string-match "(.+)" gnus-tmp-from)
4017                (substring gnus-tmp-from
4018                           (1+ (match-beginning 0)) (1- (match-end 0))))
4019               (t gnus-tmp-from)))
4020             (when (string= gnus-tmp-name "")
4021               (setq gnus-tmp-name gnus-tmp-from))
4022             (unless (numberp gnus-tmp-lines)
4023               (setq gnus-tmp-lines 0))
4024             (gnus-put-text-property
4025              (point)
4026              (progn (eval gnus-summary-line-format-spec) (point))
4027              'gnus-number number)
4028             (when gnus-visual-p
4029               (forward-line -1)
4030               (gnus-run-hooks 'gnus-summary-update-hook)
4031               (forward-line 1))
4032
4033             (setq gnus-tmp-prev-subject subject)))
4034
4035         (when (nth 1 thread)
4036           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4037         (incf gnus-tmp-level)
4038         (setq threads (if thread-end nil (cdar thread)))
4039         (unless threads
4040           (setq gnus-tmp-level 0)))))
4041   (gnus-message 7 "Generating summary...done"))
4042
4043 (defun gnus-summary-prepare-unthreaded (headers)
4044   "Generate an unthreaded summary buffer based on HEADERS."
4045   (let (header number mark)
4046
4047     (beginning-of-line)
4048
4049     (while headers
4050       ;; We may have to root out some bad articles...
4051       (when (memq (setq number (mail-header-number
4052                                 (setq header (pop headers))))
4053                   gnus-newsgroup-limit)
4054         ;; Mark article as read when it has a low score.
4055         (when (and gnus-summary-mark-below
4056                    (< (or (cdr (assq number gnus-newsgroup-scored))
4057                           gnus-summary-default-score 0)
4058                       gnus-summary-mark-below)
4059                    (not (gnus-summary-article-ancient-p number)))
4060           (setq gnus-newsgroup-unreads
4061                 (delq number gnus-newsgroup-unreads))
4062           (if gnus-newsgroup-auto-expire
4063               (push number gnus-newsgroup-expirable)
4064             (push (cons number gnus-low-score-mark)
4065                   gnus-newsgroup-reads)))
4066
4067         (setq mark (gnus-article-mark number))
4068         (push (gnus-data-make number mark (1+ (point)) header 0)
4069               gnus-newsgroup-data)
4070         (gnus-summary-insert-line
4071          header 0 number
4072          mark (memq number gnus-newsgroup-replied)
4073          (memq number gnus-newsgroup-expirable)
4074          (mail-header-subject header) nil
4075          (cdr (assq number gnus-newsgroup-scored))
4076          (memq number gnus-newsgroup-processable))))))
4077
4078 (defun gnus-summary-remove-list-identifiers ()
4079   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4080   (let ((regexp (if (stringp gnus-list-identifiers)
4081                     gnus-list-identifiers
4082                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4083     (dolist (header gnus-newsgroup-headers)
4084       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
4085                                   " *\\)\\)+\\(Re: +\\)?\\)")
4086                           (mail-header-subject header))
4087         (mail-header-set-subject
4088          header (concat (substring (mail-header-subject header)
4089                                    0 (match-beginning 1))
4090                         (or
4091                          (match-string 3 (mail-header-subject header))
4092                          (match-string 5 (mail-header-subject header)))
4093                         (substring (mail-header-subject header)
4094                                    (match-end 1))))))))
4095
4096 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4097   "Select newsgroup GROUP.
4098 If READ-ALL is non-nil, all articles in the group are selected.
4099 If SELECT-ARTICLES, only select those articles from GROUP."
4100   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4101          ;;!!! Dirty hack; should be removed.
4102          (gnus-summary-ignore-duplicates
4103           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4104               t
4105             gnus-summary-ignore-duplicates))
4106          (info (nth 2 entry))
4107          articles fetched-articles cached)
4108
4109     (unless (gnus-check-server
4110              (setq gnus-current-select-method
4111                    (gnus-find-method-for-group group)))
4112       (error "Couldn't open server"))
4113
4114     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4115         (gnus-activate-group group)     ; Or we can activate it...
4116         (progn                          ; Or we bug out.
4117           (when (equal major-mode 'gnus-summary-mode)
4118             (kill-buffer (current-buffer)))
4119           (error "Couldn't request group %s: %s"
4120                  group (gnus-status-message group))))
4121
4122     (unless (gnus-request-group group t)
4123       (when (equal major-mode 'gnus-summary-mode)
4124         (kill-buffer (current-buffer)))
4125       (error "Couldn't request group %s: %s"
4126              group (gnus-status-message group)))
4127
4128     (setq gnus-newsgroup-name group)
4129     (setq gnus-newsgroup-unselected nil)
4130     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4131     (gnus-summary-setup-default-charset)
4132
4133     ;; Adjust and set lists of article marks.
4134     (when info
4135       (gnus-adjust-marked-articles info))
4136
4137     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4138     (when (gnus-virtual-group-p group)
4139       (setq cached gnus-newsgroup-cached))
4140
4141     (setq gnus-newsgroup-unreads
4142           (gnus-set-difference
4143            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4144            gnus-newsgroup-dormant))
4145
4146     (setq gnus-newsgroup-processable nil)
4147
4148     (gnus-update-read-articles group gnus-newsgroup-unreads)
4149
4150     (if (setq articles select-articles)
4151         (setq gnus-newsgroup-unselected
4152               (gnus-sorted-intersection
4153                gnus-newsgroup-unreads
4154                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4155       (setq articles (gnus-articles-to-read group read-all)))
4156
4157     (cond
4158      ((null articles)
4159       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4160       'quit)
4161      ((eq articles 0) nil)
4162      (t
4163       ;; Init the dependencies hash table.
4164       (setq gnus-newsgroup-dependencies
4165             (gnus-make-hashtable (length articles)))
4166       (gnus-set-global-variables)
4167       ;; Retrieve the headers and read them in.
4168       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4169       (setq gnus-newsgroup-headers
4170             (if (eq 'nov
4171                     (setq gnus-headers-retrieved-by
4172                           (gnus-retrieve-headers
4173                            articles gnus-newsgroup-name
4174                            ;; We might want to fetch old headers, but
4175                            ;; not if there is only 1 article.
4176                            (and (or (and
4177                                      (not (eq gnus-fetch-old-headers 'some))
4178                                      (not (numberp gnus-fetch-old-headers)))
4179                                     (> (length articles) 1))
4180                                 gnus-fetch-old-headers))))
4181                 (gnus-get-newsgroup-headers-xover
4182                  articles nil nil gnus-newsgroup-name t)
4183               (gnus-get-newsgroup-headers)))
4184       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4185
4186       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4187       (when cached
4188         (setq gnus-newsgroup-cached cached))
4189
4190       ;; Suppress duplicates?
4191       (when gnus-suppress-duplicates
4192         (gnus-dup-suppress-articles))
4193
4194       ;; Set the initial limit.
4195       (setq gnus-newsgroup-limit (copy-sequence articles))
4196       ;; Remove canceled articles from the list of unread articles.
4197       (setq gnus-newsgroup-unreads
4198             (gnus-set-sorted-intersection
4199              gnus-newsgroup-unreads
4200              (setq fetched-articles
4201                    (mapcar (lambda (headers) (mail-header-number headers))
4202                            gnus-newsgroup-headers))))
4203       ;; Removed marked articles that do not exist.
4204       (gnus-update-missing-marks
4205        (gnus-sorted-complement fetched-articles articles))
4206       ;; We might want to build some more threads first.
4207       (when (and gnus-fetch-old-headers
4208                  (eq gnus-headers-retrieved-by 'nov))
4209         (if (eq gnus-fetch-old-headers 'invisible)
4210             (gnus-build-all-threads)
4211           (gnus-build-old-threads)))
4212       ;; Let the Gnus agent mark articles as read.
4213       (when gnus-agent
4214         (gnus-agent-get-undownloaded-list))
4215       ;; Remove list identifiers from subject
4216       (when gnus-list-identifiers
4217         (gnus-summary-remove-list-identifiers))
4218       ;; Check whether auto-expire is to be done in this group.
4219       (setq gnus-newsgroup-auto-expire
4220             (gnus-group-auto-expirable-p group))
4221       ;; Set up the article buffer now, if necessary.
4222       (unless gnus-single-article-buffer
4223         (gnus-article-setup-buffer))
4224       ;; First and last article in this newsgroup.
4225       (when gnus-newsgroup-headers
4226         (setq gnus-newsgroup-begin
4227               (mail-header-number (car gnus-newsgroup-headers))
4228               gnus-newsgroup-end
4229               (mail-header-number
4230                (gnus-last-element gnus-newsgroup-headers))))
4231       ;; GROUP is successfully selected.
4232       (or gnus-newsgroup-headers t)))))
4233
4234 (defun gnus-articles-to-read (group &optional read-all)
4235   "Find out what articles the user wants to read."
4236   (let* ((articles
4237           ;; Select all articles if `read-all' is non-nil, or if there
4238           ;; are no unread articles.
4239           (if (or read-all
4240                   (and (zerop (length gnus-newsgroup-marked))
4241                        (zerop (length gnus-newsgroup-unreads)))
4242                   (eq (gnus-group-find-parameter group 'display)
4243                       'all))
4244               (or
4245                (gnus-uncompress-range (gnus-active group))
4246                (gnus-cache-articles-in-group group))
4247             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4248                           (copy-sequence gnus-newsgroup-unreads))
4249                   '<)))
4250          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4251          (scored (length scored-list))
4252          (number (length articles))
4253          (marked (+ (length gnus-newsgroup-marked)
4254                     (length gnus-newsgroup-dormant)))
4255          (select
4256           (cond
4257            ((numberp read-all)
4258             read-all)
4259            (t
4260             (condition-case ()
4261                 (cond
4262                  ((and (or (<= scored marked) (= scored number))
4263                        (numberp gnus-large-newsgroup)
4264                        (> number gnus-large-newsgroup))
4265                   (let ((input
4266                          (read-string
4267                           (format
4268                            "How many articles from %s (default %d): "
4269                            (gnus-limit-string gnus-newsgroup-name 35)
4270                            number))))
4271                     (if (string-match "^[ \t]*$" input) number input)))
4272                  ((and (> scored marked) (< scored number)
4273                        (> (- scored number) 20))
4274                   (let ((input
4275                          (read-string
4276                           (format "%s %s (%d scored, %d total): "
4277                                   "How many articles from"
4278                                   group scored number))))
4279                     (if (string-match "^[ \t]*$" input)
4280                         number input)))
4281                  (t number))
4282               (quit nil))))))
4283     (setq select (if (stringp select) (string-to-number select) select))
4284     (if (or (null select) (zerop select))
4285         select
4286       (if (and (not (zerop scored)) (<= (abs select) scored))
4287           (progn
4288             (setq articles (sort scored-list '<))
4289             (setq number (length articles)))
4290         (setq articles (copy-sequence articles)))
4291
4292       (when (< (abs select) number)
4293         (if (< select 0)
4294             ;; Select the N oldest articles.
4295             (setcdr (nthcdr (1- (abs select)) articles) nil)
4296           ;; Select the N most recent articles.
4297           (setq articles (nthcdr (- number select) articles))))
4298       (setq gnus-newsgroup-unselected
4299             (gnus-sorted-intersection
4300              gnus-newsgroup-unreads
4301              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4302       (when gnus-alter-articles-to-read-function
4303         (setq gnus-newsgroup-unreads
4304               (sort 
4305                (funcall gnus-alter-articles-to-read-function
4306                         gnus-newsgroup-name gnus-newsgroup-unreads)
4307                '<)))
4308       articles)))
4309
4310 (defun gnus-killed-articles (killed articles)
4311   (let (out)
4312     (while articles
4313       (when (inline (gnus-member-of-range (car articles) killed))
4314         (push (car articles) out))
4315       (setq articles (cdr articles)))
4316     out))
4317
4318 (defun gnus-uncompress-marks (marks)
4319   "Uncompress the mark ranges in MARKS."
4320   (let ((uncompressed '(score bookmark))
4321         out)
4322     (while marks
4323       (if (memq (caar marks) uncompressed)
4324           (push (car marks) out)
4325         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4326       (setq marks (cdr marks)))
4327     out))
4328
4329 (defun gnus-adjust-marked-articles (info)
4330   "Set all article lists and remove all marks that are no longer valid."
4331   (let* ((marked-lists (gnus-info-marks info))
4332          (active (gnus-active (gnus-info-group info)))
4333          (min (car active))
4334          (max (cdr active))
4335          (types gnus-article-mark-lists)
4336          (uncompressed '(score bookmark killed))
4337          marks var articles article mark)
4338
4339     (while marked-lists
4340       (setq marks (pop marked-lists))
4341       (set (setq var (intern (format "gnus-newsgroup-%s"
4342                                      (car (rassq (setq mark (car marks))
4343                                                  types)))))
4344            (if (memq (car marks) uncompressed) (cdr marks)
4345              (gnus-uncompress-range (cdr marks))))
4346
4347       (setq articles (symbol-value var))
4348
4349       ;; All articles have to be subsets of the active articles.
4350       (cond
4351        ;; Adjust "simple" lists.
4352        ((memq mark '(tick dormant expire reply save))
4353         (while articles
4354           (when (or (< (setq article (pop articles)) min) (> article max))
4355             (set var (delq article (symbol-value var))))))
4356        ;; Adjust assocs.
4357        ((memq mark uncompressed)
4358         (when (not (listp (cdr (symbol-value var))))
4359           (set var (list (symbol-value var))))
4360         (when (not (listp (cdr articles)))
4361           (setq articles (list articles)))
4362         (while articles
4363           (when (or (not (consp (setq article (pop articles))))
4364                     (< (car article) min)
4365                     (> (car article) max))
4366             (set var (delq article (symbol-value var))))))))))
4367
4368 (defun gnus-update-missing-marks (missing)
4369   "Go through the list of MISSING articles and remove them from the mark lists."
4370   (when missing
4371     (let ((types gnus-article-mark-lists)
4372           var m)
4373       ;; Go through all types.
4374       (while types
4375         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4376         (when (symbol-value var)
4377           ;; This list has articles.  So we delete all missing articles
4378           ;; from it.
4379           (setq m missing)
4380           (while m
4381             (set var (delq (pop m) (symbol-value var)))))))))
4382
4383 (defun gnus-update-marks ()
4384   "Enter the various lists of marked articles into the newsgroup info list."
4385   (let ((types gnus-article-mark-lists)
4386         (info (gnus-get-info gnus-newsgroup-name))
4387         (uncompressed '(score bookmark killed))
4388         type list newmarked symbol delta-marks)
4389     (when info
4390       ;; Add all marks lists to the list of marks lists.
4391       (while (setq type (pop types))
4392         (setq list (symbol-value
4393                     (setq symbol
4394                           (intern (format "gnus-newsgroup-%s"
4395                                           (car type))))))
4396
4397         (when list
4398           ;; Get rid of the entries of the articles that have the
4399           ;; default score.
4400           (when (and (eq (cdr type) 'score)
4401                      gnus-save-score
4402                      list)
4403             (let* ((arts list)
4404                    (prev (cons nil list))
4405                    (all prev))
4406               (while arts
4407                 (if (or (not (consp (car arts)))
4408                         (= (cdar arts) gnus-summary-default-score))
4409                     (setcdr prev (cdr arts))
4410                   (setq prev arts))
4411                 (setq arts (cdr arts)))
4412               (setq list (cdr all)))))
4413
4414         (unless (memq (cdr type) uncompressed)
4415           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4416        
4417         (when (gnus-check-backend-function
4418                'request-set-mark gnus-newsgroup-name)
4419           ;; propagate flags to server, with the following exceptions:
4420           ;; uncompressed:s are not proper flags (they are cons cells)
4421           ;; cache is a internal gnus flag
4422           ;; download are local to one gnus installation (well)
4423           ;; unsend are for nndraft groups only
4424           ;; xxx: generality of this?  this suits nnimap anyway
4425           (unless (memq (cdr type) (append '(cache download unsend)
4426                                            uncompressed))
4427             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4428                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4429                    (add (gnus-remove-from-range
4430                          (gnus-copy-sequence list) old)))
4431               (when add
4432                 (push (list add 'add (list (cdr type))) delta-marks))
4433               (when del
4434                 (push (list del 'del (list (cdr type))) delta-marks)))))
4435           
4436         (when list
4437           (push (cons (cdr type) list) newmarked)))
4438
4439       (when delta-marks
4440         (unless (gnus-check-group gnus-newsgroup-name)
4441           (error "Can't open server for %s" gnus-newsgroup-name))
4442         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4443           
4444       ;; Enter these new marks into the info of the group.
4445       (if (nthcdr 3 info)
4446           (setcar (nthcdr 3 info) newmarked)
4447         ;; Add the marks lists to the end of the info.
4448         (when newmarked
4449           (setcdr (nthcdr 2 info) (list newmarked))))
4450
4451       ;; Cut off the end of the info if there's nothing else there.
4452       (let ((i 5))
4453         (while (and (> i 2)
4454                     (not (nth i info)))
4455           (when (nthcdr (decf i) info)
4456             (setcdr (nthcdr i info) nil)))))))
4457
4458 (defun gnus-set-mode-line (where)
4459   "Set the mode line of the article or summary buffers.
4460 If WHERE is `summary', the summary mode line format will be used."
4461   ;; Is this mode line one we keep updated?
4462   (when (and (memq where gnus-updated-mode-lines)
4463              (symbol-value
4464               (intern (format "gnus-%s-mode-line-format-spec" where))))
4465     (let (mode-string)
4466       (save-excursion
4467         ;; We evaluate this in the summary buffer since these
4468         ;; variables are buffer-local to that buffer.
4469         (set-buffer gnus-summary-buffer)
4470         ;; We bind all these variables that are used in the `eval' form
4471         ;; below.
4472         (let* ((mformat (symbol-value
4473                          (intern
4474                           (format "gnus-%s-mode-line-format-spec" where))))
4475                (gnus-tmp-group-name (gnus-group-name-decode 
4476                                      gnus-newsgroup-name
4477                                      (gnus-group-name-charset 
4478                                       nil
4479                                       gnus-newsgroup-name)))
4480                (gnus-tmp-article-number (or gnus-current-article 0))
4481                (gnus-tmp-unread gnus-newsgroup-unreads)
4482                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4483                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4484                (gnus-tmp-unread-and-unselected
4485                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4486                             (zerop gnus-tmp-unselected))
4487                        "")
4488                       ((zerop gnus-tmp-unselected)
4489                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4490                       (t (format "{%d(+%d) more}"
4491                                  gnus-tmp-unread-and-unticked
4492                                  gnus-tmp-unselected))))
4493                (gnus-tmp-subject
4494                 (if (and gnus-current-headers
4495                          (vectorp gnus-current-headers))
4496                     (gnus-mode-string-quote
4497                      (mail-header-subject gnus-current-headers))
4498                   ""))
4499                bufname-length max-len
4500                gnus-tmp-header);; passed as argument to any user-format-funcs
4501           (setq mode-string (eval mformat))
4502           (setq bufname-length (if (string-match "%b" mode-string)
4503                                    (- (length
4504                                        (buffer-name
4505                                         (if (eq where 'summary)
4506                                             nil
4507                                           (get-buffer gnus-article-buffer))))
4508                                       2)
4509                                  0))
4510           (setq max-len (max 4 (if gnus-mode-non-string-length
4511                                    (- (window-width)
4512                                       gnus-mode-non-string-length
4513                                       bufname-length)
4514                                  (length mode-string))))
4515           ;; We might have to chop a bit of the string off...
4516           (when (> (length mode-string) max-len)
4517             (setq mode-string
4518                   (concat (truncate-string-to-width mode-string (- max-len 3))
4519                           "...")))
4520           ;; Pad the mode string a bit.
4521           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4522       ;; Update the mode line.
4523       (setq mode-line-buffer-identification
4524             (gnus-mode-line-buffer-identification (list mode-string)))
4525       (set-buffer-modified-p t))))
4526
4527 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4528   "Go through the HEADERS list and add all Xrefs to a hash table.
4529 The resulting hash table is returned, or nil if no Xrefs were found."
4530   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4531          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4532          (xref-hashtb (gnus-make-hashtable))
4533          start group entry number xrefs header)
4534     (while headers
4535       (setq header (pop headers))
4536       (when (and (setq xrefs (mail-header-xref header))
4537                  (not (memq (setq number (mail-header-number header))
4538                             unreads)))
4539         (setq start 0)
4540         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4541           (setq start (match-end 0))
4542           (setq group (if prefix
4543                           (concat prefix (substring xrefs (match-beginning 1)
4544                                                     (match-end 1)))
4545                         (substring xrefs (match-beginning 1) (match-end 1))))
4546           (setq number
4547                 (string-to-int (substring xrefs (match-beginning 2)
4548                                           (match-end 2))))
4549           (if (setq entry (gnus-gethash group xref-hashtb))
4550               (setcdr entry (cons number (cdr entry)))
4551             (gnus-sethash group (cons number nil) xref-hashtb)))))
4552     (and start xref-hashtb)))
4553
4554 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4555   "Look through all the headers and mark the Xrefs as read."
4556   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4557         name entry info xref-hashtb idlist method nth4)
4558     (save-excursion
4559       (set-buffer gnus-group-buffer)
4560       (when (setq xref-hashtb
4561                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4562         (mapatoms
4563          (lambda (group)
4564            (unless (string= from-newsgroup (setq name (symbol-name group)))
4565              (setq idlist (symbol-value group))
4566              ;; Dead groups are not updated.
4567              (and (prog1
4568                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4569                             info (nth 2 entry))
4570                     (when (stringp (setq nth4 (gnus-info-method info)))
4571                       (setq nth4 (gnus-server-to-method nth4))))
4572                   ;; Only do the xrefs if the group has the same
4573                   ;; select method as the group we have just read.
4574                   (or (gnus-methods-equal-p
4575                        nth4 (gnus-find-method-for-group from-newsgroup))
4576                       virtual
4577                       (equal nth4 (setq method (gnus-find-method-for-group
4578                                                 from-newsgroup)))
4579                       (and (equal (car nth4) (car method))
4580                            (equal (nth 1 nth4) (nth 1 method))))
4581                   gnus-use-cross-reference
4582                   (or (not (eq gnus-use-cross-reference t))
4583                       virtual
4584                       ;; Only do cross-references on subscribed
4585                       ;; groups, if that is what is wanted.
4586                       (<= (gnus-info-level info) gnus-level-subscribed))
4587                   (gnus-group-make-articles-read name idlist))))
4588          xref-hashtb)))))
4589
4590 (defun gnus-compute-read-articles (group articles)
4591   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4592          (info (nth 2 entry))
4593          (active (gnus-active group))
4594          ninfo)
4595     (when entry
4596       ;; First peel off all invalid article numbers.
4597       (when active
4598         (let ((ids articles)
4599               id first)
4600           (while (setq id (pop ids))
4601             (when (and first (> id (cdr active)))
4602               ;; We'll end up in this situation in one particular
4603               ;; obscure situation.  If you re-scan a group and get
4604               ;; a new article that is cross-posted to a different
4605               ;; group that has not been re-scanned, you might get
4606               ;; crossposted article that has a higher number than
4607               ;; Gnus believes possible.  So we re-activate this
4608               ;; group as well.  This might mean doing the
4609               ;; crossposting thingy will *increase* the number
4610               ;; of articles in some groups.  Tsk, tsk.
4611               (setq active (or (gnus-activate-group group) active)))
4612             (when (or (> id (cdr active))
4613                       (< id (car active)))
4614               (setq articles (delq id articles))))))
4615       ;; If the read list is nil, we init it.
4616       (if (and active
4617                (null (gnus-info-read info))
4618                (> (car active) 1))
4619           (setq ninfo (cons 1 (1- (car active))))
4620         (setq ninfo (gnus-info-read info)))
4621       ;; Then we add the read articles to the range.
4622       (gnus-add-to-range
4623        ninfo (setq articles (sort articles '<))))))
4624
4625 (defun gnus-group-make-articles-read (group articles)
4626   "Update the info of GROUP to say that ARTICLES are read."
4627   (let* ((num 0)
4628          (entry (gnus-gethash group gnus-newsrc-hashtb))
4629          (info (nth 2 entry))
4630          (active (gnus-active group))
4631          range)
4632     (when entry
4633       (setq range (gnus-compute-read-articles group articles))
4634       (save-excursion
4635         (set-buffer gnus-group-buffer)
4636         (gnus-undo-register
4637           `(progn
4638              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4639              (gnus-info-set-read ',info ',(gnus-info-read info))
4640              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4641              (gnus-group-update-group ,group t))))
4642       ;; Add the read articles to the range.
4643       (gnus-info-set-read info range)
4644       ;; Then we have to re-compute how many unread
4645       ;; articles there are in this group.
4646       (when active
4647         (cond
4648          ((not range)
4649           (setq num (- (1+ (cdr active)) (car active))))
4650          ((not (listp (cdr range)))
4651           (setq num (- (cdr active) (- (1+ (cdr range))
4652                                        (car range)))))
4653          (t
4654           (while range
4655             (if (numberp (car range))
4656                 (setq num (1+ num))
4657               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4658             (setq range (cdr range)))
4659           (setq num (- (cdr active) num))))
4660         ;; Update the number of unread articles.
4661         (setcar entry num)
4662         ;; Update the group buffer.
4663         (gnus-group-update-group group t)))))
4664
4665 (defvar gnus-newsgroup-none-id 0)
4666
4667 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4668   (let ((cur nntp-server-buffer)
4669         (dependencies
4670          (or dependencies
4671              (save-excursion (set-buffer gnus-summary-buffer)
4672                              gnus-newsgroup-dependencies)))
4673         headers id end ref
4674         (mail-parse-charset gnus-newsgroup-charset)
4675         (mail-parse-ignored-charsets
4676          (save-excursion (condition-case nil
4677                              (set-buffer gnus-summary-buffer)
4678                            (error))
4679                          gnus-newsgroup-ignored-charsets)))
4680     (save-excursion
4681       (set-buffer nntp-server-buffer)
4682       ;; Translate all TAB characters into SPACE characters.
4683       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4684       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4685       (gnus-run-hooks 'gnus-parse-headers-hook)
4686       (let ((case-fold-search t)
4687             in-reply-to header p lines chars)
4688         (goto-char (point-min))
4689         ;; Search to the beginning of the next header.  Error messages
4690         ;; do not begin with 2 or 3.
4691         (while (re-search-forward "^[23][0-9]+ " nil t)
4692           (setq id nil
4693                 ref nil)
4694           ;; This implementation of this function, with nine
4695           ;; search-forwards instead of the one re-search-forward and
4696           ;; a case (which basically was the old function) is actually
4697           ;; about twice as fast, even though it looks messier.  You
4698           ;; can't have everything, I guess.  Speed and elegance
4699           ;; doesn't always go hand in hand.
4700           (setq
4701            header
4702            (vector
4703             ;; Number.
4704             (prog1
4705                 (read cur)
4706               (end-of-line)
4707               (setq p (point))
4708               (narrow-to-region (point)
4709                                 (or (and (search-forward "\n.\n" nil t)
4710                                          (- (point) 2))
4711                                     (point))))
4712             ;; Subject.
4713             (progn
4714               (goto-char p)
4715               (if (search-forward "\nsubject: " nil t)
4716                   (funcall gnus-decode-encoded-word-function
4717                            (nnheader-header-value))
4718                 "(none)"))
4719             ;; From.
4720             (progn
4721               (goto-char p)
4722               (if (search-forward "\nfrom: " nil t)
4723                   (funcall gnus-decode-encoded-word-function
4724                            (nnheader-header-value))
4725                 "(nobody)"))
4726             ;; Date.
4727             (progn
4728               (goto-char p)
4729               (if (search-forward "\ndate: " nil t)
4730                   (nnheader-header-value) ""))
4731             ;; Message-ID.
4732             (progn
4733               (goto-char p)
4734               (setq id (if (re-search-forward
4735                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4736                            ;; We do it this way to make sure the Message-ID
4737                            ;; is (somewhat) syntactically valid.
4738                            (buffer-substring (match-beginning 1)
4739                                              (match-end 1))
4740                          ;; If there was no message-id, we just fake one
4741                          ;; to make subsequent routines simpler.
4742                          (nnheader-generate-fake-message-id))))
4743             ;; References.
4744             (progn
4745               (goto-char p)
4746               (if (search-forward "\nreferences: " nil t)
4747                   (progn
4748                     (setq end (point))
4749                     (prog1
4750                         (nnheader-header-value)
4751                       (setq ref
4752                             (buffer-substring
4753                              (progn
4754                                (end-of-line)
4755                                (search-backward ">" end t)
4756                                (1+ (point)))
4757                              (progn
4758                                (search-backward "<" end t)
4759                                (point))))))
4760                 ;; Get the references from the in-reply-to header if there
4761                 ;; were no references and the in-reply-to header looks
4762                 ;; promising.
4763                 (if (and (search-forward "\nin-reply-to: " nil t)
4764                          (setq in-reply-to (nnheader-header-value))
4765                          (string-match "<[^>]+>" in-reply-to))
4766                     (let (ref2)
4767                       (setq ref (substring in-reply-to (match-beginning 0)
4768                                            (match-end 0)))
4769                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4770                         (setq ref2 (substring in-reply-to (match-beginning 0)
4771                                               (match-end 0)))
4772                         (when (> (length ref2) (length ref))
4773                           (setq ref ref2)))
4774                       ref)
4775                   (setq ref nil))))
4776             ;; Chars.
4777             (progn
4778               (goto-char p)
4779               (if (search-forward "\nchars: " nil t)
4780                   (if (numberp (setq chars (ignore-errors (read cur))))
4781                       chars 0)
4782                 0))
4783             ;; Lines.
4784             (progn
4785               (goto-char p)
4786               (if (search-forward "\nlines: " nil t)
4787                   (if (numberp (setq lines (ignore-errors (read cur))))
4788                       lines 0)
4789                 0))
4790             ;; Xref.
4791             (progn
4792               (goto-char p)
4793               (and (search-forward "\nxref: " nil t)
4794                    (nnheader-header-value)))
4795             ;; Extra.
4796             (when gnus-extra-headers
4797               (let ((extra gnus-extra-headers)
4798                     out)
4799                 (while extra
4800                   (goto-char p)
4801                   (when (search-forward
4802                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4803                     (push (cons (car extra) (nnheader-header-value))
4804                           out))
4805                   (pop extra))
4806                 out))))
4807           (when (equal id ref)
4808             (setq ref nil))
4809
4810           (when gnus-alter-header-function
4811             (funcall gnus-alter-header-function header)
4812             (setq id (mail-header-id header)
4813                   ref (gnus-parent-id (mail-header-references header))))
4814
4815           (when (setq header
4816                       (gnus-dependencies-add-header
4817                        header dependencies force-new))
4818             (push header headers))
4819           (goto-char (point-max))
4820           (widen))
4821         (nreverse headers)))))
4822
4823 ;; Goes through the xover lines and returns a list of vectors
4824 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4825                                                   force-new dependencies
4826                                                   group also-fetch-heads)
4827   "Parse the news overview data in the server buffer.
4828 Return a list of headers that match SEQUENCE (see
4829 `nntp-retrieve-headers')."
4830   ;; Get the Xref when the users reads the articles since most/some
4831   ;; NNTP servers do not include Xrefs when using XOVER.
4832   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4833   (let ((mail-parse-charset gnus-newsgroup-charset)
4834         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4835         (cur nntp-server-buffer)
4836         (dependencies (or dependencies gnus-newsgroup-dependencies))
4837         number headers header)
4838     (save-excursion
4839       (set-buffer nntp-server-buffer)
4840       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4841       ;; Allow the user to mangle the headers before parsing them.
4842       (gnus-run-hooks 'gnus-parse-headers-hook)
4843       (goto-char (point-min))
4844       (while (not (eobp))
4845         (condition-case ()
4846             (while (and sequence (not (eobp)))
4847               (setq number (read cur))
4848               (while (and sequence
4849                           (< (car sequence) number))
4850                 (setq sequence (cdr sequence)))
4851               (and sequence
4852                    (eq number (car sequence))
4853                    (progn
4854                      (setq sequence (cdr sequence))
4855                      (setq header (inline
4856                                     (gnus-nov-parse-line
4857                                      number dependencies force-new))))
4858                    (push header headers))
4859               (forward-line 1))
4860           (error
4861            (gnus-error 4 "Strange nov line (%d)"
4862                        (count-lines (point-min) (point)))))
4863         (forward-line 1))
4864       ;; A common bug in inn is that if you have posted an article and
4865       ;; then retrieves the active file, it will answer correctly --
4866       ;; the new article is included.  However, a NOV entry for the
4867       ;; article may not have been generated yet, so this may fail.
4868       ;; We work around this problem by retrieving the last few
4869       ;; headers using HEAD.
4870       (if (or (not also-fetch-heads)
4871               (not sequence))
4872           ;; We (probably) got all the headers.
4873           (nreverse headers)
4874         (let ((gnus-nov-is-evil t))
4875           (nconc
4876            (nreverse headers)
4877            (when (gnus-retrieve-headers sequence group)
4878              (gnus-get-newsgroup-headers))))))))
4879
4880 (defun gnus-article-get-xrefs ()
4881   "Fill in the Xref value in `gnus-current-headers', if necessary.
4882 This is meant to be called in `gnus-article-internal-prepare-hook'."
4883   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4884                                  gnus-current-headers)))
4885     (or (not gnus-use-cross-reference)
4886         (not headers)
4887         (and (mail-header-xref headers)
4888              (not (string= (mail-header-xref headers) "")))
4889         (let ((case-fold-search t)
4890               xref)
4891           (save-restriction
4892             (nnheader-narrow-to-headers)
4893             (goto-char (point-min))
4894             (when (or (and (not (eobp))
4895                            (eq (downcase (char-after)) ?x)
4896                            (looking-at "Xref:"))
4897                       (search-forward "\nXref:" nil t))
4898               (goto-char (1+ (match-end 0)))
4899               (setq xref (buffer-substring (point)
4900                                            (progn (end-of-line) (point))))
4901               (mail-header-set-xref headers xref)))))))
4902
4903 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4904   "Find article ID and insert the summary line for that article.
4905 OLD-HEADER can either be a header or a line number to insert
4906 the subject line on."
4907   (let* ((line (and (numberp old-header) old-header))
4908          (old-header (and (vectorp old-header) old-header))
4909          (header (cond ((and old-header use-old-header)
4910                         old-header)
4911                        ((and (numberp id)
4912                              (gnus-number-to-header id))
4913                         (gnus-number-to-header id))
4914                        (t
4915                         (gnus-read-header id))))
4916          (number (and (numberp id) id))
4917          d)
4918     (when header
4919       ;; Rebuild the thread that this article is part of and go to the
4920       ;; article we have fetched.
4921       (when (and (not gnus-show-threads)
4922                  old-header)
4923         (when (and number
4924                    (setq d (gnus-data-find (mail-header-number old-header))))
4925           (goto-char (gnus-data-pos d))
4926           (gnus-data-remove
4927            number
4928            (- (gnus-point-at-bol)
4929               (prog1
4930                   (1+ (gnus-point-at-eol))
4931                 (gnus-delete-line))))))
4932       (when old-header
4933         (mail-header-set-number header (mail-header-number old-header)))
4934       (setq gnus-newsgroup-sparse
4935             (delq (setq number (mail-header-number header))
4936                   gnus-newsgroup-sparse))
4937       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4938       (push number gnus-newsgroup-limit)
4939       (gnus-rebuild-thread (mail-header-id header) line)
4940       (gnus-summary-goto-subject number nil t))
4941     (when (and (numberp number)
4942                (> number 0))
4943       ;; We have to update the boundaries even if we can't fetch the
4944       ;; article if ID is a number -- so that the next `P' or `N'
4945       ;; command will fetch the previous (or next) article even
4946       ;; if the one we tried to fetch this time has been canceled.
4947       (when (> number gnus-newsgroup-end)
4948         (setq gnus-newsgroup-end number))
4949       (when (< number gnus-newsgroup-begin)
4950         (setq gnus-newsgroup-begin number))
4951       (setq gnus-newsgroup-unselected
4952             (delq number gnus-newsgroup-unselected)))
4953     ;; Report back a success?
4954     (and header (mail-header-number header))))
4955
4956 ;;; Process/prefix in the summary buffer
4957
4958 (defun gnus-summary-work-articles (n)
4959   "Return a list of articles to be worked upon.
4960 The prefix argument, the list of process marked articles, and the
4961 current article will be taken into consideration."
4962   (save-excursion
4963     (set-buffer gnus-summary-buffer)
4964     (cond
4965      (n
4966       ;; A numerical prefix has been given.
4967       (setq n (prefix-numeric-value n))
4968       (let ((backward (< n 0))
4969             (n (abs (prefix-numeric-value n)))
4970             articles article)
4971         (save-excursion
4972           (while
4973               (and (> n 0)
4974                    (push (setq article (gnus-summary-article-number))
4975                          articles)
4976                    (if backward
4977                        (gnus-summary-find-prev nil article)
4978                      (gnus-summary-find-next nil article)))
4979             (decf n)))
4980         (nreverse articles)))
4981      ((and (gnus-region-active-p) (mark))
4982       (message "region active")
4983       ;; Work on the region between point and mark.
4984       (let ((max (max (point) (mark)))
4985             articles article)
4986         (save-excursion
4987           (goto-char (min (min (point) (mark))))
4988           (while
4989               (and
4990                (push (setq article (gnus-summary-article-number)) articles)
4991                (gnus-summary-find-next nil article)
4992                (< (point) max)))
4993           (nreverse articles))))
4994      (gnus-newsgroup-processable
4995       ;; There are process-marked articles present.
4996       ;; Save current state.
4997       (gnus-summary-save-process-mark)
4998       ;; Return the list.
4999       (reverse gnus-newsgroup-processable))
5000      (t
5001       ;; Just return the current article.
5002       (list (gnus-summary-article-number))))))
5003
5004 (defmacro gnus-summary-iterate (arg &rest forms)
5005   "Iterate over the process/prefixed articles and do FORMS.
5006 ARG is the interactive prefix given to the command.  FORMS will be
5007 executed with point over the summary line of the articles."
5008   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5009     `(let ((,articles (gnus-summary-work-articles ,arg)))
5010        (while ,articles
5011          (gnus-summary-goto-subject (car ,articles))
5012          ,@forms
5013          (pop ,articles)))))
5014
5015 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5016 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5017
5018 (defun gnus-summary-save-process-mark ()
5019   "Push the current set of process marked articles on the stack."
5020   (interactive)
5021   (push (copy-sequence gnus-newsgroup-processable)
5022         gnus-newsgroup-process-stack))
5023
5024 (defun gnus-summary-kill-process-mark ()
5025   "Push the current set of process marked articles on the stack and unmark."
5026   (interactive)
5027   (gnus-summary-save-process-mark)
5028   (gnus-summary-unmark-all-processable))
5029
5030 (defun gnus-summary-yank-process-mark ()
5031   "Pop the last process mark state off the stack and restore it."
5032   (interactive)
5033   (unless gnus-newsgroup-process-stack
5034     (error "Empty mark stack"))
5035   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5036
5037 (defun gnus-summary-process-mark-set (set)
5038   "Make SET into the current process marked articles."
5039   (gnus-summary-unmark-all-processable)
5040   (while set
5041     (gnus-summary-set-process-mark (pop set))))
5042
5043 ;;; Searching and stuff
5044
5045 (defun gnus-summary-search-group (&optional backward use-level)
5046   "Search for next unread newsgroup.
5047 If optional argument BACKWARD is non-nil, search backward instead."
5048   (save-excursion
5049     (set-buffer gnus-group-buffer)
5050     (when (gnus-group-search-forward
5051            backward nil (if use-level (gnus-group-group-level) nil))
5052       (gnus-group-group-name))))
5053
5054 (defun gnus-summary-best-group (&optional exclude-group)
5055   "Find the name of the best unread group.
5056 If EXCLUDE-GROUP, do not go to this group."
5057   (save-excursion
5058     (set-buffer gnus-group-buffer)
5059     (save-excursion
5060       (gnus-group-best-unread-group exclude-group))))
5061
5062 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5063   (if backward (gnus-summary-find-prev)
5064     (let* ((dummy (gnus-summary-article-intangible-p))
5065            (article (or article (gnus-summary-article-number)))
5066            (arts (gnus-data-find-list article))
5067            result)
5068       (when (and (not dummy)
5069                  (or (not gnus-summary-check-current)
5070                      (not unread)
5071                      (not (gnus-data-unread-p (car arts)))))
5072         (setq arts (cdr arts)))
5073       (when (setq result
5074                   (if unread
5075                       (progn
5076                         (while arts
5077                           (when (or (and undownloaded
5078                                          (eq gnus-undownloaded-mark
5079                                              (gnus-data-mark (car arts))))
5080                                     (gnus-data-unread-p (car arts)))
5081                             (setq result (car arts)
5082                                   arts nil))
5083                           (setq arts (cdr arts)))
5084                         result)
5085                     (car arts)))
5086         (goto-char (gnus-data-pos result))
5087         (gnus-data-number result)))))
5088
5089 (defun gnus-summary-find-prev (&optional unread article)
5090   (let* ((eobp (eobp))
5091          (article (or article (gnus-summary-article-number)))
5092          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5093          result)
5094     (when (and (not eobp)
5095                (or (not gnus-summary-check-current)
5096                    (not unread)
5097                    (not (gnus-data-unread-p (car arts)))))
5098       (setq arts (cdr arts)))
5099     (when (setq result
5100                 (if unread
5101                     (progn
5102                       (while arts
5103                         (when (gnus-data-unread-p (car arts))
5104                           (setq result (car arts)
5105                                 arts nil))
5106                         (setq arts (cdr arts)))
5107                       result)
5108                   (car arts)))
5109       (goto-char (gnus-data-pos result))
5110       (gnus-data-number result))))
5111
5112 (defun gnus-summary-find-subject (subject &optional unread backward article)
5113   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5114          (article (or article (gnus-summary-article-number)))
5115          (articles (gnus-data-list backward))
5116          (arts (gnus-data-find-list article articles))
5117          result)
5118     (when (or (not gnus-summary-check-current)
5119               (not unread)
5120               (not (gnus-data-unread-p (car arts))))
5121       (setq arts (cdr arts)))
5122     (while arts
5123       (and (or (not unread)
5124                (gnus-data-unread-p (car arts)))
5125            (vectorp (gnus-data-header (car arts)))
5126            (gnus-subject-equal
5127             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5128            (setq result (car arts)
5129                  arts nil))
5130       (setq arts (cdr arts)))
5131     (and result
5132          (goto-char (gnus-data-pos result))
5133          (gnus-data-number result))))
5134
5135 (defun gnus-summary-search-forward (&optional unread subject backward)
5136   "Search forward for an article.
5137 If UNREAD, look for unread articles.  If SUBJECT, look for
5138 articles with that subject.  If BACKWARD, search backward instead."
5139   (cond (subject (gnus-summary-find-subject subject unread backward))
5140         (backward (gnus-summary-find-prev unread))
5141         (t (gnus-summary-find-next unread))))
5142
5143 (defun gnus-recenter (&optional n)
5144   "Center point in window and redisplay frame.
5145 Also do horizontal recentering."
5146   (interactive "P")
5147   (when (and gnus-auto-center-summary
5148              (not (eq gnus-auto-center-summary 'vertical)))
5149     (gnus-horizontal-recenter))
5150   (recenter n))
5151
5152 (defun gnus-summary-recenter ()
5153   "Center point in the summary window.
5154 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5155 displayed, no centering will be performed."
5156   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5157   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5158   (interactive)
5159   (let* ((top (cond ((< (window-height) 4) 0)
5160                     ((< (window-height) 7) 1)
5161                     (t (if (numberp gnus-auto-center-summary)
5162                            gnus-auto-center-summary
5163                          2))))
5164          (height (1- (window-height)))
5165          (bottom (save-excursion (goto-char (point-max))
5166                                  (forward-line (- height))
5167                                  (point)))
5168          (window (get-buffer-window (current-buffer))))
5169     ;; The user has to want it.
5170     (when gnus-auto-center-summary
5171       (when (get-buffer-window gnus-article-buffer)
5172         ;; Only do recentering when the article buffer is displayed,
5173         ;; Set the window start to either `bottom', which is the biggest
5174         ;; possible valid number, or the second line from the top,
5175         ;; whichever is the least.
5176         (set-window-start
5177          window (min bottom (save-excursion
5178                               (forward-line (- top)) (point)))
5179          t))
5180       ;; Do horizontal recentering while we're at it.
5181       (when (and (get-buffer-window (current-buffer) t)
5182                  (not (eq gnus-auto-center-summary 'vertical)))
5183         (let ((selected (selected-window)))
5184           (select-window (get-buffer-window (current-buffer) t))
5185           (gnus-summary-position-point)
5186           (gnus-horizontal-recenter)
5187           (select-window selected))))))
5188
5189 (defun gnus-summary-jump-to-group (newsgroup)
5190   "Move point to NEWSGROUP in group mode buffer."
5191   ;; Keep update point of group mode buffer if visible.
5192   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5193       (save-window-excursion
5194         ;; Take care of tree window mode.
5195         (when (get-buffer-window gnus-group-buffer)
5196           (pop-to-buffer gnus-group-buffer))
5197         (gnus-group-jump-to-group newsgroup))
5198     (save-excursion
5199       ;; Take care of tree window mode.
5200       (if (get-buffer-window gnus-group-buffer)
5201           (pop-to-buffer gnus-group-buffer)
5202         (set-buffer gnus-group-buffer))
5203       (gnus-group-jump-to-group newsgroup))))
5204
5205 ;; This function returns a list of article numbers based on the
5206 ;; difference between the ranges of read articles in this group and
5207 ;; the range of active articles.
5208 (defun gnus-list-of-unread-articles (group)
5209   (let* ((read (gnus-info-read (gnus-get-info group)))
5210          (active (or (gnus-active group) (gnus-activate-group group)))
5211          (last (cdr active))
5212          first nlast unread)
5213     ;; If none are read, then all are unread.
5214     (if (not read)
5215         (setq first (car active))
5216       ;; If the range of read articles is a single range, then the
5217       ;; first unread article is the article after the last read
5218       ;; article.  Sounds logical, doesn't it?
5219       (if (and (not (listp (cdr read)))
5220                (or (< (car read) (car active))
5221                    (progn (setq read (list read))
5222                           nil)))
5223           (setq first (max (car active) (1+ (cdr read))))
5224         ;; `read' is a list of ranges.
5225         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5226                                   (caar read)))
5227                   1)
5228           (setq first (car active)))
5229         (while read
5230           (when first
5231             (while (< first nlast)
5232               (push first unread)
5233               (setq first (1+ first))))
5234           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5235           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5236           (setq read (cdr read)))))
5237     ;; And add the last unread articles.
5238     (while (<= first last)
5239       (push first unread)
5240       (setq first (1+ first)))
5241     ;; Return the list of unread articles.
5242     (delq 0 (nreverse unread))))
5243
5244 (defun gnus-list-of-read-articles (group)
5245   "Return a list of unread, unticked and non-dormant articles."
5246   (let* ((info (gnus-get-info group))
5247          (marked (gnus-info-marks info))
5248          (active (gnus-active group)))
5249     (and info active
5250          (gnus-set-difference
5251           (gnus-sorted-complement
5252            (gnus-uncompress-range active)
5253            (gnus-list-of-unread-articles group))
5254           (append
5255            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5256            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5257
5258 ;; Various summary commands
5259
5260 (defun gnus-summary-select-article-buffer ()
5261   "Reconfigure windows to show article buffer."
5262   (interactive)
5263   (if (not (gnus-buffer-live-p gnus-article-buffer))
5264       (error "There is no article buffer for this summary buffer")
5265     (gnus-configure-windows 'article)
5266     (select-window (get-buffer-window gnus-article-buffer))))
5267
5268 (defun gnus-summary-universal-argument (arg)
5269   "Perform any operation on all articles that are process/prefixed."
5270   (interactive "P")
5271   (let ((articles (gnus-summary-work-articles arg))
5272         func article)
5273     (if (eq
5274          (setq
5275           func
5276           (key-binding
5277            (read-key-sequence
5278             (substitute-command-keys
5279              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5280          'undefined)
5281         (gnus-error 1 "Undefined key")
5282       (save-excursion
5283         (while articles
5284           (gnus-summary-goto-subject (setq article (pop articles)))
5285           (let (gnus-newsgroup-processable)
5286             (command-execute func))
5287           (gnus-summary-remove-process-mark article)))))
5288   (gnus-summary-position-point))
5289
5290 (defun gnus-summary-toggle-truncation (&optional arg)
5291   "Toggle truncation of summary lines.
5292 With arg, turn line truncation on iff arg is positive."
5293   (interactive "P")
5294   (setq truncate-lines
5295         (if (null arg) (not truncate-lines)
5296           (> (prefix-numeric-value arg) 0)))
5297   (redraw-display))
5298
5299 (defun gnus-summary-reselect-current-group (&optional all rescan)
5300   "Exit and then reselect the current newsgroup.
5301 The prefix argument ALL means to select all articles."
5302   (interactive "P")
5303   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5304     (error "Ephemeral groups can't be reselected"))
5305   (let ((current-subject (gnus-summary-article-number))
5306         (group gnus-newsgroup-name))
5307     (setq gnus-newsgroup-begin nil)
5308     (gnus-summary-exit)
5309     ;; We have to adjust the point of group mode buffer because
5310     ;; point was moved to the next unread newsgroup by exiting.
5311     (gnus-summary-jump-to-group group)
5312     (when rescan
5313       (save-excursion
5314         (gnus-group-get-new-news-this-group 1)))
5315     (gnus-group-read-group all t)
5316     (gnus-summary-goto-subject current-subject nil t)))
5317
5318 (defun gnus-summary-rescan-group (&optional all)
5319   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5320   (interactive "P")
5321   (gnus-summary-reselect-current-group all t))
5322
5323 (defun gnus-summary-update-info (&optional non-destructive)
5324   (save-excursion
5325     (let ((group gnus-newsgroup-name))
5326       (when group
5327         (when gnus-newsgroup-kill-headers
5328           (setq gnus-newsgroup-killed
5329                 (gnus-compress-sequence
5330                  (nconc
5331                   (gnus-set-sorted-intersection
5332                    (gnus-uncompress-range gnus-newsgroup-killed)
5333                    (setq gnus-newsgroup-unselected
5334                          (sort gnus-newsgroup-unselected '<)))
5335                   (setq gnus-newsgroup-unreads
5336                         (sort gnus-newsgroup-unreads '<)))
5337                  t)))
5338         (unless (listp (cdr gnus-newsgroup-killed))
5339           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5340         (let ((headers gnus-newsgroup-headers))
5341           ;; Set the new ranges of read articles.
5342           (save-excursion
5343             (set-buffer gnus-group-buffer)
5344             (gnus-undo-force-boundary))
5345           (gnus-update-read-articles
5346            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5347           ;; Set the current article marks.
5348           (let ((gnus-newsgroup-scored
5349                  (if (and (not gnus-save-score)
5350                           (not non-destructive))
5351                      nil
5352                    gnus-newsgroup-scored)))
5353             (save-excursion
5354               (gnus-update-marks)))
5355           ;; Do the cross-ref thing.
5356           (when gnus-use-cross-reference
5357             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5358           ;; Do not switch windows but change the buffer to work.
5359           (set-buffer gnus-group-buffer)
5360           (unless (gnus-ephemeral-group-p group)
5361             (gnus-group-update-group group)))))))
5362
5363 (defun gnus-summary-save-newsrc (&optional force)
5364   "Save the current number of read/marked articles in the dribble buffer.
5365 The dribble buffer will then be saved.
5366 If FORCE (the prefix), also save the .newsrc file(s)."
5367   (interactive "P")
5368   (gnus-summary-update-info t)
5369   (if force
5370       (gnus-save-newsrc-file)
5371     (gnus-dribble-save)))
5372
5373 (defun gnus-summary-exit (&optional temporary)
5374   "Exit reading current newsgroup, and then return to group selection mode.
5375 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5376   (interactive)
5377   (gnus-set-global-variables)
5378   (when (gnus-buffer-live-p gnus-article-buffer)
5379     (save-excursion
5380       (set-buffer gnus-article-buffer)
5381       (mm-destroy-parts gnus-article-mime-handles)))
5382   (gnus-kill-save-kill-buffer)
5383   (gnus-async-halt-prefetch)
5384   (let* ((group gnus-newsgroup-name)
5385          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5386          (mode major-mode)
5387          (group-point nil)
5388          (buf (current-buffer)))
5389     (unless quit-config
5390       ;; Do adaptive scoring, and possibly save score files.
5391       (when gnus-newsgroup-adaptive
5392         (gnus-score-adaptive))
5393       (when gnus-use-scoring
5394         (gnus-score-save)))
5395     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5396     ;; If we have several article buffers, we kill them at exit.
5397     (unless gnus-single-article-buffer
5398       (gnus-kill-buffer gnus-original-article-buffer)
5399       (setq gnus-article-current nil))
5400     (when gnus-use-cache
5401       (gnus-cache-possibly-remove-articles)
5402       (gnus-cache-save-buffers))
5403     (gnus-async-prefetch-remove-group group)
5404     (when gnus-suppress-duplicates
5405       (gnus-dup-enter-articles))
5406     (when gnus-use-trees
5407       (gnus-tree-close group))
5408     (when gnus-use-cache
5409       (gnus-cache-write-active))
5410     ;; Remove entries for this group.
5411     (nnmail-purge-split-history (gnus-group-real-name group))
5412     ;; Make all changes in this group permanent.
5413     (unless quit-config
5414       (gnus-run-hooks 'gnus-exit-group-hook)
5415       (gnus-summary-update-info))
5416     (gnus-close-group group)
5417     ;; Make sure where we were, and go to next newsgroup.
5418     (set-buffer gnus-group-buffer)
5419     (unless quit-config
5420       (gnus-group-jump-to-group group))
5421     (gnus-run-hooks 'gnus-summary-exit-hook)
5422     (unless (or quit-config
5423                 ;; If this group has disappeared from the summary
5424                 ;; buffer, don't skip forwards.
5425                 (not (string= group (gnus-group-group-name))))
5426       (gnus-group-next-unread-group 1))
5427     (setq group-point (point))
5428     (if temporary
5429         nil                             ;Nothing to do.
5430       ;; If we have several article buffers, we kill them at exit.
5431       (unless gnus-single-article-buffer
5432         (gnus-kill-buffer gnus-article-buffer)
5433         (gnus-kill-buffer gnus-original-article-buffer)
5434         (setq gnus-article-current nil))
5435       (set-buffer buf)
5436       (if (not gnus-kill-summary-on-exit)
5437           (gnus-deaden-summary)
5438         ;; We set all buffer-local variables to nil.  It is unclear why
5439         ;; this is needed, but if we don't, buffer-local variables are
5440         ;; not garbage-collected, it seems.  This would the lead to en
5441         ;; ever-growing Emacs.
5442         (gnus-summary-clear-local-variables)
5443         (when (get-buffer gnus-article-buffer)
5444           (bury-buffer gnus-article-buffer))
5445         ;; We clear the global counterparts of the buffer-local
5446         ;; variables as well, just to be on the safe side.
5447         (set-buffer gnus-group-buffer)
5448         (gnus-summary-clear-local-variables)
5449         ;; Return to group mode buffer.
5450         (when (eq mode 'gnus-summary-mode)
5451           (gnus-kill-buffer buf)))
5452       (setq gnus-current-select-method gnus-select-method)
5453       (pop-to-buffer gnus-group-buffer)
5454       (if (not quit-config)
5455           (progn
5456             (goto-char group-point)
5457             (gnus-configure-windows 'group 'force))
5458         (gnus-handle-ephemeral-exit quit-config))
5459       ;; Clear the current group name.
5460       (unless quit-config
5461         (setq gnus-newsgroup-name nil)))))
5462
5463 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5464 (defun gnus-summary-exit-no-update (&optional no-questions)
5465   "Quit reading current newsgroup without updating read article info."
5466   (interactive)
5467   (let* ((group gnus-newsgroup-name)
5468          (quit-config (gnus-group-quit-config group)))
5469     (when (or no-questions
5470               gnus-expert-user
5471               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5472       (gnus-async-halt-prefetch)
5473       (mapcar 'funcall
5474               (delq 'gnus-summary-expire-articles
5475                     (copy-sequence gnus-summary-prepare-exit-hook)))
5476       (when (gnus-buffer-live-p gnus-article-buffer)
5477         (save-excursion
5478           (set-buffer gnus-article-buffer)
5479           (mm-destroy-parts gnus-article-mime-handles)))
5480       ;; If we have several article buffers, we kill them at exit.
5481       (unless gnus-single-article-buffer
5482         (gnus-kill-buffer gnus-article-buffer)
5483         (gnus-kill-buffer gnus-original-article-buffer)
5484         (setq gnus-article-current nil))
5485       (if (not gnus-kill-summary-on-exit)
5486           (gnus-deaden-summary)
5487         (gnus-close-group group)
5488         (gnus-summary-clear-local-variables)
5489         (set-buffer gnus-group-buffer)
5490         (gnus-summary-clear-local-variables)
5491         (when (get-buffer gnus-summary-buffer)
5492           (kill-buffer gnus-summary-buffer)))
5493       (unless gnus-single-article-buffer
5494         (setq gnus-article-current nil))
5495       (when gnus-use-trees
5496         (gnus-tree-close group))
5497       (gnus-async-prefetch-remove-group group)
5498       (when (get-buffer gnus-article-buffer)
5499         (bury-buffer gnus-article-buffer))
5500       ;; Return to the group buffer.
5501       (gnus-configure-windows 'group 'force)
5502       ;; Clear the current group name.
5503       (setq gnus-newsgroup-name nil)
5504       (when (equal (gnus-group-group-name) group)
5505         (gnus-group-next-unread-group 1))
5506       (when quit-config
5507         (gnus-handle-ephemeral-exit quit-config)))))
5508
5509 (defun gnus-handle-ephemeral-exit (quit-config)
5510   "Handle movement when leaving an ephemeral group.
5511 The state which existed when entering the ephemeral is reset."
5512   (if (not (buffer-name (car quit-config)))
5513       (gnus-configure-windows 'group 'force)
5514     (set-buffer (car quit-config))
5515     (cond ((eq major-mode 'gnus-summary-mode)
5516            (gnus-set-global-variables))
5517           ((eq major-mode 'gnus-article-mode)
5518            (save-excursion
5519              ;; The `gnus-summary-buffer' variable may point
5520              ;; to the old summary buffer when using a single
5521              ;; article buffer.
5522              (unless (gnus-buffer-live-p gnus-summary-buffer)
5523                (set-buffer gnus-group-buffer))
5524              (set-buffer gnus-summary-buffer)
5525              (gnus-set-global-variables))))
5526     (if (or (eq (cdr quit-config) 'article)
5527             (eq (cdr quit-config) 'pick))
5528         (progn
5529           ;; The current article may be from the ephemeral group
5530           ;; thus it is best that we reload this article
5531           (gnus-summary-show-article)
5532           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5533               (gnus-configure-windows 'pick 'force)
5534             (gnus-configure-windows (cdr quit-config) 'force)))
5535       (gnus-configure-windows (cdr quit-config) 'force))
5536     (when (eq major-mode 'gnus-summary-mode)
5537       (gnus-summary-next-subject 1 nil t)
5538       (gnus-summary-recenter)
5539       (gnus-summary-position-point))))
5540
5541 ;;; Dead summaries.
5542
5543 (defvar gnus-dead-summary-mode-map nil)
5544
5545 (unless gnus-dead-summary-mode-map
5546   (setq gnus-dead-summary-mode-map (make-keymap))
5547   (suppress-keymap gnus-dead-summary-mode-map)
5548   (substitute-key-definition
5549    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5550   (let ((keys '("\C-d" "\r" "\177" [delete])))
5551     (while keys
5552       (define-key gnus-dead-summary-mode-map
5553         (pop keys) 'gnus-summary-wake-up-the-dead))))
5554
5555 (defvar gnus-dead-summary-mode nil
5556   "Minor mode for Gnus summary buffers.")
5557
5558 (defun gnus-dead-summary-mode (&optional arg)
5559   "Minor mode for Gnus summary buffers."
5560   (interactive "P")
5561   (when (eq major-mode 'gnus-summary-mode)
5562     (make-local-variable 'gnus-dead-summary-mode)
5563     (setq gnus-dead-summary-mode
5564           (if (null arg) (not gnus-dead-summary-mode)
5565             (> (prefix-numeric-value arg) 0)))
5566     (when gnus-dead-summary-mode
5567       (gnus-add-minor-mode
5568        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5569
5570 (defun gnus-deaden-summary ()
5571   "Make the current summary buffer into a dead summary buffer."
5572   ;; Kill any previous dead summary buffer.
5573   (when (and gnus-dead-summary
5574              (buffer-name gnus-dead-summary))
5575     (save-excursion
5576       (set-buffer gnus-dead-summary)
5577       (when gnus-dead-summary-mode
5578         (kill-buffer (current-buffer)))))
5579   ;; Make this the current dead summary.
5580   (setq gnus-dead-summary (current-buffer))
5581   (gnus-dead-summary-mode 1)
5582   (let ((name (buffer-name)))
5583     (when (string-match "Summary" name)
5584       (rename-buffer
5585        (concat (substring name 0 (match-beginning 0)) "Dead "
5586                (substring name (match-beginning 0)))
5587        t)
5588       (bury-buffer))))
5589
5590 (defun gnus-kill-or-deaden-summary (buffer)
5591   "Kill or deaden the summary BUFFER."
5592   (save-excursion
5593     (when (and (buffer-name buffer)
5594                (not gnus-single-article-buffer))
5595       (save-excursion
5596         (set-buffer buffer)
5597         (gnus-kill-buffer gnus-article-buffer)
5598         (gnus-kill-buffer gnus-original-article-buffer)))
5599     (cond (gnus-kill-summary-on-exit
5600            (when (and gnus-use-trees
5601                       (gnus-buffer-exists-p buffer))
5602              (save-excursion
5603                (set-buffer buffer)
5604                (gnus-tree-close gnus-newsgroup-name)))
5605            (gnus-kill-buffer buffer))
5606           ((gnus-buffer-exists-p buffer)
5607            (save-excursion
5608              (set-buffer buffer)
5609              (gnus-deaden-summary))))))
5610
5611 (defun gnus-summary-wake-up-the-dead (&rest args)
5612   "Wake up the dead summary buffer."
5613   (interactive)
5614   (gnus-dead-summary-mode -1)
5615   (let ((name (buffer-name)))
5616     (when (string-match "Dead " name)
5617       (rename-buffer
5618        (concat (substring name 0 (match-beginning 0))
5619                (substring name (match-end 0)))
5620        t)))
5621   (gnus-message 3 "This dead summary is now alive again"))
5622
5623 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5624 (defun gnus-summary-fetch-faq (&optional faq-dir)
5625   "Fetch the FAQ for the current group.
5626 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5627 in."
5628   (interactive
5629    (list
5630     (when current-prefix-arg
5631       (completing-read
5632        "Faq dir: " (and (listp gnus-group-faq-directory)
5633                         (mapcar (lambda (file) (list file))
5634                                 gnus-group-faq-directory))))))
5635   (let (gnus-faq-buffer)
5636     (when (setq gnus-faq-buffer
5637                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5638       (gnus-configure-windows 'summary-faq))))
5639
5640 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5641 (defun gnus-summary-describe-group (&optional force)
5642   "Describe the current newsgroup."
5643   (interactive "P")
5644   (gnus-group-describe-group force gnus-newsgroup-name))
5645
5646 (defun gnus-summary-describe-briefly ()
5647   "Describe summary mode commands briefly."
5648   (interactive)
5649   (gnus-message 6 (substitute-command-keys "\\<gnus-summary-mode-map>\\[gnus-summary-next-page]:Select  \\[gnus-summary-next-unread-article]:Forward  \\[gnus-summary-prev-unread-article]:Backward  \\[gnus-summary-exit]:Exit  \\[gnus-info-find-node]:Run Info        \\[gnus-summary-describe-briefly]:This help")))
5650
5651 ;; Walking around group mode buffer from summary mode.
5652
5653 (defun gnus-summary-next-group (&optional no-article target-group backward)
5654   "Exit current newsgroup and then select next unread newsgroup.
5655 If prefix argument NO-ARTICLE is non-nil, no article is selected
5656 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5657 previous group instead."
5658   (interactive "P")
5659   ;; Stop pre-fetching.
5660   (gnus-async-halt-prefetch)
5661   (let ((current-group gnus-newsgroup-name)
5662         (current-buffer (current-buffer))
5663         entered)
5664     ;; First we semi-exit this group to update Xrefs and all variables.
5665     ;; We can't do a real exit, because the window conf must remain
5666     ;; the same in case the user is prompted for info, and we don't
5667     ;; want the window conf to change before that...
5668     (gnus-summary-exit t)
5669     (while (not entered)
5670       ;; Then we find what group we are supposed to enter.
5671       (set-buffer gnus-group-buffer)
5672       (gnus-group-jump-to-group current-group)
5673       (setq target-group
5674             (or target-group
5675                 (if (eq gnus-keep-same-level 'best)
5676                     (gnus-summary-best-group gnus-newsgroup-name)
5677                   (gnus-summary-search-group backward gnus-keep-same-level))))
5678       (if (not target-group)
5679           ;; There are no further groups, so we return to the group
5680           ;; buffer.
5681           (progn
5682             (gnus-message 5 "Returning to the group buffer")
5683             (setq entered t)
5684             (when (gnus-buffer-live-p current-buffer)
5685               (set-buffer current-buffer)
5686               (gnus-summary-exit))
5687             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5688         ;; We try to enter the target group.
5689         (gnus-group-jump-to-group target-group)
5690         (let ((unreads (gnus-group-group-unread)))
5691           (if (and (or (eq t unreads)
5692                        (and unreads (not (zerop unreads))))
5693                    (gnus-summary-read-group
5694                     target-group nil no-article
5695                     (and (buffer-name current-buffer) current-buffer)
5696                     nil backward))
5697               (setq entered t)
5698             (setq current-group target-group
5699                   target-group nil)))))))
5700
5701 (defun gnus-summary-prev-group (&optional no-article)
5702   "Exit current newsgroup and then select previous unread newsgroup.
5703 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5704   (interactive "P")
5705   (gnus-summary-next-group no-article nil t))
5706
5707 ;; Walking around summary lines.
5708
5709 (defun gnus-summary-first-subject (&optional unread undownloaded)
5710   "Go to the first unread subject.
5711 If UNREAD is non-nil, go to the first unread article.
5712 Returns the article selected or nil if there are no unread articles."
5713   (interactive "P")
5714   (prog1
5715       (cond
5716        ;; Empty summary.
5717        ((null gnus-newsgroup-data)
5718         (gnus-message 3 "No articles in the group")
5719         nil)
5720        ;; Pick the first article.
5721        ((not unread)
5722         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5723         (gnus-data-number (car gnus-newsgroup-data)))
5724        ;; No unread articles.
5725        ((null gnus-newsgroup-unreads)
5726         (gnus-message 3 "No more unread articles")
5727         nil)
5728        ;; Find the first unread article.
5729        (t
5730         (let ((data gnus-newsgroup-data))
5731           (while (and data
5732                       (and (not (and undownloaded
5733                                      (eq gnus-undownloaded-mark
5734                                          (gnus-data-mark (car data)))))
5735                            (not (gnus-data-unread-p (car data)))))
5736             (setq data (cdr data)))
5737           (when data
5738             (goto-char (gnus-data-pos (car data)))
5739             (gnus-data-number (car data))))))
5740     (gnus-summary-position-point)))
5741
5742 (defun gnus-summary-next-subject (n &optional unread dont-display)
5743   "Go to next N'th summary line.
5744 If N is negative, go to the previous N'th subject line.
5745 If UNREAD is non-nil, only unread articles are selected.
5746 The difference between N and the actual number of steps taken is
5747 returned."
5748   (interactive "p")
5749   (let ((backward (< n 0))
5750         (n (abs n)))
5751     (while (and (> n 0)
5752                 (if backward
5753                     (gnus-summary-find-prev unread)
5754                   (gnus-summary-find-next unread)))
5755       (unless (zerop (setq n (1- n)))
5756         (gnus-summary-show-thread)))
5757     (when (/= 0 n)
5758       (gnus-message 7 "No more%s articles"
5759                     (if unread " unread" "")))
5760     (unless dont-display
5761       (gnus-summary-recenter)
5762       (gnus-summary-position-point))
5763     n))
5764
5765 (defun gnus-summary-next-unread-subject (n)
5766   "Go to next N'th unread summary line."
5767   (interactive "p")
5768   (gnus-summary-next-subject n t))
5769
5770 (defun gnus-summary-prev-subject (n &optional unread)
5771   "Go to previous N'th summary line.
5772 If optional argument UNREAD is non-nil, only unread article is selected."
5773   (interactive "p")
5774   (gnus-summary-next-subject (- n) unread))
5775
5776 (defun gnus-summary-prev-unread-subject (n)
5777   "Go to previous N'th unread summary line."
5778   (interactive "p")
5779   (gnus-summary-next-subject (- n) t))
5780
5781 (defun gnus-summary-goto-subject (article &optional force silent)
5782   "Go the subject line of ARTICLE.
5783 If FORCE, also allow jumping to articles not currently shown."
5784   (interactive "nArticle number: ")
5785   (let ((b (point))
5786         (data (gnus-data-find article)))
5787     ;; We read in the article if we have to.
5788     (and (not data)
5789          force
5790          (gnus-summary-insert-subject
5791           article
5792           (if (or (numberp force) (vectorp force)) force)
5793           t)
5794          (setq data (gnus-data-find article)))
5795     (goto-char b)
5796     (if (not data)
5797         (progn
5798           (unless silent
5799             (gnus-message 3 "Can't find article %d" article))
5800           nil)
5801       (goto-char (gnus-data-pos data))
5802       (gnus-summary-position-point)
5803       article)))
5804
5805 ;; Walking around summary lines with displaying articles.
5806
5807 (defun gnus-summary-expand-window (&optional arg)
5808   "Make the summary buffer take up the entire Emacs frame.
5809 Given a prefix, will force an `article' buffer configuration."
5810   (interactive "P")
5811   (if arg
5812       (gnus-configure-windows 'article 'force)
5813     (gnus-configure-windows 'summary 'force)))
5814
5815 (defun gnus-summary-display-article (article &optional all-header)
5816   "Display ARTICLE in article buffer."
5817   (gnus-set-global-variables)
5818   (if (null article)
5819       nil
5820     (prog1
5821         (if gnus-summary-display-article-function
5822             (funcall gnus-summary-display-article-function article all-header)
5823           (gnus-article-prepare article all-header))
5824       (gnus-run-hooks 'gnus-select-article-hook)
5825       (when (and gnus-current-article
5826                  (not (zerop gnus-current-article)))
5827         (gnus-summary-goto-subject gnus-current-article))
5828       (gnus-summary-recenter)
5829       (when (and gnus-use-trees gnus-show-threads)
5830         (gnus-possibly-generate-tree article)
5831         (gnus-highlight-selected-tree article))
5832       ;; Successfully display article.
5833       (gnus-article-set-window-start
5834        (cdr (assq article gnus-newsgroup-bookmarks))))))
5835
5836 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5837   "Select the current article.
5838 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5839 non-nil, the article will be re-fetched even if it already present in
5840 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5841 be displayed."
5842   ;; Make sure we are in the summary buffer to work around bbdb bug.
5843   (unless (eq major-mode 'gnus-summary-mode)
5844     (set-buffer gnus-summary-buffer))
5845   (let ((article (or article (gnus-summary-article-number)))
5846         (all-headers (not (not all-headers))) ;Must be T or NIL.
5847         gnus-summary-display-article-function)
5848     (and (not pseudo)
5849          (gnus-summary-article-pseudo-p article)
5850          (error "This is a pseudo-article"))
5851     (save-excursion
5852       (set-buffer gnus-summary-buffer)
5853       (if (or (and gnus-single-article-buffer
5854                    (or (null gnus-current-article)
5855                        (null gnus-article-current)
5856                        (null (get-buffer gnus-article-buffer))
5857                        (not (eq article (cdr gnus-article-current)))
5858                        (not (equal (car gnus-article-current)
5859                                    gnus-newsgroup-name))))
5860               (and (not gnus-single-article-buffer)
5861                    (or (null gnus-current-article)
5862                        (not (eq gnus-current-article article))))
5863               force)
5864           ;; The requested article is different from the current article.
5865           (progn
5866             (when (gnus-buffer-live-p gnus-article-buffer)
5867               (with-current-buffer gnus-article-buffer
5868                 (mm-enable-multibyte)))
5869             (gnus-summary-display-article article all-headers)
5870             (when (gnus-buffer-live-p gnus-article-buffer)
5871               (with-current-buffer gnus-article-buffer
5872                 (if (not gnus-article-decoded-p) ;; a local variable
5873                     (mm-disable-multibyte))))
5874             (when (or all-headers gnus-show-all-headers)
5875               (gnus-article-show-all-headers))
5876             (gnus-article-set-window-start
5877              (cdr (assq article gnus-newsgroup-bookmarks)))
5878             article)
5879         (when (or all-headers gnus-show-all-headers)
5880           (gnus-article-show-all-headers))
5881         'old))))
5882
5883 (defun gnus-summary-set-current-mark (&optional current-mark)
5884   "Obsolete function."
5885   nil)
5886
5887 (defun gnus-summary-next-article (&optional unread subject backward push)
5888   "Select the next article.
5889 If UNREAD, only unread articles are selected.
5890 If SUBJECT, only articles with SUBJECT are selected.
5891 If BACKWARD, the previous article is selected instead of the next."
5892   (interactive "P")
5893   (cond
5894    ;; Is there such an article?
5895    ((and (gnus-summary-search-forward unread subject backward)
5896          (or (gnus-summary-display-article (gnus-summary-article-number))
5897              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5898     (gnus-summary-position-point))
5899    ;; If not, we try the first unread, if that is wanted.
5900    ((and subject
5901          gnus-auto-select-same
5902          (gnus-summary-first-unread-article))
5903     (gnus-summary-position-point)
5904     (gnus-message 6 "Wrapped"))
5905    ;; Try to get next/previous article not displayed in this group.
5906    ((and gnus-auto-extend-newsgroup
5907          (not unread) (not subject))
5908     (gnus-summary-goto-article
5909      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5910      nil (count-lines (point-min) (point))))
5911    ;; Go to next/previous group.
5912    (t
5913     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5914       (gnus-summary-jump-to-group gnus-newsgroup-name))
5915     (let ((cmd last-command-char)
5916           (point
5917            (save-excursion
5918              (set-buffer gnus-group-buffer)
5919              (point)))
5920           (group
5921            (if (eq gnus-keep-same-level 'best)
5922                (gnus-summary-best-group gnus-newsgroup-name)
5923              (gnus-summary-search-group backward gnus-keep-same-level))))
5924       ;; For some reason, the group window gets selected.  We change
5925       ;; it back.
5926       (select-window (get-buffer-window (current-buffer)))
5927       ;; Select next unread newsgroup automagically.
5928       (cond
5929        ((or (not gnus-auto-select-next)
5930             (not cmd))
5931         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5932        ((or (eq gnus-auto-select-next 'quietly)
5933             (and (eq gnus-auto-select-next 'slightly-quietly)
5934                  push)
5935             (and (eq gnus-auto-select-next 'almost-quietly)
5936                  (gnus-summary-last-article-p)))
5937         ;; Select quietly.
5938         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5939             (gnus-summary-exit)
5940           (gnus-message 7 "No more%s articles (%s)..."
5941                         (if unread " unread" "")
5942                         (if group (concat "selecting " group)
5943                           "exiting"))
5944           (gnus-summary-next-group nil group backward)))
5945        (t
5946         (when (gnus-key-press-event-p last-input-event)
5947           (gnus-summary-walk-group-buffer
5948            gnus-newsgroup-name cmd unread backward point))))))))
5949
5950 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5951   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5952                       (?\C-p (gnus-group-prev-unread-group 1))))
5953         (cursor-in-echo-area t)
5954         keve key group ended)
5955     (save-excursion
5956       (set-buffer gnus-group-buffer)
5957       (goto-char start)
5958       (setq group
5959             (if (eq gnus-keep-same-level 'best)
5960                 (gnus-summary-best-group gnus-newsgroup-name)
5961               (gnus-summary-search-group backward gnus-keep-same-level))))
5962     (while (not ended)
5963       (gnus-message
5964        5 "No more%s articles%s" (if unread " unread" "")
5965        (if (and group
5966                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5967            (format " (Type %s for %s [%s])"
5968                    (single-key-description cmd) group
5969                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5970          (format " (Type %s to exit %s)"
5971                  (single-key-description cmd)
5972                  gnus-newsgroup-name)))
5973       ;; Confirm auto selection.
5974       (setq key (car (setq keve (gnus-read-event-char))))
5975       (setq ended t)
5976       (cond
5977        ((assq key keystrokes)
5978         (let ((obuf (current-buffer)))
5979           (switch-to-buffer gnus-group-buffer)
5980           (when group
5981             (gnus-group-jump-to-group group))
5982           (eval (cadr (assq key keystrokes)))
5983           (setq group (gnus-group-group-name))
5984           (switch-to-buffer obuf))
5985         (setq ended nil))
5986        ((equal key cmd)
5987         (if (or (not group)
5988                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5989             (gnus-summary-exit)
5990           (gnus-summary-next-group nil group backward)))
5991        (t
5992         (push (cdr keve) unread-command-events))))))
5993
5994 (defun gnus-summary-next-unread-article ()
5995   "Select unread article after current one."
5996   (interactive)
5997   (gnus-summary-next-article
5998    (or (not (eq gnus-summary-goto-unread 'never))
5999        (gnus-summary-last-article-p (gnus-summary-article-number)))
6000    (and gnus-auto-select-same
6001         (gnus-summary-article-subject))))
6002
6003 (defun gnus-summary-prev-article (&optional unread subject)
6004   "Select the article after the current one.
6005 If UNREAD is non-nil, only unread articles are selected."
6006   (interactive "P")
6007   (gnus-summary-next-article unread subject t))
6008
6009 (defun gnus-summary-prev-unread-article ()
6010   "Select unread article before current one."
6011   (interactive)
6012   (gnus-summary-prev-article
6013    (or (not (eq gnus-summary-goto-unread 'never))
6014        (gnus-summary-first-article-p (gnus-summary-article-number)))
6015    (and gnus-auto-select-same
6016         (gnus-summary-article-subject))))
6017
6018 (defun gnus-summary-next-page (&optional lines circular)
6019   "Show next page of the selected article.
6020 If at the end of the current article, select the next article.
6021 LINES says how many lines should be scrolled up.
6022
6023 If CIRCULAR is non-nil, go to the start of the article instead of
6024 selecting the next article when reaching the end of the current
6025 article."
6026   (interactive "P")
6027   (setq gnus-summary-buffer (current-buffer))
6028   (gnus-set-global-variables)
6029   (let ((article (gnus-summary-article-number))
6030         (article-window (get-buffer-window gnus-article-buffer t))
6031         endp)
6032     ;; If the buffer is empty, we have no article.
6033     (unless article
6034       (error "No article to select"))
6035     (gnus-configure-windows 'article)
6036     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6037         (if (and (eq gnus-summary-goto-unread 'never)
6038                  (not (gnus-summary-last-article-p article)))
6039             (gnus-summary-next-article)
6040           (gnus-summary-next-unread-article))
6041       (if (or (null gnus-current-article)
6042               (null gnus-article-current)
6043               (/= article (cdr gnus-article-current))
6044               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6045           ;; Selected subject is different from current article's.
6046           (gnus-summary-display-article article)
6047         (when article-window
6048           (gnus-eval-in-buffer-window gnus-article-buffer
6049             (setq endp (gnus-article-next-page lines)))
6050           (when endp
6051             (cond (circular
6052                    (gnus-summary-beginning-of-article))
6053                   (lines
6054                    (gnus-message 3 "End of message"))
6055                   ((null lines)
6056                    (if (and (eq gnus-summary-goto-unread 'never)
6057                             (not (gnus-summary-last-article-p article)))
6058                        (gnus-summary-next-article)
6059                      (gnus-summary-next-unread-article))))))))
6060     (gnus-summary-recenter)
6061     (gnus-summary-position-point)))
6062
6063 (defun gnus-summary-prev-page (&optional lines move)
6064   "Show previous page of selected article.
6065 Argument LINES specifies lines to be scrolled down.
6066 If MOVE, move to the previous unread article if point is at
6067 the beginning of the buffer."
6068   (interactive "P")
6069   (let ((article (gnus-summary-article-number))
6070         (article-window (get-buffer-window gnus-article-buffer t))
6071         endp)
6072     (gnus-configure-windows 'article)
6073     (if (or (null gnus-current-article)
6074             (null gnus-article-current)
6075             (/= article (cdr gnus-article-current))
6076             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6077         ;; Selected subject is different from current article's.
6078         (gnus-summary-display-article article)
6079       (gnus-summary-recenter)
6080       (when article-window
6081         (gnus-eval-in-buffer-window gnus-article-buffer
6082           (setq endp (gnus-article-prev-page lines)))
6083         (when (and move endp)
6084           (cond (lines
6085                  (gnus-message 3 "Beginning of message"))
6086                 ((null lines)
6087                  (if (and (eq gnus-summary-goto-unread 'never)
6088                           (not (gnus-summary-first-article-p article)))
6089                      (gnus-summary-prev-article)
6090                    (gnus-summary-prev-unread-article))))))))
6091   (gnus-summary-position-point))
6092
6093 (defun gnus-summary-prev-page-or-article (&optional lines)
6094   "Show previous page of selected article.
6095 Argument LINES specifies lines to be scrolled down.
6096 If at the beginning of the article, go to the next article."
6097   (interactive "P")
6098   (gnus-summary-prev-page lines t))
6099
6100 (defun gnus-summary-scroll-up (lines)
6101   "Scroll up (or down) one line current article.
6102 Argument LINES specifies lines to be scrolled up (or down if negative)."
6103   (interactive "p")
6104   (gnus-configure-windows 'article)
6105   (gnus-summary-show-thread)
6106   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6107     (gnus-eval-in-buffer-window gnus-article-buffer
6108       (cond ((> lines 0)
6109              (when (gnus-article-next-page lines)
6110                (gnus-message 3 "End of message")))
6111             ((< lines 0)
6112              (gnus-article-prev-page (- lines))))))
6113   (gnus-summary-recenter)
6114   (gnus-summary-position-point))
6115
6116 (defun gnus-summary-scroll-down (lines)
6117   "Scroll down (or up) one line current article.
6118 Argument LINES specifies lines to be scrolled down (or up if negative)."
6119   (interactive "p")
6120   (gnus-summary-scroll-up (- lines)))
6121
6122 (defun gnus-summary-next-same-subject ()
6123   "Select next article which has the same subject as current one."
6124   (interactive)
6125   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6126
6127 (defun gnus-summary-prev-same-subject ()
6128   "Select previous article which has the same subject as current one."
6129   (interactive)
6130   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6131
6132 (defun gnus-summary-next-unread-same-subject ()
6133   "Select next unread article which has the same subject as current one."
6134   (interactive)
6135   (gnus-summary-next-article t (gnus-summary-article-subject)))
6136
6137 (defun gnus-summary-prev-unread-same-subject ()
6138   "Select previous unread article which has the same subject as current one."
6139   (interactive)
6140   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6141
6142 (defun gnus-summary-first-unread-article ()
6143   "Select the first unread article.
6144 Return nil if there are no unread articles."
6145   (interactive)
6146   (prog1
6147       (when (gnus-summary-first-subject t)
6148         (gnus-summary-show-thread)
6149         (gnus-summary-first-subject t)
6150         (gnus-summary-display-article (gnus-summary-article-number)))
6151     (gnus-summary-position-point)))
6152
6153 (defun gnus-summary-first-unread-subject ()
6154   "Place the point on the subject line of the first unread article.
6155 Return nil if there are no unread articles."
6156   (interactive)
6157   (prog1
6158       (when (gnus-summary-first-subject t)
6159         (gnus-summary-show-thread)
6160         (gnus-summary-first-subject t))
6161     (gnus-summary-position-point)))
6162
6163 (defun gnus-summary-first-article ()
6164   "Select the first article.
6165 Return nil if there are no articles."
6166   (interactive)
6167   (prog1
6168       (when (gnus-summary-first-subject)
6169         (gnus-summary-show-thread)
6170         (gnus-summary-first-subject)
6171         (gnus-summary-display-article (gnus-summary-article-number)))
6172     (gnus-summary-position-point)))
6173
6174 (defun gnus-summary-best-unread-article ()
6175   "Select the unread article with the highest score."
6176   (interactive)
6177   (let ((best -1000000)
6178         (data gnus-newsgroup-data)
6179         article score)
6180     (while data
6181       (and (gnus-data-unread-p (car data))
6182            (> (setq score
6183                     (gnus-summary-article-score (gnus-data-number (car data))))
6184               best)
6185            (setq best score
6186                  article (gnus-data-number (car data))))
6187       (setq data (cdr data)))
6188     (prog1
6189         (if article
6190             (gnus-summary-goto-article article)
6191           (error "No unread articles"))
6192       (gnus-summary-position-point))))
6193
6194 (defun gnus-summary-last-subject ()
6195   "Go to the last displayed subject line in the group."
6196   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6197     (when article
6198       (gnus-summary-goto-subject article))))
6199
6200 (defun gnus-summary-goto-article (article &optional all-headers force)
6201   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6202 If ALL-HEADERS is non-nil, no header lines are hidden.
6203 If FORCE, go to the article even if it isn't displayed.  If FORCE
6204 is a number, it is the line the article is to be displayed on."
6205   (interactive
6206    (list
6207     (completing-read
6208      "Article number or Message-ID: "
6209      (mapcar (lambda (number) (list (int-to-string number)))
6210              gnus-newsgroup-limit))
6211     current-prefix-arg
6212     t))
6213   (prog1
6214       (if (and (stringp article)
6215                (string-match "@" article))
6216           (gnus-summary-refer-article article)
6217         (when (stringp article)
6218           (setq article (string-to-number article)))
6219         (if (gnus-summary-goto-subject article force)
6220             (gnus-summary-display-article article all-headers)
6221           (gnus-message 4 "Couldn't go to article %s" article) nil))
6222     (gnus-summary-position-point)))
6223
6224 (defun gnus-summary-goto-last-article ()
6225   "Go to the previously read article."
6226   (interactive)
6227   (prog1
6228       (when gnus-last-article
6229         (gnus-summary-goto-article gnus-last-article nil t))
6230     (gnus-summary-position-point)))
6231
6232 (defun gnus-summary-pop-article (number)
6233   "Pop one article off the history and go to the previous.
6234 NUMBER articles will be popped off."
6235   (interactive "p")
6236   (let (to)
6237     (setq gnus-newsgroup-history
6238           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6239     (if to
6240         (gnus-summary-goto-article (car to) nil t)
6241       (error "Article history empty")))
6242   (gnus-summary-position-point))
6243
6244 ;; Summary commands and functions for limiting the summary buffer.
6245
6246 (defun gnus-summary-limit-to-articles (n)
6247   "Limit the summary buffer to the next N articles.
6248 If not given a prefix, use the process marked articles instead."
6249   (interactive "P")
6250   (prog1
6251       (let ((articles (gnus-summary-work-articles n)))
6252         (setq gnus-newsgroup-processable nil)
6253         (gnus-summary-limit articles))
6254     (gnus-summary-position-point)))
6255
6256 (defun gnus-summary-pop-limit (&optional total)
6257   "Restore the previous limit.
6258 If given a prefix, remove all limits."
6259   (interactive "P")
6260   (when total
6261     (setq gnus-newsgroup-limits
6262           (list (mapcar (lambda (h) (mail-header-number h))
6263                         gnus-newsgroup-headers))))
6264   (unless gnus-newsgroup-limits
6265     (error "No limit to pop"))
6266   (prog1
6267       (gnus-summary-limit nil 'pop)
6268     (gnus-summary-position-point)))
6269
6270 (defun gnus-summary-limit-to-subject (subject &optional header)
6271   "Limit the summary buffer to articles that have subjects that match a regexp."
6272   (interactive "sLimit to subject (regexp): ")
6273   (unless header
6274     (setq header "subject"))
6275   (when (not (equal "" subject))
6276     (prog1
6277         (let ((articles (gnus-summary-find-matching
6278                          (or header "subject") subject 'all)))
6279           (unless articles
6280             (error "Found no matches for \"%s\"" subject))
6281           (gnus-summary-limit articles))
6282       (gnus-summary-position-point))))
6283
6284 (defun gnus-summary-limit-to-author (from)
6285   "Limit the summary buffer to articles that have authors that match a regexp."
6286   (interactive "sLimit to author (regexp): ")
6287   (gnus-summary-limit-to-subject from "from"))
6288
6289 (defun gnus-summary-limit-to-age (age &optional younger-p)
6290   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6291 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6292 articles that are younger than AGE days."
6293   (interactive
6294    (let ((younger current-prefix-arg)
6295          (days-got nil)
6296          days)
6297      (while (not days-got)
6298        (setq days (if younger
6299                       (read-string "Limit to articles within (in days): ")
6300                     (read-string "Limit to articles old than (in days): ")))
6301        (when (> (length days) 0)
6302          (setq days (read days)))
6303        (if (numberp days)
6304            (setq days-got t)
6305          (message "Please enter a number.")
6306          (sleep-for 1)))
6307      (list days younger)))
6308   (prog1
6309       (let ((data gnus-newsgroup-data)
6310             (cutoff (days-to-time age))
6311             articles d date is-younger)
6312         (while (setq d (pop data))
6313           (when (and (vectorp (gnus-data-header d))
6314                      (setq date (mail-header-date (gnus-data-header d))))
6315             (setq is-younger (time-less-p
6316                               (time-since (condition-case ()
6317                                               (date-to-time date)
6318                                             (error '(0 0))))
6319                               cutoff))
6320             (when (if younger-p
6321                       is-younger
6322                     (not is-younger))
6323               (push (gnus-data-number d) articles))))
6324         (gnus-summary-limit (nreverse articles)))
6325     (gnus-summary-position-point)))
6326
6327 (defun gnus-summary-limit-to-extra (header regexp)
6328   "Limit the summary buffer to articles that match an 'extra' header."
6329   (interactive
6330    (let ((header
6331           (intern
6332            (gnus-completing-read
6333             (symbol-name (car gnus-extra-headers))
6334             "Limit extra header:"
6335             (mapcar (lambda (x)
6336                       (cons (symbol-name x) x))
6337                     gnus-extra-headers)
6338             nil
6339             t))))
6340      (list header
6341            (read-string (format "Limit to header %s (regexp): " header)))))
6342   (when (not (equal "" regexp))
6343     (prog1
6344         (let ((articles (gnus-summary-find-matching
6345                          (cons 'extra header) regexp 'all)))
6346           (unless articles
6347             (error "Found no matches for \"%s\"" regexp))
6348           (gnus-summary-limit articles))
6349       (gnus-summary-position-point))))
6350
6351 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6352 (make-obsolete
6353  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6354
6355 (defun gnus-summary-limit-to-unread (&optional all)
6356   "Limit the summary buffer to articles that are not marked as read.
6357 If ALL is non-nil, limit strictly to unread articles."
6358   (interactive "P")
6359   (if all
6360       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6361     (gnus-summary-limit-to-marks
6362      ;; Concat all the marks that say that an article is read and have
6363      ;; those removed.
6364      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6365            gnus-killed-mark gnus-kill-file-mark
6366            gnus-low-score-mark gnus-expirable-mark
6367            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6368            gnus-duplicate-mark gnus-souped-mark)
6369      'reverse)))
6370
6371 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6372 (make-obsolete 'gnus-summary-delete-marked-with
6373                'gnus-summary-limit-exlude-marks)
6374
6375 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6376   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6377 If REVERSE, limit the summary buffer to articles that are marked
6378 with MARKS.  MARKS can either be a string of marks or a list of marks.
6379 Returns how many articles were removed."
6380   (interactive "sMarks: ")
6381   (gnus-summary-limit-to-marks marks t))
6382
6383 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6384   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6385 If REVERSE (the prefix), limit the summary buffer to articles that are
6386 not marked with MARKS.  MARKS can either be a string of marks or a
6387 list of marks.
6388 Returns how many articles were removed."
6389   (interactive "sMarks: \nP")
6390   (prog1
6391       (let ((data gnus-newsgroup-data)
6392             (marks (if (listp marks) marks
6393                      (append marks nil))) ; Transform to list.
6394             articles)
6395         (while data
6396           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6397                   (memq (gnus-data-mark (car data)) marks))
6398             (push (gnus-data-number (car data)) articles))
6399           (setq data (cdr data)))
6400         (gnus-summary-limit articles))
6401     (gnus-summary-position-point)))
6402
6403 (defun gnus-summary-limit-to-score (&optional score)
6404   "Limit to articles with score at or above SCORE."
6405   (interactive "P")
6406   (setq score (if score
6407                   (prefix-numeric-value score)
6408                 (or gnus-summary-default-score 0)))
6409   (let ((data gnus-newsgroup-data)
6410         articles)
6411     (while data
6412       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6413                 score)
6414         (push (gnus-data-number (car data)) articles))
6415       (setq data (cdr data)))
6416     (prog1
6417         (gnus-summary-limit articles)
6418       (gnus-summary-position-point))))
6419
6420 (defun gnus-summary-limit-include-thread (id)
6421   "Display all the hidden articles that in the current thread."
6422   (interactive (list (mail-header-id (gnus-summary-article-header))))
6423   (let ((articles (gnus-articles-in-thread
6424                    (gnus-id-to-thread (gnus-root-id id)))))
6425     (prog1
6426         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6427       (gnus-summary-position-point))))
6428
6429 (defun gnus-summary-limit-include-dormant ()
6430   "Display all the hidden articles that are marked as dormant.
6431 Note that this command only works on a subset of the articles currently
6432 fetched for this group."
6433   (interactive)
6434   (unless gnus-newsgroup-dormant
6435     (error "There are no dormant articles in this group"))
6436   (prog1
6437       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6438     (gnus-summary-position-point)))
6439
6440 (defun gnus-summary-limit-exclude-dormant ()
6441   "Hide all dormant articles."
6442   (interactive)
6443   (prog1
6444       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6445     (gnus-summary-position-point)))
6446
6447 (defun gnus-summary-limit-exclude-childless-dormant ()
6448   "Hide all dormant articles that have no children."
6449   (interactive)
6450   (let ((data (gnus-data-list t))
6451         articles d children)
6452     ;; Find all articles that are either not dormant or have
6453     ;; children.
6454     (while (setq d (pop data))
6455       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6456                 (and (setq children
6457                            (gnus-article-children (gnus-data-number d)))
6458                      (let (found)
6459                        (while children
6460                          (when (memq (car children) articles)
6461                            (setq children nil
6462                                  found t))
6463                          (pop children))
6464                        found)))
6465         (push (gnus-data-number d) articles)))
6466     ;; Do the limiting.
6467     (prog1
6468         (gnus-summary-limit articles)
6469       (gnus-summary-position-point))))
6470
6471 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6472   "Mark all unread excluded articles as read.
6473 If ALL, mark even excluded ticked and dormants as read."
6474   (interactive "P")
6475   (let ((articles (gnus-sorted-complement
6476                    (sort
6477                     (mapcar (lambda (h) (mail-header-number h))
6478                             gnus-newsgroup-headers)
6479                     '<)
6480                    (sort gnus-newsgroup-limit '<)))
6481         article)
6482     (setq gnus-newsgroup-unreads
6483           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6484     (if all
6485         (setq gnus-newsgroup-dormant nil
6486               gnus-newsgroup-marked nil
6487               gnus-newsgroup-reads
6488               (nconc
6489                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6490                gnus-newsgroup-reads))
6491       (while (setq article (pop articles))
6492         (unless (or (memq article gnus-newsgroup-dormant)
6493                     (memq article gnus-newsgroup-marked))
6494           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6495
6496 (defun gnus-summary-limit (articles &optional pop)
6497   (if pop
6498       ;; We pop the previous limit off the stack and use that.
6499       (setq articles (car gnus-newsgroup-limits)
6500             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6501     ;; We use the new limit, so we push the old limit on the stack.
6502     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6503   ;; Set the limit.
6504   (setq gnus-newsgroup-limit articles)
6505   (let ((total (length gnus-newsgroup-data))
6506         (data (gnus-data-find-list (gnus-summary-article-number)))
6507         (gnus-summary-mark-below nil)   ; Inhibit this.
6508         found)
6509     ;; This will do all the work of generating the new summary buffer
6510     ;; according to the new limit.
6511     (gnus-summary-prepare)
6512     ;; Hide any threads, possibly.
6513     (and gnus-show-threads
6514          gnus-thread-hide-subtree
6515          (gnus-summary-hide-all-threads))
6516     ;; Try to return to the article you were at, or one in the
6517     ;; neighborhood.
6518     (when data
6519       ;; We try to find some article after the current one.
6520       (while data
6521         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6522           (setq data nil
6523                 found t))
6524         (setq data (cdr data))))
6525     (unless found
6526       ;; If there is no data, that means that we were after the last
6527       ;; article.  The same goes when we can't find any articles
6528       ;; after the current one.
6529       (goto-char (point-max))
6530       (gnus-summary-find-prev))
6531     (gnus-set-mode-line 'summary)
6532     ;; We return how many articles were removed from the summary
6533     ;; buffer as a result of the new limit.
6534     (- total (length gnus-newsgroup-data))))
6535
6536 (defsubst gnus-invisible-cut-children (threads)
6537   (let ((num 0))
6538     (while threads
6539       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6540         (incf num))
6541       (pop threads))
6542     (< num 2)))
6543
6544 (defsubst gnus-cut-thread (thread)
6545   "Go forwards in the thread until we find an article that we want to display."
6546   (when (or (eq gnus-fetch-old-headers 'some)
6547             (eq gnus-fetch-old-headers 'invisible)
6548             (numberp gnus-fetch-old-headers)
6549             (eq gnus-build-sparse-threads 'some)
6550             (eq gnus-build-sparse-threads 'more))
6551     ;; Deal with old-fetched headers and sparse threads.
6552     (while (and
6553             thread
6554             (or
6555              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6556              (gnus-summary-article-ancient-p
6557               (mail-header-number (car thread))))
6558             (if (or (<= (length (cdr thread)) 1)
6559                     (eq gnus-fetch-old-headers 'invisible))
6560                 (setq gnus-newsgroup-limit
6561                       (delq (mail-header-number (car thread))
6562                             gnus-newsgroup-limit)
6563                       thread (cadr thread))
6564               (when (gnus-invisible-cut-children (cdr thread))
6565                 (let ((th (cdr thread)))
6566                   (while th
6567                     (if (memq (mail-header-number (caar th))
6568                               gnus-newsgroup-limit)
6569                         (setq thread (car th)
6570                               th nil)
6571                       (setq th (cdr th))))))))))
6572   thread)
6573
6574 (defun gnus-cut-threads (threads)
6575   "Cut off all uninteresting articles from the beginning of threads."
6576   (when (or (eq gnus-fetch-old-headers 'some)
6577             (eq gnus-fetch-old-headers 'invisible)
6578             (numberp gnus-fetch-old-headers)
6579             (eq gnus-build-sparse-threads 'some)
6580             (eq gnus-build-sparse-threads 'more))
6581     (let ((th threads))
6582       (while th
6583         (setcar th (gnus-cut-thread (car th)))
6584         (setq th (cdr th)))))
6585   ;; Remove nixed out threads.
6586   (delq nil threads))
6587
6588 (defun gnus-summary-initial-limit (&optional show-if-empty)
6589   "Figure out what the initial limit is supposed to be on group entry.
6590 This entails weeding out unwanted dormants, low-scored articles,
6591 fetch-old-headers verbiage, and so on."
6592   ;; Most groups have nothing to remove.
6593   (if (or gnus-inhibit-limiting
6594           (and (null gnus-newsgroup-dormant)
6595                (not (eq gnus-fetch-old-headers 'some))
6596                (not (numberp gnus-fetch-old-headers))
6597                (not (eq gnus-fetch-old-headers 'invisible))
6598                (null gnus-summary-expunge-below)
6599                (not (eq gnus-build-sparse-threads 'some))
6600                (not (eq gnus-build-sparse-threads 'more))
6601                (null gnus-thread-expunge-below)
6602                (not gnus-use-nocem)))
6603       ()                                ; Do nothing.
6604     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6605     (setq gnus-newsgroup-limit nil)
6606     (mapatoms
6607      (lambda (node)
6608        (unless (car (symbol-value node))
6609          ;; These threads have no parents -- they are roots.
6610          (let ((nodes (cdr (symbol-value node)))
6611                thread)
6612            (while nodes
6613              (if (and gnus-thread-expunge-below
6614                       (< (gnus-thread-total-score (car nodes))
6615                          gnus-thread-expunge-below))
6616                  (gnus-expunge-thread (pop nodes))
6617                (setq thread (pop nodes))
6618                (gnus-summary-limit-children thread))))))
6619      gnus-newsgroup-dependencies)
6620     ;; If this limitation resulted in an empty group, we might
6621     ;; pop the previous limit and use it instead.
6622     (when (and (not gnus-newsgroup-limit)
6623                show-if-empty)
6624       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6625     gnus-newsgroup-limit))
6626
6627 (defun gnus-summary-limit-children (thread)
6628   "Return 1 if this subthread is visible and 0 if it is not."
6629   ;; First we get the number of visible children to this thread.  This
6630   ;; is done by recursing down the thread using this function, so this
6631   ;; will really go down to a leaf article first, before slowly
6632   ;; working its way up towards the root.
6633   (when thread
6634     (let ((children
6635            (if (cdr thread)
6636                (apply '+ (mapcar 'gnus-summary-limit-children
6637                                  (cdr thread)))
6638              0))
6639           (number (mail-header-number (car thread)))
6640           score)
6641       (if (and
6642            (not (memq number gnus-newsgroup-marked))
6643            (or
6644             ;; If this article is dormant and has absolutely no visible
6645             ;; children, then this article isn't visible.
6646             (and (memq number gnus-newsgroup-dormant)
6647                  (zerop children))
6648             ;; If this is "fetch-old-headered" and there is no
6649             ;; visible children, then we don't want this article.
6650             (and (or (eq gnus-fetch-old-headers 'some)
6651                      (numberp gnus-fetch-old-headers))
6652                  (gnus-summary-article-ancient-p number)
6653                  (zerop children))
6654             ;; If this is "fetch-old-headered" and `invisible', then
6655             ;; we don't want this article.
6656             (and (eq gnus-fetch-old-headers 'invisible)
6657                  (gnus-summary-article-ancient-p number))
6658             ;; If this is a sparsely inserted article with no children,
6659             ;; we don't want it.
6660             (and (eq gnus-build-sparse-threads 'some)
6661                  (gnus-summary-article-sparse-p number)
6662                  (zerop children))
6663             ;; If we use expunging, and this article is really
6664             ;; low-scored, then we don't want this article.
6665             (when (and gnus-summary-expunge-below
6666                        (< (setq score
6667                                 (or (cdr (assq number gnus-newsgroup-scored))
6668                                     gnus-summary-default-score))
6669                           gnus-summary-expunge-below))
6670               ;; We increase the expunge-tally here, but that has
6671               ;; nothing to do with the limits, really.
6672               (incf gnus-newsgroup-expunged-tally)
6673               ;; We also mark as read here, if that's wanted.
6674               (when (and gnus-summary-mark-below
6675                          (< score gnus-summary-mark-below))
6676                 (setq gnus-newsgroup-unreads
6677                       (delq number gnus-newsgroup-unreads))
6678                 (if gnus-newsgroup-auto-expire
6679                     (push number gnus-newsgroup-expirable)
6680                   (push (cons number gnus-low-score-mark)
6681                         gnus-newsgroup-reads)))
6682               t)
6683             ;; Check NoCeM things.
6684             (if (and gnus-use-nocem
6685                      (gnus-nocem-unwanted-article-p
6686                       (mail-header-id (car thread))))
6687                 (progn
6688                   (setq gnus-newsgroup-unreads
6689                         (delq number gnus-newsgroup-unreads))
6690                   t))))
6691           ;; Nope, invisible article.
6692           0
6693         ;; Ok, this article is to be visible, so we add it to the limit
6694         ;; and return 1.
6695         (push number gnus-newsgroup-limit)
6696         1))))
6697
6698 (defun gnus-expunge-thread (thread)
6699   "Mark all articles in THREAD as read."
6700   (let* ((number (mail-header-number (car thread))))
6701     (incf gnus-newsgroup-expunged-tally)
6702     ;; We also mark as read here, if that's wanted.
6703     (setq gnus-newsgroup-unreads
6704           (delq number gnus-newsgroup-unreads))
6705     (if gnus-newsgroup-auto-expire
6706         (push number gnus-newsgroup-expirable)
6707       (push (cons number gnus-low-score-mark)
6708             gnus-newsgroup-reads)))
6709   ;; Go recursively through all subthreads.
6710   (mapcar 'gnus-expunge-thread (cdr thread)))
6711
6712 ;; Summary article oriented commands
6713
6714 (defun gnus-summary-refer-parent-article (n)
6715   "Refer parent article N times.
6716 If N is negative, go to ancestor -N instead.
6717 The difference between N and the number of articles fetched is returned."
6718   (interactive "p")
6719   (let ((skip 1)
6720         error header ref)
6721     (when (not (natnump n))
6722       (setq skip (abs n)
6723             n 1))
6724     (while (and (> n 0)
6725                 (not error))
6726       (setq header (gnus-summary-article-header))
6727       (if (and (eq (mail-header-number header)
6728                    (cdr gnus-article-current))
6729                (equal gnus-newsgroup-name
6730                       (car gnus-article-current)))
6731           ;; If we try to find the parent of the currently
6732           ;; displayed article, then we take a look at the actual
6733           ;; References header, since this is slightly more
6734           ;; reliable than the References field we got from the
6735           ;; server.
6736           (save-excursion
6737             (set-buffer gnus-original-article-buffer)
6738             (nnheader-narrow-to-headers)
6739             (unless (setq ref (message-fetch-field "references"))
6740               (setq ref (message-fetch-field "in-reply-to")))
6741             (widen))
6742         (setq ref
6743               ;; It's not the current article, so we take a bet on
6744               ;; the value we got from the server.
6745               (mail-header-references header)))
6746       (if (and ref
6747                (not (equal ref "")))
6748           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6749             (gnus-message 1 "Couldn't find parent"))
6750         (gnus-message 1 "No references in article %d"
6751                       (gnus-summary-article-number))
6752         (setq error t))
6753       (decf n))
6754     (gnus-summary-position-point)
6755     n))
6756
6757 (defun gnus-summary-refer-references ()
6758   "Fetch all articles mentioned in the References header.
6759 Return the number of articles fetched."
6760   (interactive)
6761   (let ((ref (mail-header-references (gnus-summary-article-header)))
6762         (current (gnus-summary-article-number))
6763         (n 0))
6764     (if (or (not ref)
6765             (equal ref ""))
6766         (error "No References in the current article")
6767       ;; For each Message-ID in the References header...
6768       (while (string-match "<[^>]*>" ref)
6769         (incf n)
6770         ;; ... fetch that article.
6771         (gnus-summary-refer-article
6772          (prog1 (match-string 0 ref)
6773            (setq ref (substring ref (match-end 0))))))
6774       (gnus-summary-goto-subject current)
6775       (gnus-summary-position-point)
6776       n)))
6777
6778 (defun gnus-summary-refer-thread (&optional limit)
6779   "Fetch all articles in the current thread.
6780 If LIMIT (the numerical prefix), fetch that many old headers instead
6781 of what's specified by the `gnus-refer-thread-limit' variable."
6782   (interactive "P")
6783   (let ((id (mail-header-id (gnus-summary-article-header)))
6784         (limit (if limit (prefix-numeric-value limit)
6785                  gnus-refer-thread-limit)))
6786     ;; We want to fetch LIMIT *old* headers, but we also have to
6787     ;; re-fetch all the headers in the current buffer, because many of
6788     ;; them may be undisplayed.  So we adjust LIMIT.
6789     (when (numberp limit)
6790       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6791     (unless (eq gnus-fetch-old-headers 'invisible)
6792       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6793       ;; Retrieve the headers and read them in.
6794       (if (eq (gnus-retrieve-headers
6795                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6796               'nov)
6797           (gnus-build-all-threads)
6798         (error "Can't fetch thread from backends that don't support NOV"))
6799       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6800     (gnus-summary-limit-include-thread id)))
6801
6802 (defun gnus-summary-refer-article (message-id)
6803   "Fetch an article specified by MESSAGE-ID."
6804   (interactive "sMessage-ID: ")
6805   (when (and (stringp message-id)
6806              (not (zerop (length message-id))))
6807     ;; Construct the correct Message-ID if necessary.
6808     ;; Suggested by tale@pawl.rpi.edu.
6809     (unless (string-match "^<" message-id)
6810       (setq message-id (concat "<" message-id)))
6811     (unless (string-match ">$" message-id)
6812       (setq message-id (concat message-id ">")))
6813     (let* ((header (gnus-id-to-header message-id))
6814            (sparse (and header
6815                         (gnus-summary-article-sparse-p
6816                          (mail-header-number header))
6817                         (memq (mail-header-number header)
6818                               gnus-newsgroup-limit)))
6819            number)
6820       (cond
6821        ;; If the article is present in the buffer we just go to it.
6822        ((and header
6823              (or (not (gnus-summary-article-sparse-p
6824                        (mail-header-number header)))
6825                  sparse))
6826         (prog1
6827             (gnus-summary-goto-article
6828              (mail-header-number header) nil t)
6829           (when sparse
6830             (gnus-summary-update-article (mail-header-number header)))))
6831        (t
6832         ;; We fetch the article.
6833         (catch 'found
6834           (dolist (gnus-override-method (gnus-refer-article-methods))
6835             (gnus-check-server gnus-override-method)
6836             ;; Fetch the header, and display the article.
6837             (when (setq number (gnus-summary-insert-subject message-id))
6838               (gnus-summary-select-article nil nil nil number)
6839               (throw 'found t)))
6840           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6841
6842 (defun gnus-refer-article-methods ()
6843   "Return a list of referrable methods."
6844   (cond
6845    ;; No method, so we default to current and native.
6846    ((null gnus-refer-article-method)
6847     (list gnus-current-select-method gnus-select-method))
6848    ;; Current.
6849    ((eq 'current gnus-refer-article-method)
6850     (list gnus-current-select-method))
6851    ;; List of select methods.
6852    ((not (stringp (cadr gnus-refer-article-method)))
6853     (let (out)
6854       (dolist (method gnus-refer-article-method)
6855         (push (if (eq 'current method)
6856                   gnus-current-select-method
6857                 method)
6858               out))
6859       (nreverse out)))
6860    ;; One single select method.
6861    (t
6862     (list gnus-refer-article-method))))
6863
6864 (defun gnus-summary-edit-parameters ()
6865   "Edit the group parameters of the current group."
6866   (interactive)
6867   (gnus-group-edit-group gnus-newsgroup-name 'params))
6868
6869 (defun gnus-summary-customize-parameters ()
6870   "Customize the group parameters of the current group."
6871   (interactive)
6872   (gnus-group-customize gnus-newsgroup-name))
6873
6874 (defun gnus-summary-enter-digest-group (&optional force)
6875   "Enter an nndoc group based on the current article.
6876 If FORCE, force a digest interpretation.  If not, try
6877 to guess what the document format is."
6878   (interactive "P")
6879   (let ((conf gnus-current-window-configuration))
6880     (save-excursion
6881       (gnus-summary-select-article))
6882     (setq gnus-current-window-configuration conf)
6883     (let* ((name (format "%s-%d"
6884                          (gnus-group-prefixed-name
6885                           gnus-newsgroup-name (list 'nndoc ""))
6886                          (save-excursion
6887                            (set-buffer gnus-summary-buffer)
6888                            gnus-current-article)))
6889            (ogroup gnus-newsgroup-name)
6890            (params (append (gnus-info-params (gnus-get-info ogroup))
6891                            (list (cons 'to-group ogroup))
6892                            (list (cons 'save-article-group ogroup))))
6893            (case-fold-search t)
6894            (buf (current-buffer))
6895            dig to-address)
6896       (save-excursion
6897         (set-buffer gnus-original-article-buffer)
6898         ;; Have the digest group inherit the main mail address of
6899         ;; the parent article.
6900         (when (setq to-address (or (message-fetch-field "reply-to")
6901                                    (message-fetch-field "from")))
6902           (setq params (append (list (cons 'to-address to-address)))))
6903         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6904         (insert-buffer-substring gnus-original-article-buffer)
6905         ;; Remove lines that may lead nndoc to misinterpret the
6906         ;; document type.
6907         (narrow-to-region
6908          (goto-char (point-min))
6909          (or (search-forward "\n\n" nil t) (point)))
6910         (goto-char (point-min))
6911         (delete-matching-lines "^Path:\\|^From ")
6912         (widen))
6913       (unwind-protect
6914           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6915                     (gnus-newsgroup-ephemeral-ignored-charsets
6916                      gnus-newsgroup-ignored-charsets))
6917                 (gnus-group-read-ephemeral-group
6918                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6919                               (nndoc-article-type
6920                                ,(if force 'mbox 'guess))) t))
6921               ;; Make all postings to this group go to the parent group.
6922               (nconc (gnus-info-params (gnus-get-info name))
6923                      params)
6924             ;; Couldn't select this doc group.
6925             (switch-to-buffer buf)
6926             (gnus-set-global-variables)
6927             (gnus-configure-windows 'summary)
6928             (gnus-message 3 "Article couldn't be entered?"))
6929         (kill-buffer dig)))))
6930
6931 (defun gnus-summary-read-document (n)
6932   "Open a new group based on the current article(s).
6933 This will allow you to read digests and other similar
6934 documents as newsgroups.
6935 Obeys the standard process/prefix convention."
6936   (interactive "P")
6937   (let* ((articles (gnus-summary-work-articles n))
6938          (ogroup gnus-newsgroup-name)
6939          (params (append (gnus-info-params (gnus-get-info ogroup))
6940                          (list (cons 'to-group ogroup))))
6941          article group egroup groups vgroup)
6942     (while (setq article (pop articles))
6943       (setq group (format "%s-%d" gnus-newsgroup-name article))
6944       (gnus-summary-remove-process-mark article)
6945       (when (gnus-summary-display-article article)
6946         (save-excursion
6947           (with-temp-buffer
6948             (insert-buffer-substring gnus-original-article-buffer)
6949             ;; Remove some headers that may lead nndoc to make
6950             ;; the wrong guess.
6951             (message-narrow-to-head)
6952             (goto-char (point-min))
6953             (delete-matching-lines "^\\(Path\\):\\|^From ")
6954             (widen)
6955             (if (setq egroup
6956                       (gnus-group-read-ephemeral-group
6957                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6958                                      (nndoc-article-type guess))
6959                        t nil t))
6960                 (progn
6961                   ;; Make all postings to this group go to the parent group.
6962                   (nconc (gnus-info-params (gnus-get-info egroup))
6963                          params)
6964                   (push egroup groups))
6965               ;; Couldn't select this doc group.
6966               (gnus-error 3 "Article couldn't be entered"))))))
6967     ;; Now we have selected all the documents.
6968     (cond
6969      ((not groups)
6970       (error "None of the articles could be interpreted as documents"))
6971      ((gnus-group-read-ephemeral-group
6972        (setq vgroup (format
6973                      "nnvirtual:%s-%s" gnus-newsgroup-name
6974                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6975        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6976        t
6977        (cons (current-buffer) 'summary)))
6978      (t
6979       (error "Couldn't select virtual nndoc group")))))
6980
6981 (defun gnus-summary-isearch-article (&optional regexp-p)
6982   "Do incremental search forward on the current article.
6983 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6984   (interactive "P")
6985   (gnus-summary-select-article)
6986   (gnus-configure-windows 'article)
6987   (gnus-eval-in-buffer-window gnus-article-buffer
6988     (save-restriction
6989       (widen)
6990       (isearch-forward regexp-p))))
6991
6992 (defun gnus-summary-search-article-forward (regexp &optional backward)
6993   "Search for an article containing REGEXP forward.
6994 If BACKWARD, search backward instead."
6995   (interactive
6996    (list (read-string
6997           (format "Search article %s (regexp%s): "
6998                   (if current-prefix-arg "backward" "forward")
6999                   (if gnus-last-search-regexp
7000                       (concat ", default " gnus-last-search-regexp)
7001                     "")))
7002          current-prefix-arg))
7003   (if (string-equal regexp "")
7004       (setq regexp (or gnus-last-search-regexp ""))
7005     (setq gnus-last-search-regexp regexp))
7006   (if (gnus-summary-search-article regexp backward)
7007       (gnus-summary-show-thread)
7008     (error "Search failed: \"%s\"" regexp)))
7009
7010 (defun gnus-summary-search-article-backward (regexp)
7011   "Search for an article containing REGEXP backward."
7012   (interactive
7013    (list (read-string
7014           (format "Search article backward (regexp%s): "
7015                   (if gnus-last-search-regexp
7016                       (concat ", default " gnus-last-search-regexp)
7017                     "")))))
7018   (gnus-summary-search-article-forward regexp 'backward))
7019
7020 (defun gnus-summary-search-article (regexp &optional backward)
7021   "Search for an article containing REGEXP.
7022 Optional argument BACKWARD means do search for backward.
7023 `gnus-select-article-hook' is not called during the search."
7024   ;; We have to require this here to make sure that the following
7025   ;; dynamic binding isn't shadowed by autoloading.
7026   (require 'gnus-async)
7027   (require 'gnus-art)
7028   (let ((gnus-select-article-hook nil)  ;Disable hook.
7029         (gnus-article-prepare-hook nil)
7030         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7031         (gnus-use-article-prefetch nil)
7032         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7033         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7034         (sum (current-buffer))
7035         (gnus-display-mime-function nil)
7036         (found nil)
7037         point)
7038     (gnus-save-hidden-threads
7039       (gnus-summary-select-article)
7040       (set-buffer gnus-article-buffer)
7041       (goto-char (window-point (get-buffer-window (current-buffer))))
7042       (when backward
7043         (forward-line -1))
7044       (while (not found)
7045         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7046         (if (if backward
7047                 (re-search-backward regexp nil t)
7048               (re-search-forward regexp nil t))
7049             ;; We found the regexp.
7050             (progn
7051               (setq found 'found)
7052               (beginning-of-line)
7053               (set-window-start
7054                (get-buffer-window (current-buffer))
7055                (point))
7056               (forward-line 1)
7057               (set-window-point
7058                (get-buffer-window (current-buffer))
7059                (point))
7060               (set-buffer sum)
7061               (setq point (point)))
7062           ;; We didn't find it, so we go to the next article.
7063           (set-buffer sum)
7064           (setq found 'not)
7065           (while (eq found 'not)
7066             (if (not (if backward (gnus-summary-find-prev)
7067                        (gnus-summary-find-next)))
7068                 ;; No more articles.
7069                 (setq found t)
7070               ;; Select the next article and adjust point.
7071               (unless (gnus-summary-article-sparse-p
7072                        (gnus-summary-article-number))
7073                 (setq found nil)
7074                 (gnus-summary-select-article)
7075                 (set-buffer gnus-article-buffer)
7076                 (widen)
7077                 (goto-char (if backward (point-max) (point-min))))))))
7078       (gnus-message 7 ""))
7079     ;; Return whether we found the regexp.
7080     (when (eq found 'found)
7081       (goto-char point)
7082       (gnus-summary-show-thread)
7083       (gnus-summary-goto-subject gnus-current-article)
7084       (gnus-summary-position-point)
7085       t)))
7086
7087 (defun gnus-summary-find-matching (header regexp &optional backward unread
7088                                           not-case-fold)
7089   "Return a list of all articles that match REGEXP on HEADER.
7090 The search stars on the current article and goes forwards unless
7091 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7092 If UNREAD is non-nil, only unread articles will
7093 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7094 in the comparisons."
7095   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7096                 (gnus-data-find-list
7097                  (gnus-summary-article-number) (gnus-data-list backward))))
7098         (case-fold-search (not not-case-fold))
7099         articles d func)
7100     (if (consp header)
7101         (if (eq (car header) 'extra)
7102             (setq func
7103                   `(lambda (h)
7104                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7105                          "")))
7106           (error "%s is an invalid header" header))
7107       (unless (fboundp (intern (concat "mail-header-" header)))
7108         (error "%s is not a valid header" header))
7109       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7110     (while data
7111       (setq d (car data))
7112       (and (or (not unread)             ; We want all articles...
7113                (gnus-data-unread-p d))  ; Or just unreads.
7114            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7115            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7116            (push (gnus-data-number d) articles)) ; Success!
7117       (setq data (cdr data)))
7118     (nreverse articles)))
7119
7120 (defun gnus-summary-execute-command (header regexp command &optional backward)
7121   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7122 If HEADER is an empty string (or nil), the match is done on the entire
7123 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7124   (interactive
7125    (list (let ((completion-ignore-case t))
7126            (completing-read
7127             "Header name: "
7128             (mapcar (lambda (string) (list string))
7129                     '("Number" "Subject" "From" "Lines" "Date"
7130                       "Message-ID" "Xref" "References" "Body"))
7131             nil 'require-match))
7132          (read-string "Regexp: ")
7133          (read-key-sequence "Command: ")
7134          current-prefix-arg))
7135   (when (equal header "Body")
7136     (setq header ""))
7137   ;; Hidden thread subtrees must be searched as well.
7138   (gnus-summary-show-all-threads)
7139   ;; We don't want to change current point nor window configuration.
7140   (save-excursion
7141     (save-window-excursion
7142       (gnus-message 6 "Executing %s..." (key-description command))
7143       ;; We'd like to execute COMMAND interactively so as to give arguments.
7144       (gnus-execute header regexp
7145                     `(call-interactively ',(key-binding command))
7146                     backward)
7147       (gnus-message 6 "Executing %s...done" (key-description command)))))
7148
7149 (defun gnus-summary-beginning-of-article ()
7150   "Scroll the article back to the beginning."
7151   (interactive)
7152   (gnus-summary-select-article)
7153   (gnus-configure-windows 'article)
7154   (gnus-eval-in-buffer-window gnus-article-buffer
7155     (widen)
7156     (goto-char (point-min))
7157     (when gnus-page-broken
7158       (gnus-narrow-to-page))))
7159
7160 (defun gnus-summary-end-of-article ()
7161   "Scroll to the end of the article."
7162   (interactive)
7163   (gnus-summary-select-article)
7164   (gnus-configure-windows 'article)
7165   (gnus-eval-in-buffer-window gnus-article-buffer
7166     (widen)
7167     (goto-char (point-max))
7168     (recenter -3)
7169     (when gnus-page-broken
7170       (gnus-narrow-to-page))))
7171
7172 (defun gnus-summary-print-article (&optional filename n)
7173   "Generate and print a PostScript image of the N next (mail) articles.
7174
7175 If N is negative, print the N previous articles.  If N is nil and articles
7176 have been marked with the process mark, print these instead.
7177
7178 If the optional first argument FILENAME is nil, send the image to the
7179 printer.  If FILENAME is a string, save the PostScript image in a file with
7180 that name.  If FILENAME is a number, prompt the user for the name of the file
7181 to save in."
7182   (interactive (list (ps-print-preprint current-prefix-arg)
7183                      current-prefix-arg))
7184   (dolist (article (gnus-summary-work-articles n))
7185     (gnus-summary-select-article nil nil 'pseudo article)
7186     (gnus-eval-in-buffer-window gnus-article-buffer
7187       (let ((buffer (generate-new-buffer " *print*")))
7188         (unwind-protect
7189             (progn
7190               (copy-to-buffer buffer (point-min) (point-max))
7191               (set-buffer buffer)
7192               (gnus-article-delete-invisible-text)
7193               (let ((ps-left-header
7194                      (list
7195                       (concat "("
7196                               (mail-header-subject gnus-current-headers) ")")
7197                       (concat "("
7198                               (mail-header-from gnus-current-headers) ")")))
7199                     (ps-right-header
7200                      (list
7201                       "/pagenumberstring load"
7202                       (concat "("
7203                               (mail-header-date gnus-current-headers) ")"))))
7204                 (gnus-run-hooks 'gnus-ps-print-hook)
7205                 (save-excursion
7206                   (ps-print-buffer-with-faces filename))))
7207           (kill-buffer buffer))))))
7208
7209 (defun gnus-summary-show-article (&optional arg)
7210   "Force re-fetching of the current article.
7211 If ARG (the prefix) is a number, show the article with the charset
7212 defined in `gnus-summary-show-article-charset-alist', or the charset
7213 inputed.
7214 If ARG (the prefix) is non-nil and not a number, show the raw article
7215 without any article massaging functions being run."
7216   (interactive "P")
7217   (cond
7218    ((numberp arg)
7219     (let ((gnus-newsgroup-charset
7220            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7221                (read-coding-system "Charset: ")))
7222           (gnus-newsgroup-ignored-charsets 'gnus-all))
7223       (gnus-summary-select-article nil 'force)))
7224    ((not arg)
7225     ;; Select the article the normal way.
7226     (gnus-summary-select-article nil 'force))
7227    (t
7228     ;; We have to require this here to make sure that the following
7229     ;; dynamic binding isn't shadowed by autoloading.
7230     (require 'gnus-async)
7231     (require 'gnus-art)
7232     ;; Bind the article treatment functions to nil.
7233     (let ((gnus-have-all-headers t)
7234           gnus-article-prepare-hook
7235           gnus-article-decode-hook
7236           gnus-display-mime-function
7237           gnus-break-pages)
7238       ;; Destroy any MIME parts.
7239       (when (gnus-buffer-live-p gnus-article-buffer)
7240         (save-excursion
7241           (set-buffer gnus-article-buffer)
7242           (mm-destroy-parts gnus-article-mime-handles)))
7243       (gnus-summary-select-article nil 'force))))
7244   (gnus-summary-goto-subject gnus-current-article)
7245   (gnus-summary-position-point))
7246
7247 (defun gnus-summary-verbose-headers (&optional arg)
7248   "Toggle permanent full header display.
7249 If ARG is a positive number, turn header display on.
7250 If ARG is a negative number, turn header display off."
7251   (interactive "P")
7252   (setq gnus-show-all-headers
7253         (cond ((or (not (numberp arg))
7254                    (zerop arg))
7255                (not gnus-show-all-headers))
7256               ((natnump arg)
7257                t)))
7258   (gnus-summary-show-article))
7259
7260 (defun gnus-summary-toggle-header (&optional arg)
7261   "Show the headers if they are hidden, or hide them if they are shown.
7262 If ARG is a positive number, show the entire header.
7263 If ARG is a negative number, hide the unwanted header lines."
7264   (interactive "P")
7265   (save-excursion
7266     (set-buffer gnus-article-buffer)
7267     (save-restriction
7268       (let* ((buffer-read-only nil)
7269              (inhibit-point-motion-hooks t)
7270              hidden e)
7271         (setq hidden
7272               (if (numberp arg)
7273                   (>= arg 0)
7274                 (save-restriction
7275                   (article-narrow-to-head)
7276                   (gnus-article-hidden-text-p 'headers))))
7277         (goto-char (point-min))
7278         (when (search-forward "\n\n" nil t)
7279           (delete-region (point-min) (1- (point))))
7280         (goto-char (point-min))
7281         (save-excursion
7282           (set-buffer gnus-original-article-buffer)
7283           (goto-char (point-min))
7284           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7285         (insert-buffer-substring gnus-original-article-buffer 1 e)
7286         (save-restriction
7287           (narrow-to-region (point-min) (point))
7288           (article-decode-encoded-words)
7289           (if  hidden
7290               (let ((gnus-treat-hide-headers nil)
7291                     (gnus-treat-hide-boring-headers nil))
7292                 (setq gnus-article-wash-types
7293                       (delq 'headers gnus-article-wash-types))
7294                 (gnus-treat-article 'head))
7295             (gnus-treat-article 'head)))
7296         (gnus-set-mode-line 'article)))))
7297
7298 (defun gnus-summary-show-all-headers ()
7299   "Make all header lines visible."
7300   (interactive)
7301   (gnus-article-show-all-headers))
7302
7303 (defun gnus-summary-caesar-message (&optional arg)
7304   "Caesar rotate the current article by 13.
7305 The numerical prefix specifies how many places to rotate each letter
7306 forward."
7307   (interactive "P")
7308   (gnus-summary-select-article)
7309   (let ((mail-header-separator ""))
7310     (gnus-eval-in-buffer-window gnus-article-buffer
7311       (save-restriction
7312         (widen)
7313         (let ((start (window-start))
7314               buffer-read-only)
7315           (message-caesar-buffer-body arg)
7316           (set-window-start (get-buffer-window (current-buffer)) start))))))
7317
7318 (defun gnus-summary-stop-page-breaking ()
7319   "Stop page breaking in the current article."
7320   (interactive)
7321   (gnus-summary-select-article)
7322   (gnus-eval-in-buffer-window gnus-article-buffer
7323     (widen)
7324     (when (gnus-visual-p 'page-marker)
7325       (let ((buffer-read-only nil))
7326         (gnus-remove-text-with-property 'gnus-prev)
7327         (gnus-remove-text-with-property 'gnus-next))
7328       (setq gnus-page-broken nil))))
7329
7330 (defun gnus-summary-move-article (&optional n to-newsgroup
7331                                             select-method action)
7332   "Move the current article to a different newsgroup.
7333 If N is a positive number, move the N next articles.
7334 If N is a negative number, move the N previous articles.
7335 If N is nil and any articles have been marked with the process mark,
7336 move those articles instead.
7337 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7338 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7339 re-spool using this method.
7340
7341 For this function to work, both the current newsgroup and the
7342 newsgroup that you want to move to have to support the `request-move'
7343 and `request-accept' functions.
7344
7345 ACTION can be either `move' (the default), `crosspost' or `copy'."
7346   (interactive "P")
7347   (unless action
7348     (setq action 'move))
7349   ;; Disable marking as read.
7350   (let (gnus-mark-article-hook)
7351     (save-window-excursion
7352       (gnus-summary-select-article)))
7353   ;; Check whether the source group supports the required functions.
7354   (cond ((and (eq action 'move)
7355               (not (gnus-check-backend-function
7356                     'request-move-article gnus-newsgroup-name)))
7357          (error "The current group does not support article moving"))
7358         ((and (eq action 'crosspost)
7359               (not (gnus-check-backend-function
7360                     'request-replace-article gnus-newsgroup-name)))
7361          (error "The current group does not support article editing")))
7362   (let ((articles (gnus-summary-work-articles n))
7363         (prefix (if (gnus-check-backend-function
7364                     'request-move-article gnus-newsgroup-name)
7365                     (gnus-group-real-prefix gnus-newsgroup-name)
7366                   ""))
7367         (names '((move "Move" "Moving")
7368                  (copy "Copy" "Copying")
7369                  (crosspost "Crosspost" "Crossposting")))
7370         (copy-buf (save-excursion
7371                     (nnheader-set-temp-buffer " *copy article*")))
7372         art-group to-method new-xref article to-groups)
7373     (unless (assq action names)
7374       (error "Unknown action %s" action))
7375     ;; Read the newsgroup name.
7376     (when (and (not to-newsgroup)
7377                (not select-method))
7378       (setq to-newsgroup
7379             (gnus-read-move-group-name
7380              (cadr (assq action names))
7381              (symbol-value (intern (format "gnus-current-%s-group" action)))
7382              articles prefix))
7383       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7384     (setq to-method (or select-method
7385                         (gnus-server-to-method
7386                          (gnus-group-method to-newsgroup))))
7387     ;; Check the method we are to move this article to...
7388     (unless (gnus-check-backend-function
7389              'request-accept-article (car to-method))
7390       (error "%s does not support article copying" (car to-method)))
7391     (unless (gnus-check-server to-method)
7392       (error "Can't open server %s" (car to-method)))
7393     (gnus-message 6 "%s to %s: %s..."
7394                   (caddr (assq action names))
7395                   (or (car select-method) to-newsgroup) articles)
7396     (while articles
7397       (setq article (pop articles))
7398       (setq
7399        art-group
7400        (cond
7401         ;; Move the article.
7402         ((eq action 'move)
7403          ;; Remove this article from future suppression.
7404          (gnus-dup-unsuppress-article article)
7405          (gnus-request-move-article
7406           article                       ; Article to move
7407           gnus-newsgroup-name           ; From newsgroup
7408           (nth 1 (gnus-find-method-for-group
7409                   gnus-newsgroup-name)) ; Server
7410           (list 'gnus-request-accept-article
7411                 to-newsgroup (list 'quote select-method)
7412                 (not articles) t)       ; Accept form
7413           (not articles)))              ; Only save nov last time
7414         ;; Copy the article.
7415         ((eq action 'copy)
7416          (save-excursion
7417            (set-buffer copy-buf)
7418            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7419              (gnus-request-accept-article
7420               to-newsgroup select-method (not articles) t))))
7421         ;; Crosspost the article.
7422         ((eq action 'crosspost)
7423          (let ((xref (message-tokenize-header
7424                       (mail-header-xref (gnus-summary-article-header article))
7425                       " ")))
7426            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7427                                   ":" article))
7428            (unless xref
7429              (setq xref (list (system-name))))
7430            (setq new-xref
7431                  (concat
7432                   (mapconcat 'identity
7433                              (delete "Xref:" (delete new-xref xref))
7434                              " ")
7435                   " " new-xref))
7436            (save-excursion
7437              (set-buffer copy-buf)
7438              ;; First put the article in the destination group.
7439              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7440              (when (consp (setq art-group
7441                                 (gnus-request-accept-article
7442                                  to-newsgroup select-method (not articles))))
7443                (setq new-xref (concat new-xref " " (car art-group)
7444                                       ":" (cdr art-group)))
7445                ;; Now we have the new Xrefs header, so we insert
7446                ;; it and replace the new article.
7447                (nnheader-replace-header "Xref" new-xref)
7448                (gnus-request-replace-article
7449                 (cdr art-group) to-newsgroup (current-buffer))
7450                art-group))))))
7451       (cond
7452        ((not art-group)
7453         (gnus-message 1 "Couldn't %s article %s: %s"
7454                       (cadr (assq action names)) article
7455                       (nnheader-get-report (car to-method))))
7456        ((eq art-group 'junk)
7457         (when (eq action 'move)
7458           (gnus-summary-mark-article article gnus-canceled-mark)
7459           (gnus-message 4 "Deleted article %s" article)))
7460        (t
7461         (let* ((pto-group (gnus-group-prefixed-name
7462                            (car art-group) to-method))
7463                (entry
7464                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7465                (info (nth 2 entry))
7466                (to-group (gnus-info-group info))
7467                to-marks)
7468           ;; Update the group that has been moved to.
7469           (when (and info
7470                      (memq action '(move copy)))
7471             (unless (member to-group to-groups)
7472               (push to-group to-groups))
7473
7474             (unless (memq article gnus-newsgroup-unreads)
7475               (push 'read to-marks)
7476               (gnus-info-set-read
7477                info (gnus-add-to-range (gnus-info-read info)
7478                                        (list (cdr art-group)))))
7479
7480             ;; See whether the article is to be put in the cache.
7481             (let ((marks gnus-article-mark-lists)
7482                   (to-article (cdr art-group)))
7483
7484               ;; Enter the article into the cache in the new group,
7485               ;; if that is required.
7486               (when gnus-use-cache
7487                 (gnus-cache-possibly-enter-article
7488                  to-group to-article
7489                  (memq article gnus-newsgroup-marked)
7490                  (memq article gnus-newsgroup-dormant)
7491                  (memq article gnus-newsgroup-unreads)))
7492
7493               (when gnus-preserve-marks
7494                 ;; Copy any marks over to the new group.
7495                 (when (and (equal to-group gnus-newsgroup-name)
7496                            (not (memq article gnus-newsgroup-unreads)))
7497                   ;; Mark this article as read in this group.
7498                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7499                   (setcdr (gnus-active to-group) to-article)
7500                   (setcdr gnus-newsgroup-active to-article))
7501
7502                 (while marks
7503                   (when (memq article (symbol-value
7504                                        (intern (format "gnus-newsgroup-%s"
7505                                                        (caar marks)))))
7506                     (push (cdar marks) to-marks)
7507                     ;; If the other group is the same as this group,
7508                     ;; then we have to add the mark to the list.
7509                     (when (equal to-group gnus-newsgroup-name)
7510                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7511                            (cons to-article
7512                                  (symbol-value
7513                                   (intern (format "gnus-newsgroup-%s"
7514                                                   (caar marks)))))))
7515                     ;; Copy the marks to other group.
7516                     (gnus-add-marked-articles
7517                      to-group (cdar marks) (list to-article) info))
7518                   (setq marks (cdr marks)))
7519
7520                 (gnus-request-set-mark to-group (list (list (list to-article)
7521                                                             'set
7522                                                             to-marks))))
7523
7524               (gnus-dribble-enter
7525                (concat "(gnus-group-set-info '"
7526                        (gnus-prin1-to-string (gnus-get-info to-group))
7527                        ")"))))
7528
7529           ;; Update the Xref header in this article to point to
7530           ;; the new crossposted article we have just created.
7531           (when (eq action 'crosspost)
7532             (save-excursion
7533               (set-buffer copy-buf)
7534               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7535               (nnheader-replace-header "Xref" new-xref)
7536               (gnus-request-replace-article
7537                article gnus-newsgroup-name (current-buffer)))))
7538
7539         ;;;!!!Why is this necessary?
7540         (set-buffer gnus-summary-buffer)
7541
7542         (gnus-summary-goto-subject article)
7543         (when (eq action 'move)
7544           (gnus-summary-mark-article article gnus-canceled-mark))))
7545       (gnus-summary-remove-process-mark article))
7546     ;; Re-activate all groups that have been moved to.
7547     (while to-groups
7548       (save-excursion
7549         (set-buffer gnus-group-buffer)
7550         (when (gnus-group-goto-group (car to-groups) t)
7551           (gnus-group-get-new-news-this-group 1 t))
7552         (pop to-groups)))
7553
7554     (gnus-kill-buffer copy-buf)
7555     (gnus-summary-position-point)
7556     (gnus-set-mode-line 'summary)))
7557
7558 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7559   "Move the current article to a different newsgroup.
7560 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7561 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7562 re-spool using this method."
7563   (interactive "P")
7564   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7565
7566 (defun gnus-summary-crosspost-article (&optional n)
7567   "Crosspost the current article to some other group."
7568   (interactive "P")
7569   (gnus-summary-move-article n nil nil 'crosspost))
7570
7571 (defcustom gnus-summary-respool-default-method nil
7572   "Default method for respooling an article.
7573 If nil, use to the current newsgroup method."
7574   :type '(choice (gnus-select-method :value (nnml ""))
7575                  (const nil))
7576   :group 'gnus-summary-mail)
7577
7578 (defun gnus-summary-respool-article (&optional n method)
7579   "Respool the current article.
7580 The article will be squeezed through the mail spooling process again,
7581 which means that it will be put in some mail newsgroup or other
7582 depending on `nnmail-split-methods'.
7583 If N is a positive number, respool the N next articles.
7584 If N is a negative number, respool the N previous articles.
7585 If N is nil and any articles have been marked with the process mark,
7586 respool those articles instead.
7587
7588 Respooling can be done both from mail groups and \"real\" newsgroups.
7589 In the former case, the articles in question will be moved from the
7590 current group into whatever groups they are destined to.  In the
7591 latter case, they will be copied into the relevant groups."
7592   (interactive
7593    (list current-prefix-arg
7594          (let* ((methods (gnus-methods-using 'respool))
7595                 (methname
7596                  (symbol-name (or gnus-summary-respool-default-method
7597                                   (car (gnus-find-method-for-group
7598                                         gnus-newsgroup-name)))))
7599                 (method
7600                  (gnus-completing-read
7601                   methname "What backend do you want to use when respooling?"
7602                   methods nil t nil 'gnus-mail-method-history))
7603                 ms)
7604            (cond
7605             ((zerop (length (setq ms (gnus-servers-using-backend
7606                                       (intern method)))))
7607              (list (intern method) ""))
7608             ((= 1 (length ms))
7609              (car ms))
7610             (t
7611              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7612                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7613                            ms-alist))))))))
7614   (unless method
7615     (error "No method given for respooling"))
7616   (if (assoc (symbol-name
7617               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7618              (gnus-methods-using 'respool))
7619       (gnus-summary-move-article n nil method)
7620     (gnus-summary-copy-article n nil method)))
7621
7622 (defun gnus-summary-import-article (file)
7623   "Import an arbitrary file into a mail newsgroup."
7624   (interactive "fImport file: ")
7625   (let ((group gnus-newsgroup-name)
7626         (now (current-time))
7627         atts lines)
7628     (unless (gnus-check-backend-function 'request-accept-article group)
7629       (error "%s does not support article importing" group))
7630     (or (file-readable-p file)
7631         (not (file-regular-p file))
7632         (error "Can't read %s" file))
7633     (save-excursion
7634       (set-buffer (gnus-get-buffer-create " *import file*"))
7635       (erase-buffer)
7636       (nnheader-insert-file-contents file)
7637       (goto-char (point-min))
7638       (unless (nnheader-article-p)
7639         ;; This doesn't look like an article, so we fudge some headers.
7640         (setq atts (file-attributes file)
7641               lines (count-lines (point-min) (point-max)))
7642         (insert "From: " (read-string "From: ") "\n"
7643                 "Subject: " (read-string "Subject: ") "\n"
7644                 "Date: " (message-make-date (nth 5 atts))
7645                 "\n"
7646                 "Message-ID: " (message-make-message-id) "\n"
7647                 "Lines: " (int-to-string lines) "\n"
7648                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7649       (gnus-request-accept-article group nil t)
7650       (kill-buffer (current-buffer)))))
7651
7652 (defun gnus-summary-article-posted-p ()
7653   "Say whether the current (mail) article is available from news as well.
7654 This will be the case if the article has both been mailed and posted."
7655   (interactive)
7656   (let ((id (mail-header-references (gnus-summary-article-header)))
7657         (gnus-override-method (car (gnus-refer-article-methods))))
7658     (if (gnus-request-head id "")
7659         (gnus-message 2 "The current message was found on %s"
7660                       gnus-override-method)
7661       (gnus-message 2 "The current message couldn't be found on %s"
7662                     gnus-override-method)
7663       nil)))
7664
7665 (defun gnus-summary-expire-articles (&optional now)
7666   "Expire all articles that are marked as expirable in the current group."
7667   (interactive)
7668   (when (gnus-check-backend-function
7669          'request-expire-articles gnus-newsgroup-name)
7670     ;; This backend supports expiry.
7671     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7672            (expirable (if total
7673                           (progn
7674                             ;; We need to update the info for
7675                             ;; this group for `gnus-list-of-read-articles'
7676                             ;; to give us the right answer.
7677                             (gnus-run-hooks 'gnus-exit-group-hook)
7678                             (gnus-summary-update-info)
7679                             (gnus-list-of-read-articles gnus-newsgroup-name))
7680                         (setq gnus-newsgroup-expirable
7681                               (sort gnus-newsgroup-expirable '<))))
7682            (expiry-wait (if now 'immediate
7683                           (gnus-group-find-parameter
7684                            gnus-newsgroup-name 'expiry-wait)))
7685            es)
7686       (when expirable
7687         ;; There are expirable articles in this group, so we run them
7688         ;; through the expiry process.
7689         (gnus-message 6 "Expiring articles...")
7690         (unless (gnus-check-group gnus-newsgroup-name)
7691           (error "Can't open server for %s" gnus-newsgroup-name))
7692         ;; The list of articles that weren't expired is returned.
7693         (save-excursion
7694           (if expiry-wait
7695               (let ((nnmail-expiry-wait-function nil)
7696                     (nnmail-expiry-wait expiry-wait))
7697                 (setq es (gnus-request-expire-articles
7698                           expirable gnus-newsgroup-name)))
7699             (setq es (gnus-request-expire-articles
7700                       expirable gnus-newsgroup-name))))
7701         (unless total
7702           (setq gnus-newsgroup-expirable es))
7703         ;; We go through the old list of expirable, and mark all
7704         ;; really expired articles as nonexistent.
7705         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7706           (let ((gnus-use-cache nil))
7707             (while expirable
7708               (unless (memq (car expirable) es)
7709                 (when (gnus-data-find (car expirable))
7710                   (gnus-summary-mark-article
7711                    (car expirable) gnus-canceled-mark)))
7712               (setq expirable (cdr expirable)))))
7713         (gnus-message 6 "Expiring articles...done")))))
7714
7715 (defun gnus-summary-expire-articles-now ()
7716   "Expunge all expirable articles in the current group.
7717 This means that *all* articles that are marked as expirable will be
7718 deleted forever, right now."
7719   (interactive)
7720   (or gnus-expert-user
7721       (gnus-yes-or-no-p
7722        "Are you really, really, really sure you want to delete all these messages? ")
7723       (error "Phew!"))
7724   (gnus-summary-expire-articles t))
7725
7726 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7727 (defun gnus-summary-delete-article (&optional n)
7728   "Delete the N next (mail) articles.
7729 This command actually deletes articles.  This is not a marking
7730 command.  The article will disappear forever from your life, never to
7731 return.
7732 If N is negative, delete backwards.
7733 If N is nil and articles have been marked with the process mark,
7734 delete these instead."
7735   (interactive "P")
7736   (unless (gnus-check-backend-function 'request-expire-articles
7737                                        gnus-newsgroup-name)
7738     (error "The current newsgroup does not support article deletion"))
7739   ;; Compute the list of articles to delete.
7740   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7741         not-deleted)
7742     (if (and gnus-novice-user
7743              (not (gnus-yes-or-no-p
7744                    (format "Do you really want to delete %s forever? "
7745                            (if (> (length articles) 1)
7746                                (format "these %s articles" (length articles))
7747                              "this article")))))
7748         ()
7749       ;; Delete the articles.
7750       (setq not-deleted (gnus-request-expire-articles
7751                          articles gnus-newsgroup-name 'force))
7752       (while articles
7753         (gnus-summary-remove-process-mark (car articles))
7754         ;; The backend might not have been able to delete the article
7755         ;; after all.
7756         (unless (memq (car articles) not-deleted)
7757           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7758         (setq articles (cdr articles)))
7759       (when not-deleted
7760         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7761     (gnus-summary-position-point)
7762     (gnus-set-mode-line 'summary)
7763     not-deleted))
7764
7765 (defun gnus-summary-edit-article (&optional arg)
7766   "Edit the current article.
7767 This will have permanent effect only in mail groups.
7768 If ARG is nil, edit the decoded articles.
7769 If ARG is 1, edit the raw articles. 
7770 If ARG is 2, edit the raw articles even in read-only groups.
7771 Otherwise, allow editing of articles even in read-only
7772 groups."
7773   (interactive "P")
7774   (let (force raw)
7775     (cond 
7776      ((null arg))
7777      ((eq arg 1) (setq raw t))
7778      ((eq arg 2) (setq raw t
7779                        force t))
7780      (t (setq force t)))
7781     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
7782         (error "Can't edit the raw article in group nndraft:drafts."))
7783     (save-excursion
7784       (set-buffer gnus-summary-buffer)
7785       (let ((mail-parse-charset gnus-newsgroup-charset)
7786             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7787         (gnus-set-global-variables)
7788         (when (and (not force)
7789                    (gnus-group-read-only-p))
7790           (error "The current newsgroup does not support article editing"))
7791         (gnus-summary-show-article t)
7792         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
7793           (with-current-buffer gnus-article-buffer
7794             (mm-enable-multibyte)))
7795         (if (equal gnus-newsgroup-name "nndraft:drafts")
7796             (setq raw t))
7797         (gnus-article-edit-article
7798          (if raw 'ignore 
7799            #'(lambda () 
7800                (let ((mbl mml-buffer-list))
7801                  (setq mml-buffer-list nil)
7802                  (mime-to-mml)
7803                  (make-local-hook 'kill-buffer-hook)
7804                  (let ((mml-buffer-list mml-buffer-list))
7805                    (setq mml-buffer-list mbl)
7806                    (make-local-variable 'mml-buffer-list))
7807                  (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
7808          `(lambda (no-highlight)
7809             (let ((mail-parse-charset ',gnus-newsgroup-charset)
7810                   (mail-parse-ignored-charsets 
7811                    ',gnus-newsgroup-ignored-charsets))
7812               ,(if (not raw) '(progn 
7813                                 (mml-to-mime)
7814                                 (mml-destroy-buffers)
7815                                 (remove-hook 'kill-buffer-hook 
7816                                              'mml-destroy-buffers t)
7817                                 (kill-local-variable 'mml-buffer-list)))
7818               (gnus-summary-edit-article-done
7819                ,(or (mail-header-references gnus-current-headers) "")
7820                ,(gnus-group-read-only-p) 
7821                ,gnus-summary-buffer no-highlight))))))))
7822
7823 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7824
7825 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7826                                                  no-highlight)
7827   "Make edits to the current article permanent."
7828   (interactive)
7829   ;; Replace the article.
7830   (let ((buf (current-buffer)))
7831     (with-temp-buffer
7832       (insert-buffer-substring buf)
7833       (if (and (not read-only)
7834                (not (gnus-request-replace-article
7835                      (cdr gnus-article-current) (car gnus-article-current)
7836                      (current-buffer) t)))
7837           (error "Couldn't replace article")
7838         ;; Update the summary buffer.
7839         (if (and references
7840                  (equal (message-tokenize-header references " ")
7841                         (message-tokenize-header
7842                          (or (message-fetch-field "references") "") " ")))
7843             ;; We only have to update this line.
7844             (save-excursion
7845               (save-restriction
7846                 (message-narrow-to-head)
7847                 (let ((head (buffer-string))
7848                       header)
7849                   (with-temp-buffer
7850                     (insert (format "211 %d Article retrieved.\n"
7851                                     (cdr gnus-article-current)))
7852                     (insert head)
7853                     (insert ".\n")
7854                     (let ((nntp-server-buffer (current-buffer)))
7855                       (setq header (car (gnus-get-newsgroup-headers
7856                                          (save-excursion
7857                                            (set-buffer gnus-summary-buffer)
7858                                            gnus-newsgroup-dependencies)
7859                                          t))))
7860                     (save-excursion
7861                       (set-buffer gnus-summary-buffer)
7862                       (gnus-data-set-header
7863                        (gnus-data-find (cdr gnus-article-current))
7864                        header)
7865                       (gnus-summary-update-article-line
7866                        (cdr gnus-article-current) header))))))
7867           ;; Update threads.
7868           (set-buffer (or buffer gnus-summary-buffer))
7869           (gnus-summary-update-article (cdr gnus-article-current)))
7870         ;; Prettify the article buffer again.
7871         (unless no-highlight
7872           (save-excursion
7873             (set-buffer gnus-article-buffer)
7874             ;;;!!! Fix this -- article should be rehighlighted.
7875             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7876             (set-buffer gnus-original-article-buffer)
7877             (gnus-request-article
7878              (cdr gnus-article-current)
7879              (car gnus-article-current) (current-buffer))))
7880         ;; Prettify the summary buffer line.
7881         (when (gnus-visual-p 'summary-highlight 'highlight)
7882           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7883
7884 (defun gnus-summary-edit-wash (key)
7885   "Perform editing command KEY in the article buffer."
7886   (interactive
7887    (list
7888     (progn
7889       (message "%s" (concat (this-command-keys) "- "))
7890       (read-char))))
7891   (message "")
7892   (gnus-summary-edit-article)
7893   (execute-kbd-macro (concat (this-command-keys) key))
7894   (gnus-article-edit-done))
7895
7896 ;;; Respooling
7897
7898 (defun gnus-summary-respool-query (&optional silent trace)
7899   "Query where the respool algorithm would put this article."
7900   (interactive)
7901   (let (gnus-mark-article-hook)
7902     (gnus-summary-select-article)
7903     (save-excursion
7904       (set-buffer gnus-original-article-buffer)
7905       (save-restriction
7906         (message-narrow-to-head)
7907         (let ((groups (nnmail-article-group 'identity trace)))
7908           (unless silent
7909             (if groups
7910                 (message "This message would go to %s"
7911                          (mapconcat 'car groups ", "))
7912               (message "This message would go to no groups"))
7913             groups))))))
7914
7915 (defun gnus-summary-respool-trace ()
7916   "Trace where the respool algorithm would put this article.
7917 Display a buffer showing all fancy splitting patterns which matched."
7918   (interactive)
7919   (gnus-summary-respool-query nil t))
7920
7921 ;; Summary marking commands.
7922
7923 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7924   "Mark articles which has the same subject as read, and then select the next.
7925 If UNMARK is positive, remove any kind of mark.
7926 If UNMARK is negative, tick articles."
7927   (interactive "P")
7928   (when unmark
7929     (setq unmark (prefix-numeric-value unmark)))
7930   (let ((count
7931          (gnus-summary-mark-same-subject
7932           (gnus-summary-article-subject) unmark)))
7933     ;; Select next unread article.  If auto-select-same mode, should
7934     ;; select the first unread article.
7935     (gnus-summary-next-article t (and gnus-auto-select-same
7936                                       (gnus-summary-article-subject)))
7937     (gnus-message 7 "%d article%s marked as %s"
7938                   count (if (= count 1) " is" "s are")
7939                   (if unmark "unread" "read"))))
7940
7941 (defun gnus-summary-kill-same-subject (&optional unmark)
7942   "Mark articles which has the same subject as read.
7943 If UNMARK is positive, remove any kind of mark.
7944 If UNMARK is negative, tick articles."
7945   (interactive "P")
7946   (when unmark
7947     (setq unmark (prefix-numeric-value unmark)))
7948   (let ((count
7949          (gnus-summary-mark-same-subject
7950           (gnus-summary-article-subject) unmark)))
7951     ;; If marked as read, go to next unread subject.
7952     (when (null unmark)
7953       ;; Go to next unread subject.
7954       (gnus-summary-next-subject 1 t))
7955     (gnus-message 7 "%d articles are marked as %s"
7956                   count (if unmark "unread" "read"))))
7957
7958 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7959   "Mark articles with same SUBJECT as read, and return marked number.
7960 If optional argument UNMARK is positive, remove any kinds of marks.
7961 If optional argument UNMARK is negative, mark articles as unread instead."
7962   (let ((count 1))
7963     (save-excursion
7964       (cond
7965        ((null unmark)                   ; Mark as read.
7966         (while (and
7967                 (progn
7968                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7969                   (gnus-summary-show-thread) t)
7970                 (gnus-summary-find-subject subject))
7971           (setq count (1+ count))))
7972        ((> unmark 0)                    ; Tick.
7973         (while (and
7974                 (progn
7975                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7976                   (gnus-summary-show-thread) t)
7977                 (gnus-summary-find-subject subject))
7978           (setq count (1+ count))))
7979        (t                               ; Mark as unread.
7980         (while (and
7981                 (progn
7982                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7983                   (gnus-summary-show-thread) t)
7984                 (gnus-summary-find-subject subject))
7985           (setq count (1+ count)))))
7986       (gnus-set-mode-line 'summary)
7987       ;; Return the number of marked articles.
7988       count)))
7989
7990 (defun gnus-summary-mark-as-processable (n &optional unmark)
7991   "Set the process mark on the next N articles.
7992 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7993 the process mark instead.  The difference between N and the actual
7994 number of articles marked is returned."
7995   (interactive "p")
7996   (let ((backward (< n 0))
7997         (n (abs n)))
7998     (while (and
7999             (> n 0)
8000             (if unmark
8001                 (gnus-summary-remove-process-mark
8002                  (gnus-summary-article-number))
8003               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8004             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8005       (setq n (1- n)))
8006     (when (/= 0 n)
8007       (gnus-message 7 "No more articles"))
8008     (gnus-summary-recenter)
8009     (gnus-summary-position-point)
8010     n))
8011
8012 (defun gnus-summary-unmark-as-processable (n)
8013   "Remove the process mark from the next N articles.
8014 If N is negative, unmark backward instead.  The difference between N and
8015 the actual number of articles unmarked is returned."
8016   (interactive "p")
8017   (gnus-summary-mark-as-processable n t))
8018
8019 (defun gnus-summary-unmark-all-processable ()
8020   "Remove the process mark from all articles."
8021   (interactive)
8022   (save-excursion
8023     (while gnus-newsgroup-processable
8024       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8025   (gnus-summary-position-point))
8026
8027 (defun gnus-summary-mark-as-expirable (n)
8028   "Mark N articles forward as expirable.
8029 If N is negative, mark backward instead.  The difference between N and
8030 the actual number of articles marked is returned."
8031   (interactive "p")
8032   (gnus-summary-mark-forward n gnus-expirable-mark))
8033
8034 (defun gnus-summary-mark-article-as-replied (article)
8035   "Mark ARTICLE replied and update the summary line."
8036   (push article gnus-newsgroup-replied)
8037   (let ((buffer-read-only nil))
8038     (when (gnus-summary-goto-subject article nil t)
8039       (gnus-summary-update-secondary-mark article))))
8040
8041 (defun gnus-summary-set-bookmark (article)
8042   "Set a bookmark in current article."
8043   (interactive (list (gnus-summary-article-number)))
8044   (when (or (not (get-buffer gnus-article-buffer))
8045             (not gnus-current-article)
8046             (not gnus-article-current)
8047             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8048     (error "No current article selected"))
8049   ;; Remove old bookmark, if one exists.
8050   (let ((old (assq article gnus-newsgroup-bookmarks)))
8051     (when old
8052       (setq gnus-newsgroup-bookmarks
8053             (delq old gnus-newsgroup-bookmarks))))
8054   ;; Set the new bookmark, which is on the form
8055   ;; (article-number . line-number-in-body).
8056   (push
8057    (cons article
8058          (save-excursion
8059            (set-buffer gnus-article-buffer)
8060            (count-lines
8061             (min (point)
8062                  (save-excursion
8063                    (goto-char (point-min))
8064                    (search-forward "\n\n" nil t)
8065                    (point)))
8066             (point))))
8067    gnus-newsgroup-bookmarks)
8068   (gnus-message 6 "A bookmark has been added to the current article."))
8069
8070 (defun gnus-summary-remove-bookmark (article)
8071   "Remove the bookmark from the current article."
8072   (interactive (list (gnus-summary-article-number)))
8073   ;; Remove old bookmark, if one exists.
8074   (let ((old (assq article gnus-newsgroup-bookmarks)))
8075     (if old
8076         (progn
8077           (setq gnus-newsgroup-bookmarks
8078                 (delq old gnus-newsgroup-bookmarks))
8079           (gnus-message 6 "Removed bookmark."))
8080       (gnus-message 6 "No bookmark in current article."))))
8081
8082 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8083 (defun gnus-summary-mark-as-dormant (n)
8084   "Mark N articles forward as dormant.
8085 If N is negative, mark backward instead.  The difference between N and
8086 the actual number of articles marked is returned."
8087   (interactive "p")
8088   (gnus-summary-mark-forward n gnus-dormant-mark))
8089
8090 (defun gnus-summary-set-process-mark (article)
8091   "Set the process mark on ARTICLE and update the summary line."
8092   (setq gnus-newsgroup-processable
8093         (cons article
8094               (delq article gnus-newsgroup-processable)))
8095   (when (gnus-summary-goto-subject article)
8096     (gnus-summary-show-thread)
8097     (gnus-summary-goto-subject article)
8098     (gnus-summary-update-secondary-mark article)))
8099
8100 (defun gnus-summary-remove-process-mark (article)
8101   "Remove the process mark from ARTICLE and update the summary line."
8102   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8103   (when (gnus-summary-goto-subject article)
8104     (gnus-summary-show-thread)
8105     (gnus-summary-goto-subject article)
8106     (gnus-summary-update-secondary-mark article)))
8107
8108 (defun gnus-summary-set-saved-mark (article)
8109   "Set the process mark on ARTICLE and update the summary line."
8110   (push article gnus-newsgroup-saved)
8111   (when (gnus-summary-goto-subject article)
8112     (gnus-summary-update-secondary-mark article)))
8113
8114 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8115   "Mark N articles as read forwards.
8116 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8117 The difference between N and the actual number of articles marked is
8118 returned.
8119 Iff NO-EXPIRE, auto-expiry will be inhibited."
8120   (interactive "p")
8121   (gnus-summary-show-thread)
8122   (let ((backward (< n 0))
8123         (gnus-summary-goto-unread
8124          (and gnus-summary-goto-unread
8125               (not (eq gnus-summary-goto-unread 'never))
8126               (not (memq mark (list gnus-unread-mark
8127                                     gnus-ticked-mark gnus-dormant-mark)))))
8128         (n (abs n))
8129         (mark (or mark gnus-del-mark)))
8130     (while (and (> n 0)
8131                 (gnus-summary-mark-article nil mark no-expire)
8132                 (zerop (gnus-summary-next-subject
8133                         (if backward -1 1)
8134                         (and gnus-summary-goto-unread
8135                              (not (eq gnus-summary-goto-unread 'never)))
8136                         t)))
8137       (setq n (1- n)))
8138     (when (/= 0 n)
8139       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8140     (gnus-summary-recenter)
8141     (gnus-summary-position-point)
8142     (gnus-set-mode-line 'summary)
8143     n))
8144
8145 (defun gnus-summary-mark-article-as-read (mark)
8146   "Mark the current article quickly as read with MARK."
8147   (let ((article (gnus-summary-article-number)))
8148     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8149     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8150     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8151     (push (cons article mark) gnus-newsgroup-reads)
8152     ;; Possibly remove from cache, if that is used.
8153     (when gnus-use-cache
8154       (gnus-cache-enter-remove-article article))
8155     ;; Allow the backend to change the mark.
8156     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8157     ;; Check for auto-expiry.
8158     (when (and gnus-newsgroup-auto-expire
8159                (memq mark gnus-auto-expirable-marks))
8160       (setq mark gnus-expirable-mark)
8161       ;; Let the backend know about the mark change.
8162       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8163       (push article gnus-newsgroup-expirable))
8164     ;; Set the mark in the buffer.
8165     (gnus-summary-update-mark mark 'unread)
8166     t))
8167
8168 (defun gnus-summary-mark-article-as-unread (mark)
8169   "Mark the current article quickly as unread with MARK."
8170   (let* ((article (gnus-summary-article-number))
8171          (old-mark (gnus-summary-article-mark article)))
8172     ;; Allow the backend to change the mark.
8173     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8174     (if (eq mark old-mark)
8175         t
8176       (if (<= article 0)
8177           (progn
8178             (gnus-error 1 "Can't mark negative article numbers")
8179             nil)
8180         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8181         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8182         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8183         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8184         (cond ((= mark gnus-ticked-mark)
8185                (push article gnus-newsgroup-marked))
8186               ((= mark gnus-dormant-mark)
8187                (push article gnus-newsgroup-dormant))
8188               (t
8189                (push article gnus-newsgroup-unreads)))
8190         (gnus-pull article gnus-newsgroup-reads)
8191
8192         ;; See whether the article is to be put in the cache.
8193         (and gnus-use-cache
8194              (vectorp (gnus-summary-article-header article))
8195              (save-excursion
8196                (gnus-cache-possibly-enter-article
8197                 gnus-newsgroup-name article
8198                 (= mark gnus-ticked-mark)
8199                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8200
8201         ;; Fix the mark.
8202         (gnus-summary-update-mark mark 'unread)
8203         t))))
8204
8205 (defun gnus-summary-mark-article (&optional article mark no-expire)
8206   "Mark ARTICLE with MARK.  MARK can be any character.
8207 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8208 `??' (dormant) and `?E' (expirable).
8209 If MARK is nil, then the default character `?r' is used.
8210 If ARTICLE is nil, then the article on the current line will be
8211 marked.
8212 Iff NO-EXPIRE, auto-expiry will be inhibited."
8213   ;; The mark might be a string.
8214   (when (stringp mark)
8215     (setq mark (aref mark 0)))
8216   ;; If no mark is given, then we check auto-expiring.
8217   (when (null mark)
8218     (setq mark gnus-del-mark))
8219   (when (and (not no-expire)
8220              gnus-newsgroup-auto-expire
8221              (memq mark gnus-auto-expirable-marks))
8222     (setq mark gnus-expirable-mark))
8223   (let ((article (or article (gnus-summary-article-number)))
8224         (old-mark (gnus-summary-article-mark article)))
8225     ;; Allow the backend to change the mark.
8226     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8227     (if (eq mark old-mark)
8228         t
8229       (unless article
8230         (error "No article on current line"))
8231       (if (not (if (or (= mark gnus-unread-mark)
8232                        (= mark gnus-ticked-mark)
8233                        (= mark gnus-dormant-mark))
8234                    (gnus-mark-article-as-unread article mark)
8235                  (gnus-mark-article-as-read article mark)))
8236           t
8237         ;; See whether the article is to be put in the cache.
8238         (and gnus-use-cache
8239              (not (= mark gnus-canceled-mark))
8240              (vectorp (gnus-summary-article-header article))
8241              (save-excursion
8242                (gnus-cache-possibly-enter-article
8243                 gnus-newsgroup-name article
8244                 (= mark gnus-ticked-mark)
8245                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8246
8247         (when (gnus-summary-goto-subject article nil t)
8248           (let ((buffer-read-only nil))
8249             (gnus-summary-show-thread)
8250             ;; Fix the mark.
8251             (gnus-summary-update-mark mark 'unread)
8252             t))))))
8253
8254 (defun gnus-summary-update-secondary-mark (article)
8255   "Update the secondary (read, process, cache) mark."
8256   (gnus-summary-update-mark
8257    (cond ((memq article gnus-newsgroup-processable)
8258           gnus-process-mark)
8259          ((memq article gnus-newsgroup-cached)
8260           gnus-cached-mark)
8261          ((memq article gnus-newsgroup-replied)
8262           gnus-replied-mark)
8263          ((memq article gnus-newsgroup-saved)
8264           gnus-saved-mark)
8265          (t gnus-unread-mark))
8266    'replied)
8267   (when (gnus-visual-p 'summary-highlight 'highlight)
8268     (gnus-run-hooks 'gnus-summary-update-hook))
8269   t)
8270
8271 (defun gnus-summary-update-mark (mark type)
8272   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8273         (buffer-read-only nil))
8274     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8275     (when forward
8276       (when (looking-at "\r")
8277         (incf forward))
8278       (when (<= (+ forward (point)) (point-max))
8279         ;; Go to the right position on the line.
8280         (goto-char (+ forward (point)))
8281         ;; Replace the old mark with the new mark.
8282         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8283         ;; Optionally update the marks by some user rule.
8284         (when (eq type 'unread)
8285           (gnus-data-set-mark
8286            (gnus-data-find (gnus-summary-article-number)) mark)
8287           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8288
8289 (defun gnus-mark-article-as-read (article &optional mark)
8290   "Enter ARTICLE in the pertinent lists and remove it from others."
8291   ;; Make the article expirable.
8292   (let ((mark (or mark gnus-del-mark)))
8293     (if (= mark gnus-expirable-mark)
8294         (push article gnus-newsgroup-expirable)
8295       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8296     ;; Remove from unread and marked lists.
8297     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8298     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8299     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8300     (push (cons article mark) gnus-newsgroup-reads)
8301     ;; Possibly remove from cache, if that is used.
8302     (when gnus-use-cache
8303       (gnus-cache-enter-remove-article article))
8304     t))
8305
8306 (defun gnus-mark-article-as-unread (article &optional mark)
8307   "Enter ARTICLE in the pertinent lists and remove it from others."
8308   (let ((mark (or mark gnus-ticked-mark)))
8309     (if (<= article 0)
8310         (progn
8311           (gnus-error 1 "Can't mark negative article numbers")
8312           nil)
8313       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8314             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8315             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8316             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8317
8318       ;; Unsuppress duplicates?
8319       (when gnus-suppress-duplicates
8320         (gnus-dup-unsuppress-article article))
8321
8322       (cond ((= mark gnus-ticked-mark)
8323              (push article gnus-newsgroup-marked))
8324             ((= mark gnus-dormant-mark)
8325              (push article gnus-newsgroup-dormant))
8326             (t
8327              (push article gnus-newsgroup-unreads)))
8328       (gnus-pull article gnus-newsgroup-reads)
8329       t)))
8330
8331 (defalias 'gnus-summary-mark-as-unread-forward
8332   'gnus-summary-tick-article-forward)
8333 (make-obsolete 'gnus-summary-mark-as-unread-forward
8334                'gnus-summary-tick-article-forward)
8335 (defun gnus-summary-tick-article-forward (n)
8336   "Tick N articles forwards.
8337 If N is negative, tick backwards instead.
8338 The difference between N and the number of articles ticked is returned."
8339   (interactive "p")
8340   (gnus-summary-mark-forward n gnus-ticked-mark))
8341
8342 (defalias 'gnus-summary-mark-as-unread-backward
8343   'gnus-summary-tick-article-backward)
8344 (make-obsolete 'gnus-summary-mark-as-unread-backward
8345                'gnus-summary-tick-article-backward)
8346 (defun gnus-summary-tick-article-backward (n)
8347   "Tick N articles backwards.
8348 The difference between N and the number of articles ticked is returned."
8349   (interactive "p")
8350   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8351
8352 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8353 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8354 (defun gnus-summary-tick-article (&optional article clear-mark)
8355   "Mark current article as unread.
8356 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8357 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8358   (interactive)
8359   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8360                                        gnus-ticked-mark)))
8361
8362 (defun gnus-summary-mark-as-read-forward (n)
8363   "Mark N articles as read forwards.
8364 If N is negative, mark backwards instead.
8365 The difference between N and the actual number of articles marked is
8366 returned."
8367   (interactive "p")
8368   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8369
8370 (defun gnus-summary-mark-as-read-backward (n)
8371   "Mark the N articles as read backwards.
8372 The difference between N and the actual number of articles marked is
8373 returned."
8374   (interactive "p")
8375   (gnus-summary-mark-forward
8376    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8377
8378 (defun gnus-summary-mark-as-read (&optional article mark)
8379   "Mark current article as read.
8380 ARTICLE specifies the article to be marked as read.
8381 MARK specifies a string to be inserted at the beginning of the line."
8382   (gnus-summary-mark-article article mark))
8383
8384 (defun gnus-summary-clear-mark-forward (n)
8385   "Clear marks from N articles forward.
8386 If N is negative, clear backward instead.
8387 The difference between N and the number of marks cleared is returned."
8388   (interactive "p")
8389   (gnus-summary-mark-forward n gnus-unread-mark))
8390
8391 (defun gnus-summary-clear-mark-backward (n)
8392   "Clear marks from N articles backward.
8393 The difference between N and the number of marks cleared is returned."
8394   (interactive "p")
8395   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8396
8397 (defun gnus-summary-mark-unread-as-read ()
8398   "Intended to be used by `gnus-summary-mark-article-hook'."
8399   (when (memq gnus-current-article gnus-newsgroup-unreads)
8400     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8401
8402 (defun gnus-summary-mark-read-and-unread-as-read ()
8403   "Intended to be used by `gnus-summary-mark-article-hook'."
8404   (let ((mark (gnus-summary-article-mark)))
8405     (when (or (gnus-unread-mark-p mark)
8406               (gnus-read-mark-p mark))
8407       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8408
8409 (defun gnus-summary-mark-region-as-read (point mark all)
8410   "Mark all unread articles between point and mark as read.
8411 If given a prefix, mark all articles between point and mark as read,
8412 even ticked and dormant ones."
8413   (interactive "r\nP")
8414   (save-excursion
8415     (let (article)
8416       (goto-char point)
8417       (beginning-of-line)
8418       (while (and
8419               (< (point) mark)
8420               (progn
8421                 (when (or all
8422                           (memq (setq article (gnus-summary-article-number))
8423                                 gnus-newsgroup-unreads))
8424                   (gnus-summary-mark-article article gnus-del-mark))
8425                 t)
8426               (gnus-summary-find-next))))))
8427
8428 (defun gnus-summary-mark-below (score mark)
8429   "Mark articles with score less than SCORE with MARK."
8430   (interactive "P\ncMark: ")
8431   (setq score (if score
8432                   (prefix-numeric-value score)
8433                 (or gnus-summary-default-score 0)))
8434   (save-excursion
8435     (set-buffer gnus-summary-buffer)
8436     (goto-char (point-min))
8437     (while
8438         (progn
8439           (and (< (gnus-summary-article-score) score)
8440                (gnus-summary-mark-article nil mark))
8441           (gnus-summary-find-next)))))
8442
8443 (defun gnus-summary-kill-below (&optional score)
8444   "Mark articles with score below SCORE as read."
8445   (interactive "P")
8446   (gnus-summary-mark-below score gnus-killed-mark))
8447
8448 (defun gnus-summary-clear-above (&optional score)
8449   "Clear all marks from articles with score above SCORE."
8450   (interactive "P")
8451   (gnus-summary-mark-above score gnus-unread-mark))
8452
8453 (defun gnus-summary-tick-above (&optional score)
8454   "Tick all articles with score above SCORE."
8455   (interactive "P")
8456   (gnus-summary-mark-above score gnus-ticked-mark))
8457
8458 (defun gnus-summary-mark-above (score mark)
8459   "Mark articles with score over SCORE with MARK."
8460   (interactive "P\ncMark: ")
8461   (setq score (if score
8462                   (prefix-numeric-value score)
8463                 (or gnus-summary-default-score 0)))
8464   (save-excursion
8465     (set-buffer gnus-summary-buffer)
8466     (goto-char (point-min))
8467     (while (and (progn
8468                   (when (> (gnus-summary-article-score) score)
8469                     (gnus-summary-mark-article nil mark))
8470                   t)
8471                 (gnus-summary-find-next)))))
8472
8473 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8474 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8475 (defun gnus-summary-limit-include-expunged (&optional no-error)
8476   "Display all the hidden articles that were expunged for low scores."
8477   (interactive)
8478   (let ((buffer-read-only nil))
8479     (let ((scored gnus-newsgroup-scored)
8480           headers h)
8481       (while scored
8482         (unless (gnus-summary-goto-subject (caar scored))
8483           (and (setq h (gnus-summary-article-header (caar scored)))
8484                (< (cdar scored) gnus-summary-expunge-below)
8485                (push h headers)))
8486         (setq scored (cdr scored)))
8487       (if (not headers)
8488           (when (not no-error)
8489             (error "No expunged articles hidden"))
8490         (goto-char (point-min))
8491         (gnus-summary-prepare-unthreaded (nreverse headers))
8492         (goto-char (point-min))
8493         (gnus-summary-position-point)
8494         t))))
8495
8496 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8497   "Mark all unread articles in this newsgroup as read.
8498 If prefix argument ALL is non-nil, ticked and dormant articles will
8499 also be marked as read.
8500 If QUIETLY is non-nil, no questions will be asked.
8501 If TO-HERE is non-nil, it should be a point in the buffer.  All
8502 articles before this point will be marked as read.
8503 Note that this function will only catch up the unread article
8504 in the current summary buffer limitation.
8505 The number of articles marked as read is returned."
8506   (interactive "P")
8507   (prog1
8508       (save-excursion
8509         (when (or quietly
8510                   (not gnus-interactive-catchup) ;Without confirmation?
8511                   gnus-expert-user
8512                   (gnus-y-or-n-p
8513                    (if all
8514                        "Mark absolutely all articles as read? "
8515                      "Mark all unread articles as read? ")))
8516           (if (and not-mark
8517                    (not gnus-newsgroup-adaptive)
8518                    (not gnus-newsgroup-auto-expire)
8519                    (not gnus-suppress-duplicates)
8520                    (or (not gnus-use-cache)
8521                        (eq gnus-use-cache 'passive)))
8522               (progn
8523                 (when all
8524                   (setq gnus-newsgroup-marked nil
8525                         gnus-newsgroup-dormant nil))
8526                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8527             ;; We actually mark all articles as canceled, which we
8528             ;; have to do when using auto-expiry or adaptive scoring.
8529             (gnus-summary-show-all-threads)
8530             (when (gnus-summary-first-subject (not all) t)
8531               (while (and
8532                       (if to-here (< (point) to-here) t)
8533                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8534                       (gnus-summary-find-next (not all) nil nil t))))
8535             (gnus-set-mode-line 'summary))
8536           t))
8537     (gnus-summary-position-point)))
8538
8539 (defun gnus-summary-catchup-to-here (&optional all)
8540   "Mark all unticked articles before the current one as read.
8541 If ALL is non-nil, also mark ticked and dormant articles as read."
8542   (interactive "P")
8543   (save-excursion
8544     (gnus-save-hidden-threads
8545       (let ((beg (point)))
8546         ;; We check that there are unread articles.
8547         (when (or all (gnus-summary-find-prev))
8548           (gnus-summary-catchup all t beg)))))
8549   (gnus-summary-position-point))
8550
8551 (defun gnus-summary-catchup-all (&optional quietly)
8552   "Mark all articles in this newsgroup as read."
8553   (interactive "P")
8554   (gnus-summary-catchup t quietly))
8555
8556 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8557   "Mark all unread articles in this group as read, then exit.
8558 If prefix argument ALL is non-nil, all articles are marked as read."
8559   (interactive "P")
8560   (when (gnus-summary-catchup all quietly nil 'fast)
8561     ;; Select next newsgroup or exit.
8562     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8563              (eq gnus-auto-select-next 'quietly))
8564         (gnus-summary-next-group nil)
8565       (gnus-summary-exit))))
8566
8567 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8568   "Mark all articles in this newsgroup as read, and then exit."
8569   (interactive "P")
8570   (gnus-summary-catchup-and-exit t quietly))
8571
8572 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8573   "Mark all articles in this group as read and select the next group.
8574 If given a prefix, mark all articles, unread as well as ticked, as
8575 read."
8576   (interactive "P")
8577   (save-excursion
8578     (gnus-summary-catchup all))
8579   (gnus-summary-next-group))
8580
8581 ;;;
8582 ;;; with article
8583 ;;;
8584
8585 (defmacro gnus-with-article (article &rest forms)
8586   "Select ARTICLE and perform FORMS in the original article buffer.
8587 Then replace the article with the result."
8588   `(progn
8589      ;; We don't want the article to be marked as read.
8590      (let (gnus-mark-article-hook)
8591        (gnus-summary-select-article t t nil ,article))
8592      (set-buffer gnus-original-article-buffer)
8593      ,@forms
8594      (if (not (gnus-check-backend-function
8595                'request-replace-article (car gnus-article-current)))
8596          (gnus-message 5 "Read-only group; not replacing")
8597        (unless (gnus-request-replace-article
8598                 ,article (car gnus-article-current)
8599                 (current-buffer) t)
8600          (error "Couldn't replace article")))
8601      ;; The cache and backlog have to be flushed somewhat.
8602      (when gnus-keep-backlog
8603        (gnus-backlog-remove-article
8604         (car gnus-article-current) (cdr gnus-article-current)))
8605      (when gnus-use-cache
8606        (gnus-cache-update-article
8607         (car gnus-article-current) (cdr gnus-article-current)))))
8608
8609 (put 'gnus-with-article 'lisp-indent-function 1)
8610 (put 'gnus-with-article 'edebug-form-spec '(form body))
8611
8612 ;; Thread-based commands.
8613
8614 (defun gnus-summary-articles-in-thread (&optional article)
8615   "Return a list of all articles in the current thread.
8616 If ARTICLE is non-nil, return all articles in the thread that starts
8617 with that article."
8618   (let* ((article (or article (gnus-summary-article-number)))
8619          (data (gnus-data-find-list article))
8620          (top-level (gnus-data-level (car data)))
8621          (top-subject
8622           (cond ((null gnus-thread-operation-ignore-subject)
8623                  (gnus-simplify-subject-re
8624                   (mail-header-subject (gnus-data-header (car data)))))
8625                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8626                  (gnus-simplify-subject-fuzzy
8627                   (mail-header-subject (gnus-data-header (car data)))))
8628                 (t nil)))
8629          (end-point (save-excursion
8630                       (if (gnus-summary-go-to-next-thread)
8631                           (point) (point-max))))
8632          articles)
8633     (while (and data
8634                 (< (gnus-data-pos (car data)) end-point))
8635       (when (or (not top-subject)
8636                 (string= top-subject
8637                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8638                              (gnus-simplify-subject-fuzzy
8639                               (mail-header-subject
8640                                (gnus-data-header (car data))))
8641                            (gnus-simplify-subject-re
8642                             (mail-header-subject
8643                              (gnus-data-header (car data)))))))
8644         (push (gnus-data-number (car data)) articles))
8645       (unless (and (setq data (cdr data))
8646                    (> (gnus-data-level (car data)) top-level))
8647         (setq data nil)))
8648     ;; Return the list of articles.
8649     (nreverse articles)))
8650
8651 (defun gnus-summary-rethread-current ()
8652   "Rethread the thread the current article is part of."
8653   (interactive)
8654   (let* ((gnus-show-threads t)
8655          (article (gnus-summary-article-number))
8656          (id (mail-header-id (gnus-summary-article-header)))
8657          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8658     (unless id
8659       (error "No article on the current line"))
8660     (gnus-rebuild-thread id)
8661     (gnus-summary-goto-subject article)))
8662
8663 (defun gnus-summary-reparent-thread ()
8664   "Make the current article child of the marked (or previous) article.
8665
8666 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8667 is non-nil or the Subject: of both articles are the same."
8668   (interactive)
8669   (unless (not (gnus-group-read-only-p))
8670     (error "The current newsgroup does not support article editing"))
8671   (unless (<= (length gnus-newsgroup-processable) 1)
8672     (error "No more than one article may be marked"))
8673   (save-window-excursion
8674     (let ((gnus-article-buffer " *reparent*")
8675           (current-article (gnus-summary-article-number))
8676           ;; First grab the marked article, otherwise one line up.
8677           (parent-article (if (not (null gnus-newsgroup-processable))
8678                               (car gnus-newsgroup-processable)
8679                             (save-excursion
8680                               (if (eq (forward-line -1) 0)
8681                                   (gnus-summary-article-number)
8682                                 (error "Beginning of summary buffer"))))))
8683       (unless (not (eq current-article parent-article))
8684         (error "An article may not be self-referential"))
8685       (let ((message-id (mail-header-id
8686                          (gnus-summary-article-header parent-article))))
8687         (unless (and message-id (not (equal message-id "")))
8688           (error "No message-id in desired parent"))
8689         (gnus-with-article current-article
8690           (goto-char (point-min))
8691           (if (re-search-forward "^References: " nil t)
8692               (progn
8693                 (re-search-forward "^[^ \t]" nil t)
8694                 (forward-line -1)
8695                 (end-of-line)
8696                 (insert " " message-id))
8697             (insert "References: " message-id "\n")))
8698         (set-buffer gnus-summary-buffer)
8699         (gnus-summary-unmark-all-processable)
8700         (gnus-summary-update-article current-article)
8701         (gnus-summary-rethread-current)
8702         (gnus-message 3 "Article %d is now the child of article %d"
8703                       current-article parent-article)))))
8704
8705 (defun gnus-summary-toggle-threads (&optional arg)
8706   "Toggle showing conversation threads.
8707 If ARG is positive number, turn showing conversation threads on."
8708   (interactive "P")
8709   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8710     (setq gnus-show-threads
8711           (if (null arg) (not gnus-show-threads)
8712             (> (prefix-numeric-value arg) 0)))
8713     (gnus-summary-prepare)
8714     (gnus-summary-goto-subject current)
8715     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8716     (gnus-summary-position-point)))
8717
8718 (defun gnus-summary-show-all-threads ()
8719   "Show all threads."
8720   (interactive)
8721   (save-excursion
8722     (let ((buffer-read-only nil))
8723       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8724   (gnus-summary-position-point))
8725
8726 (defun gnus-summary-show-thread ()
8727   "Show thread subtrees.
8728 Returns nil if no thread was there to be shown."
8729   (interactive)
8730   (let ((buffer-read-only nil)
8731         (orig (point))
8732         ;; first goto end then to beg, to have point at beg after let
8733         (end (progn (end-of-line) (point)))
8734         (beg (progn (beginning-of-line) (point))))
8735     (prog1
8736         ;; Any hidden lines here?
8737         (search-forward "\r" end t)
8738       (subst-char-in-region beg end ?\^M ?\n t)
8739       (goto-char orig)
8740       (gnus-summary-position-point))))
8741
8742 (defun gnus-summary-hide-all-threads ()
8743   "Hide all thread subtrees."
8744   (interactive)
8745   (save-excursion
8746     (goto-char (point-min))
8747     (gnus-summary-hide-thread)
8748     (while (zerop (gnus-summary-next-thread 1 t))
8749       (gnus-summary-hide-thread)))
8750   (gnus-summary-position-point))
8751
8752 (defun gnus-summary-hide-thread ()
8753   "Hide thread subtrees.
8754 Returns nil if no threads were there to be hidden."
8755   (interactive)
8756   (let ((buffer-read-only nil)
8757         (start (point))
8758         (article (gnus-summary-article-number)))
8759     (goto-char start)
8760     ;; Go forward until either the buffer ends or the subthread
8761     ;; ends.
8762     (when (and (not (eobp))
8763                (or (zerop (gnus-summary-next-thread 1 t))
8764                    (goto-char (point-max))))
8765       (prog1
8766           (if (and (> (point) start)
8767                    (search-backward "\n" start t))
8768               (progn
8769                 (subst-char-in-region start (point) ?\n ?\^M)
8770                 (gnus-summary-goto-subject article))
8771             (goto-char start)
8772             nil)))))
8773
8774 (defun gnus-summary-go-to-next-thread (&optional previous)
8775   "Go to the same level (or less) next thread.
8776 If PREVIOUS is non-nil, go to previous thread instead.
8777 Return the article number moved to, or nil if moving was impossible."
8778   (let ((level (gnus-summary-thread-level))
8779         (way (if previous -1 1))
8780         (beg (point)))
8781     (forward-line way)
8782     (while (and (not (eobp))
8783                 (< level (gnus-summary-thread-level)))
8784       (forward-line way))
8785     (if (eobp)
8786         (progn
8787           (goto-char beg)
8788           nil)
8789       (setq beg (point))
8790       (prog1
8791           (gnus-summary-article-number)
8792         (goto-char beg)))))
8793
8794 (defun gnus-summary-next-thread (n &optional silent)
8795   "Go to the same level next N'th thread.
8796 If N is negative, search backward instead.
8797 Returns the difference between N and the number of skips actually
8798 done.
8799
8800 If SILENT, don't output messages."
8801   (interactive "p")
8802   (let ((backward (< n 0))
8803         (n (abs n)))
8804     (while (and (> n 0)
8805                 (gnus-summary-go-to-next-thread backward))
8806       (decf n))
8807     (unless silent
8808       (gnus-summary-position-point))
8809     (when (and (not silent) (/= 0 n))
8810       (gnus-message 7 "No more threads"))
8811     n))
8812
8813 (defun gnus-summary-prev-thread (n)
8814   "Go to the same level previous N'th thread.
8815 Returns the difference between N and the number of skips actually
8816 done."
8817   (interactive "p")
8818   (gnus-summary-next-thread (- n)))
8819
8820 (defun gnus-summary-go-down-thread ()
8821   "Go down one level in the current thread."
8822   (let ((children (gnus-summary-article-children)))
8823     (when children
8824       (gnus-summary-goto-subject (car children)))))
8825
8826 (defun gnus-summary-go-up-thread ()
8827   "Go up one level in the current thread."
8828   (let ((parent (gnus-summary-article-parent)))
8829     (when parent
8830       (gnus-summary-goto-subject parent))))
8831
8832 (defun gnus-summary-down-thread (n)
8833   "Go down thread N steps.
8834 If N is negative, go up instead.
8835 Returns the difference between N and how many steps down that were
8836 taken."
8837   (interactive "p")
8838   (let ((up (< n 0))
8839         (n (abs n)))
8840     (while (and (> n 0)
8841                 (if up (gnus-summary-go-up-thread)
8842                   (gnus-summary-go-down-thread)))
8843       (setq n (1- n)))
8844     (gnus-summary-position-point)
8845     (when (/= 0 n)
8846       (gnus-message 7 "Can't go further"))
8847     n))
8848
8849 (defun gnus-summary-up-thread (n)
8850   "Go up thread N steps.
8851 If N is negative, go up instead.
8852 Returns the difference between N and how many steps down that were
8853 taken."
8854   (interactive "p")
8855   (gnus-summary-down-thread (- n)))
8856
8857 (defun gnus-summary-top-thread ()
8858   "Go to the top of the thread."
8859   (interactive)
8860   (while (gnus-summary-go-up-thread))
8861   (gnus-summary-article-number))
8862
8863 (defun gnus-summary-kill-thread (&optional unmark)
8864   "Mark articles under current thread as read.
8865 If the prefix argument is positive, remove any kinds of marks.
8866 If the prefix argument is negative, tick articles instead."
8867   (interactive "P")
8868   (when unmark
8869     (setq unmark (prefix-numeric-value unmark)))
8870   (let ((articles (gnus-summary-articles-in-thread)))
8871     (save-excursion
8872       ;; Expand the thread.
8873       (gnus-summary-show-thread)
8874       ;; Mark all the articles.
8875       (while articles
8876         (gnus-summary-goto-subject (car articles))
8877         (cond ((null unmark)
8878                (gnus-summary-mark-article-as-read gnus-killed-mark))
8879               ((> unmark 0)
8880                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8881               (t
8882                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8883         (setq articles (cdr articles))))
8884     ;; Hide killed subtrees.
8885     (and (null unmark)
8886          gnus-thread-hide-killed
8887          (gnus-summary-hide-thread))
8888     ;; If marked as read, go to next unread subject.
8889     (when (null unmark)
8890       ;; Go to next unread subject.
8891       (gnus-summary-next-subject 1 t)))
8892   (gnus-set-mode-line 'summary))
8893
8894 ;; Summary sorting commands
8895
8896 (defun gnus-summary-sort-by-number (&optional reverse)
8897   "Sort the summary buffer by article number.
8898 Argument REVERSE means reverse order."
8899   (interactive "P")
8900   (gnus-summary-sort 'number reverse))
8901
8902 (defun gnus-summary-sort-by-author (&optional reverse)
8903   "Sort the summary buffer by author name alphabetically.
8904 If `case-fold-search' is non-nil, case of letters is ignored.
8905 Argument REVERSE means reverse order."
8906   (interactive "P")
8907   (gnus-summary-sort 'author reverse))
8908
8909 (defun gnus-summary-sort-by-subject (&optional reverse)
8910   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8911 If `case-fold-search' is non-nil, case of letters is ignored.
8912 Argument REVERSE means reverse order."
8913   (interactive "P")
8914   (gnus-summary-sort 'subject reverse))
8915
8916 (defun gnus-summary-sort-by-date (&optional reverse)
8917   "Sort the summary buffer by date.
8918 Argument REVERSE means reverse order."
8919   (interactive "P")
8920   (gnus-summary-sort 'date reverse))
8921
8922 (defun gnus-summary-sort-by-score (&optional reverse)
8923   "Sort the summary buffer by score.
8924 Argument REVERSE means reverse order."
8925   (interactive "P")
8926   (gnus-summary-sort 'score reverse))
8927
8928 (defun gnus-summary-sort-by-lines (&optional reverse)
8929   "Sort the summary buffer by the number of lines.
8930 Argument REVERSE means reverse order."
8931   (interactive "P")
8932   (gnus-summary-sort 'lines reverse))
8933
8934 (defun gnus-summary-sort-by-chars (&optional reverse)
8935   "Sort the summary buffer by article length.
8936 Argument REVERSE means reverse order."
8937   (interactive "P")
8938   (gnus-summary-sort 'chars reverse))
8939
8940 (defun gnus-summary-sort (predicate reverse)
8941   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8942   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8943          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8944          (gnus-thread-sort-functions
8945           (if (not reverse)
8946               thread
8947             `(lambda (t1 t2)
8948                (,thread t2 t1))))
8949          (gnus-article-sort-functions
8950           (if (not reverse)
8951               article
8952             `(lambda (t1 t2)
8953                (,article t2 t1))))
8954          (buffer-read-only)
8955          (gnus-summary-prepare-hook nil))
8956     ;; We do the sorting by regenerating the threads.
8957     (gnus-summary-prepare)
8958     ;; Hide subthreads if needed.
8959     (when (and gnus-show-threads gnus-thread-hide-subtree)
8960       (gnus-summary-hide-all-threads))))
8961
8962 ;; Summary saving commands.
8963
8964 (defun gnus-summary-save-article (&optional n not-saved)
8965   "Save the current article using the default saver function.
8966 If N is a positive number, save the N next articles.
8967 If N is a negative number, save the N previous articles.
8968 If N is nil and any articles have been marked with the process mark,
8969 save those articles instead.
8970 The variable `gnus-default-article-saver' specifies the saver function."
8971   (interactive "P")
8972   (let* ((articles (gnus-summary-work-articles n))
8973          (save-buffer (save-excursion
8974                         (nnheader-set-temp-buffer " *Gnus Save*")))
8975          (num (length articles))
8976          header file)
8977     (dolist (article articles)
8978       (setq header (gnus-summary-article-header article))
8979       (if (not (vectorp header))
8980           ;; This is a pseudo-article.
8981           (if (assq 'name header)
8982               (gnus-copy-file (cdr (assq 'name header)))
8983             (gnus-message 1 "Article %d is unsaveable" article))
8984         ;; This is a real article.
8985         (save-window-excursion
8986           (gnus-summary-select-article t nil nil article))
8987         (save-excursion
8988           (set-buffer save-buffer)
8989           (erase-buffer)
8990           (insert-buffer-substring gnus-original-article-buffer))
8991         (setq file (gnus-article-save save-buffer file num))
8992         (gnus-summary-remove-process-mark article)
8993         (unless not-saved
8994           (gnus-summary-set-saved-mark article))))
8995     (gnus-kill-buffer save-buffer)
8996     (gnus-summary-position-point)
8997     (gnus-set-mode-line 'summary)
8998     n))
8999
9000 (defun gnus-summary-pipe-output (&optional arg)
9001   "Pipe the current article to a subprocess.
9002 If N is a positive number, pipe the N next articles.
9003 If N is a negative number, pipe the N previous articles.
9004 If N is nil and any articles have been marked with the process mark,
9005 pipe those articles instead."
9006   (interactive "P")
9007   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9008     (gnus-summary-save-article arg t))
9009   (gnus-configure-windows 'pipe))
9010
9011 (defun gnus-summary-save-article-mail (&optional arg)
9012   "Append the current article to an mail file.
9013 If N is a positive number, save the N next articles.
9014 If N is a negative number, save the N previous articles.
9015 If N is nil and any articles have been marked with the process mark,
9016 save those articles instead."
9017   (interactive "P")
9018   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9019     (gnus-summary-save-article arg)))
9020
9021 (defun gnus-summary-save-article-rmail (&optional arg)
9022   "Append the current article to an rmail file.
9023 If N is a positive number, save the N next articles.
9024 If N is a negative number, save the N previous articles.
9025 If N is nil and any articles have been marked with the process mark,
9026 save those articles instead."
9027   (interactive "P")
9028   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9029     (gnus-summary-save-article arg)))
9030
9031 (defun gnus-summary-save-article-file (&optional arg)
9032   "Append the current article to a file.
9033 If N is a positive number, save the N next articles.
9034 If N is a negative number, save the N previous articles.
9035 If N is nil and any articles have been marked with the process mark,
9036 save those articles instead."
9037   (interactive "P")
9038   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9039     (gnus-summary-save-article arg)))
9040
9041 (defun gnus-summary-write-article-file (&optional arg)
9042   "Write the current article to a file, deleting the previous file.
9043 If N is a positive number, save the N next articles.
9044 If N is a negative number, save the N previous articles.
9045 If N is nil and any articles have been marked with the process mark,
9046 save those articles instead."
9047   (interactive "P")
9048   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9049     (gnus-summary-save-article arg)))
9050
9051 (defun gnus-summary-save-article-body-file (&optional arg)
9052   "Append the current article body to a file.
9053 If N is a positive number, save the N next articles.
9054 If N is a negative number, save the N previous articles.
9055 If N is nil and any articles have been marked with the process mark,
9056 save those articles instead."
9057   (interactive "P")
9058   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9059     (gnus-summary-save-article arg)))
9060
9061 (defun gnus-summary-pipe-message (program)
9062   "Pipe the current article through PROGRAM."
9063   (interactive "sProgram: ")
9064   (gnus-summary-select-article)
9065   (let ((mail-header-separator ""))
9066     (gnus-eval-in-buffer-window gnus-article-buffer
9067       (save-restriction
9068         (widen)
9069         (let ((start (window-start))
9070               buffer-read-only)
9071           (message-pipe-buffer-body program)
9072           (set-window-start (get-buffer-window (current-buffer)) start))))))
9073
9074 (defun gnus-get-split-value (methods)
9075   "Return a value based on the split METHODS."
9076   (let (split-name method result match)
9077     (when methods
9078       (save-excursion
9079         (set-buffer gnus-original-article-buffer)
9080         (save-restriction
9081           (nnheader-narrow-to-headers)
9082           (while methods
9083             (goto-char (point-min))
9084             (setq method (pop methods))
9085             (setq match (car method))
9086             (when (cond
9087                    ((stringp match)
9088                     ;; Regular expression.
9089                     (ignore-errors
9090                       (re-search-forward match nil t)))
9091                    ((gnus-functionp match)
9092                     ;; Function.
9093                     (save-restriction
9094                       (widen)
9095                       (setq result (funcall match gnus-newsgroup-name))))
9096                    ((consp match)
9097                     ;; Form.
9098                     (save-restriction
9099                       (widen)
9100                       (setq result (eval match)))))
9101               (setq split-name (append (cdr method) split-name))
9102               (cond ((stringp result)
9103                      (push (expand-file-name
9104                             result gnus-article-save-directory)
9105                            split-name))
9106                     ((consp result)
9107                      (setq split-name (append result split-name)))))))))
9108     (nreverse split-name)))
9109
9110 (defun gnus-valid-move-group-p (group)
9111   (and (boundp group)
9112        (symbol-name group)
9113        (symbol-value group)
9114        (gnus-get-function (gnus-find-method-for-group
9115                            (symbol-name group)) 'request-accept-article t)))
9116
9117 (defun gnus-read-move-group-name (prompt default articles prefix)
9118   "Read a group name."
9119   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9120          (minibuffer-confirm-incomplete nil) ; XEmacs
9121          (prom
9122           (format "%s %s to:"
9123                   prompt
9124                   (if (> (length articles) 1)
9125                       (format "these %d articles" (length articles))
9126                     "this article")))
9127          (to-newsgroup
9128           (cond
9129            ((null split-name)
9130             (gnus-completing-read default prom
9131                                   gnus-active-hashtb
9132                                   'gnus-valid-move-group-p
9133                                   nil prefix
9134                                   'gnus-group-history))
9135            ((= 1 (length split-name))
9136             (gnus-completing-read (car split-name) prom
9137                                   gnus-active-hashtb
9138                                   'gnus-valid-move-group-p
9139                                   nil nil
9140                                   'gnus-group-history))
9141            (t
9142             (gnus-completing-read nil prom
9143                                   (mapcar (lambda (el) (list el))
9144                                           (nreverse split-name))
9145                                   nil nil nil
9146                                   'gnus-group-history))))
9147          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9148     (when to-newsgroup
9149       (if (or (string= to-newsgroup "")
9150               (string= to-newsgroup prefix))
9151           (setq to-newsgroup default))
9152       (unless to-newsgroup
9153         (error "No group name entered"))
9154       (or (gnus-active to-newsgroup)
9155           (gnus-activate-group to-newsgroup nil nil to-method)
9156           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9157                                      to-newsgroup))
9158               (or (and (gnus-request-create-group to-newsgroup to-method)
9159                        (gnus-activate-group
9160                         to-newsgroup nil nil to-method)
9161                        (gnus-subscribe-group to-newsgroup))
9162                   (error "Couldn't create group %s" to-newsgroup)))
9163           (error "No such group: %s" to-newsgroup)))
9164     to-newsgroup))
9165
9166 (defun gnus-summary-save-parts (type dir n &optional reverse)
9167   "Save parts matching TYPE to DIR.
9168 If REVERSE, save parts that do not match TYPE."
9169   (interactive
9170    (list (read-string "Save parts of type: " "image/.*")
9171          (read-file-name "Save to directory: " nil nil t)
9172          current-prefix-arg))
9173   (gnus-summary-iterate n
9174     (let ((gnus-display-mime-function nil)
9175           (gnus-inhibit-treatment t))
9176       (gnus-summary-select-article))
9177     (save-excursion
9178       (set-buffer gnus-article-buffer)
9179       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9180         (when handles
9181           (gnus-summary-save-parts-1 type dir handles reverse)
9182           (mm-destroy-parts handles))))))
9183
9184 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9185   (if (stringp (car handle))
9186       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9187               (cdr handle))
9188     (when (if reverse
9189               (not (string-match type (mm-handle-media-type handle)))
9190             (string-match type (mm-handle-media-type handle)))
9191       (let ((file (expand-file-name
9192                    (file-name-nondirectory
9193                     (or
9194                      (mail-content-type-get
9195                       (mm-handle-disposition handle) 'filename)
9196                      (concat gnus-newsgroup-name
9197                              "." (number-to-string
9198                                   (cdr gnus-article-current)))))
9199                    dir)))
9200         (unless (file-exists-p file)
9201           (mm-save-part-to-file handle file))))))
9202
9203 ;; Summary extract commands
9204
9205 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9206   (let ((buffer-read-only nil)
9207         (article (gnus-summary-article-number))
9208         after-article b e)
9209     (unless (gnus-summary-goto-subject article)
9210       (error "No such article: %d" article))
9211     (gnus-summary-position-point)
9212     ;; If all commands are to be bunched up on one line, we collect
9213     ;; them here.
9214     (unless gnus-view-pseudos-separately
9215       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9216             files action)
9217         (while ps
9218           (setq action (cdr (assq 'action (car ps))))
9219           (setq files (list (cdr (assq 'name (car ps)))))
9220           (while (and ps (cdr ps)
9221                       (string= (or action "1")
9222                                (or (cdr (assq 'action (cadr ps))) "2")))
9223             (push (cdr (assq 'name (cadr ps))) files)
9224             (setcdr ps (cddr ps)))
9225           (when files
9226             (when (not (string-match "%s" action))
9227               (push " " files))
9228             (push " " files)
9229             (when (assq 'execute (car ps))
9230               (setcdr (assq 'execute (car ps))
9231                       (funcall (if (string-match "%s" action)
9232                                    'format 'concat)
9233                                action
9234                                (mapconcat
9235                                 (lambda (f)
9236                                   (if (equal f " ")
9237                                       f
9238                                     (mm-quote-arg f)))
9239                                 files " ")))))
9240           (setq ps (cdr ps)))))
9241     (if (and gnus-view-pseudos (not not-view))
9242         (while pslist
9243           (when (assq 'execute (car pslist))
9244             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9245                                   (eq gnus-view-pseudos 'not-confirm)))
9246           (setq pslist (cdr pslist)))
9247       (save-excursion
9248         (while pslist
9249           (setq after-article (or (cdr (assq 'article (car pslist)))
9250                                   (gnus-summary-article-number)))
9251           (gnus-summary-goto-subject after-article)
9252           (forward-line 1)
9253           (setq b (point))
9254           (insert "    " (file-name-nondirectory
9255                           (cdr (assq 'name (car pslist))))
9256                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9257           (setq e (point))
9258           (forward-line -1)             ; back to `b'
9259           (gnus-add-text-properties
9260            b (1- e) (list 'gnus-number gnus-reffed-article-number
9261                           gnus-mouse-face-prop gnus-mouse-face))
9262           (gnus-data-enter
9263            after-article gnus-reffed-article-number
9264            gnus-unread-mark b (car pslist) 0 (- e b))
9265           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9266           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9267           (setq pslist (cdr pslist)))))))
9268
9269 (defun gnus-pseudos< (p1 p2)
9270   (let ((c1 (cdr (assq 'action p1)))
9271         (c2 (cdr (assq 'action p2))))
9272     (and c1 c2 (string< c1 c2))))
9273
9274 (defun gnus-request-pseudo-article (props)
9275   (cond ((assq 'execute props)
9276          (gnus-execute-command (cdr (assq 'execute props)))))
9277   (let ((gnus-current-article (gnus-summary-article-number)))
9278     (gnus-run-hooks 'gnus-mark-article-hook)))
9279
9280 (defun gnus-execute-command (command &optional automatic)
9281   (save-excursion
9282     (gnus-article-setup-buffer)
9283     (set-buffer gnus-article-buffer)
9284     (setq buffer-read-only nil)
9285     (let ((command (if automatic command
9286                      (read-string "Command: " (cons command 0)))))
9287       (erase-buffer)
9288       (insert "$ " command "\n\n")
9289       (if gnus-view-pseudo-asynchronously
9290           (start-process "gnus-execute" (current-buffer) shell-file-name
9291                          shell-command-switch command)
9292         (call-process shell-file-name nil t nil
9293                       shell-command-switch command)))))
9294
9295 ;; Summary kill commands.
9296
9297 (defun gnus-summary-edit-global-kill (article)
9298   "Edit the \"global\" kill file."
9299   (interactive (list (gnus-summary-article-number)))
9300   (gnus-group-edit-global-kill article))
9301
9302 (defun gnus-summary-edit-local-kill ()
9303   "Edit a local kill file applied to the current newsgroup."
9304   (interactive)
9305   (setq gnus-current-headers (gnus-summary-article-header))
9306   (gnus-group-edit-local-kill
9307    (gnus-summary-article-number) gnus-newsgroup-name))
9308
9309 ;;; Header reading.
9310
9311 (defun gnus-read-header (id &optional header)
9312   "Read the headers of article ID and enter them into the Gnus system."
9313   (let ((group gnus-newsgroup-name)
9314         (gnus-override-method
9315          (or
9316           gnus-override-method
9317           (and (gnus-news-group-p gnus-newsgroup-name)
9318                (car (gnus-refer-article-methods)))))
9319         where)
9320     ;; First we check to see whether the header in question is already
9321     ;; fetched.
9322     (if (stringp id)
9323         ;; This is a Message-ID.
9324         (setq header (or header (gnus-id-to-header id)))
9325       ;; This is an article number.
9326       (setq header (or header (gnus-summary-article-header id))))
9327     (if (and header
9328              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9329         ;; We have found the header.
9330         header
9331       ;; If this is a sparse article, we have to nix out its
9332       ;; previous entry in the thread hashtb.
9333       (when (and header
9334                  (gnus-summary-article-sparse-p (mail-header-number header)))
9335         (let* ((parent (gnus-parent-id (mail-header-references header)))
9336                (thread (and parent (gnus-id-to-thread parent))))
9337           (when thread
9338             (delq (assq header thread) thread))))
9339       ;; We have to really fetch the header to this article.
9340       (save-excursion
9341         (set-buffer nntp-server-buffer)
9342         (when (setq where (gnus-request-head id group))
9343           (nnheader-fold-continuation-lines)
9344           (goto-char (point-max))
9345           (insert ".\n")
9346           (goto-char (point-min))
9347           (insert "211 ")
9348           (princ (cond
9349                   ((numberp id) id)
9350                   ((cdr where) (cdr where))
9351                   (header (mail-header-number header))
9352                   (t gnus-reffed-article-number))
9353                  (current-buffer))
9354           (insert " Article retrieved.\n"))
9355         (if (or (not where)
9356                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9357             ()                          ; Malformed head.
9358           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9359             (when (and (stringp id)
9360                        (not (string= (gnus-group-real-name group)
9361                                      (car where))))
9362               ;; If we fetched by Message-ID and the article came
9363               ;; from a different group, we fudge some bogus article
9364               ;; numbers for this article.
9365               (mail-header-set-number header gnus-reffed-article-number))
9366             (save-excursion
9367               (set-buffer gnus-summary-buffer)
9368               (decf gnus-reffed-article-number)
9369               (gnus-remove-header (mail-header-number header))
9370               (push header gnus-newsgroup-headers)
9371               (setq gnus-current-headers header)
9372               (push (mail-header-number header) gnus-newsgroup-limit)))
9373           header)))))
9374
9375 (defun gnus-remove-header (number)
9376   "Remove header NUMBER from `gnus-newsgroup-headers'."
9377   (if (and gnus-newsgroup-headers
9378            (= number (mail-header-number (car gnus-newsgroup-headers))))
9379       (pop gnus-newsgroup-headers)
9380     (let ((headers gnus-newsgroup-headers))
9381       (while (and (cdr headers)
9382                   (not (= number (mail-header-number (cadr headers)))))
9383         (pop headers))
9384       (when (cdr headers)
9385         (setcdr headers (cddr headers))))))
9386
9387 ;;;
9388 ;;; summary highlights
9389 ;;;
9390
9391 (defun gnus-highlight-selected-summary ()
9392   "Highlight selected article in summary buffer."
9393   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9394   (when gnus-summary-selected-face
9395     (save-excursion
9396       (let* ((beg (progn (beginning-of-line) (point)))
9397              (end (progn (end-of-line) (point)))
9398              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9399              (from (if (get-text-property beg gnus-mouse-face-prop)
9400                        beg
9401                      (or (next-single-property-change
9402                           beg gnus-mouse-face-prop nil end)
9403                          beg)))
9404              (to
9405               (if (= from end)
9406                   (- from 2)
9407                 (or (next-single-property-change
9408                      from gnus-mouse-face-prop nil end)
9409                     end))))
9410         ;; If no mouse-face prop on line we will have to = from = end,
9411         ;; so we highlight the entire line instead.
9412         (when (= (+ to 2) from)
9413           (setq from beg)
9414           (setq to end))
9415         (if gnus-newsgroup-selected-overlay
9416             ;; Move old overlay.
9417             (gnus-move-overlay
9418              gnus-newsgroup-selected-overlay from to (current-buffer))
9419           ;; Create new overlay.
9420           (gnus-overlay-put
9421            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9422            'face gnus-summary-selected-face))))))
9423
9424 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9425 (defun gnus-summary-highlight-line ()
9426   "Highlight current line according to `gnus-summary-highlight'."
9427   (let* ((list gnus-summary-highlight)
9428          (p (point))
9429          (end (progn (end-of-line) (point)))
9430          ;; now find out where the line starts and leave point there.
9431          (beg (progn (beginning-of-line) (point)))
9432          (article (gnus-summary-article-number))
9433          (score (or (cdr (assq (or article gnus-current-article)
9434                                gnus-newsgroup-scored))
9435                     gnus-summary-default-score 0))
9436          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9437          (inhibit-read-only t))
9438     ;; Eval the cars of the lists until we find a match.
9439     (let ((default gnus-summary-default-score))
9440       (while (and list
9441                   (not (eval (caar list))))
9442         (setq list (cdr list))))
9443     (let ((face (cdar list)))
9444       (unless (eq face (get-text-property beg 'face))
9445         (gnus-put-text-property-excluding-characters-with-faces
9446          beg end 'face
9447          (setq face (if (boundp face) (symbol-value face) face)))
9448         (when gnus-summary-highlight-line-function
9449           (funcall gnus-summary-highlight-line-function article face))))
9450     (goto-char p)))
9451
9452 (defun gnus-update-read-articles (group unread &optional compute)
9453   "Update the list of read articles in GROUP."
9454   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9455          (entry (gnus-gethash group gnus-newsrc-hashtb))
9456          (info (nth 2 entry))
9457          (prev 1)
9458          (unread (sort (copy-sequence unread) '<))
9459          read)
9460     (if (or (not info) (not active))
9461         ;; There is no info on this group if it was, in fact,
9462         ;; killed.  Gnus stores no information on killed groups, so
9463         ;; there's nothing to be done.
9464         ;; One could store the information somewhere temporarily,
9465         ;; perhaps...  Hmmm...
9466         ()
9467       ;; Remove any negative articles numbers.
9468       (while (and unread (< (car unread) 0))
9469         (setq unread (cdr unread)))
9470       ;; Remove any expired article numbers
9471       (while (and unread (< (car unread) (car active)))
9472         (setq unread (cdr unread)))
9473       ;; Compute the ranges of read articles by looking at the list of
9474       ;; unread articles.
9475       (while unread
9476         (when (/= (car unread) prev)
9477           (push (if (= prev (1- (car unread))) prev
9478                   (cons prev (1- (car unread))))
9479                 read))
9480         (setq prev (1+ (car unread)))
9481         (setq unread (cdr unread)))
9482       (when (<= prev (cdr active))
9483         (push (cons prev (cdr active)) read))
9484       (setq read (if (> (length read) 1) (nreverse read) read))
9485       (if compute
9486           read
9487         (save-excursion
9488           (set-buffer gnus-group-buffer)
9489           (gnus-undo-register
9490             `(progn
9491                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9492                (gnus-info-set-read ',info ',(gnus-info-read info))
9493                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9494                (gnus-group-update-group ,group t))))
9495         ;; Propagate the read marks to the backend.
9496         (if (gnus-check-backend-function 'request-set-mark group)
9497             (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9498                   (add (gnus-remove-from-range read (gnus-info-read info))))
9499               (when (or add del)
9500                 (unless (gnus-check-group group)
9501                   (error "Can't open server for %s" group))
9502                 (gnus-request-set-mark
9503                  group (delq nil (list (if add (list add 'add '(read)))
9504                                        (if del (list del 'del '(read)))))))))
9505         ;; Enter this list into the group info.
9506         (gnus-info-set-read info read)
9507         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9508         (gnus-get-unread-articles-in-group info (gnus-active group))
9509         t))))
9510
9511 (defun gnus-offer-save-summaries ()
9512   "Offer to save all active summary buffers."
9513   (save-excursion
9514     (let ((buflist (buffer-list))
9515           buffers bufname)
9516       ;; Go through all buffers and find all summaries.
9517       (while buflist
9518         (and (setq bufname (buffer-name (car buflist)))
9519              (string-match "Summary" bufname)
9520              (save-excursion
9521                (set-buffer bufname)
9522                ;; We check that this is, indeed, a summary buffer.
9523                (and (eq major-mode 'gnus-summary-mode)
9524                     ;; Also make sure this isn't bogus.
9525                     gnus-newsgroup-prepared
9526                     ;; Also make sure that this isn't a dead summary buffer.
9527                     (not gnus-dead-summary-mode)))
9528              (push bufname buffers))
9529         (setq buflist (cdr buflist)))
9530       ;; Go through all these summary buffers and offer to save them.
9531       (when buffers
9532         (map-y-or-n-p
9533          "Update summary buffer %s? "
9534          (lambda (buf)
9535            (switch-to-buffer buf)
9536            (gnus-summary-exit))
9537          buffers)))))
9538
9539 (defun gnus-summary-setup-default-charset ()
9540   "Setup newsgroup default charset."
9541   (if (equal gnus-newsgroup-name "nndraft:drafts")
9542       (setq gnus-newsgroup-charset nil)
9543     (let* ((name (and gnus-newsgroup-name
9544                       (gnus-group-real-name gnus-newsgroup-name)))
9545            (ignored-charsets
9546             (or gnus-newsgroup-ephemeral-ignored-charsets
9547                 (append
9548                  (and gnus-newsgroup-name
9549                       (or (gnus-group-find-parameter gnus-newsgroup-name
9550                                                      'ignored-charsets t)
9551                           (let ((alist gnus-group-ignored-charsets-alist)
9552                                 elem (charsets nil))
9553                             (while (setq elem (pop alist))
9554                               (when (and name
9555                                          (string-match (car elem) name))
9556                                 (setq alist nil
9557                                       charsets (cdr elem))))
9558                             charsets)))
9559                  gnus-newsgroup-ignored-charsets))))
9560       (setq gnus-newsgroup-charset
9561             (or gnus-newsgroup-ephemeral-charset
9562                 (and gnus-newsgroup-name
9563                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9564                          (let ((alist gnus-group-charset-alist)
9565                                elem charset)
9566                            (while (setq elem (pop alist))
9567                              (when (and name
9568                                         (string-match (car elem) name))
9569                                (setq alist nil
9570                                      charset (cadr elem))))
9571                            charset)))
9572                 gnus-default-charset))
9573       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9574            ignored-charsets))))
9575
9576 ;;;
9577 ;;; Mime Commands
9578 ;;;
9579
9580 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9581   "Display the current article buffer fully MIME-buttonized.
9582 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9583 treated as multipart/mixed."
9584   (interactive "P")
9585   (require 'gnus-art)
9586   (let ((gnus-unbuttonized-mime-types nil)
9587         (gnus-mime-display-multipart-as-mixed show-all-parts))
9588     (gnus-summary-show-article)))
9589
9590 (defun gnus-summary-repair-multipart (article)
9591   "Add a Content-Type header to a multipart article without one."
9592   (interactive (list (gnus-summary-article-number)))
9593   (gnus-with-article article
9594     (message-narrow-to-head)
9595     (goto-char (point-max))
9596     (widen)
9597     (when (search-forward "\n--" nil t)
9598       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9599         (message-narrow-to-head)
9600         (message-remove-header "Mime-Version")
9601         (message-remove-header "Content-Type")
9602         (goto-char (point-max))
9603         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9604                         separator))
9605         (insert "Mime-Version: 1.0\n")
9606         (widen))))
9607   (let (gnus-mark-article-hook)
9608     (gnus-summary-select-article t t nil article)))
9609
9610 (defun gnus-summary-toggle-display-buttonized ()
9611   "Toggle the buttonizing of the article buffer."
9612   (interactive)
9613   (require 'gnus-art)
9614   (if (setq gnus-inhibit-mime-unbuttonizing
9615             (not gnus-inhibit-mime-unbuttonizing))
9616       (let ((gnus-unbuttonized-mime-types nil))
9617         (gnus-summary-show-article))
9618     (gnus-summary-show-article)))
9619
9620 ;;;
9621 ;;; Generic summary marking commands
9622 ;;;
9623
9624 (defvar gnus-summary-marking-alist
9625   '((read gnus-del-mark "d")
9626     (unread gnus-unread-mark "u")
9627     (ticked gnus-ticked-mark "!")
9628     (dormant gnus-dormant-mark "?")
9629     (expirable gnus-expirable-mark "e"))
9630   "An alist of names/marks/keystrokes.")
9631
9632 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9633 (defvar gnus-summary-mark-map)
9634
9635 (defun gnus-summary-make-all-marking-commands ()
9636   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9637   (dolist (elem gnus-summary-marking-alist)
9638     (apply 'gnus-summary-make-marking-command elem)))
9639
9640 (defun gnus-summary-make-marking-command (name mark keystroke)
9641   (let ((map (make-sparse-keymap)))
9642     (define-key gnus-summary-generic-mark-map keystroke map)
9643     (dolist (lway `((next "next" next nil "n")
9644                     (next-unread "next unread" next t "N")
9645                     (prev "previous" prev nil "p")
9646                     (prev-unread "previous unread" prev t "P")
9647                     (nomove "" nil nil ,keystroke)))
9648       (let ((func (gnus-summary-make-marking-command-1
9649                    mark (car lway) lway name)))
9650         (setq func (eval func))
9651         (define-key map (nth 4 lway) func)))))
9652       
9653 (defun gnus-summary-make-marking-command-1 (mark way lway name)
9654   `(defun ,(intern
9655             (format "gnus-summary-put-mark-as-%s%s"
9656                     name (if (eq way 'nomove)
9657                              ""
9658                            (concat "-" (symbol-name way)))))
9659      (n)
9660      ,(format
9661        "Mark the current article as %s%s.
9662 If N, the prefix, then repeat N times.
9663 If N is negative, move in reverse order.
9664 The difference between N and the actual number of articles marked is
9665 returned."
9666        name (cadr lway))
9667      (interactive "p")
9668      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9669     
9670 (defun gnus-summary-generic-mark (n mark move unread)
9671   "Mark N articles with MARK."
9672   (unless (eq major-mode 'gnus-summary-mode)
9673     (error "This command can only be used in the summary buffer"))
9674   (gnus-summary-show-thread)
9675   (let ((nummove
9676          (cond
9677           ((eq move 'next) 1)
9678           ((eq move 'prev) -1)
9679           (t 0))))
9680     (if (zerop nummove)
9681         (setq n 1)
9682       (when (< n 0)
9683         (setq n (abs n)
9684               nummove (* -1 nummove))))
9685     (while (and (> n 0)
9686                 (gnus-summary-mark-article nil mark)
9687                 (zerop (gnus-summary-next-subject nummove unread t)))
9688       (setq n (1- n)))
9689     (when (/= 0 n)
9690       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9691     (gnus-summary-recenter)
9692     (gnus-summary-position-point)
9693     (gnus-set-mode-line 'summary)
9694     n))
9695
9696 (gnus-summary-make-all-marking-commands)
9697
9698 (gnus-ems-redefine)
9699
9700 (provide 'gnus-sum)
9701
9702 (run-hooks 'gnus-sum-load-hook)
9703
9704 ;;; gnus-sum.el ends here