Sync up with Pterodactyl Gnus v0.99.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
7 ;; Keywords: mail, news, MIME
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
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 'mime-view)
39
40 (eval-when-compile
41   (require 'mime-play)
42   (require 'static))
43
44 (eval-and-compile
45   (autoload 'gnus-cache-articles-in-group "gnus-cache"))
46
47 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
48 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
49
50 (defcustom gnus-kill-summary-on-exit t
51   "*If non-nil, kill the summary buffer when you exit from it.
52 If nil, the summary will become a \"*Dead Summary*\" buffer, and
53 it will be killed sometime later."
54   :group 'gnus-summary-exit
55   :type 'boolean)
56
57 (defcustom gnus-fetch-old-headers nil
58   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
59 If an unread article in the group refers to an older, already read (or
60 just marked as read) article, the old article will not normally be
61 displayed in the Summary buffer.  If this variable is non-nil, Gnus
62 will attempt to grab the headers to the old articles, and thereby
63 build complete threads.  If it has the value `some', only enough
64 headers to connect otherwise loose threads will be displayed.  This
65 variable can also be a number.  In that case, no more than that number
66 of old headers will be fetched.  If it has the value `invisible', all
67 old headers will be fetched, but none will be displayed.
68
69 The server has to support NOV for any of this to work."
70   :group 'gnus-thread
71   :type '(choice (const :tag "off" nil)
72                  (const some)
73                  number
74                  (sexp :menu-tag "other" t)))
75
76 (defcustom gnus-refer-thread-limit 200
77   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
78 If t, fetch all the available old headers."
79   :group 'gnus-thread
80   :type '(choice number
81                  (sexp :menu-tag "other" t)))
82
83 (defcustom gnus-summary-make-false-root 'adopt
84   "*nil means that Gnus won't gather loose threads.
85 If the root of a thread has expired or been read in a previous
86 session, the information necessary to build a complete thread has been
87 lost.  Instead of having many small sub-threads from this original thread
88 scattered all over the summary buffer, Gnus can gather them.
89
90 If non-nil, Gnus will try to gather all loose sub-threads from an
91 original thread into one large thread.
92
93 If this variable is non-nil, it should be one of `none', `adopt',
94 `dummy' or `empty'.
95
96 If this variable is `none', Gnus will not make a false root, but just
97 present the sub-threads after another.
98 If this variable is `dummy', Gnus will create a dummy root that will
99 have all the sub-threads as children.
100 If this variable is `adopt', Gnus will make one of the \"children\"
101 the parent and mark all the step-children as such.
102 If this variable is `empty', the \"children\" are printed with empty
103 subject fields.  (Or rather, they will be printed with a string
104 given by the `gnus-summary-same-subject' variable.)"
105   :group 'gnus-thread
106   :type '(choice (const :tag "off" nil)
107                  (const none)
108                  (const dummy)
109                  (const adopt)
110                  (const empty)))
111
112 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
113   "*A regexp to match subjects to be excluded from loose thread gathering.
114 As loose thread gathering is done on subjects only, that means that
115 there can be many false gatherings performed.  By rooting out certain
116 common subjects, gathering might become saner."
117   :group 'gnus-thread
118   :type 'regexp)
119
120 (defcustom gnus-summary-gather-subject-limit nil
121   "*Maximum length of subject comparisons when gathering loose threads.
122 Use nil to compare full subjects.  Setting this variable to a low
123 number will help gather threads that have been corrupted by
124 newsreaders chopping off subject lines, but it might also mean that
125 unrelated articles that have subject that happen to begin with the
126 same few characters will be incorrectly gathered.
127
128 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
129 comparing subjects."
130   :group 'gnus-thread
131   :type '(choice (const :tag "off" nil)
132                  (const fuzzy)
133                  (sexp :menu-tag "on" t)))
134
135 (defcustom gnus-simplify-subject-functions nil
136   "List of functions taking a string argument that simplify subjects.
137 The functions are applied recursively.
138
139 Useful functions to put in this list include: `gnus-simplify-subject-re',
140 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
141   :group 'gnus-thread
142   :type '(repeat function))
143
144 (defcustom gnus-simplify-ignored-prefixes nil
145   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
146   :group 'gnus-thread
147   :type '(choice (const :tag "off" nil)
148                  regexp))
149
150 (defcustom gnus-build-sparse-threads nil
151   "*If non-nil, fill in the gaps in threads.
152 If `some', only fill in the gaps that are needed to tie loose threads
153 together.  If `more', fill in all leaf nodes that Gnus can find.  If
154 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
155   :group 'gnus-thread
156   :type '(choice (const :tag "off" nil)
157                  (const some)
158                  (const more)
159                  (sexp :menu-tag "all" t)))
160
161 (defcustom gnus-summary-thread-gathering-function
162   'gnus-gather-threads-by-subject
163   "*Function used for gathering loose threads.
164 There are two pre-defined functions: `gnus-gather-threads-by-subject',
165 which only takes Subjects into consideration; and
166 `gnus-gather-threads-by-references', which compared the References
167 headers of the articles to find matches."
168   :group 'gnus-thread
169   :type '(radio (function-item gnus-gather-threads-by-subject)
170                 (function-item gnus-gather-threads-by-references)
171                 (function :tag "other")))
172
173 (defcustom gnus-summary-same-subject ""
174   "*String indicating that the current article has the same subject as the previous.
175 This variable will only be used if the value of
176 `gnus-summary-make-false-root' is `empty'."
177   :group 'gnus-summary-format
178   :type 'string)
179
180 (defcustom gnus-summary-goto-unread t
181   "*If t, marking commands will go to the next unread article.
182 If `never', commands that usually go to the next unread article, will
183 go to the next article, whether it is read or not.
184 If nil, only the marking commands will go to the next (un)read article."
185   :group 'gnus-summary-marks
186   :link '(custom-manual "(gnus)Setting Marks")
187   :type '(choice (const :tag "off" nil)
188                  (const never)
189                  (sexp :menu-tag "on" t)))
190
191 (defcustom gnus-summary-default-score 0
192   "*Default article score level.
193 All scores generated by the score files will be added to this score.
194 If this variable is nil, scoring will be disabled."
195   :group 'gnus-score-default
196   :type '(choice (const :tag "disable")
197                  integer))
198
199 (defcustom gnus-summary-zcore-fuzz 0
200   "*Fuzziness factor for the zcore in the summary buffer.
201 Articles with scores closer than this to `gnus-summary-default-score'
202 will not be marked."
203   :group 'gnus-summary-format
204   :type 'integer)
205
206 (defcustom gnus-simplify-subject-fuzzy-regexp nil
207   "*Strings to be removed when doing fuzzy matches.
208 This can either be a regular expression or list of regular expressions
209 that will be removed from subject strings if fuzzy subject
210 simplification is selected."
211   :group 'gnus-thread
212   :type '(repeat regexp))
213
214 (defcustom gnus-show-threads t
215   "*If non-nil, display threads in summary mode."
216   :group 'gnus-thread
217   :type 'boolean)
218
219 (defcustom gnus-thread-hide-subtree nil
220   "*If non-nil, hide all threads initially.
221 If threads are hidden, you have to run the command
222 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
223 to expose hidden threads."
224   :group 'gnus-thread
225   :type 'boolean)
226
227 (defcustom gnus-thread-hide-killed t
228   "*If non-nil, hide killed threads automatically."
229   :group 'gnus-thread
230   :type 'boolean)
231
232 (defcustom gnus-thread-ignore-subject t
233   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
234 If nil, articles that have different subjects from their parents will
235 start separate threads."
236   :group 'gnus-thread
237   :type 'boolean)
238
239 (defcustom gnus-thread-operation-ignore-subject t
240   "*If non-nil, subjects will be ignored when doing thread commands.
241 This affects commands like `gnus-summary-kill-thread' and
242 `gnus-summary-lower-thread'.
243
244 If this variable is nil, articles in the same thread with different
245 subjects will not be included in the operation in question.  If this
246 variable is `fuzzy', only articles that have subjects that are fuzzily
247 equal will be included."
248   :group 'gnus-thread
249   :type '(choice (const :tag "off" nil)
250                  (const fuzzy)
251                  (sexp :tag "on" t)))
252
253 (defcustom gnus-thread-indent-level 4
254   "*Number that says how much each sub-thread should be indented."
255   :group 'gnus-thread
256   :type 'integer)
257
258 (defcustom gnus-auto-extend-newsgroup t
259   "*If non-nil, extend newsgroup forward and backward when requested."
260   :group 'gnus-summary-choose
261   :type 'boolean)
262
263 (defcustom gnus-auto-select-first t
264   "*If nil, don't select the first unread article when entering a group.
265 If this variable is `best', select the highest-scored unread article
266 in the group.  If t, select the first unread article.
267
268 This variable can also be a function to place point on a likely
269 subject line.  Useful values include `gnus-summary-first-unread-subject',
270 `gnus-summary-first-unread-article' and
271 `gnus-summary-best-unread-article'.
272
273 If you want to prevent automatic selection of the first unread article
274 in some newsgroups, set the variable to nil in
275 `gnus-select-group-hook'."
276   :group 'gnus-group-select
277   :type '(choice (const :tag "none" nil)
278                  (const best)
279                  (sexp :menu-tag "first" t)
280                  (function-item gnus-summary-first-unread-subject)
281                  (function-item gnus-summary-first-unread-article)
282                  (function-item gnus-summary-best-unread-article)))
283
284 (defcustom gnus-dont-select-after-jump-to-other-group nil
285   "If non-nil, don't select the first unread article after entering the
286 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
287 it is depend on the value of `gnus-auto-select-first' whether to select
288 or not."
289   :group 'gnus-group-select
290   :type 'boolean)
291
292 (defcustom gnus-auto-select-next t
293   "*If non-nil, offer to go to the next group from the end of the previous.
294 If the value is t and the next newsgroup is empty, Gnus will exit
295 summary mode and go back to group mode.  If the value is neither nil
296 nor t, Gnus will select the following unread newsgroup.  In
297 particular, if the value is the symbol `quietly', the next unread
298 newsgroup will be selected without any confirmation, and if it is
299 `almost-quietly', the next group will be selected without any
300 confirmation if you are located on the last article in the group.
301 Finally, if this variable is `slightly-quietly', the `Z n' command
302 will go to the next group without confirmation."
303   :group 'gnus-summary-maneuvering
304   :type '(choice (const :tag "off" nil)
305                  (const quietly)
306                  (const almost-quietly)
307                  (const slightly-quietly)
308                  (sexp :menu-tag "on" t)))
309
310 (defcustom gnus-auto-select-same nil
311   "*If non-nil, select the next article with the same subject.
312 If there are no more articles with the same subject, go to
313 the first unread article."
314   :group 'gnus-summary-maneuvering
315   :type 'boolean)
316
317 (defcustom gnus-summary-check-current nil
318   "*If non-nil, consider the current article when moving.
319 The \"unread\" movement commands will stay on the same line if the
320 current article is unread."
321   :group 'gnus-summary-maneuvering
322   :type 'boolean)
323
324 (defcustom gnus-auto-center-summary t
325   "*If non-nil, always center the current summary buffer.
326 In particular, if `vertical' do only vertical recentering.  If non-nil
327 and non-`vertical', do both horizontal and vertical recentering."
328   :group 'gnus-summary-maneuvering
329   :type '(choice (const :tag "none" nil)
330                  (const vertical)
331                  (integer :tag "height")
332                  (sexp :menu-tag "both" t)))
333
334 (defcustom gnus-show-all-headers nil
335   "*If non-nil, don't hide any headers."
336   :group 'gnus-article-hiding
337   :group 'gnus-article-headers
338   :type 'boolean)
339
340 (defcustom gnus-summary-ignore-duplicates nil
341   "*If non-nil, ignore articles with identical Message-ID headers."
342   :group 'gnus-summary
343   :type 'boolean)
344
345 (defcustom gnus-single-article-buffer t
346   "*If non-nil, display all articles in the same buffer.
347 If nil, each group will get its own article buffer."
348   :group 'gnus-article-various
349   :type 'boolean)
350
351 (defcustom gnus-break-pages t
352   "*If non-nil, do page breaking on articles.
353 The page delimiter is specified by the `gnus-page-delimiter'
354 variable."
355   :group 'gnus-article-various
356   :type 'boolean)
357
358 (defcustom gnus-show-mime t
359   "*If non-nil, do mime processing of articles.
360 The articles will simply be fed to the function given by
361 `gnus-article-display-method-for-mime'."
362   :group 'gnus-article-mime
363   :type 'boolean)
364
365 (defcustom gnus-move-split-methods nil
366   "*Variable used to suggest where articles are to be moved to.
367 It uses the same syntax as the `gnus-split-methods' variable."
368   :group 'gnus-summary-mail
369   :type '(repeat (choice (list :value (fun) function)
370                          (cons :value ("" "") regexp (repeat string))
371                          (sexp :value nil))))
372
373 (defcustom gnus-unread-mark ? ;Whitespace
374   "*Mark used for unread articles."
375   :group 'gnus-summary-marks
376   :type 'character)
377
378 (defcustom gnus-ticked-mark ?!
379   "*Mark used for ticked articles."
380   :group 'gnus-summary-marks
381   :type 'character)
382
383 (defcustom gnus-dormant-mark ??
384   "*Mark used for dormant articles."
385   :group 'gnus-summary-marks
386   :type 'character)
387
388 (defcustom gnus-del-mark ?r
389   "*Mark used for del'd articles."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-read-mark ?R
394   "*Mark used for read articles."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-expirable-mark ?E
399   "*Mark used for expirable articles."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-killed-mark ?K
404   "*Mark used for killed articles."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-souped-mark ?F
409   "*Mark used for killed articles."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-kill-file-mark ?X
414   "*Mark used for articles killed by kill files."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-low-score-mark ?Y
419   "*Mark used for articles with a low score."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-catchup-mark ?C
424   "*Mark used for articles that are caught up."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-replied-mark ?A
429   "*Mark used for articles that have been replied to."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-cached-mark ?*
434   "*Mark used for articles that are in the cache."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-saved-mark ?S
439   "*Mark used for articles that have been saved to."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-ancient-mark ?O
444   "*Mark used for ancient articles."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-sparse-mark ?Q
449   "*Mark used for sparsely reffed articles."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-canceled-mark ?G
454   "*Mark used for canceled articles."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-duplicate-mark ?M
459   "*Mark used for duplicate articles."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-undownloaded-mark ?@
464   "*Mark used for articles that weren't downloaded."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-downloadable-mark ?%
469   "*Mark used for articles that are to be downloaded."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-unsendable-mark ?=
474   "*Mark used for articles that won't be sent."
475   :group 'gnus-summary-marks
476   :type 'character)
477
478 (defcustom gnus-score-over-mark ?+
479   "*Score mark used for articles with high scores."
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-score-below-mark ?-
484   "*Score mark used for articles with low scores."
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-empty-thread-mark ? ;Whitespace
489   "*There is no thread under the article."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-not-empty-thread-mark ?=
494   "*There is a thread under the article."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-view-pseudo-asynchronously nil
499   "*If non-nil, Gnus will view pseudo-articles asynchronously."
500   :group 'gnus-extract-view
501   :type 'boolean)
502
503 (defcustom gnus-auto-expirable-marks
504   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
505         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
506         gnus-souped-mark gnus-duplicate-mark)
507   "*The list of marks converted into expiration if a group is auto-expirable."
508   :group 'gnus-summary
509   :type '(repeat character))
510
511 (defcustom gnus-inhibit-user-auto-expire t
512   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
513   :group 'gnus-summary
514   :type 'boolean)
515
516 (defcustom gnus-view-pseudos nil
517   "*If `automatic', pseudo-articles will be viewed automatically.
518 If `not-confirm', pseudos will be viewed automatically, and the user
519 will not be asked to confirm the command."
520   :group 'gnus-extract-view
521   :type '(choice (const :tag "off" nil)
522                  (const automatic)
523                  (const not-confirm)))
524
525 (defcustom gnus-view-pseudos-separately t
526   "*If non-nil, one pseudo-article will be created for each file to be viewed.
527 If nil, all files that use the same viewing command will be given as a
528 list of parameters to that command."
529   :group 'gnus-extract-view
530   :type 'boolean)
531
532 (defcustom gnus-insert-pseudo-articles t
533   "*If non-nil, insert pseudo-articles when decoding articles."
534   :group 'gnus-extract-view
535   :type 'boolean)
536
537 (defcustom gnus-summary-dummy-line-format
538   "  %(:                          :%) %S\n"
539   "*The format specification for the dummy roots in the summary buffer.
540 It works along the same lines as a normal formatting string,
541 with some simple extensions.
542
543 %S  The subject"
544   :group 'gnus-threading
545   :type 'string)
546
547 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
548   "*The format specification for the summary mode line.
549 It works along the same lines as a normal formatting string,
550 with some simple extensions:
551
552 %G  Group name
553 %p  Unprefixed group name
554 %A  Current article number
555 %z  Current article score
556 %V  Gnus version
557 %U  Number of unread articles in the group
558 %e  Number of unselected articles in the group
559 %Z  A string with unread/unselected article counts
560 %g  Shortish group name
561 %S  Subject of the current article
562 %u  User-defined spec
563 %s  Current score file name
564 %d  Number of dormant articles
565 %r  Number of articles that have been marked as read in this session
566 %E  Number of articles expunged by the score files"
567   :group 'gnus-summary-format
568   :type 'string)
569
570 (defcustom gnus-list-identifiers nil
571   "Regexp that matches list identifiers to be removed from subject.
572 This can also be a list of regexps."
573   :group 'gnus-summary-format
574   :group 'gnus-article-hiding
575   :type '(choice (const :tag "none" nil)
576                  (regexp :value ".*")
577                  (repeat :value (".*") regexp)))
578
579 (defcustom gnus-summary-mark-below 0
580   "*Mark all articles with a score below this variable as read.
581 This variable is local to each summary buffer and usually set by the
582 score file."
583   :group 'gnus-score-default
584   :type 'integer)
585
586 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
587   "*List of functions used for sorting articles in the summary buffer.
588 This variable is only used when not using a threaded display."
589   :group 'gnus-summary-sort
590   :type '(repeat (choice (function-item gnus-article-sort-by-number)
591                          (function-item gnus-article-sort-by-author)
592                          (function-item gnus-article-sort-by-subject)
593                          (function-item gnus-article-sort-by-date)
594                          (function-item gnus-article-sort-by-score)
595                          (function :tag "other"))))
596
597 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
598   "*List of functions used for sorting threads in the summary buffer.
599 By default, threads are sorted by article number.
600
601 Each function takes two threads and return non-nil if the first thread
602 should be sorted before the other.  If you use more than one function,
603 the primary sort function should be the last.  You should probably
604 always include `gnus-thread-sort-by-number' in the list of sorting
605 functions -- preferably first.
606
607 Ready-made functions include `gnus-thread-sort-by-number',
608 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
609 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
610 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
611   :group 'gnus-summary-sort
612   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
613                          (function-item gnus-thread-sort-by-author)
614                          (function-item gnus-thread-sort-by-subject)
615                          (function-item gnus-thread-sort-by-date)
616                          (function-item gnus-thread-sort-by-score)
617                          (function-item gnus-thread-sort-by-total-score)
618                          (function :tag "other"))))
619
620 (defcustom gnus-thread-score-function '+
621   "*Function used for calculating the total score of a thread.
622
623 The function is called with the scores of the article and each
624 subthread and should then return the score of the thread.
625
626 Some functions you can use are `+', `max', or `min'."
627   :group 'gnus-summary-sort
628   :type 'function)
629
630 (defcustom gnus-summary-expunge-below nil
631   "All articles that have a score less than this variable will be expunged.
632 This variable is local to the summary buffers."
633   :group 'gnus-score-default
634   :type '(choice (const :tag "off" nil)
635                  integer))
636
637 (defcustom gnus-thread-expunge-below nil
638   "All threads that have a total score less than this variable will be expunged.
639 See `gnus-thread-score-function' for en explanation of what a
640 \"thread score\" is.
641
642 This variable is local to the summary buffers."
643   :group 'gnus-threading
644   :group 'gnus-score-default
645   :type '(choice (const :tag "off" nil)
646                  integer))
647
648 (defcustom gnus-summary-mode-hook nil
649   "*A hook for Gnus summary mode.
650 This hook is run before any variables are set in the summary buffer."
651   :group 'gnus-summary-various
652   :type 'hook)
653
654 (defcustom gnus-summary-menu-hook nil
655   "*Hook run after the creation of the summary mode menu."
656   :group 'gnus-summary-visual
657   :type 'hook)
658
659 (defcustom gnus-summary-exit-hook nil
660   "*A hook called on exit from the summary buffer.
661 It will be called with point in the group buffer."
662   :group 'gnus-summary-exit
663   :type 'hook)
664
665 (defcustom gnus-summary-prepare-hook nil
666   "*A hook called after the summary buffer has been generated.
667 If you want to modify the summary buffer, you can use this hook."
668   :group 'gnus-summary-various
669   :type 'hook)
670
671 (defcustom gnus-summary-prepared-hook nil
672   "*A hook called as the last thing after the summary buffer has been generated."
673   :group 'gnus-summary-various
674   :type 'hook)
675
676 (defcustom gnus-summary-generate-hook nil
677   "*A hook run just before generating the summary buffer.
678 This hook is commonly used to customize threading variables and the
679 like."
680   :group 'gnus-summary-various
681   :type 'hook)
682
683 (defcustom gnus-select-group-hook nil
684   "*A hook called when a newsgroup is selected.
685
686 If you'd like to simplify subjects like the
687 `gnus-summary-next-same-subject' command does, you can use the
688 following hook:
689
690  (setq gnus-select-group-hook
691       (list
692         (lambda ()
693           (mapcar (lambda (header)
694                      (mail-header-set-subject
695                       header
696                       (gnus-simplify-subject
697                        (mail-header-subject header) 're-only)))
698                   gnus-newsgroup-headers))))"
699   :group 'gnus-group-select
700   :type 'hook)
701
702 (defcustom gnus-select-article-hook nil
703   "*A hook called when an article is selected."
704   :group 'gnus-summary-choose
705   :type 'hook)
706
707 (defcustom gnus-visual-mark-article-hook
708   (list 'gnus-highlight-selected-summary)
709   "*Hook run after selecting an article in the summary buffer.
710 It is meant to be used for highlighting the article in some way.  It
711 is not run if `gnus-visual' is nil."
712   :group 'gnus-summary-visual
713   :type 'hook)
714
715 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
716   "*A hook called before parsing the headers."
717   :group 'gnus-various
718   :type 'hook)
719
720 (defcustom gnus-exit-group-hook nil
721   "*A hook called when exiting (not quitting) summary mode."
722   :group 'gnus-various
723   :type 'hook)
724
725 (defcustom gnus-summary-update-hook
726   (list 'gnus-summary-highlight-line)
727   "*A hook called when a summary line is changed.
728 The hook will not be called if `gnus-visual' is nil.
729
730 The default function `gnus-summary-highlight-line' will
731 highlight the line according to the `gnus-summary-highlight'
732 variable."
733   :group 'gnus-summary-visual
734   :type 'hook)
735
736 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
737   "*A hook called when an article is selected for the first time.
738 The hook is intended to mark an article as read (or unread)
739 automatically when it is selected."
740   :group 'gnus-summary-choose
741   :type 'hook)
742
743 (defcustom gnus-group-no-more-groups-hook nil
744   "*A hook run when returning to group mode having no more (unread) groups."
745   :group 'gnus-group-select
746   :type 'hook)
747
748 (defcustom gnus-ps-print-hook nil
749   "*A hook run before ps-printing something from Gnus."
750   :group 'gnus-summary
751   :type 'hook)
752
753 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
754   "Face used for highlighting the current article in the summary buffer."
755   :group 'gnus-summary-visual
756   :type 'face)
757
758 (defcustom gnus-summary-highlight
759   '(((= mark gnus-canceled-mark)
760      . gnus-summary-cancelled-face)
761     ((and (> score default)
762           (or (= mark gnus-dormant-mark)
763               (= mark gnus-ticked-mark)))
764      . gnus-summary-high-ticked-face)
765     ((and (< score default)
766           (or (= mark gnus-dormant-mark)
767               (= mark gnus-ticked-mark)))
768      . gnus-summary-low-ticked-face)
769     ((or (= mark gnus-dormant-mark)
770          (= mark gnus-ticked-mark))
771      . gnus-summary-normal-ticked-face)
772     ((and (> score default) (= mark gnus-ancient-mark))
773      . gnus-summary-high-ancient-face)
774     ((and (< score default) (= mark gnus-ancient-mark))
775      . gnus-summary-low-ancient-face)
776     ((= mark gnus-ancient-mark)
777      . gnus-summary-normal-ancient-face)
778     ((and (> score default) (= mark gnus-unread-mark))
779      . gnus-summary-high-unread-face)
780     ((and (< score default) (= mark gnus-unread-mark))
781      . gnus-summary-low-unread-face)
782     ((and (memq article gnus-newsgroup-incorporated) 
783           (= mark gnus-unread-mark))
784      . gnus-summary-incorporated-face)
785     ((= mark gnus-unread-mark)
786      . gnus-summary-normal-unread-face)
787     ((and (> score default) (memq mark (list gnus-downloadable-mark
788                                              gnus-undownloaded-mark)))
789      . gnus-summary-high-unread-face)
790     ((and (< score default) (memq mark (list gnus-downloadable-mark
791                                              gnus-undownloaded-mark)))
792      . gnus-summary-low-unread-face)
793     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
794      . gnus-summary-normal-unread-face)
795     ((> score default)
796      . gnus-summary-high-read-face)
797     ((< score default)
798      . gnus-summary-low-read-face)
799     (t
800      . gnus-summary-normal-read-face))
801   "*Controls the highlighting of summary buffer lines.
802
803 A list of (FORM . FACE) pairs.  When deciding how a a particular
804 summary line should be displayed, each form is evaluated.  The content
805 of the face field after the first true form is used.  You can change
806 how those summary lines are displayed, by editing the face field.
807
808 You can use the following variables in the FORM field.
809
810 score:   The articles score
811 default: The default article score.
812 below:   The score below which articles are automatically marked as read.
813 mark:    The articles mark."
814   :group 'gnus-summary-visual
815   :type '(repeat (cons (sexp :tag "Form" nil)
816                        face)))
817
818 (defcustom gnus-alter-header-function nil
819   "Function called to allow alteration of article header structures.
820 The function is called with one parameter, the article header vector,
821 which it may alter in any way.")
822
823 (defvar gnus-decode-encoded-word-function
824   (mime-find-field-decoder 'From 'nov)
825   "Variable that says which function should be used to decode a string with encoded words.")
826
827 (defcustom gnus-extra-headers nil
828   "*Extra headers to parse."
829   :group 'gnus-summary
830   :type '(repeat symbol))
831
832 (defcustom gnus-ignored-from-addresses
833   (and user-mail-address (regexp-quote user-mail-address))
834   "*Regexp of From headers that may be suppressed in favor of To headers."
835   :group 'gnus-summary
836   :type 'regexp)
837
838 (defcustom gnus-group-charset-alist
839   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
840     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
841     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
842     ("^relcom\\>" koi8-r)
843     ("^fido7\\>" koi8-r)
844     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
845     ("^israel\\>" iso-8859-1)
846     ("^han\\>" euc-kr)
847     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
848     (".*" iso-8859-1))
849   "Alist of regexps (to match group names) and default charsets to be used when reading."
850   :type '(repeat (list (regexp :tag "Group")
851                        (symbol :tag "Charset")))
852   :group 'gnus-charset)
853
854 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
855   "List of charsets that should be ignored.
856 When these charsets are used in the \"charset\" parameter, the
857 default charset will be used instead."
858   :type '(repeat symbol)
859   :group 'gnus-charset)
860
861 (defcustom gnus-group-ignored-charsets-alist 
862   '(("alt\\.chinese\\.text" iso-8859-1))
863   "Alist of regexps (to match group names) and charsets that should be ignored.
864 When these charsets are used in the \"charset\" parameter, the
865 default charset will be used instead."
866   :type '(repeat (cons (regexp :tag "Group")
867                        (repeat symbol)))
868   :group 'gnus-charset)
869
870 (defcustom gnus-group-highlight-words-alist nil
871   "Alist of group regexps and highlight regexps.
872 This variable uses the same syntax as `gnus-emphasis-alist'."
873   :type '(repeat (cons (regexp :tag "Group")
874                        (repeat (list (regexp :tag "Highlight regexp")
875                                      (number :tag "Group for entire word" 0)
876                                      (number :tag "Group for displayed part" 0)
877                                      (symbol :tag "Face" 
878                                              gnus-emphasis-highlight-words)))))
879   :group 'gnus-summary-visual)
880
881 (defcustom gnus-use-wheel nil
882   "Use Intelli-mouse on summary movement"
883   :type 'boolean
884   :group 'gnus-summary-maneuvering)
885
886 (defcustom gnus-wheel-scroll-amount '(5 . 1)
887   "Amount to scroll messages by spinning the mouse wheel.
888 This is actually a cons cell, where the first item is the amount to scroll
889 on a normal wheel event, and the second is the amount to scroll when the
890 wheel is moved with the shift key depressed."
891   :type '(cons (integer :tag "Shift") integer)
892   :group 'gnus-summary-maneuvering)
893
894 (defcustom gnus-wheel-edge-resistance 2
895   "How hard it should be to change the current article
896 by moving the mouse over the edge of the article window."
897   :type 'integer
898   :group 'gnus-summary-maneuvering)
899
900 ;;; Internal variables
901
902 (defvar gnus-scores-exclude-files nil)
903 (defvar gnus-page-broken nil)
904 (defvar gnus-inhibit-mime-unbuttonizing nil)
905
906 (defvar gnus-original-article nil)
907 (defvar gnus-article-internal-prepare-hook nil)
908 (defvar gnus-newsgroup-process-stack nil)
909
910 (defvar gnus-thread-indent-array nil)
911 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
912 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
913   "Function called to sort the articles within a thread after it has 
914 been gathered together.")
915
916 ;; Avoid highlighting in kill files.
917 (defvar gnus-summary-inhibit-highlight nil)
918 (defvar gnus-newsgroup-selected-overlay nil)
919 (defvar gnus-inhibit-limiting nil)
920 (defvar gnus-newsgroup-adaptive-score-file nil)
921 (defvar gnus-current-score-file nil)
922 (defvar gnus-current-move-group nil)
923 (defvar gnus-current-copy-group nil)
924 (defvar gnus-current-crosspost-group nil)
925
926 (defvar gnus-newsgroup-dependencies nil)
927 (defvar gnus-newsgroup-adaptive nil)
928 (defvar gnus-summary-display-article-function nil)
929 (defvar gnus-summary-highlight-line-function nil
930   "Function called after highlighting a summary line.")
931
932 (defvar gnus-summary-line-format-alist
933   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
934     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
935     (?s gnus-tmp-subject-or-nil ?s)
936     (?n gnus-tmp-name ?s)
937     (?A (std11-address-string
938          (car (mime-read-field 'From gnus-tmp-header))) ?s)
939     (?a (or (std11-full-name-string
940              (car (mime-read-field 'From gnus-tmp-header)))
941             gnus-tmp-from) ?s)
942     (?F gnus-tmp-from ?s)
943     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
944     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
945     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
946     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
947     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
948     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
949     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
950     (?L gnus-tmp-lines ?d)
951     (?I gnus-tmp-indentation ?s)
952     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
953     (?R gnus-tmp-replied ?c)
954     (?\[ gnus-tmp-opening-bracket ?c)
955     (?\] gnus-tmp-closing-bracket ?c)
956     (?\> (make-string gnus-tmp-level ? ) ?s)
957     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
958     (?i gnus-tmp-score ?d)
959     (?z gnus-tmp-score-char ?c)
960     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
961     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
962     (?U gnus-tmp-unread ?c)
963     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
964     (?t (gnus-summary-number-of-articles-in-thread
965          (and (boundp 'thread) (car thread)) gnus-tmp-level)
966         ?d)
967     (?e (gnus-summary-number-of-articles-in-thread
968          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
969         ?c)
970     (?u gnus-tmp-user-defined ?s)
971     (?P (gnus-pick-line-number) ?d))
972   "An alist of format specifications that can appear in summary lines,
973 and what variables they correspond with, along with the type of the
974 variable (string, integer, character, etc).")
975
976 (defvar gnus-summary-dummy-line-format-alist
977   `((?S gnus-tmp-subject ?s)
978     (?N gnus-tmp-number ?d)
979     (?u gnus-tmp-user-defined ?s)))
980
981 (defvar gnus-summary-mode-line-format-alist
982   `((?G gnus-tmp-group-name ?s)
983     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
984     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
985     (?A gnus-tmp-article-number ?d)
986     (?Z gnus-tmp-unread-and-unselected ?s)
987     (?V gnus-version ?s)
988     (?U gnus-tmp-unread-and-unticked ?d)
989     (?S gnus-tmp-subject ?s)
990     (?e gnus-tmp-unselected ?d)
991     (?u gnus-tmp-user-defined ?s)
992     (?d (length gnus-newsgroup-dormant) ?d)
993     (?t (length gnus-newsgroup-marked) ?d)
994     (?r (length gnus-newsgroup-reads) ?d)
995     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
996     (?E gnus-newsgroup-expunged-tally ?d)
997     (?s (gnus-current-score-file-nondirectory) ?s)))
998
999 (defvar gnus-last-search-regexp nil
1000   "Default regexp for article search command.")
1001
1002 (defvar gnus-summary-search-article-matched-data nil
1003   "Last matched data of article search command.  It is the local variable
1004 in `gnus-article-buffer' which consists of the list of start position,
1005 end position and text.")
1006
1007 (defvar gnus-last-shell-command nil
1008   "Default shell command on article.")
1009
1010 (defvar gnus-newsgroup-begin nil)
1011 (defvar gnus-newsgroup-end nil)
1012 (defvar gnus-newsgroup-last-rmail nil)
1013 (defvar gnus-newsgroup-last-mail nil)
1014 (defvar gnus-newsgroup-last-folder nil)
1015 (defvar gnus-newsgroup-last-file nil)
1016 (defvar gnus-newsgroup-auto-expire nil)
1017 (defvar gnus-newsgroup-active nil)
1018
1019 (defvar gnus-newsgroup-data nil)
1020 (defvar gnus-newsgroup-data-reverse nil)
1021 (defvar gnus-newsgroup-limit nil)
1022 (defvar gnus-newsgroup-limits nil)
1023
1024 (defvar gnus-newsgroup-unreads nil
1025   "List of unread articles in the current newsgroup.")
1026
1027 (defvar gnus-newsgroup-unselected nil
1028   "List of unselected unread articles in the current newsgroup.")
1029
1030 (defvar gnus-newsgroup-reads nil
1031   "Alist of read articles and article marks in the current newsgroup.")
1032
1033 (defvar gnus-newsgroup-expunged-tally nil)
1034
1035 (defvar gnus-newsgroup-marked nil
1036   "List of ticked articles in the current newsgroup (a subset of unread art).")
1037
1038 (defvar gnus-newsgroup-killed nil
1039   "List of ranges of articles that have been through the scoring process.")
1040
1041 (defvar gnus-newsgroup-cached nil
1042   "List of articles that come from the article cache.")
1043
1044 (defvar gnus-newsgroup-saved nil
1045   "List of articles that have been saved.")
1046
1047 (defvar gnus-newsgroup-kill-headers nil)
1048
1049 (defvar gnus-newsgroup-replied nil
1050   "List of articles that have been replied to in the current newsgroup.")
1051
1052 (defvar gnus-newsgroup-expirable nil
1053   "List of articles in the current newsgroup that can be expired.")
1054
1055 (defvar gnus-newsgroup-processable nil
1056   "List of articles in the current newsgroup that can be processed.")
1057
1058 (defvar gnus-newsgroup-downloadable nil
1059   "List of articles in the current newsgroup that can be processed.")
1060
1061 (defvar gnus-newsgroup-undownloaded nil
1062   "List of articles in the current newsgroup that haven't been downloaded..")
1063
1064 (defvar gnus-newsgroup-unsendable nil
1065   "List of articles in the current newsgroup that won't be sent.")
1066
1067 (defvar gnus-newsgroup-bookmarks nil
1068   "List of articles in the current newsgroup that have bookmarks.")
1069
1070 (defvar gnus-newsgroup-dormant nil
1071   "List of dormant articles in the current newsgroup.")
1072
1073 (defvar gnus-newsgroup-scored nil
1074   "List of scored articles in the current newsgroup.")
1075
1076 (defvar gnus-newsgroup-incorporated nil
1077   "List of incorporated articles in the current newsgroup.")
1078
1079 (defvar gnus-newsgroup-headers nil
1080   "List of article headers in the current newsgroup.")
1081
1082 (defvar gnus-newsgroup-threads nil)
1083
1084 (defvar gnus-newsgroup-prepared nil
1085   "Whether the current group has been prepared properly.")
1086
1087 (defvar gnus-newsgroup-ancient nil
1088   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1089
1090 (defvar gnus-newsgroup-sparse nil)
1091
1092 (defvar gnus-current-article nil)
1093 (defvar gnus-article-current nil)
1094 (defvar gnus-current-headers nil)
1095 (defvar gnus-have-all-headers nil)
1096 (defvar gnus-last-article nil)
1097 (defvar gnus-newsgroup-history nil)
1098 (defvar gnus-newsgroup-charset nil)
1099 (defvar gnus-newsgroup-ephemeral-charset nil)
1100 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1101
1102 (defconst gnus-summary-local-variables
1103   '(gnus-newsgroup-name
1104     gnus-newsgroup-begin gnus-newsgroup-end
1105     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1106     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1107     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1108     gnus-newsgroup-unselected gnus-newsgroup-marked
1109     gnus-newsgroup-reads gnus-newsgroup-saved
1110     gnus-newsgroup-replied gnus-newsgroup-expirable
1111     gnus-newsgroup-processable gnus-newsgroup-killed
1112     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1113     gnus-newsgroup-unsendable
1114     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1115     gnus-newsgroup-headers gnus-newsgroup-threads
1116     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1117     gnus-current-article gnus-current-headers gnus-have-all-headers
1118     gnus-last-article gnus-article-internal-prepare-hook
1119     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1120     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1121     gnus-thread-expunge-below
1122     gnus-score-alist gnus-current-score-file
1123     (gnus-summary-expunge-below . global)
1124     (gnus-summary-mark-below . global)
1125     gnus-newsgroup-active gnus-scores-exclude-files
1126     gnus-newsgroup-history gnus-newsgroup-ancient
1127     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1128     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1129     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1130     (gnus-newsgroup-expunged-tally . 0)
1131     gnus-cache-removable-articles gnus-newsgroup-cached
1132     gnus-newsgroup-data gnus-newsgroup-data-reverse
1133     gnus-newsgroup-limit gnus-newsgroup-limits
1134     gnus-newsgroup-charset
1135     gnus-newsgroup-incorporated)
1136   "Variables that are buffer-local to the summary buffers.")
1137
1138 ;; Byte-compiler warning.
1139 (defvar gnus-article-mode-map)
1140
1141 ;; Subject simplification.
1142
1143 (defun gnus-simplify-whitespace (str)
1144   "Remove excessive whitespace."
1145   (let ((mystr str))
1146     ;; Multiple spaces.
1147     (while (string-match "[ \t][ \t]+" mystr)
1148       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1149                           " "
1150                           (substring mystr (match-end 0)))))
1151     ;; Leading spaces.
1152     (when (string-match "^[ \t]+" mystr)
1153       (setq mystr (substring mystr (match-end 0))))
1154     ;; Trailing spaces.
1155     (when (string-match "[ \t]+$" mystr)
1156       (setq mystr (substring mystr 0 (match-beginning 0))))
1157     mystr))
1158
1159 (defsubst gnus-simplify-subject-re (subject)
1160   "Remove \"Re:\" from subject lines."
1161   (if (string-match "^[Rr][Ee]: *" subject)
1162       (substring subject (match-end 0))
1163     subject))
1164
1165 (defun gnus-simplify-subject (subject &optional re-only)
1166   "Remove `Re:' and words in parentheses.
1167 If RE-ONLY is non-nil, strip leading `Re:'s only."
1168   (let ((case-fold-search t))           ;Ignore case.
1169     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1170     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1171       (setq subject (substring subject (match-end 0))))
1172     ;; Remove uninteresting prefixes.
1173     (when (and (not re-only)
1174                gnus-simplify-ignored-prefixes
1175                (string-match gnus-simplify-ignored-prefixes subject))
1176       (setq subject (substring subject (match-end 0))))
1177     ;; Remove words in parentheses from end.
1178     (unless re-only
1179       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1180         (setq subject (substring subject 0 (match-beginning 0)))))
1181     ;; Return subject string.
1182     subject))
1183
1184 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1185 ;; all whitespace.
1186 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1187   (goto-char (point-min))
1188   (while (re-search-forward regexp nil t)
1189       (replace-match (or newtext ""))))
1190
1191 (defun gnus-simplify-buffer-fuzzy ()
1192   "Simplify string in the buffer fuzzily.
1193 The string in the accessible portion of the current buffer is simplified.
1194 It is assumed to be a single-line subject.
1195 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1196 matter is removed.  Additional things can be deleted by setting
1197 gnus-simplify-subject-fuzzy-regexp."
1198   (let ((case-fold-search t)
1199         (modified-tick))
1200     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1201
1202     (while (not (eq modified-tick (buffer-modified-tick)))
1203       (setq modified-tick (buffer-modified-tick))
1204       (cond
1205        ((listp gnus-simplify-subject-fuzzy-regexp)
1206         (mapcar 'gnus-simplify-buffer-fuzzy-step
1207                 gnus-simplify-subject-fuzzy-regexp))
1208        (gnus-simplify-subject-fuzzy-regexp
1209         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1210       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1211       (gnus-simplify-buffer-fuzzy-step
1212        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1213       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1214
1215     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1216     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1217     (gnus-simplify-buffer-fuzzy-step " $")
1218     (gnus-simplify-buffer-fuzzy-step "^ +")))
1219
1220 (defun gnus-simplify-subject-fuzzy (subject)
1221   "Simplify a subject string fuzzily.
1222 See `gnus-simplify-buffer-fuzzy' for details."
1223   (save-excursion
1224     (gnus-set-work-buffer)
1225     (let ((case-fold-search t))
1226       ;; Remove uninteresting prefixes.
1227       (when (and gnus-simplify-ignored-prefixes
1228                  (string-match gnus-simplify-ignored-prefixes subject))
1229         (setq subject (substring subject (match-end 0))))
1230       (insert subject)
1231       (inline (gnus-simplify-buffer-fuzzy))
1232       (buffer-string))))
1233
1234 (defsubst gnus-simplify-subject-fully (subject)
1235   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1236   (cond
1237    (gnus-simplify-subject-functions
1238     (gnus-map-function gnus-simplify-subject-functions subject))
1239    ((null gnus-summary-gather-subject-limit)
1240     (gnus-simplify-subject-re subject))
1241    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1242     (gnus-simplify-subject-fuzzy subject))
1243    ((numberp gnus-summary-gather-subject-limit)
1244     (gnus-limit-string (gnus-simplify-subject-re subject)
1245                        gnus-summary-gather-subject-limit))
1246    (t
1247     subject)))
1248
1249 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1250   "Check whether two subjects are equal.
1251 If optional argument simple-first is t, first argument is already
1252 simplified."
1253   (cond
1254    ((null simple-first)
1255     (equal (gnus-simplify-subject-fully s1)
1256            (gnus-simplify-subject-fully s2)))
1257    (t
1258     (equal s1
1259            (gnus-simplify-subject-fully s2)))))
1260
1261 (defun gnus-summary-bubble-group ()
1262   "Increase the score of the current group.
1263 This is a handy function to add to `gnus-summary-exit-hook' to
1264 increase the score of each group you read."
1265   (gnus-group-add-score gnus-newsgroup-name))
1266
1267 \f
1268 ;;;
1269 ;;; Gnus summary mode
1270 ;;;
1271
1272 (put 'gnus-summary-mode 'mode-class 'special)
1273
1274 (when t
1275   ;; Non-orthogonal keys
1276
1277   (gnus-define-keys gnus-summary-mode-map
1278     " " gnus-summary-next-page
1279     "\177" gnus-summary-prev-page
1280     [delete] gnus-summary-prev-page
1281     [backspace] gnus-summary-prev-page
1282     "\r" gnus-summary-scroll-up
1283     "\M-\r" gnus-summary-scroll-down
1284     "n" gnus-summary-next-unread-article
1285     "p" gnus-summary-prev-unread-article
1286     "N" gnus-summary-next-article
1287     "P" gnus-summary-prev-article
1288     "\M-\C-n" gnus-summary-next-same-subject
1289     "\M-\C-p" gnus-summary-prev-same-subject
1290     "\M-n" gnus-summary-next-unread-subject
1291     "\M-p" gnus-summary-prev-unread-subject
1292     "." gnus-summary-first-unread-article
1293     "," gnus-summary-best-unread-article
1294     "\M-s" gnus-summary-search-article-forward
1295     "\M-r" gnus-summary-search-article-backward
1296     "<" gnus-summary-beginning-of-article
1297     ">" gnus-summary-end-of-article
1298     "j" gnus-summary-goto-article
1299     "^" gnus-summary-refer-parent-article
1300     "\M-^" gnus-summary-refer-article
1301     "u" gnus-summary-tick-article-forward
1302     "!" gnus-summary-tick-article-forward
1303     "U" gnus-summary-tick-article-backward
1304     "d" gnus-summary-mark-as-read-forward
1305     "D" gnus-summary-mark-as-read-backward
1306     "E" gnus-summary-mark-as-expirable
1307     "\M-u" gnus-summary-clear-mark-forward
1308     "\M-U" gnus-summary-clear-mark-backward
1309     "k" gnus-summary-kill-same-subject-and-select
1310     "\C-k" gnus-summary-kill-same-subject
1311     "\M-\C-k" gnus-summary-kill-thread
1312     "\M-\C-l" gnus-summary-lower-thread
1313     "e" gnus-summary-edit-article
1314     "#" gnus-summary-mark-as-processable
1315     "\M-#" gnus-summary-unmark-as-processable
1316     "\M-\C-t" gnus-summary-toggle-threads
1317     "\M-\C-s" gnus-summary-show-thread
1318     "\M-\C-h" gnus-summary-hide-thread
1319     "\M-\C-f" gnus-summary-next-thread
1320     "\M-\C-b" gnus-summary-prev-thread
1321     "\M-\C-u" gnus-summary-up-thread
1322     "\M-\C-d" gnus-summary-down-thread
1323     "&" gnus-summary-execute-command
1324     "c" gnus-summary-catchup-and-exit
1325     "\C-w" gnus-summary-mark-region-as-read
1326     "\C-t" gnus-summary-toggle-truncation
1327     "?" gnus-summary-mark-as-dormant
1328     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1329     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1330     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1331     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1332     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1333     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1334     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1335     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1336     "=" gnus-summary-expand-window
1337     "\C-x\C-s" gnus-summary-reselect-current-group
1338     "\M-g" gnus-summary-rescan-group
1339     "w" gnus-summary-stop-page-breaking
1340     "\C-c\C-r" gnus-summary-caesar-message
1341     "\M-t" gnus-summary-toggle-mime
1342     "f" gnus-summary-followup
1343     "F" gnus-summary-followup-with-original
1344     "C" gnus-summary-cancel-article
1345     "r" gnus-summary-reply
1346     "R" gnus-summary-reply-with-original
1347     "\C-c\C-f" gnus-summary-mail-forward
1348     "o" gnus-summary-save-article
1349     "\C-o" gnus-summary-save-article-mail
1350     "|" gnus-summary-pipe-output
1351     "\M-k" gnus-summary-edit-local-kill
1352     "\M-K" gnus-summary-edit-global-kill
1353     ;; "V" gnus-version
1354     "\C-c\C-d" gnus-summary-describe-group
1355     "q" gnus-summary-exit
1356     "Q" gnus-summary-exit-no-update
1357     "\C-c\C-i" gnus-info-find-node
1358     gnus-mouse-2 gnus-mouse-pick-article
1359     "m" gnus-summary-mail-other-window
1360     "a" gnus-summary-post-news
1361     "x" gnus-summary-limit-to-unread
1362     "s" gnus-summary-isearch-article
1363     "t" gnus-article-toggle-headers
1364     "g" gnus-summary-show-article
1365     "l" gnus-summary-goto-last-article
1366     "v" gnus-summary-preview-mime-message
1367     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1368     "\C-d" gnus-summary-enter-digest-group
1369     "\M-\C-d" gnus-summary-read-document
1370     "\M-\C-e" gnus-summary-edit-parameters
1371     "\M-\C-g" gnus-summary-customize-parameters
1372     "\C-c\C-b" gnus-bug
1373     "*" gnus-cache-enter-article
1374     "\M-*" gnus-cache-remove-article
1375     "\M-&" gnus-summary-universal-argument
1376     "\C-l" gnus-recenter
1377     "I" gnus-summary-increase-score
1378     "L" gnus-summary-lower-score
1379     "\M-i" gnus-symbolic-argument
1380     "h" gnus-summary-select-article-buffer
1381
1382     "V" gnus-summary-score-map
1383     "X" gnus-uu-extract-map
1384     "S" gnus-summary-send-map)
1385
1386   ;; Sort of orthogonal keymap
1387   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1388     "t" gnus-summary-tick-article-forward
1389     "!" gnus-summary-tick-article-forward
1390     "d" gnus-summary-mark-as-read-forward
1391     "r" gnus-summary-mark-as-read-forward
1392     "c" gnus-summary-clear-mark-forward
1393     " " gnus-summary-clear-mark-forward
1394     "e" gnus-summary-mark-as-expirable
1395     "x" gnus-summary-mark-as-expirable
1396     "?" gnus-summary-mark-as-dormant
1397     "b" gnus-summary-set-bookmark
1398     "B" gnus-summary-remove-bookmark
1399     "#" gnus-summary-mark-as-processable
1400     "\M-#" gnus-summary-unmark-as-processable
1401     "S" gnus-summary-limit-include-expunged
1402     "C" gnus-summary-catchup
1403     "H" gnus-summary-catchup-to-here
1404     "\C-c" gnus-summary-catchup-all
1405     "k" gnus-summary-kill-same-subject-and-select
1406     "K" gnus-summary-kill-same-subject
1407     "P" gnus-uu-mark-map)
1408
1409   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1410     "c" gnus-summary-clear-above
1411     "u" gnus-summary-tick-above
1412     "m" gnus-summary-mark-above
1413     "k" gnus-summary-kill-below)
1414
1415   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1416     "/" gnus-summary-limit-to-subject
1417     "n" gnus-summary-limit-to-articles
1418     "w" gnus-summary-pop-limit
1419     "s" gnus-summary-limit-to-subject
1420     "a" gnus-summary-limit-to-author
1421     "u" gnus-summary-limit-to-unread
1422     "m" gnus-summary-limit-to-marks
1423     "M" gnus-summary-limit-exclude-marks
1424     "v" gnus-summary-limit-to-score
1425     "*" gnus-summary-limit-include-cached
1426     "D" gnus-summary-limit-include-dormant
1427     "T" gnus-summary-limit-include-thread
1428     "d" gnus-summary-limit-exclude-dormant
1429     "t" gnus-summary-limit-to-age
1430     "x" gnus-summary-limit-to-extra 
1431     "E" gnus-summary-limit-include-expunged
1432     "c" gnus-summary-limit-exclude-childless-dormant
1433     "C" gnus-summary-limit-mark-excluded-as-read)
1434
1435   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1436     "n" gnus-summary-next-unread-article
1437     "p" gnus-summary-prev-unread-article
1438     "N" gnus-summary-next-article
1439     "P" gnus-summary-prev-article
1440     "\C-n" gnus-summary-next-same-subject
1441     "\C-p" gnus-summary-prev-same-subject
1442     "\M-n" gnus-summary-next-unread-subject
1443     "\M-p" gnus-summary-prev-unread-subject
1444     "f" gnus-summary-first-unread-article
1445     "b" gnus-summary-best-unread-article
1446     "j" gnus-summary-goto-article
1447     "g" gnus-summary-goto-subject
1448     "l" gnus-summary-goto-last-article
1449     "o" gnus-summary-pop-article)
1450
1451   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1452     "k" gnus-summary-kill-thread
1453     "l" gnus-summary-lower-thread
1454     "i" gnus-summary-raise-thread
1455     "T" gnus-summary-toggle-threads
1456     "t" gnus-summary-rethread-current
1457     "^" gnus-summary-reparent-thread
1458     "s" gnus-summary-show-thread
1459     "S" gnus-summary-show-all-threads
1460     "h" gnus-summary-hide-thread
1461     "H" gnus-summary-hide-all-threads
1462     "n" gnus-summary-next-thread
1463     "p" gnus-summary-prev-thread
1464     "u" gnus-summary-up-thread
1465     "o" gnus-summary-top-thread
1466     "d" gnus-summary-down-thread
1467     "#" gnus-uu-mark-thread
1468     "\M-#" gnus-uu-unmark-thread)
1469
1470   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1471     "g" gnus-summary-prepare
1472     "c" gnus-summary-insert-cached-articles)
1473
1474   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1475     "c" gnus-summary-catchup-and-exit
1476     "C" gnus-summary-catchup-all-and-exit
1477     "E" gnus-summary-exit-no-update
1478     "J" gnus-summary-jump-to-other-group
1479     "Q" gnus-summary-exit
1480     "Z" gnus-summary-exit
1481     "n" gnus-summary-catchup-and-goto-next-group
1482     "R" gnus-summary-reselect-current-group
1483     "G" gnus-summary-rescan-group
1484     "N" gnus-summary-next-group
1485     "s" gnus-summary-save-newsrc
1486     "P" gnus-summary-prev-group)
1487
1488   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1489     " " gnus-summary-next-page
1490     "n" gnus-summary-next-page
1491     "\177" gnus-summary-prev-page
1492     [delete] gnus-summary-prev-page
1493     "p" gnus-summary-prev-page
1494     "\r" gnus-summary-scroll-up
1495     "\M-\r" gnus-summary-scroll-down
1496     "<" gnus-summary-beginning-of-article
1497     ">" gnus-summary-end-of-article
1498     "b" gnus-summary-beginning-of-article
1499     "e" gnus-summary-end-of-article
1500     "^" gnus-summary-refer-parent-article
1501     "r" gnus-summary-refer-parent-article
1502     "D" gnus-summary-enter-digest-group
1503     "R" gnus-summary-refer-references
1504     "T" gnus-summary-refer-thread
1505     "g" gnus-summary-show-article
1506     "s" gnus-summary-isearch-article
1507     "P" gnus-summary-print-article
1508     "t" gnus-article-babel)
1509
1510   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1511     "b" gnus-article-add-buttons
1512     "B" gnus-article-add-buttons-to-head
1513     "o" gnus-article-treat-overstrike
1514     "e" gnus-article-emphasize
1515     "w" gnus-article-fill-cited-article
1516     "Q" gnus-article-fill-long-lines
1517     "C" gnus-article-capitalize-sentences
1518     "c" gnus-article-remove-cr
1519     "f" gnus-article-display-x-face
1520     "l" gnus-summary-stop-page-breaking
1521     "r" gnus-summary-caesar-message
1522     "t" gnus-article-toggle-headers
1523     "v" gnus-summary-verbose-headers
1524     "m" gnus-summary-toggle-mime
1525     "H" gnus-article-strip-headers-in-body
1526     "d" gnus-article-treat-dumbquotes
1527     "s" gnus-smiley-display)
1528
1529   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1530     "a" gnus-article-hide
1531     "h" gnus-article-toggle-headers
1532     "b" gnus-article-hide-boring-headers
1533     "s" gnus-article-hide-signature
1534     "c" gnus-article-hide-citation
1535     "C" gnus-article-hide-citation-in-followups
1536     "l" gnus-article-hide-list-identifiers
1537     "p" gnus-article-hide-pgp
1538     "B" gnus-article-strip-banner
1539     "P" gnus-article-hide-pem
1540     "\C-c" gnus-article-hide-citation-maybe)
1541
1542   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1543     "a" gnus-article-highlight
1544     "h" gnus-article-highlight-headers
1545     "c" gnus-article-highlight-citation
1546     "s" gnus-article-highlight-signature)
1547
1548   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1549     "z" gnus-article-date-ut
1550     "u" gnus-article-date-ut
1551     "l" gnus-article-date-local
1552     "e" gnus-article-date-lapsed
1553     "o" gnus-article-date-original
1554     "i" gnus-article-date-iso8601
1555     "s" gnus-article-date-user)
1556
1557   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1558     "t" gnus-article-remove-trailing-blank-lines
1559     "l" gnus-article-strip-leading-blank-lines
1560     "m" gnus-article-strip-multiple-blank-lines
1561     "a" gnus-article-strip-blank-lines
1562     "A" gnus-article-strip-all-blank-lines
1563     "s" gnus-article-strip-leading-space
1564     "e" gnus-article-strip-trailing-space)
1565
1566   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1567     "v" gnus-version
1568     "f" gnus-summary-fetch-faq
1569     "d" gnus-summary-describe-group
1570     "h" gnus-summary-describe-briefly
1571     "i" gnus-info-find-node)
1572
1573   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1574     "e" gnus-summary-expire-articles
1575     "\M-\C-e" gnus-summary-expire-articles-now
1576     "\177" gnus-summary-delete-article
1577     [delete] gnus-summary-delete-article
1578     [backspace] gnus-summary-delete-article
1579     "m" gnus-summary-move-article
1580     "r" gnus-summary-respool-article
1581     "w" gnus-summary-edit-article
1582     "c" gnus-summary-copy-article
1583     "B" gnus-summary-crosspost-article
1584     "q" gnus-summary-respool-query
1585     "t" gnus-summary-respool-trace
1586     "i" gnus-summary-import-article
1587     "p" gnus-summary-article-posted-p)
1588
1589   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1590     "o" gnus-summary-save-article
1591     "m" gnus-summary-save-article-mail
1592     "F" gnus-summary-write-article-file
1593     "r" gnus-summary-save-article-rmail
1594     "f" gnus-summary-save-article-file
1595     "b" gnus-summary-save-article-body-file
1596     "h" gnus-summary-save-article-folder
1597     "v" gnus-summary-save-article-vm
1598     "p" gnus-summary-pipe-output
1599     "s" gnus-soup-add-article)
1600
1601   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1602     "b" gnus-summary-display-buttonized
1603     "m" gnus-summary-repair-multipart
1604     "v" gnus-article-view-part
1605     "o" gnus-article-save-part
1606     "c" gnus-article-copy-part
1607     "e" gnus-article-externalize-part
1608     "i" gnus-article-inline-part
1609     "|" gnus-article-pipe-part)
1610   )
1611
1612 (defun gnus-summary-make-menu-bar ()
1613   (gnus-turn-off-edit-menu 'summary)
1614
1615   (unless (boundp 'gnus-summary-misc-menu)
1616
1617     (easy-menu-define
1618      gnus-summary-kill-menu gnus-summary-mode-map ""
1619      (cons
1620       "Score"
1621       (nconc
1622        (list
1623         ["Enter score..." gnus-summary-score-entry t]
1624         ["Customize" gnus-score-customize t])
1625        (gnus-make-score-map 'increase)
1626        (gnus-make-score-map 'lower)
1627        '(("Mark"
1628           ["Kill below" gnus-summary-kill-below t]
1629           ["Mark above" gnus-summary-mark-above t]
1630           ["Tick above" gnus-summary-tick-above t]
1631           ["Clear above" gnus-summary-clear-above t])
1632          ["Current score" gnus-summary-current-score t]
1633          ["Set score" gnus-summary-set-score t]
1634          ["Switch current score file..." gnus-score-change-score-file t]
1635          ["Set mark below..." gnus-score-set-mark-below t]
1636          ["Set expunge below..." gnus-score-set-expunge-below t]
1637          ["Edit current score file" gnus-score-edit-current-scores t]
1638          ["Edit score file" gnus-score-edit-file t]
1639          ["Trace score" gnus-score-find-trace t]
1640          ["Find words" gnus-score-find-favourite-words t]
1641          ["Rescore buffer" gnus-summary-rescore t]
1642          ["Increase score..." gnus-summary-increase-score t]
1643          ["Lower score..." gnus-summary-lower-score t]))))
1644
1645     ;; Define both the Article menu in the summary buffer and the equivalent
1646     ;; Commands menu in the article buffer here for consistency.
1647     (let ((innards
1648            '(("Hide"
1649               ["All" gnus-article-hide t]
1650               ["Headers" gnus-article-toggle-headers t]
1651               ["Signature" gnus-article-hide-signature t]
1652               ["Citation" gnus-article-hide-citation t]
1653               ["List identifiers" gnus-article-hide-list-identifiers t]
1654               ["PGP" gnus-article-hide-pgp t]
1655               ["Banner" gnus-article-strip-banner t]
1656               ["Boring headers" gnus-article-hide-boring-headers t])
1657              ("Highlight"
1658               ["All" gnus-article-highlight t]
1659               ["Headers" gnus-article-highlight-headers t]
1660               ["Signature" gnus-article-highlight-signature t]
1661               ["Citation" gnus-article-highlight-citation t])
1662              ("Date"
1663               ["Local" gnus-article-date-local t]
1664               ["ISO8601" gnus-article-date-iso8601 t]
1665               ["UT" gnus-article-date-ut t]
1666               ["Original" gnus-article-date-original t]
1667               ["Lapsed" gnus-article-date-lapsed t]
1668               ["User-defined" gnus-article-date-user t])
1669              ("Washing"
1670               ("Remove Blanks"
1671                ["Leading" gnus-article-strip-leading-blank-lines t]
1672                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1673                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1674                ["All of the above" gnus-article-strip-blank-lines t]
1675                ["All" gnus-article-strip-all-blank-lines t]
1676                ["Leading space" gnus-article-strip-leading-space t]
1677                ["Trailing space" gnus-article-strip-trailing-space t])
1678               ["Overstrike" gnus-article-treat-overstrike t]
1679               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1680               ["Emphasis" gnus-article-emphasize t]
1681               ["Word wrap" gnus-article-fill-cited-article t]
1682               ["Fill long lines" gnus-article-fill-long-lines t]
1683               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1684               ["CR" gnus-article-remove-cr t]
1685               ["Show X-Face" gnus-article-display-x-face t]
1686               ["Rot 13" gnus-summary-caesar-message t]
1687               ["Unix pipe" gnus-summary-pipe-message t]
1688               ["Add buttons" gnus-article-add-buttons t]
1689               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1690               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1691               ["Toggle MIME" gnus-summary-toggle-mime t]
1692               ["Verbose header" gnus-summary-verbose-headers t]
1693               ["Toggle header" gnus-summary-toggle-header t]
1694               ["Toggle smileys" gnus-smiley-display t])
1695              ("Output"
1696               ["Save in default format" gnus-summary-save-article t]
1697               ["Save in file" gnus-summary-save-article-file t]
1698               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1699               ["Save in MH folder" gnus-summary-save-article-folder t]
1700               ["Save in VM folder" gnus-summary-save-article-vm t]
1701               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1702               ["Save body in file" gnus-summary-save-article-body-file t]
1703               ["Pipe through a filter" gnus-summary-pipe-output t]
1704               ["Add to SOUP packet" gnus-soup-add-article t]
1705               ["Print" gnus-summary-print-article t])
1706              ("Backend"
1707               ["Respool article..." gnus-summary-respool-article t]
1708               ["Move article..." gnus-summary-move-article
1709                (gnus-check-backend-function
1710                 'request-move-article gnus-newsgroup-name)]
1711               ["Copy article..." gnus-summary-copy-article t]
1712               ["Crosspost article..." gnus-summary-crosspost-article
1713                (gnus-check-backend-function
1714                 'request-replace-article gnus-newsgroup-name)]
1715               ["Import file..." gnus-summary-import-article t]
1716               ["Check if posted" gnus-summary-article-posted-p t]
1717               ["Edit article" gnus-summary-edit-article
1718                (not (gnus-group-read-only-p))]
1719               ["Delete article" gnus-summary-delete-article
1720                (gnus-check-backend-function
1721                 'request-expire-articles gnus-newsgroup-name)]
1722               ["Query respool" gnus-summary-respool-query t]
1723               ["Trace respool" gnus-summary-respool-trace t]
1724               ["Delete expirable articles" gnus-summary-expire-articles-now
1725                (gnus-check-backend-function
1726                 'request-expire-articles gnus-newsgroup-name)])
1727              ("Extract"
1728               ["Uudecode" gnus-uu-decode-uu t]
1729               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1730               ["Unshar" gnus-uu-decode-unshar t]
1731               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1732               ["Save" gnus-uu-decode-save t]
1733               ["Binhex" gnus-uu-decode-binhex t]
1734               ["Postscript" gnus-uu-decode-postscript t])
1735              ("Cache"
1736               ["Enter article" gnus-cache-enter-article t]
1737               ["Remove article" gnus-cache-remove-article t])
1738              ["Translate" gnus-article-babel t]
1739              ["Select article buffer" gnus-summary-select-article-buffer t]
1740              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1741              ["Isearch article..." gnus-summary-isearch-article t]
1742              ["Beginning of the article" gnus-summary-beginning-of-article t]
1743              ["End of the article" gnus-summary-end-of-article t]
1744              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1745              ["Fetch referenced articles" gnus-summary-refer-references t]
1746              ["Fetch current thread" gnus-summary-refer-thread t]
1747              ["Fetch article with id..." gnus-summary-refer-article t]
1748              ["Redisplay" gnus-summary-show-article t])))
1749       (easy-menu-define
1750        gnus-summary-article-menu gnus-summary-mode-map ""
1751        (cons "Article" innards))
1752
1753       (easy-menu-define
1754        gnus-article-commands-menu gnus-article-mode-map ""
1755        (cons "Commands" innards)))
1756
1757     (easy-menu-define
1758      gnus-summary-thread-menu gnus-summary-mode-map ""
1759      '("Threads"
1760        ["Toggle threading" gnus-summary-toggle-threads t]
1761        ["Hide threads" gnus-summary-hide-all-threads t]
1762        ["Show threads" gnus-summary-show-all-threads t]
1763        ["Hide thread" gnus-summary-hide-thread t]
1764        ["Show thread" gnus-summary-show-thread t]
1765        ["Go to next thread" gnus-summary-next-thread t]
1766        ["Go to previous thread" gnus-summary-prev-thread t]
1767        ["Go down thread" gnus-summary-down-thread t]
1768        ["Go up thread" gnus-summary-up-thread t]
1769        ["Top of thread" gnus-summary-top-thread t]
1770        ["Mark thread as read" gnus-summary-kill-thread t]
1771        ["Lower thread score" gnus-summary-lower-thread t]
1772        ["Raise thread score" gnus-summary-raise-thread t]
1773        ["Rethread current" gnus-summary-rethread-current t]
1774        ))
1775
1776     (easy-menu-define
1777      gnus-summary-post-menu gnus-summary-mode-map ""
1778      '("Post"
1779        ["Post an article" gnus-summary-post-news t]
1780        ["Followup" gnus-summary-followup t]
1781        ["Followup and yank" gnus-summary-followup-with-original t]
1782        ["Supersede article" gnus-summary-supersede-article t]
1783        ["Cancel article" gnus-summary-cancel-article t]
1784        ["Reply" gnus-summary-reply t]
1785        ["Reply and yank" gnus-summary-reply-with-original t]
1786        ["Wide reply" gnus-summary-wide-reply t]
1787        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1788        ["Mail forward" gnus-summary-mail-forward t]
1789        ["Post forward" gnus-summary-post-forward t]
1790        ["Digest and mail" gnus-summary-mail-digest t]
1791        ["Digest and post" gnus-summary-post-digest t]
1792        ["Resend message" gnus-summary-resend-message t]
1793        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1794        ["Send a mail" gnus-summary-mail-other-window t]
1795        ["Uuencode and post" gnus-uu-post-news t]
1796        ["Followup via news" gnus-summary-followup-to-mail t]
1797        ["Followup via news and yank"
1798         gnus-summary-followup-to-mail-with-original t]
1799        ;;("Draft"
1800        ;;["Send" gnus-summary-send-draft t]
1801        ;;["Send bounced" gnus-resend-bounced-mail t])
1802        ))
1803
1804     (easy-menu-define
1805      gnus-summary-misc-menu gnus-summary-mode-map ""
1806      '("Misc"
1807        ("Mark Read"
1808         ["Mark as read" gnus-summary-mark-as-read-forward t]
1809         ["Mark same subject and select"
1810          gnus-summary-kill-same-subject-and-select t]
1811         ["Mark same subject" gnus-summary-kill-same-subject t]
1812         ["Catchup" gnus-summary-catchup t]
1813         ["Catchup all" gnus-summary-catchup-all t]
1814         ["Catchup to here" gnus-summary-catchup-to-here t]
1815         ["Catchup region" gnus-summary-mark-region-as-read t]
1816         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1817        ("Mark Various"
1818         ["Tick" gnus-summary-tick-article-forward t]
1819         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1820         ["Remove marks" gnus-summary-clear-mark-forward t]
1821         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1822         ["Set bookmark" gnus-summary-set-bookmark t]
1823         ["Remove bookmark" gnus-summary-remove-bookmark t])
1824        ("Mark Limit"
1825         ["Marks..." gnus-summary-limit-to-marks t]
1826         ["Subject..." gnus-summary-limit-to-subject t]
1827         ["Author..." gnus-summary-limit-to-author t]
1828         ["Age..." gnus-summary-limit-to-age t]
1829         ["Extra..." gnus-summary-limit-to-extra t]
1830         ["Score" gnus-summary-limit-to-score t]
1831         ["Unread" gnus-summary-limit-to-unread t]
1832         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1833         ["Articles" gnus-summary-limit-to-articles t]
1834         ["Pop limit" gnus-summary-pop-limit t]
1835         ["Show dormant" gnus-summary-limit-include-dormant t]
1836         ["Hide childless dormant"
1837          gnus-summary-limit-exclude-childless-dormant t]
1838         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1839         ["Hide marked" gnus-summary-limit-exclude-marks t]
1840         ["Show expunged" gnus-summary-show-all-expunged t])
1841        ("Process Mark"
1842         ["Set mark" gnus-summary-mark-as-processable t]
1843         ["Remove mark" gnus-summary-unmark-as-processable t]
1844         ["Remove all marks" gnus-summary-unmark-all-processable t]
1845         ["Mark above" gnus-uu-mark-over t]
1846         ["Mark series" gnus-uu-mark-series t]
1847         ["Mark region" gnus-uu-mark-region t]
1848         ["Unmark region" gnus-uu-unmark-region t]
1849         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1850         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1851         ["Mark all" gnus-uu-mark-all t]
1852         ["Mark buffer" gnus-uu-mark-buffer t]
1853         ["Mark sparse" gnus-uu-mark-sparse t]
1854         ["Mark thread" gnus-uu-mark-thread t]
1855         ["Unmark thread" gnus-uu-unmark-thread t]
1856         ("Process Mark Sets"
1857          ["Kill" gnus-summary-kill-process-mark t]
1858          ["Yank" gnus-summary-yank-process-mark
1859           gnus-newsgroup-process-stack]
1860          ["Save" gnus-summary-save-process-mark t]))
1861        ("Scroll article"
1862         ["Page forward" gnus-summary-next-page t]
1863         ["Page backward" gnus-summary-prev-page t]
1864         ["Line forward" gnus-summary-scroll-up t])
1865        ("Move"
1866         ["Next unread article" gnus-summary-next-unread-article t]
1867         ["Previous unread article" gnus-summary-prev-unread-article t]
1868         ["Next article" gnus-summary-next-article t]
1869         ["Previous article" gnus-summary-prev-article t]
1870         ["Next unread subject" gnus-summary-next-unread-subject t]
1871         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1872         ["Next article same subject" gnus-summary-next-same-subject t]
1873         ["Previous article same subject" gnus-summary-prev-same-subject t]
1874         ["First unread article" gnus-summary-first-unread-article t]
1875         ["Best unread article" gnus-summary-best-unread-article t]
1876         ["Go to subject number..." gnus-summary-goto-subject t]
1877         ["Go to article number..." gnus-summary-goto-article t]
1878         ["Go to the last article" gnus-summary-goto-last-article t]
1879         ["Pop article off history" gnus-summary-pop-article t])
1880        ("Sort"
1881         ["Sort by number" gnus-summary-sort-by-number t]
1882         ["Sort by author" gnus-summary-sort-by-author t]
1883         ["Sort by subject" gnus-summary-sort-by-subject t]
1884         ["Sort by date" gnus-summary-sort-by-date t]
1885         ["Sort by score" gnus-summary-sort-by-score t]
1886         ["Sort by lines" gnus-summary-sort-by-lines t]
1887         ["Sort by characters" gnus-summary-sort-by-chars t])
1888        ("Help"
1889         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1890         ["Describe group" gnus-summary-describe-group t]
1891         ["Read manual" gnus-info-find-node t])
1892        ("Modes"
1893         ["Pick and read" gnus-pick-mode t]
1894         ["Binary" gnus-binary-mode t])
1895        ("Regeneration"
1896         ["Regenerate" gnus-summary-prepare t]
1897         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1898         ["Toggle threading" gnus-summary-toggle-threads t])
1899        ["Filter articles..." gnus-summary-execute-command t]
1900        ["Run command on subjects..." gnus-summary-universal-argument t]
1901        ["Search articles forward..." gnus-summary-search-article-forward t]
1902        ["Search articles backward..." gnus-summary-search-article-backward t]
1903        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1904        ["Expand window" gnus-summary-expand-window t]
1905        ["Expire expirable articles" gnus-summary-expire-articles
1906         (gnus-check-backend-function
1907          'request-expire-articles gnus-newsgroup-name)]
1908        ["Edit local kill file" gnus-summary-edit-local-kill t]
1909        ["Edit main kill file" gnus-summary-edit-global-kill t]
1910        ["Edit group parameters" gnus-summary-edit-parameters t]
1911        ["Customize group parameters" gnus-summary-customize-parameters t]
1912        ["Send a bug report" gnus-bug t]
1913        ("Exit"
1914         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1915         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1916         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1917         ["Exit group" gnus-summary-exit t]
1918         ["Exit group without updating" gnus-summary-exit-no-update t]
1919         ["Exit and goto next group" gnus-summary-next-group t]
1920         ["Exit and goto prev group" gnus-summary-prev-group t]
1921         ["Reselect group" gnus-summary-reselect-current-group t]
1922         ["Rescan group" gnus-summary-rescan-group t]
1923         ["Update dribble" gnus-summary-save-newsrc t])))
1924
1925     (gnus-run-hooks 'gnus-summary-menu-hook)))
1926
1927 (defun gnus-score-set-default (var value)
1928   "A version of set that updates the GNU Emacs menu-bar."
1929   (set var value)
1930   ;; It is the message that forces the active status to be updated.
1931   (message ""))
1932
1933 (defun gnus-make-score-map (type)
1934   "Make a summary score map of type TYPE."
1935   (if t
1936       nil
1937     (let ((headers '(("author" "from" string)
1938                      ("subject" "subject" string)
1939                      ("article body" "body" string)
1940                      ("article head" "head" string)
1941                      ("xref" "xref" string)
1942                      ("extra header" "extra" string)
1943                      ("lines" "lines" number)
1944                      ("followups to author" "followup" string)))
1945           (types '((number ("less than" <)
1946                            ("greater than" >)
1947                            ("equal" =))
1948                    (string ("substring" s)
1949                            ("exact string" e)
1950                            ("fuzzy string" f)
1951                            ("regexp" r))))
1952           (perms '(("temporary" (current-time-string))
1953                    ("permanent" nil)
1954                    ("immediate" now)))
1955           header)
1956       (list
1957        (apply
1958         'nconc
1959         (list
1960          (if (eq type 'lower)
1961              "Lower score"
1962            "Increase score"))
1963         (let (outh)
1964           (while headers
1965             (setq header (car headers))
1966             (setq outh
1967                   (cons
1968                    (apply
1969                     'nconc
1970                     (list (car header))
1971                     (let ((ts (cdr (assoc (nth 2 header) types)))
1972                           outt)
1973                       (while ts
1974                         (setq outt
1975                               (cons
1976                                (apply
1977                                 'nconc
1978                                 (list (caar ts))
1979                                 (let ((ps perms)
1980                                       outp)
1981                                   (while ps
1982                                     (setq outp
1983                                           (cons
1984                                            (vector
1985                                             (caar ps)
1986                                             (list
1987                                              'gnus-summary-score-entry
1988                                              (nth 1 header)
1989                                              (if (or (string= (nth 1 header)
1990                                                               "head")
1991                                                      (string= (nth 1 header)
1992                                                               "body"))
1993                                                  ""
1994                                                (list 'gnus-summary-header
1995                                                      (nth 1 header)))
1996                                              (list 'quote (nth 1 (car ts)))
1997                                              (list 'gnus-score-default nil)
1998                                              (nth 1 (car ps))
1999                                              t)
2000                                             t)
2001                                            outp))
2002                                     (setq ps (cdr ps)))
2003                                   (list (nreverse outp))))
2004                                outt))
2005                         (setq ts (cdr ts)))
2006                       (list (nreverse outt))))
2007                    outh))
2008             (setq headers (cdr headers)))
2009           (list (nreverse outh))))))))
2010
2011 \f
2012
2013 (defun gnus-summary-mode (&optional group)
2014   "Major mode for reading articles.
2015
2016 All normal editing commands are switched off.
2017 \\<gnus-summary-mode-map>
2018 Each line in this buffer represents one article.  To read an
2019 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2020 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2021 respectively.
2022
2023 You can also post articles and send mail from this buffer.  To
2024 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2025 of an article, type `\\[gnus-summary-reply]'.
2026
2027 There are approx. one gazillion commands you can execute in this
2028 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2029
2030 The following commands are available:
2031
2032 \\{gnus-summary-mode-map}"
2033   (interactive)
2034   (when (gnus-visual-p 'summary-menu 'menu)
2035     (gnus-summary-make-menu-bar))
2036   (kill-all-local-variables)
2037   (gnus-summary-make-local-variables)
2038   (gnus-make-thread-indent-array)
2039   (gnus-simplify-mode-line)
2040   (setq major-mode 'gnus-summary-mode)
2041   (setq mode-name "Summary")
2042   (make-local-variable 'minor-mode-alist)
2043   (use-local-map gnus-summary-mode-map)
2044   (buffer-disable-undo)
2045   (setq buffer-read-only t)             ;Disable modification
2046   (setq truncate-lines t)
2047   (setq selective-display t)
2048   (setq selective-display-ellipses t)   ;Display `...'
2049   (gnus-summary-set-display-table)
2050   (gnus-set-default-directory)
2051   (setq gnus-newsgroup-name group)
2052   (unless (gnus-news-group-p group)
2053     (setq gnus-newsgroup-incorporated
2054           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2055   (make-local-variable 'gnus-summary-line-format)
2056   (make-local-variable 'gnus-summary-line-format-spec)
2057   (make-local-variable 'gnus-summary-dummy-line-format)
2058   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2059   (make-local-variable 'gnus-summary-mark-positions)
2060   (make-local-hook 'pre-command-hook)
2061   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2062   (gnus-run-hooks 'gnus-summary-mode-hook)
2063   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2064   (gnus-update-summary-mark-positions))
2065
2066 (defun gnus-summary-make-local-variables ()
2067   "Make all the local summary buffer variables."
2068   (let (global)
2069     (dolist (local gnus-summary-local-variables)
2070       (if (consp local)
2071           (progn
2072             (if (eq (cdr local) 'global)
2073                 ;; Copy the global value of the variable.
2074                 (setq global (symbol-value (car local)))
2075               ;; Use the value from the list.
2076               (setq global (eval (cdr local))))
2077             (set (make-local-variable (car local)) global))
2078         ;; Simple nil-valued local variable.
2079         (set (make-local-variable local) nil)))))
2080
2081 (defun gnus-summary-clear-local-variables ()
2082   (let ((locals gnus-summary-local-variables))
2083     (while locals
2084       (if (consp (car locals))
2085           (and (vectorp (caar locals))
2086                (set (caar locals) nil))
2087         (and (vectorp (car locals))
2088              (set (car locals) nil)))
2089       (setq locals (cdr locals)))))
2090
2091 ;; Summary data functions.
2092
2093 (defmacro gnus-data-number (data)
2094   `(car ,data))
2095
2096 (defmacro gnus-data-set-number (data number)
2097   `(setcar ,data ,number))
2098
2099 (defmacro gnus-data-mark (data)
2100   `(nth 1 ,data))
2101
2102 (defmacro gnus-data-set-mark (data mark)
2103   `(setcar (nthcdr 1 ,data) ,mark))
2104
2105 (defmacro gnus-data-pos (data)
2106   `(nth 2 ,data))
2107
2108 (defmacro gnus-data-set-pos (data pos)
2109   `(setcar (nthcdr 2 ,data) ,pos))
2110
2111 (defmacro gnus-data-header (data)
2112   `(nth 3 ,data))
2113
2114 (defmacro gnus-data-set-header (data header)
2115   `(setcar (nthcdr 3 ,data) ,header))
2116
2117 (defmacro gnus-data-level (data)
2118   `(nth 4 ,data))
2119
2120 (defmacro gnus-data-unread-p (data)
2121   `(= (nth 1 ,data) gnus-unread-mark))
2122
2123 (defmacro gnus-data-read-p (data)
2124   `(/= (nth 1 ,data) gnus-unread-mark))
2125
2126 (defmacro gnus-data-pseudo-p (data)
2127   `(consp (nth 3 ,data)))
2128
2129 (defmacro gnus-data-find (number)
2130   `(assq ,number gnus-newsgroup-data))
2131
2132 (defmacro gnus-data-find-list (number &optional data)
2133   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2134      (memq (assq ,number bdata)
2135            bdata)))
2136
2137 (defmacro gnus-data-make (number mark pos header level)
2138   `(list ,number ,mark ,pos ,header ,level))
2139
2140 (defun gnus-data-enter (after-article number mark pos header level offset)
2141   (let ((data (gnus-data-find-list after-article)))
2142     (unless data
2143       (error "No such article: %d" after-article))
2144     (setcdr data (cons (gnus-data-make number mark pos header level)
2145                        (cdr data)))
2146     (setq gnus-newsgroup-data-reverse nil)
2147     (gnus-data-update-list (cddr data) offset)))
2148
2149 (defun gnus-data-enter-list (after-article list &optional offset)
2150   (when list
2151     (let ((data (and after-article (gnus-data-find-list after-article)))
2152           (ilist list))
2153       (if (not (or data
2154                    after-article))
2155           (let ((odata gnus-newsgroup-data))
2156             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2157             (when offset
2158               (gnus-data-update-list odata offset)))
2159         ;; Find the last element in the list to be spliced into the main
2160         ;; list.
2161         (while (cdr list)
2162           (setq list (cdr list)))
2163         (if (not data)
2164             (progn
2165               (setcdr list gnus-newsgroup-data)
2166               (setq gnus-newsgroup-data ilist)
2167               (when offset
2168                 (gnus-data-update-list (cdr list) offset)))
2169           (setcdr list (cdr data))
2170           (setcdr data ilist)
2171           (when offset
2172             (gnus-data-update-list (cdr list) offset))))
2173       (setq gnus-newsgroup-data-reverse nil))))
2174
2175 (defun gnus-data-remove (article &optional offset)
2176   (let ((data gnus-newsgroup-data))
2177     (if (= (gnus-data-number (car data)) article)
2178         (progn
2179           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2180                 gnus-newsgroup-data-reverse nil)
2181           (when offset
2182             (gnus-data-update-list gnus-newsgroup-data offset)))
2183       (while (cdr data)
2184         (when (= (gnus-data-number (cadr data)) article)
2185           (setcdr data (cddr data))
2186           (when offset
2187             (gnus-data-update-list (cdr data) offset))
2188           (setq data nil
2189                 gnus-newsgroup-data-reverse nil))
2190         (setq data (cdr data))))))
2191
2192 (defmacro gnus-data-list (backward)
2193   `(if ,backward
2194        (or gnus-newsgroup-data-reverse
2195            (setq gnus-newsgroup-data-reverse
2196                  (reverse gnus-newsgroup-data)))
2197      gnus-newsgroup-data))
2198
2199 (defun gnus-data-update-list (data offset)
2200   "Add OFFSET to the POS of all data entries in DATA."
2201   (setq gnus-newsgroup-data-reverse nil)
2202   (while data
2203     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2204     (setq data (cdr data))))
2205
2206 (defun gnus-summary-article-pseudo-p (article)
2207   "Say whether this article is a pseudo article or not."
2208   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2209
2210 (defmacro gnus-summary-article-sparse-p (article)
2211   "Say whether this article is a sparse article or not."
2212   `(memq ,article gnus-newsgroup-sparse))
2213
2214 (defmacro gnus-summary-article-ancient-p (article)
2215   "Say whether this article is a sparse article or not."
2216   `(memq ,article gnus-newsgroup-ancient))
2217
2218 (defun gnus-article-parent-p (number)
2219   "Say whether this article is a parent or not."
2220   (let ((data (gnus-data-find-list number)))
2221     (and (cdr data)                     ; There has to be an article after...
2222          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2223             (gnus-data-level (nth 1 data))))))
2224
2225 (defun gnus-article-children (number)
2226   "Return a list of all children to NUMBER."
2227   (let* ((data (gnus-data-find-list number))
2228          (level (gnus-data-level (car data)))
2229          children)
2230     (setq data (cdr data))
2231     (while (and data
2232                 (= (gnus-data-level (car data)) (1+ level)))
2233       (push (gnus-data-number (car data)) children)
2234       (setq data (cdr data)))
2235     children))
2236
2237 (defmacro gnus-summary-skip-intangible ()
2238   "If the current article is intangible, then jump to a different article."
2239   '(let ((to (get-text-property (point) 'gnus-intangible)))
2240      (and to (gnus-summary-goto-subject to))))
2241
2242 (defmacro gnus-summary-article-intangible-p ()
2243   "Say whether this article is intangible or not."
2244   '(get-text-property (point) 'gnus-intangible))
2245
2246 (defun gnus-article-read-p (article)
2247   "Say whether ARTICLE is read or not."
2248   (not (or (memq article gnus-newsgroup-marked)
2249            (memq article gnus-newsgroup-unreads)
2250            (memq article gnus-newsgroup-unselected)
2251            (memq article gnus-newsgroup-dormant))))
2252
2253 ;; Some summary mode macros.
2254
2255 (defmacro gnus-summary-article-number ()
2256   "The article number of the article on the current line.
2257 If there isn's an article number here, then we return the current
2258 article number."
2259   '(progn
2260      (gnus-summary-skip-intangible)
2261      (or (get-text-property (point) 'gnus-number)
2262          (gnus-summary-last-subject))))
2263
2264 (defmacro gnus-summary-article-header (&optional number)
2265   "Return the header of article NUMBER."
2266   `(gnus-data-header (gnus-data-find
2267                       ,(or number '(gnus-summary-article-number)))))
2268
2269 (defmacro gnus-summary-thread-level (&optional number)
2270   "Return the level of thread that starts with article NUMBER."
2271   `(if (and (eq gnus-summary-make-false-root 'dummy)
2272             (get-text-property (point) 'gnus-intangible))
2273        0
2274      (gnus-data-level (gnus-data-find
2275                        ,(or number '(gnus-summary-article-number))))))
2276
2277 (defmacro gnus-summary-article-mark (&optional number)
2278   "Return the mark of article NUMBER."
2279   `(gnus-data-mark (gnus-data-find
2280                     ,(or number '(gnus-summary-article-number)))))
2281
2282 (defmacro gnus-summary-article-pos (&optional number)
2283   "Return the position of the line of article NUMBER."
2284   `(gnus-data-pos (gnus-data-find
2285                    ,(or number '(gnus-summary-article-number)))))
2286
2287 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2288 (defmacro gnus-summary-article-subject (&optional number)
2289   "Return current subject string or nil if nothing."
2290   `(let ((headers
2291           ,(if number
2292                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2293              '(gnus-data-header (assq (gnus-summary-article-number)
2294                                       gnus-newsgroup-data)))))
2295      (and headers
2296           (vectorp headers)
2297           (mail-header-subject headers))))
2298
2299 (defmacro gnus-summary-article-score (&optional number)
2300   "Return current article score."
2301   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2302                   gnus-newsgroup-scored))
2303        gnus-summary-default-score 0))
2304
2305 (defun gnus-summary-article-children (&optional number)
2306   "Return a list of article numbers that are children of article NUMBER."
2307   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2308          (level (gnus-data-level (car data)))
2309          l children)
2310     (while (and (setq data (cdr data))
2311                 (> (setq l (gnus-data-level (car data))) level))
2312       (and (= (1+ level) l)
2313            (push (gnus-data-number (car data))
2314                  children)))
2315     (nreverse children)))
2316
2317 (defun gnus-summary-article-parent (&optional number)
2318   "Return the article number of the parent of article NUMBER."
2319   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2320                                     (gnus-data-list t)))
2321          (level (gnus-data-level (car data))))
2322     (if (zerop level)
2323         ()                              ; This is a root.
2324       ;; We search until we find an article with a level less than
2325       ;; this one.  That function has to be the parent.
2326       (while (and (setq data (cdr data))
2327                   (not (< (gnus-data-level (car data)) level))))
2328       (and data (gnus-data-number (car data))))))
2329
2330 (defun gnus-unread-mark-p (mark)
2331   "Say whether MARK is the unread mark."
2332   (= mark gnus-unread-mark))
2333
2334 (defun gnus-read-mark-p (mark)
2335   "Say whether MARK is one of the marks that mark as read.
2336 This is all marks except unread, ticked, dormant, and expirable."
2337   (not (or (= mark gnus-unread-mark)
2338            (= mark gnus-ticked-mark)
2339            (= mark gnus-dormant-mark)
2340            (= mark gnus-expirable-mark))))
2341
2342 (defmacro gnus-article-mark (number)
2343   "Return the MARK of article NUMBER.
2344 This macro should only be used when computing the mark the \"first\"
2345 time; i.e., when generating the summary lines.  After that,
2346 `gnus-summary-article-mark' should be used to examine the
2347 marks of articles."
2348   `(cond
2349     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2350     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2351     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2352     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2353     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2354     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2355     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2356     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2357            gnus-ancient-mark))))
2358
2359 ;; Saving hidden threads.
2360
2361 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2362 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2363
2364 (defmacro gnus-save-hidden-threads (&rest forms)
2365   "Save hidden threads, eval FORMS, and restore the hidden threads."
2366   (let ((config (make-symbol "config")))
2367     `(let ((,config (gnus-hidden-threads-configuration)))
2368        (unwind-protect
2369            (save-excursion
2370              ,@forms)
2371          (gnus-restore-hidden-threads-configuration ,config)))))
2372
2373 (defun gnus-data-compute-positions ()
2374   "Compute the positions of all articles."
2375   (setq gnus-newsgroup-data-reverse nil)
2376   (let ((data gnus-newsgroup-data))
2377     (save-excursion
2378       (gnus-save-hidden-threads
2379         (gnus-summary-show-all-threads)
2380         (goto-char (point-min))
2381         (while data
2382           (while (get-text-property (point) 'gnus-intangible)
2383             (forward-line 1))
2384           (gnus-data-set-pos (car data) (+ (point) 3))
2385           (setq data (cdr data))
2386           (forward-line 1))))))
2387
2388 (defun gnus-hidden-threads-configuration ()
2389   "Return the current hidden threads configuration."
2390   (save-excursion
2391     (let (config)
2392       (goto-char (point-min))
2393       (while (search-forward "\r" nil t)
2394         (push (1- (point)) config))
2395       config)))
2396
2397 (defun gnus-restore-hidden-threads-configuration (config)
2398   "Restore hidden threads configuration from CONFIG."
2399   (let (point buffer-read-only)
2400     (while (setq point (pop config))
2401       (when (and (< point (point-max))
2402                  (goto-char point)
2403                  (eq (char-after) ?\n))
2404         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2405
2406 ;; Various summary mode internalish functions.
2407
2408 (defun gnus-mouse-pick-article (e)
2409   (interactive "e")
2410   (mouse-set-point e)
2411   (gnus-summary-next-page nil t))
2412
2413 (defun gnus-summary-set-display-table ()
2414   ;; Change the display table.  Odd characters have a tendency to mess
2415   ;; up nicely formatted displays - we make all possible glyphs
2416   ;; display only a single character.
2417
2418   ;; We start from the standard display table, if any.
2419   (let ((table (or (copy-sequence standard-display-table)
2420                    (make-display-table)))
2421         (i 32))
2422     ;; Nix out all the control chars...
2423     (while (>= (setq i (1- i)) 0)
2424       (aset table i [??]))
2425     ;; ... but not newline and cr, of course.  (cr is necessary for the
2426     ;; selective display).
2427     (aset table ?\n nil)
2428     (aset table ?\r nil)
2429     ;; We keep TAB as well.
2430     (aset table ?\t nil)
2431     ;; We nix out any glyphs over 126 that are not set already.
2432     (let ((i 256))
2433       (while (>= (setq i (1- i)) 127)
2434         ;; Only modify if the entry is nil.
2435         (unless (aref table i)
2436           (aset table i [??]))))
2437     (setq buffer-display-table table)))
2438
2439 (defun gnus-summary-setup-buffer (group)
2440   "Initialize summary buffer."
2441   (let ((buffer (concat "*Summary " group "*")))
2442     (if (get-buffer buffer)
2443         (progn
2444           (set-buffer buffer)
2445           (setq gnus-summary-buffer (current-buffer))
2446           (not gnus-newsgroup-prepared))
2447       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2448       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2449       (gnus-summary-mode group)
2450       (when gnus-carpal
2451         (gnus-carpal-setup-buffer 'summary))
2452       (unless gnus-single-article-buffer
2453         (make-local-variable 'gnus-article-buffer)
2454         (make-local-variable 'gnus-article-current)
2455         (make-local-variable 'gnus-original-article-buffer))
2456       (setq gnus-newsgroup-name group)
2457       t)))
2458
2459 (defun gnus-set-global-variables ()
2460   ;; Set the global equivalents of the summary buffer-local variables
2461   ;; to the latest values they had.  These reflect the summary buffer
2462   ;; that was in action when the last article was fetched.
2463   (when (eq major-mode 'gnus-summary-mode)
2464     (setq gnus-summary-buffer (current-buffer))
2465     (let ((name gnus-newsgroup-name)
2466           (marked gnus-newsgroup-marked)
2467           (unread gnus-newsgroup-unreads)
2468           (headers gnus-current-headers)
2469           (data gnus-newsgroup-data)
2470           (summary gnus-summary-buffer)
2471           (article-buffer gnus-article-buffer)
2472           (original gnus-original-article-buffer)
2473           (gac gnus-article-current)
2474           (reffed gnus-reffed-article-number)
2475           (score-file gnus-current-score-file)
2476           (default-charset gnus-newsgroup-charset))
2477       (save-excursion
2478         (set-buffer gnus-group-buffer)
2479         (setq gnus-newsgroup-name name
2480               gnus-newsgroup-marked marked
2481               gnus-newsgroup-unreads unread
2482               gnus-current-headers headers
2483               gnus-newsgroup-data data
2484               gnus-article-current gac
2485               gnus-summary-buffer summary
2486               gnus-article-buffer article-buffer
2487               gnus-original-article-buffer original
2488               gnus-reffed-article-number reffed
2489               gnus-current-score-file score-file
2490               gnus-newsgroup-charset default-charset)
2491         ;; The article buffer also has local variables.
2492         (when (gnus-buffer-live-p gnus-article-buffer)
2493           (set-buffer gnus-article-buffer)
2494           (setq gnus-summary-buffer summary))))))
2495
2496 (defun gnus-summary-article-unread-p (article)
2497   "Say whether ARTICLE is unread or not."
2498   (memq article gnus-newsgroup-unreads))
2499
2500 (defun gnus-summary-first-article-p (&optional article)
2501   "Return whether ARTICLE is the first article in the buffer."
2502   (if (not (setq article (or article (gnus-summary-article-number))))
2503       nil
2504     (eq article (caar gnus-newsgroup-data))))
2505
2506 (defun gnus-summary-last-article-p (&optional article)
2507   "Return whether ARTICLE is the last article in the buffer."
2508   (if (not (setq article (or article (gnus-summary-article-number))))
2509       ;; All non-existent numbers are the last article.  :-)
2510       t
2511     (not (cdr (gnus-data-find-list article)))))
2512
2513 (defun gnus-make-thread-indent-array ()
2514   (let ((n 200))
2515     (unless (and gnus-thread-indent-array
2516                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2517       (setq gnus-thread-indent-array (make-vector 201 "")
2518             gnus-thread-indent-array-level gnus-thread-indent-level)
2519       (while (>= n 0)
2520         (aset gnus-thread-indent-array n
2521               (make-string (* n gnus-thread-indent-level) ? ))
2522         (setq n (1- n))))))
2523
2524 (defun gnus-update-summary-mark-positions ()
2525   "Compute where the summary marks are to go."
2526   (save-excursion
2527     (when (gnus-buffer-exists-p gnus-summary-buffer)
2528       (set-buffer gnus-summary-buffer))
2529     (let ((gnus-replied-mark 129)
2530           (gnus-score-below-mark 130)
2531           (gnus-score-over-mark 130)
2532           (gnus-download-mark 131)
2533           (spec gnus-summary-line-format-spec)
2534           gnus-visual pos)
2535       (save-excursion
2536         (gnus-set-work-buffer)
2537         (let ((gnus-summary-line-format-spec spec)
2538               (gnus-newsgroup-downloadable '((0 . t))))
2539           (gnus-summary-insert-line
2540            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2541            0 nil 128 t nil "" nil 1)
2542           (goto-char (point-min))
2543           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2544                                              (- (point) 2)))))
2545           (goto-char (point-min))
2546           (push (cons 'replied (and (search-forward "\201" nil t)
2547                                     (- (point) 2)))
2548                 pos)
2549           (goto-char (point-min))
2550           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2551                 pos)
2552           (goto-char (point-min))
2553           (push (cons 'download
2554                       (and (search-forward "\203" nil t) (- (point) 2)))
2555                 pos)))
2556       (setq gnus-summary-mark-positions pos))))
2557
2558 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2559   "Insert a dummy root in the summary buffer."
2560   (beginning-of-line)
2561   (gnus-add-text-properties
2562    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2563    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2564
2565 (defun gnus-summary-from-or-to-or-newsgroups (header)
2566   (let ((to (cdr (assq 'To (mail-header-extra header))))
2567         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2568         (default-mime-charset (with-current-buffer gnus-summary-buffer
2569                                 default-mime-charset)))
2570     (cond
2571      ((and to
2572            gnus-ignored-from-addresses
2573            (string-match gnus-ignored-from-addresses
2574                          (mail-header-from header)))
2575       (concat "-> "
2576               (or (car (funcall gnus-extract-address-components
2577                                 (funcall
2578                                  gnus-decode-encoded-word-function to)))
2579                   (funcall gnus-decode-encoded-word-function to))))
2580      ((and newsgroups
2581            gnus-ignored-from-addresses
2582            (string-match gnus-ignored-from-addresses
2583                          (mail-header-from header)))
2584       (concat "=> " newsgroups))
2585      (t
2586       (or (car (funcall gnus-extract-address-components
2587                         (mail-header-from header)))
2588           (mail-header-from header))))))
2589
2590 (defun gnus-summary-insert-line (gnus-tmp-header
2591                                  gnus-tmp-level gnus-tmp-current
2592                                  gnus-tmp-unread gnus-tmp-replied
2593                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2594                                  &optional gnus-tmp-dummy gnus-tmp-score
2595                                  gnus-tmp-process)
2596   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2597          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2598          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2599          (gnus-tmp-score-char
2600           (if (or (null gnus-summary-default-score)
2601                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2602                       gnus-summary-zcore-fuzz))
2603               ? ;Whitespace
2604             (if (< gnus-tmp-score gnus-summary-default-score)
2605                 gnus-score-below-mark gnus-score-over-mark)))
2606          (gnus-tmp-replied
2607           (cond (gnus-tmp-process gnus-process-mark)
2608                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2609                  gnus-cached-mark)
2610                 (gnus-tmp-replied gnus-replied-mark)
2611                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2612                  gnus-saved-mark)
2613                 (t gnus-unread-mark)))
2614          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2615          (gnus-tmp-name
2616           (cond
2617            ((string-match "<[^>]+> *$" gnus-tmp-from)
2618             (let ((beg (match-beginning 0)))
2619               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2620                        (substring gnus-tmp-from (1+ (match-beginning 0))
2621                                   (1- (match-end 0))))
2622                   (substring gnus-tmp-from 0 beg))))
2623            ((string-match "(.+)" gnus-tmp-from)
2624             (substring gnus-tmp-from
2625                        (1+ (match-beginning 0)) (1- (match-end 0))))
2626            (t gnus-tmp-from)))
2627          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2628          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2629          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2630          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2631          (buffer-read-only nil))
2632     (when (string= gnus-tmp-name "")
2633       (setq gnus-tmp-name gnus-tmp-from))
2634     (unless (numberp gnus-tmp-lines)
2635       (setq gnus-tmp-lines 0))
2636     (gnus-put-text-property-excluding-characters-with-faces
2637      (point)
2638      (progn (eval gnus-summary-line-format-spec) (point))
2639      'gnus-number gnus-tmp-number)
2640     (when (gnus-visual-p 'summary-highlight 'highlight)
2641       (forward-line -1)
2642       (gnus-run-hooks 'gnus-summary-update-hook)
2643       (forward-line 1))))
2644
2645 (defun gnus-summary-update-line (&optional dont-update)
2646   ;; Update summary line after change.
2647   (when (and gnus-summary-default-score
2648              (not gnus-summary-inhibit-highlight))
2649     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2650            (article (gnus-summary-article-number))
2651            (score (gnus-summary-article-score article)))
2652       (unless dont-update
2653         (if (and gnus-summary-mark-below
2654                  (< (gnus-summary-article-score)
2655                     gnus-summary-mark-below))
2656             ;; This article has a low score, so we mark it as read.
2657             (when (memq article gnus-newsgroup-unreads)
2658               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2659           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2660             ;; This article was previously marked as read on account
2661             ;; of a low score, but now it has risen, so we mark it as
2662             ;; unread.
2663             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2664         (gnus-summary-update-mark
2665          (if (or (null gnus-summary-default-score)
2666                  (<= (abs (- score gnus-summary-default-score))
2667                      gnus-summary-zcore-fuzz))
2668              ? ;Whitespace
2669            (if (< score gnus-summary-default-score)
2670                gnus-score-below-mark gnus-score-over-mark))
2671          'score))
2672       ;; Do visual highlighting.
2673       (when (gnus-visual-p 'summary-highlight 'highlight)
2674         (gnus-run-hooks 'gnus-summary-update-hook)))))
2675
2676 (defvar gnus-tmp-new-adopts nil)
2677
2678 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2679   "Return the number of articles in THREAD.
2680 This may be 0 in some cases -- if none of the articles in
2681 the thread are to be displayed."
2682   (let* ((number
2683           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2684           (cond
2685            ((not (listp thread))
2686             1)
2687            ((and (consp thread) (cdr thread))
2688             (apply
2689              '+ 1 (mapcar
2690                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2691            ((null thread)
2692             1)
2693            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2694             1)
2695            (t 0))))
2696     (when (and level (zerop level) gnus-tmp-new-adopts)
2697       (incf number
2698             (apply '+ (mapcar
2699                        'gnus-summary-number-of-articles-in-thread
2700                        gnus-tmp-new-adopts))))
2701     (if char
2702         (if (> number 1) gnus-not-empty-thread-mark
2703           gnus-empty-thread-mark)
2704       number)))
2705
2706 (defun gnus-summary-set-local-parameters (group)
2707   "Go through the local params of GROUP and set all variable specs in that list."
2708   (let ((params (gnus-group-find-parameter group))
2709         elem)
2710     (while params
2711       (setq elem (car params)
2712             params (cdr params))
2713       (and (consp elem)                 ; Has to be a cons.
2714            (consp (cdr elem))           ; The cdr has to be a list.
2715            (symbolp (car elem))         ; Has to be a symbol in there.
2716            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2717            (ignore-errors               ; So we set it.
2718              (make-local-variable (car elem))
2719              (set (car elem) (eval (nth 1 elem))))))))
2720
2721 (defun gnus-summary-read-group (group &optional show-all no-article
2722                                       kill-buffer no-display backward
2723                                       select-articles)
2724   "Start reading news in newsgroup GROUP.
2725 If SHOW-ALL is non-nil, already read articles are also listed.
2726 If NO-ARTICLE is non-nil, no article is selected initially.
2727 If NO-DISPLAY, don't generate a summary buffer."
2728   (let (result)
2729     (while (and group
2730                 (null (setq result
2731                             (let ((gnus-auto-select-next nil))
2732                               (or (gnus-summary-read-group-1
2733                                    group show-all no-article
2734                                    kill-buffer no-display
2735                                    select-articles)
2736                                   (setq show-all nil
2737                                         select-articles nil)))))
2738                 (eq gnus-auto-select-next 'quietly))
2739       (set-buffer gnus-group-buffer)
2740       ;; The entry function called above goes to the next
2741       ;; group automatically, so we go two groups back
2742       ;; if we are searching for the previous group.
2743       (when backward
2744         (gnus-group-prev-unread-group 2))
2745       (if (not (equal group (gnus-group-group-name)))
2746           (setq group (gnus-group-group-name))
2747         (setq group nil)))
2748     result))
2749
2750 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2751   "Directly jump to the other GROUP from summary buffer.
2752 If SHOW-ALL is non-nil, already read articles are also listed."
2753   (interactive
2754    (if (eq gnus-summary-buffer (current-buffer))
2755        (list (completing-read
2756               "Group: " gnus-active-hashtb nil t
2757               (when (and gnus-newsgroup-name
2758                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2759                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2760               'gnus-group-history)
2761              current-prefix-arg)
2762      (error "%s must be invoked from a gnus summary buffer." this-command)))
2763   (unless (or (zerop (length group))
2764               (and gnus-newsgroup-name
2765                    (string-equal gnus-newsgroup-name group)))
2766     (gnus-summary-exit)
2767     (gnus-summary-read-group group show-all
2768                              gnus-dont-select-after-jump-to-other-group)))
2769
2770 (defun gnus-summary-read-group-1 (group show-all no-article
2771                                         kill-buffer no-display
2772                                         &optional select-articles)
2773   ;; Killed foreign groups can't be entered.
2774   (when (and (not (gnus-group-native-p group))
2775              (not (gnus-gethash group gnus-newsrc-hashtb)))
2776     (error "Dead non-native groups can't be entered"))
2777   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2778   (let* ((new-group (gnus-summary-setup-buffer group))
2779          (quit-config (gnus-group-quit-config group))
2780          (did-select (and new-group (gnus-select-newsgroup
2781                                      group show-all select-articles))))
2782     (cond
2783      ;; This summary buffer exists already, so we just select it.
2784      ((not new-group)
2785       (gnus-set-global-variables)
2786       (when kill-buffer
2787         (gnus-kill-or-deaden-summary kill-buffer))
2788       (gnus-configure-windows 'summary 'force)
2789       (gnus-set-mode-line 'summary)
2790       (gnus-summary-position-point)
2791       (message "")
2792       t)
2793      ;; We couldn't select this group.
2794      ((null did-select)
2795       (when (and (eq major-mode 'gnus-summary-mode)
2796                  (not (equal (current-buffer) kill-buffer)))
2797         (kill-buffer (current-buffer))
2798         (if (not quit-config)
2799             (progn
2800               ;; Update the info -- marks might need to be removed,
2801               ;; for instance.
2802               (gnus-summary-update-info)
2803               (set-buffer gnus-group-buffer)
2804               (gnus-group-jump-to-group group)
2805               (gnus-group-next-unread-group 1))
2806           (gnus-handle-ephemeral-exit quit-config)))
2807       (gnus-message 3 "Can't select group")
2808       nil)
2809      ;; The user did a `C-g' while prompting for number of articles,
2810      ;; so we exit this group.
2811      ((eq did-select 'quit)
2812       (and (eq major-mode 'gnus-summary-mode)
2813            (not (equal (current-buffer) kill-buffer))
2814            (kill-buffer (current-buffer)))
2815       (when kill-buffer
2816         (gnus-kill-or-deaden-summary kill-buffer))
2817       (if (not quit-config)
2818           (progn
2819             (set-buffer gnus-group-buffer)
2820             (gnus-group-jump-to-group group)
2821             (gnus-group-next-unread-group 1)
2822             (gnus-configure-windows 'group 'force))
2823         (gnus-handle-ephemeral-exit quit-config))
2824       ;; Finally signal the quit.
2825       (signal 'quit nil))
2826      ;; The group was successfully selected.
2827      (t
2828       (gnus-set-global-variables)
2829       ;; Save the active value in effect when the group was entered.
2830       (setq gnus-newsgroup-active
2831             (gnus-copy-sequence
2832              (gnus-active gnus-newsgroup-name)))
2833       ;; You can change the summary buffer in some way with this hook.
2834       (gnus-run-hooks 'gnus-select-group-hook)
2835       ;; Set any local variables in the group parameters.
2836       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2837       (gnus-update-format-specifications
2838        nil 'summary 'summary-mode 'summary-dummy)
2839       (gnus-update-summary-mark-positions)
2840       ;; Do score processing.
2841       (when gnus-use-scoring
2842         (gnus-possibly-score-headers))
2843       ;; Check whether to fill in the gaps in the threads.
2844       (when gnus-build-sparse-threads
2845         (gnus-build-sparse-threads))
2846       ;; Find the initial limit.
2847       (if gnus-show-threads
2848           (if show-all
2849               (let ((gnus-newsgroup-dormant nil))
2850                 (gnus-summary-initial-limit show-all))
2851             (gnus-summary-initial-limit show-all))
2852         ;; When untreaded, all articles are always shown.
2853         (setq gnus-newsgroup-limit
2854               (mapcar
2855                (lambda (header) (mail-header-number header))
2856                gnus-newsgroup-headers)))
2857       ;; Generate the summary buffer.
2858       (unless no-display
2859         (gnus-summary-prepare))
2860       (when gnus-use-trees
2861         (gnus-tree-open group)
2862         (setq gnus-summary-highlight-line-function
2863               'gnus-tree-highlight-article))
2864       ;; If the summary buffer is empty, but there are some low-scored
2865       ;; articles or some excluded dormants, we include these in the
2866       ;; buffer.
2867       (when (and (zerop (buffer-size))
2868                  (not no-display))
2869         (cond (gnus-newsgroup-dormant
2870                (gnus-summary-limit-include-dormant))
2871               ((and gnus-newsgroup-scored show-all)
2872                (gnus-summary-limit-include-expunged t))))
2873       ;; Function `gnus-apply-kill-file' must be called in this hook.
2874       (gnus-run-hooks 'gnus-apply-kill-hook)
2875       (if (and (zerop (buffer-size))
2876                (not no-display))
2877           (progn
2878             ;; This newsgroup is empty.
2879             (gnus-summary-catchup-and-exit nil t)
2880             (gnus-message 6 "No unread news")
2881             (when kill-buffer
2882               (gnus-kill-or-deaden-summary kill-buffer))
2883             ;; Return nil from this function.
2884             nil)
2885         ;; Hide conversation thread subtrees.  We cannot do this in
2886         ;; gnus-summary-prepare-hook since kill processing may not
2887         ;; work with hidden articles.
2888         (and gnus-show-threads
2889              gnus-thread-hide-subtree
2890              (gnus-summary-hide-all-threads))
2891         (when kill-buffer
2892           (gnus-kill-or-deaden-summary kill-buffer))
2893         ;; Show first unread article if requested.
2894         (if (and (not no-article)
2895                  (not no-display)
2896                  gnus-newsgroup-unreads
2897                  gnus-auto-select-first)
2898             (progn
2899               (gnus-configure-windows 'summary)
2900               (cond
2901                ((eq gnus-auto-select-first 'best)
2902                 (gnus-summary-best-unread-article))
2903                ((eq gnus-auto-select-first t)
2904                 (gnus-summary-first-unread-article))
2905                ((gnus-functionp gnus-auto-select-first)
2906                 (funcall gnus-auto-select-first))))
2907           ;; Don't select any articles, just move point to the first
2908           ;; article in the group.
2909           (goto-char (point-min))
2910           (gnus-summary-position-point)
2911           (gnus-configure-windows 'summary 'force)
2912           (gnus-set-mode-line 'summary))
2913         (when (get-buffer-window gnus-group-buffer t)
2914           ;; Gotta use windows, because recenter does weird stuff if
2915           ;; the current buffer ain't the displayed window.
2916           (let ((owin (selected-window)))
2917             (select-window (get-buffer-window gnus-group-buffer t))
2918             (when (gnus-group-goto-group group)
2919               (recenter))
2920             (select-window owin)))
2921         ;; Mark this buffer as "prepared".
2922         (setq gnus-newsgroup-prepared t)
2923         (gnus-run-hooks 'gnus-summary-prepared-hook)
2924         t)))))
2925
2926 (defun gnus-summary-prepare ()
2927   "Generate the summary buffer."
2928   (interactive)
2929   (let ((buffer-read-only nil))
2930     (erase-buffer)
2931     (setq gnus-newsgroup-data nil
2932           gnus-newsgroup-data-reverse nil)
2933     (gnus-run-hooks 'gnus-summary-generate-hook)
2934     ;; Generate the buffer, either with threads or without.
2935     (when gnus-newsgroup-headers
2936       (gnus-summary-prepare-threads
2937        (if gnus-show-threads
2938            (gnus-sort-gathered-threads
2939             (funcall gnus-summary-thread-gathering-function
2940                      (gnus-sort-threads
2941                       (gnus-cut-threads (gnus-make-threads)))))
2942          ;; Unthreaded display.
2943          (gnus-sort-articles gnus-newsgroup-headers))))
2944     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2945     ;; Call hooks for modifying summary buffer.
2946     (goto-char (point-min))
2947     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2948
2949 (defsubst gnus-general-simplify-subject (subject)
2950   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2951   (setq subject
2952         (cond
2953          ;; Truncate the subject.
2954          (gnus-simplify-subject-functions
2955           (gnus-map-function gnus-simplify-subject-functions subject))
2956          ((numberp gnus-summary-gather-subject-limit)
2957           (setq subject (gnus-simplify-subject-re subject))
2958           (if (> (length subject) gnus-summary-gather-subject-limit)
2959               (substring subject 0 gnus-summary-gather-subject-limit)
2960             subject))
2961          ;; Fuzzily simplify it.
2962          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2963           (gnus-simplify-subject-fuzzy subject))
2964          ;; Just remove the leading "Re:".
2965          (t
2966           (gnus-simplify-subject-re subject))))
2967
2968   (if (and gnus-summary-gather-exclude-subject
2969            (string-match gnus-summary-gather-exclude-subject subject))
2970       nil                               ; This article shouldn't be gathered
2971     subject))
2972
2973 (defun gnus-summary-simplify-subject-query ()
2974   "Query where the respool algorithm would put this article."
2975   (interactive)
2976   (gnus-summary-select-article)
2977   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2978
2979 (defun gnus-gather-threads-by-subject (threads)
2980   "Gather threads by looking at Subject headers."
2981   (if (not gnus-summary-make-false-root)
2982       threads
2983     (let ((hashtb (gnus-make-hashtable 1024))
2984           (prev threads)
2985           (result threads)
2986           subject hthread whole-subject)
2987       (while threads
2988         (setq subject (gnus-general-simplify-subject
2989                        (setq whole-subject (mail-header-subject
2990                                             (caar threads)))))
2991         (when subject
2992           (if (setq hthread (gnus-gethash subject hashtb))
2993               (progn
2994                 ;; We enter a dummy root into the thread, if we
2995                 ;; haven't done that already.
2996                 (unless (stringp (caar hthread))
2997                   (setcar hthread (list whole-subject (car hthread))))
2998                 ;; We add this new gathered thread to this gathered
2999                 ;; thread.
3000                 (setcdr (car hthread)
3001                         (nconc (cdar hthread) (list (car threads))))
3002                 ;; Remove it from the list of threads.
3003                 (setcdr prev (cdr threads))
3004                 (setq threads prev))
3005             ;; Enter this thread into the hash table.
3006             (gnus-sethash subject threads hashtb)))
3007         (setq prev threads)
3008         (setq threads (cdr threads)))
3009       result)))
3010
3011 (defun gnus-gather-threads-by-references (threads)
3012   "Gather threads by looking at References headers."
3013   (let ((idhashtb (gnus-make-hashtable 1024))
3014         (thhashtb (gnus-make-hashtable 1024))
3015         (prev threads)
3016         (result threads)
3017         ids references id gthread gid entered ref)
3018     (while threads
3019       (when (setq references (mail-header-references (caar threads)))
3020         (setq id (mail-header-id (caar threads))
3021               ids (gnus-split-references references)
3022               entered nil)
3023         (while (setq ref (pop ids))
3024           (setq ids (delete ref ids))
3025           (if (not (setq gid (gnus-gethash ref idhashtb)))
3026               (progn
3027                 (gnus-sethash ref id idhashtb)
3028                 (gnus-sethash id threads thhashtb))
3029             (setq gthread (gnus-gethash gid thhashtb))
3030             (unless entered
3031               ;; We enter a dummy root into the thread, if we
3032               ;; haven't done that already.
3033               (unless (stringp (caar gthread))
3034                 (setcar gthread (list (mail-header-subject (caar gthread))
3035                                       (car gthread))))
3036               ;; We add this new gathered thread to this gathered
3037               ;; thread.
3038               (setcdr (car gthread)
3039                       (nconc (cdar gthread) (list (car threads)))))
3040             ;; Add it into the thread hash table.
3041             (gnus-sethash id gthread thhashtb)
3042             (setq entered t)
3043             ;; Remove it from the list of threads.
3044             (setcdr prev (cdr threads))
3045             (setq threads prev))))
3046       (setq prev threads)
3047       (setq threads (cdr threads)))
3048     result))
3049
3050 (defun gnus-sort-gathered-threads (threads)
3051   "Sort subtreads inside each gathered thread by article number."
3052   (let ((result threads))
3053     (while threads
3054       (when (stringp (caar threads))
3055         (setcdr (car threads)
3056                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3057       (setq threads (cdr threads)))
3058     result))
3059
3060 (defun gnus-thread-loop-p (root thread)
3061   "Say whether ROOT is in THREAD."
3062   (let ((stack (list thread))
3063         (infloop 0)
3064         th)
3065     (while (setq thread (pop stack))
3066       (setq th (cdr thread))
3067       (while (and th
3068                   (not (eq (caar th) root)))
3069         (pop th))
3070       (if th
3071           ;; We have found a loop.
3072           (let (ref-dep)
3073             (setcdr thread (delq (car th) (cdr thread)))
3074             (if (boundp (setq ref-dep (intern "none"
3075                                               gnus-newsgroup-dependencies)))
3076                 (setcdr (symbol-value ref-dep)
3077                         (nconc (cdr (symbol-value ref-dep))
3078                                (list (car th))))
3079               (set ref-dep (list nil (car th))))
3080             (setq infloop 1
3081                   stack nil))
3082         ;; Push all the subthreads onto the stack.
3083         (push (cdr thread) stack)))
3084     infloop))
3085
3086 (defun gnus-make-threads ()
3087   "Go through the dependency hashtb and find the roots.  Return all threads."
3088   (let (threads)
3089     (while (catch 'infloop
3090              (mapatoms
3091               (lambda (refs)
3092                 ;; Deal with self-referencing References loops.
3093                 (when (and (car (symbol-value refs))
3094                            (not (zerop
3095                                  (apply
3096                                   '+
3097                                   (mapcar
3098                                    (lambda (thread)
3099                                      (gnus-thread-loop-p
3100                                       (car (symbol-value refs)) thread))
3101                                    (cdr (symbol-value refs)))))))
3102                   (setq threads nil)
3103                   (throw 'infloop t))
3104                 (unless (car (symbol-value refs))
3105                   ;; These threads do not refer back to any other articles,
3106                   ;; so they're roots.
3107                   (setq threads (append (cdr (symbol-value refs)) threads))))
3108               gnus-newsgroup-dependencies)))
3109     threads))
3110
3111 ;; Build the thread tree.
3112 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3113   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3114
3115 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3116 if it was already present.
3117
3118 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3119 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3120 Message-IDs will be renamed be renamed to a unique Message-ID before
3121 being entered.
3122
3123 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3124   (let* ((id (mail-header-id header))
3125          (id-dep (and id (intern id dependencies)))
3126          ref ref-dep ref-header)
3127     ;; Enter this `header' in the `dependencies' table.
3128     (cond
3129      ((not id-dep)
3130       (setq header nil))
3131      ;; The first two cases do the normal part: enter a new `header'
3132      ;; in the `dependencies' table.
3133      ((not (boundp id-dep))
3134       (set id-dep (list header)))
3135      ((null (car (symbol-value id-dep)))
3136       (setcar (symbol-value id-dep) header))
3137
3138      ;; From here the `header' was already present in the
3139      ;; `dependencies' table.
3140      (force-new
3141       ;; Overrides an existing entry;
3142       ;; just set the header part of the entry.
3143       (setcar (symbol-value id-dep) header))
3144
3145      ;; Renames the existing `header' to a unique Message-ID.
3146      ((not gnus-summary-ignore-duplicates)
3147       ;; An article with this Message-ID has already been seen.
3148       ;; We rename the Message-ID.
3149       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3150            (list header))
3151       (mail-header-set-id header id))
3152
3153      ;; The last case ignores an existing entry, except it adds any
3154      ;; additional Xrefs (in case the two articles came from different
3155      ;; servers.
3156      ;; Also sets `header' to `nil' meaning that the `dependencies'
3157      ;; table was *not* modified.
3158      (t
3159       (mail-header-set-xref
3160        (car (symbol-value id-dep))
3161        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3162                    "")
3163                (or (mail-header-xref header) "")))
3164       (setq header nil)))
3165
3166     (when header
3167       ;; First check if that we are not creating a References loop.
3168       (setq ref (gnus-parent-id (mail-header-references header)))
3169       (while (and ref
3170                   (setq ref-dep (intern-soft ref dependencies))
3171                   (boundp ref-dep)
3172                   (setq ref-header (car (symbol-value ref-dep))))
3173         (if (string= id ref)
3174             ;; Yuk!  This is a reference loop.  Make the article be a
3175             ;; root article.
3176             (progn
3177               (mail-header-set-references (car (symbol-value id-dep)) "none")
3178               (setq ref nil))
3179           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3180       (setq ref (gnus-parent-id (mail-header-references header)))
3181       (setq ref-dep (intern (or ref "none") dependencies))
3182       (if (boundp ref-dep)
3183           (setcdr (symbol-value ref-dep)
3184                   (nconc (cdr (symbol-value ref-dep))
3185                          (list (symbol-value id-dep))))
3186         (set ref-dep (list nil (symbol-value id-dep)))))
3187     header))
3188
3189 (defun gnus-build-sparse-threads ()
3190   (let ((headers gnus-newsgroup-headers)
3191         (mail-parse-charset gnus-newsgroup-charset)
3192         (gnus-summary-ignore-duplicates t)
3193         header references generation relations
3194         subject child end new-child date)
3195     ;; First we create an alist of generations/relations, where
3196     ;; generations is how much we trust the relation, and the relation
3197     ;; is parent/child.
3198     (gnus-message 7 "Making sparse threads...")
3199     (save-excursion
3200       (nnheader-set-temp-buffer " *gnus sparse threads*")
3201       (while (setq header (pop headers))
3202         (when (and (setq references (mail-header-references header))
3203                    (not (string= references "")))
3204           (insert references)
3205           (setq child (mail-header-id header)
3206                 subject (mail-header-subject header)
3207                 date (mail-header-date header)
3208                 generation 0)
3209           (while (search-backward ">" nil t)
3210             (setq end (1+ (point)))
3211             (when (search-backward "<" nil t)
3212               (setq new-child (buffer-substring (point) end))
3213               (push (list (incf generation)
3214                           child (setq child new-child)
3215                           subject date)
3216                     relations)))
3217           (when child
3218             (push (list (1+ generation) child nil subject) relations))
3219           (erase-buffer)))
3220       (kill-buffer (current-buffer)))
3221     ;; Sort over trustworthiness.
3222     (mapcar
3223      (lambda (relation)
3224        (when (gnus-dependencies-add-header
3225               (make-full-mail-header
3226                gnus-reffed-article-number
3227                (nth 3 relation) "" (or (nth 4 relation) "")
3228                (nth 1 relation)
3229                (or (nth 2 relation) "") 0 0 "")
3230               gnus-newsgroup-dependencies nil)
3231          (push gnus-reffed-article-number gnus-newsgroup-limit)
3232          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3233          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3234                gnus-newsgroup-reads)
3235          (decf gnus-reffed-article-number)))
3236      (sort relations 'car-less-than-car))
3237     (gnus-message 7 "Making sparse threads...done")))
3238
3239 (defun gnus-build-old-threads ()
3240   ;; Look at all the articles that refer back to old articles, and
3241   ;; fetch the headers for the articles that aren't there.  This will
3242   ;; build complete threads - if the roots haven't been expired by the
3243   ;; server, that is.
3244   (let ((mail-parse-charset gnus-newsgroup-charset)
3245         id heads)
3246     (mapatoms
3247      (lambda (refs)
3248        (when (not (car (symbol-value refs)))
3249          (setq heads (cdr (symbol-value refs)))
3250          (while heads
3251            (if (memq (mail-header-number (caar heads))
3252                      gnus-newsgroup-dormant)
3253                (setq heads (cdr heads))
3254              (setq id (symbol-name refs))
3255              (while (and (setq id (gnus-build-get-header id))
3256                          (not (car (gnus-id-to-thread id)))))
3257              (setq heads nil)))))
3258      gnus-newsgroup-dependencies)))
3259
3260 ;; This function has to be called with point after the article number
3261 ;; on the beginning of the line.
3262 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3263   (let ((eol (gnus-point-at-eol))
3264         (buffer (current-buffer))
3265         header)
3266
3267     ;; overview: [num subject from date id refs chars lines misc]
3268     (unless (eobp)
3269       (forward-char))
3270
3271     (setq header
3272           (make-full-mail-header
3273            number                               ; number
3274            (nnheader-nov-field)                 ; subject
3275            (nnheader-nov-field)                 ; from
3276            (nnheader-nov-field)                 ; date
3277            (nnheader-nov-read-message-id)       ; id
3278            (nnheader-nov-field)                 ; refs
3279            (nnheader-nov-read-integer)          ; chars
3280            (nnheader-nov-read-integer)          ; lines
3281            (unless (eobp)
3282              (nnheader-nov-field))              ; misc
3283            (nnheader-nov-parse-extra)))         ; extra
3284
3285     (when gnus-alter-header-function
3286       (funcall gnus-alter-header-function header))
3287     (gnus-dependencies-add-header header dependencies force-new)))
3288
3289 (defun gnus-build-get-header (id)
3290   ;; Look through the buffer of NOV lines and find the header to
3291   ;; ID.  Enter this line into the dependencies hash table, and return
3292   ;; the id of the parent article (if any).
3293   (let ((deps gnus-newsgroup-dependencies)
3294         found header)
3295     (prog1
3296         (save-excursion
3297           (set-buffer nntp-server-buffer)
3298           (let ((case-fold-search nil))
3299             (goto-char (point-min))
3300             (while (and (not found)
3301                         (search-forward id nil t))
3302               (beginning-of-line)
3303               (setq found (looking-at
3304                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3305                                    (regexp-quote id))))
3306               (or found (beginning-of-line 2)))
3307             (when found
3308               (beginning-of-line)
3309               (and
3310                (setq header (gnus-nov-parse-line
3311                              (read (current-buffer)) deps))
3312                (gnus-parent-id (mail-header-references header))))))
3313       (when header
3314         (let ((number (mail-header-number header)))
3315           (push number gnus-newsgroup-limit)
3316           (push header gnus-newsgroup-headers)
3317           (if (memq number gnus-newsgroup-unselected)
3318               (progn
3319                 (push number gnus-newsgroup-unreads)
3320                 (setq gnus-newsgroup-unselected
3321                       (delq number gnus-newsgroup-unselected)))
3322             (push number gnus-newsgroup-ancient)))))))
3323
3324 (defun gnus-build-all-threads ()
3325   "Read all the headers."
3326   (let ((gnus-summary-ignore-duplicates t)
3327         (mail-parse-charset gnus-newsgroup-charset)
3328         (dependencies gnus-newsgroup-dependencies)
3329         header article)
3330     (save-excursion
3331       (set-buffer nntp-server-buffer)
3332       (let ((case-fold-search nil))
3333         (goto-char (point-min))
3334         (while (not (eobp))
3335           (ignore-errors
3336             (setq article (read (current-buffer))
3337                   header (gnus-nov-parse-line article dependencies)))
3338           (when header
3339             (save-excursion
3340               (set-buffer gnus-summary-buffer)
3341               (push header gnus-newsgroup-headers)
3342               (if (memq (setq article (mail-header-number header))
3343                         gnus-newsgroup-unselected)
3344                   (progn
3345                     (push article gnus-newsgroup-unreads)
3346                     (setq gnus-newsgroup-unselected
3347                           (delq article gnus-newsgroup-unselected)))
3348                 (push article gnus-newsgroup-ancient)))
3349             (forward-line 1)))))))
3350
3351 (defun gnus-summary-update-article-line (article header)
3352   "Update the line for ARTICLE using HEADERS."
3353   (let* ((id (mail-header-id header))
3354          (thread (gnus-id-to-thread id)))
3355     (unless thread
3356       (error "Article in no thread"))
3357     ;; Update the thread.
3358     (setcar thread header)
3359     (gnus-summary-goto-subject article)
3360     (let* ((datal (gnus-data-find-list article))
3361            (data (car datal))
3362            (length (when (cdr datal)
3363                      (- (gnus-data-pos data)
3364                         (gnus-data-pos (cadr datal)))))
3365            (buffer-read-only nil)
3366            (level (gnus-summary-thread-level)))
3367       (gnus-delete-line)
3368       (gnus-summary-insert-line
3369        header level nil (gnus-article-mark article)
3370        (memq article gnus-newsgroup-replied)
3371        (memq article gnus-newsgroup-expirable)
3372        ;; Only insert the Subject string when it's different
3373        ;; from the previous Subject string.
3374        (if (gnus-subject-equal
3375             (condition-case ()
3376                 (mail-header-subject
3377                  (gnus-data-header
3378                   (cadr
3379                    (gnus-data-find-list
3380                     article
3381                     (gnus-data-list t)))))
3382               ;; Error on the side of excessive subjects.
3383               (error ""))
3384             (mail-header-subject header))
3385            ""
3386          (mail-header-subject header))
3387        nil (cdr (assq article gnus-newsgroup-scored))
3388        (memq article gnus-newsgroup-processable))
3389       (when length
3390         (gnus-data-update-list
3391          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3392
3393 (defun gnus-summary-update-article (article &optional iheader)
3394   "Update ARTICLE in the summary buffer."
3395   (set-buffer gnus-summary-buffer)
3396   (let* ((header (gnus-summary-article-header article))
3397          (id (mail-header-id header))
3398          (data (gnus-data-find article))
3399          (thread (gnus-id-to-thread id))
3400          (references (mail-header-references header))
3401          (parent
3402           (gnus-id-to-thread
3403            (or (gnus-parent-id
3404                 (when (and references
3405                            (not (equal "" references)))
3406                   references))
3407                "none")))
3408          (buffer-read-only nil)
3409          (old (car thread)))
3410     (when thread
3411       (unless iheader
3412         (setcar thread nil)
3413         (when parent
3414           (delq thread parent)))
3415       (if (gnus-summary-insert-subject id header)
3416           ;; Set the (possibly) new article number in the data structure.
3417           (gnus-data-set-number data (gnus-id-to-article id))
3418         (setcar thread old)
3419         nil))))
3420
3421 (defun gnus-rebuild-thread (id &optional line)
3422   "Rebuild the thread containing ID.
3423 If LINE, insert the rebuilt thread starting on line LINE."
3424   (let ((buffer-read-only nil)
3425         old-pos current thread data)
3426     (if (not gnus-show-threads)
3427         (setq thread (list (car (gnus-id-to-thread id))))
3428       ;; Get the thread this article is part of.
3429       (setq thread (gnus-remove-thread id)))
3430     (setq old-pos (gnus-point-at-bol))
3431     (setq current (save-excursion
3432                     (and (zerop (forward-line -1))
3433                          (gnus-summary-article-number))))
3434     ;; If this is a gathered thread, we have to go some re-gathering.
3435     (when (stringp (car thread))
3436       (let ((subject (car thread))
3437             roots thr)
3438         (setq thread (cdr thread))
3439         (while thread
3440           (unless (memq (setq thr (gnus-id-to-thread
3441                                    (gnus-root-id
3442                                     (mail-header-id (caar thread)))))
3443                         roots)
3444             (push thr roots))
3445           (setq thread (cdr thread)))
3446         ;; We now have all (unique) roots.
3447         (if (= (length roots) 1)
3448             ;; All the loose roots are now one solid root.
3449             (setq thread (car roots))
3450           (setq thread (cons subject (gnus-sort-threads roots))))))
3451     (let (threads)
3452       ;; We then insert this thread into the summary buffer.
3453       (when line
3454         (goto-char (point-min))
3455         (forward-line (1- line)))
3456       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3457         (if gnus-show-threads
3458             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3459           (gnus-summary-prepare-unthreaded thread))
3460         (setq data (nreverse gnus-newsgroup-data))
3461         (setq threads gnus-newsgroup-threads))
3462       ;; We splice the new data into the data structure.
3463       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3464       ;;!!! then we want to insert at the beginning of the buffer.
3465       ;;!!! That happens to be true with Gnus now, but that may
3466       ;;!!! change in the future.  Perhaps.
3467       (gnus-data-enter-list
3468        (if line nil current) data (- (point) old-pos))
3469       (setq gnus-newsgroup-threads
3470             (nconc threads gnus-newsgroup-threads))
3471       (gnus-data-compute-positions))))
3472
3473 (defun gnus-number-to-header (number)
3474   "Return the header for article NUMBER."
3475   (let ((headers gnus-newsgroup-headers))
3476     (while (and headers
3477                 (not (= number (mail-header-number (car headers)))))
3478       (pop headers))
3479     (when headers
3480       (car headers))))
3481
3482 (defun gnus-parent-headers (in-headers &optional generation)
3483   "Return the headers of the GENERATIONeth parent of HEADERS."
3484   (unless generation
3485     (setq generation 1))
3486   (let ((parent t)
3487         (headers in-headers)
3488         references)
3489     (while (and parent
3490                 (not (zerop generation))
3491                 (setq references (mail-header-references headers)))
3492       (setq headers (if (and references
3493                              (setq parent (gnus-parent-id references)))
3494                         (car (gnus-id-to-thread parent))
3495                       nil))
3496       (decf generation))
3497     (and (not (eq headers in-headers))
3498          headers)))
3499
3500 (defun gnus-id-to-thread (id)
3501   "Return the (sub-)thread where ID appears."
3502   (gnus-gethash id gnus-newsgroup-dependencies))
3503
3504 (defun gnus-id-to-article (id)
3505   "Return the article number of ID."
3506   (let ((thread (gnus-id-to-thread id)))
3507     (when (and thread
3508                (car thread))
3509       (mail-header-number (car thread)))))
3510
3511 (defun gnus-id-to-header (id)
3512   "Return the article headers of ID."
3513   (car (gnus-id-to-thread id)))
3514
3515 (defun gnus-article-displayed-root-p (article)
3516   "Say whether ARTICLE is a root(ish) article."
3517   (let ((level (gnus-summary-thread-level article))
3518         (refs (mail-header-references  (gnus-summary-article-header article)))
3519         particle)
3520     (cond
3521      ((null level) nil)
3522      ((zerop level) t)
3523      ((null refs) t)
3524      ((null (gnus-parent-id refs)) t)
3525      ((and (= 1 level)
3526            (null (setq particle (gnus-id-to-article
3527                                  (gnus-parent-id refs))))
3528            (null (gnus-summary-thread-level particle)))))))
3529
3530 (defun gnus-root-id (id)
3531   "Return the id of the root of the thread where ID appears."
3532   (let (last-id prev)
3533     (while (and id (setq prev (car (gnus-id-to-thread id))))
3534       (setq last-id id
3535             id (gnus-parent-id (mail-header-references prev))))
3536     last-id))
3537
3538 (defun gnus-articles-in-thread (thread)
3539   "Return the list of articles in THREAD."
3540   (cons (mail-header-number (car thread))
3541         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3542
3543 (defun gnus-remove-thread (id &optional dont-remove)
3544   "Remove the thread that has ID in it."
3545   (let (headers thread last-id)
3546     ;; First go up in this thread until we find the root.
3547     (setq last-id (gnus-root-id id)
3548           headers (message-flatten-list (gnus-id-to-thread last-id)))
3549     ;; We have now found the real root of this thread.  It might have
3550     ;; been gathered into some loose thread, so we have to search
3551     ;; through the threads to find the thread we wanted.
3552     (let ((threads gnus-newsgroup-threads)
3553           sub)
3554       (while threads
3555         (setq sub (car threads))
3556         (if (stringp (car sub))
3557             ;; This is a gathered thread, so we look at the roots
3558             ;; below it to find whether this article is in this
3559             ;; gathered root.
3560             (progn
3561               (setq sub (cdr sub))
3562               (while sub
3563                 (when (member (caar sub) headers)
3564                   (setq thread (car threads)
3565                         threads nil
3566                         sub nil))
3567                 (setq sub (cdr sub))))
3568           ;; It's an ordinary thread, so we check it.
3569           (when (eq (car sub) (car headers))
3570             (setq thread sub
3571                   threads nil)))
3572         (setq threads (cdr threads)))
3573       ;; If this article is in no thread, then it's a root.
3574       (if thread
3575           (unless dont-remove
3576             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3577         (setq thread (gnus-id-to-thread last-id)))
3578       (when thread
3579         (prog1
3580             thread                      ; We return this thread.
3581           (unless dont-remove
3582             (if (stringp (car thread))
3583                 (progn
3584                   ;; If we use dummy roots, then we have to remove the
3585                   ;; dummy root as well.
3586                   (when (eq gnus-summary-make-false-root 'dummy)
3587                     ;; We go to the dummy root by going to
3588                     ;; the first sub-"thread", and then one line up.
3589                     (gnus-summary-goto-article
3590                      (mail-header-number (caadr thread)))
3591                     (forward-line -1)
3592                     (gnus-delete-line)
3593                     (gnus-data-compute-positions))
3594                   (setq thread (cdr thread))
3595                   (while thread
3596                     (gnus-remove-thread-1 (car thread))
3597                     (setq thread (cdr thread))))
3598               (gnus-summary-show-all-threads)
3599               (gnus-remove-thread-1 thread))))))))
3600
3601 (defun gnus-remove-thread-1 (thread)
3602   "Remove the thread THREAD recursively."
3603   (let ((number (mail-header-number (pop thread)))
3604         d)
3605     (setq thread (reverse thread))
3606     (while thread
3607       (gnus-remove-thread-1 (pop thread)))
3608     (when (setq d (gnus-data-find number))
3609       (goto-char (gnus-data-pos d))
3610       (gnus-data-remove
3611        number
3612        (- (gnus-point-at-bol)
3613           (prog1
3614               (1+ (gnus-point-at-eol))
3615             (gnus-delete-line)))))))
3616
3617 (defun gnus-sort-threads (threads)
3618   "Sort THREADS."
3619   (if (not gnus-thread-sort-functions)
3620       threads
3621     (gnus-message 8 "Sorting threads...")
3622     (prog1
3623         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3624       (gnus-message 8 "Sorting threads...done"))))
3625
3626 (defun gnus-sort-articles (articles)
3627   "Sort ARTICLES."
3628   (when gnus-article-sort-functions
3629     (gnus-message 7 "Sorting articles...")
3630     (prog1
3631         (setq gnus-newsgroup-headers
3632               (sort articles (gnus-make-sort-function
3633                               gnus-article-sort-functions)))
3634       (gnus-message 7 "Sorting articles...done"))))
3635
3636 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3637 (defmacro gnus-thread-header (thread)
3638   ;; Return header of first article in THREAD.
3639   ;; Note that THREAD must never, ever be anything else than a variable -
3640   ;; using some other form will lead to serious barfage.
3641   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3642   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3643   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3644         (vector thread) 2))
3645
3646 (defsubst gnus-article-sort-by-number (h1 h2)
3647   "Sort articles by article number."
3648   (< (mail-header-number h1)
3649      (mail-header-number h2)))
3650
3651 (defun gnus-thread-sort-by-number (h1 h2)
3652   "Sort threads by root article number."
3653   (gnus-article-sort-by-number
3654    (gnus-thread-header h1) (gnus-thread-header h2)))
3655
3656 (defsubst gnus-article-sort-by-lines (h1 h2)
3657   "Sort articles by article Lines header."
3658   (< (mail-header-lines h1)
3659      (mail-header-lines h2)))
3660
3661 (defun gnus-thread-sort-by-lines (h1 h2)
3662   "Sort threads by root article Lines header."
3663   (gnus-article-sort-by-lines
3664    (gnus-thread-header h1) (gnus-thread-header h2)))
3665
3666 (defsubst gnus-article-sort-by-chars (h1 h2)
3667   "Sort articles by octet length."
3668   (< (mail-header-chars h1)
3669      (mail-header-chars h2)))
3670
3671 (defun gnus-thread-sort-by-chars (h1 h2)
3672   "Sort threads by root article octet length."
3673   (gnus-article-sort-by-chars
3674    (gnus-thread-header h1) (gnus-thread-header h2)))
3675
3676 (defsubst gnus-article-sort-by-author (h1 h2)
3677   "Sort articles by root author."
3678   (string-lessp
3679    (let ((addr (car (mime-read-field 'From h1))))
3680      (or (std11-full-name-string addr)
3681          (std11-address-string addr)
3682          ""))
3683    (let ((addr (car (mime-read-field 'From h2))))
3684      (or (std11-full-name-string addr)
3685          (std11-address-string addr)
3686          ""))
3687    ))
3688
3689 (defun gnus-thread-sort-by-author (h1 h2)
3690   "Sort threads by root author."
3691   (gnus-article-sort-by-author
3692    (gnus-thread-header h1)  (gnus-thread-header h2)))
3693
3694 (defsubst gnus-article-sort-by-subject (h1 h2)
3695   "Sort articles by root subject."
3696   (string-lessp
3697    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3698    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3699
3700 (defun gnus-thread-sort-by-subject (h1 h2)
3701   "Sort threads by root subject."
3702   (gnus-article-sort-by-subject
3703    (gnus-thread-header h1) (gnus-thread-header h2)))
3704
3705 (defsubst gnus-article-sort-by-date (h1 h2)
3706   "Sort articles by root article date."
3707   (time-less-p
3708    (gnus-date-get-time (mail-header-date h1))
3709    (gnus-date-get-time (mail-header-date h2))))
3710
3711 (defun gnus-thread-sort-by-date (h1 h2)
3712   "Sort threads by root article date."
3713   (gnus-article-sort-by-date
3714    (gnus-thread-header h1) (gnus-thread-header h2)))
3715
3716 (defsubst gnus-article-sort-by-score (h1 h2)
3717   "Sort articles by root article score.
3718 Unscored articles will be counted as having a score of zero."
3719   (> (or (cdr (assq (mail-header-number h1)
3720                     gnus-newsgroup-scored))
3721          gnus-summary-default-score 0)
3722      (or (cdr (assq (mail-header-number h2)
3723                     gnus-newsgroup-scored))
3724          gnus-summary-default-score 0)))
3725
3726 (defun gnus-thread-sort-by-score (h1 h2)
3727   "Sort threads by root article score."
3728   (gnus-article-sort-by-score
3729    (gnus-thread-header h1) (gnus-thread-header h2)))
3730
3731 (defun gnus-thread-sort-by-total-score (h1 h2)
3732   "Sort threads by the sum of all scores in the thread.
3733 Unscored articles will be counted as having a score of zero."
3734   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3735
3736 (defun gnus-thread-total-score (thread)
3737   ;; This function find the total score of THREAD.
3738   (cond ((null thread)
3739          0)
3740         ((consp thread)
3741          (if (stringp (car thread))
3742              (apply gnus-thread-score-function 0
3743                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3744            (gnus-thread-total-score-1 thread)))
3745         (t
3746          (gnus-thread-total-score-1 (list thread)))))
3747
3748 (defun gnus-thread-total-score-1 (root)
3749   ;; This function find the total score of the thread below ROOT.
3750   (setq root (car root))
3751   (apply gnus-thread-score-function
3752          (or (append
3753               (mapcar 'gnus-thread-total-score
3754                       (cdr (gnus-id-to-thread (mail-header-id root))))
3755               (when (> (mail-header-number root) 0)
3756                 (list (or (cdr (assq (mail-header-number root)
3757                                      gnus-newsgroup-scored))
3758                           gnus-summary-default-score 0))))
3759              (list gnus-summary-default-score)
3760              '(0))))
3761
3762 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3763 (defvar gnus-tmp-prev-subject nil)
3764 (defvar gnus-tmp-false-parent nil)
3765 (defvar gnus-tmp-root-expunged nil)
3766 (defvar gnus-tmp-dummy-line nil)
3767
3768 (defvar gnus-tmp-header)
3769 (defun gnus-extra-header (type &optional header)
3770   "Return the extra header of TYPE."
3771   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3772       ""))
3773
3774 (defun gnus-summary-prepare-threads (threads)
3775   "Prepare summary buffer from THREADS and indentation LEVEL.
3776 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3777 or a straight list of headers."
3778   (gnus-message 7 "Generating summary...")
3779
3780   (setq gnus-newsgroup-threads threads)
3781   (beginning-of-line)
3782
3783   (let ((gnus-tmp-level 0)
3784         (default-score (or gnus-summary-default-score 0))
3785         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3786         thread number subject stack state gnus-tmp-gathered beg-match
3787         new-roots gnus-tmp-new-adopts thread-end
3788         gnus-tmp-header gnus-tmp-unread
3789         gnus-tmp-replied gnus-tmp-subject-or-nil
3790         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3791         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3792         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3793
3794     (setq gnus-tmp-prev-subject nil)
3795
3796     (if (vectorp (car threads))
3797         ;; If this is a straight (sic) list of headers, then a
3798         ;; threaded summary display isn't required, so we just create
3799         ;; an unthreaded one.
3800         (gnus-summary-prepare-unthreaded threads)
3801
3802       ;; Do the threaded display.
3803
3804       (while (or threads stack gnus-tmp-new-adopts new-roots)
3805
3806         (if (and (= gnus-tmp-level 0)
3807                  (or (not stack)
3808                      (= (caar stack) 0))
3809                  (not gnus-tmp-false-parent)
3810                  (or gnus-tmp-new-adopts new-roots))
3811             (if gnus-tmp-new-adopts
3812                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3813                       thread (list (car gnus-tmp-new-adopts))
3814                       gnus-tmp-header (caar thread)
3815                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3816               (when new-roots
3817                 (setq thread (list (car new-roots))
3818                       gnus-tmp-header (caar thread)
3819                       new-roots (cdr new-roots))))
3820
3821           (if threads
3822               ;; If there are some threads, we do them before the
3823               ;; threads on the stack.
3824               (setq thread threads
3825                     gnus-tmp-header (caar thread))
3826             ;; There were no current threads, so we pop something off
3827             ;; the stack.
3828             (setq state (car stack)
3829                   gnus-tmp-level (car state)
3830                   thread (cdr state)
3831                   stack (cdr stack)
3832                   gnus-tmp-header (caar thread))))
3833
3834         (setq gnus-tmp-false-parent nil)
3835         (setq gnus-tmp-root-expunged nil)
3836         (setq thread-end nil)
3837
3838         (if (stringp gnus-tmp-header)
3839             ;; The header is a dummy root.
3840             (cond
3841              ((eq gnus-summary-make-false-root 'adopt)
3842               ;; We let the first article adopt the rest.
3843               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3844                                                (cddar thread)))
3845               (setq gnus-tmp-gathered
3846                     (nconc (mapcar
3847                             (lambda (h) (mail-header-number (car h)))
3848                             (cddar thread))
3849                            gnus-tmp-gathered))
3850               (setq thread (cons (list (caar thread)
3851                                        (cadar thread))
3852                                  (cdr thread)))
3853               (setq gnus-tmp-level -1
3854                     gnus-tmp-false-parent t))
3855              ((eq gnus-summary-make-false-root 'empty)
3856               ;; We print adopted articles with empty subject fields.
3857               (setq gnus-tmp-gathered
3858                     (nconc (mapcar
3859                             (lambda (h) (mail-header-number (car h)))
3860                             (cddar thread))
3861                            gnus-tmp-gathered))
3862               (setq gnus-tmp-level -1))
3863              ((eq gnus-summary-make-false-root 'dummy)
3864               ;; We remember that we probably want to output a dummy
3865               ;; root.
3866               (setq gnus-tmp-dummy-line gnus-tmp-header)
3867               (setq gnus-tmp-prev-subject gnus-tmp-header))
3868              (t
3869               ;; We do not make a root for the gathered
3870               ;; sub-threads at all.
3871               (setq gnus-tmp-level -1)))
3872
3873           (setq number (mail-header-number gnus-tmp-header)
3874                 subject (mail-header-subject gnus-tmp-header))
3875
3876           (cond
3877            ;; If the thread has changed subject, we might want to make
3878            ;; this subthread into a root.
3879            ((and (null gnus-thread-ignore-subject)
3880                  (not (zerop gnus-tmp-level))
3881                  gnus-tmp-prev-subject
3882                  (not (inline
3883                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3884             (setq new-roots (nconc new-roots (list (car thread)))
3885                   thread-end t
3886                   gnus-tmp-header nil))
3887            ;; If the article lies outside the current limit,
3888            ;; then we do not display it.
3889            ((not (memq number gnus-newsgroup-limit))
3890             (setq gnus-tmp-gathered
3891                   (nconc (mapcar
3892                           (lambda (h) (mail-header-number (car h)))
3893                           (cdar thread))
3894                          gnus-tmp-gathered))
3895             (setq gnus-tmp-new-adopts (if (cdar thread)
3896                                           (append gnus-tmp-new-adopts
3897                                                   (cdar thread))
3898                                         gnus-tmp-new-adopts)
3899                   thread-end t
3900                   gnus-tmp-header nil)
3901             (when (zerop gnus-tmp-level)
3902               (setq gnus-tmp-root-expunged t)))
3903            ;; Perhaps this article is to be marked as read?
3904            ((and gnus-summary-mark-below
3905                  (< (or (cdr (assq number gnus-newsgroup-scored))
3906                         default-score)
3907                     gnus-summary-mark-below)
3908                  ;; Don't touch sparse articles.
3909                  (not (gnus-summary-article-sparse-p number))
3910                  (not (gnus-summary-article-ancient-p number)))
3911             (setq gnus-newsgroup-unreads
3912                   (delq number gnus-newsgroup-unreads))
3913             (if gnus-newsgroup-auto-expire
3914                 (push number gnus-newsgroup-expirable)
3915               (push (cons number gnus-low-score-mark)
3916                     gnus-newsgroup-reads))))
3917
3918           (when gnus-tmp-header
3919             ;; We may have an old dummy line to output before this
3920             ;; article.
3921             (when (and gnus-tmp-dummy-line
3922                        (gnus-subject-equal
3923                         gnus-tmp-dummy-line
3924                         (mail-header-subject gnus-tmp-header)))
3925               (gnus-summary-insert-dummy-line
3926                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3927               (setq gnus-tmp-dummy-line nil))
3928
3929             ;; Compute the mark.
3930             (setq gnus-tmp-unread (gnus-article-mark number))
3931
3932             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3933                                   gnus-tmp-header gnus-tmp-level)
3934                   gnus-newsgroup-data)
3935
3936             ;; Actually insert the line.
3937             (setq
3938              gnus-tmp-subject-or-nil
3939              (cond
3940               ((and gnus-thread-ignore-subject
3941                     gnus-tmp-prev-subject
3942                     (not (inline (gnus-subject-equal
3943                                   gnus-tmp-prev-subject subject))))
3944                subject)
3945               ((zerop gnus-tmp-level)
3946                (if (and (eq gnus-summary-make-false-root 'empty)
3947                         (memq number gnus-tmp-gathered)
3948                         gnus-tmp-prev-subject
3949                         (inline (gnus-subject-equal
3950                                  gnus-tmp-prev-subject subject)))
3951                    gnus-summary-same-subject
3952                  subject))
3953               (t gnus-summary-same-subject)))
3954             (if (and (eq gnus-summary-make-false-root 'adopt)
3955                      (= gnus-tmp-level 1)
3956                      (memq number gnus-tmp-gathered))
3957                 (setq gnus-tmp-opening-bracket ?\<
3958                       gnus-tmp-closing-bracket ?\>)
3959               (setq gnus-tmp-opening-bracket ?\[
3960                     gnus-tmp-closing-bracket ?\]))
3961             (setq
3962              gnus-tmp-indentation
3963              (aref gnus-thread-indent-array gnus-tmp-level)
3964              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3965              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3966                                 gnus-summary-default-score 0)
3967              gnus-tmp-score-char
3968              (if (or (null gnus-summary-default-score)
3969                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3970                          gnus-summary-zcore-fuzz))
3971                  ? ;Whitespace
3972                (if (< gnus-tmp-score gnus-summary-default-score)
3973                    gnus-score-below-mark gnus-score-over-mark))
3974              gnus-tmp-replied
3975              (cond ((memq number gnus-newsgroup-processable)
3976                     gnus-process-mark)
3977                    ((memq number gnus-newsgroup-cached)
3978                     gnus-cached-mark)
3979                    ((memq number gnus-newsgroup-replied)
3980                     gnus-replied-mark)
3981                    ((memq number gnus-newsgroup-saved)
3982                     gnus-saved-mark)
3983                    (t gnus-unread-mark))
3984              gnus-tmp-from (mail-header-from gnus-tmp-header)
3985              gnus-tmp-name
3986              (cond
3987               ((string-match "<[^>]+> *$" gnus-tmp-from)
3988                (setq beg-match (match-beginning 0))
3989                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3990                         (substring gnus-tmp-from (1+ (match-beginning 0))
3991                                    (1- (match-end 0))))
3992                    (substring gnus-tmp-from 0 beg-match)))
3993               ((string-match "(.+)" gnus-tmp-from)
3994                (substring gnus-tmp-from
3995                           (1+ (match-beginning 0)) (1- (match-end 0))))
3996               (t gnus-tmp-from)))
3997             (when (string= gnus-tmp-name "")
3998               (setq gnus-tmp-name gnus-tmp-from))
3999             (unless (numberp gnus-tmp-lines)
4000               (setq gnus-tmp-lines 0))
4001             (gnus-put-text-property
4002              (point)
4003              (progn (eval gnus-summary-line-format-spec) (point))
4004              'gnus-number number)
4005             (when gnus-visual-p
4006               (forward-line -1)
4007               (gnus-run-hooks 'gnus-summary-update-hook)
4008               (forward-line 1))
4009
4010             (setq gnus-tmp-prev-subject subject)))
4011
4012         (when (nth 1 thread)
4013           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4014         (incf gnus-tmp-level)
4015         (setq threads (if thread-end nil (cdar thread)))
4016         (unless threads
4017           (setq gnus-tmp-level 0)))))
4018   (gnus-message 7 "Generating summary...done"))
4019
4020 (defun gnus-summary-prepare-unthreaded (headers)
4021   "Generate an unthreaded summary buffer based on HEADERS."
4022   (let (header number mark)
4023
4024     (beginning-of-line)
4025
4026     (while headers
4027       ;; We may have to root out some bad articles...
4028       (when (memq (setq number (mail-header-number
4029                                 (setq header (pop headers))))
4030                   gnus-newsgroup-limit)
4031         ;; Mark article as read when it has a low score.
4032         (when (and gnus-summary-mark-below
4033                    (< (or (cdr (assq number gnus-newsgroup-scored))
4034                           gnus-summary-default-score 0)
4035                       gnus-summary-mark-below)
4036                    (not (gnus-summary-article-ancient-p number)))
4037           (setq gnus-newsgroup-unreads
4038                 (delq number gnus-newsgroup-unreads))
4039           (if gnus-newsgroup-auto-expire
4040               (push number gnus-newsgroup-expirable)
4041             (push (cons number gnus-low-score-mark)
4042                   gnus-newsgroup-reads)))
4043
4044         (setq mark (gnus-article-mark number))
4045         (push (gnus-data-make number mark (1+ (point)) header 0)
4046               gnus-newsgroup-data)
4047         (gnus-summary-insert-line
4048          header 0 number
4049          mark (memq number gnus-newsgroup-replied)
4050          (memq number gnus-newsgroup-expirable)
4051          (mail-header-subject header) nil
4052          (cdr (assq number gnus-newsgroup-scored))
4053          (memq number gnus-newsgroup-processable))))))
4054
4055 (defun gnus-summary-remove-list-identifiers ()
4056   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4057   (let ((regexp (if (stringp gnus-list-identifiers)
4058                     gnus-list-identifiers
4059                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4060     (dolist (header gnus-newsgroup-headers)
4061       (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4062                           (mail-header-subject header))
4063         (mail-header-set-subject
4064          header (concat (substring (mail-header-subject header)
4065                                    0 (match-beginning 2))
4066                         (substring (mail-header-subject header)
4067                                    (match-end 2))))))))
4068
4069 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4070   "Select newsgroup GROUP.
4071 If READ-ALL is non-nil, all articles in the group are selected.
4072 If SELECT-ARTICLES, only select those articles from GROUP."
4073   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4074          ;;!!! Dirty hack; should be removed.
4075          (gnus-summary-ignore-duplicates
4076           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4077               t
4078             gnus-summary-ignore-duplicates))
4079          (info (nth 2 entry))
4080          articles fetched-articles cached)
4081
4082     (unless (gnus-check-server
4083              (setq gnus-current-select-method
4084                    (gnus-find-method-for-group group)))
4085       (error "Couldn't open server"))
4086
4087     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4088         (gnus-activate-group group)     ; Or we can activate it...
4089         (progn                          ; Or we bug out.
4090           (when (equal major-mode 'gnus-summary-mode)
4091             (kill-buffer (current-buffer)))
4092           (error "Couldn't request group %s: %s"
4093                  group (gnus-status-message group))))
4094
4095     (unless (gnus-request-group group t)
4096       (when (equal major-mode 'gnus-summary-mode)
4097         (kill-buffer (current-buffer)))
4098       (error "Couldn't request group %s: %s"
4099              group (gnus-status-message group)))
4100
4101     (setq gnus-newsgroup-name group)
4102     (setq gnus-newsgroup-unselected nil)
4103     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4104     (gnus-summary-setup-default-charset)
4105
4106     ;; Adjust and set lists of article marks.
4107     (when info
4108       (gnus-adjust-marked-articles info))
4109
4110     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4111     (setq cached
4112           (if (gnus-virtual-group-p group)
4113               gnus-newsgroup-cached
4114             (gnus-cache-articles-in-group group)))
4115
4116     (setq gnus-newsgroup-unreads
4117           (gnus-set-difference
4118            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4119            gnus-newsgroup-dormant))
4120
4121     (setq gnus-newsgroup-processable nil)
4122
4123     (gnus-update-read-articles group gnus-newsgroup-unreads)
4124
4125     (if (setq articles select-articles)
4126         (setq gnus-newsgroup-unselected
4127               (gnus-sorted-intersection
4128                gnus-newsgroup-unreads
4129                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4130       (setq articles (gnus-articles-to-read group read-all)))
4131
4132     (cond
4133      ((null articles)
4134       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4135       'quit)
4136      ((eq articles 0) nil)
4137      (t
4138       ;; Init the dependencies hash table.
4139       (setq gnus-newsgroup-dependencies
4140             (gnus-make-hashtable (length articles)))
4141       (gnus-set-global-variables)
4142       ;; Retrieve the headers and read them in.
4143       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4144       (setq gnus-newsgroup-headers
4145             (gnus-retrieve-parsed-headers
4146              articles gnus-newsgroup-name
4147              ;; We might want to fetch old headers, but
4148              ;; not if there is only 1 article.
4149              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4150                            (not (numberp gnus-fetch-old-headers)))
4151                       (> (length articles) 1))
4152                   gnus-fetch-old-headers)))
4153       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4154
4155       ;; Suppress duplicates?
4156       (when gnus-suppress-duplicates
4157         (gnus-dup-suppress-articles))
4158
4159       ;; Set the initial limit.
4160       (setq gnus-newsgroup-limit (copy-sequence articles))
4161       ;; Remove canceled articles from the list of unread articles.
4162       (setq gnus-newsgroup-unreads
4163             (gnus-set-sorted-intersection
4164              gnus-newsgroup-unreads
4165              (setq fetched-articles
4166                    (mapcar (lambda (headers) (mail-header-number headers))
4167                            gnus-newsgroup-headers))))
4168       ;; Removed marked articles that do not exist.
4169       (gnus-update-missing-marks
4170        (gnus-sorted-complement fetched-articles articles))
4171
4172       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4173       (when cached
4174         (setq gnus-newsgroup-cached cached))
4175
4176       ;; We might want to build some more threads first.
4177       (when (and gnus-fetch-old-headers
4178                  (eq gnus-headers-retrieved-by 'nov))
4179         (if (eq gnus-fetch-old-headers 'invisible)
4180             (gnus-build-all-threads)
4181           (gnus-build-old-threads)))
4182       ;; Let the Gnus agent mark articles as read.
4183       (when gnus-agent
4184         (gnus-agent-get-undownloaded-list))
4185       ;; Remove list identifiers from subject
4186       (when gnus-list-identifiers
4187         (gnus-summary-remove-list-identifiers))
4188       ;; Check whether auto-expire is to be done in this group.
4189       (setq gnus-newsgroup-auto-expire
4190             (gnus-group-auto-expirable-p group))
4191       ;; Set up the article buffer now, if necessary.
4192       (unless gnus-single-article-buffer
4193         (gnus-article-setup-buffer))
4194       ;; First and last article in this newsgroup.
4195       (when gnus-newsgroup-headers
4196         (setq gnus-newsgroup-begin
4197               (mail-header-number (car gnus-newsgroup-headers))
4198               gnus-newsgroup-end
4199               (mail-header-number
4200                (gnus-last-element gnus-newsgroup-headers))))
4201       ;; GROUP is successfully selected.
4202       (or gnus-newsgroup-headers t)))))
4203
4204 (defun gnus-articles-to-read (group &optional read-all)
4205   ;; Find out what articles the user wants to read.
4206   (let* ((articles
4207           ;; Select all articles if `read-all' is non-nil, or if there
4208           ;; are no unread articles.
4209           (if (or read-all
4210                   (and (zerop (length gnus-newsgroup-marked))
4211                        (zerop (length gnus-newsgroup-unreads)))
4212                   (eq (gnus-group-find-parameter group 'display)
4213                       'all))
4214               (or
4215                (gnus-uncompress-range (gnus-active group))
4216                (gnus-cache-articles-in-group group))
4217             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4218                           (copy-sequence gnus-newsgroup-unreads))
4219                   '<)))
4220          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4221          (scored (length scored-list))
4222          (number (length articles))
4223          (marked (+ (length gnus-newsgroup-marked)
4224                     (length gnus-newsgroup-dormant)))
4225          (select
4226           (cond
4227            ((numberp read-all)
4228             read-all)
4229            (t
4230             (condition-case ()
4231                 (cond
4232                  ((and (or (<= scored marked) (= scored number))
4233                        (natnump gnus-large-newsgroup)
4234                        (> number gnus-large-newsgroup))
4235                   (let ((input (read-from-minibuffer
4236                                 (format
4237                                  "How many articles from %s (max %d): "
4238                                  (gnus-limit-string gnus-newsgroup-name 35)
4239                                  number)
4240                                 (static-if (< emacs-major-version 20)
4241                                     (number-to-string gnus-large-newsgroup)
4242                                   (cons
4243                                    (number-to-string gnus-large-newsgroup)
4244                                    0)))))
4245                     (if (string-match "^[ \t]*$" input)
4246                         number
4247                       input)))
4248                  ((and (> scored marked) (< scored number)
4249                        (> (- scored number) 20))
4250                   (let ((input
4251                          (read-string
4252                           (format "%s %s (%d scored, %d total): "
4253                                   "How many articles from"
4254                                   group scored number))))
4255                     (if (string-match "^[ \t]*$" input)
4256                         number input)))
4257                  (t number))
4258               (quit nil))))))
4259     (setq select (if (stringp select) (string-to-number select) select))
4260     (if (or (null select) (zerop select))
4261         select
4262       (if (and (not (zerop scored)) (<= (abs select) scored))
4263           (progn
4264             (setq articles (sort scored-list '<))
4265             (setq number (length articles)))
4266         (setq articles (copy-sequence articles)))
4267
4268       (when (< (abs select) number)
4269         (if (< select 0)
4270             ;; Select the N oldest articles.
4271             (setcdr (nthcdr (1- (abs select)) articles) nil)
4272           ;; Select the N most recent articles.
4273           (setq articles (nthcdr (- number select) articles))))
4274       (setq gnus-newsgroup-unselected
4275             (gnus-sorted-intersection
4276              gnus-newsgroup-unreads
4277              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4278       articles)))
4279
4280 (defun gnus-killed-articles (killed articles)
4281   (let (out)
4282     (while articles
4283       (when (inline (gnus-member-of-range (car articles) killed))
4284         (push (car articles) out))
4285       (setq articles (cdr articles)))
4286     out))
4287
4288 (defun gnus-uncompress-marks (marks)
4289   "Uncompress the mark ranges in MARKS."
4290   (let ((uncompressed '(score bookmark))
4291         out)
4292     (while marks
4293       (if (memq (caar marks) uncompressed)
4294           (push (car marks) out)
4295         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4296       (setq marks (cdr marks)))
4297     out))
4298
4299 (defun gnus-adjust-marked-articles (info)
4300   "Set all article lists and remove all marks that are no longer valid."
4301   (let* ((marked-lists (gnus-info-marks info))
4302          (active (gnus-active (gnus-info-group info)))
4303          (min (car active))
4304          (max (cdr active))
4305          (types gnus-article-mark-lists)
4306          (uncompressed '(score bookmark killed))
4307          marks var articles article mark)
4308
4309     (while marked-lists
4310       (setq marks (pop marked-lists))
4311       (set (setq var (intern (format "gnus-newsgroup-%s"
4312                                      (car (rassq (setq mark (car marks))
4313                                                  types)))))
4314            (if (memq (car marks) uncompressed) (cdr marks)
4315              (gnus-uncompress-range (cdr marks))))
4316
4317       (setq articles (symbol-value var))
4318
4319       ;; All articles have to be subsets of the active articles.
4320       (cond
4321        ;; Adjust "simple" lists.
4322        ((memq mark '(tick dormant expire reply save))
4323         (while articles
4324           (when (or (< (setq article (pop articles)) min) (> article max))
4325             (set var (delq article (symbol-value var))))))
4326        ;; Adjust assocs.
4327        ((memq mark uncompressed)
4328         (when (not (listp (cdr (symbol-value var))))
4329           (set var (list (symbol-value var))))
4330         (when (not (listp (cdr articles)))
4331           (setq articles (list articles)))
4332         (while articles
4333           (when (or (not (consp (setq article (pop articles))))
4334                     (< (car article) min)
4335                     (> (car article) max))
4336             (set var (delq article (symbol-value var))))))))))
4337
4338 (defun gnus-update-missing-marks (missing)
4339   "Go through the list of MISSING articles and remove them from the mark lists."
4340   (when missing
4341     (let ((types gnus-article-mark-lists)
4342           var m)
4343       ;; Go through all types.
4344       (while types
4345         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4346         (when (symbol-value var)
4347           ;; This list has articles.  So we delete all missing articles
4348           ;; from it.
4349           (setq m missing)
4350           (while m
4351             (set var (delq (pop m) (symbol-value var)))))))))
4352
4353 (defun gnus-update-marks ()
4354   "Enter the various lists of marked articles into the newsgroup info list."
4355   (let ((types gnus-article-mark-lists)
4356         (info (gnus-get-info gnus-newsgroup-name))
4357         (uncompressed '(score bookmark killed))
4358         type list newmarked symbol delta-marks)
4359     (when info
4360       ;; Add all marks lists to the list of marks lists.
4361       (while (setq type (pop types))
4362         (setq list (symbol-value
4363                           (setq symbol
4364                                 (intern (format "gnus-newsgroup-%s"
4365                                                 (car type))))))
4366
4367         (when list
4368           ;; Get rid of the entries of the articles that have the
4369           ;; default score.
4370           (when (and (eq (cdr type) 'score)
4371                      gnus-save-score
4372                      list)
4373             (let* ((arts list)
4374                    (prev (cons nil list))
4375                    (all prev))
4376               (while arts
4377                 (if (or (not (consp (car arts)))
4378                         (= (cdar arts) gnus-summary-default-score))
4379                     (setcdr prev (cdr arts))
4380                   (setq prev arts))
4381                 (setq arts (cdr arts)))
4382               (setq list (cdr all)))))
4383
4384        (or (memq (cdr type) uncompressed)
4385            (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4386        
4387        (when (gnus-check-backend-function 'request-set-mark
4388                                           gnus-newsgroup-name)
4389          ;; uncompressed:s are not proper flags (they are cons cells)
4390          ;; cache is a internal gnus flag
4391          (unless (memq (cdr type) (cons 'cache uncompressed))
4392            (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4393                   (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4394                   (add (gnus-remove-from-range (gnus-copy-sequence list) old)))
4395              (if add
4396                  (push (list add 'add (list (cdr type))) delta-marks))
4397              (if del
4398                  (push (list del 'del (list (cdr type))) delta-marks)))))
4399           
4400         (when list
4401          (push (cons (cdr type) list) newmarked)))
4402
4403       (when delta-marks
4404         (unless (gnus-check-group gnus-newsgroup-name)
4405           (error "Can't open server for %s" gnus-newsgroup-name))
4406         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4407           
4408       ;; Enter these new marks into the info of the group.
4409       (if (nthcdr 3 info)
4410           (setcar (nthcdr 3 info) newmarked)
4411         ;; Add the marks lists to the end of the info.
4412         (when newmarked
4413           (setcdr (nthcdr 2 info) (list newmarked))))
4414
4415       ;; Cut off the end of the info if there's nothing else there.
4416       (let ((i 5))
4417         (while (and (> i 2)
4418                     (not (nth i info)))
4419           (when (nthcdr (decf i) info)
4420             (setcdr (nthcdr i info) nil)))))))
4421
4422 (defun gnus-set-mode-line (where)
4423   "This function sets the mode line of the article or summary buffers.
4424 If WHERE is `summary', the summary mode line format will be used."
4425   ;; Is this mode line one we keep updated?
4426   (when (and (memq where gnus-updated-mode-lines)
4427              (symbol-value
4428               (intern (format "gnus-%s-mode-line-format-spec" where))))
4429     (let (mode-string)
4430       (save-excursion
4431         ;; We evaluate this in the summary buffer since these
4432         ;; variables are buffer-local to that buffer.
4433         (set-buffer gnus-summary-buffer)
4434         ;; We bind all these variables that are used in the `eval' form
4435         ;; below.
4436         (let* ((mformat (symbol-value
4437                          (intern
4438                           (format "gnus-%s-mode-line-format-spec" where))))
4439                (gnus-tmp-group-name gnus-newsgroup-name)
4440                (gnus-tmp-article-number (or gnus-current-article 0))
4441                (gnus-tmp-unread gnus-newsgroup-unreads)
4442                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4443                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4444                (gnus-tmp-unread-and-unselected
4445                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4446                             (zerop gnus-tmp-unselected))
4447                        "")
4448                       ((zerop gnus-tmp-unselected)
4449                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4450                       (t (format "{%d(+%d) more}"
4451                                  gnus-tmp-unread-and-unticked
4452                                  gnus-tmp-unselected))))
4453                (gnus-tmp-subject
4454                 (if (and gnus-current-headers
4455                          (vectorp gnus-current-headers))
4456                     (gnus-mode-string-quote
4457                      (mail-header-subject gnus-current-headers))
4458                   ""))
4459                bufname-length max-len
4460                gnus-tmp-header);; passed as argument to any user-format-funcs
4461           (setq mode-string (eval mformat))
4462           (setq bufname-length (if (string-match "%b" mode-string)
4463                                    (- (length
4464                                        (buffer-name
4465                                         (if (eq where 'summary)
4466                                             nil
4467                                           (get-buffer gnus-article-buffer))))
4468                                       2)
4469                                  0))
4470           (setq max-len (max 4 (if gnus-mode-non-string-length
4471                                    (- (window-width)
4472                                       gnus-mode-non-string-length
4473                                       bufname-length)
4474                                  (length mode-string))))
4475           ;; We might have to chop a bit of the string off...
4476           (when (> (length mode-string) max-len)
4477             (setq mode-string
4478                   (concat (gnus-truncate-string mode-string (- max-len 3))
4479                           "...")))
4480           ;; Pad the mode string a bit.
4481           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4482       ;; Update the mode line.
4483       (setq mode-line-buffer-identification
4484             (gnus-mode-line-buffer-identification (list mode-string)))
4485       (set-buffer-modified-p t))))
4486
4487 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4488   "Go through the HEADERS list and add all Xrefs to a hash table.
4489 The resulting hash table is returned, or nil if no Xrefs were found."
4490   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4491          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4492          (xref-hashtb (gnus-make-hashtable))
4493          start group entry number xrefs header)
4494     (while headers
4495       (setq header (pop headers))
4496       (when (and (setq xrefs (mail-header-xref header))
4497                  (not (memq (setq number (mail-header-number header))
4498                             unreads)))
4499         (setq start 0)
4500         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4501           (setq start (match-end 0))
4502           (setq group (if prefix
4503                           (concat prefix (substring xrefs (match-beginning 1)
4504                                                     (match-end 1)))
4505                         (substring xrefs (match-beginning 1) (match-end 1))))
4506           (setq number
4507                 (string-to-int (substring xrefs (match-beginning 2)
4508                                           (match-end 2))))
4509           (if (setq entry (gnus-gethash group xref-hashtb))
4510               (setcdr entry (cons number (cdr entry)))
4511             (gnus-sethash group (cons number nil) xref-hashtb)))))
4512     (and start xref-hashtb)))
4513
4514 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4515   "Look through all the headers and mark the Xrefs as read."
4516   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4517         name entry info xref-hashtb idlist method nth4)
4518     (save-excursion
4519       (set-buffer gnus-group-buffer)
4520       (when (setq xref-hashtb
4521                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4522         (mapatoms
4523          (lambda (group)
4524            (unless (string= from-newsgroup (setq name (symbol-name group)))
4525              (setq idlist (symbol-value group))
4526              ;; Dead groups are not updated.
4527              (and (prog1
4528                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4529                             info (nth 2 entry))
4530                     (when (stringp (setq nth4 (gnus-info-method info)))
4531                       (setq nth4 (gnus-server-to-method nth4))))
4532                   ;; Only do the xrefs if the group has the same
4533                   ;; select method as the group we have just read.
4534                   (or (gnus-methods-equal-p
4535                        nth4 (gnus-find-method-for-group from-newsgroup))
4536                       virtual
4537                       (equal nth4 (setq method (gnus-find-method-for-group
4538                                                 from-newsgroup)))
4539                       (and (equal (car nth4) (car method))
4540                            (equal (nth 1 nth4) (nth 1 method))))
4541                   gnus-use-cross-reference
4542                   (or (not (eq gnus-use-cross-reference t))
4543                       virtual
4544                       ;; Only do cross-references on subscribed
4545                       ;; groups, if that is what is wanted.
4546                       (<= (gnus-info-level info) gnus-level-subscribed))
4547                   (gnus-group-make-articles-read name idlist))))
4548          xref-hashtb)))))
4549
4550 (defun gnus-compute-read-articles (group articles)
4551   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4552          (info (nth 2 entry))
4553          (active (gnus-active group))
4554          ninfo)
4555     (when entry
4556       ;; First peel off all invalid article numbers.
4557       (when active
4558         (let ((ids articles)
4559               id first)
4560           (while (setq id (pop ids))
4561             (when (and first (> id (cdr active)))
4562               ;; We'll end up in this situation in one particular
4563               ;; obscure situation.  If you re-scan a group and get
4564               ;; a new article that is cross-posted to a different
4565               ;; group that has not been re-scanned, you might get
4566               ;; crossposted article that has a higher number than
4567               ;; Gnus believes possible.  So we re-activate this
4568               ;; group as well.  This might mean doing the
4569               ;; crossposting thingy will *increase* the number
4570               ;; of articles in some groups.  Tsk, tsk.
4571               (setq active (or (gnus-activate-group group) active)))
4572             (when (or (> id (cdr active))
4573                       (< id (car active)))
4574               (setq articles (delq id articles))))))
4575       ;; If the read list is nil, we init it.
4576       (if (and active
4577                (null (gnus-info-read info))
4578                (> (car active) 1))
4579           (setq ninfo (cons 1 (1- (car active))))
4580         (setq ninfo (gnus-info-read info)))
4581       ;; Then we add the read articles to the range.
4582       (gnus-add-to-range
4583        ninfo (setq articles (sort articles '<))))))
4584
4585 (defun gnus-group-make-articles-read (group articles)
4586   "Update the info of GROUP to say that ARTICLES are read."
4587   (let* ((num 0)
4588          (entry (gnus-gethash group gnus-newsrc-hashtb))
4589          (info (nth 2 entry))
4590          (active (gnus-active group))
4591          range)
4592     (when entry
4593       (setq range (gnus-compute-read-articles group articles))
4594       (save-excursion
4595         (set-buffer gnus-group-buffer)
4596         (gnus-undo-register
4597           `(progn
4598              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4599              (gnus-info-set-read ',info ',(gnus-info-read info))
4600              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4601              (gnus-group-update-group ,group t))))
4602       ;; Add the read articles to the range.
4603       (gnus-info-set-read info range)
4604       ;; Then we have to re-compute how many unread
4605       ;; articles there are in this group.
4606       (when active
4607         (cond
4608          ((not range)
4609           (setq num (- (1+ (cdr active)) (car active))))
4610          ((not (listp (cdr range)))
4611           (setq num (- (cdr active) (- (1+ (cdr range))
4612                                        (car range)))))
4613          (t
4614           (while range
4615             (if (numberp (car range))
4616                 (setq num (1+ num))
4617               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4618             (setq range (cdr range)))
4619           (setq num (- (cdr active) num))))
4620         ;; Update the number of unread articles.
4621         (setcar entry num)
4622         ;; Update the group buffer.
4623         (gnus-group-update-group group t)))))
4624
4625 (defvar gnus-newsgroup-none-id 0)
4626
4627 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4628   (let ((cur nntp-server-buffer)
4629         (dependencies
4630          (or dependencies
4631              (save-excursion (set-buffer gnus-summary-buffer)
4632                              gnus-newsgroup-dependencies)))
4633         headers id end ref
4634         (mail-parse-charset gnus-newsgroup-charset)
4635         (mail-parse-ignored-charsets
4636          (save-excursion (condition-case nil
4637                              (set-buffer gnus-summary-buffer)
4638                            (error))
4639                          gnus-newsgroup-ignored-charsets)))
4640     (save-excursion
4641       (set-buffer nntp-server-buffer)
4642       ;; Translate all TAB characters into SPACE characters.
4643       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4644       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4645       (gnus-run-hooks 'gnus-parse-headers-hook)
4646       (let ((case-fold-search t)
4647             in-reply-to header p lines chars ctype)
4648         (goto-char (point-min))
4649         ;; Search to the beginning of the next header.  Error messages
4650         ;; do not begin with 2 or 3.
4651         (while (re-search-forward "^[23][0-9]+ " nil t)
4652           (setq id nil
4653                 ref nil)
4654           ;; This implementation of this function, with nine
4655           ;; search-forwards instead of the one re-search-forward and
4656           ;; a case (which basically was the old function) is actually
4657           ;; about twice as fast, even though it looks messier.  You
4658           ;; can't have everything, I guess.  Speed and elegance
4659           ;; doesn't always go hand in hand.
4660           (setq
4661            header
4662            (make-full-mail-header
4663             ;; Number.
4664             (prog1
4665                 (read cur)
4666               (end-of-line)
4667               (setq p (point))
4668               (narrow-to-region (point)
4669                                 (or (and (search-forward "\n.\n" nil t)
4670                                          (- (point) 2))
4671                                     (point))))
4672             ;; Subject.
4673             (progn
4674               (goto-char p)
4675               (if (search-forward "\nsubject: " nil t)
4676                   (buffer-substring (match-end 0) (std11-field-end))
4677                 "(none)"))
4678             ;; From.
4679             (progn
4680               (goto-char p)
4681               (if (search-forward "\nfrom: " nil t)
4682                   (buffer-substring (match-end 0) (std11-field-end))
4683                 "(nobody)"))
4684             ;; Date.
4685             (progn
4686               (goto-char p)
4687               (if (search-forward "\ndate: " nil t)
4688                   (buffer-substring (match-end 0) (std11-field-end))
4689                 ""))
4690             ;; Message-ID.
4691             (progn
4692               (goto-char p)
4693               (setq id (if (re-search-forward
4694                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4695                            ;; We do it this way to make sure the Message-ID
4696                            ;; is (somewhat) syntactically valid.
4697                            (buffer-substring (match-beginning 1)
4698                                              (match-end 1))
4699                          ;; If there was no message-id, we just fake one
4700                          ;; to make subsequent routines simpler.
4701                          (nnheader-generate-fake-message-id))))
4702             ;; References.
4703             (progn
4704               (goto-char p)
4705               (if (search-forward "\nreferences: " nil t)
4706                   (progn
4707                     (setq end (point))
4708                     (prog1
4709                         (buffer-substring (match-end 0) (std11-field-end))
4710                       (setq ref
4711                             (buffer-substring
4712                              (progn
4713                                ;; (end-of-line)
4714                                (search-backward ">" end t)
4715                                (1+ (point)))
4716                              (progn
4717                                (search-backward "<" end t)
4718                                (point))))))
4719                 ;; Get the references from the in-reply-to header if there
4720                 ;; were no references and the in-reply-to header looks
4721                 ;; promising.
4722                 (if (and (search-forward "\nin-reply-to: " nil t)
4723                          (setq in-reply-to
4724                                (buffer-substring (match-end 0)
4725                                                  (std11-field-end)))
4726                          (string-match "<[^>]+>" in-reply-to))
4727                     (let (ref2)
4728                       (setq ref (substring in-reply-to (match-beginning 0)
4729                                            (match-end 0)))
4730                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4731                         (setq ref2 (substring in-reply-to (match-beginning 0)
4732                                               (match-end 0)))
4733                         (when (> (length ref2) (length ref))
4734                           (setq ref ref2)))
4735                       ref)
4736                   (setq ref nil))))
4737             ;; Chars.
4738             (progn
4739               (goto-char p)
4740               (if (search-forward "\nchars: " nil t)
4741                   (if (numberp (setq chars (ignore-errors (read cur))))
4742                       chars 0)
4743                 0))
4744             ;; Lines.
4745             (progn
4746               (goto-char p)
4747               (if (search-forward "\nlines: " nil t)
4748                   (if (numberp (setq lines (ignore-errors (read cur))))
4749                       lines 0)
4750                 0))
4751             ;; Xref.
4752             (progn
4753               (goto-char p)
4754               (and (search-forward "\nxref: " nil t)
4755                    (buffer-substring (match-end 0) (std11-field-end))))
4756             ;; Extra.
4757             (when gnus-extra-headers
4758               (let ((extra gnus-extra-headers)
4759                     out)
4760                 (while extra
4761                   (goto-char p)
4762                   (when (search-forward
4763                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4764                     (push (cons (car extra)
4765                                 (buffer-substring (match-end 0)
4766                                                   (std11-field-end)))
4767                           out))
4768                   (pop extra))
4769                 out))))
4770           (goto-char p)
4771           (if (and (search-forward "\ncontent-type: " nil t)
4772                    (setq ctype
4773                          (buffer-substring (match-end 0) (std11-field-end))))
4774               (mime-entity-set-content-type-internal
4775                header (mime-parse-Content-Type ctype)))
4776           (when (equal id ref)
4777             (setq ref nil))
4778
4779           (when gnus-alter-header-function
4780             (funcall gnus-alter-header-function header)
4781             (setq id (mail-header-id header)
4782                   ref (gnus-parent-id (mail-header-references header))))
4783
4784           (when (setq header
4785                       (gnus-dependencies-add-header
4786                        header dependencies force-new))
4787             (push header headers))
4788           (goto-char (point-max))
4789           (widen))
4790         (nreverse headers)))))
4791
4792 ;; Goes through the xover lines and returns a list of vectors
4793 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4794                                                   force-new dependencies
4795                                                   group also-fetch-heads)
4796   "Parse the news overview data in the server buffer, and return a
4797 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4798   ;; Get the Xref when the users reads the articles since most/some
4799   ;; NNTP servers do not include Xrefs when using XOVER.
4800   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4801   (let ((mail-parse-charset gnus-newsgroup-charset)
4802         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4803         (cur nntp-server-buffer)
4804         (dependencies (or dependencies gnus-newsgroup-dependencies))
4805         number headers header)
4806     (save-excursion
4807       (set-buffer nntp-server-buffer)
4808       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4809       ;; Allow the user to mangle the headers before parsing them.
4810       (gnus-run-hooks 'gnus-parse-headers-hook)
4811       (goto-char (point-min))
4812       (while (not (eobp))
4813         (condition-case ()
4814             (while (and sequence (not (eobp)))
4815               (setq number (read cur))
4816               (while (and sequence
4817                           (< (car sequence) number))
4818                 (setq sequence (cdr sequence)))
4819               (and sequence
4820                    (eq number (car sequence))
4821                    (progn
4822                      (setq sequence (cdr sequence))
4823                      (setq header (inline
4824                                     (gnus-nov-parse-line
4825                                      number dependencies force-new))))
4826                    (push header headers))
4827               (forward-line 1))
4828           (error
4829            (gnus-error 4 "Strange nov line (%d)"
4830                        (count-lines (point-min) (point)))))
4831         (forward-line 1))
4832       ;; A common bug in inn is that if you have posted an article and
4833       ;; then retrieves the active file, it will answer correctly --
4834       ;; the new article is included.  However, a NOV entry for the
4835       ;; article may not have been generated yet, so this may fail.
4836       ;; We work around this problem by retrieving the last few
4837       ;; headers using HEAD.
4838       (if (or (not also-fetch-heads)
4839               (not sequence))
4840           ;; We (probably) got all the headers.
4841           (nreverse headers)
4842         (let ((gnus-nov-is-evil t))
4843           (nconc
4844            (nreverse headers)
4845            (gnus-retrieve-parsed-headers sequence group)
4846            ))))))
4847
4848 (defun gnus-article-get-xrefs ()
4849   "Fill in the Xref value in `gnus-current-headers', if necessary.
4850 This is meant to be called in `gnus-article-internal-prepare-hook'."
4851   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4852                                  gnus-current-headers)))
4853     (or (not gnus-use-cross-reference)
4854         (not headers)
4855         (and (mail-header-xref headers)
4856              (not (string= (mail-header-xref headers) "")))
4857         (let ((case-fold-search t)
4858               xref)
4859           (save-restriction
4860             (nnheader-narrow-to-headers)
4861             (goto-char (point-min))
4862             (when (or (and (not (eobp))
4863                            (eq (downcase (char-after)) ?x)
4864                            (looking-at "Xref:"))
4865                       (search-forward "\nXref:" nil t))
4866               (goto-char (1+ (match-end 0)))
4867               (setq xref (buffer-substring (point)
4868                                            (progn (end-of-line) (point))))
4869               (mail-header-set-xref headers xref)))))))
4870
4871 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4872   "Find article ID and insert the summary line for that article.
4873 OLD-HEADER can either be a header or a line number to insert
4874 the subject line on."
4875   (let* ((line (and (numberp old-header) old-header))
4876          (old-header (and (vectorp old-header) old-header))
4877          (header (cond ((and old-header use-old-header)
4878                         old-header)
4879                        ((and (numberp id)
4880                              (gnus-number-to-header id))
4881                         (gnus-number-to-header id))
4882                        (t
4883                         (gnus-read-header id))))
4884          (number (and (numberp id) id))
4885          d)
4886     (when header
4887       ;; Rebuild the thread that this article is part of and go to the
4888       ;; article we have fetched.
4889       (when (and (not gnus-show-threads)
4890                  old-header)
4891         (when (and number
4892                    (setq d (gnus-data-find (mail-header-number old-header))))
4893           (goto-char (gnus-data-pos d))
4894           (gnus-data-remove
4895            number
4896            (- (gnus-point-at-bol)
4897               (prog1
4898                   (1+ (gnus-point-at-eol))
4899                 (gnus-delete-line))))))
4900       (when old-header
4901         (mail-header-set-number header (mail-header-number old-header)))
4902       (setq gnus-newsgroup-sparse
4903             (delq (setq number (mail-header-number header))
4904                   gnus-newsgroup-sparse))
4905       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4906       (push number gnus-newsgroup-limit)
4907       (gnus-rebuild-thread (mail-header-id header) line)
4908       (gnus-summary-goto-subject number nil t))
4909     (when (and (numberp number)
4910                (> number 0))
4911       ;; We have to update the boundaries even if we can't fetch the
4912       ;; article if ID is a number -- so that the next `P' or `N'
4913       ;; command will fetch the previous (or next) article even
4914       ;; if the one we tried to fetch this time has been canceled.
4915       (when (> number gnus-newsgroup-end)
4916         (setq gnus-newsgroup-end number))
4917       (when (< number gnus-newsgroup-begin)
4918         (setq gnus-newsgroup-begin number))
4919       (setq gnus-newsgroup-unselected
4920             (delq number gnus-newsgroup-unselected)))
4921     ;; Report back a success?
4922     (and header (mail-header-number header))))
4923
4924 ;;; Process/prefix in the summary buffer
4925
4926 (defun gnus-summary-work-articles (n)
4927   "Return a list of articles to be worked upon.
4928 The prefix argument, the list of process marked articles, and the
4929 current article will be taken into consideration."
4930   (save-excursion
4931     (set-buffer gnus-summary-buffer)
4932     (cond
4933      (n
4934       ;; A numerical prefix has been given.
4935       (setq n (prefix-numeric-value n))
4936       (let ((backward (< n 0))
4937             (n (abs (prefix-numeric-value n)))
4938             articles article)
4939         (save-excursion
4940           (while
4941               (and (> n 0)
4942                    (push (setq article (gnus-summary-article-number))
4943                          articles)
4944                    (if backward
4945                        (gnus-summary-find-prev nil article)
4946                      (gnus-summary-find-next nil article)))
4947             (decf n)))
4948         (nreverse articles)))
4949      ((and (gnus-region-active-p) (mark))
4950       (message "region active")
4951       ;; Work on the region between point and mark.
4952       (let ((max (max (point) (mark)))
4953             articles article)
4954         (save-excursion
4955           (goto-char (min (point) (mark)))
4956           (while
4957               (and
4958                (push (setq article (gnus-summary-article-number)) articles)
4959                (gnus-summary-find-next nil article)
4960                (< (point) max)))
4961           (nreverse articles))))
4962      (gnus-newsgroup-processable
4963       ;; There are process-marked articles present.
4964       ;; Save current state.
4965       (gnus-summary-save-process-mark)
4966       ;; Return the list.
4967       (reverse gnus-newsgroup-processable))
4968      (t
4969       ;; Just return the current article.
4970       (list (gnus-summary-article-number))))))
4971
4972 (defmacro gnus-summary-iterate (arg &rest forms)
4973   "Iterate over the process/prefixed articles and do FORMS.
4974 ARG is the interactive prefix given to the command.  FORMS will be
4975 executed with point over the summary line of the articles."
4976   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4977     `(let ((,articles (gnus-summary-work-articles ,arg)))
4978        (while ,articles
4979          (gnus-summary-goto-subject (car ,articles))
4980          ,@forms
4981          (pop ,articles)))))
4982
4983 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4984 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4985
4986 (defun gnus-summary-save-process-mark ()
4987   "Push the current set of process marked articles on the stack."
4988   (interactive)
4989   (push (copy-sequence gnus-newsgroup-processable)
4990         gnus-newsgroup-process-stack))
4991
4992 (defun gnus-summary-kill-process-mark ()
4993   "Push the current set of process marked articles on the stack and unmark."
4994   (interactive)
4995   (gnus-summary-save-process-mark)
4996   (gnus-summary-unmark-all-processable))
4997
4998 (defun gnus-summary-yank-process-mark ()
4999   "Pop the last process mark state off the stack and restore it."
5000   (interactive)
5001   (unless gnus-newsgroup-process-stack
5002     (error "Empty mark stack"))
5003   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5004
5005 (defun gnus-summary-process-mark-set (set)
5006   "Make SET into the current process marked articles."
5007   (gnus-summary-unmark-all-processable)
5008   (while set
5009     (gnus-summary-set-process-mark (pop set))))
5010
5011 ;;; Searching and stuff
5012
5013 (defun gnus-summary-search-group (&optional backward use-level)
5014   "Search for next unread newsgroup.
5015 If optional argument BACKWARD is non-nil, search backward instead."
5016   (save-excursion
5017     (set-buffer gnus-group-buffer)
5018     (when (gnus-group-search-forward
5019            backward nil (if use-level (gnus-group-group-level) nil))
5020       (gnus-group-group-name))))
5021
5022 (defun gnus-summary-best-group (&optional exclude-group)
5023   "Find the name of the best unread group.
5024 If EXCLUDE-GROUP, do not go to this group."
5025   (save-excursion
5026     (set-buffer gnus-group-buffer)
5027     (save-excursion
5028       (gnus-group-best-unread-group exclude-group))))
5029
5030 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5031   (if backward (gnus-summary-find-prev)
5032     (let* ((dummy (gnus-summary-article-intangible-p))
5033            (article (or article (gnus-summary-article-number)))
5034            (arts (gnus-data-find-list article))
5035            result)
5036       (when (and (not dummy)
5037                  (or (not gnus-summary-check-current)
5038                      (not unread)
5039                      (not (gnus-data-unread-p (car arts)))))
5040         (setq arts (cdr arts)))
5041       (when (setq result
5042                   (if unread
5043                       (progn
5044                         (while arts
5045                           (when (or (and undownloaded
5046                                          (eq gnus-undownloaded-mark
5047                                              (gnus-data-mark (car arts))))
5048                                     (gnus-data-unread-p (car arts)))
5049                             (setq result (car arts)
5050                                   arts nil))
5051                           (setq arts (cdr arts)))
5052                         result)
5053                     (car arts)))
5054         (goto-char (gnus-data-pos result))
5055         (gnus-data-number result)))))
5056
5057 (defun gnus-summary-find-prev (&optional unread article)
5058   (let* ((eobp (eobp))
5059          (article (or article (gnus-summary-article-number)))
5060          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5061          result)
5062     (when (and (not eobp)
5063                (or (not gnus-summary-check-current)
5064                    (not unread)
5065                    (not (gnus-data-unread-p (car arts)))))
5066       (setq arts (cdr arts)))
5067     (when (setq result
5068                 (if unread
5069                     (progn
5070                       (while arts
5071                         (when (gnus-data-unread-p (car arts))
5072                           (setq result (car arts)
5073                                 arts nil))
5074                         (setq arts (cdr arts)))
5075                       result)
5076                   (car arts)))
5077       (goto-char (gnus-data-pos result))
5078       (gnus-data-number result))))
5079
5080 (defun gnus-summary-find-subject (subject &optional unread backward article)
5081   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5082          (article (or article (gnus-summary-article-number)))
5083          (articles (gnus-data-list backward))
5084          (arts (gnus-data-find-list article articles))
5085          result)
5086     (when (or (not gnus-summary-check-current)
5087               (not unread)
5088               (not (gnus-data-unread-p (car arts))))
5089       (setq arts (cdr arts)))
5090     (while arts
5091       (and (or (not unread)
5092                (gnus-data-unread-p (car arts)))
5093            (vectorp (gnus-data-header (car arts)))
5094            (gnus-subject-equal
5095             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5096            (setq result (car arts)
5097                  arts nil))
5098       (setq arts (cdr arts)))
5099     (and result
5100          (goto-char (gnus-data-pos result))
5101          (gnus-data-number result))))
5102
5103 (defun gnus-summary-search-forward (&optional unread subject backward)
5104   "Search forward for an article.
5105 If UNREAD, look for unread articles.  If SUBJECT, look for
5106 articles with that subject.  If BACKWARD, search backward instead."
5107   (cond (subject (gnus-summary-find-subject subject unread backward))
5108         (backward (gnus-summary-find-prev unread))
5109         (t (gnus-summary-find-next unread))))
5110
5111 (defun gnus-recenter (&optional n)
5112   "Center point in window and redisplay frame.
5113 Also do horizontal recentering."
5114   (interactive "P")
5115   (when (and gnus-auto-center-summary
5116              (not (eq gnus-auto-center-summary 'vertical)))
5117     (gnus-horizontal-recenter))
5118   (recenter n))
5119
5120 (defun gnus-summary-recenter ()
5121   "Center point in the summary window.
5122 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5123 displayed, no centering will be performed."
5124   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5125   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5126   (let* ((top (cond ((< (window-height) 4) 0)
5127                     ((< (window-height) 7) 1)
5128                     (t (if (numberp gnus-auto-center-summary)
5129                            gnus-auto-center-summary
5130                          2))))
5131          (height (1- (window-height)))
5132          (bottom (save-excursion (goto-char (point-max))
5133                                  (forward-line (- height))
5134                                  (point)))
5135          (window (get-buffer-window (current-buffer))))
5136     ;; The user has to want it.
5137     (when gnus-auto-center-summary
5138       (when (get-buffer-window gnus-article-buffer)
5139         ;; Only do recentering when the article buffer is displayed,
5140         ;; Set the window start to either `bottom', which is the biggest
5141         ;; possible valid number, or the second line from the top,
5142         ;; whichever is the least.
5143         (set-window-start
5144          window (min bottom (save-excursion
5145                               (forward-line (- top)) (point)))
5146          t))
5147       ;; Do horizontal recentering while we're at it.
5148       (when (and (get-buffer-window (current-buffer) t)
5149                  (not (eq gnus-auto-center-summary 'vertical)))
5150         (let ((selected (selected-window)))
5151           (select-window (get-buffer-window (current-buffer) t))
5152           (gnus-summary-position-point)
5153           (gnus-horizontal-recenter)
5154           (select-window selected))))))
5155
5156 (defun gnus-summary-jump-to-group (newsgroup)
5157   "Move point to NEWSGROUP in group mode buffer."
5158   ;; Keep update point of group mode buffer if visible.
5159   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5160       (save-window-excursion
5161         ;; Take care of tree window mode.
5162         (when (get-buffer-window gnus-group-buffer)
5163           (pop-to-buffer gnus-group-buffer))
5164         (gnus-group-jump-to-group newsgroup))
5165     (save-excursion
5166       ;; Take care of tree window mode.
5167       (if (get-buffer-window gnus-group-buffer)
5168           (pop-to-buffer gnus-group-buffer)
5169         (set-buffer gnus-group-buffer))
5170       (gnus-group-jump-to-group newsgroup))))
5171
5172 ;; This function returns a list of article numbers based on the
5173 ;; difference between the ranges of read articles in this group and
5174 ;; the range of active articles.
5175 (defun gnus-list-of-unread-articles (group)
5176   (let* ((read (gnus-info-read (gnus-get-info group)))
5177          (active (or (gnus-active group) (gnus-activate-group group)))
5178          (last (cdr active))
5179          first nlast unread)
5180     ;; If none are read, then all are unread.
5181     (if (not read)
5182         (setq first (car active))
5183       ;; If the range of read articles is a single range, then the
5184       ;; first unread article is the article after the last read
5185       ;; article.  Sounds logical, doesn't it?
5186       (if (not (listp (cdr read)))
5187           (setq first (max (car active) (1+ (cdr read))))
5188         ;; `read' is a list of ranges.
5189         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5190                                   (caar read)))
5191                   1)
5192           (setq first (car active)))
5193         (while read
5194           (when first
5195             (while (< first nlast)
5196               (push first unread)
5197               (setq first (1+ first))))
5198           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5199           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5200           (setq read (cdr read)))))
5201     ;; And add the last unread articles.
5202     (while (<= first last)
5203       (push first unread)
5204       (setq first (1+ first)))
5205     ;; Return the list of unread articles.
5206     (delq 0 (nreverse unread))))
5207
5208 (defun gnus-list-of-read-articles (group)
5209   "Return a list of unread, unticked and non-dormant articles."
5210   (let* ((info (gnus-get-info group))
5211          (marked (gnus-info-marks info))
5212          (active (gnus-active group)))
5213     (and info active
5214          (gnus-set-difference
5215           (gnus-sorted-complement
5216            (gnus-uncompress-range active)
5217            (gnus-list-of-unread-articles group))
5218           (append
5219            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5220            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5221
5222 ;; Various summary commands
5223
5224 (defun gnus-summary-select-article-buffer ()
5225   "Reconfigure windows to show article buffer."
5226   (interactive)
5227   (if (not (gnus-buffer-live-p gnus-article-buffer))
5228       (error "There is no article buffer for this summary buffer")
5229     (gnus-configure-windows 'article)
5230     (select-window (get-buffer-window gnus-article-buffer))))
5231
5232 (defun gnus-summary-universal-argument (arg)
5233   "Perform any operation on all articles that are process/prefixed."
5234   (interactive "P")
5235   (let ((articles (gnus-summary-work-articles arg))
5236         func article)
5237     (if (eq
5238          (setq
5239           func
5240           (key-binding
5241            (read-key-sequence
5242             (substitute-command-keys
5243              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5244              ))))
5245          'undefined)
5246         (gnus-error 1 "Undefined key")
5247       (save-excursion
5248         (while articles
5249           (gnus-summary-goto-subject (setq article (pop articles)))
5250           (let (gnus-newsgroup-processable)
5251             (command-execute func))
5252           (gnus-summary-remove-process-mark article)))))
5253   (gnus-summary-position-point))
5254
5255 (defun gnus-summary-toggle-truncation (&optional arg)
5256   "Toggle truncation of summary lines.
5257 With arg, turn line truncation on iff arg is positive."
5258   (interactive "P")
5259   (setq truncate-lines
5260         (if (null arg) (not truncate-lines)
5261           (> (prefix-numeric-value arg) 0)))
5262   (redraw-display))
5263
5264 (defun gnus-summary-reselect-current-group (&optional all rescan)
5265   "Rescan the current newsgroup, exit and then reselect it.
5266 The prefix argument ALL means to select all articles."
5267   (interactive "P")
5268   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5269     (error "Ephemeral groups can't be reselected"))
5270   (let ((current-subject (gnus-summary-article-number))
5271         (group gnus-newsgroup-name))
5272     (save-excursion
5273       (set-buffer gnus-group-buffer)
5274       ;; We have to adjust the point of group mode buffer because
5275       ;; point was moved to the next unread newsgroup by exiting.
5276       (gnus-summary-jump-to-group group)
5277       (when rescan
5278         (save-excursion
5279           (gnus-group-get-new-news-this-group 1))))
5280     (setq gnus-newsgroup-begin nil)
5281     (gnus-summary-exit)
5282     (gnus-group-read-group all t group)
5283     (gnus-summary-goto-subject current-subject nil t)))
5284
5285 (defun gnus-summary-rescan-group (&optional all)
5286   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5287   (interactive "P")
5288   (gnus-summary-reselect-current-group all t))
5289
5290 (defun gnus-summary-update-info (&optional non-destructive)
5291   (save-excursion
5292     (let ((group gnus-newsgroup-name))
5293       (when group
5294         (when gnus-newsgroup-kill-headers
5295           (setq gnus-newsgroup-killed
5296                 (gnus-compress-sequence
5297                  (nconc
5298                   (gnus-set-sorted-intersection
5299                    (gnus-uncompress-range gnus-newsgroup-killed)
5300                    (setq gnus-newsgroup-unselected
5301                          (sort gnus-newsgroup-unselected '<)))
5302                   (setq gnus-newsgroup-unreads
5303                         (sort gnus-newsgroup-unreads '<)))
5304                  t)))
5305         (unless (listp (cdr gnus-newsgroup-killed))
5306           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5307         (let ((headers gnus-newsgroup-headers))
5308           ;; Set the new ranges of read articles.
5309           (save-excursion
5310             (set-buffer gnus-group-buffer)
5311             (gnus-undo-force-boundary))
5312           (gnus-update-read-articles
5313            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5314           ;; Set the current article marks.
5315           (let ((gnus-newsgroup-scored
5316                  (if (and (not gnus-save-score)
5317                           (not non-destructive))
5318                      nil
5319                    gnus-newsgroup-scored)))
5320             (save-excursion
5321               (gnus-update-marks)))
5322           ;; Do the cross-ref thing.
5323           (when gnus-use-cross-reference
5324             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5325           ;; Do not switch windows but change the buffer to work.
5326           (set-buffer gnus-group-buffer)
5327           (unless (gnus-ephemeral-group-p group)
5328             (gnus-group-update-group group)))))))
5329
5330 (defun gnus-summary-save-newsrc (&optional force)
5331   "Save the current number of read/marked articles in the dribble buffer.
5332 The dribble buffer will then be saved.
5333 If FORCE (the prefix), also save the .newsrc file(s)."
5334   (interactive "P")
5335   (gnus-summary-update-info t)
5336   (if force
5337       (gnus-save-newsrc-file)
5338     (gnus-dribble-save)))
5339
5340 (defun gnus-summary-exit (&optional temporary)
5341   "Exit reading current newsgroup, and then return to group selection mode.
5342 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5343   (interactive)
5344   (gnus-set-global-variables)
5345   (gnus-kill-save-kill-buffer)
5346   (gnus-async-halt-prefetch)
5347   (let* ((group gnus-newsgroup-name)
5348          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5349          (mode major-mode)
5350          (group-point nil)
5351          (buf (current-buffer)))
5352     (unless quit-config
5353       ;; Do adaptive scoring, and possibly save score files.
5354       (when gnus-newsgroup-adaptive
5355         (gnus-score-adaptive))
5356       (when gnus-use-scoring
5357         (gnus-score-save)))
5358     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5359     ;; If we have several article buffers, we kill them at exit.
5360     (unless gnus-single-article-buffer
5361       (gnus-kill-buffer gnus-original-article-buffer)
5362       (setq gnus-article-current nil))
5363     (when gnus-use-cache
5364       (gnus-cache-possibly-remove-articles)
5365       (gnus-cache-save-buffers))
5366     (gnus-async-prefetch-remove-group group)
5367     (when gnus-suppress-duplicates
5368       (gnus-dup-enter-articles))
5369     (when gnus-use-trees
5370       (gnus-tree-close group))
5371     ;; Remove entries for this group.
5372     (nnmail-purge-split-history (gnus-group-real-name group))
5373     ;; Make all changes in this group permanent.
5374     (unless quit-config
5375       (gnus-run-hooks 'gnus-exit-group-hook)
5376       (gnus-summary-update-info))
5377     (gnus-close-group group)
5378     ;; Make sure where we were, and go to next newsgroup.
5379     (set-buffer gnus-group-buffer)
5380     (unless quit-config
5381       (gnus-group-jump-to-group group))
5382     (gnus-run-hooks 'gnus-summary-exit-hook)
5383     (unless (or quit-config
5384                 ;; If this group has disappeared from the summary
5385                 ;; buffer, don't skip forwards.
5386                 (not (string= group (gnus-group-group-name))))
5387       (gnus-group-next-unread-group 1))
5388     (setq group-point (point))
5389     (if temporary
5390         nil                             ;Nothing to do.
5391       ;; If we have several article buffers, we kill them at exit.
5392       (unless gnus-single-article-buffer
5393         (gnus-kill-buffer gnus-article-buffer)
5394         (gnus-kill-buffer gnus-original-article-buffer)
5395         (setq gnus-article-current nil))
5396       (set-buffer buf)
5397       (if (not gnus-kill-summary-on-exit)
5398           (gnus-deaden-summary)
5399         ;; We set all buffer-local variables to nil.  It is unclear why
5400         ;; this is needed, but if we don't, buffer-local variables are
5401         ;; not garbage-collected, it seems.  This would the lead to en
5402         ;; ever-growing Emacs.
5403         (gnus-summary-clear-local-variables)
5404         (when (get-buffer gnus-article-buffer)
5405           (bury-buffer gnus-article-buffer))
5406         ;; We clear the global counterparts of the buffer-local
5407         ;; variables as well, just to be on the safe side.
5408         (set-buffer gnus-group-buffer)
5409         (gnus-summary-clear-local-variables)
5410         ;; Return to group mode buffer.
5411         (when (eq mode 'gnus-summary-mode)
5412           (gnus-kill-buffer buf)))
5413       (setq gnus-current-select-method gnus-select-method)
5414       (pop-to-buffer gnus-group-buffer)
5415       (if (not quit-config)
5416           (progn
5417             (goto-char group-point)
5418             (gnus-configure-windows 'group 'force))
5419         (gnus-handle-ephemeral-exit quit-config))
5420       ;; Clear the current group name.
5421       (unless quit-config
5422         (setq gnus-newsgroup-name nil)))))
5423
5424 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5425 (defun gnus-summary-exit-no-update (&optional no-questions)
5426   "Quit reading current newsgroup without updating read article info."
5427   (interactive)
5428   (let* ((group gnus-newsgroup-name)
5429          (quit-config (gnus-group-quit-config group)))
5430     (when (or no-questions
5431               gnus-expert-user
5432               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5433       (gnus-async-halt-prefetch)
5434       (mapcar 'funcall
5435               (delq 'gnus-summary-expire-articles
5436                     (copy-sequence gnus-summary-prepare-exit-hook)))
5437       ;; If we have several article buffers, we kill them at exit.
5438       (unless gnus-single-article-buffer
5439         (gnus-kill-buffer gnus-article-buffer)
5440         (gnus-kill-buffer gnus-original-article-buffer)
5441         (setq gnus-article-current nil))
5442       (if (not gnus-kill-summary-on-exit)
5443           (gnus-deaden-summary)
5444         (gnus-close-group group)
5445         (gnus-summary-clear-local-variables)
5446         (set-buffer gnus-group-buffer)
5447         (gnus-summary-clear-local-variables)
5448         (when (get-buffer gnus-summary-buffer)
5449           (kill-buffer gnus-summary-buffer)))
5450       (unless gnus-single-article-buffer
5451         (setq gnus-article-current nil))
5452       (when gnus-use-trees
5453         (gnus-tree-close group))
5454       (gnus-async-prefetch-remove-group group)
5455       (when (get-buffer gnus-article-buffer)
5456         (bury-buffer gnus-article-buffer))
5457       ;; Return to the group buffer.
5458       (gnus-configure-windows 'group 'force)
5459       ;; Clear the current group name.
5460       (setq gnus-newsgroup-name nil)
5461       (when (equal (gnus-group-group-name) group)
5462         (gnus-group-next-unread-group 1))
5463       (when quit-config
5464         (gnus-handle-ephemeral-exit quit-config)))))
5465
5466 (defun gnus-handle-ephemeral-exit (quit-config)
5467   "Handle movement when leaving an ephemeral group.
5468 The state which existed when entering the ephemeral is reset."
5469   (if (not (buffer-name (car quit-config)))
5470       (gnus-configure-windows 'group 'force)
5471     (set-buffer (car quit-config))
5472     (cond ((eq major-mode 'gnus-summary-mode)
5473            (gnus-set-global-variables))
5474           ((eq major-mode 'gnus-article-mode)
5475            (save-excursion
5476              ;; The `gnus-summary-buffer' variable may point
5477              ;; to the old summary buffer when using a single
5478              ;; article buffer.
5479              (unless (gnus-buffer-live-p gnus-summary-buffer)
5480                (set-buffer gnus-group-buffer))
5481              (set-buffer gnus-summary-buffer)
5482              (gnus-set-global-variables))))
5483     (if (or (eq (cdr quit-config) 'article)
5484             (eq (cdr quit-config) 'pick))
5485         (progn
5486           ;; The current article may be from the ephemeral group
5487           ;; thus it is best that we reload this article
5488           (gnus-summary-show-article)
5489           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5490               (gnus-configure-windows 'pick 'force)
5491             (gnus-configure-windows (cdr quit-config) 'force)))
5492       (gnus-configure-windows (cdr quit-config) 'force))
5493     (when (eq major-mode 'gnus-summary-mode)
5494       (gnus-summary-next-subject 1 nil t)
5495       (gnus-summary-recenter)
5496       (gnus-summary-position-point))))
5497
5498 (defun gnus-summary-preview-mime-message ()
5499   "MIME decode and play this message."
5500   (interactive)
5501   (let ((gnus-break-pages nil)
5502         (gnus-show-mime t))
5503     (gnus-summary-select-article gnus-show-all-headers t))
5504   (select-window (get-buffer-window gnus-article-buffer)))
5505
5506 ;;; Dead summaries.
5507
5508 (defvar gnus-dead-summary-mode-map nil)
5509
5510 (unless gnus-dead-summary-mode-map
5511   (setq gnus-dead-summary-mode-map (make-keymap))
5512   (suppress-keymap gnus-dead-summary-mode-map)
5513   (substitute-key-definition
5514    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5515   (let ((keys '("\C-d" "\r" "\177" [delete])))
5516     (while keys
5517       (define-key gnus-dead-summary-mode-map
5518         (pop keys) 'gnus-summary-wake-up-the-dead))))
5519
5520 (defvar gnus-dead-summary-mode nil
5521   "Minor mode for Gnus summary buffers.")
5522
5523 (defun gnus-dead-summary-mode (&optional arg)
5524   "Minor mode for Gnus summary buffers."
5525   (interactive "P")
5526   (when (eq major-mode 'gnus-summary-mode)
5527     (make-local-variable 'gnus-dead-summary-mode)
5528     (setq gnus-dead-summary-mode
5529           (if (null arg) (not gnus-dead-summary-mode)
5530             (> (prefix-numeric-value arg) 0)))
5531     (when gnus-dead-summary-mode
5532       (gnus-add-minor-mode
5533        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5534
5535 (defun gnus-deaden-summary ()
5536   "Make the current summary buffer into a dead summary buffer."
5537   ;; Kill any previous dead summary buffer.
5538   (when (and gnus-dead-summary
5539              (buffer-name gnus-dead-summary))
5540     (save-excursion
5541       (set-buffer gnus-dead-summary)
5542       (when gnus-dead-summary-mode
5543         (kill-buffer (current-buffer)))))
5544   ;; Make this the current dead summary.
5545   (setq gnus-dead-summary (current-buffer))
5546   (gnus-dead-summary-mode 1)
5547   (let ((name (buffer-name)))
5548     (when (string-match "Summary" name)
5549       (rename-buffer
5550        (concat (substring name 0 (match-beginning 0)) "Dead "
5551                (substring name (match-beginning 0)))
5552        t))))
5553
5554 (defun gnus-kill-or-deaden-summary (buffer)
5555   "Kill or deaden the summary BUFFER."
5556   (save-excursion
5557     (when (and (buffer-name buffer)
5558                (not gnus-single-article-buffer))
5559       (save-excursion
5560         (set-buffer buffer)
5561         (gnus-kill-buffer gnus-article-buffer)
5562         (gnus-kill-buffer gnus-original-article-buffer)))
5563     (cond (gnus-kill-summary-on-exit
5564            (when (and gnus-use-trees
5565                       (gnus-buffer-exists-p buffer))
5566              (save-excursion
5567                (set-buffer buffer)
5568                (gnus-tree-close gnus-newsgroup-name)))
5569            (gnus-kill-buffer buffer))
5570           ((gnus-buffer-exists-p buffer)
5571            (save-excursion
5572              (set-buffer buffer)
5573              (gnus-deaden-summary))))))
5574
5575 (defun gnus-summary-wake-up-the-dead (&rest args)
5576   "Wake up the dead summary buffer."
5577   (interactive)
5578   (gnus-dead-summary-mode -1)
5579   (let ((name (buffer-name)))
5580     (when (string-match "Dead " name)
5581       (rename-buffer
5582        (concat (substring name 0 (match-beginning 0))
5583                (substring name (match-end 0)))
5584        t)))
5585   (gnus-message 3 "This dead summary is now alive again"))
5586
5587 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5588 (defun gnus-summary-fetch-faq (&optional faq-dir)
5589   "Fetch the FAQ for the current group.
5590 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5591 in."
5592   (interactive
5593    (list
5594     (when current-prefix-arg
5595       (completing-read
5596        "Faq dir: " (and (listp gnus-group-faq-directory)
5597                         (mapcar (lambda (file) (list file))
5598                                 gnus-group-faq-directory))))))
5599   (let (gnus-faq-buffer)
5600     (when (setq gnus-faq-buffer
5601                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5602       (gnus-configure-windows 'summary-faq))))
5603
5604 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5605 (defun gnus-summary-describe-group (&optional force)
5606   "Describe the current newsgroup."
5607   (interactive "P")
5608   (gnus-group-describe-group force gnus-newsgroup-name))
5609
5610 (defun gnus-summary-describe-briefly ()
5611   "Describe summary mode commands briefly."
5612   (interactive)
5613   (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")))
5614
5615 ;; Walking around group mode buffer from summary mode.
5616
5617 (defun gnus-summary-next-group (&optional no-article target-group backward)
5618   "Exit current newsgroup and then select next unread newsgroup.
5619 If prefix argument NO-ARTICLE is non-nil, no article is selected
5620 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5621 previous group instead."
5622   (interactive "P")
5623   ;; Stop pre-fetching.
5624   (gnus-async-halt-prefetch)
5625   (let ((current-group gnus-newsgroup-name)
5626         (current-buffer (current-buffer))
5627         entered)
5628     ;; First we semi-exit this group to update Xrefs and all variables.
5629     ;; We can't do a real exit, because the window conf must remain
5630     ;; the same in case the user is prompted for info, and we don't
5631     ;; want the window conf to change before that...
5632     (gnus-summary-exit t)
5633     (while (not entered)
5634       ;; Then we find what group we are supposed to enter.
5635       (set-buffer gnus-group-buffer)
5636       (gnus-group-jump-to-group current-group)
5637       (setq target-group
5638             (or target-group
5639                 (if (eq gnus-keep-same-level 'best)
5640                     (gnus-summary-best-group gnus-newsgroup-name)
5641                   (gnus-summary-search-group backward gnus-keep-same-level))))
5642       (if (not target-group)
5643           ;; There are no further groups, so we return to the group
5644           ;; buffer.
5645           (progn
5646             (gnus-message 5 "Returning to the group buffer")
5647             (setq entered t)
5648             (when (gnus-buffer-live-p current-buffer)
5649               (set-buffer current-buffer)
5650               (gnus-summary-exit))
5651             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5652         ;; We try to enter the target group.
5653         (gnus-group-jump-to-group target-group)
5654         (let ((unreads (gnus-group-group-unread)))
5655           (if (and (or (eq t unreads)
5656                        (and unreads (not (zerop unreads))))
5657                    (gnus-summary-read-group
5658                     target-group nil no-article
5659                     (and (buffer-name current-buffer) current-buffer)
5660                     nil backward))
5661               (setq entered t)
5662             (setq current-group target-group
5663                   target-group nil)))))))
5664
5665 (defun gnus-summary-prev-group (&optional no-article)
5666   "Exit current newsgroup and then select previous unread newsgroup.
5667 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5668   (interactive "P")
5669   (gnus-summary-next-group no-article nil t))
5670
5671 ;; Walking around summary lines.
5672
5673 (defun gnus-summary-first-subject (&optional unread undownloaded)
5674   "Go to the first unread subject.
5675 If UNREAD is non-nil, go to the first unread article.
5676 Returns the article selected or nil if there are no unread articles."
5677   (interactive "P")
5678   (prog1
5679       (cond
5680        ;; Empty summary.
5681        ((null gnus-newsgroup-data)
5682         (gnus-message 3 "No articles in the group")
5683         nil)
5684        ;; Pick the first article.
5685        ((not unread)
5686         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5687         (gnus-data-number (car gnus-newsgroup-data)))
5688        ;; No unread articles.
5689        ((null gnus-newsgroup-unreads)
5690         (gnus-message 3 "No more unread articles")
5691         nil)
5692        ;; Find the first unread article.
5693        (t
5694         (let ((data gnus-newsgroup-data))
5695           (while (and data
5696                       (and (not (and undownloaded
5697                                      (eq gnus-undownloaded-mark
5698                                          (gnus-data-mark (car data)))))
5699                            (not (gnus-data-unread-p (car data)))))
5700             (setq data (cdr data)))
5701           (when data
5702             (goto-char (gnus-data-pos (car data)))
5703             (gnus-data-number (car data))))))
5704     (gnus-summary-position-point)))
5705
5706 (defun gnus-summary-next-subject (n &optional unread dont-display)
5707   "Go to next N'th summary line.
5708 If N is negative, go to the previous N'th subject line.
5709 If UNREAD is non-nil, only unread articles are selected.
5710 The difference between N and the actual number of steps taken is
5711 returned."
5712   (interactive "p")
5713   (let ((backward (< n 0))
5714         (n (abs n)))
5715     (while (and (> n 0)
5716                 (if backward
5717                     (gnus-summary-find-prev unread)
5718                   (gnus-summary-find-next unread)))
5719       (gnus-summary-show-thread)
5720       (setq n (1- n)))
5721     (when (/= 0 n)
5722       (gnus-message 7 "No more%s articles"
5723                     (if unread " unread" "")))
5724     (unless dont-display
5725       (gnus-summary-recenter)
5726       (gnus-summary-position-point))
5727     n))
5728
5729 (defun gnus-summary-next-unread-subject (n)
5730   "Go to next N'th unread summary line."
5731   (interactive "p")
5732   (gnus-summary-next-subject n t))
5733
5734 (defun gnus-summary-prev-subject (n &optional unread)
5735   "Go to previous N'th summary line.
5736 If optional argument UNREAD is non-nil, only unread article is selected."
5737   (interactive "p")
5738   (gnus-summary-next-subject (- n) unread))
5739
5740 (defun gnus-summary-prev-unread-subject (n)
5741   "Go to previous N'th unread summary line."
5742   (interactive "p")
5743   (gnus-summary-next-subject (- n) t))
5744
5745 (defun gnus-summary-goto-subject (article &optional force silent)
5746   "Go the subject line of ARTICLE.
5747 If FORCE, also allow jumping to articles not currently shown."
5748   (interactive "nArticle number: ")
5749   (let ((b (point))
5750         (data (gnus-data-find article)))
5751     ;; We read in the article if we have to.
5752     (and (not data)
5753          force
5754          (gnus-summary-insert-subject
5755           article
5756           (if (or (numberp force) (vectorp force)) force)
5757           t)
5758          (setq data (gnus-data-find article)))
5759     (goto-char b)
5760     (if (not data)
5761         (progn
5762           (unless silent
5763             (gnus-message 3 "Can't find article %d" article))
5764           nil)
5765       (goto-char (gnus-data-pos data))
5766       (gnus-summary-position-point)
5767       article)))
5768
5769 ;; Walking around summary lines with displaying articles.
5770
5771 (defun gnus-summary-expand-window (&optional arg)
5772   "Make the summary buffer take up the entire Emacs frame.
5773 Given a prefix, will force an `article' buffer configuration."
5774   (interactive "P")
5775   (if arg
5776       (gnus-configure-windows 'article 'force)
5777     (gnus-configure-windows 'summary 'force)))
5778
5779 (defun gnus-summary-display-article (article &optional all-header)
5780   "Display ARTICLE in article buffer."
5781   (gnus-set-global-variables)
5782   (if (null article)
5783       nil
5784     (prog1
5785         (if gnus-summary-display-article-function
5786             (funcall gnus-summary-display-article-function article all-header)
5787           (gnus-article-prepare article all-header))
5788       (with-current-buffer gnus-article-buffer
5789         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5790              nil))
5791       (gnus-run-hooks 'gnus-select-article-hook)
5792       (when (and gnus-current-article
5793                  (not (zerop gnus-current-article)))
5794         (gnus-summary-goto-subject gnus-current-article))
5795       (gnus-summary-recenter)
5796       (when (and gnus-use-trees gnus-show-threads)
5797         (gnus-possibly-generate-tree article)
5798         (gnus-highlight-selected-tree article))
5799       ;; Successfully display article.
5800       (gnus-article-set-window-start
5801        (cdr (assq article gnus-newsgroup-bookmarks))))))
5802
5803 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5804   "Select the current article.
5805 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5806 non-nil, the article will be re-fetched even if it already present in
5807 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5808 be displayed."
5809   ;; Make sure we are in the summary buffer to work around bbdb bug.
5810   (unless (eq major-mode 'gnus-summary-mode)
5811     (set-buffer gnus-summary-buffer))
5812   (let ((article (or article (gnus-summary-article-number)))
5813         (all-headers (not (not all-headers))) ;Must be T or NIL.
5814         gnus-summary-display-article-function
5815         did)
5816     (and (not pseudo)
5817          (gnus-summary-article-pseudo-p article)
5818          (error "This is a pseudo-article"))
5819     (prog1
5820         (save-excursion
5821           (set-buffer gnus-summary-buffer)
5822           (if (or (and gnus-single-article-buffer
5823                        (or (null gnus-current-article)
5824                            (null gnus-article-current)
5825                            (null (get-buffer gnus-article-buffer))
5826                            (not (eq article (cdr gnus-article-current)))
5827                            (not (equal (car gnus-article-current)
5828                                        gnus-newsgroup-name))))
5829                   (and (not gnus-single-article-buffer)
5830                        (or (null gnus-current-article)
5831                            (not (eq gnus-current-article article))))
5832                   force)
5833               ;; The requested article is different from the current article.
5834               (prog1
5835                   (gnus-summary-display-article article all-headers)
5836                 (setq did article)
5837                 (when (or all-headers gnus-show-all-headers)
5838                   (if (eq 'gnus-summary-toggle-mime this-command)
5839                       (gnus-article-show-all)
5840                     (gnus-article-show-all-headers))))
5841             (when (or all-headers gnus-show-all-headers)
5842               (gnus-article-show-all-headers))
5843             'old))
5844       (when did
5845         (gnus-article-set-window-start
5846          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5847
5848 (defun gnus-summary-set-current-mark (&optional current-mark)
5849   "Obsolete function."
5850   nil)
5851
5852 (defun gnus-summary-next-article (&optional unread subject backward push)
5853   "Select the next article.
5854 If UNREAD, only unread articles are selected.
5855 If SUBJECT, only articles with SUBJECT are selected.
5856 If BACKWARD, the previous article is selected instead of the next."
5857   (interactive "P")
5858   (cond
5859    ;; Is there such an article?
5860    ((and (gnus-summary-search-forward unread subject backward)
5861          (or (gnus-summary-display-article (gnus-summary-article-number))
5862              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5863     (gnus-summary-position-point))
5864    ;; If not, we try the first unread, if that is wanted.
5865    ((and subject
5866          gnus-auto-select-same
5867          (gnus-summary-first-unread-article))
5868     (gnus-summary-position-point)
5869     (gnus-message 6 "Wrapped"))
5870    ;; Try to get next/previous article not displayed in this group.
5871    ((and gnus-auto-extend-newsgroup
5872          (not unread) (not subject))
5873     (gnus-summary-goto-article
5874      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5875      nil (count-lines (point-min) (point))))
5876    ;; Go to next/previous group.
5877    (t
5878     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5879       (gnus-summary-jump-to-group gnus-newsgroup-name))
5880     (let ((cmd last-command-char)
5881           (point
5882            (save-excursion
5883              (set-buffer gnus-group-buffer)
5884              (point)))
5885           (group
5886            (if (eq gnus-keep-same-level 'best)
5887                (gnus-summary-best-group gnus-newsgroup-name)
5888              (gnus-summary-search-group backward gnus-keep-same-level))))
5889       ;; For some reason, the group window gets selected.  We change
5890       ;; it back.
5891       (select-window (get-buffer-window (current-buffer)))
5892       ;; Select next unread newsgroup automagically.
5893       (cond
5894        ((or (not gnus-auto-select-next)
5895             (not cmd))
5896         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5897        ((or (eq gnus-auto-select-next 'quietly)
5898             (and (eq gnus-auto-select-next 'slightly-quietly)
5899                  push)
5900             (and (eq gnus-auto-select-next 'almost-quietly)
5901                  (gnus-summary-last-article-p)))
5902         ;; Select quietly.
5903         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5904             (gnus-summary-exit)
5905           (gnus-message 7 "No more%s articles (%s)..."
5906                         (if unread " unread" "")
5907                         (if group (concat "selecting " group)
5908                           "exiting"))
5909           (gnus-summary-next-group nil group backward)))
5910        (t
5911         (when (gnus-key-press-event-p last-input-event)
5912           (gnus-summary-walk-group-buffer
5913            gnus-newsgroup-name cmd unread backward point))))))))
5914
5915 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5916   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5917                       (?\C-p (gnus-group-prev-unread-group 1))))
5918         (cursor-in-echo-area t)
5919         keve key group ended)
5920     (save-excursion
5921       (set-buffer gnus-group-buffer)
5922       (goto-char start)
5923       (setq group
5924             (if (eq gnus-keep-same-level 'best)
5925                 (gnus-summary-best-group gnus-newsgroup-name)
5926               (gnus-summary-search-group backward gnus-keep-same-level))))
5927     (while (not ended)
5928       (gnus-message
5929        5 "No more%s articles%s" (if unread " unread" "")
5930        (if (and group
5931                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5932            (format " (Type %s for %s [%s])"
5933                    (single-key-description cmd) group
5934                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5935          (format " (Type %s to exit %s)"
5936                  (single-key-description cmd)
5937                  gnus-newsgroup-name)))
5938       ;; Confirm auto selection.
5939       (setq key (car (setq keve (gnus-read-event-char))))
5940       (setq ended t)
5941       (cond
5942        ((assq key keystrokes)
5943         (let ((obuf (current-buffer)))
5944           (switch-to-buffer gnus-group-buffer)
5945           (when group
5946             (gnus-group-jump-to-group group))
5947           (eval (cadr (assq key keystrokes)))
5948           (setq group (gnus-group-group-name))
5949           (switch-to-buffer obuf))
5950         (setq ended nil))
5951        ((equal key cmd)
5952         (if (or (not group)
5953                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5954             (gnus-summary-exit)
5955           (gnus-summary-next-group nil group backward)))
5956        (t
5957         (push (cdr keve) unread-command-events))))))
5958
5959 (defun gnus-summary-next-unread-article ()
5960   "Select unread article after current one."
5961   (interactive)
5962   (gnus-summary-next-article
5963    (or (not (eq gnus-summary-goto-unread 'never))
5964        (gnus-summary-last-article-p (gnus-summary-article-number)))
5965    (and gnus-auto-select-same
5966         (gnus-summary-article-subject))))
5967
5968 (defun gnus-summary-prev-article (&optional unread subject)
5969   "Select the article after the current one.
5970 If UNREAD is non-nil, only unread articles are selected."
5971   (interactive "P")
5972   (gnus-summary-next-article unread subject t))
5973
5974 (defun gnus-summary-prev-unread-article ()
5975   "Select unread article before current one."
5976   (interactive)
5977   (gnus-summary-prev-article
5978    (or (not (eq gnus-summary-goto-unread 'never))
5979        (gnus-summary-first-article-p (gnus-summary-article-number)))
5980    (and gnus-auto-select-same
5981         (gnus-summary-article-subject))))
5982
5983 (defun gnus-summary-next-page (&optional lines circular)
5984   "Show next page of the selected article.
5985 If at the end of the current article, select the next article.
5986 LINES says how many lines should be scrolled up.
5987
5988 If CIRCULAR is non-nil, go to the start of the article instead of
5989 selecting the next article when reaching the end of the current
5990 article."
5991   (interactive "P")
5992   (setq gnus-summary-buffer (current-buffer))
5993   (gnus-set-global-variables)
5994   (let ((article (gnus-summary-article-number))
5995         (article-window (get-buffer-window gnus-article-buffer t))
5996         endp)
5997     ;; If the buffer is empty, we have no article.
5998     (unless article
5999       (error "No article to select"))
6000     (gnus-configure-windows 'article)
6001     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6002         (if (and (eq gnus-summary-goto-unread 'never)
6003                  (not (gnus-summary-last-article-p article)))
6004             (gnus-summary-next-article)
6005           (gnus-summary-next-unread-article))
6006       (if (or (null gnus-current-article)
6007               (null gnus-article-current)
6008               (/= article (cdr gnus-article-current))
6009               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6010           ;; Selected subject is different from current article's.
6011           (gnus-summary-display-article article)
6012         (when article-window
6013           (gnus-eval-in-buffer-window gnus-article-buffer
6014             (setq endp (gnus-article-next-page lines)))
6015           (when endp
6016             (cond (circular
6017                    (gnus-summary-beginning-of-article))
6018                   (lines
6019                    (gnus-message 3 "End of message"))
6020                   ((null lines)
6021                    (if (and (eq gnus-summary-goto-unread 'never)
6022                             (not (gnus-summary-last-article-p article)))
6023                        (gnus-summary-next-article)
6024                      (gnus-summary-next-unread-article))))))))
6025     (gnus-summary-recenter)
6026     (gnus-summary-position-point)))
6027
6028 (defun gnus-summary-prev-page (&optional lines move)
6029   "Show previous page of selected article.
6030 Argument LINES specifies lines to be scrolled down.
6031 If MOVE, move to the previous unread article if point is at
6032 the beginning of the buffer."
6033   (interactive "P")
6034   (let ((article (gnus-summary-article-number))
6035         (article-window (get-buffer-window gnus-article-buffer t))
6036         endp)
6037     (gnus-configure-windows 'article)
6038     (if (or (null gnus-current-article)
6039             (null gnus-article-current)
6040             (/= article (cdr gnus-article-current))
6041             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6042         ;; Selected subject is different from current article's.
6043         (gnus-summary-display-article article)
6044       (gnus-summary-recenter)
6045       (when article-window
6046         (gnus-eval-in-buffer-window gnus-article-buffer
6047           (setq endp (gnus-article-prev-page lines)))
6048         (when (and move endp)
6049           (cond (lines
6050                  (gnus-message 3 "Beginning of message"))
6051                 ((null lines)
6052                  (if (and (eq gnus-summary-goto-unread 'never)
6053                           (not (gnus-summary-first-article-p article)))
6054                      (gnus-summary-prev-article)
6055                    (gnus-summary-prev-unread-article))))))))
6056   (gnus-summary-position-point))
6057
6058 (defun gnus-summary-prev-page-or-article (&optional lines)
6059   "Show previous page of selected article.
6060 Argument LINES specifies lines to be scrolled down.
6061 If at the beginning of the article, go to the next article."
6062   (interactive "P")
6063   (gnus-summary-prev-page lines t))
6064
6065 (defun gnus-summary-scroll-up (lines)
6066   "Scroll up (or down) one line current article.
6067 Argument LINES specifies lines to be scrolled up (or down if negative)."
6068   (interactive "p")
6069   (gnus-configure-windows 'article)
6070   (gnus-summary-show-thread)
6071   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6072     (gnus-eval-in-buffer-window gnus-article-buffer
6073       (cond ((> lines 0)
6074              (when (gnus-article-next-page lines)
6075                (gnus-message 3 "End of message")))
6076             ((< lines 0)
6077              (gnus-article-prev-page (- lines))))))
6078   (gnus-summary-recenter)
6079   (gnus-summary-position-point))
6080
6081 (defun gnus-summary-scroll-down (lines)
6082   "Scroll down (or up) one line current article.
6083 Argument LINES specifies lines to be scrolled down (or up if negative)."
6084   (interactive "p")
6085   (gnus-summary-scroll-up (- lines)))
6086
6087 (defun gnus-summary-next-same-subject ()
6088   "Select next article which has the same subject as current one."
6089   (interactive)
6090   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6091
6092 (defun gnus-summary-prev-same-subject ()
6093   "Select previous article which has the same subject as current one."
6094   (interactive)
6095   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6096
6097 (defun gnus-summary-next-unread-same-subject ()
6098   "Select next unread article which has the same subject as current one."
6099   (interactive)
6100   (gnus-summary-next-article t (gnus-summary-article-subject)))
6101
6102 (defun gnus-summary-prev-unread-same-subject ()
6103   "Select previous unread article which has the same subject as current one."
6104   (interactive)
6105   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6106
6107 (defun gnus-summary-first-unread-article ()
6108   "Select the first unread article.
6109 Return nil if there are no unread articles."
6110   (interactive)
6111   (prog1
6112       (when (gnus-summary-first-subject t)
6113         (gnus-summary-show-thread)
6114         (gnus-summary-first-subject t)
6115         (gnus-summary-display-article (gnus-summary-article-number)))
6116     (gnus-summary-position-point)))
6117
6118 (defun gnus-summary-first-unread-subject ()
6119   "Place the point on the subject line of the first unread article.
6120 Return nil if there are no unread articles."
6121   (interactive)
6122   (prog1
6123       (when (gnus-summary-first-subject t)
6124         (gnus-summary-show-thread)
6125         (gnus-summary-first-subject t))
6126     (gnus-summary-position-point)))
6127
6128 (defun gnus-summary-first-article ()
6129   "Select the first article.
6130 Return nil if there are no articles."
6131   (interactive)
6132   (prog1
6133       (when (gnus-summary-first-subject)
6134         (gnus-summary-show-thread)
6135         (gnus-summary-first-subject)
6136         (gnus-summary-display-article (gnus-summary-article-number)))
6137     (gnus-summary-position-point)))
6138
6139 (defun gnus-summary-best-unread-article ()
6140   "Select the unread article with the highest score."
6141   (interactive)
6142   (let ((best -1000000)
6143         (data gnus-newsgroup-data)
6144         article score)
6145     (while data
6146       (and (gnus-data-unread-p (car data))
6147            (> (setq score
6148                     (gnus-summary-article-score (gnus-data-number (car data))))
6149               best)
6150            (setq best score
6151                  article (gnus-data-number (car data))))
6152       (setq data (cdr data)))
6153     (prog1
6154         (if article
6155             (gnus-summary-goto-article article)
6156           (error "No unread articles"))
6157       (gnus-summary-position-point))))
6158
6159 (defun gnus-summary-last-subject ()
6160   "Go to the last displayed subject line in the group."
6161   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6162     (when article
6163       (gnus-summary-goto-subject article))))
6164
6165 (defun gnus-summary-goto-article (article &optional all-headers force)
6166   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6167 If ALL-HEADERS is non-nil, no header lines are hidden.
6168 If FORCE, go to the article even if it isn't displayed.  If FORCE
6169 is a number, it is the line the article is to be displayed on."
6170   (interactive
6171    (list
6172     (completing-read
6173      "Article number or Message-ID: "
6174      (mapcar (lambda (number) (list (int-to-string number)))
6175              gnus-newsgroup-limit))
6176     current-prefix-arg
6177     t))
6178   (prog1
6179       (if (and (stringp article)
6180                (string-match "@" article))
6181           (gnus-summary-refer-article article)
6182         (when (stringp article)
6183           (setq article (string-to-number article)))
6184         (if (gnus-summary-goto-subject article force)
6185             (gnus-summary-display-article article all-headers)
6186           (gnus-message 4 "Couldn't go to article %s" article) nil))
6187     (gnus-summary-position-point)))
6188
6189 (defun gnus-summary-goto-last-article ()
6190   "Go to the previously read article."
6191   (interactive)
6192   (prog1
6193       (when gnus-last-article
6194         (gnus-summary-goto-article gnus-last-article nil t))
6195     (gnus-summary-position-point)))
6196
6197 (defun gnus-summary-pop-article (number)
6198   "Pop one article off the history and go to the previous.
6199 NUMBER articles will be popped off."
6200   (interactive "p")
6201   (let (to)
6202     (setq gnus-newsgroup-history
6203           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6204     (if to
6205         (gnus-summary-goto-article (car to) nil t)
6206       (error "Article history empty")))
6207   (gnus-summary-position-point))
6208
6209 ;; Summary commands and functions for limiting the summary buffer.
6210
6211 (defun gnus-summary-limit-to-articles (n)
6212   "Limit the summary buffer to the next N articles.
6213 If not given a prefix, use the process marked articles instead."
6214   (interactive "P")
6215   (prog1
6216       (let ((articles (gnus-summary-work-articles n)))
6217         (setq gnus-newsgroup-processable nil)
6218         (gnus-summary-limit articles))
6219     (gnus-summary-position-point)))
6220
6221 (defun gnus-summary-pop-limit (&optional total)
6222   "Restore the previous limit.
6223 If given a prefix, remove all limits."
6224   (interactive "P")
6225   (when total
6226     (setq gnus-newsgroup-limits
6227           (list (mapcar (lambda (h) (mail-header-number h))
6228                         gnus-newsgroup-headers))))
6229   (unless gnus-newsgroup-limits
6230     (error "No limit to pop"))
6231   (prog1
6232       (gnus-summary-limit nil 'pop)
6233     (gnus-summary-position-point)))
6234
6235 (defun gnus-summary-limit-to-subject (subject &optional header)
6236   "Limit the summary buffer to articles that have subjects that match a regexp."
6237   (interactive "sLimit to subject (regexp): ")
6238   (unless header
6239     (setq header "subject"))
6240   (when (not (equal "" subject))
6241     (prog1
6242         (let ((articles (gnus-summary-find-matching
6243                          (or header "subject") subject 'all)))
6244           (unless articles
6245             (error "Found no matches for \"%s\"" subject))
6246           (gnus-summary-limit articles))
6247       (gnus-summary-position-point))))
6248
6249 (defun gnus-summary-limit-to-author (from)
6250   "Limit the summary buffer to articles that have authors that match a regexp."
6251   (interactive "sLimit to author (regexp): ")
6252   (gnus-summary-limit-to-subject from "from"))
6253
6254 (defun gnus-summary-limit-to-age (age &optional younger-p)
6255   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6256 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6257 articles that are younger than AGE days."
6258   (interactive
6259    (let ((younger current-prefix-arg)
6260          (days-got nil)
6261          days)
6262      (while (not days-got)
6263        (setq days (if younger
6264                       (read-string "Limit to articles within (in days): ")
6265                     (read-string "Limit to articles old than (in days): ")))
6266        (when (> (length days) 0)
6267          (setq days (read days)))
6268        (if (numberp days)
6269            (setq days-got t)
6270          (message "Please enter a number.")
6271          (sleep-for 1)))
6272      (list days younger)))
6273   (prog1
6274       (let ((data gnus-newsgroup-data)
6275             (cutoff (days-to-time age))
6276             articles d date is-younger)
6277         (while (setq d (pop data))
6278           (when (and (vectorp (gnus-data-header d))
6279                      (setq date (mail-header-date (gnus-data-header d))))
6280             (setq is-younger (time-less-p
6281                               (time-since (condition-case ()
6282                                               (date-to-time date)
6283                                             (error '(0 0))))
6284                               cutoff))
6285             (when (if younger-p
6286                       is-younger
6287                     (not is-younger))
6288               (push (gnus-data-number d) articles))))
6289         (gnus-summary-limit (nreverse articles)))
6290     (gnus-summary-position-point)))
6291
6292 (defun gnus-summary-limit-to-extra (header regexp)
6293   "Limit the summary buffer to articles that match an 'extra' header."
6294   (interactive
6295    (let ((header
6296           (intern
6297            (gnus-completing-read
6298             (symbol-name (car gnus-extra-headers))      
6299             "Limit extra header:"       
6300             (mapcar (lambda (x) 
6301                       (cons (symbol-name x) x))
6302                     gnus-extra-headers)
6303             nil                 
6304             t))))
6305      (list header
6306            (read-string (format "Limit to header %s (regexp): " header)))))
6307   (when (not (equal "" regexp))
6308     (prog1
6309         (let ((articles (gnus-summary-find-matching
6310                          (cons 'extra header) regexp 'all)))
6311           (unless articles
6312             (error "Found no matches for \"%s\"" regexp))
6313           (gnus-summary-limit articles))
6314       (gnus-summary-position-point))))
6315
6316 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6317 (make-obsolete
6318  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6319
6320 (defun gnus-summary-limit-to-unread (&optional all)
6321   "Limit the summary buffer to articles that are not marked as read.
6322 If ALL is non-nil, limit strictly to unread articles."
6323   (interactive "P")
6324   (if all
6325       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6326     (gnus-summary-limit-to-marks
6327      ;; Concat all the marks that say that an article is read and have
6328      ;; those removed.
6329      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6330            gnus-killed-mark gnus-kill-file-mark
6331            gnus-low-score-mark gnus-expirable-mark
6332            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6333            gnus-duplicate-mark gnus-souped-mark)
6334      'reverse)))
6335
6336 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6337 (make-obsolete 'gnus-summary-delete-marked-with
6338                'gnus-summary-limit-exlude-marks)
6339
6340 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6341   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6342 If REVERSE, limit the summary buffer to articles that are marked
6343 with MARKS.  MARKS can either be a string of marks or a list of marks.
6344 Returns how many articles were removed."
6345   (interactive "sMarks: ")
6346   (gnus-summary-limit-to-marks marks t))
6347
6348 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6349   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6350 If REVERSE (the prefix), limit the summary buffer to articles that are
6351 not marked with MARKS.  MARKS can either be a string of marks or a
6352 list of marks.
6353 Returns how many articles were removed."
6354   (interactive "sMarks: \nP")
6355   (prog1
6356       (let ((data gnus-newsgroup-data)
6357             (marks (if (listp marks) marks
6358                      (append marks nil))) ; Transform to list.
6359             articles)
6360         (while data
6361           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6362                   (memq (gnus-data-mark (car data)) marks))
6363             (push (gnus-data-number (car data)) articles))
6364           (setq data (cdr data)))
6365         (gnus-summary-limit articles))
6366     (gnus-summary-position-point)))
6367
6368 (defun gnus-summary-limit-to-score (&optional score)
6369   "Limit to articles with score at or above SCORE."
6370   (interactive "P")
6371   (setq score (if score
6372                   (prefix-numeric-value score)
6373                 (or gnus-summary-default-score 0)))
6374   (let ((data gnus-newsgroup-data)
6375         articles)
6376     (while data
6377       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6378                 score)
6379         (push (gnus-data-number (car data)) articles))
6380       (setq data (cdr data)))
6381     (prog1
6382         (gnus-summary-limit articles)
6383       (gnus-summary-position-point))))
6384
6385 (defun gnus-summary-limit-include-thread (id)
6386   "Display all the hidden articles that in the current thread."
6387   (interactive (list (mail-header-id (gnus-summary-article-header))))
6388   (let ((articles (gnus-articles-in-thread
6389                    (gnus-id-to-thread (gnus-root-id id)))))
6390     (prog1
6391         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6392       (gnus-summary-position-point))))
6393
6394 (defun gnus-summary-limit-include-dormant ()
6395   "Display all the hidden articles that are marked as dormant.
6396 Note that this command only works on a subset of the articles currently
6397 fetched for this group."
6398   (interactive)
6399   (unless gnus-newsgroup-dormant
6400     (error "There are no dormant articles in this group"))
6401   (prog1
6402       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6403     (gnus-summary-position-point)))
6404
6405 (defun gnus-summary-limit-exclude-dormant ()
6406   "Hide all dormant articles."
6407   (interactive)
6408   (prog1
6409       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6410     (gnus-summary-position-point)))
6411
6412 (defun gnus-summary-limit-exclude-childless-dormant ()
6413   "Hide all dormant articles that have no children."
6414   (interactive)
6415   (let ((data (gnus-data-list t))
6416         articles d children)
6417     ;; Find all articles that are either not dormant or have
6418     ;; children.
6419     (while (setq d (pop data))
6420       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6421                 (and (setq children
6422                            (gnus-article-children (gnus-data-number d)))
6423                      (let (found)
6424                        (while children
6425                          (when (memq (car children) articles)
6426                            (setq children nil
6427                                  found t))
6428                          (pop children))
6429                        found)))
6430         (push (gnus-data-number d) articles)))
6431     ;; Do the limiting.
6432     (prog1
6433         (gnus-summary-limit articles)
6434       (gnus-summary-position-point))))
6435
6436 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6437   "Mark all unread excluded articles as read.
6438 If ALL, mark even excluded ticked and dormants as read."
6439   (interactive "P")
6440   (let ((articles (gnus-sorted-complement
6441                    (sort
6442                     (mapcar (lambda (h) (mail-header-number h))
6443                             gnus-newsgroup-headers)
6444                     '<)
6445                    (sort gnus-newsgroup-limit '<)))
6446         article)
6447     (setq gnus-newsgroup-unreads
6448           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6449     (if all
6450         (setq gnus-newsgroup-dormant nil
6451               gnus-newsgroup-marked nil
6452               gnus-newsgroup-reads
6453               (nconc
6454                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6455                gnus-newsgroup-reads))
6456       (while (setq article (pop articles))
6457         (unless (or (memq article gnus-newsgroup-dormant)
6458                     (memq article gnus-newsgroup-marked))
6459           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6460
6461 (defun gnus-summary-limit (articles &optional pop)
6462   (if pop
6463       ;; We pop the previous limit off the stack and use that.
6464       (setq articles (car gnus-newsgroup-limits)
6465             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6466     ;; We use the new limit, so we push the old limit on the stack.
6467     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6468   ;; Set the limit.
6469   (setq gnus-newsgroup-limit articles)
6470   (let ((total (length gnus-newsgroup-data))
6471         (data (gnus-data-find-list (gnus-summary-article-number)))
6472         (gnus-summary-mark-below nil)   ; Inhibit this.
6473         found)
6474     ;; This will do all the work of generating the new summary buffer
6475     ;; according to the new limit.
6476     (gnus-summary-prepare)
6477     ;; Hide any threads, possibly.
6478     (and gnus-show-threads
6479          gnus-thread-hide-subtree
6480          (gnus-summary-hide-all-threads))
6481     ;; Try to return to the article you were at, or one in the
6482     ;; neighborhood.
6483     (when data
6484       ;; We try to find some article after the current one.
6485       (while data
6486         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6487           (setq data nil
6488                 found t))
6489         (setq data (cdr data))))
6490     (unless found
6491       ;; If there is no data, that means that we were after the last
6492       ;; article.  The same goes when we can't find any articles
6493       ;; after the current one.
6494       (goto-char (point-max))
6495       (gnus-summary-find-prev))
6496     (gnus-set-mode-line 'summary)
6497     ;; We return how many articles were removed from the summary
6498     ;; buffer as a result of the new limit.
6499     (- total (length gnus-newsgroup-data))))
6500
6501 (defsubst gnus-invisible-cut-children (threads)
6502   (let ((num 0))
6503     (while threads
6504       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6505         (incf num))
6506       (pop threads))
6507     (< num 2)))
6508
6509 (defsubst gnus-cut-thread (thread)
6510   "Go forwards in the thread until we find an article that we want to display."
6511   (when (or (eq gnus-fetch-old-headers 'some)
6512             (eq gnus-fetch-old-headers 'invisible)
6513             (numberp gnus-fetch-old-headers)
6514             (eq gnus-build-sparse-threads 'some)
6515             (eq gnus-build-sparse-threads 'more))
6516     ;; Deal with old-fetched headers and sparse threads.
6517     (while (and
6518             thread
6519             (or
6520              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6521              (gnus-summary-article-ancient-p
6522               (mail-header-number (car thread))))
6523             (if (or (<= (length (cdr thread)) 1)
6524                     (eq gnus-fetch-old-headers 'invisible))
6525                 (setq gnus-newsgroup-limit
6526                       (delq (mail-header-number (car thread))
6527                             gnus-newsgroup-limit)
6528                       thread (cadr thread))
6529               (when (gnus-invisible-cut-children (cdr thread))
6530                 (let ((th (cdr thread)))
6531                   (while th
6532                     (if (memq (mail-header-number (caar th))
6533                               gnus-newsgroup-limit)
6534                         (setq thread (car th)
6535                               th nil)
6536                       (setq th (cdr th))))))))))
6537   thread)
6538
6539 (defun gnus-cut-threads (threads)
6540   "Cut off all uninteresting articles from the beginning of threads."
6541   (when (or (eq gnus-fetch-old-headers 'some)
6542             (eq gnus-fetch-old-headers 'invisible)
6543             (numberp gnus-fetch-old-headers)
6544             (eq gnus-build-sparse-threads 'some)
6545             (eq gnus-build-sparse-threads 'more))
6546     (let ((th threads))
6547       (while th
6548         (setcar th (gnus-cut-thread (car th)))
6549         (setq th (cdr th)))))
6550   ;; Remove nixed out threads.
6551   (delq nil threads))
6552
6553 (defun gnus-summary-initial-limit (&optional show-if-empty)
6554   "Figure out what the initial limit is supposed to be on group entry.
6555 This entails weeding out unwanted dormants, low-scored articles,
6556 fetch-old-headers verbiage, and so on."
6557   ;; Most groups have nothing to remove.
6558   (if (or gnus-inhibit-limiting
6559           (and (null gnus-newsgroup-dormant)
6560                (not (eq gnus-fetch-old-headers 'some))
6561                (not (numberp gnus-fetch-old-headers))
6562                (not (eq gnus-fetch-old-headers 'invisible))
6563                (null gnus-summary-expunge-below)
6564                (not (eq gnus-build-sparse-threads 'some))
6565                (not (eq gnus-build-sparse-threads 'more))
6566                (null gnus-thread-expunge-below)
6567                (not gnus-use-nocem)))
6568       ()                                ; Do nothing.
6569     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6570     (setq gnus-newsgroup-limit nil)
6571     (mapatoms
6572      (lambda (node)
6573        (unless (car (symbol-value node))
6574          ;; These threads have no parents -- they are roots.
6575          (let ((nodes (cdr (symbol-value node)))
6576                thread)
6577            (while nodes
6578              (if (and gnus-thread-expunge-below
6579                       (< (gnus-thread-total-score (car nodes))
6580                          gnus-thread-expunge-below))
6581                  (gnus-expunge-thread (pop nodes))
6582                (setq thread (pop nodes))
6583                (gnus-summary-limit-children thread))))))
6584      gnus-newsgroup-dependencies)
6585     ;; If this limitation resulted in an empty group, we might
6586     ;; pop the previous limit and use it instead.
6587     (when (and (not gnus-newsgroup-limit)
6588                show-if-empty)
6589       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6590     gnus-newsgroup-limit))
6591
6592 (defun gnus-summary-limit-children (thread)
6593   "Return 1 if this subthread is visible and 0 if it is not."
6594   ;; First we get the number of visible children to this thread.  This
6595   ;; is done by recursing down the thread using this function, so this
6596   ;; will really go down to a leaf article first, before slowly
6597   ;; working its way up towards the root.
6598   (when thread
6599     (let ((children
6600            (if (cdr thread)
6601                (apply '+ (mapcar 'gnus-summary-limit-children
6602                                  (cdr thread)))
6603              0))
6604           (number (mail-header-number (car thread)))
6605           score)
6606       (if (and
6607            (not (memq number gnus-newsgroup-marked))
6608            (or
6609             ;; If this article is dormant and has absolutely no visible
6610             ;; children, then this article isn't visible.
6611             (and (memq number gnus-newsgroup-dormant)
6612                  (zerop children))
6613             ;; If this is "fetch-old-headered" and there is no
6614             ;; visible children, then we don't want this article.
6615             (and (or (eq gnus-fetch-old-headers 'some)
6616                      (numberp gnus-fetch-old-headers))
6617                  (gnus-summary-article-ancient-p number)
6618                  (zerop children))
6619             ;; If this is "fetch-old-headered" and `invisible', then
6620             ;; we don't want this article.
6621             (and (eq gnus-fetch-old-headers 'invisible)
6622                  (gnus-summary-article-ancient-p number))
6623             ;; If this is a sparsely inserted article with no children,
6624             ;; we don't want it.
6625             (and (eq gnus-build-sparse-threads 'some)
6626                  (gnus-summary-article-sparse-p number)
6627                  (zerop children))
6628             ;; If we use expunging, and this article is really
6629             ;; low-scored, then we don't want this article.
6630             (when (and gnus-summary-expunge-below
6631                        (< (setq score
6632                                 (or (cdr (assq number gnus-newsgroup-scored))
6633                                     gnus-summary-default-score))
6634                           gnus-summary-expunge-below))
6635               ;; We increase the expunge-tally here, but that has
6636               ;; nothing to do with the limits, really.
6637               (incf gnus-newsgroup-expunged-tally)
6638               ;; We also mark as read here, if that's wanted.
6639               (when (and gnus-summary-mark-below
6640                          (< score gnus-summary-mark-below))
6641                 (setq gnus-newsgroup-unreads
6642                       (delq number gnus-newsgroup-unreads))
6643                 (if gnus-newsgroup-auto-expire
6644                     (push number gnus-newsgroup-expirable)
6645                   (push (cons number gnus-low-score-mark)
6646                         gnus-newsgroup-reads)))
6647               t)
6648             ;; Check NoCeM things.
6649             (if (and gnus-use-nocem
6650                      (gnus-nocem-unwanted-article-p
6651                       (mail-header-id (car thread))))
6652                 (progn
6653                   (setq gnus-newsgroup-unreads
6654                         (delq number gnus-newsgroup-unreads))
6655                   t))))
6656           ;; Nope, invisible article.
6657           0
6658         ;; Ok, this article is to be visible, so we add it to the limit
6659         ;; and return 1.
6660         (push number gnus-newsgroup-limit)
6661         1))))
6662
6663 (defun gnus-expunge-thread (thread)
6664   "Mark all articles in THREAD as read."
6665   (let* ((number (mail-header-number (car thread))))
6666     (incf gnus-newsgroup-expunged-tally)
6667     ;; We also mark as read here, if that's wanted.
6668     (setq gnus-newsgroup-unreads
6669           (delq number gnus-newsgroup-unreads))
6670     (if gnus-newsgroup-auto-expire
6671         (push number gnus-newsgroup-expirable)
6672       (push (cons number gnus-low-score-mark)
6673             gnus-newsgroup-reads)))
6674   ;; Go recursively through all subthreads.
6675   (mapcar 'gnus-expunge-thread (cdr thread)))
6676
6677 ;; Summary article oriented commands
6678
6679 (defun gnus-summary-refer-parent-article (n)
6680   "Refer parent article N times.
6681 If N is negative, go to ancestor -N instead.
6682 The difference between N and the number of articles fetched is returned."
6683   (interactive "p")
6684   (let ((skip 1)
6685         error header ref)
6686     (when (not (natnump n))
6687       (setq skip (abs n)
6688             n 1))
6689     (while (and (> n 0)
6690                 (not error))
6691       (setq header (gnus-summary-article-header))
6692       (if (and (eq (mail-header-number header)
6693                    (cdr gnus-article-current))
6694                (equal gnus-newsgroup-name
6695                       (car gnus-article-current)))
6696           ;; If we try to find the parent of the currently
6697           ;; displayed article, then we take a look at the actual
6698           ;; References header, since this is slightly more
6699           ;; reliable than the References field we got from the
6700           ;; server.
6701           (save-excursion
6702             (set-buffer gnus-original-article-buffer)
6703             (nnheader-narrow-to-headers)
6704             (unless (setq ref (message-fetch-field "references"))
6705               (setq ref (message-fetch-field "in-reply-to")))
6706             (widen))
6707         (setq ref
6708               ;; It's not the current article, so we take a bet on
6709               ;; the value we got from the server.
6710               (mail-header-references header)))
6711       (if (and ref
6712                (not (equal ref "")))
6713           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6714             (gnus-message 1 "Couldn't find parent"))
6715         (gnus-message 1 "No references in article %d"
6716                       (gnus-summary-article-number))
6717         (setq error t))
6718       (decf n))
6719     (gnus-summary-position-point)
6720     n))
6721
6722 (defun gnus-summary-refer-references ()
6723   "Fetch all articles mentioned in the References header.
6724 Return the number of articles fetched."
6725   (interactive)
6726   (let ((ref (mail-header-references (gnus-summary-article-header)))
6727         (current (gnus-summary-article-number))
6728         (n 0))
6729     (if (or (not ref)
6730             (equal ref ""))
6731         (error "No References in the current article")
6732       ;; For each Message-ID in the References header...
6733       (while (string-match "<[^>]*>" ref)
6734         (incf n)
6735         ;; ... fetch that article.
6736         (gnus-summary-refer-article
6737          (prog1 (match-string 0 ref)
6738            (setq ref (substring ref (match-end 0))))))
6739       (gnus-summary-goto-subject current)
6740       (gnus-summary-position-point)
6741       n)))
6742
6743 (defun gnus-summary-refer-thread (&optional limit)
6744   "Fetch all articles in the current thread.
6745 If LIMIT (the numerical prefix), fetch that many old headers instead
6746 of what's specified by the `gnus-refer-thread-limit' variable."
6747   (interactive "P")
6748   (let ((id (mail-header-id (gnus-summary-article-header)))
6749         (limit (if limit (prefix-numeric-value limit)
6750                  gnus-refer-thread-limit)))
6751     ;; We want to fetch LIMIT *old* headers, but we also have to
6752     ;; re-fetch all the headers in the current buffer, because many of
6753     ;; them may be undisplayed.  So we adjust LIMIT.
6754     (when (numberp limit)
6755       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6756     (unless (eq gnus-fetch-old-headers 'invisible)
6757       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6758       ;; Retrieve the headers and read them in.
6759       (if (eq (gnus-retrieve-headers
6760                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6761               'nov)
6762           (gnus-build-all-threads)
6763         (error "Can't fetch thread from backends that don't support NOV"))
6764       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6765     (gnus-summary-limit-include-thread id)))
6766
6767 (defun gnus-summary-refer-article (message-id)
6768   "Fetch an article specified by MESSAGE-ID."
6769   (interactive "sMessage-ID: ")
6770   (when (and (stringp message-id)
6771              (not (zerop (length message-id))))
6772     ;; Construct the correct Message-ID if necessary.
6773     ;; Suggested by tale@pawl.rpi.edu.
6774     (unless (string-match "^<" message-id)
6775       (setq message-id (concat "<" message-id)))
6776     (unless (string-match ">$" message-id)
6777       (setq message-id (concat message-id ">")))
6778     (let* ((header (gnus-id-to-header message-id))
6779            (sparse (and header
6780                         (gnus-summary-article-sparse-p
6781                          (mail-header-number header))
6782                         (memq (mail-header-number header)
6783                               gnus-newsgroup-limit)))
6784            number)
6785       (cond
6786        ;; If the article is present in the buffer we just go to it.
6787        ((and header
6788              (or (not (gnus-summary-article-sparse-p
6789                        (mail-header-number header)))
6790                  sparse))
6791         (prog1
6792             (gnus-summary-goto-article
6793              (mail-header-number header) nil t)
6794           (when sparse
6795             (gnus-summary-update-article (mail-header-number header)))))
6796        (t
6797         ;; We fetch the article.
6798         (catch 'found
6799           (dolist (gnus-override-method
6800                    (cond ((null gnus-refer-article-method)
6801                           (list 'current gnus-select-method))
6802                          ((consp (car gnus-refer-article-method))
6803                           gnus-refer-article-method)
6804                          (t
6805                           (list gnus-refer-article-method))))
6806             (when (eq 'current gnus-override-method)
6807               (setq gnus-override-method gnus-current-select-method))
6808             (gnus-check-server gnus-override-method)
6809             ;; Fetch the header, and display the article.
6810             (when (setq number (gnus-summary-insert-subject message-id))
6811               (gnus-summary-select-article nil nil nil number)
6812               (throw 'found t)))
6813           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6814
6815 (defun gnus-summary-edit-parameters ()
6816   "Edit the group parameters of the current group."
6817   (interactive)
6818   (gnus-group-edit-group gnus-newsgroup-name 'params))
6819
6820 (defun gnus-summary-customize-parameters ()
6821   "Customize the group parameters of the current group."
6822   (interactive)
6823   (gnus-group-customize gnus-newsgroup-name))
6824
6825 (defun gnus-summary-enter-digest-group (&optional force)
6826   "Enter an nndoc group based on the current article.
6827 If FORCE, force a digest interpretation.  If not, try
6828 to guess what the document format is."
6829   (interactive "P")
6830   (let ((conf gnus-current-window-configuration))
6831     (save-excursion
6832       (gnus-summary-select-article))
6833     (setq gnus-current-window-configuration conf)
6834     (let* ((name (format "%s-%d"
6835                          (gnus-group-prefixed-name
6836                           gnus-newsgroup-name (list 'nndoc ""))
6837                          (save-excursion
6838                            (set-buffer gnus-summary-buffer)
6839                            gnus-current-article)))
6840            (ogroup gnus-newsgroup-name)
6841            (params (append (gnus-info-params (gnus-get-info ogroup))
6842                            (list (cons 'to-group ogroup))
6843                            (list (cons 'save-article-group ogroup))))
6844            (case-fold-search t)
6845            (buf (current-buffer))
6846            dig)
6847       (save-excursion
6848         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6849         (insert-buffer-substring gnus-original-article-buffer)
6850         ;; Remove lines that may lead nndoc to misinterpret the
6851         ;; document type.
6852         (narrow-to-region
6853          (goto-char (point-min))
6854          (or (search-forward "\n\n" nil t) (point)))
6855         (goto-char (point-min))
6856         (delete-matching-lines "^Path:\\|^From ")
6857         (widen))
6858       (unwind-protect
6859           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6860                     (gnus-newsgroup-ephemeral-ignored-charsets
6861                      gnus-newsgroup-ignored-charsets))
6862                 (gnus-group-read-ephemeral-group
6863                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6864                               (nndoc-article-type
6865                                ,(if force 'mbox 'guess))) t))
6866               ;; Make all postings to this group go to the parent group.
6867               (nconc (gnus-info-params (gnus-get-info name))
6868                      params)
6869             ;; Couldn't select this doc group.
6870             (switch-to-buffer buf)
6871             (gnus-set-global-variables)
6872             (gnus-configure-windows 'summary)
6873             (gnus-message 3 "Article couldn't be entered?"))
6874         (kill-buffer dig)))))
6875
6876 (defun gnus-summary-read-document (n)
6877   "Open a new group based on the current article(s).
6878 This will allow you to read digests and other similar
6879 documents as newsgroups.
6880 Obeys the standard process/prefix convention."
6881   (interactive "P")
6882   (let* ((articles (gnus-summary-work-articles n))
6883          (ogroup gnus-newsgroup-name)
6884          (params (append (gnus-info-params (gnus-get-info ogroup))
6885                          (list (cons 'to-group ogroup))))
6886          article group egroup groups vgroup)
6887     (while (setq article (pop articles))
6888       (setq group (format "%s-%d" gnus-newsgroup-name article))
6889       (gnus-summary-remove-process-mark article)
6890       (when (gnus-summary-display-article article)
6891         (save-excursion
6892           (with-temp-buffer
6893             (insert-buffer-substring gnus-original-article-buffer)
6894             ;; Remove some headers that may lead nndoc to make
6895             ;; the wrong guess.
6896             (message-narrow-to-head)
6897             (goto-char (point-min))
6898             (delete-matching-lines "^\\(Path\\):\\|^From ")
6899             (widen)
6900             (if (setq egroup
6901                       (gnus-group-read-ephemeral-group
6902                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6903                                      (nndoc-article-type guess))
6904                        t nil t))
6905                 (progn
6906                   ;; Make all postings to this group go to the parent group.
6907                   (nconc (gnus-info-params (gnus-get-info egroup))
6908                          params)
6909                   (push egroup groups))
6910               ;; Couldn't select this doc group.
6911               (gnus-error 3 "Article couldn't be entered"))))))
6912     ;; Now we have selected all the documents.
6913     (cond
6914      ((not groups)
6915       (error "None of the articles could be interpreted as documents"))
6916      ((gnus-group-read-ephemeral-group
6917        (setq vgroup (format
6918                      "nnvirtual:%s-%s" gnus-newsgroup-name
6919                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6920        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6921        t
6922        (cons (current-buffer) 'summary)))
6923      (t
6924       (error "Couldn't select virtual nndoc group")))))
6925
6926 (defun gnus-summary-isearch-article (&optional regexp-p)
6927   "Do incremental search forward on the current article.
6928 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6929   (interactive "P")
6930   (let* ((gnus-inhibit-treatment t)
6931          (old (gnus-summary-select-article)))
6932     (gnus-configure-windows 'article)
6933     (gnus-eval-in-buffer-window gnus-article-buffer
6934       (save-restriction
6935         (widen)
6936         (when (eq 'old old)
6937           (gnus-article-show-all-headers))
6938         (goto-char (point-min))
6939         (isearch-forward regexp-p)))))
6940
6941 (defun gnus-summary-search-article-forward (regexp &optional backward)
6942   "Search for an article containing REGEXP forward.
6943 If BACKWARD, search backward instead."
6944   (interactive
6945    (list (read-string
6946           (format "Search article %s (regexp%s): "
6947                   (if current-prefix-arg "backward" "forward")
6948                   (if gnus-last-search-regexp
6949                       (concat ", default " gnus-last-search-regexp)
6950                     "")))
6951          current-prefix-arg))
6952   (if (string-equal regexp "")
6953       (setq regexp (or gnus-last-search-regexp ""))
6954     (setq gnus-last-search-regexp regexp))
6955   (if (gnus-summary-search-article regexp backward)
6956       (gnus-summary-show-thread)
6957     (error "Search failed: \"%s\"" regexp)))
6958
6959 (defun gnus-summary-search-article-backward (regexp)
6960   "Search for an article containing REGEXP backward."
6961   (interactive
6962    (list (read-string
6963           (format "Search article backward (regexp%s): "
6964                   (if gnus-last-search-regexp
6965                       (concat ", default " gnus-last-search-regexp)
6966                     "")))))
6967   (gnus-summary-search-article-forward regexp 'backward))
6968
6969 (eval-when-compile
6970   (defmacro gnus-summary-search-article-position-point (regexp backward)
6971     "Dehighlight the last matched text and goto the beginning position."
6972     (` (if (and gnus-summary-search-article-matched-data
6973                 (let ((text (caddr gnus-summary-search-article-matched-data))
6974                       (inhibit-read-only t)
6975                       buffer-read-only)
6976                   (delete-region
6977                    (goto-char (car gnus-summary-search-article-matched-data))
6978                    (cadr gnus-summary-search-article-matched-data))
6979                   (insert text)
6980                   (string-match (, regexp) text)))
6981            (if (, backward) (beginning-of-line) (end-of-line))
6982          (goto-char (if (, backward) (point-max) (point-min))))))
6983
6984   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
6985     "Place point where X-Face image is displayed."
6986     (if (featurep 'xemacs)
6987         (` (let ((end (if (search-forward "\n\n" nil t)
6988                           (goto-char (1- (point)))
6989                         (point-min)))
6990                  extent)
6991              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
6992              (unless (and (re-search-forward "^From:" end t)
6993                           (setq extent (extent-at (point)))
6994                           (extent-begin-glyph extent))
6995                (goto-char (, opoint)))))
6996       (` (let ((end (if (search-forward "\n\n" nil t)
6997                         (goto-char (1- (point)))
6998                       (point-min))))
6999            (goto-char
7000             (or (text-property-any (or (search-backward "\n\n" nil t)
7001                                        (point-min))
7002                                    end 'x-face-mule-bitmap-image t)
7003                 (, opoint)))))))
7004
7005   (defmacro gnus-summary-search-article-highlight-matched-text
7006     (backward treated x-face)
7007     "Highlight matched text in the function `gnus-summary-search-article'."
7008     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7009              (end (set-marker (make-marker) (match-end 0)))
7010              (inhibit-read-only t)
7011              buffer-read-only)
7012          (unless treated
7013            (let ((,@
7014                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7015                     (mapcar
7016                      (lambda (item) (setq items (delq item items)))
7017                      '(gnus-treat-buttonize
7018                        gnus-treat-fill-article
7019                        gnus-treat-fill-long-lines
7020                        gnus-treat-emphasize
7021                        gnus-treat-highlight-headers
7022                        gnus-treat-highlight-citation
7023                        gnus-treat-highlight-signature
7024                        gnus-treat-overstrike
7025                        gnus-treat-display-xface
7026                        gnus-treat-buttonize-head
7027                        gnus-treat-decode-article-as-default-mime-charset))
7028                     (static-if (featurep 'xemacs)
7029                         items
7030                       (cons '(x-face-mule-delete-x-face-field
7031                               (quote never))
7032                             items))))
7033                  (gnus-treat-display-xface
7034                   (when (, x-face) gnus-treat-display-xface)))
7035              (gnus-article-prepare-mime-display)))
7036          (goto-char (if (, backward) start end))
7037          (when (, x-face)
7038            (gnus-summary-search-article-highlight-goto-x-face (point)))
7039          (setq gnus-summary-search-article-matched-data
7040                (list start end (buffer-substring start end)))
7041          (unless (eq start end);; matched text has been deleted. :-<
7042            (put-text-property start end 'face
7043                               (or (find-face 'isearch)
7044                                   'secondary-selection))))))
7045   )
7046
7047 (defun gnus-summary-search-article (regexp &optional backward)
7048   "Search for an article containing REGEXP.
7049 Optional argument BACKWARD means do search for backward.
7050 `gnus-select-article-hook' is not called during the search."
7051   ;; We have to require this here to make sure that the following
7052   ;; dynamic binding isn't shadowed by autoloading.
7053   (require 'gnus-async)
7054   (require 'gnus-art)
7055   (let ((gnus-select-article-hook nil)  ;Disable hook.
7056         (gnus-article-display-hook nil)
7057         (gnus-article-prepare-hook nil)
7058         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7059         (gnus-use-article-prefetch nil)
7060         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7061         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7062         (sum (current-buffer))
7063         (found nil)
7064         point treated)
7065     (gnus-save-hidden-threads
7066       (static-if (featurep 'xemacs)
7067           (let ((gnus-inhibit-treatment t))
7068             (setq treated (eq 'old (gnus-summary-select-article)))
7069             (when (and treated
7070                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7071                                  (window-live-p (get-buffer-window
7072                                                  gnus-article-buffer t)))))
7073               (gnus-summary-select-article nil t)
7074               (setq treated nil)))
7075         (let ((gnus-inhibit-treatment t)
7076               (x-face-mule-delete-x-face-field 'never))
7077           (setq treated (eq 'old (gnus-summary-select-article)))
7078           (when (and treated
7079                      (not
7080                       (and (gnus-buffer-live-p gnus-article-buffer)
7081                            (window-live-p (get-buffer-window
7082                                            gnus-article-buffer t))
7083                            (or (not (string-match "^\\^X-Face:" regexp))
7084                                (with-current-buffer gnus-article-buffer
7085                                  gnus-summary-search-article-matched-data)))))
7086             (gnus-summary-select-article nil t)
7087             (setq treated nil))))
7088       (set-buffer gnus-article-buffer)
7089       (widen)
7090       (if treated
7091           (progn
7092             (gnus-article-show-all-headers)
7093             (gnus-summary-search-article-position-point regexp backward))
7094         (goto-char (if backward (point-max) (point-min))))
7095       (while (not found)
7096         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7097         (if (if backward
7098                 (re-search-backward regexp nil t)
7099               (re-search-forward regexp nil t))
7100             ;; We found the regexp.
7101             (progn
7102               (gnus-summary-search-article-highlight-matched-text
7103                backward treated (string-match "^\\^X-Face:" regexp))
7104               (setq found 'found)
7105               (forward-line
7106                (/ (- 2 (window-height
7107                         (get-buffer-window gnus-article-buffer t)))
7108                   2))
7109               (set-window-start
7110                (get-buffer-window (current-buffer))
7111                (point))
7112               (set-buffer sum)
7113               (setq point (point)))
7114           ;; We didn't find it, so we go to the next article.
7115           (set-buffer sum)
7116           (setq found 'not)
7117           (while (eq found 'not)
7118             (if (not (if backward (gnus-summary-find-prev)
7119                        (gnus-summary-find-next)))
7120                 ;; No more articles.
7121                 (setq found t)
7122               ;; Select the next article and adjust point.
7123               (unless (gnus-summary-article-sparse-p
7124                        (gnus-summary-article-number))
7125                 (setq found nil)
7126                 (let ((gnus-inhibit-treatment t))
7127                   (gnus-summary-select-article))
7128                 (setq treated nil)
7129                 (set-buffer gnus-article-buffer)
7130                 (widen)
7131                 (goto-char (if backward (point-max) (point-min))))))))
7132       (gnus-message 7 ""))
7133     ;; Return whether we found the regexp.
7134     (when (eq found 'found)
7135       (goto-char point)
7136       (gnus-summary-show-thread)
7137       (gnus-summary-goto-subject gnus-current-article)
7138       (gnus-summary-position-point)
7139       t)))
7140
7141 (defun gnus-summary-find-matching (header regexp &optional backward unread
7142                                           not-case-fold)
7143   "Return a list of all articles that match REGEXP on HEADER.
7144 The search stars on the current article and goes forwards unless
7145 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7146 If UNREAD is non-nil, only unread articles will
7147 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7148 in the comparisons."
7149   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7150                 (gnus-data-find-list
7151                  (gnus-summary-article-number) (gnus-data-list backward))))
7152         (case-fold-search (not not-case-fold))
7153         articles d func)
7154     (if (consp header)
7155         (if (eq (car header) 'extra)
7156             (setq func
7157                   `(lambda (h)
7158                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7159                          "")))
7160           (error "%s is an invalid header" header))
7161       (unless (fboundp (intern (concat "mail-header-" header)))
7162         (error "%s is not a valid header" header))
7163       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7164     (while data
7165       (setq d (car data))
7166       (and (or (not unread)             ; We want all articles...
7167                (gnus-data-unread-p d))  ; Or just unreads.
7168            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7169            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7170            (push (gnus-data-number d) articles)) ; Success!
7171       (setq data (cdr data)))
7172     (nreverse articles)))
7173
7174 (defun gnus-summary-execute-command (header regexp command &optional backward)
7175   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7176 If HEADER is an empty string (or nil), the match is done on the entire
7177 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7178   (interactive
7179    (list (let ((completion-ignore-case t))
7180            (completing-read
7181             "Header name: "
7182             (mapcar (lambda (string) (list string))
7183                     '("Number" "Subject" "From" "Lines" "Date"
7184                       "Message-ID" "Xref" "References" "Body"))
7185             nil 'require-match))
7186          (read-string "Regexp: ")
7187          (read-key-sequence "Command: ")
7188          current-prefix-arg))
7189   (when (equal header "Body")
7190     (setq header ""))
7191   ;; Hidden thread subtrees must be searched as well.
7192   (gnus-summary-show-all-threads)
7193   ;; We don't want to change current point nor window configuration.
7194   (save-excursion
7195     (save-window-excursion
7196       (gnus-message 6 "Executing %s..." (key-description command))
7197       ;; We'd like to execute COMMAND interactively so as to give arguments.
7198       (gnus-execute header regexp
7199                     `(call-interactively ',(key-binding command))
7200                     backward)
7201       (gnus-message 6 "Executing %s...done" (key-description command)))))
7202
7203 (defun gnus-summary-beginning-of-article ()
7204   "Scroll the article back to the beginning."
7205   (interactive)
7206   (gnus-summary-select-article)
7207   (gnus-configure-windows 'article)
7208   (gnus-eval-in-buffer-window gnus-article-buffer
7209     (widen)
7210     (goto-char (point-min))
7211     (when gnus-page-broken
7212       (gnus-narrow-to-page))))
7213
7214 (defun gnus-summary-end-of-article ()
7215   "Scroll to the end of the article."
7216   (interactive)
7217   (gnus-summary-select-article)
7218   (gnus-configure-windows 'article)
7219   (gnus-eval-in-buffer-window gnus-article-buffer
7220     (widen)
7221     (goto-char (point-max))
7222     (recenter -3)
7223     (when gnus-page-broken
7224       (gnus-narrow-to-page))))
7225
7226 (defun gnus-summary-print-article (&optional filename n)
7227   "Generate and print a PostScript image of the N next (mail) articles.
7228
7229 If N is negative, print the N previous articles.  If N is nil and articles
7230 have been marked with the process mark, print these instead.
7231
7232 If the optional first argument FILENAME is nil, send the image to the
7233 printer.  If FILENAME is a string, save the PostScript image in a file with
7234 that name.  If FILENAME is a number, prompt the user for the name of the file
7235 to save in."
7236   (interactive (list (ps-print-preprint current-prefix-arg)
7237                      current-prefix-arg))
7238   (dolist (article (gnus-summary-work-articles n))
7239     (gnus-summary-select-article nil nil 'pseudo article)
7240     (gnus-eval-in-buffer-window gnus-article-buffer
7241       (let ((buffer (generate-new-buffer " *print*")))
7242         (unwind-protect
7243             (progn
7244               (copy-to-buffer buffer (point-min) (point-max))
7245               (set-buffer buffer)
7246               (gnus-article-delete-invisible-text)
7247               (let ((ps-left-header
7248                      (list
7249                       (concat "("
7250                               (mail-header-subject gnus-current-headers) ")")
7251                       (concat "("
7252                               (mail-header-from gnus-current-headers) ")")))
7253                     (ps-right-header
7254                      (list
7255                       "/pagenumberstring load"
7256                       (concat "("
7257                               (mail-header-date gnus-current-headers) ")"))))
7258                 (gnus-run-hooks 'gnus-ps-print-hook)
7259                 (save-excursion
7260                   (ps-print-buffer-with-faces filename))))
7261           (kill-buffer buffer))))))
7262
7263 (defun gnus-summary-show-article (&optional arg)
7264   "Force re-fetching of the current article.
7265 If ARG (the prefix) is non-nil, show the raw article without any
7266 article massaging functions being run."
7267   (interactive "P")
7268   (if (not arg)
7269       ;; Select the article the normal way.
7270       (gnus-summary-select-article nil 'force)
7271     ;; We have to require this here to make sure that the following
7272     ;; dynamic binding isn't shadowed by autoloading.
7273     (require 'gnus-async)
7274     (require 'gnus-art)
7275     ;; Bind the article treatment functions to nil.
7276     (let ((gnus-have-all-headers t)
7277           gnus-article-display-hook
7278           gnus-article-prepare-hook
7279           gnus-article-decode-hook
7280           gnus-break-pages
7281           gnus-show-mime
7282           gnus-visual)
7283       (gnus-summary-select-article nil 'force)))
7284   (gnus-summary-goto-subject gnus-current-article)
7285   (gnus-summary-position-point))
7286
7287 (defun gnus-summary-verbose-headers (&optional arg)
7288   "Toggle permanent full header display.
7289 If ARG is a positive number, turn header display on.
7290 If ARG is a negative number, turn header display off."
7291   (interactive "P")
7292   (setq gnus-show-all-headers
7293         (cond ((or (not (numberp arg))
7294                    (zerop arg))
7295                (not gnus-show-all-headers))
7296               ((natnump arg)
7297                t)))
7298   (gnus-summary-show-article))
7299
7300 (defun gnus-summary-toggle-header (&optional arg)
7301   "Show the headers if they are hidden, or hide them if they are shown.
7302 If ARG is a positive number, show the entire header.
7303 If ARG is a negative number, hide the unwanted header lines."
7304   (interactive "P")
7305   (save-excursion
7306     (set-buffer gnus-article-buffer)
7307     (save-restriction
7308       (let* ((buffer-read-only nil)
7309              (inhibit-point-motion-hooks t)
7310              hidden e)
7311         (setq hidden
7312               (if (numberp arg)
7313                   (>= arg 0)
7314                 (save-restriction 
7315                   (article-narrow-to-head)
7316                   (gnus-article-hidden-text-p 'headers))))
7317         (goto-char (point-min))
7318         (when (search-forward "\n\n" nil t)
7319           (delete-region (point-min) (1- (point))))
7320         (goto-char (point-min))
7321         (save-excursion
7322           (set-buffer gnus-original-article-buffer)
7323           (goto-char (point-min))
7324           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7325         (insert-buffer-substring gnus-original-article-buffer 1 e)
7326         (save-restriction
7327           (narrow-to-region (point-min) (point))
7328           (article-decode-encoded-words)
7329           (if  hidden
7330               (let ((gnus-treat-hide-headers nil)
7331                     (gnus-treat-hide-boring-headers nil))
7332                 (gnus-treat-article 'head))
7333             (gnus-treat-article 'head)))))))
7334
7335 (defun gnus-summary-show-all-headers ()
7336   "Make all header lines visible."
7337   (interactive)
7338   (gnus-article-show-all-headers))
7339
7340 (defun gnus-summary-toggle-mime (&optional arg)
7341   "Toggle MIME processing.
7342 If ARG is a positive number, turn MIME processing on."
7343   (interactive "P")
7344   (setq gnus-show-mime
7345         (if (null arg)
7346             (not gnus-show-mime)
7347           (> (prefix-numeric-value arg) 0)))
7348   (gnus-summary-select-article t 'force))
7349
7350 (defun gnus-summary-caesar-message (&optional arg)
7351   "Caesar rotate the current article by 13.
7352 The numerical prefix specifies how many places to rotate each letter
7353 forward."
7354   (interactive "P")
7355   (gnus-summary-select-article)
7356   (let ((mail-header-separator ""))
7357     (gnus-eval-in-buffer-window gnus-article-buffer
7358       (save-restriction
7359         (widen)
7360         (let ((start (window-start))
7361               buffer-read-only)
7362           (message-caesar-buffer-body arg)
7363           (set-window-start (get-buffer-window (current-buffer)) start))))))
7364
7365 (defun gnus-summary-stop-page-breaking ()
7366   "Stop page breaking in the current article."
7367   (interactive)
7368   (gnus-summary-select-article)
7369   (gnus-eval-in-buffer-window gnus-article-buffer
7370     (widen)
7371     (when (gnus-visual-p 'page-marker)
7372       (let ((buffer-read-only nil))
7373         (gnus-remove-text-with-property 'gnus-prev)
7374         (gnus-remove-text-with-property 'gnus-next))
7375       (setq gnus-page-broken nil))))
7376
7377 (defun gnus-summary-move-article (&optional n to-newsgroup
7378                                             select-method action)
7379   "Move the current article to a different newsgroup.
7380 If N is a positive number, move the N next articles.
7381 If N is a negative number, move the N previous articles.
7382 If N is nil and any articles have been marked with the process mark,
7383 move those articles instead.
7384 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7385 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7386 re-spool using this method.
7387
7388 For this function to work, both the current newsgroup and the
7389 newsgroup that you want to move to have to support the `request-move'
7390 and `request-accept' functions."
7391   (interactive "P")
7392   (unless action
7393     (setq action 'move))
7394   ;; Disable marking as read.
7395   (let (gnus-mark-article-hook)
7396     (save-window-excursion
7397       (gnus-summary-select-article)))
7398   ;; Check whether the source group supports the required functions.
7399   (cond ((and (eq action 'move)
7400               (not (gnus-check-backend-function
7401                     'request-move-article gnus-newsgroup-name)))
7402          (error "The current group does not support article moving"))
7403         ((and (eq action 'crosspost)
7404               (not (gnus-check-backend-function
7405                     'request-replace-article gnus-newsgroup-name)))
7406          (error "The current group does not support article editing")))
7407   (let ((articles (gnus-summary-work-articles n))
7408         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7409         (names '((move "Move" "Moving")
7410                  (copy "Copy" "Copying")
7411                  (crosspost "Crosspost" "Crossposting")))
7412         (copy-buf (save-excursion
7413                     (nnheader-set-temp-buffer " *copy article*")))
7414         (default-marks gnus-article-mark-lists)
7415         (no-expire-marks (delete '(expirable . expire)
7416                                  (copy-sequence gnus-article-mark-lists)))
7417         art-group to-method new-xref article to-groups)
7418     (unless (assq action names)
7419       (error "Unknown action %s" action))
7420     ;; Read the newsgroup name.
7421     (when (and (not to-newsgroup)
7422                (not select-method))
7423       (setq to-newsgroup
7424             (gnus-read-move-group-name
7425              (cadr (assq action names))
7426              (symbol-value (intern (format "gnus-current-%s-group" action)))
7427              articles prefix))
7428       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7429     (setq to-method (or select-method
7430                         (gnus-group-name-to-method to-newsgroup)))
7431     ;; Check the method we are to move this article to...
7432     (unless (gnus-check-backend-function
7433              'request-accept-article (car to-method))
7434       (error "%s does not support article copying" (car to-method)))
7435     (unless (gnus-check-server to-method)
7436       (error "Can't open server %s" (car to-method)))
7437     (gnus-message 6 "%s to %s: %s..."
7438                   (caddr (assq action names))
7439                   (or (car select-method) to-newsgroup) articles)
7440     (while articles
7441       (setq article (pop articles))
7442       (setq
7443        art-group
7444        (cond
7445         ;; Move the article.
7446         ((eq action 'move)
7447          ;; Remove this article from future suppression.
7448          (gnus-dup-unsuppress-article article)
7449          (gnus-request-move-article
7450           article                       ; Article to move
7451           gnus-newsgroup-name           ; From newsgroup
7452           (nth 1 (gnus-find-method-for-group
7453                   gnus-newsgroup-name)) ; Server
7454           (list 'gnus-request-accept-article
7455                 to-newsgroup (list 'quote select-method)
7456                 (not articles) t)               ; Accept form
7457           (not articles)))              ; Only save nov last time
7458         ;; Copy the article.
7459         ((eq action 'copy)
7460          (save-excursion
7461            (set-buffer copy-buf)
7462            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7463              (gnus-request-accept-article
7464               to-newsgroup select-method (not articles) t))))
7465         ;; Crosspost the article.
7466         ((eq action 'crosspost)
7467          (let ((xref (message-tokenize-header
7468                       (mail-header-xref (gnus-summary-article-header article))
7469                       " ")))
7470            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7471                                   ":" article))
7472            (unless xref
7473              (setq xref (list (system-name))))
7474            (setq new-xref
7475                  (concat
7476                   (mapconcat 'identity
7477                              (delete "Xref:" (delete new-xref xref))
7478                              " ")
7479                   " " new-xref))
7480            (save-excursion
7481              (set-buffer copy-buf)
7482              ;; First put the article in the destination group.
7483              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7484              (when (consp (setq art-group
7485                                 (gnus-request-accept-article
7486                                  to-newsgroup select-method (not articles))))
7487                (setq new-xref (concat new-xref " " (car art-group)
7488                                       ":" (cdr art-group)))
7489                ;; Now we have the new Xrefs header, so we insert
7490                ;; it and replace the new article.
7491                (nnheader-replace-header "Xref" new-xref)
7492                (gnus-request-replace-article
7493                 (cdr art-group) to-newsgroup (current-buffer))
7494                art-group))))))
7495       (cond
7496        ((not art-group)
7497        (gnus-message 1 "Couldn't %s article %s: %s"
7498                      (cadr (assq action names)) article
7499                      (nnheader-get-report (car to-method))))
7500        ((and (eq art-group 'junk)
7501              (eq action 'move))
7502         (gnus-summary-mark-article article gnus-canceled-mark)
7503         (gnus-message 4 "Deleted article %s" article))
7504        (t
7505         (let* ((pto-group (gnus-group-prefixed-name
7506                            (car art-group) to-method))
7507                (entry
7508                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7509                (info (nth 2 entry))
7510                (to-group (gnus-info-group info))
7511                to-marks)
7512           ;; Update the group that has been moved to.
7513           (when (and info
7514                      (memq action '(move copy)))
7515             (unless (member to-group to-groups)
7516               (push to-group to-groups))
7517
7518             (unless (memq article gnus-newsgroup-unreads)
7519               (push 'read to-marks)
7520               (gnus-info-set-read
7521                info (gnus-add-to-range (gnus-info-read info)
7522                                        (list (cdr art-group)))))
7523
7524             ;; Copy any marks over to the new group.
7525             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7526                              default-marks
7527                            no-expire-marks))
7528                   (to-article (cdr art-group)))
7529
7530               ;; See whether the article is to be put in the cache.
7531               (when gnus-use-cache
7532                 (gnus-cache-possibly-enter-article
7533                  to-group to-article
7534                  (let ((header (copy-sequence
7535                                 (gnus-summary-article-header article))))
7536                    (mail-header-set-number header to-article)
7537                    header)
7538                  (memq article gnus-newsgroup-marked)
7539                  (memq article gnus-newsgroup-dormant)
7540                  (memq article gnus-newsgroup-unreads)))
7541
7542               (when (and (equal to-group gnus-newsgroup-name)
7543                          (not (memq article gnus-newsgroup-unreads)))
7544                 ;; Mark this article as read in this group.
7545                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7546                 (setcdr (gnus-active to-group) to-article)
7547                 (setcdr gnus-newsgroup-active to-article))
7548
7549               (while marks
7550                 (when (memq article (symbol-value
7551                                      (intern (format "gnus-newsgroup-%s"
7552                                                      (caar marks)))))
7553                   (push (cdar marks) to-marks)
7554                   ;; If the other group is the same as this group,
7555                   ;; then we have to add the mark to the list.
7556                   (when (equal to-group gnus-newsgroup-name)
7557                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7558                          (cons to-article
7559                                (symbol-value
7560                                 (intern (format "gnus-newsgroup-%s"
7561                                                 (caar marks)))))))
7562                   ;; Copy the marks to other group.
7563                   (gnus-add-marked-articles
7564                    to-group (cdar marks) (list to-article) info))
7565                 (setq marks (cdr marks)))
7566
7567               (gnus-request-set-mark to-group (list (list (list to-article)
7568                                                           'set
7569                                                           to-marks)))
7570
7571               (gnus-dribble-enter
7572                (concat "(gnus-group-set-info '"
7573                        (gnus-prin1-to-string (gnus-get-info to-group))
7574                        ")"))))
7575
7576           ;; Update the Xref header in this article to point to
7577           ;; the new crossposted article we have just created.
7578           (when (eq action 'crosspost)
7579             (save-excursion
7580               (set-buffer copy-buf)
7581               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7582               (nnheader-replace-header "Xref" new-xref)
7583               (gnus-request-replace-article
7584                article gnus-newsgroup-name (current-buffer)))))
7585
7586         ;;;!!!Why is this necessary?
7587         (set-buffer gnus-summary-buffer)
7588
7589         (gnus-summary-goto-subject article)
7590         (when (eq action 'move)
7591           (gnus-summary-mark-article article gnus-canceled-mark))))
7592       (gnus-summary-remove-process-mark article))
7593     ;; Re-activate all groups that have been moved to.
7594     (while to-groups
7595       (save-excursion
7596         (set-buffer gnus-group-buffer)
7597         (when (gnus-group-goto-group (car to-groups) t)
7598           (gnus-group-get-new-news-this-group 1 t))
7599         (pop to-groups)))
7600
7601     (gnus-kill-buffer copy-buf)
7602     (gnus-summary-position-point)
7603     (gnus-set-mode-line 'summary)))
7604
7605 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7606   "Move the current article to a different newsgroup.
7607 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7608 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7609 re-spool using this method."
7610   (interactive "P")
7611   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7612
7613 (defun gnus-summary-crosspost-article (&optional n)
7614   "Crosspost the current article to some other group."
7615   (interactive "P")
7616   (gnus-summary-move-article n nil nil 'crosspost))
7617
7618 (defcustom gnus-summary-respool-default-method nil
7619   "Default method for respooling an article.
7620 If nil, use to the current newsgroup method."
7621   :type '(choice (gnus-select-method :value (nnml ""))
7622                  (const nil))
7623   :group 'gnus-summary-mail)
7624
7625 (defun gnus-summary-respool-article (&optional n method)
7626   "Respool the current article.
7627 The article will be squeezed through the mail spooling process again,
7628 which means that it will be put in some mail newsgroup or other
7629 depending on `nnmail-split-methods'.
7630 If N is a positive number, respool the N next articles.
7631 If N is a negative number, respool the N previous articles.
7632 If N is nil and any articles have been marked with the process mark,
7633 respool those articles instead.
7634
7635 Respooling can be done both from mail groups and \"real\" newsgroups.
7636 In the former case, the articles in question will be moved from the
7637 current group into whatever groups they are destined to.  In the
7638 latter case, they will be copied into the relevant groups."
7639   (interactive
7640    (list current-prefix-arg
7641          (let* ((methods (gnus-methods-using 'respool))
7642                 (methname
7643                  (symbol-name (or gnus-summary-respool-default-method
7644                                   (car (gnus-find-method-for-group
7645                                         gnus-newsgroup-name)))))
7646                 (method
7647                  (gnus-completing-read
7648                   methname "What backend do you want to use when respooling?"
7649                   methods nil t nil 'gnus-mail-method-history))
7650                 ms)
7651            (cond
7652             ((zerop (length (setq ms (gnus-servers-using-backend
7653                                       (intern method)))))
7654              (list (intern method) ""))
7655             ((= 1 (length ms))
7656              (car ms))
7657             (t
7658              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7659                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7660                            ms-alist))))))))
7661   (unless method
7662     (error "No method given for respooling"))
7663   (if (assoc (symbol-name
7664               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7665              (gnus-methods-using 'respool))
7666       (gnus-summary-move-article n nil method)
7667     (gnus-summary-copy-article n nil method)))
7668
7669 (defun gnus-summary-import-article (file)
7670   "Import an arbitrary file into a mail newsgroup."
7671   (interactive "fImport file: ")
7672   (let ((group gnus-newsgroup-name)
7673         (now (current-time))
7674         atts lines)
7675     (unless (gnus-check-backend-function 'request-accept-article group)
7676       (error "%s does not support article importing" group))
7677     (or (file-readable-p file)
7678         (not (file-regular-p file))
7679         (error "Can't read %s" file))
7680     (save-excursion
7681       (set-buffer (gnus-get-buffer-create " *import file*"))
7682       (erase-buffer)
7683       (nnheader-insert-file-contents file)
7684       (goto-char (point-min))
7685       (unless (nnheader-article-p)
7686         ;; This doesn't look like an article, so we fudge some headers.
7687         (setq atts (file-attributes file)
7688               lines (count-lines (point-min) (point-max)))
7689         (insert "From: " (read-string "From: ") "\n"
7690                 "Subject: " (read-string "Subject: ") "\n"
7691                 "Date: " (message-make-date (nth 5 atts))
7692                 "\n"
7693                 "Message-ID: " (message-make-message-id) "\n"
7694                 "Lines: " (int-to-string lines) "\n"
7695                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7696       (gnus-request-accept-article group nil t)
7697       (kill-buffer (current-buffer)))))
7698
7699 (defun gnus-summary-article-posted-p ()
7700   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7701 This will be the case if the article has both been mailed and posted."
7702   (interactive)
7703   (let ((id (mail-header-references (gnus-summary-article-header)))
7704         (gnus-override-method
7705          (or gnus-refer-article-method gnus-select-method)))
7706     (if (gnus-request-head id "")
7707         (gnus-message 2 "The current message was found on %s"
7708                       gnus-override-method)
7709       (gnus-message 2 "The current message couldn't be found on %s"
7710                     gnus-override-method)
7711       nil)))
7712
7713 (defun gnus-summary-expire-articles (&optional now)
7714   "Expire all articles that are marked as expirable in the current group."
7715   (interactive)
7716   (when (gnus-check-backend-function
7717          'request-expire-articles gnus-newsgroup-name)
7718     ;; This backend supports expiry.
7719     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7720            (expirable (if total
7721                           (progn
7722                             ;; We need to update the info for
7723                             ;; this group for `gnus-list-of-read-articles'
7724                             ;; to give us the right answer.
7725                             (gnus-run-hooks 'gnus-exit-group-hook)
7726                             (gnus-summary-update-info)
7727                             (gnus-list-of-read-articles gnus-newsgroup-name))
7728                         (setq gnus-newsgroup-expirable
7729                               (sort gnus-newsgroup-expirable '<))))
7730            (expiry-wait (if now 'immediate
7731                           (gnus-group-find-parameter
7732                            gnus-newsgroup-name 'expiry-wait)))
7733            es)
7734       (when expirable
7735         ;; There are expirable articles in this group, so we run them
7736         ;; through the expiry process.
7737         (gnus-message 6 "Expiring articles...")
7738         (unless (gnus-check-group gnus-newsgroup-name)
7739           (error "Can't open server for %s" gnus-newsgroup-name))
7740         ;; The list of articles that weren't expired is returned.
7741         (save-excursion
7742           (if expiry-wait
7743               (let ((nnmail-expiry-wait-function nil)
7744                     (nnmail-expiry-wait expiry-wait))
7745                 (setq es (gnus-request-expire-articles
7746                           expirable gnus-newsgroup-name)))
7747             (setq es (gnus-request-expire-articles
7748                       expirable gnus-newsgroup-name))))
7749         (unless total
7750           (setq gnus-newsgroup-expirable es))
7751         ;; We go through the old list of expirable, and mark all
7752         ;; really expired articles as nonexistent.
7753         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7754           (let ((gnus-use-cache nil))
7755             (while expirable
7756               (unless (memq (car expirable) es)
7757                 (when (gnus-data-find (car expirable))
7758                   (gnus-summary-mark-article
7759                    (car expirable) gnus-canceled-mark)))
7760               (setq expirable (cdr expirable)))))
7761         (gnus-message 6 "Expiring articles...done")))))
7762
7763 (defun gnus-summary-expire-articles-now ()
7764   "Expunge all expirable articles in the current group.
7765 This means that *all* articles that are marked as expirable will be
7766 deleted forever, right now."
7767   (interactive)
7768   (or gnus-expert-user
7769       (gnus-yes-or-no-p
7770        "Are you really, really, really sure you want to delete all these messages? ")
7771       (error "Phew!"))
7772   (gnus-summary-expire-articles t))
7773
7774 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7775 (defun gnus-summary-delete-article (&optional n)
7776   "Delete the N next (mail) articles.
7777 This command actually deletes articles.  This is not a marking
7778 command.  The article will disappear forever from your life, never to
7779 return.
7780 If N is negative, delete backwards.
7781 If N is nil and articles have been marked with the process mark,
7782 delete these instead."
7783   (interactive "P")
7784   (unless (gnus-check-backend-function 'request-expire-articles
7785                                        gnus-newsgroup-name)
7786     (error "The current newsgroup does not support article deletion"))
7787   ;; Compute the list of articles to delete.
7788   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7789         not-deleted)
7790     (if (and gnus-novice-user
7791              (not (gnus-yes-or-no-p
7792                    (format "Do you really want to delete %s forever? "
7793                            (if (> (length articles) 1)
7794                                (format "these %s articles" (length articles))
7795                              "this article")))))
7796         ()
7797       ;; Delete the articles.
7798       (setq not-deleted (gnus-request-expire-articles
7799                          articles gnus-newsgroup-name 'force))
7800       (while articles
7801         (gnus-summary-remove-process-mark (car articles))
7802         ;; The backend might not have been able to delete the article
7803         ;; after all.
7804         (unless (memq (car articles) not-deleted)
7805           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7806         (setq articles (cdr articles)))
7807       (when not-deleted
7808         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7809     (gnus-summary-position-point)
7810     (gnus-set-mode-line 'summary)
7811     not-deleted))
7812
7813 (defun gnus-summary-edit-article (&optional force)
7814   "Edit the current article.
7815 This will have permanent effect only in mail groups.
7816 If FORCE is non-nil, allow editing of articles even in read-only
7817 groups."
7818   (interactive "P")
7819   (save-excursion
7820     (set-buffer gnus-summary-buffer)
7821     (let ((mail-parse-charset gnus-newsgroup-charset)
7822           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7823       (gnus-set-global-variables)
7824       (when (and (not force)
7825                  (gnus-group-read-only-p))
7826         (error "The current newsgroup does not support article editing"))
7827       (gnus-summary-show-article t)
7828       (gnus-article-edit-article
7829        'ignore
7830        `(lambda (no-highlight)
7831           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7832                 (mail-parse-ignored-charsets
7833                  ',gnus-newsgroup-ignored-charsets))
7834             (gnus-summary-edit-article-done
7835              ,(or (mail-header-references gnus-current-headers) "")
7836              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7837
7838 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7839
7840 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7841                                                  no-highlight)
7842   "Make edits to the current article permanent."
7843   (interactive)
7844   ;; Replace the article.
7845   (let ((buf (current-buffer)))
7846     (with-temp-buffer
7847       (insert-buffer-substring buf)
7848       (if (and (not read-only)
7849                (not (gnus-request-replace-article
7850                      (cdr gnus-article-current) (car gnus-article-current)
7851                      (current-buffer) t)))
7852           (error "Couldn't replace article")
7853         ;; Update the summary buffer.
7854         (if (and references
7855                  (equal (message-tokenize-header references " ")
7856                         (message-tokenize-header
7857                          (or (message-fetch-field "references") "") " ")))
7858             ;; We only have to update this line.
7859             (save-excursion
7860               (save-restriction
7861                 (message-narrow-to-head)
7862                 (let ((head (buffer-string))
7863                       header)
7864                   (with-temp-buffer
7865                     (insert (format "211 %d Article retrieved.\n"
7866                                     (cdr gnus-article-current)))
7867                     (insert head)
7868                     (insert ".\n")
7869                     (let ((nntp-server-buffer (current-buffer)))
7870                       (setq header (car (gnus-get-newsgroup-headers
7871                                          (save-excursion
7872                                            (set-buffer gnus-summary-buffer)
7873                                            gnus-newsgroup-dependencies)
7874                                          t))))
7875                     (save-excursion
7876                       (set-buffer gnus-summary-buffer)
7877                       (gnus-data-set-header
7878                        (gnus-data-find (cdr gnus-article-current))
7879                        header)
7880                       (gnus-summary-update-article-line
7881                        (cdr gnus-article-current) header))))))
7882           ;; Update threads.
7883           (set-buffer (or buffer gnus-summary-buffer))
7884           (gnus-summary-update-article (cdr gnus-article-current)))
7885         ;; Prettify the article buffer again.
7886         (unless no-highlight
7887           (save-excursion
7888             (set-buffer gnus-article-buffer)
7889             ;;;!!! Fix this -- article should be rehighlighted.
7890             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7891             (set-buffer gnus-original-article-buffer)
7892             (gnus-request-article
7893              (cdr gnus-article-current)
7894              (car gnus-article-current) (current-buffer))))
7895         ;; Prettify the summary buffer line.
7896         (when (gnus-visual-p 'summary-highlight 'highlight)
7897           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7898
7899 (defun gnus-summary-edit-wash (key)
7900   "Perform editing command KEY in the article buffer."
7901   (interactive
7902    (list
7903     (progn
7904       (message "%s" (concat (this-command-keys) "- "))
7905       (read-char))))
7906   (message "")
7907   (gnus-summary-edit-article)
7908   (execute-kbd-macro (concat (this-command-keys) key))
7909   (gnus-article-edit-done))
7910
7911 ;;; Respooling
7912
7913 (defun gnus-summary-respool-query (&optional silent trace)
7914   "Query where the respool algorithm would put this article."
7915   (interactive)
7916   (let (gnus-mark-article-hook)
7917     (gnus-summary-select-article)
7918     (save-excursion
7919       (set-buffer gnus-original-article-buffer)
7920       (save-restriction
7921         (message-narrow-to-head)
7922         (let ((groups (nnmail-article-group 'identity trace)))
7923           (unless silent
7924             (if groups
7925                 (message "This message would go to %s"
7926                          (mapconcat 'car groups ", "))
7927               (message "This message would go to no groups"))
7928             groups))))))
7929
7930 (defun gnus-summary-respool-trace ()
7931   "Trace where the respool algorithm would put this article.
7932 Display a buffer showing all fancy splitting patterns which matched."
7933   (interactive)
7934   (gnus-summary-respool-query nil t))
7935
7936 ;; Summary marking commands.
7937
7938 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7939   "Mark articles which has the same subject as read, and then select the next.
7940 If UNMARK is positive, remove any kind of mark.
7941 If UNMARK is negative, tick articles."
7942   (interactive "P")
7943   (when unmark
7944     (setq unmark (prefix-numeric-value unmark)))
7945   (let ((count
7946          (gnus-summary-mark-same-subject
7947           (gnus-summary-article-subject) unmark)))
7948     ;; Select next unread article.  If auto-select-same mode, should
7949     ;; select the first unread article.
7950     (gnus-summary-next-article t (and gnus-auto-select-same
7951                                       (gnus-summary-article-subject)))
7952     (gnus-message 7 "%d article%s marked as %s"
7953                   count (if (= count 1) " is" "s are")
7954                   (if unmark "unread" "read"))))
7955
7956 (defun gnus-summary-kill-same-subject (&optional unmark)
7957   "Mark articles which has the same subject as read.
7958 If UNMARK is positive, remove any kind of mark.
7959 If UNMARK is negative, tick articles."
7960   (interactive "P")
7961   (when unmark
7962     (setq unmark (prefix-numeric-value unmark)))
7963   (let ((count
7964          (gnus-summary-mark-same-subject
7965           (gnus-summary-article-subject) unmark)))
7966     ;; If marked as read, go to next unread subject.
7967     (when (null unmark)
7968       ;; Go to next unread subject.
7969       (gnus-summary-next-subject 1 t))
7970     (gnus-message 7 "%d articles are marked as %s"
7971                   count (if unmark "unread" "read"))))
7972
7973 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7974   "Mark articles with same SUBJECT as read, and return marked number.
7975 If optional argument UNMARK is positive, remove any kinds of marks.
7976 If optional argument UNMARK is negative, mark articles as unread instead."
7977   (let ((count 1))
7978     (save-excursion
7979       (cond
7980        ((null unmark)                   ; Mark as read.
7981         (while (and
7982                 (progn
7983                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7984                   (gnus-summary-show-thread) t)
7985                 (gnus-summary-find-subject subject))
7986           (setq count (1+ count))))
7987        ((> unmark 0)                    ; Tick.
7988         (while (and
7989                 (progn
7990                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7991                   (gnus-summary-show-thread) t)
7992                 (gnus-summary-find-subject subject))
7993           (setq count (1+ count))))
7994        (t                               ; Mark as unread.
7995         (while (and
7996                 (progn
7997                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7998                   (gnus-summary-show-thread) t)
7999                 (gnus-summary-find-subject subject))
8000           (setq count (1+ count)))))
8001       (gnus-set-mode-line 'summary)
8002       ;; Return the number of marked articles.
8003       count)))
8004
8005 (defun gnus-summary-mark-as-processable (n &optional unmark)
8006   "Set the process mark on the next N articles.
8007 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8008 the process mark instead.  The difference between N and the actual
8009 number of articles marked is returned."
8010   (interactive "p")
8011   (let ((backward (< n 0))
8012         (n (abs n)))
8013     (while (and
8014             (> n 0)
8015             (if unmark
8016                 (gnus-summary-remove-process-mark
8017                  (gnus-summary-article-number))
8018               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8019             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8020       (setq n (1- n)))
8021     (when (/= 0 n)
8022       (gnus-message 7 "No more articles"))
8023     (gnus-summary-recenter)
8024     (gnus-summary-position-point)
8025     n))
8026
8027 (defun gnus-summary-unmark-as-processable (n)
8028   "Remove the process mark from the next N articles.
8029 If N is negative, unmark backward instead.  The difference between N and
8030 the actual number of articles unmarked is returned."
8031   (interactive "p")
8032   (gnus-summary-mark-as-processable n t))
8033
8034 (defun gnus-summary-unmark-all-processable ()
8035   "Remove the process mark from all articles."
8036   (interactive)
8037   (save-excursion
8038     (while gnus-newsgroup-processable
8039       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8040   (gnus-summary-position-point))
8041
8042 (defun gnus-summary-mark-as-expirable (n)
8043   "Mark N articles forward as expirable.
8044 If N is negative, mark backward instead.  The difference between N and
8045 the actual number of articles marked is returned."
8046   (interactive "p")
8047   (gnus-summary-mark-forward n gnus-expirable-mark))
8048
8049 (defun gnus-summary-mark-article-as-replied (article)
8050   "Mark ARTICLE replied and update the summary line."
8051   (push article gnus-newsgroup-replied)
8052   (let ((buffer-read-only nil))
8053     (when (gnus-summary-goto-subject article nil t)
8054       (gnus-summary-update-secondary-mark article))))
8055
8056 (defun gnus-summary-set-bookmark (article)
8057   "Set a bookmark in current article."
8058   (interactive (list (gnus-summary-article-number)))
8059   (when (or (not (get-buffer gnus-article-buffer))
8060             (not gnus-current-article)
8061             (not gnus-article-current)
8062             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8063     (error "No current article selected"))
8064   ;; Remove old bookmark, if one exists.
8065   (let ((old (assq article gnus-newsgroup-bookmarks)))
8066     (when old
8067       (setq gnus-newsgroup-bookmarks
8068             (delq old gnus-newsgroup-bookmarks))))
8069   ;; Set the new bookmark, which is on the form
8070   ;; (article-number . line-number-in-body).
8071   (push
8072    (cons article
8073          (save-excursion
8074            (set-buffer gnus-article-buffer)
8075            (count-lines
8076             (min (point)
8077                  (save-excursion
8078                    (goto-char (point-min))
8079                    (search-forward "\n\n" nil t)
8080                    (point)))
8081             (point))))
8082    gnus-newsgroup-bookmarks)
8083   (gnus-message 6 "A bookmark has been added to the current article."))
8084
8085 (defun gnus-summary-remove-bookmark (article)
8086   "Remove the bookmark from the current article."
8087   (interactive (list (gnus-summary-article-number)))
8088   ;; Remove old bookmark, if one exists.
8089   (let ((old (assq article gnus-newsgroup-bookmarks)))
8090     (if old
8091         (progn
8092           (setq gnus-newsgroup-bookmarks
8093                 (delq old gnus-newsgroup-bookmarks))
8094           (gnus-message 6 "Removed bookmark."))
8095       (gnus-message 6 "No bookmark in current article."))))
8096
8097 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8098 (defun gnus-summary-mark-as-dormant (n)
8099   "Mark N articles forward as dormant.
8100 If N is negative, mark backward instead.  The difference between N and
8101 the actual number of articles marked is returned."
8102   (interactive "p")
8103   (gnus-summary-mark-forward n gnus-dormant-mark))
8104
8105 (defun gnus-summary-set-process-mark (article)
8106   "Set the process mark on ARTICLE and update the summary line."
8107   (setq gnus-newsgroup-processable
8108         (cons article
8109               (delq article gnus-newsgroup-processable)))
8110   (when (gnus-summary-goto-subject article)
8111     (gnus-summary-show-thread)
8112     (gnus-summary-goto-subject article)
8113     (gnus-summary-update-secondary-mark article)))
8114
8115 (defun gnus-summary-remove-process-mark (article)
8116   "Remove the process mark from ARTICLE and update the summary line."
8117   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8118   (when (gnus-summary-goto-subject article)
8119     (gnus-summary-show-thread)
8120     (gnus-summary-goto-subject article)
8121     (gnus-summary-update-secondary-mark article)))
8122
8123 (defun gnus-summary-set-saved-mark (article)
8124   "Set the process mark on ARTICLE and update the summary line."
8125   (push article gnus-newsgroup-saved)
8126   (when (gnus-summary-goto-subject article)
8127     (gnus-summary-update-secondary-mark article)))
8128
8129 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8130   "Mark N articles as read forwards.
8131 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8132 The difference between N and the actual number of articles marked is
8133 returned."
8134   (interactive "p")
8135   (gnus-summary-show-thread)
8136   (let ((backward (< n 0))
8137         (gnus-summary-goto-unread
8138          (and gnus-summary-goto-unread
8139               (not (eq gnus-summary-goto-unread 'never))
8140               (not (memq mark (list gnus-unread-mark
8141                                     gnus-ticked-mark gnus-dormant-mark)))))
8142         (n (abs n))
8143         (mark (or mark gnus-del-mark)))
8144     (while (and (> n 0)
8145                 (gnus-summary-mark-article nil mark no-expire)
8146                 (zerop (gnus-summary-next-subject
8147                         (if backward -1 1)
8148                         (and gnus-summary-goto-unread
8149                              (not (eq gnus-summary-goto-unread 'never)))
8150                         t)))
8151       (setq n (1- n)))
8152     (when (/= 0 n)
8153       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8154     (gnus-summary-recenter)
8155     (gnus-summary-position-point)
8156     (gnus-set-mode-line 'summary)
8157     n))
8158
8159 (defun gnus-summary-mark-article-as-read (mark)
8160   "Mark the current article quickly as read with MARK."
8161   (let ((article (gnus-summary-article-number)))
8162     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8163     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8164     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8165     (push (cons article mark) gnus-newsgroup-reads)
8166     ;; Possibly remove from cache, if that is used.
8167     (when gnus-use-cache
8168       (gnus-cache-enter-remove-article article))
8169     ;; Allow the backend to change the mark.
8170     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8171     ;; Check for auto-expiry.
8172     (when (and gnus-newsgroup-auto-expire
8173                (memq mark gnus-auto-expirable-marks))
8174       (setq mark gnus-expirable-mark)
8175       ;; Let the backend know about the mark change.
8176       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8177       (push article gnus-newsgroup-expirable))
8178     ;; Set the mark in the buffer.
8179     (gnus-summary-update-mark mark 'unread)
8180     t))
8181
8182 (defun gnus-summary-mark-article-as-unread (mark)
8183   "Mark the current article quickly as unread with MARK."
8184   (let* ((article (gnus-summary-article-number))
8185          (old-mark (gnus-summary-article-mark article)))
8186     ;; Allow the backend to change the mark.
8187     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8188     (if (eq mark old-mark)
8189         t
8190       (if (<= article 0)
8191           (progn
8192             (gnus-error 1 "Can't mark negative article numbers")
8193             nil)
8194         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8195         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8196         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8197         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8198         (cond ((= mark gnus-ticked-mark)
8199                (push article gnus-newsgroup-marked))
8200               ((= mark gnus-dormant-mark)
8201                (push article gnus-newsgroup-dormant))
8202               (t
8203                (push article gnus-newsgroup-unreads)))
8204         (gnus-pull article gnus-newsgroup-reads)
8205
8206         ;; See whether the article is to be put in the cache.
8207         (and gnus-use-cache
8208              (vectorp (gnus-summary-article-header article))
8209              (save-excursion
8210                (gnus-cache-possibly-enter-article
8211                 gnus-newsgroup-name article
8212                 (gnus-summary-article-header article)
8213                 (= mark gnus-ticked-mark)
8214                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8215
8216         ;; Fix the mark.
8217         (gnus-summary-update-mark mark 'unread)
8218         t))))
8219
8220 (defun gnus-summary-mark-article (&optional article mark no-expire)
8221   "Mark ARTICLE with MARK.  MARK can be any character.
8222 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8223 `??' (dormant) and `?E' (expirable).
8224 If MARK is nil, then the default character `?r' is used.
8225 If ARTICLE is nil, then the article on the current line will be
8226 marked."
8227   ;; The mark might be a string.
8228   (when (stringp mark)
8229     (setq mark (aref mark 0)))
8230   ;; If no mark is given, then we check auto-expiring.
8231   (when (null mark)
8232     (setq mark gnus-del-mark))
8233   (when (and (not no-expire)
8234              gnus-newsgroup-auto-expire
8235              (memq mark gnus-auto-expirable-marks))
8236     (setq mark gnus-expirable-mark))
8237   (let ((article (or article (gnus-summary-article-number)))
8238         (old-mark (gnus-summary-article-mark article)))
8239     ;; Allow the backend to change the mark.
8240     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8241     (if (eq mark old-mark)
8242         t
8243       (unless article
8244         (error "No article on current line"))
8245       (if (not (if (or (= mark gnus-unread-mark)
8246                        (= mark gnus-ticked-mark)
8247                        (= mark gnus-dormant-mark))
8248                    (gnus-mark-article-as-unread article mark)
8249                  (gnus-mark-article-as-read article mark)))
8250           t
8251         ;; See whether the article is to be put in the cache.
8252         (and gnus-use-cache
8253              (not (= mark gnus-canceled-mark))
8254              (vectorp (gnus-summary-article-header article))
8255              (save-excursion
8256                (gnus-cache-possibly-enter-article
8257                 gnus-newsgroup-name article
8258                 (gnus-summary-article-header article)
8259                 (= mark gnus-ticked-mark)
8260                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8261
8262         (when (gnus-summary-goto-subject article nil t)
8263           (let ((buffer-read-only nil))
8264             (gnus-summary-show-thread)
8265             ;; Fix the mark.
8266             (gnus-summary-update-mark mark 'unread)
8267             t))))))
8268
8269 (defun gnus-summary-update-secondary-mark (article)
8270   "Update the secondary (read, process, cache) mark."
8271   (gnus-summary-update-mark
8272    (cond ((memq article gnus-newsgroup-processable)
8273           gnus-process-mark)
8274          ((memq article gnus-newsgroup-cached)
8275           gnus-cached-mark)
8276          ((memq article gnus-newsgroup-replied)
8277           gnus-replied-mark)
8278          ((memq article gnus-newsgroup-saved)
8279           gnus-saved-mark)
8280          (t gnus-unread-mark))
8281    'replied)
8282   (when (gnus-visual-p 'summary-highlight 'highlight)
8283     (gnus-run-hooks 'gnus-summary-update-hook))
8284   t)
8285
8286 (defun gnus-summary-update-mark (mark type)
8287   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8288         (buffer-read-only nil))
8289     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8290     (when forward
8291       (when (looking-at "\r")
8292         (incf forward))
8293       (when (<= (+ forward (point)) (point-max))
8294         ;; Go to the right position on the line.
8295         (goto-char (+ forward (point)))
8296         ;; Replace the old mark with the new mark.
8297         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8298         ;; Optionally update the marks by some user rule.
8299         (when (eq type 'unread)
8300           (gnus-data-set-mark
8301            (gnus-data-find (gnus-summary-article-number)) mark)
8302           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8303
8304 (defun gnus-mark-article-as-read (article &optional mark)
8305   "Enter ARTICLE in the pertinent lists and remove it from others."
8306   ;; Make the article expirable.
8307   (let ((mark (or mark gnus-del-mark)))
8308     (if (= mark gnus-expirable-mark)
8309         (push article gnus-newsgroup-expirable)
8310       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8311     ;; Remove from unread and marked lists.
8312     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8313     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8314     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8315     (push (cons article mark) gnus-newsgroup-reads)
8316     ;; Possibly remove from cache, if that is used.
8317     (when gnus-use-cache
8318       (gnus-cache-enter-remove-article article))
8319     t))
8320
8321 (defun gnus-mark-article-as-unread (article &optional mark)
8322   "Enter ARTICLE in the pertinent lists and remove it from others."
8323   (let ((mark (or mark gnus-ticked-mark)))
8324     (if (<= article 0)
8325         (progn
8326           (gnus-error 1 "Can't mark negative article numbers")
8327           nil)
8328       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8329             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8330             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8331             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8332
8333       ;; Unsuppress duplicates?
8334       (when gnus-suppress-duplicates
8335         (gnus-dup-unsuppress-article article))
8336
8337       (cond ((= mark gnus-ticked-mark)
8338              (push article gnus-newsgroup-marked))
8339             ((= mark gnus-dormant-mark)
8340              (push article gnus-newsgroup-dormant))
8341             (t
8342              (push article gnus-newsgroup-unreads)))
8343       (gnus-pull article gnus-newsgroup-reads)
8344       t)))
8345
8346 (defalias 'gnus-summary-mark-as-unread-forward
8347   'gnus-summary-tick-article-forward)
8348 (make-obsolete 'gnus-summary-mark-as-unread-forward
8349                'gnus-summary-tick-article-forward)
8350 (defun gnus-summary-tick-article-forward (n)
8351   "Tick N articles forwards.
8352 If N is negative, tick backwards instead.
8353 The difference between N and the number of articles ticked is returned."
8354   (interactive "p")
8355   (gnus-summary-mark-forward n gnus-ticked-mark))
8356
8357 (defalias 'gnus-summary-mark-as-unread-backward
8358   'gnus-summary-tick-article-backward)
8359 (make-obsolete 'gnus-summary-mark-as-unread-backward
8360                'gnus-summary-tick-article-backward)
8361 (defun gnus-summary-tick-article-backward (n)
8362   "Tick N articles backwards.
8363 The difference between N and the number of articles ticked is returned."
8364   (interactive "p")
8365   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8366
8367 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8368 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8369 (defun gnus-summary-tick-article (&optional article clear-mark)
8370   "Mark current article as unread.
8371 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8372 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8373   (interactive)
8374   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8375                                        gnus-ticked-mark)))
8376
8377 (defun gnus-summary-mark-as-read-forward (n)
8378   "Mark N articles as read forwards.
8379 If N is negative, mark backwards instead.
8380 The difference between N and the actual number of articles marked is
8381 returned."
8382   (interactive "p")
8383   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8384
8385 (defun gnus-summary-mark-as-read-backward (n)
8386   "Mark the N articles as read backwards.
8387 The difference between N and the actual number of articles marked is
8388 returned."
8389   (interactive "p")
8390   (gnus-summary-mark-forward
8391    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8392
8393 (defun gnus-summary-mark-as-read (&optional article mark)
8394   "Mark current article as read.
8395 ARTICLE specifies the article to be marked as read.
8396 MARK specifies a string to be inserted at the beginning of the line."
8397   (gnus-summary-mark-article article mark))
8398
8399 (defun gnus-summary-clear-mark-forward (n)
8400   "Clear marks from N articles forward.
8401 If N is negative, clear backward instead.
8402 The difference between N and the number of marks cleared is returned."
8403   (interactive "p")
8404   (gnus-summary-mark-forward n gnus-unread-mark))
8405
8406 (defun gnus-summary-clear-mark-backward (n)
8407   "Clear marks from N articles backward.
8408 The difference between N and the number of marks cleared is returned."
8409   (interactive "p")
8410   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8411
8412 (defun gnus-summary-mark-unread-as-read ()
8413   "Intended to be used by `gnus-summary-mark-article-hook'."
8414   (when (memq gnus-current-article gnus-newsgroup-unreads)
8415     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8416
8417 (defun gnus-summary-mark-read-and-unread-as-read ()
8418   "Intended to be used by `gnus-summary-mark-article-hook'."
8419   (let ((mark (gnus-summary-article-mark)))
8420     (when (or (gnus-unread-mark-p mark)
8421               (gnus-read-mark-p mark))
8422       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8423
8424 (defun gnus-summary-mark-region-as-read (point mark all)
8425   "Mark all unread articles between point and mark as read.
8426 If given a prefix, mark all articles between point and mark as read,
8427 even ticked and dormant ones."
8428   (interactive "r\nP")
8429   (save-excursion
8430     (let (article)
8431       (goto-char point)
8432       (beginning-of-line)
8433       (while (and
8434               (< (point) mark)
8435               (progn
8436                 (when (or all
8437                           (memq (setq article (gnus-summary-article-number))
8438                                 gnus-newsgroup-unreads))
8439                   (gnus-summary-mark-article article gnus-del-mark))
8440                 t)
8441               (gnus-summary-find-next))))))
8442
8443 (defun gnus-summary-mark-below (score mark)
8444   "Mark articles with score less than SCORE with MARK."
8445   (interactive "P\ncMark: ")
8446   (setq score (if score
8447                   (prefix-numeric-value score)
8448                 (or gnus-summary-default-score 0)))
8449   (save-excursion
8450     (set-buffer gnus-summary-buffer)
8451     (goto-char (point-min))
8452     (while
8453         (progn
8454           (and (< (gnus-summary-article-score) score)
8455                (gnus-summary-mark-article nil mark))
8456           (gnus-summary-find-next)))))
8457
8458 (defun gnus-summary-kill-below (&optional score)
8459   "Mark articles with score below SCORE as read."
8460   (interactive "P")
8461   (gnus-summary-mark-below score gnus-killed-mark))
8462
8463 (defun gnus-summary-clear-above (&optional score)
8464   "Clear all marks from articles with score above SCORE."
8465   (interactive "P")
8466   (gnus-summary-mark-above score gnus-unread-mark))
8467
8468 (defun gnus-summary-tick-above (&optional score)
8469   "Tick all articles with score above SCORE."
8470   (interactive "P")
8471   (gnus-summary-mark-above score gnus-ticked-mark))
8472
8473 (defun gnus-summary-mark-above (score mark)
8474   "Mark articles with score over SCORE with MARK."
8475   (interactive "P\ncMark: ")
8476   (setq score (if score
8477                   (prefix-numeric-value score)
8478                 (or gnus-summary-default-score 0)))
8479   (save-excursion
8480     (set-buffer gnus-summary-buffer)
8481     (goto-char (point-min))
8482     (while (and (progn
8483                   (when (> (gnus-summary-article-score) score)
8484                     (gnus-summary-mark-article nil mark))
8485                   t)
8486                 (gnus-summary-find-next)))))
8487
8488 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8489 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8490 (defun gnus-summary-limit-include-expunged (&optional no-error)
8491   "Display all the hidden articles that were expunged for low scores."
8492   (interactive)
8493   (let ((buffer-read-only nil))
8494     (let ((scored gnus-newsgroup-scored)
8495           headers h)
8496       (while scored
8497         (unless (gnus-summary-goto-subject (caar scored))
8498           (and (setq h (gnus-summary-article-header (caar scored)))
8499                (< (cdar scored) gnus-summary-expunge-below)
8500                (push h headers)))
8501         (setq scored (cdr scored)))
8502       (if (not headers)
8503           (when (not no-error)
8504             (error "No expunged articles hidden"))
8505         (goto-char (point-min))
8506         (gnus-summary-prepare-unthreaded (nreverse headers))
8507         (goto-char (point-min))
8508         (gnus-summary-position-point)
8509         t))))
8510
8511 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8512   "Mark all unread articles in this newsgroup as read.
8513 If prefix argument ALL is non-nil, ticked and dormant articles will
8514 also be marked as read.
8515 If QUIETLY is non-nil, no questions will be asked.
8516 If TO-HERE is non-nil, it should be a point in the buffer.  All
8517 articles before this point will be marked as read.
8518 Note that this function will only catch up the unread article
8519 in the current summary buffer limitation.
8520 The number of articles marked as read is returned."
8521   (interactive "P")
8522   (prog1
8523       (save-excursion
8524         (when (or quietly
8525                   (not gnus-interactive-catchup) ;Without confirmation?
8526                   gnus-expert-user
8527                   (gnus-y-or-n-p
8528                    (if all
8529                        "Mark absolutely all articles as read? "
8530                      "Mark all unread articles as read? ")))
8531           (if (and not-mark
8532                    (not gnus-newsgroup-adaptive)
8533                    (not gnus-newsgroup-auto-expire)
8534                    (not gnus-suppress-duplicates)
8535                    (or (not gnus-use-cache)
8536                        (eq gnus-use-cache 'passive)))
8537               (progn
8538                 (when all
8539                   (setq gnus-newsgroup-marked nil
8540                         gnus-newsgroup-dormant nil))
8541                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8542             ;; We actually mark all articles as canceled, which we
8543             ;; have to do when using auto-expiry or adaptive scoring.
8544             (gnus-summary-show-all-threads)
8545             (when (gnus-summary-first-subject (not all) t)
8546               (while (and
8547                       (if to-here (< (point) to-here) t)
8548                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8549                       (gnus-summary-find-next (not all) nil nil t))))
8550             (gnus-set-mode-line 'summary))
8551           t))
8552     (gnus-summary-position-point)))
8553
8554 (defun gnus-summary-catchup-to-here (&optional all)
8555   "Mark all unticked articles before the current one as read.
8556 If ALL is non-nil, also mark ticked and dormant articles as read."
8557   (interactive "P")
8558   (save-excursion
8559     (gnus-save-hidden-threads
8560       (let ((beg (point)))
8561         ;; We check that there are unread articles.
8562         (when (or all (gnus-summary-find-prev))
8563           (gnus-summary-catchup all t beg)))))
8564   (gnus-summary-position-point))
8565
8566 (defun gnus-summary-catchup-all (&optional quietly)
8567   "Mark all articles in this newsgroup as read."
8568   (interactive "P")
8569   (gnus-summary-catchup t quietly))
8570
8571 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8572   "Mark all unread articles in this group as read, then exit.
8573 If prefix argument ALL is non-nil, all articles are marked as read."
8574   (interactive "P")
8575   (when (gnus-summary-catchup all quietly nil 'fast)
8576     ;; Select next newsgroup or exit.
8577     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8578              (eq gnus-auto-select-next 'quietly))
8579         (gnus-summary-next-group nil)
8580       (gnus-summary-exit))))
8581
8582 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8583   "Mark all articles in this newsgroup as read, and then exit."
8584   (interactive "P")
8585   (gnus-summary-catchup-and-exit t quietly))
8586
8587 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8588   "Mark all articles in this group as read and select the next group.
8589 If given a prefix, mark all articles, unread as well as ticked, as
8590 read."
8591   (interactive "P")
8592   (save-excursion
8593     (gnus-summary-catchup all))
8594   (gnus-summary-next-group))
8595
8596 ;; Thread-based commands.
8597
8598 (defun gnus-summary-articles-in-thread (&optional article)
8599   "Return a list of all articles in the current thread.
8600 If ARTICLE is non-nil, return all articles in the thread that starts
8601 with that article."
8602   (let* ((article (or article (gnus-summary-article-number)))
8603          (data (gnus-data-find-list article))
8604          (top-level (gnus-data-level (car data)))
8605          (top-subject
8606           (cond ((null gnus-thread-operation-ignore-subject)
8607                  (gnus-simplify-subject-re
8608                   (mail-header-subject (gnus-data-header (car data)))))
8609                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8610                  (gnus-simplify-subject-fuzzy
8611                   (mail-header-subject (gnus-data-header (car data)))))
8612                 (t nil)))
8613          (end-point (save-excursion
8614                       (if (gnus-summary-go-to-next-thread)
8615                           (point) (point-max))))
8616          articles)
8617     (while (and data
8618                 (< (gnus-data-pos (car data)) end-point))
8619       (when (or (not top-subject)
8620                 (string= top-subject
8621                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8622                              (gnus-simplify-subject-fuzzy
8623                               (mail-header-subject
8624                                (gnus-data-header (car data))))
8625                            (gnus-simplify-subject-re
8626                             (mail-header-subject
8627                              (gnus-data-header (car data)))))))
8628         (push (gnus-data-number (car data)) articles))
8629       (unless (and (setq data (cdr data))
8630                    (> (gnus-data-level (car data)) top-level))
8631         (setq data nil)))
8632     ;; Return the list of articles.
8633     (nreverse articles)))
8634
8635 (defun gnus-summary-rethread-current ()
8636   "Rethread the thread the current article is part of."
8637   (interactive)
8638   (let* ((gnus-show-threads t)
8639          (article (gnus-summary-article-number))
8640          (id (mail-header-id (gnus-summary-article-header)))
8641          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8642     (unless id
8643       (error "No article on the current line"))
8644     (gnus-rebuild-thread id)
8645     (gnus-summary-goto-subject article)))
8646
8647 (defun gnus-summary-reparent-thread ()
8648   "Make the current article child of the marked (or previous) article.
8649
8650 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8651 is non-nil or the Subject: of both articles are the same."
8652   (interactive)
8653   (unless (not (gnus-group-read-only-p))
8654     (error "The current newsgroup does not support article editing"))
8655   (unless (<= (length gnus-newsgroup-processable) 1)
8656     (error "No more than one article may be marked"))
8657   (save-window-excursion
8658     (let ((gnus-article-buffer " *reparent*")
8659           (current-article (gnus-summary-article-number))
8660           ;; First grab the marked article, otherwise one line up.
8661           (parent-article (if (not (null gnus-newsgroup-processable))
8662                               (car gnus-newsgroup-processable)
8663                             (save-excursion
8664                               (if (eq (forward-line -1) 0)
8665                                   (gnus-summary-article-number)
8666                                 (error "Beginning of summary buffer"))))))
8667       (unless (not (eq current-article parent-article))
8668         (error "An article may not be self-referential"))
8669       (let ((message-id (mail-header-id
8670                          (gnus-summary-article-header parent-article))))
8671         (unless (and message-id (not (equal message-id "")))
8672           (error "No message-id in desired parent"))
8673         (gnus-with-article current-article
8674           (goto-char (point-min))
8675           (if (re-search-forward "^References: " nil t)
8676               (progn
8677                 (re-search-forward "^[^ \t]" nil t)
8678                 (forward-line -1)
8679                 (end-of-line)
8680                 (insert " " message-id))
8681             (insert "References: " message-id "\n")))
8682         (set-buffer gnus-summary-buffer)
8683         (gnus-summary-unmark-all-processable)
8684         (gnus-summary-update-article current-article)
8685         (gnus-summary-rethread-current)
8686         (gnus-message 3 "Article %d is now the child of article %d"
8687                       current-article parent-article)))))
8688
8689 (defun gnus-summary-toggle-threads (&optional arg)
8690   "Toggle showing conversation threads.
8691 If ARG is positive number, turn showing conversation threads on."
8692   (interactive "P")
8693   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8694     (setq gnus-show-threads
8695           (if (null arg) (not gnus-show-threads)
8696             (> (prefix-numeric-value arg) 0)))
8697     (gnus-summary-prepare)
8698     (gnus-summary-goto-subject current)
8699     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8700     (gnus-summary-position-point)))
8701
8702 (defun gnus-summary-show-all-threads ()
8703   "Show all threads."
8704   (interactive)
8705   (save-excursion
8706     (let ((buffer-read-only nil))
8707       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8708   (gnus-summary-position-point))
8709
8710 (defun gnus-summary-show-thread ()
8711   "Show thread subtrees.
8712 Returns nil if no thread was there to be shown."
8713   (interactive)
8714   (let ((buffer-read-only nil)
8715         (orig (point))
8716         ;; first goto end then to beg, to have point at beg after let
8717         (end (progn (end-of-line) (point)))
8718         (beg (progn (beginning-of-line) (point))))
8719     (prog1
8720         ;; Any hidden lines here?
8721         (search-forward "\r" end t)
8722       (subst-char-in-region beg end ?\^M ?\n t)
8723       (goto-char orig)
8724       (gnus-summary-position-point))))
8725
8726 (defun gnus-summary-hide-all-threads ()
8727   "Hide all thread subtrees."
8728   (interactive)
8729   (save-excursion
8730     (goto-char (point-min))
8731     (gnus-summary-hide-thread)
8732     (while (zerop (gnus-summary-next-thread 1 t))
8733       (gnus-summary-hide-thread)))
8734   (gnus-summary-position-point))
8735
8736 (defun gnus-summary-hide-thread ()
8737   "Hide thread subtrees.
8738 Returns nil if no threads were there to be hidden."
8739   (interactive)
8740   (let ((buffer-read-only nil)
8741         (start (point))
8742         (article (gnus-summary-article-number)))
8743     (goto-char start)
8744     ;; Go forward until either the buffer ends or the subthread
8745     ;; ends.
8746     (when (and (not (eobp))
8747                (or (zerop (gnus-summary-next-thread 1 t))
8748                    (goto-char (point-max))))
8749       (prog1
8750           (if (and (> (point) start)
8751                    (search-backward "\n" start t))
8752               (progn
8753                 (subst-char-in-region start (point) ?\n ?\^M)
8754                 (gnus-summary-goto-subject article))
8755             (goto-char start)
8756             nil)
8757         ;;(gnus-summary-position-point)
8758         ))))
8759
8760 (defun gnus-summary-go-to-next-thread (&optional previous)
8761   "Go to the same level (or less) next thread.
8762 If PREVIOUS is non-nil, go to previous thread instead.
8763 Return the article number moved to, or nil if moving was impossible."
8764   (let ((level (gnus-summary-thread-level))
8765         (way (if previous -1 1))
8766         (beg (point)))
8767     (forward-line way)
8768     (while (and (not (eobp))
8769                 (< level (gnus-summary-thread-level)))
8770       (forward-line way))
8771     (if (eobp)
8772         (progn
8773           (goto-char beg)
8774           nil)
8775       (setq beg (point))
8776       (prog1
8777           (gnus-summary-article-number)
8778         (goto-char beg)))))
8779
8780 (defun gnus-summary-next-thread (n &optional silent)
8781   "Go to the same level next N'th thread.
8782 If N is negative, search backward instead.
8783 Returns the difference between N and the number of skips actually
8784 done.
8785
8786 If SILENT, don't output messages."
8787   (interactive "p")
8788   (let ((backward (< n 0))
8789         (n (abs n)))
8790     (while (and (> n 0)
8791                 (gnus-summary-go-to-next-thread backward))
8792       (decf n))
8793     (unless silent
8794       (gnus-summary-position-point))
8795     (when (and (not silent) (/= 0 n))
8796       (gnus-message 7 "No more threads"))
8797     n))
8798
8799 (defun gnus-summary-prev-thread (n)
8800   "Go to the same level previous N'th thread.
8801 Returns the difference between N and the number of skips actually
8802 done."
8803   (interactive "p")
8804   (gnus-summary-next-thread (- n)))
8805
8806 (defun gnus-summary-go-down-thread ()
8807   "Go down one level in the current thread."
8808   (let ((children (gnus-summary-article-children)))
8809     (when children
8810       (gnus-summary-goto-subject (car children)))))
8811
8812 (defun gnus-summary-go-up-thread ()
8813   "Go up one level in the current thread."
8814   (let ((parent (gnus-summary-article-parent)))
8815     (when parent
8816       (gnus-summary-goto-subject parent))))
8817
8818 (defun gnus-summary-down-thread (n)
8819   "Go down thread N steps.
8820 If N is negative, go up instead.
8821 Returns the difference between N and how many steps down that were
8822 taken."
8823   (interactive "p")
8824   (let ((up (< n 0))
8825         (n (abs n)))
8826     (while (and (> n 0)
8827                 (if up (gnus-summary-go-up-thread)
8828                   (gnus-summary-go-down-thread)))
8829       (setq n (1- n)))
8830     (gnus-summary-position-point)
8831     (when (/= 0 n)
8832       (gnus-message 7 "Can't go further"))
8833     n))
8834
8835 (defun gnus-summary-up-thread (n)
8836   "Go up thread N steps.
8837 If N is negative, go up instead.
8838 Returns the difference between N and how many steps down that were
8839 taken."
8840   (interactive "p")
8841   (gnus-summary-down-thread (- n)))
8842
8843 (defun gnus-summary-top-thread ()
8844   "Go to the top of the thread."
8845   (interactive)
8846   (while (gnus-summary-go-up-thread))
8847   (gnus-summary-article-number))
8848
8849 (defun gnus-summary-kill-thread (&optional unmark)
8850   "Mark articles under current thread as read.
8851 If the prefix argument is positive, remove any kinds of marks.
8852 If the prefix argument is negative, tick articles instead."
8853   (interactive "P")
8854   (when unmark
8855     (setq unmark (prefix-numeric-value unmark)))
8856   (let ((articles (gnus-summary-articles-in-thread)))
8857     (save-excursion
8858       ;; Expand the thread.
8859       (gnus-summary-show-thread)
8860       ;; Mark all the articles.
8861       (while articles
8862         (gnus-summary-goto-subject (car articles))
8863         (cond ((null unmark)
8864                (gnus-summary-mark-article-as-read gnus-killed-mark))
8865               ((> unmark 0)
8866                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8867               (t
8868                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8869         (setq articles (cdr articles))))
8870     ;; Hide killed subtrees.
8871     (and (null unmark)
8872          gnus-thread-hide-killed
8873          (gnus-summary-hide-thread))
8874     ;; If marked as read, go to next unread subject.
8875     (when (null unmark)
8876       ;; Go to next unread subject.
8877       (gnus-summary-next-subject 1 t)))
8878   (gnus-set-mode-line 'summary))
8879
8880 ;; Summary sorting commands
8881
8882 (defun gnus-summary-sort-by-number (&optional reverse)
8883   "Sort the summary buffer by article number.
8884 Argument REVERSE means reverse order."
8885   (interactive "P")
8886   (gnus-summary-sort 'number reverse))
8887
8888 (defun gnus-summary-sort-by-author (&optional reverse)
8889   "Sort the summary buffer by author name alphabetically.
8890 If case-fold-search is non-nil, case of letters is ignored.
8891 Argument REVERSE means reverse order."
8892   (interactive "P")
8893   (gnus-summary-sort 'author reverse))
8894
8895 (defun gnus-summary-sort-by-subject (&optional reverse)
8896   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8897 If case-fold-search is non-nil, case of letters is ignored.
8898 Argument REVERSE means reverse order."
8899   (interactive "P")
8900   (gnus-summary-sort 'subject reverse))
8901
8902 (defun gnus-summary-sort-by-date (&optional reverse)
8903   "Sort the summary buffer by date.
8904 Argument REVERSE means reverse order."
8905   (interactive "P")
8906   (gnus-summary-sort 'date reverse))
8907
8908 (defun gnus-summary-sort-by-score (&optional reverse)
8909   "Sort the summary buffer by score.
8910 Argument REVERSE means reverse order."
8911   (interactive "P")
8912   (gnus-summary-sort 'score reverse))
8913
8914 (defun gnus-summary-sort-by-lines (&optional reverse)
8915   "Sort the summary buffer by the number of lines.
8916 Argument REVERSE means reverse order."
8917   (interactive "P")
8918   (gnus-summary-sort 'lines reverse))
8919
8920 (defun gnus-summary-sort-by-chars (&optional reverse)
8921   "Sort the summary buffer by article length.
8922 Argument REVERSE means reverse order."
8923   (interactive "P")
8924   (gnus-summary-sort 'chars reverse))   
8925
8926 (defun gnus-summary-sort (predicate reverse)
8927   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8928   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8929          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8930          (gnus-thread-sort-functions
8931           (if (not reverse)
8932               thread
8933             `(lambda (t1 t2)
8934                (,thread t2 t1))))
8935          (gnus-article-sort-functions
8936           (if (not reverse)
8937               article
8938             `(lambda (t1 t2)
8939                (,article t2 t1))))
8940          (buffer-read-only)
8941          (gnus-summary-prepare-hook nil))
8942     ;; We do the sorting by regenerating the threads.
8943     (gnus-summary-prepare)
8944     ;; Hide subthreads if needed.
8945     (when (and gnus-show-threads gnus-thread-hide-subtree)
8946       (gnus-summary-hide-all-threads))))
8947
8948 ;; Summary saving commands.
8949
8950 (defun gnus-summary-save-article (&optional n not-saved)
8951   "Save the current article using the default saver function.
8952 If N is a positive number, save the N next articles.
8953 If N is a negative number, save the N previous articles.
8954 If N is nil and any articles have been marked with the process mark,
8955 save those articles instead.
8956 The variable `gnus-default-article-saver' specifies the saver function."
8957   (interactive "P")
8958   (let* ((articles (gnus-summary-work-articles n))
8959          (save-buffer (save-excursion
8960                         (nnheader-set-temp-buffer " *Gnus Save*")))
8961          (num (length articles))
8962          header file)
8963     (dolist (article articles)
8964       (setq header (gnus-summary-article-header article))
8965       (if (not (vectorp header))
8966           ;; This is a pseudo-article.
8967           (if (assq 'name header)
8968               (gnus-copy-file (cdr (assq 'name header)))
8969             (gnus-message 1 "Article %d is unsaveable" article))
8970         ;; This is a real article.
8971         (save-window-excursion
8972           (gnus-summary-select-article t nil nil article))
8973         (save-excursion
8974           (set-buffer save-buffer)
8975           (erase-buffer)
8976           (insert-buffer-substring gnus-original-article-buffer))
8977         (setq file (gnus-article-save save-buffer file num))
8978         (gnus-summary-remove-process-mark article)
8979         (unless not-saved
8980           (gnus-summary-set-saved-mark article))))
8981     (gnus-kill-buffer save-buffer)
8982     (gnus-summary-position-point)
8983     (gnus-set-mode-line 'summary)
8984     n))
8985
8986 (defun gnus-summary-pipe-output (&optional arg)
8987   "Pipe the current article to a subprocess.
8988 If N is a positive number, pipe the N next articles.
8989 If N is a negative number, pipe the N previous articles.
8990 If N is nil and any articles have been marked with the process mark,
8991 pipe those articles instead."
8992   (interactive "P")
8993   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8994     (gnus-summary-save-article arg t))
8995   (gnus-configure-windows 'pipe))
8996
8997 (defun gnus-summary-save-article-mail (&optional arg)
8998   "Append the current article to an mail file.
8999 If N is a positive number, save the N next articles.
9000 If N is a negative number, save the N previous articles.
9001 If N is nil and any articles have been marked with the process mark,
9002 save those articles instead."
9003   (interactive "P")
9004   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9005     (gnus-summary-save-article arg)))
9006
9007 (defun gnus-summary-save-article-rmail (&optional arg)
9008   "Append the current article to an rmail file.
9009 If N is a positive number, save the N next articles.
9010 If N is a negative number, save the N previous articles.
9011 If N is nil and any articles have been marked with the process mark,
9012 save those articles instead."
9013   (interactive "P")
9014   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9015     (gnus-summary-save-article arg)))
9016
9017 (defun gnus-summary-save-article-file (&optional arg)
9018   "Append the current article to a file.
9019 If N is a positive number, save the N next articles.
9020 If N is a negative number, save the N previous articles.
9021 If N is nil and any articles have been marked with the process mark,
9022 save those articles instead."
9023   (interactive "P")
9024   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9025     (gnus-summary-save-article arg)))
9026
9027 (defun gnus-summary-write-article-file (&optional arg)
9028   "Write the current article to a file, deleting the previous file.
9029 If N is a positive number, save the N next articles.
9030 If N is a negative number, save the N previous articles.
9031 If N is nil and any articles have been marked with the process mark,
9032 save those articles instead."
9033   (interactive "P")
9034   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9035     (gnus-summary-save-article arg)))
9036
9037 (defun gnus-summary-save-article-body-file (&optional arg)
9038   "Append the current article body to a file.
9039 If N is a positive number, save the N next articles.
9040 If N is a negative number, save the N previous articles.
9041 If N is nil and any articles have been marked with the process mark,
9042 save those articles instead."
9043   (interactive "P")
9044   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9045     (gnus-summary-save-article arg)))
9046
9047 (defun gnus-summary-pipe-message (program)
9048   "Pipe the current article through PROGRAM."
9049   (interactive "sProgram: ")
9050   (gnus-summary-select-article)
9051   (let ((mail-header-separator ""))
9052     (gnus-eval-in-buffer-window gnus-article-buffer
9053       (save-restriction
9054         (widen)
9055         (let ((start (window-start))
9056               buffer-read-only)
9057           (message-pipe-buffer-body program)
9058           (set-window-start (get-buffer-window (current-buffer)) start))))))
9059
9060 (defun gnus-get-split-value (methods)
9061   "Return a value based on the split METHODS."
9062   (let (split-name method result match)
9063     (when methods
9064       (save-excursion
9065         (set-buffer gnus-original-article-buffer)
9066         (save-restriction
9067           (nnheader-narrow-to-headers)
9068           (while methods
9069             (goto-char (point-min))
9070             (setq method (pop methods))
9071             (setq match (car method))
9072             (when (cond
9073                    ((stringp match)
9074                     ;; Regular expression.
9075                     (ignore-errors
9076                       (re-search-forward match nil t)))
9077                    ((gnus-functionp match)
9078                     ;; Function.
9079                     (save-restriction
9080                       (widen)
9081                       (setq result (funcall match gnus-newsgroup-name))))
9082                    ((consp match)
9083                     ;; Form.
9084                     (save-restriction
9085                       (widen)
9086                       (setq result (eval match)))))
9087               (setq split-name (append (cdr method) split-name))
9088               (cond ((stringp result)
9089                      (push (expand-file-name
9090                             result gnus-article-save-directory)
9091                            split-name))
9092                     ((consp result)
9093                      (setq split-name (append result split-name)))))))))
9094     (nreverse split-name)))
9095
9096 (defun gnus-valid-move-group-p (group)
9097   (and (boundp group)
9098        (symbol-name group)
9099        (symbol-value group)
9100        (gnus-get-function (gnus-find-method-for-group
9101                            (symbol-name group)) 'request-accept-article t)))
9102
9103 (defun gnus-read-move-group-name (prompt default articles prefix)
9104   "Read a group name."
9105   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9106          (minibuffer-confirm-incomplete nil) ; XEmacs
9107          (prom
9108           (format "%s %s to:"
9109                   prompt
9110                   (if (> (length articles) 1)
9111                       (format "these %d articles" (length articles))
9112                     "this article")))
9113          (to-newsgroup
9114           (cond
9115            ((null split-name)
9116             (gnus-completing-read default prom
9117                                   gnus-active-hashtb
9118                                   'gnus-valid-move-group-p
9119                                   nil prefix
9120                                   'gnus-group-history))
9121            ((= 1 (length split-name))
9122             (gnus-completing-read (car split-name) prom
9123                                   gnus-active-hashtb
9124                                   'gnus-valid-move-group-p
9125                                   nil nil
9126                                   'gnus-group-history))
9127            (t
9128             (gnus-completing-read nil prom
9129                                   (mapcar (lambda (el) (list el))
9130                                           (nreverse split-name))
9131                                   nil nil nil
9132                                   'gnus-group-history)))))
9133     (when to-newsgroup
9134       (if (or (string= to-newsgroup "")
9135               (string= to-newsgroup prefix))
9136           (setq to-newsgroup default))
9137       (unless to-newsgroup
9138         (error "No group name entered"))
9139       (or (gnus-active to-newsgroup)
9140           (gnus-activate-group to-newsgroup)
9141           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9142                                      to-newsgroup))
9143               (or (and (gnus-request-create-group
9144                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
9145                        (gnus-activate-group
9146                         to-newsgroup nil nil
9147                         (gnus-group-name-to-method to-newsgroup))
9148                        (gnus-subscribe-group to-newsgroup))
9149                   (error "Couldn't create group %s" to-newsgroup)))
9150           (error "No such group: %s" to-newsgroup)))
9151     to-newsgroup))
9152
9153 (defun gnus-summary-save-parts (type dir n reverse)
9154   "Save parts matching TYPE to DIR.
9155 If REVERSE, save parts that do not match TYPE."
9156   (interactive
9157    (list (read-string "Save parts of type: " "image/.*")
9158          (read-file-name "Save to directory: " t nil t)
9159          current-prefix-arg))
9160   (gnus-summary-iterate n
9161     (let ((gnus-display-mime-function nil)
9162           (gnus-inhibit-treatment t))
9163       (gnus-summary-select-article))
9164     (save-excursion
9165       (set-buffer gnus-article-buffer)
9166       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9167         (when handles
9168           (gnus-summary-save-parts-1 type dir handles reverse)
9169           (mm-destroy-parts handles))))))
9170
9171 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9172   (if (stringp (car handle))
9173       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9174               (cdr handle))
9175     (when (if reverse
9176               (not (string-match type (mm-handle-media-type handle)))
9177             (string-match type (mm-handle-media-type handle)))
9178       (let ((file (expand-file-name
9179                    (file-name-nondirectory
9180                     (or
9181                      (mail-content-type-get
9182                       (mm-handle-disposition handle) 'filename)
9183                      (concat gnus-newsgroup-name "." gnus-current-article)))
9184                    dir)))
9185         (unless (file-exists-p file)
9186           (mm-save-part-to-file handle file))))))
9187
9188 ;; Summary extract commands
9189
9190 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9191   (let ((buffer-read-only nil)
9192         (article (gnus-summary-article-number))
9193         after-article b e)
9194     (unless (gnus-summary-goto-subject article)
9195       (error "No such article: %d" article))
9196     (gnus-summary-position-point)
9197     ;; If all commands are to be bunched up on one line, we collect
9198     ;; them here.
9199     (unless gnus-view-pseudos-separately
9200       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9201             files action)
9202         (while ps
9203           (setq action (cdr (assq 'action (car ps))))
9204           (setq files (list (cdr (assq 'name (car ps)))))
9205           (while (and ps (cdr ps)
9206                       (string= (or action "1")
9207                                (or (cdr (assq 'action (cadr ps))) "2")))
9208             (push (cdr (assq 'name (cadr ps))) files)
9209             (setcdr ps (cddr ps)))
9210           (when files
9211             (when (not (string-match "%s" action))
9212               (push " " files))
9213             (push " " files)
9214             (when (assq 'execute (car ps))
9215               (setcdr (assq 'execute (car ps))
9216                       (funcall (if (string-match "%s" action)
9217                                    'format 'concat)
9218                                action
9219                                (mapconcat
9220                                 (lambda (f)
9221                                   (if (equal f " ")
9222                                       f
9223                                     (gnus-quote-arg-for-sh-or-csh f)))
9224                                 files " ")))))
9225           (setq ps (cdr ps)))))
9226     (if (and gnus-view-pseudos (not not-view))
9227         (while pslist
9228           (when (assq 'execute (car pslist))
9229             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9230                                   (eq gnus-view-pseudos 'not-confirm)))
9231           (setq pslist (cdr pslist)))
9232       (save-excursion
9233         (while pslist
9234           (setq after-article (or (cdr (assq 'article (car pslist)))
9235                                   (gnus-summary-article-number)))
9236           (gnus-summary-goto-subject after-article)
9237           (forward-line 1)
9238           (setq b (point))
9239           (insert "    " (file-name-nondirectory
9240                           (cdr (assq 'name (car pslist))))
9241                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9242           (setq e (point))
9243           (forward-line -1)             ; back to `b'
9244           (gnus-add-text-properties
9245            b (1- e) (list 'gnus-number gnus-reffed-article-number
9246                           gnus-mouse-face-prop gnus-mouse-face))
9247           (gnus-data-enter
9248            after-article gnus-reffed-article-number
9249            gnus-unread-mark b (car pslist) 0 (- e b))
9250           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9251           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9252           (setq pslist (cdr pslist)))))))
9253
9254 (defun gnus-pseudos< (p1 p2)
9255   (let ((c1 (cdr (assq 'action p1)))
9256         (c2 (cdr (assq 'action p2))))
9257     (and c1 c2 (string< c1 c2))))
9258
9259 (defun gnus-request-pseudo-article (props)
9260   (cond ((assq 'execute props)
9261          (gnus-execute-command (cdr (assq 'execute props)))))
9262   (let ((gnus-current-article (gnus-summary-article-number)))
9263     (gnus-run-hooks 'gnus-mark-article-hook)))
9264
9265 (defun gnus-execute-command (command &optional automatic)
9266   (save-excursion
9267     (gnus-article-setup-buffer)
9268     (set-buffer gnus-article-buffer)
9269     (setq buffer-read-only nil)
9270     (let ((command (if automatic command
9271                      (read-string "Command: " (cons command 0)))))
9272       (erase-buffer)
9273       (insert "$ " command "\n\n")
9274       (if gnus-view-pseudo-asynchronously
9275           (start-process "gnus-execute" (current-buffer) shell-file-name
9276                          shell-command-switch command)
9277         (call-process shell-file-name nil t nil
9278                       shell-command-switch command)))))
9279
9280 ;; Summary kill commands.
9281
9282 (defun gnus-summary-edit-global-kill (article)
9283   "Edit the \"global\" kill file."
9284   (interactive (list (gnus-summary-article-number)))
9285   (gnus-group-edit-global-kill article))
9286
9287 (defun gnus-summary-edit-local-kill ()
9288   "Edit a local kill file applied to the current newsgroup."
9289   (interactive)
9290   (setq gnus-current-headers (gnus-summary-article-header))
9291   (gnus-group-edit-local-kill
9292    (gnus-summary-article-number) gnus-newsgroup-name))
9293
9294 ;;; Header reading.
9295
9296 (defun gnus-read-header (id &optional header)
9297   "Read the headers of article ID and enter them into the Gnus system."
9298   (let ((group gnus-newsgroup-name)
9299         (gnus-override-method
9300          (and (gnus-news-group-p gnus-newsgroup-name)
9301               gnus-refer-article-method))
9302         where)
9303     ;; First we check to see whether the header in question is already
9304     ;; fetched.
9305     (if (stringp id)
9306         ;; This is a Message-ID.
9307         (setq header (or header (gnus-id-to-header id)))
9308       ;; This is an article number.
9309       (setq header (or header (gnus-summary-article-header id))))
9310     (if (and header
9311              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9312         ;; We have found the header.
9313         header
9314       ;; If this is a sparse article, we have to nix out its
9315       ;; previous entry in the thread hashtb.
9316       (when (and header
9317                  (gnus-summary-article-sparse-p (mail-header-number header)))
9318         (let* ((parent (gnus-parent-id (mail-header-references header)))
9319                (thread (and parent (gnus-id-to-thread parent))))
9320           (when thread
9321             (delq (assq header thread) thread))))
9322       ;; We have to really fetch the header to this article.
9323       (save-excursion
9324         (set-buffer nntp-server-buffer)
9325         (when (setq where (gnus-request-head id group))
9326           (nnheader-fold-continuation-lines)
9327           (goto-char (point-max))
9328           (insert ".\n")
9329           (goto-char (point-min))
9330           (insert "211 ")
9331           (princ (cond
9332                   ((numberp id) id)
9333                   ((cdr where) (cdr where))
9334                   (header (mail-header-number header))
9335                   (t gnus-reffed-article-number))
9336                  (current-buffer))
9337           (insert " Article retrieved.\n"))
9338         (if (or (not where)
9339                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9340             ()                          ; Malformed head.
9341           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9342             (when (and (stringp id)
9343                        (not (string= (gnus-group-real-name group)
9344                                      (car where))))
9345               ;; If we fetched by Message-ID and the article came
9346               ;; from a different group, we fudge some bogus article
9347               ;; numbers for this article.
9348               (mail-header-set-number header gnus-reffed-article-number))
9349             (save-excursion
9350               (set-buffer gnus-summary-buffer)
9351               (decf gnus-reffed-article-number)
9352               (gnus-remove-header (mail-header-number header))
9353               (push header gnus-newsgroup-headers)
9354               (setq gnus-current-headers header)
9355               (push (mail-header-number header) gnus-newsgroup-limit)))
9356           header)))))
9357
9358 (defun gnus-remove-header (number)
9359   "Remove header NUMBER from `gnus-newsgroup-headers'."
9360   (if (and gnus-newsgroup-headers
9361            (= number (mail-header-number (car gnus-newsgroup-headers))))
9362       (pop gnus-newsgroup-headers)
9363     (let ((headers gnus-newsgroup-headers))
9364       (while (and (cdr headers)
9365                   (not (= number (mail-header-number (cadr headers)))))
9366         (pop headers))
9367       (when (cdr headers)
9368         (setcdr headers (cddr headers))))))
9369
9370 ;;;
9371 ;;; summary highlights
9372 ;;;
9373
9374 (defun gnus-highlight-selected-summary ()
9375   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9376   ;; Highlight selected article in summary buffer
9377   (when gnus-summary-selected-face
9378     (save-excursion
9379       (let* ((beg (progn (beginning-of-line) (point)))
9380              (end (progn (end-of-line) (point)))
9381              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9382              (from (if (get-text-property beg gnus-mouse-face-prop)
9383                        beg
9384                      (or (next-single-property-change
9385                           beg gnus-mouse-face-prop nil end)
9386                          beg)))
9387              (to
9388               (if (= from end)
9389                   (- from 2)
9390                 (or (next-single-property-change
9391                      from gnus-mouse-face-prop nil end)
9392                     end))))
9393         ;; If no mouse-face prop on line we will have to = from = end,
9394         ;; so we highlight the entire line instead.
9395         (when (= (+ to 2) from)
9396           (setq from beg)
9397           (setq to end))
9398         (if gnus-newsgroup-selected-overlay
9399             ;; Move old overlay.
9400             (gnus-move-overlay
9401              gnus-newsgroup-selected-overlay from to (current-buffer))
9402           ;; Create new overlay.
9403           (gnus-overlay-put
9404            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9405            'face gnus-summary-selected-face))))))
9406
9407 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9408 (defun gnus-summary-highlight-line ()
9409   "Highlight current line according to `gnus-summary-highlight'."
9410   (let* ((list gnus-summary-highlight)
9411          (p (point))
9412          (end (progn (end-of-line) (point)))
9413          ;; now find out where the line starts and leave point there.
9414          (beg (progn (beginning-of-line) (point)))
9415          (article (gnus-summary-article-number))
9416          (score (or (cdr (assq (or article gnus-current-article)
9417                                gnus-newsgroup-scored))
9418                     gnus-summary-default-score 0))
9419          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9420          (inhibit-read-only t))
9421     ;; Eval the cars of the lists until we find a match.
9422     (let ((default gnus-summary-default-score))
9423       (while (and list
9424                   (not (eval (caar list))))
9425         (setq list (cdr list))))
9426     (let ((face (cdar list)))
9427       (unless (eq face (get-text-property beg 'face))
9428         (gnus-put-text-property-excluding-characters-with-faces
9429          beg end 'face
9430          (setq face (if (boundp face) (symbol-value face) face)))
9431         (when gnus-summary-highlight-line-function
9432           (funcall gnus-summary-highlight-line-function article face))))
9433     (goto-char p)))
9434
9435 (defun gnus-update-read-articles (group unread &optional compute)
9436   "Update the list of read articles in GROUP."
9437   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9438          (entry (gnus-gethash group gnus-newsrc-hashtb))
9439          (info (nth 2 entry))
9440          (prev 1)
9441          (unread (sort (copy-sequence unread) '<))
9442          read)
9443     (if (or (not info) (not active))
9444         ;; There is no info on this group if it was, in fact,
9445         ;; killed.  Gnus stores no information on killed groups, so
9446         ;; there's nothing to be done.
9447         ;; One could store the information somewhere temporarily,
9448         ;; perhaps...  Hmmm...
9449         ()
9450       ;; Remove any negative articles numbers.
9451       (while (and unread (< (car unread) 0))
9452         (setq unread (cdr unread)))
9453       ;; Remove any expired article numbers
9454       (while (and unread (< (car unread) (car active)))
9455         (setq unread (cdr unread)))
9456       ;; Compute the ranges of read articles by looking at the list of
9457       ;; unread articles.
9458       (while unread
9459         (when (/= (car unread) prev)
9460           (push (if (= prev (1- (car unread))) prev
9461                   (cons prev (1- (car unread))))
9462                 read))
9463         (setq prev (1+ (car unread)))
9464         (setq unread (cdr unread)))
9465       (when (<= prev (cdr active))
9466         (push (cons prev (cdr active)) read))
9467       (setq read (if (> (length read) 1) (nreverse read) read))
9468       (if compute
9469           read
9470         (save-excursion
9471           (set-buffer gnus-group-buffer)
9472           (gnus-undo-register
9473             `(progn
9474                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9475                (gnus-info-set-read ',info ',(gnus-info-read info))
9476                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9477                (gnus-group-update-group ,group t))))
9478        ;; Propagate the read marks to the backend.
9479        (if (gnus-check-backend-function 'request-set-mark group)
9480            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9481                  (add (gnus-remove-from-range read (gnus-info-read info))))
9482              (when (or add del)
9483                (unless (gnus-check-group group)
9484                  (error "Can't open server for %s" group))
9485                (gnus-request-set-mark
9486                 group (delq nil (list (if add (list add 'add '(read)))
9487                                       (if del (list del 'del '(read)))))))))
9488         ;; Enter this list into the group info.
9489         (gnus-info-set-read info read)
9490         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9491         (gnus-get-unread-articles-in-group info (gnus-active group))
9492         t))))
9493
9494 (defun gnus-offer-save-summaries ()
9495   "Offer to save all active summary buffers."
9496   (save-excursion
9497     (let ((buflist (buffer-list))
9498           buffers bufname)
9499       ;; Go through all buffers and find all summaries.
9500       (while buflist
9501         (and (setq bufname (buffer-name (car buflist)))
9502              (string-match "Summary" bufname)
9503              (save-excursion
9504                (set-buffer bufname)
9505                ;; We check that this is, indeed, a summary buffer.
9506                (and (eq major-mode 'gnus-summary-mode)
9507                     ;; Also make sure this isn't bogus.
9508                     gnus-newsgroup-prepared
9509                     ;; Also make sure that this isn't a dead summary buffer.
9510                     (not gnus-dead-summary-mode)))
9511              (push bufname buffers))
9512         (setq buflist (cdr buflist)))
9513       ;; Go through all these summary buffers and offer to save them.
9514       (when buffers
9515         (map-y-or-n-p
9516          "Update summary buffer %s? "
9517          (lambda (buf)
9518            (switch-to-buffer buf)
9519            (gnus-summary-exit))
9520          buffers)))))
9521
9522
9523 ;;; @ for mime-partial
9524 ;;;
9525
9526 (defun gnus-request-partial-message ()
9527   (save-excursion
9528     (let ((number (gnus-summary-article-number))
9529           (group gnus-newsgroup-name)
9530           (mother gnus-article-buffer))
9531       (set-buffer (get-buffer-create " *Partial Article*"))
9532       (erase-buffer)
9533       (setq mime-preview-buffer mother)
9534       (gnus-request-article-this-buffer number group)
9535       (mime-parse-buffer)
9536       )))
9537
9538 (autoload 'mime-combine-message/partial-pieces-automatically
9539   "mime-partial"
9540   "Internal method to combine message/partial messages automatically.")
9541
9542 (mime-add-condition
9543  'action '((type . message)(subtype . partial)
9544            (major-mode . gnus-original-article-mode)
9545            (method . mime-combine-message/partial-pieces-automatically)
9546            (summary-buffer-exp . gnus-summary-buffer)
9547            (request-partial-message-method . gnus-request-partial-message)
9548            ))
9549
9550
9551 ;;; @ for message/rfc822
9552 ;;;
9553
9554 (defun gnus-mime-extract-message/rfc822 (entity situation)
9555   (let (group article num cwin swin cur)
9556     (with-current-buffer (mime-entity-buffer entity)
9557       (save-restriction
9558         (narrow-to-region (mime-entity-body-start entity)
9559                           (mime-entity-body-end entity))
9560         (setq group (or (cdr (assq 'group situation))
9561                         (completing-read "Group: "
9562                                          gnus-active-hashtb
9563                                          nil
9564                                          (gnus-read-active-file-p)
9565                                          gnus-newsgroup-name))
9566               article (gnus-request-accept-article group)
9567               )
9568         ))
9569     (when (and (consp article)
9570                (numberp (setq article (cdr article))))
9571       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9572             cwin (get-buffer-window (current-buffer) t)
9573             )
9574       (save-window-excursion
9575         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9576             (select-window swin)
9577           (set-buffer gnus-summary-buffer)
9578           )
9579         (setq cur gnus-current-article)
9580         (forward-line num)
9581         (let (gnus-show-threads)
9582           (gnus-summary-goto-subject article t)
9583           )
9584         (gnus-summary-clear-mark-forward 1)
9585         (gnus-summary-goto-subject cur)
9586         )
9587       (when (and cwin (window-frame cwin))
9588         (select-frame (window-frame cwin))
9589         )
9590       (when (boundp 'mime-acting-situation-to-override)
9591         (set-alist 'mime-acting-situation-to-override
9592                    'group
9593                    group)
9594         (set-alist 'mime-acting-situation-to-override
9595                    'after-method
9596                    `(progn
9597                       (save-current-buffer
9598                         (set-buffer gnus-group-buffer)
9599                         (gnus-activate-group ,group)
9600                         )
9601                       (gnus-summary-goto-article ,cur
9602                                                  gnus-show-all-headers)
9603                       ))
9604         (set-alist 'mime-acting-situation-to-override
9605                    'number num)
9606         )
9607       )))
9608
9609 (mime-add-condition
9610  'action '((type . message)(subtype . rfc822)
9611            (major-mode . gnus-original-article-mode)
9612            (method . gnus-mime-extract-message/rfc822)
9613            (mode . "extract")
9614            ))
9615
9616 (mime-add-condition
9617  'action '((type . message)(subtype . news)
9618            (major-mode . gnus-original-article-mode)
9619            (method . gnus-mime-extract-message/rfc822)
9620            (mode . "extract")
9621            ))
9622
9623 (defun gnus-mime-extract-multipart (entity situation)
9624   (let ((children (mime-entity-children entity))
9625         mime-acting-situation-to-override
9626         f)
9627     (while children
9628       (mime-play-entity (car children)
9629                         (cons (assq 'mode situation)
9630                               mime-acting-situation-to-override))
9631       (setq children (cdr children)))
9632     (if (setq f (cdr (assq 'after-method
9633                            mime-acting-situation-to-override)))
9634         (eval f)
9635       )))  
9636
9637 (mime-add-condition
9638  'action '((type . multipart)
9639            (method . gnus-mime-extract-multipart)
9640            (mode . "extract")
9641            )
9642  'with-default)
9643
9644
9645 ;;; @ end
9646 ;;;
9647
9648 (defun gnus-summary-setup-default-charset ()
9649   "Setup newsgroup default charset."
9650   (if (equal gnus-newsgroup-name "nndraft:drafts")
9651       (setq gnus-newsgroup-charset nil)
9652   (let* ((name (and gnus-newsgroup-name
9653                    (gnus-group-real-name gnus-newsgroup-name)))
9654          (ignored-charsets 
9655           (or gnus-newsgroup-ephemeral-ignored-charsets
9656               (append
9657                (and gnus-newsgroup-name
9658                     (or (gnus-group-find-parameter gnus-newsgroup-name
9659                                                    'ignored-charsets t)
9660                         (let ((alist gnus-group-ignored-charsets-alist)
9661                            elem (charsets nil))
9662                           (while (setq elem (pop alist))
9663                             (when (and name
9664                                        (string-match (car elem) name))
9665                               (setq alist nil
9666                                     charsets (cdr elem))))
9667                           charsets))))
9668               gnus-newsgroup-ignored-charsets)))
9669     (setq gnus-newsgroup-charset
9670           (or gnus-newsgroup-ephemeral-charset
9671               (and gnus-newsgroup-name
9672                    (or (gnus-group-find-parameter gnus-newsgroup-name
9673                                                   'charset)
9674                        (let ((alist gnus-group-charset-alist)
9675                              elem (charset nil))
9676                          (while (setq elem (pop alist))
9677                            (when (and name
9678                                       (string-match (car elem) name))
9679                              (setq alist nil
9680                                    charset (cadr elem))))
9681                          charset)))
9682               gnus-default-charset))
9683     (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9684          ignored-charsets))))
9685
9686 ;;;
9687 ;;; Mime Commands
9688 ;;;
9689
9690 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9691   "Display the current article buffer fully MIME-buttonized.
9692 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9693 treated as multipart/mixed."
9694   (interactive "P")
9695   (require 'gnus-art)
9696   (let ((gnus-unbuttonized-mime-types nil)
9697         (gnus-mime-display-multipart-as-mixed show-all-parts))
9698     (gnus-summary-show-article)))
9699
9700 (defun gnus-summary-repair-multipart (article)
9701   "Add a Content-Type header to a multipart article without one."
9702   (interactive (list (gnus-summary-article-number)))
9703   (gnus-with-article article
9704     (message-narrow-to-head)
9705     (goto-char (point-max))
9706     (widen)
9707     (when (search-forward "\n--" nil t)
9708       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9709         (message-narrow-to-head)
9710         (message-remove-header "Mime-Version")
9711         (message-remove-header "Content-Type")
9712         (goto-char (point-max))
9713         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9714                         separator))
9715         (insert "Mime-Version: 1.0\n")
9716         (widen))))
9717   (let (gnus-mark-article-hook)
9718     (gnus-summary-select-article t t nil article)))
9719
9720 (defun gnus-summary-toggle-display-buttonized ()
9721   "Toggle the buttonizing of the article buffer."
9722   (interactive)
9723   (require 'gnus-art)
9724   (if (setq gnus-inhibit-mime-unbuttonizing
9725             (not gnus-inhibit-mime-unbuttonizing))
9726       (let ((gnus-unbuttonized-mime-types nil))
9727         (gnus-summary-show-article))
9728     (gnus-summary-show-article)))
9729
9730 ;;;
9731 ;;; Intelli-mouse commmands
9732 ;;;
9733
9734 (defun gnus-wheel-summary-scroll (event)
9735   (interactive "e")
9736   (let ((amount (if (memq 'shift (event-modifiers event))
9737                     (car gnus-wheel-scroll-amount)
9738                   (cdr gnus-wheel-scroll-amount)))
9739         (direction (- (* (static-if (featurep 'xemacs)
9740                              (event-button event)
9741                            (cond ((eq 'mouse-4 (event-basic-type event))
9742                                   4)
9743                                  ((eq 'mouse-5 (event-basic-type event))
9744                                   5)))
9745                          2) 9))
9746         edge)
9747     (gnus-summary-scroll-up (* amount direction))
9748     (when (gnus-eval-in-buffer-window gnus-article-buffer
9749             (save-restriction
9750               (widen)
9751               (and (if (< 0 direction)
9752                        (gnus-article-next-page 0)
9753                      (gnus-article-prev-page 0)
9754                      (bobp))
9755                    (if (setq edge (get-text-property
9756                                    (point-min) 'gnus-wheel-edge))
9757                        (setq edge (* edge direction))
9758                      (setq edge -1))
9759                    (or (plusp edge)
9760                        (let ((buffer-read-only nil)
9761                              (inhibit-read-only t))
9762                          (put-text-property (point-min) (point-max)
9763                                             'gnus-wheel-edge direction)
9764                          nil))
9765                    (or (> edge gnus-wheel-edge-resistance)
9766                        (let ((buffer-read-only nil)
9767                              (inhibit-read-only t))
9768                          (put-text-property (point-min) (point-max)
9769                                             'gnus-wheel-edge
9770                                             (* (1+ edge) direction))
9771                          nil))
9772                    (eq last-command 'gnus-wheel-summary-scroll))
9773               ))
9774       (gnus-summary-next-article nil nil (minusp direction)))
9775     ))
9776
9777 (defun gnus-wheel-install ()
9778   "Enable mouse wheel support on summary window."
9779   (when gnus-use-wheel
9780     (let ((keys 
9781            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
9782       (dolist (key keys)
9783         (define-key gnus-summary-mode-map key
9784           'gnus-wheel-summary-scroll)))))
9785
9786 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
9787
9788 ;;;
9789 ;;; with article
9790 ;;;
9791
9792 (defmacro gnus-with-article (article &rest forms)
9793   "Select ARTICLE and perform FORMS in the original article buffer.
9794 Then replace the article with the result."
9795   `(progn
9796      ;; We don't want the article to be marked as read.
9797      (let (gnus-mark-article-hook)
9798        (gnus-summary-select-article t t nil ,article))
9799      (set-buffer gnus-original-article-buffer)
9800      ,@forms
9801      (if (not (gnus-check-backend-function
9802                'request-replace-article (car gnus-article-current)))
9803          (gnus-message 5 "Read-only group; not replacing")
9804        (unless (gnus-request-replace-article
9805                 ,article (car gnus-article-current)
9806                 (current-buffer) t)
9807          (error "Couldn't replace article")))
9808      ;; The cache and backlog have to be flushed somewhat.
9809      (when gnus-keep-backlog
9810        (gnus-backlog-remove-article
9811         (car gnus-article-current) (cdr gnus-article-current)))
9812      (when gnus-use-cache
9813        (gnus-cache-update-article
9814         (car gnus-article-current) (cdr gnus-article-current)))))
9815
9816 (put 'gnus-with-article 'lisp-indent-function 1)
9817 (put 'gnus-with-article 'edebug-form-spec '(form body))
9818
9819 ;;;
9820 ;;; Generic summary marking commands
9821 ;;;
9822
9823 (defvar gnus-summary-marking-alist
9824   '((read gnus-del-mark "d")
9825     (unread gnus-unread-mark "u")
9826     (ticked gnus-ticked-mark "!")
9827     (dormant gnus-dormant-mark "?")
9828     (expirable gnus-expirable-mark "e"))
9829   "An alist of names/marks/keystrokes.")
9830
9831 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9832 (defvar gnus-summary-mark-map)
9833
9834 (defun gnus-summary-make-all-marking-commands ()
9835   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9836   (dolist (elem gnus-summary-marking-alist)
9837     (apply 'gnus-summary-make-marking-command elem)))
9838
9839 (defun gnus-summary-make-marking-command (name mark keystroke)
9840   (let ((map (make-sparse-keymap)))
9841     (define-key gnus-summary-generic-mark-map keystroke map)
9842     (dolist (lway `((next "next" next nil "n")
9843                     (next-unread "next unread" next t "N")
9844                     (prev "previous" prev nil "p")
9845                     (prev-unread "previous unread" prev t "P")
9846                     (nomove "" nil nil ,keystroke)))
9847       (let ((func (gnus-summary-make-marking-command-1
9848                    mark (car lway) lway name)))
9849         (setq func (eval func))
9850         (define-key map (nth 4 lway) func)))))
9851       
9852 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9853   `(defun ,(intern
9854             (format "gnus-summary-put-mark-as-%s%s"
9855                     name (if (eq way 'nomove)
9856                              ""
9857                            (concat "-" (symbol-name way)))))
9858      (n)
9859      ,(format
9860        "Mark the current article as %s%s.
9861 If N, the prefix, then repeat N times.
9862 If N is negative, move in reverse order.
9863 The difference between N and the actual number of articles marked is
9864 returned."
9865        name (car (cdr lway)))
9866      (interactive "p")
9867      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9868     
9869 (defun gnus-summary-generic-mark (n mark move unread)
9870   "Mark N articles with MARK."
9871   (unless (eq major-mode 'gnus-summary-mode)
9872     (error "This command can only be used in the summary buffer"))
9873   (gnus-summary-show-thread)
9874   (let ((nummove
9875          (cond
9876           ((eq move 'next) 1)
9877           ((eq move 'prev) -1)
9878           (t 0))))
9879     (if (zerop nummove)
9880         (setq n 1)
9881       (when (< n 0)
9882         (setq n (abs n)
9883               nummove (* -1 nummove))))
9884     (while (and (> n 0)
9885                 (gnus-summary-mark-article nil mark)
9886                 (zerop (gnus-summary-next-subject nummove unread t)))
9887       (setq n (1- n)))
9888     (when (/= 0 n)
9889       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9890     (gnus-summary-recenter)
9891     (gnus-summary-position-point)
9892     (gnus-set-mode-line 'summary)
9893     n))
9894
9895 (gnus-summary-make-all-marking-commands)
9896
9897 (gnus-ems-redefine)
9898
9899 (provide 'gnus-sum)
9900
9901 (run-hooks 'gnus-sum-load-hook)
9902
9903 ;;; gnus-sum.el ends here