Revert previous change of not making error.
[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
32 (require 'gnus)
33 (require 'gnus-group)
34 (require 'gnus-spec)
35 (require 'gnus-range)
36 (require 'gnus-int)
37 (require 'gnus-undo)
38 (require 'gnus-util)
39 (require 'mime-view)
40
41 (eval-when-compile
42   (require 'mime-play)
43   (require 'static))
44
45 (eval-and-compile
46   (autoload 'gnus-cache-articles-in-group "gnus-cache"))
47
48 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
49 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
50
51 (defcustom gnus-kill-summary-on-exit t
52   "*If non-nil, kill the summary buffer when you exit from it.
53 If nil, the summary will become a \"*Dead Summary*\" buffer, and
54 it will be killed sometime later."
55   :group 'gnus-summary-exit
56   :type 'boolean)
57
58 (defcustom gnus-fetch-old-headers nil
59   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
60 If an unread article in the group refers to an older, already read (or
61 just marked as read) article, the old article will not normally be
62 displayed in the Summary buffer.  If this variable is non-nil, Gnus
63 will attempt to grab the headers to the old articles, and thereby
64 build complete threads.  If it has the value `some', only enough
65 headers to connect otherwise loose threads will be displayed.  This
66 variable can also be a number.  In that case, no more than that number
67 of old headers will be fetched.  If it has the value `invisible', all
68 old headers will be fetched, but none will be displayed.
69
70 The server has to support NOV for any of this to work."
71   :group 'gnus-thread
72   :type '(choice (const :tag "off" nil)
73                  (const some)
74                  number
75                  (sexp :menu-tag "other" t)))
76
77 (defcustom gnus-refer-thread-limit 200
78   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
79 If t, fetch all the available old headers."
80   :group 'gnus-thread
81   :type '(choice number
82                  (sexp :menu-tag "other" t)))
83
84 (defcustom gnus-summary-make-false-root 'adopt
85   "*nil means that Gnus won't gather loose threads.
86 If the root of a thread has expired or been read in a previous
87 session, the information necessary to build a complete thread has been
88 lost.  Instead of having many small sub-threads from this original thread
89 scattered all over the summary buffer, Gnus can gather them.
90
91 If non-nil, Gnus will try to gather all loose sub-threads from an
92 original thread into one large thread.
93
94 If this variable is non-nil, it should be one of `none', `adopt',
95 `dummy' or `empty'.
96
97 If this variable is `none', Gnus will not make a false root, but just
98 present the sub-threads after another.
99 If this variable is `dummy', Gnus will create a dummy root that will
100 have all the sub-threads as children.
101 If this variable is `adopt', Gnus will make one of the \"children\"
102 the parent and mark all the step-children as such.
103 If this variable is `empty', the \"children\" are printed with empty
104 subject fields.  (Or rather, they will be printed with a string
105 given by the `gnus-summary-same-subject' variable.)"
106   :group 'gnus-thread
107   :type '(choice (const :tag "off" nil)
108                  (const none)
109                  (const dummy)
110                  (const adopt)
111                  (const empty)))
112
113 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
114   "*A regexp to match subjects to be excluded from loose thread gathering.
115 As loose thread gathering is done on subjects only, that means that
116 there can be many false gatherings performed.  By rooting out certain
117 common subjects, gathering might become saner."
118   :group 'gnus-thread
119   :type 'regexp)
120
121 (defcustom gnus-summary-gather-subject-limit nil
122   "*Maximum length of subject comparisons when gathering loose threads.
123 Use nil to compare full subjects.  Setting this variable to a low
124 number will help gather threads that have been corrupted by
125 newsreaders chopping off subject lines, but it might also mean that
126 unrelated articles that have subject that happen to begin with the
127 same few characters will be incorrectly gathered.
128
129 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
130 comparing subjects."
131   :group 'gnus-thread
132   :type '(choice (const :tag "off" nil)
133                  (const fuzzy)
134                  (sexp :menu-tag "on" t)))
135
136 (defcustom gnus-simplify-subject-functions nil
137   "List of functions taking a string argument that simplify subjects.
138 The functions are applied recursively.
139
140 Useful functions to put in this list include: `gnus-simplify-subject-re',
141 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
142   :group 'gnus-thread
143   :type '(repeat function))
144
145 (defcustom gnus-simplify-ignored-prefixes nil
146   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
147   :group 'gnus-thread
148   :type '(choice (const :tag "off" nil)
149                  regexp))
150
151 (defcustom gnus-build-sparse-threads nil
152   "*If non-nil, fill in the gaps in threads.
153 If `some', only fill in the gaps that are needed to tie loose threads
154 together.  If `more', fill in all leaf nodes that Gnus can find.  If
155 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
156   :group 'gnus-thread
157   :type '(choice (const :tag "off" nil)
158                  (const some)
159                  (const more)
160                  (sexp :menu-tag "all" t)))
161
162 (defcustom gnus-summary-thread-gathering-function
163   'gnus-gather-threads-by-subject
164   "*Function used for gathering loose threads.
165 There are two pre-defined functions: `gnus-gather-threads-by-subject',
166 which only takes Subjects into consideration; and
167 `gnus-gather-threads-by-references', which compared the References
168 headers of the articles to find matches."
169   :group 'gnus-thread
170   :type '(radio (function-item gnus-gather-threads-by-subject)
171                 (function-item gnus-gather-threads-by-references)
172                 (function :tag "other")))
173
174 (defcustom gnus-summary-same-subject ""
175   "*String indicating that the current article has the same subject as the previous.
176 This variable will only be used if the value of
177 `gnus-summary-make-false-root' is `empty'."
178   :group 'gnus-summary-format
179   :type 'string)
180
181 (defcustom gnus-summary-goto-unread t
182   "*If t, marking commands will go to the next unread article.
183 If `never', commands that usually go to the next unread article, will
184 go to the next article, whether it is read or not.
185 If nil, only the marking commands will go to the next (un)read article."
186   :group 'gnus-summary-marks
187   :link '(custom-manual "(gnus)Setting Marks")
188   :type '(choice (const :tag "off" nil)
189                  (const never)
190                  (sexp :menu-tag "on" t)))
191
192 (defcustom gnus-summary-default-score 0
193   "*Default article score level.
194 All scores generated by the score files will be added to this score.
195 If this variable is nil, scoring will be disabled."
196   :group 'gnus-score-default
197   :type '(choice (const :tag "disable")
198                  integer))
199
200 (defcustom gnus-summary-zcore-fuzz 0
201   "*Fuzziness factor for the zcore in the summary buffer.
202 Articles with scores closer than this to `gnus-summary-default-score'
203 will not be marked."
204   :group 'gnus-summary-format
205   :type 'integer)
206
207 (defcustom gnus-simplify-subject-fuzzy-regexp nil
208   "*Strings to be removed when doing fuzzy matches.
209 This can either be a regular expression or list of regular expressions
210 that will be removed from subject strings if fuzzy subject
211 simplification is selected."
212   :group 'gnus-thread
213   :type '(repeat regexp))
214
215 (defcustom gnus-show-threads t
216   "*If non-nil, display threads in summary mode."
217   :group 'gnus-thread
218   :type 'boolean)
219
220 (defcustom gnus-thread-hide-subtree nil
221   "*If non-nil, hide all threads initially.
222 If threads are hidden, you have to run the command
223 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
224 to expose hidden threads."
225   :group 'gnus-thread
226   :type 'boolean)
227
228 (defcustom gnus-thread-hide-killed t
229   "*If non-nil, hide killed threads automatically."
230   :group 'gnus-thread
231   :type 'boolean)
232
233 (defcustom gnus-thread-ignore-subject t
234   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
235 If nil, articles that have different subjects from their parents will
236 start separate threads."
237   :group 'gnus-thread
238   :type 'boolean)
239
240 (defcustom gnus-thread-operation-ignore-subject t
241   "*If non-nil, subjects will be ignored when doing thread commands.
242 This affects commands like `gnus-summary-kill-thread' and
243 `gnus-summary-lower-thread'.
244
245 If this variable is nil, articles in the same thread with different
246 subjects will not be included in the operation in question.  If this
247 variable is `fuzzy', only articles that have subjects that are fuzzily
248 equal will be included."
249   :group 'gnus-thread
250   :type '(choice (const :tag "off" nil)
251                  (const fuzzy)
252                  (sexp :tag "on" t)))
253
254 (defcustom gnus-thread-indent-level 4
255   "*Number that says how much each sub-thread should be indented."
256   :group 'gnus-thread
257   :type 'integer)
258
259 (defcustom gnus-auto-extend-newsgroup t
260   "*If non-nil, extend newsgroup forward and backward when requested."
261   :group 'gnus-summary-choose
262   :type 'boolean)
263
264 (defcustom gnus-auto-select-first t
265   "*If nil, don't select the first unread article when entering a group.
266 If this variable is `best', select the highest-scored unread article
267 in the group.  If t, select the first unread article.
268
269 This variable can also be a function to place point on a likely
270 subject line.  Useful values include `gnus-summary-first-unread-subject',
271 `gnus-summary-first-unread-article' and
272 `gnus-summary-best-unread-article'.
273
274 If you want to prevent automatic selection of the first unread article
275 in some newsgroups, set the variable to nil in
276 `gnus-select-group-hook'."
277   :group 'gnus-group-select
278   :type '(choice (const :tag "none" nil)
279                  (const best)
280                  (sexp :menu-tag "first" t)
281                  (function-item gnus-summary-first-unread-subject)
282                  (function-item gnus-summary-first-unread-article)
283                  (function-item gnus-summary-best-unread-article)))
284
285 (defcustom gnus-dont-select-after-jump-to-other-group nil
286   "If non-nil, don't select the first unread article after entering the
287 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
288 it is depend on the value of `gnus-auto-select-first' whether to select
289 or not."
290   :group 'gnus-group-select
291   :type 'boolean)
292
293 (defcustom gnus-auto-select-next t
294   "*If non-nil, offer to go to the next group from the end of the previous.
295 If the value is t and the next newsgroup is empty, Gnus will exit
296 summary mode and go back to group mode.  If the value is neither nil
297 nor t, Gnus will select the following unread newsgroup.  In
298 particular, if the value is the symbol `quietly', the next unread
299 newsgroup will be selected without any confirmation, and if it is
300 `almost-quietly', the next group will be selected without any
301 confirmation if you are located on the last article in the group.
302 Finally, if this variable is `slightly-quietly', the `Z n' command
303 will go to the next group without confirmation."
304   :group 'gnus-summary-maneuvering
305   :type '(choice (const :tag "off" nil)
306                  (const quietly)
307                  (const almost-quietly)
308                  (const slightly-quietly)
309                  (sexp :menu-tag "on" t)))
310
311 (defcustom gnus-auto-select-same nil
312   "*If non-nil, select the next article with the same subject.
313 If there are no more articles with the same subject, go to
314 the first unread article."
315   :group 'gnus-summary-maneuvering
316   :type 'boolean)
317
318 (defcustom gnus-summary-check-current nil
319   "*If non-nil, consider the current article when moving.
320 The \"unread\" movement commands will stay on the same line if the
321 current article is unread."
322   :group 'gnus-summary-maneuvering
323   :type 'boolean)
324
325 (defcustom gnus-auto-center-summary t
326   "*If non-nil, always center the current summary buffer.
327 In particular, if `vertical' do only vertical recentering.  If non-nil
328 and non-`vertical', do both horizontal and vertical recentering."
329   :group 'gnus-summary-maneuvering
330   :type '(choice (const :tag "none" nil)
331                  (const vertical)
332                  (integer :tag "height")
333                  (sexp :menu-tag "both" t)))
334
335 (defcustom gnus-show-all-headers nil
336   "*If non-nil, don't hide any headers."
337   :group 'gnus-article-hiding
338   :group 'gnus-article-headers
339   :type 'boolean)
340
341 (defcustom gnus-summary-ignore-duplicates nil
342   "*If non-nil, ignore articles with identical Message-ID headers."
343   :group 'gnus-summary
344   :type 'boolean)
345
346 (defcustom gnus-single-article-buffer t
347   "*If non-nil, display all articles in the same buffer.
348 If nil, each group will get its own article buffer."
349   :group 'gnus-article-various
350   :type 'boolean)
351
352 (defcustom gnus-break-pages t
353   "*If non-nil, do page breaking on articles.
354 The page delimiter is specified by the `gnus-page-delimiter'
355 variable."
356   :group 'gnus-article-various
357   :type 'boolean)
358
359 (defcustom gnus-show-mime t
360   "*If non-nil, do mime processing of articles.
361 The articles will simply be fed to the function given by
362 `gnus-article-display-method-for-mime'."
363   :group 'gnus-article-mime
364   :type 'boolean)
365
366 (defcustom gnus-move-split-methods nil
367   "*Variable used to suggest where articles are to be moved to.
368 It uses the same syntax as the `gnus-split-methods' variable."
369   :group 'gnus-summary-mail
370   :type '(repeat (choice (list :value (fun) function)
371                          (cons :value ("" "") regexp (repeat string))
372                          (sexp :value nil))))
373
374 (defcustom gnus-unread-mark ? ;Whitespace
375   "*Mark used for unread articles."
376   :group 'gnus-summary-marks
377   :type 'character)
378
379 (defcustom gnus-ticked-mark ?!
380   "*Mark used for ticked articles."
381   :group 'gnus-summary-marks
382   :type 'character)
383
384 (defcustom gnus-dormant-mark ??
385   "*Mark used for dormant articles."
386   :group 'gnus-summary-marks
387   :type 'character)
388
389 (defcustom gnus-del-mark ?r
390   "*Mark used for del'd articles."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-read-mark ?R
395   "*Mark used for read articles."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-expirable-mark ?E
400   "*Mark used for expirable articles."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-killed-mark ?K
405   "*Mark used for killed articles."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-souped-mark ?F
410   "*Mark used for killed articles."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-kill-file-mark ?X
415   "*Mark used for articles killed by kill files."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-low-score-mark ?Y
420   "*Mark used for articles with a low score."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-catchup-mark ?C
425   "*Mark used for articles that are caught up."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-replied-mark ?A
430   "*Mark used for articles that have been replied to."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-cached-mark ?*
435   "*Mark used for articles that are in the cache."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-saved-mark ?S
440   "*Mark used for articles that have been saved to."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-ancient-mark ?O
445   "*Mark used for ancient articles."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-sparse-mark ?Q
450   "*Mark used for sparsely reffed articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-canceled-mark ?G
455   "*Mark used for canceled articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-duplicate-mark ?M
460   "*Mark used for duplicate articles."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-undownloaded-mark ?@
465   "*Mark used for articles that weren't downloaded."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-downloadable-mark ?%
470   "*Mark used for articles that are to be downloaded."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-unsendable-mark ?=
475   "*Mark used for articles that won't be sent."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-score-over-mark ?+
480   "*Score mark used for articles with high scores."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-score-below-mark ?-
485   "*Score mark used for articles with low scores."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-empty-thread-mark ? ;Whitespace
490   "*There is no thread under the article."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-not-empty-thread-mark ?=
495   "*There is a thread under the article."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-view-pseudo-asynchronously nil
500   "*If non-nil, Gnus will view pseudo-articles asynchronously."
501   :group 'gnus-extract-view
502   :type 'boolean)
503
504 (defcustom gnus-auto-expirable-marks
505   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
506         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
507         gnus-souped-mark gnus-duplicate-mark)
508   "*The list of marks converted into expiration if a group is auto-expirable."
509   :group 'gnus-summary
510   :type '(repeat character))
511
512 (defcustom gnus-inhibit-user-auto-expire t
513   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
514   :group 'gnus-summary
515   :type 'boolean)
516
517 (defcustom gnus-view-pseudos nil
518   "*If `automatic', pseudo-articles will be viewed automatically.
519 If `not-confirm', pseudos will be viewed automatically, and the user
520 will not be asked to confirm the command."
521   :group 'gnus-extract-view
522   :type '(choice (const :tag "off" nil)
523                  (const automatic)
524                  (const not-confirm)))
525
526 (defcustom gnus-view-pseudos-separately t
527   "*If non-nil, one pseudo-article will be created for each file to be viewed.
528 If nil, all files that use the same viewing command will be given as a
529 list of parameters to that command."
530   :group 'gnus-extract-view
531   :type 'boolean)
532
533 (defcustom gnus-insert-pseudo-articles t
534   "*If non-nil, insert pseudo-articles when decoding articles."
535   :group 'gnus-extract-view
536   :type 'boolean)
537
538 (defcustom gnus-summary-dummy-line-format
539   "  %(:                          :%) %S\n"
540   "*The format specification for the dummy roots in the summary buffer.
541 It works along the same lines as a normal formatting string,
542 with some simple extensions.
543
544 %S  The subject"
545   :group 'gnus-threading
546   :type 'string)
547
548 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
549   "*The format specification for the summary mode line.
550 It works along the same lines as a normal formatting string,
551 with some simple extensions:
552
553 %G  Group name
554 %p  Unprefixed group name
555 %A  Current article number
556 %z  Current article score
557 %V  Gnus version
558 %U  Number of unread articles in the group
559 %e  Number of unselected articles in the group
560 %Z  A string with unread/unselected article counts
561 %g  Shortish group name
562 %S  Subject of the current article
563 %u  User-defined spec
564 %s  Current score file name
565 %d  Number of dormant articles
566 %r  Number of articles that have been marked as read in this session
567 %E  Number of articles expunged by the score files"
568   :group 'gnus-summary-format
569   :type 'string)
570
571 (defcustom gnus-list-identifiers nil
572   "Regexp that matches list identifiers to be removed from subject.
573 This can also be a list of regexps."
574   :group 'gnus-summary-format
575   :group 'gnus-article-hiding
576   :type '(choice (const :tag "none" nil)
577                  (regexp :value ".*")
578                  (repeat :value (".*") regexp)))
579
580 (defcustom gnus-summary-mark-below 0
581   "*Mark all articles with a score below this variable as read.
582 This variable is local to each summary buffer and usually set by the
583 score file."
584   :group 'gnus-score-default
585   :type 'integer)
586
587 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
588   "*List of functions used for sorting articles in the summary buffer.
589 This variable is only used when not using a threaded display."
590   :group 'gnus-summary-sort
591   :type '(repeat (choice (function-item gnus-article-sort-by-number)
592                          (function-item gnus-article-sort-by-author)
593                          (function-item gnus-article-sort-by-subject)
594                          (function-item gnus-article-sort-by-date)
595                          (function-item gnus-article-sort-by-score)
596                          (function :tag "other"))))
597
598 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
599   "*List of functions used for sorting threads in the summary buffer.
600 By default, threads are sorted by article number.
601
602 Each function takes two threads and return non-nil if the first thread
603 should be sorted before the other.  If you use more than one function,
604 the primary sort function should be the last.  You should probably
605 always include `gnus-thread-sort-by-number' in the list of sorting
606 functions -- preferably first.
607
608 Ready-made functions include `gnus-thread-sort-by-number',
609 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
610 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
611 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
612   :group 'gnus-summary-sort
613   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
614                          (function-item gnus-thread-sort-by-author)
615                          (function-item gnus-thread-sort-by-subject)
616                          (function-item gnus-thread-sort-by-date)
617                          (function-item gnus-thread-sort-by-score)
618                          (function-item gnus-thread-sort-by-total-score)
619                          (function :tag "other"))))
620
621 (defcustom gnus-thread-score-function '+
622   "*Function used for calculating the total score of a thread.
623
624 The function is called with the scores of the article and each
625 subthread and should then return the score of the thread.
626
627 Some functions you can use are `+', `max', or `min'."
628   :group 'gnus-summary-sort
629   :type 'function)
630
631 (defcustom gnus-summary-expunge-below nil
632   "All articles that have a score less than this variable will be expunged.
633 This variable is local to the summary buffers."
634   :group 'gnus-score-default
635   :type '(choice (const :tag "off" nil)
636                  integer))
637
638 (defcustom gnus-thread-expunge-below nil
639   "All threads that have a total score less than this variable will be expunged.
640 See `gnus-thread-score-function' for en explanation of what a
641 \"thread score\" is.
642
643 This variable is local to the summary buffers."
644   :group 'gnus-threading
645   :group 'gnus-score-default
646   :type '(choice (const :tag "off" nil)
647                  integer))
648
649 (defcustom gnus-summary-mode-hook nil
650   "*A hook for Gnus summary mode.
651 This hook is run before any variables are set in the summary buffer."
652   :group 'gnus-summary-various
653   :type 'hook)
654
655 (defcustom gnus-summary-menu-hook nil
656   "*Hook run after the creation of the summary mode menu."
657   :group 'gnus-summary-visual
658   :type 'hook)
659
660 (defcustom gnus-summary-exit-hook nil
661   "*A hook called on exit from the summary buffer.
662 It will be called with point in the group buffer."
663   :group 'gnus-summary-exit
664   :type 'hook)
665
666 (defcustom gnus-summary-prepare-hook nil
667   "*A hook called after the summary buffer has been generated.
668 If you want to modify the summary buffer, you can use this hook."
669   :group 'gnus-summary-various
670   :type 'hook)
671
672 (defcustom gnus-summary-prepared-hook nil
673   "*A hook called as the last thing after the summary buffer has been generated."
674   :group 'gnus-summary-various
675   :type 'hook)
676
677 (defcustom gnus-summary-generate-hook nil
678   "*A hook run just before generating the summary buffer.
679 This hook is commonly used to customize threading variables and the
680 like."
681   :group 'gnus-summary-various
682   :type 'hook)
683
684 (defcustom gnus-select-group-hook nil
685   "*A hook called when a newsgroup is selected.
686
687 If you'd like to simplify subjects like the
688 `gnus-summary-next-same-subject' command does, you can use the
689 following hook:
690
691  (setq gnus-select-group-hook
692       (list
693         (lambda ()
694           (mapcar (lambda (header)
695                      (mail-header-set-subject
696                       header
697                       (gnus-simplify-subject
698                        (mail-header-subject header) 're-only)))
699                   gnus-newsgroup-headers))))"
700   :group 'gnus-group-select
701   :type 'hook)
702
703 (defcustom gnus-select-article-hook nil
704   "*A hook called when an article is selected."
705   :group 'gnus-summary-choose
706   :type 'hook)
707
708 (defcustom gnus-visual-mark-article-hook
709   (list 'gnus-highlight-selected-summary)
710   "*Hook run after selecting an article in the summary buffer.
711 It is meant to be used for highlighting the article in some way.  It
712 is not run if `gnus-visual' is nil."
713   :group 'gnus-summary-visual
714   :type 'hook)
715
716 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
717   "*A hook called before parsing the headers."
718   :group 'gnus-various
719   :type 'hook)
720
721 (defcustom gnus-exit-group-hook nil
722   "*A hook called when exiting (not quitting) summary mode."
723   :group 'gnus-various
724   :type 'hook)
725
726 (defcustom gnus-summary-update-hook
727   (list 'gnus-summary-highlight-line)
728   "*A hook called when a summary line is changed.
729 The hook will not be called if `gnus-visual' is nil.
730
731 The default function `gnus-summary-highlight-line' will
732 highlight the line according to the `gnus-summary-highlight'
733 variable."
734   :group 'gnus-summary-visual
735   :type 'hook)
736
737 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
738   "*A hook called when an article is selected for the first time.
739 The hook is intended to mark an article as read (or unread)
740 automatically when it is selected."
741   :group 'gnus-summary-choose
742   :type 'hook)
743
744 (defcustom gnus-group-no-more-groups-hook nil
745   "*A hook run when returning to group mode having no more (unread) groups."
746   :group 'gnus-group-select
747   :type 'hook)
748
749 (defcustom gnus-ps-print-hook nil
750   "*A hook run before ps-printing something from Gnus."
751   :group 'gnus-summary
752   :type 'hook)
753
754 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
755   "Face used for highlighting the current article in the summary buffer."
756   :group 'gnus-summary-visual
757   :type 'face)
758
759 (defcustom gnus-summary-highlight
760   '(((= mark gnus-canceled-mark)
761      . gnus-summary-cancelled-face)
762     ((and (> score default)
763           (or (= mark gnus-dormant-mark)
764               (= mark gnus-ticked-mark)))
765      . gnus-summary-high-ticked-face)
766     ((and (< score default)
767           (or (= mark gnus-dormant-mark)
768               (= mark gnus-ticked-mark)))
769      . gnus-summary-low-ticked-face)
770     ((or (= mark gnus-dormant-mark)
771          (= mark gnus-ticked-mark))
772      . gnus-summary-normal-ticked-face)
773     ((and (> score default) (= mark gnus-ancient-mark))
774      . gnus-summary-high-ancient-face)
775     ((and (< score default) (= mark gnus-ancient-mark))
776      . gnus-summary-low-ancient-face)
777     ((= mark gnus-ancient-mark)
778      . gnus-summary-normal-ancient-face)
779     ((and (> score default) (= mark gnus-unread-mark))
780      . gnus-summary-high-unread-face)
781     ((and (< score default) (= mark gnus-unread-mark))
782      . gnus-summary-low-unread-face)
783     ((and (memq article gnus-newsgroup-incorporated) 
784           (= mark gnus-unread-mark))
785      . gnus-summary-incorporated-face)
786     ((= mark gnus-unread-mark)
787      . gnus-summary-normal-unread-face)
788     ((and (> score default) (memq mark (list gnus-downloadable-mark
789                                              gnus-undownloaded-mark)))
790      . gnus-summary-high-unread-face)
791     ((and (< score default) (memq mark (list gnus-downloadable-mark
792                                              gnus-undownloaded-mark)))
793      . gnus-summary-low-unread-face)
794     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
795      . gnus-summary-normal-unread-face)
796     ((> score default)
797      . gnus-summary-high-read-face)
798     ((< score default)
799      . gnus-summary-low-read-face)
800     (t
801      . gnus-summary-normal-read-face))
802   "*Controls the highlighting of summary buffer lines.
803
804 A list of (FORM . FACE) pairs.  When deciding how a a particular
805 summary line should be displayed, each form is evaluated.  The content
806 of the face field after the first true form is used.  You can change
807 how those summary lines are displayed, by editing the face field.
808
809 You can use the following variables in the FORM field.
810
811 score:   The articles score
812 default: The default article score.
813 below:   The score below which articles are automatically marked as read.
814 mark:    The articles mark."
815   :group 'gnus-summary-visual
816   :type '(repeat (cons (sexp :tag "Form" nil)
817                        face)))
818
819 (defcustom gnus-alter-header-function nil
820   "Function called to allow alteration of article header structures.
821 The function is called with one parameter, the article header vector,
822 which it may alter in any way.")
823
824 (defvar gnus-decode-encoded-word-function
825   (mime-find-field-decoder 'From 'nov)
826   "Variable that says which function should be used to decode a string with encoded words.")
827
828 (defcustom gnus-extra-headers nil
829   "*Extra headers to parse."
830   :group 'gnus-summary
831   :type '(repeat symbol))
832
833 (defcustom gnus-ignored-from-addresses
834   (and user-mail-address (regexp-quote user-mail-address))
835   "*Regexp of From headers that may be suppressed in favor of To headers."
836   :group 'gnus-summary
837   :type 'regexp)
838
839 (defcustom gnus-group-charset-alist
840   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
841     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
842     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
843     ("^relcom\\>" koi8-r)
844     ("^fido7\\>" koi8-r)
845     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
846     ("^israel\\>" iso-8859-1)
847     ("^han\\>" euc-kr)
848     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
849     (".*" iso-8859-1))
850   "Alist of regexps (to match group names) and default charsets to be used when reading."
851   :type '(repeat (list (regexp :tag "Group")
852                        (symbol :tag "Charset")))
853   :group 'gnus-charset)
854
855 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit)
856   "List of charsets that should be ignored.
857 When these charsets are used in the \"charset\" parameter, the
858 default charset will be used instead."
859   :type '(repeat symbol)
860   :group 'gnus-charset)
861
862 (defcustom gnus-group-ignored-charsets-alist 
863   '(("alt\\.chinese\\.text" iso-8859-1))
864   "Alist of regexps (to match group names) and charsets that should be ignored.
865 When these charsets are used in the \"charset\" parameter, the
866 default charset will be used instead."
867   :type '(repeat (cons (regexp :tag "Group")
868                        (repeat symbol)))
869   :group 'gnus-charset)
870
871 (defcustom gnus-group-highlight-words-alist nil
872   "Alist of group regexps and highlight regexps.
873 This variable uses the same syntax as `gnus-emphasis-alist'."
874   :type '(repeat (cons (regexp :tag "Group")
875                        (repeat (list (regexp :tag "Highlight regexp")
876                                      (number :tag "Group for entire word" 0)
877                                      (number :tag "Group for displayed part" 0)
878                                      (symbol :tag "Face" 
879                                              gnus-emphasis-highlight-words)))))
880   :group 'gnus-summary-visual)
881
882 (defcustom gnus-use-wheel nil
883   "Use Intelli-mouse on summary movement"
884   :type 'boolean
885   :group 'gnus-summary-maneuvering)
886
887 (defcustom gnus-wheel-scroll-amount '(5 . 1)
888   "Amount to scroll messages by spinning the mouse wheel.
889 This is actually a cons cell, where the first item is the amount to scroll
890 on a normal wheel event, and the second is the amount to scroll when the
891 wheel is moved with the shift key depressed."
892   :type '(cons (integer :tag "Shift") integer)
893   :group 'gnus-summary-maneuvering)
894
895 (defcustom gnus-wheel-edge-resistance 2
896   "How hard it should be to change the current article
897 by moving the mouse over the edge of the article window."
898   :type 'integer
899   :group 'gnus-summary-maneuvering)
900
901 ;;; Internal variables
902
903 (defvar gnus-scores-exclude-files nil)
904 (defvar gnus-page-broken nil)
905 (defvar gnus-inhibit-mime-unbuttonizing nil)
906
907 (defvar gnus-original-article nil)
908 (defvar gnus-article-internal-prepare-hook nil)
909 (defvar gnus-newsgroup-process-stack nil)
910
911 (defvar gnus-thread-indent-array nil)
912 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
913 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
914   "Function called to sort the articles within a thread after it has 
915 been gathered together.")
916
917 ;; Avoid highlighting in kill files.
918 (defvar gnus-summary-inhibit-highlight nil)
919 (defvar gnus-newsgroup-selected-overlay nil)
920 (defvar gnus-inhibit-limiting nil)
921 (defvar gnus-newsgroup-adaptive-score-file nil)
922 (defvar gnus-current-score-file nil)
923 (defvar gnus-current-move-group nil)
924 (defvar gnus-current-copy-group nil)
925 (defvar gnus-current-crosspost-group nil)
926
927 (defvar gnus-newsgroup-dependencies nil)
928 (defvar gnus-newsgroup-adaptive nil)
929 (defvar gnus-summary-display-article-function nil)
930 (defvar gnus-summary-highlight-line-function nil
931   "Function called after highlighting a summary line.")
932
933 (defvar gnus-summary-line-format-alist
934   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
935     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
936     (?s gnus-tmp-subject-or-nil ?s)
937     (?n gnus-tmp-name ?s)
938     (?A (std11-address-string
939          (car (mime-read-field 'From gnus-tmp-header))) ?s)
940     (?a (or (std11-full-name-string
941              (car (mime-read-field 'From gnus-tmp-header)))
942             gnus-tmp-from) ?s)
943     (?F gnus-tmp-from ?s)
944     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
945     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
946     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
947     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
948     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
949     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
950     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
951     (?L gnus-tmp-lines ?d)
952     (?I gnus-tmp-indentation ?s)
953     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
954     (?R gnus-tmp-replied ?c)
955     (?\[ gnus-tmp-opening-bracket ?c)
956     (?\] gnus-tmp-closing-bracket ?c)
957     (?\> (make-string gnus-tmp-level ? ) ?s)
958     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
959     (?i gnus-tmp-score ?d)
960     (?z gnus-tmp-score-char ?c)
961     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
962     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
963     (?U gnus-tmp-unread ?c)
964     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
965     (?t (gnus-summary-number-of-articles-in-thread
966          (and (boundp 'thread) (car thread)) gnus-tmp-level)
967         ?d)
968     (?e (gnus-summary-number-of-articles-in-thread
969          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
970         ?c)
971     (?u gnus-tmp-user-defined ?s)
972     (?P (gnus-pick-line-number) ?d))
973   "An alist of format specifications that can appear in summary lines,
974 and what variables they correspond with, along with the type of the
975 variable (string, integer, character, etc).")
976
977 (defvar gnus-summary-dummy-line-format-alist
978   `((?S gnus-tmp-subject ?s)
979     (?N gnus-tmp-number ?d)
980     (?u gnus-tmp-user-defined ?s)))
981
982 (defvar gnus-summary-mode-line-format-alist
983   `((?G gnus-tmp-group-name ?s)
984     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
985     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
986     (?A gnus-tmp-article-number ?d)
987     (?Z gnus-tmp-unread-and-unselected ?s)
988     (?V gnus-version ?s)
989     (?U gnus-tmp-unread-and-unticked ?d)
990     (?S gnus-tmp-subject ?s)
991     (?e gnus-tmp-unselected ?d)
992     (?u gnus-tmp-user-defined ?s)
993     (?d (length gnus-newsgroup-dormant) ?d)
994     (?t (length gnus-newsgroup-marked) ?d)
995     (?r (length gnus-newsgroup-reads) ?d)
996     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
997     (?E gnus-newsgroup-expunged-tally ?d)
998     (?s (gnus-current-score-file-nondirectory) ?s)))
999
1000 (defvar gnus-last-search-regexp nil
1001   "Default regexp for article search command.")
1002
1003 (defvar gnus-summary-search-article-matched-data nil
1004   "Last matched data of article search command.  It is the local variable
1005 in `gnus-article-buffer' which consists of the list of start position,
1006 end position and text.")
1007
1008 (defvar gnus-last-shell-command nil
1009   "Default shell command on article.")
1010
1011 (defvar gnus-newsgroup-begin nil)
1012 (defvar gnus-newsgroup-end nil)
1013 (defvar gnus-newsgroup-last-rmail nil)
1014 (defvar gnus-newsgroup-last-mail nil)
1015 (defvar gnus-newsgroup-last-folder nil)
1016 (defvar gnus-newsgroup-last-file nil)
1017 (defvar gnus-newsgroup-auto-expire nil)
1018 (defvar gnus-newsgroup-active nil)
1019
1020 (defvar gnus-newsgroup-data nil)
1021 (defvar gnus-newsgroup-data-reverse nil)
1022 (defvar gnus-newsgroup-limit nil)
1023 (defvar gnus-newsgroup-limits nil)
1024
1025 (defvar gnus-newsgroup-unreads nil
1026   "List of unread articles in the current newsgroup.")
1027
1028 (defvar gnus-newsgroup-unselected nil
1029   "List of unselected unread articles in the current newsgroup.")
1030
1031 (defvar gnus-newsgroup-reads nil
1032   "Alist of read articles and article marks in the current newsgroup.")
1033
1034 (defvar gnus-newsgroup-expunged-tally nil)
1035
1036 (defvar gnus-newsgroup-marked nil
1037   "List of ticked articles in the current newsgroup (a subset of unread art).")
1038
1039 (defvar gnus-newsgroup-killed nil
1040   "List of ranges of articles that have been through the scoring process.")
1041
1042 (defvar gnus-newsgroup-cached nil
1043   "List of articles that come from the article cache.")
1044
1045 (defvar gnus-newsgroup-saved nil
1046   "List of articles that have been saved.")
1047
1048 (defvar gnus-newsgroup-kill-headers nil)
1049
1050 (defvar gnus-newsgroup-replied nil
1051   "List of articles that have been replied to in the current newsgroup.")
1052
1053 (defvar gnus-newsgroup-expirable nil
1054   "List of articles in the current newsgroup that can be expired.")
1055
1056 (defvar gnus-newsgroup-processable nil
1057   "List of articles in the current newsgroup that can be processed.")
1058
1059 (defvar gnus-newsgroup-downloadable nil
1060   "List of articles in the current newsgroup that can be processed.")
1061
1062 (defvar gnus-newsgroup-undownloaded nil
1063   "List of articles in the current newsgroup that haven't been downloaded..")
1064
1065 (defvar gnus-newsgroup-unsendable nil
1066   "List of articles in the current newsgroup that won't be sent.")
1067
1068 (defvar gnus-newsgroup-bookmarks nil
1069   "List of articles in the current newsgroup that have bookmarks.")
1070
1071 (defvar gnus-newsgroup-dormant nil
1072   "List of dormant articles in the current newsgroup.")
1073
1074 (defvar gnus-newsgroup-scored nil
1075   "List of scored articles in the current newsgroup.")
1076
1077 (defvar gnus-newsgroup-incorporated nil
1078   "List of incorporated articles in the current newsgroup.")
1079
1080 (defvar gnus-newsgroup-headers nil
1081   "List of article headers in the current newsgroup.")
1082
1083 (defvar gnus-newsgroup-threads nil)
1084
1085 (defvar gnus-newsgroup-prepared nil
1086   "Whether the current group has been prepared properly.")
1087
1088 (defvar gnus-newsgroup-ancient nil
1089   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1090
1091 (defvar gnus-newsgroup-sparse nil)
1092
1093 (defvar gnus-current-article nil)
1094 (defvar gnus-article-current nil)
1095 (defvar gnus-current-headers nil)
1096 (defvar gnus-have-all-headers nil)
1097 (defvar gnus-last-article nil)
1098 (defvar gnus-newsgroup-history nil)
1099 (defvar gnus-newsgroup-charset nil)
1100 (defvar gnus-newsgroup-ephemeral-charset nil)
1101 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1102
1103 (defconst gnus-summary-local-variables
1104   '(gnus-newsgroup-name
1105     gnus-newsgroup-begin gnus-newsgroup-end
1106     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1107     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1108     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1109     gnus-newsgroup-unselected gnus-newsgroup-marked
1110     gnus-newsgroup-reads gnus-newsgroup-saved
1111     gnus-newsgroup-replied gnus-newsgroup-expirable
1112     gnus-newsgroup-processable gnus-newsgroup-killed
1113     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1114     gnus-newsgroup-unsendable
1115     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1116     gnus-newsgroup-headers gnus-newsgroup-threads
1117     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1118     gnus-current-article gnus-current-headers gnus-have-all-headers
1119     gnus-last-article gnus-article-internal-prepare-hook
1120     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1121     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1122     gnus-thread-expunge-below
1123     gnus-score-alist gnus-current-score-file
1124     (gnus-summary-expunge-below . global)
1125     (gnus-summary-mark-below . global)
1126     gnus-newsgroup-active gnus-scores-exclude-files
1127     gnus-newsgroup-history gnus-newsgroup-ancient
1128     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1129     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1130     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1131     (gnus-newsgroup-expunged-tally . 0)
1132     gnus-cache-removable-articles gnus-newsgroup-cached
1133     gnus-newsgroup-data gnus-newsgroup-data-reverse
1134     gnus-newsgroup-limit gnus-newsgroup-limits
1135     gnus-newsgroup-charset
1136     gnus-newsgroup-incorporated)
1137   "Variables that are buffer-local to the summary buffers.")
1138
1139 ;; Byte-compiler warning.
1140 (defvar gnus-article-mode-map)
1141
1142 ;; Subject simplification.
1143
1144 (defun gnus-simplify-whitespace (str)
1145   "Remove excessive whitespace."
1146   (let ((mystr str))
1147     ;; Multiple spaces.
1148     (while (string-match "[ \t][ \t]+" mystr)
1149       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1150                           " "
1151                           (substring mystr (match-end 0)))))
1152     ;; Leading spaces.
1153     (when (string-match "^[ \t]+" mystr)
1154       (setq mystr (substring mystr (match-end 0))))
1155     ;; Trailing spaces.
1156     (when (string-match "[ \t]+$" mystr)
1157       (setq mystr (substring mystr 0 (match-beginning 0))))
1158     mystr))
1159
1160 (defsubst gnus-simplify-subject-re (subject)
1161   "Remove \"Re:\" from subject lines."
1162   (if (string-match "^[Rr][Ee]: *" subject)
1163       (substring subject (match-end 0))
1164     subject))
1165
1166 (defun gnus-simplify-subject (subject &optional re-only)
1167   "Remove `Re:' and words in parentheses.
1168 If RE-ONLY is non-nil, strip leading `Re:'s only."
1169   (let ((case-fold-search t))           ;Ignore case.
1170     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1171     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1172       (setq subject (substring subject (match-end 0))))
1173     ;; Remove uninteresting prefixes.
1174     (when (and (not re-only)
1175                gnus-simplify-ignored-prefixes
1176                (string-match gnus-simplify-ignored-prefixes subject))
1177       (setq subject (substring subject (match-end 0))))
1178     ;; Remove words in parentheses from end.
1179     (unless re-only
1180       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1181         (setq subject (substring subject 0 (match-beginning 0)))))
1182     ;; Return subject string.
1183     subject))
1184
1185 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1186 ;; all whitespace.
1187 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1188   (goto-char (point-min))
1189   (while (re-search-forward regexp nil t)
1190       (replace-match (or newtext ""))))
1191
1192 (defun gnus-simplify-buffer-fuzzy ()
1193   "Simplify string in the buffer fuzzily.
1194 The string in the accessible portion of the current buffer is simplified.
1195 It is assumed to be a single-line subject.
1196 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1197 matter is removed.  Additional things can be deleted by setting
1198 gnus-simplify-subject-fuzzy-regexp."
1199   (let ((case-fold-search t)
1200         (modified-tick))
1201     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1202
1203     (while (not (eq modified-tick (buffer-modified-tick)))
1204       (setq modified-tick (buffer-modified-tick))
1205       (cond
1206        ((listp gnus-simplify-subject-fuzzy-regexp)
1207         (mapcar 'gnus-simplify-buffer-fuzzy-step
1208                 gnus-simplify-subject-fuzzy-regexp))
1209        (gnus-simplify-subject-fuzzy-regexp
1210         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1211       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1212       (gnus-simplify-buffer-fuzzy-step
1213        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1214       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1215
1216     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1217     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1218     (gnus-simplify-buffer-fuzzy-step " $")
1219     (gnus-simplify-buffer-fuzzy-step "^ +")))
1220
1221 (defun gnus-simplify-subject-fuzzy (subject)
1222   "Simplify a subject string fuzzily.
1223 See `gnus-simplify-buffer-fuzzy' for details."
1224   (save-excursion
1225     (gnus-set-work-buffer)
1226     (let ((case-fold-search t))
1227       ;; Remove uninteresting prefixes.
1228       (when (and gnus-simplify-ignored-prefixes
1229                  (string-match gnus-simplify-ignored-prefixes subject))
1230         (setq subject (substring subject (match-end 0))))
1231       (insert subject)
1232       (inline (gnus-simplify-buffer-fuzzy))
1233       (buffer-string))))
1234
1235 (defsubst gnus-simplify-subject-fully (subject)
1236   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1237   (cond
1238    (gnus-simplify-subject-functions
1239     (gnus-map-function gnus-simplify-subject-functions subject))
1240    ((null gnus-summary-gather-subject-limit)
1241     (gnus-simplify-subject-re subject))
1242    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1243     (gnus-simplify-subject-fuzzy subject))
1244    ((numberp gnus-summary-gather-subject-limit)
1245     (gnus-limit-string (gnus-simplify-subject-re subject)
1246                        gnus-summary-gather-subject-limit))
1247    (t
1248     subject)))
1249
1250 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1251   "Check whether two subjects are equal.
1252 If optional argument simple-first is t, first argument is already
1253 simplified."
1254   (cond
1255    ((null simple-first)
1256     (equal (gnus-simplify-subject-fully s1)
1257            (gnus-simplify-subject-fully s2)))
1258    (t
1259     (equal s1
1260            (gnus-simplify-subject-fully s2)))))
1261
1262 (defun gnus-summary-bubble-group ()
1263   "Increase the score of the current group.
1264 This is a handy function to add to `gnus-summary-exit-hook' to
1265 increase the score of each group you read."
1266   (gnus-group-add-score gnus-newsgroup-name))
1267
1268 \f
1269 ;;;
1270 ;;; Gnus summary mode
1271 ;;;
1272
1273 (put 'gnus-summary-mode 'mode-class 'special)
1274
1275 (when t
1276   ;; Non-orthogonal keys
1277
1278   (gnus-define-keys gnus-summary-mode-map
1279     " " gnus-summary-next-page
1280     "\177" gnus-summary-prev-page
1281     [delete] gnus-summary-prev-page
1282     [backspace] gnus-summary-prev-page
1283     "\r" gnus-summary-scroll-up
1284     "\M-\r" gnus-summary-scroll-down
1285     "n" gnus-summary-next-unread-article
1286     "p" gnus-summary-prev-unread-article
1287     "N" gnus-summary-next-article
1288     "P" gnus-summary-prev-article
1289     "\M-\C-n" gnus-summary-next-same-subject
1290     "\M-\C-p" gnus-summary-prev-same-subject
1291     "\M-n" gnus-summary-next-unread-subject
1292     "\M-p" gnus-summary-prev-unread-subject
1293     "." gnus-summary-first-unread-article
1294     "," gnus-summary-best-unread-article
1295     "\M-s" gnus-summary-search-article-forward
1296     "\M-r" gnus-summary-search-article-backward
1297     "<" gnus-summary-beginning-of-article
1298     ">" gnus-summary-end-of-article
1299     "j" gnus-summary-goto-article
1300     "^" gnus-summary-refer-parent-article
1301     "\M-^" gnus-summary-refer-article
1302     "u" gnus-summary-tick-article-forward
1303     "!" gnus-summary-tick-article-forward
1304     "U" gnus-summary-tick-article-backward
1305     "d" gnus-summary-mark-as-read-forward
1306     "D" gnus-summary-mark-as-read-backward
1307     "E" gnus-summary-mark-as-expirable
1308     "\M-u" gnus-summary-clear-mark-forward
1309     "\M-U" gnus-summary-clear-mark-backward
1310     "k" gnus-summary-kill-same-subject-and-select
1311     "\C-k" gnus-summary-kill-same-subject
1312     "\M-\C-k" gnus-summary-kill-thread
1313     "\M-\C-l" gnus-summary-lower-thread
1314     "e" gnus-summary-edit-article
1315     "#" gnus-summary-mark-as-processable
1316     "\M-#" gnus-summary-unmark-as-processable
1317     "\M-\C-t" gnus-summary-toggle-threads
1318     "\M-\C-s" gnus-summary-show-thread
1319     "\M-\C-h" gnus-summary-hide-thread
1320     "\M-\C-f" gnus-summary-next-thread
1321     "\M-\C-b" gnus-summary-prev-thread
1322     "\M-\C-u" gnus-summary-up-thread
1323     "\M-\C-d" gnus-summary-down-thread
1324     "&" gnus-summary-execute-command
1325     "c" gnus-summary-catchup-and-exit
1326     "\C-w" gnus-summary-mark-region-as-read
1327     "\C-t" gnus-summary-toggle-truncation
1328     "?" gnus-summary-mark-as-dormant
1329     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1330     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1331     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1332     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1333     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1334     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1335     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1336     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1337     "=" gnus-summary-expand-window
1338     "\C-x\C-s" gnus-summary-reselect-current-group
1339     "\M-g" gnus-summary-rescan-group
1340     "w" gnus-summary-stop-page-breaking
1341     "\C-c\C-r" gnus-summary-caesar-message
1342     "\M-t" gnus-summary-toggle-mime
1343     "f" gnus-summary-followup
1344     "F" gnus-summary-followup-with-original
1345     "C" gnus-summary-cancel-article
1346     "r" gnus-summary-reply
1347     "R" gnus-summary-reply-with-original
1348     "\C-c\C-f" gnus-summary-mail-forward
1349     "o" gnus-summary-save-article
1350     "\C-o" gnus-summary-save-article-mail
1351     "|" gnus-summary-pipe-output
1352     "\M-k" gnus-summary-edit-local-kill
1353     "\M-K" gnus-summary-edit-global-kill
1354     ;; "V" gnus-version
1355     "\C-c\C-d" gnus-summary-describe-group
1356     "q" gnus-summary-exit
1357     "Q" gnus-summary-exit-no-update
1358     "\C-c\C-i" gnus-info-find-node
1359     gnus-mouse-2 gnus-mouse-pick-article
1360     "m" gnus-summary-mail-other-window
1361     "a" gnus-summary-post-news
1362     "x" gnus-summary-limit-to-unread
1363     "s" gnus-summary-isearch-article
1364     "t" gnus-article-toggle-headers
1365     "g" gnus-summary-show-article
1366     "l" gnus-summary-goto-last-article
1367     "v" gnus-summary-preview-mime-message
1368     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1369     "\C-d" gnus-summary-enter-digest-group
1370     "\M-\C-d" gnus-summary-read-document
1371     "\M-\C-e" gnus-summary-edit-parameters
1372     "\M-\C-g" gnus-summary-customize-parameters
1373     "\C-c\C-b" gnus-bug
1374     "*" gnus-cache-enter-article
1375     "\M-*" gnus-cache-remove-article
1376     "\M-&" gnus-summary-universal-argument
1377     "\C-l" gnus-recenter
1378     "I" gnus-summary-increase-score
1379     "L" gnus-summary-lower-score
1380     "\M-i" gnus-symbolic-argument
1381     "h" gnus-summary-select-article-buffer
1382
1383     "V" gnus-summary-score-map
1384     "X" gnus-uu-extract-map
1385     "S" gnus-summary-send-map)
1386
1387   ;; Sort of orthogonal keymap
1388   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1389     "t" gnus-summary-tick-article-forward
1390     "!" gnus-summary-tick-article-forward
1391     "d" gnus-summary-mark-as-read-forward
1392     "r" gnus-summary-mark-as-read-forward
1393     "c" gnus-summary-clear-mark-forward
1394     " " gnus-summary-clear-mark-forward
1395     "e" gnus-summary-mark-as-expirable
1396     "x" gnus-summary-mark-as-expirable
1397     "?" gnus-summary-mark-as-dormant
1398     "b" gnus-summary-set-bookmark
1399     "B" gnus-summary-remove-bookmark
1400     "#" gnus-summary-mark-as-processable
1401     "\M-#" gnus-summary-unmark-as-processable
1402     "S" gnus-summary-limit-include-expunged
1403     "C" gnus-summary-catchup
1404     "H" gnus-summary-catchup-to-here
1405     "\C-c" gnus-summary-catchup-all
1406     "k" gnus-summary-kill-same-subject-and-select
1407     "K" gnus-summary-kill-same-subject
1408     "P" gnus-uu-mark-map)
1409
1410   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1411     "c" gnus-summary-clear-above
1412     "u" gnus-summary-tick-above
1413     "m" gnus-summary-mark-above
1414     "k" gnus-summary-kill-below)
1415
1416   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1417     "/" gnus-summary-limit-to-subject
1418     "n" gnus-summary-limit-to-articles
1419     "w" gnus-summary-pop-limit
1420     "s" gnus-summary-limit-to-subject
1421     "a" gnus-summary-limit-to-author
1422     "u" gnus-summary-limit-to-unread
1423     "m" gnus-summary-limit-to-marks
1424     "M" gnus-summary-limit-exclude-marks
1425     "v" gnus-summary-limit-to-score
1426     "*" gnus-summary-limit-include-cached
1427     "D" gnus-summary-limit-include-dormant
1428     "T" gnus-summary-limit-include-thread
1429     "d" gnus-summary-limit-exclude-dormant
1430     "t" gnus-summary-limit-to-age
1431     "x" gnus-summary-limit-to-extra 
1432     "E" gnus-summary-limit-include-expunged
1433     "c" gnus-summary-limit-exclude-childless-dormant
1434     "C" gnus-summary-limit-mark-excluded-as-read)
1435
1436   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1437     "n" gnus-summary-next-unread-article
1438     "p" gnus-summary-prev-unread-article
1439     "N" gnus-summary-next-article
1440     "P" gnus-summary-prev-article
1441     "\C-n" gnus-summary-next-same-subject
1442     "\C-p" gnus-summary-prev-same-subject
1443     "\M-n" gnus-summary-next-unread-subject
1444     "\M-p" gnus-summary-prev-unread-subject
1445     "f" gnus-summary-first-unread-article
1446     "b" gnus-summary-best-unread-article
1447     "j" gnus-summary-goto-article
1448     "g" gnus-summary-goto-subject
1449     "l" gnus-summary-goto-last-article
1450     "o" gnus-summary-pop-article)
1451
1452   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1453     "k" gnus-summary-kill-thread
1454     "l" gnus-summary-lower-thread
1455     "i" gnus-summary-raise-thread
1456     "T" gnus-summary-toggle-threads
1457     "t" gnus-summary-rethread-current
1458     "^" gnus-summary-reparent-thread
1459     "s" gnus-summary-show-thread
1460     "S" gnus-summary-show-all-threads
1461     "h" gnus-summary-hide-thread
1462     "H" gnus-summary-hide-all-threads
1463     "n" gnus-summary-next-thread
1464     "p" gnus-summary-prev-thread
1465     "u" gnus-summary-up-thread
1466     "o" gnus-summary-top-thread
1467     "d" gnus-summary-down-thread
1468     "#" gnus-uu-mark-thread
1469     "\M-#" gnus-uu-unmark-thread)
1470
1471   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1472     "g" gnus-summary-prepare
1473     "c" gnus-summary-insert-cached-articles)
1474
1475   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1476     "c" gnus-summary-catchup-and-exit
1477     "C" gnus-summary-catchup-all-and-exit
1478     "E" gnus-summary-exit-no-update
1479     "J" gnus-summary-jump-to-other-group
1480     "Q" gnus-summary-exit
1481     "Z" gnus-summary-exit
1482     "n" gnus-summary-catchup-and-goto-next-group
1483     "R" gnus-summary-reselect-current-group
1484     "G" gnus-summary-rescan-group
1485     "N" gnus-summary-next-group
1486     "s" gnus-summary-save-newsrc
1487     "P" gnus-summary-prev-group)
1488
1489   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1490     " " gnus-summary-next-page
1491     "n" gnus-summary-next-page
1492     "\177" gnus-summary-prev-page
1493     [delete] gnus-summary-prev-page
1494     "p" gnus-summary-prev-page
1495     "\r" gnus-summary-scroll-up
1496     "\M-\r" gnus-summary-scroll-down
1497     "<" gnus-summary-beginning-of-article
1498     ">" gnus-summary-end-of-article
1499     "b" gnus-summary-beginning-of-article
1500     "e" gnus-summary-end-of-article
1501     "^" gnus-summary-refer-parent-article
1502     "r" gnus-summary-refer-parent-article
1503     "D" gnus-summary-enter-digest-group
1504     "R" gnus-summary-refer-references
1505     "T" gnus-summary-refer-thread
1506     "g" gnus-summary-show-article
1507     "s" gnus-summary-isearch-article
1508     "P" gnus-summary-print-article
1509     "t" gnus-article-babel)
1510
1511   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1512     "b" gnus-article-add-buttons
1513     "B" gnus-article-add-buttons-to-head
1514     "o" gnus-article-treat-overstrike
1515     "e" gnus-article-emphasize
1516     "w" gnus-article-fill-cited-article
1517     "Q" gnus-article-fill-long-lines
1518     "C" gnus-article-capitalize-sentences
1519     "c" gnus-article-remove-cr
1520     "f" gnus-article-display-x-face
1521     "l" gnus-summary-stop-page-breaking
1522     "r" gnus-summary-caesar-message
1523     "t" gnus-article-toggle-headers
1524     "v" gnus-summary-verbose-headers
1525     "m" gnus-summary-toggle-mime
1526     "H" gnus-article-strip-headers-in-body
1527     "d" gnus-article-treat-dumbquotes)
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              ("Output"
1695               ["Save in default format" gnus-summary-save-article t]
1696               ["Save in file" gnus-summary-save-article-file t]
1697               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1698               ["Save in MH folder" gnus-summary-save-article-folder t]
1699               ["Save in VM folder" gnus-summary-save-article-vm t]
1700               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1701               ["Save body in file" gnus-summary-save-article-body-file t]
1702               ["Pipe through a filter" gnus-summary-pipe-output t]
1703               ["Add to SOUP packet" gnus-soup-add-article t]
1704               ["Print" gnus-summary-print-article t])
1705              ("Backend"
1706               ["Respool article..." gnus-summary-respool-article t]
1707               ["Move article..." gnus-summary-move-article
1708                (gnus-check-backend-function
1709                 'request-move-article gnus-newsgroup-name)]
1710               ["Copy article..." gnus-summary-copy-article t]
1711               ["Crosspost article..." gnus-summary-crosspost-article
1712                (gnus-check-backend-function
1713                 'request-replace-article gnus-newsgroup-name)]
1714               ["Import file..." gnus-summary-import-article t]
1715               ["Check if posted" gnus-summary-article-posted-p t]
1716               ["Edit article" gnus-summary-edit-article
1717                (not (gnus-group-read-only-p))]
1718               ["Delete article" gnus-summary-delete-article
1719                (gnus-check-backend-function
1720                 'request-expire-articles gnus-newsgroup-name)]
1721               ["Query respool" gnus-summary-respool-query t]
1722               ["Trace respool" gnus-summary-respool-trace t]
1723               ["Delete expirable articles" gnus-summary-expire-articles-now
1724                (gnus-check-backend-function
1725                 'request-expire-articles gnus-newsgroup-name)])
1726              ("Extract"
1727               ["Uudecode" gnus-uu-decode-uu t]
1728               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1729               ["Unshar" gnus-uu-decode-unshar t]
1730               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1731               ["Save" gnus-uu-decode-save t]
1732               ["Binhex" gnus-uu-decode-binhex t]
1733               ["Postscript" gnus-uu-decode-postscript t])
1734              ("Cache"
1735               ["Enter article" gnus-cache-enter-article t]
1736               ["Remove article" gnus-cache-remove-article t])
1737              ["Translate" gnus-article-babel t]
1738              ["Select article buffer" gnus-summary-select-article-buffer t]
1739              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1740              ["Isearch article..." gnus-summary-isearch-article t]
1741              ["Beginning of the article" gnus-summary-beginning-of-article t]
1742              ["End of the article" gnus-summary-end-of-article t]
1743              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1744              ["Fetch referenced articles" gnus-summary-refer-references t]
1745              ["Fetch current thread" gnus-summary-refer-thread t]
1746              ["Fetch article with id..." gnus-summary-refer-article t]
1747              ["Redisplay" gnus-summary-show-article t])))
1748       (easy-menu-define
1749        gnus-summary-article-menu gnus-summary-mode-map ""
1750        (cons "Article" innards))
1751
1752       (easy-menu-define
1753        gnus-article-commands-menu gnus-article-mode-map ""
1754        (cons "Commands" innards)))
1755
1756     (easy-menu-define
1757      gnus-summary-thread-menu gnus-summary-mode-map ""
1758      '("Threads"
1759        ["Toggle threading" gnus-summary-toggle-threads t]
1760        ["Hide threads" gnus-summary-hide-all-threads t]
1761        ["Show threads" gnus-summary-show-all-threads t]
1762        ["Hide thread" gnus-summary-hide-thread t]
1763        ["Show thread" gnus-summary-show-thread t]
1764        ["Go to next thread" gnus-summary-next-thread t]
1765        ["Go to previous thread" gnus-summary-prev-thread t]
1766        ["Go down thread" gnus-summary-down-thread t]
1767        ["Go up thread" gnus-summary-up-thread t]
1768        ["Top of thread" gnus-summary-top-thread t]
1769        ["Mark thread as read" gnus-summary-kill-thread t]
1770        ["Lower thread score" gnus-summary-lower-thread t]
1771        ["Raise thread score" gnus-summary-raise-thread t]
1772        ["Rethread current" gnus-summary-rethread-current t]
1773        ))
1774
1775     (easy-menu-define
1776      gnus-summary-post-menu gnus-summary-mode-map ""
1777      '("Post"
1778        ["Post an article" gnus-summary-post-news t]
1779        ["Followup" gnus-summary-followup t]
1780        ["Followup and yank" gnus-summary-followup-with-original t]
1781        ["Supersede article" gnus-summary-supersede-article t]
1782        ["Cancel article" gnus-summary-cancel-article t]
1783        ["Reply" gnus-summary-reply t]
1784        ["Reply and yank" gnus-summary-reply-with-original t]
1785        ["Wide reply" gnus-summary-wide-reply t]
1786        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1787        ["Mail forward" gnus-summary-mail-forward t]
1788        ["Post forward" gnus-summary-post-forward t]
1789        ["Digest and mail" gnus-summary-mail-digest t]
1790        ["Digest and post" gnus-summary-post-digest t]
1791        ["Resend message" gnus-summary-resend-message t]
1792        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1793        ["Send a mail" gnus-summary-mail-other-window t]
1794        ["Uuencode and post" gnus-uu-post-news t]
1795        ["Followup via news" gnus-summary-followup-to-mail t]
1796        ["Followup via news and yank"
1797         gnus-summary-followup-to-mail-with-original t]
1798        ;;("Draft"
1799        ;;["Send" gnus-summary-send-draft t]
1800        ;;["Send bounced" gnus-resend-bounced-mail t])
1801        ))
1802
1803     (easy-menu-define
1804      gnus-summary-misc-menu gnus-summary-mode-map ""
1805      '("Misc"
1806        ("Mark Read"
1807         ["Mark as read" gnus-summary-mark-as-read-forward t]
1808         ["Mark same subject and select"
1809          gnus-summary-kill-same-subject-and-select t]
1810         ["Mark same subject" gnus-summary-kill-same-subject t]
1811         ["Catchup" gnus-summary-catchup t]
1812         ["Catchup all" gnus-summary-catchup-all t]
1813         ["Catchup to here" gnus-summary-catchup-to-here t]
1814         ["Catchup region" gnus-summary-mark-region-as-read t]
1815         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1816        ("Mark Various"
1817         ["Tick" gnus-summary-tick-article-forward t]
1818         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1819         ["Remove marks" gnus-summary-clear-mark-forward t]
1820         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1821         ["Set bookmark" gnus-summary-set-bookmark t]
1822         ["Remove bookmark" gnus-summary-remove-bookmark t])
1823        ("Mark Limit"
1824         ["Marks..." gnus-summary-limit-to-marks t]
1825         ["Subject..." gnus-summary-limit-to-subject t]
1826         ["Author..." gnus-summary-limit-to-author t]
1827         ["Age..." gnus-summary-limit-to-age t]
1828         ["Extra..." gnus-summary-limit-to-extra t]
1829         ["Score" gnus-summary-limit-to-score t]
1830         ["Unread" gnus-summary-limit-to-unread t]
1831         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1832         ["Articles" gnus-summary-limit-to-articles t]
1833         ["Pop limit" gnus-summary-pop-limit t]
1834         ["Show dormant" gnus-summary-limit-include-dormant t]
1835         ["Hide childless dormant"
1836          gnus-summary-limit-exclude-childless-dormant t]
1837         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1838         ["Hide marked" gnus-summary-limit-exclude-marks t]
1839         ["Show expunged" gnus-summary-show-all-expunged t])
1840        ("Process Mark"
1841         ["Set mark" gnus-summary-mark-as-processable t]
1842         ["Remove mark" gnus-summary-unmark-as-processable t]
1843         ["Remove all marks" gnus-summary-unmark-all-processable t]
1844         ["Mark above" gnus-uu-mark-over t]
1845         ["Mark series" gnus-uu-mark-series t]
1846         ["Mark region" gnus-uu-mark-region t]
1847         ["Unmark region" gnus-uu-unmark-region t]
1848         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1849         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1850         ["Mark all" gnus-uu-mark-all t]
1851         ["Mark buffer" gnus-uu-mark-buffer t]
1852         ["Mark sparse" gnus-uu-mark-sparse t]
1853         ["Mark thread" gnus-uu-mark-thread t]
1854         ["Unmark thread" gnus-uu-unmark-thread t]
1855         ("Process Mark Sets"
1856          ["Kill" gnus-summary-kill-process-mark t]
1857          ["Yank" gnus-summary-yank-process-mark
1858           gnus-newsgroup-process-stack]
1859          ["Save" gnus-summary-save-process-mark t]))
1860        ("Scroll article"
1861         ["Page forward" gnus-summary-next-page t]
1862         ["Page backward" gnus-summary-prev-page t]
1863         ["Line forward" gnus-summary-scroll-up t])
1864        ("Move"
1865         ["Next unread article" gnus-summary-next-unread-article t]
1866         ["Previous unread article" gnus-summary-prev-unread-article t]
1867         ["Next article" gnus-summary-next-article t]
1868         ["Previous article" gnus-summary-prev-article t]
1869         ["Next unread subject" gnus-summary-next-unread-subject t]
1870         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1871         ["Next article same subject" gnus-summary-next-same-subject t]
1872         ["Previous article same subject" gnus-summary-prev-same-subject t]
1873         ["First unread article" gnus-summary-first-unread-article t]
1874         ["Best unread article" gnus-summary-best-unread-article t]
1875         ["Go to subject number..." gnus-summary-goto-subject t]
1876         ["Go to article number..." gnus-summary-goto-article t]
1877         ["Go to the last article" gnus-summary-goto-last-article t]
1878         ["Pop article off history" gnus-summary-pop-article t])
1879        ("Sort"
1880         ["Sort by number" gnus-summary-sort-by-number t]
1881         ["Sort by author" gnus-summary-sort-by-author t]
1882         ["Sort by subject" gnus-summary-sort-by-subject t]
1883         ["Sort by date" gnus-summary-sort-by-date t]
1884         ["Sort by score" gnus-summary-sort-by-score t]
1885         ["Sort by lines" gnus-summary-sort-by-lines t]
1886         ["Sort by characters" gnus-summary-sort-by-chars t])
1887        ("Help"
1888         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1889         ["Describe group" gnus-summary-describe-group t]
1890         ["Read manual" gnus-info-find-node t])
1891        ("Modes"
1892         ["Pick and read" gnus-pick-mode t]
1893         ["Binary" gnus-binary-mode t])
1894        ("Regeneration"
1895         ["Regenerate" gnus-summary-prepare t]
1896         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1897         ["Toggle threading" gnus-summary-toggle-threads t])
1898        ["Filter articles..." gnus-summary-execute-command t]
1899        ["Run command on subjects..." gnus-summary-universal-argument t]
1900        ["Search articles forward..." gnus-summary-search-article-forward t]
1901        ["Search articles backward..." gnus-summary-search-article-backward t]
1902        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1903        ["Expand window" gnus-summary-expand-window t]
1904        ["Expire expirable articles" gnus-summary-expire-articles
1905         (gnus-check-backend-function
1906          'request-expire-articles gnus-newsgroup-name)]
1907        ["Edit local kill file" gnus-summary-edit-local-kill t]
1908        ["Edit main kill file" gnus-summary-edit-global-kill t]
1909        ["Edit group parameters" gnus-summary-edit-parameters t]
1910        ["Customize group parameters" gnus-summary-customize-parameters t]
1911        ["Send a bug report" gnus-bug t]
1912        ("Exit"
1913         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1914         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1915         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1916         ["Exit group" gnus-summary-exit t]
1917         ["Exit group without updating" gnus-summary-exit-no-update t]
1918         ["Exit and goto next group" gnus-summary-next-group t]
1919         ["Exit and goto prev group" gnus-summary-prev-group t]
1920         ["Reselect group" gnus-summary-reselect-current-group t]
1921         ["Rescan group" gnus-summary-rescan-group t]
1922         ["Update dribble" gnus-summary-save-newsrc t])))
1923
1924     (gnus-run-hooks 'gnus-summary-menu-hook)))
1925
1926 (defun gnus-score-set-default (var value)
1927   "A version of set that updates the GNU Emacs menu-bar."
1928   (set var value)
1929   ;; It is the message that forces the active status to be updated.
1930   (message ""))
1931
1932 (defun gnus-make-score-map (type)
1933   "Make a summary score map of type TYPE."
1934   (if t
1935       nil
1936     (let ((headers '(("author" "from" string)
1937                      ("subject" "subject" string)
1938                      ("article body" "body" string)
1939                      ("article head" "head" string)
1940                      ("xref" "xref" string)
1941                      ("extra header" "extra" string)
1942                      ("lines" "lines" number)
1943                      ("followups to author" "followup" string)))
1944           (types '((number ("less than" <)
1945                            ("greater than" >)
1946                            ("equal" =))
1947                    (string ("substring" s)
1948                            ("exact string" e)
1949                            ("fuzzy string" f)
1950                            ("regexp" r))))
1951           (perms '(("temporary" (current-time-string))
1952                    ("permanent" nil)
1953                    ("immediate" now)))
1954           header)
1955       (list
1956        (apply
1957         'nconc
1958         (list
1959          (if (eq type 'lower)
1960              "Lower score"
1961            "Increase score"))
1962         (let (outh)
1963           (while headers
1964             (setq header (car headers))
1965             (setq outh
1966                   (cons
1967                    (apply
1968                     'nconc
1969                     (list (car header))
1970                     (let ((ts (cdr (assoc (nth 2 header) types)))
1971                           outt)
1972                       (while ts
1973                         (setq outt
1974                               (cons
1975                                (apply
1976                                 'nconc
1977                                 (list (caar ts))
1978                                 (let ((ps perms)
1979                                       outp)
1980                                   (while ps
1981                                     (setq outp
1982                                           (cons
1983                                            (vector
1984                                             (caar ps)
1985                                             (list
1986                                              'gnus-summary-score-entry
1987                                              (nth 1 header)
1988                                              (if (or (string= (nth 1 header)
1989                                                               "head")
1990                                                      (string= (nth 1 header)
1991                                                               "body"))
1992                                                  ""
1993                                                (list 'gnus-summary-header
1994                                                      (nth 1 header)))
1995                                              (list 'quote (nth 1 (car ts)))
1996                                              (list 'gnus-score-default nil)
1997                                              (nth 1 (car ps))
1998                                              t)
1999                                             t)
2000                                            outp))
2001                                     (setq ps (cdr ps)))
2002                                   (list (nreverse outp))))
2003                                outt))
2004                         (setq ts (cdr ts)))
2005                       (list (nreverse outt))))
2006                    outh))
2007             (setq headers (cdr headers)))
2008           (list (nreverse outh))))))))
2009
2010 \f
2011
2012 (defun gnus-summary-mode (&optional group)
2013   "Major mode for reading articles.
2014
2015 All normal editing commands are switched off.
2016 \\<gnus-summary-mode-map>
2017 Each line in this buffer represents one article.  To read an
2018 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2019 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2020 respectively.
2021
2022 You can also post articles and send mail from this buffer.  To
2023 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2024 of an article, type `\\[gnus-summary-reply]'.
2025
2026 There are approx. one gazillion commands you can execute in this
2027 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2028
2029 The following commands are available:
2030
2031 \\{gnus-summary-mode-map}"
2032   (interactive)
2033   (when (gnus-visual-p 'summary-menu 'menu)
2034     (gnus-summary-make-menu-bar))
2035   (kill-all-local-variables)
2036   (gnus-summary-make-local-variables)
2037   (gnus-make-thread-indent-array)
2038   (gnus-simplify-mode-line)
2039   (setq major-mode 'gnus-summary-mode)
2040   (setq mode-name "Summary")
2041   (make-local-variable 'minor-mode-alist)
2042   (use-local-map gnus-summary-mode-map)
2043   (buffer-disable-undo)
2044   (setq buffer-read-only t)             ;Disable modification
2045   (setq truncate-lines t)
2046   (setq selective-display t)
2047   (setq selective-display-ellipses t)   ;Display `...'
2048   (gnus-summary-set-display-table)
2049   (gnus-set-default-directory)
2050   (setq gnus-newsgroup-name group)
2051   (unless (gnus-news-group-p group)
2052     (setq gnus-newsgroup-incorporated
2053           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2054   (make-local-variable 'gnus-summary-line-format)
2055   (make-local-variable 'gnus-summary-line-format-spec)
2056   (make-local-variable 'gnus-summary-dummy-line-format)
2057   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2058   (make-local-variable 'gnus-summary-mark-positions)
2059   (make-local-hook 'pre-command-hook)
2060   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2061   (gnus-run-hooks 'gnus-summary-mode-hook)
2062   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2063   (gnus-update-summary-mark-positions))
2064
2065 (defun gnus-summary-make-local-variables ()
2066   "Make all the local summary buffer variables."
2067   (let (global)
2068     (dolist (local gnus-summary-local-variables)
2069       (if (consp local)
2070           (progn
2071             (if (eq (cdr local) 'global)
2072                 ;; Copy the global value of the variable.
2073                 (setq global (symbol-value (car local)))
2074               ;; Use the value from the list.
2075               (setq global (eval (cdr local))))
2076             (set (make-local-variable (car local)) global))
2077         ;; Simple nil-valued local variable.
2078         (set (make-local-variable local) nil)))))
2079
2080 (defun gnus-summary-clear-local-variables ()
2081   (let ((locals gnus-summary-local-variables))
2082     (while locals
2083       (if (consp (car locals))
2084           (and (vectorp (caar locals))
2085                (set (caar locals) nil))
2086         (and (vectorp (car locals))
2087              (set (car locals) nil)))
2088       (setq locals (cdr locals)))))
2089
2090 ;; Summary data functions.
2091
2092 (defmacro gnus-data-number (data)
2093   `(car ,data))
2094
2095 (defmacro gnus-data-set-number (data number)
2096   `(setcar ,data ,number))
2097
2098 (defmacro gnus-data-mark (data)
2099   `(nth 1 ,data))
2100
2101 (defmacro gnus-data-set-mark (data mark)
2102   `(setcar (nthcdr 1 ,data) ,mark))
2103
2104 (defmacro gnus-data-pos (data)
2105   `(nth 2 ,data))
2106
2107 (defmacro gnus-data-set-pos (data pos)
2108   `(setcar (nthcdr 2 ,data) ,pos))
2109
2110 (defmacro gnus-data-header (data)
2111   `(nth 3 ,data))
2112
2113 (defmacro gnus-data-set-header (data header)
2114   `(setf (nth 3 ,data) ,header))
2115
2116 (defmacro gnus-data-level (data)
2117   `(nth 4 ,data))
2118
2119 (defmacro gnus-data-unread-p (data)
2120   `(= (nth 1 ,data) gnus-unread-mark))
2121
2122 (defmacro gnus-data-read-p (data)
2123   `(/= (nth 1 ,data) gnus-unread-mark))
2124
2125 (defmacro gnus-data-pseudo-p (data)
2126   `(consp (nth 3 ,data)))
2127
2128 (defmacro gnus-data-find (number)
2129   `(assq ,number gnus-newsgroup-data))
2130
2131 (defmacro gnus-data-find-list (number &optional data)
2132   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2133      (memq (assq ,number bdata)
2134            bdata)))
2135
2136 (defmacro gnus-data-make (number mark pos header level)
2137   `(list ,number ,mark ,pos ,header ,level))
2138
2139 (defun gnus-data-enter (after-article number mark pos header level offset)
2140   (let ((data (gnus-data-find-list after-article)))
2141     (unless data
2142       (error "No such article: %d" after-article))
2143     (setcdr data (cons (gnus-data-make number mark pos header level)
2144                        (cdr data)))
2145     (setq gnus-newsgroup-data-reverse nil)
2146     (gnus-data-update-list (cddr data) offset)))
2147
2148 (defun gnus-data-enter-list (after-article list &optional offset)
2149   (when list
2150     (let ((data (and after-article (gnus-data-find-list after-article)))
2151           (ilist list))
2152       (if (not (or data
2153                    after-article))
2154           (let ((odata gnus-newsgroup-data))
2155             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2156             (when offset
2157               (gnus-data-update-list odata offset)))
2158         ;; Find the last element in the list to be spliced into the main
2159         ;; list.
2160         (while (cdr list)
2161           (setq list (cdr list)))
2162         (if (not data)
2163             (progn
2164               (setcdr list gnus-newsgroup-data)
2165               (setq gnus-newsgroup-data ilist)
2166               (when offset
2167                 (gnus-data-update-list (cdr list) offset)))
2168           (setcdr list (cdr data))
2169           (setcdr data ilist)
2170           (when offset
2171             (gnus-data-update-list (cdr list) offset))))
2172       (setq gnus-newsgroup-data-reverse nil))))
2173
2174 (defun gnus-data-remove (article &optional offset)
2175   (let ((data gnus-newsgroup-data))
2176     (if (= (gnus-data-number (car data)) article)
2177         (progn
2178           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2179                 gnus-newsgroup-data-reverse nil)
2180           (when offset
2181             (gnus-data-update-list gnus-newsgroup-data offset)))
2182       (while (cdr data)
2183         (when (= (gnus-data-number (cadr data)) article)
2184           (setcdr data (cddr data))
2185           (when offset
2186             (gnus-data-update-list (cdr data) offset))
2187           (setq data nil
2188                 gnus-newsgroup-data-reverse nil))
2189         (setq data (cdr data))))))
2190
2191 (defmacro gnus-data-list (backward)
2192   `(if ,backward
2193        (or gnus-newsgroup-data-reverse
2194            (setq gnus-newsgroup-data-reverse
2195                  (reverse gnus-newsgroup-data)))
2196      gnus-newsgroup-data))
2197
2198 (defun gnus-data-update-list (data offset)
2199   "Add OFFSET to the POS of all data entries in DATA."
2200   (setq gnus-newsgroup-data-reverse nil)
2201   (while data
2202     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2203     (setq data (cdr data))))
2204
2205 (defun gnus-summary-article-pseudo-p (article)
2206   "Say whether this article is a pseudo article or not."
2207   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2208
2209 (defmacro gnus-summary-article-sparse-p (article)
2210   "Say whether this article is a sparse article or not."
2211   `(memq ,article gnus-newsgroup-sparse))
2212
2213 (defmacro gnus-summary-article-ancient-p (article)
2214   "Say whether this article is a sparse article or not."
2215   `(memq ,article gnus-newsgroup-ancient))
2216
2217 (defun gnus-article-parent-p (number)
2218   "Say whether this article is a parent or not."
2219   (let ((data (gnus-data-find-list number)))
2220     (and (cdr data)                     ; There has to be an article after...
2221          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2222             (gnus-data-level (nth 1 data))))))
2223
2224 (defun gnus-article-children (number)
2225   "Return a list of all children to NUMBER."
2226   (let* ((data (gnus-data-find-list number))
2227          (level (gnus-data-level (car data)))
2228          children)
2229     (setq data (cdr data))
2230     (while (and data
2231                 (= (gnus-data-level (car data)) (1+ level)))
2232       (push (gnus-data-number (car data)) children)
2233       (setq data (cdr data)))
2234     children))
2235
2236 (defmacro gnus-summary-skip-intangible ()
2237   "If the current article is intangible, then jump to a different article."
2238   '(let ((to (get-text-property (point) 'gnus-intangible)))
2239      (and to (gnus-summary-goto-subject to))))
2240
2241 (defmacro gnus-summary-article-intangible-p ()
2242   "Say whether this article is intangible or not."
2243   '(get-text-property (point) 'gnus-intangible))
2244
2245 (defun gnus-article-read-p (article)
2246   "Say whether ARTICLE is read or not."
2247   (not (or (memq article gnus-newsgroup-marked)
2248            (memq article gnus-newsgroup-unreads)
2249            (memq article gnus-newsgroup-unselected)
2250            (memq article gnus-newsgroup-dormant))))
2251
2252 ;; Some summary mode macros.
2253
2254 (defmacro gnus-summary-article-number ()
2255   "The article number of the article on the current line.
2256 If there isn's an article number here, then we return the current
2257 article number."
2258   '(progn
2259      (gnus-summary-skip-intangible)
2260      (or (get-text-property (point) 'gnus-number)
2261          (gnus-summary-last-subject))))
2262
2263 (defmacro gnus-summary-article-header (&optional number)
2264   "Return the header of article NUMBER."
2265   `(gnus-data-header (gnus-data-find
2266                       ,(or number '(gnus-summary-article-number)))))
2267
2268 (defmacro gnus-summary-thread-level (&optional number)
2269   "Return the level of thread that starts with article NUMBER."
2270   `(if (and (eq gnus-summary-make-false-root 'dummy)
2271             (get-text-property (point) 'gnus-intangible))
2272        0
2273      (gnus-data-level (gnus-data-find
2274                        ,(or number '(gnus-summary-article-number))))))
2275
2276 (defmacro gnus-summary-article-mark (&optional number)
2277   "Return the mark of article NUMBER."
2278   `(gnus-data-mark (gnus-data-find
2279                     ,(or number '(gnus-summary-article-number)))))
2280
2281 (defmacro gnus-summary-article-pos (&optional number)
2282   "Return the position of the line of article NUMBER."
2283   `(gnus-data-pos (gnus-data-find
2284                    ,(or number '(gnus-summary-article-number)))))
2285
2286 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2287 (defmacro gnus-summary-article-subject (&optional number)
2288   "Return current subject string or nil if nothing."
2289   `(let ((headers
2290           ,(if number
2291                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2292              '(gnus-data-header (assq (gnus-summary-article-number)
2293                                       gnus-newsgroup-data)))))
2294      (and headers
2295           (vectorp headers)
2296           (mail-header-subject headers))))
2297
2298 (defmacro gnus-summary-article-score (&optional number)
2299   "Return current article score."
2300   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2301                   gnus-newsgroup-scored))
2302        gnus-summary-default-score 0))
2303
2304 (defun gnus-summary-article-children (&optional number)
2305   "Return a list of article numbers that are children of article NUMBER."
2306   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2307          (level (gnus-data-level (car data)))
2308          l children)
2309     (while (and (setq data (cdr data))
2310                 (> (setq l (gnus-data-level (car data))) level))
2311       (and (= (1+ level) l)
2312            (push (gnus-data-number (car data))
2313                  children)))
2314     (nreverse children)))
2315
2316 (defun gnus-summary-article-parent (&optional number)
2317   "Return the article number of the parent of article NUMBER."
2318   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2319                                     (gnus-data-list t)))
2320          (level (gnus-data-level (car data))))
2321     (if (zerop level)
2322         ()                              ; This is a root.
2323       ;; We search until we find an article with a level less than
2324       ;; this one.  That function has to be the parent.
2325       (while (and (setq data (cdr data))
2326                   (not (< (gnus-data-level (car data)) level))))
2327       (and data (gnus-data-number (car data))))))
2328
2329 (defun gnus-unread-mark-p (mark)
2330   "Say whether MARK is the unread mark."
2331   (= mark gnus-unread-mark))
2332
2333 (defun gnus-read-mark-p (mark)
2334   "Say whether MARK is one of the marks that mark as read.
2335 This is all marks except unread, ticked, dormant, and expirable."
2336   (not (or (= mark gnus-unread-mark)
2337            (= mark gnus-ticked-mark)
2338            (= mark gnus-dormant-mark)
2339            (= mark gnus-expirable-mark))))
2340
2341 (defmacro gnus-article-mark (number)
2342   "Return the MARK of article NUMBER.
2343 This macro should only be used when computing the mark the \"first\"
2344 time; i.e., when generating the summary lines.  After that,
2345 `gnus-summary-article-mark' should be used to examine the
2346 marks of articles."
2347   `(cond
2348     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2349     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2350     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2351     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2352     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2353     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2354     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2355     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2356            gnus-ancient-mark))))
2357
2358 ;; Saving hidden threads.
2359
2360 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2361 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2362
2363 (defmacro gnus-save-hidden-threads (&rest forms)
2364   "Save hidden threads, eval FORMS, and restore the hidden threads."
2365   (let ((config (make-symbol "config")))
2366     `(let ((,config (gnus-hidden-threads-configuration)))
2367        (unwind-protect
2368            (save-excursion
2369              ,@forms)
2370          (gnus-restore-hidden-threads-configuration ,config)))))
2371
2372 (defun gnus-data-compute-positions ()
2373   "Compute the positions of all articles."
2374   (setq gnus-newsgroup-data-reverse nil)
2375   (let ((data gnus-newsgroup-data))
2376     (save-excursion
2377       (gnus-save-hidden-threads
2378         (gnus-summary-show-all-threads)
2379         (goto-char (point-min))
2380         (while data
2381           (while (get-text-property (point) 'gnus-intangible)
2382             (forward-line 1))
2383           (gnus-data-set-pos (car data) (+ (point) 3))
2384           (setq data (cdr data))
2385           (forward-line 1))))))
2386
2387 (defun gnus-hidden-threads-configuration ()
2388   "Return the current hidden threads configuration."
2389   (save-excursion
2390     (let (config)
2391       (goto-char (point-min))
2392       (while (search-forward "\r" nil t)
2393         (push (1- (point)) config))
2394       config)))
2395
2396 (defun gnus-restore-hidden-threads-configuration (config)
2397   "Restore hidden threads configuration from CONFIG."
2398   (let (point buffer-read-only)
2399     (while (setq point (pop config))
2400       (when (and (< point (point-max))
2401                  (goto-char point)
2402                  (eq (char-after) ?\n))
2403         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2404
2405 ;; Various summary mode internalish functions.
2406
2407 (defun gnus-mouse-pick-article (e)
2408   (interactive "e")
2409   (mouse-set-point e)
2410   (gnus-summary-next-page nil t))
2411
2412 (defun gnus-summary-set-display-table ()
2413   ;; Change the display table.  Odd characters have a tendency to mess
2414   ;; up nicely formatted displays - we make all possible glyphs
2415   ;; display only a single character.
2416
2417   ;; We start from the standard display table, if any.
2418   (let ((table (or (copy-sequence standard-display-table)
2419                    (make-display-table)))
2420         (i 32))
2421     ;; Nix out all the control chars...
2422     (while (>= (setq i (1- i)) 0)
2423       (aset table i [??]))
2424     ;; ... but not newline and cr, of course.  (cr is necessary for the
2425     ;; selective display).
2426     (aset table ?\n nil)
2427     (aset table ?\r nil)
2428     ;; We keep TAB as well.
2429     (aset table ?\t nil)
2430     ;; We nix out any glyphs over 126 that are not set already.
2431     (let ((i 256))
2432       (while (>= (setq i (1- i)) 127)
2433         ;; Only modify if the entry is nil.
2434         (unless (aref table i)
2435           (aset table i [??]))))
2436     (setq buffer-display-table table)))
2437
2438 (defun gnus-summary-setup-buffer (group)
2439   "Initialize summary buffer."
2440   (let ((buffer (concat "*Summary " group "*")))
2441     (if (get-buffer buffer)
2442         (progn
2443           (set-buffer buffer)
2444           (setq gnus-summary-buffer (current-buffer))
2445           (not gnus-newsgroup-prepared))
2446       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2447       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2448       (gnus-summary-mode group)
2449       (when gnus-carpal
2450         (gnus-carpal-setup-buffer 'summary))
2451       (unless gnus-single-article-buffer
2452         (make-local-variable 'gnus-article-buffer)
2453         (make-local-variable 'gnus-article-current)
2454         (make-local-variable 'gnus-original-article-buffer))
2455       (setq gnus-newsgroup-name group)
2456       t)))
2457
2458 (defun gnus-set-global-variables ()
2459   ;; Set the global equivalents of the summary buffer-local variables
2460   ;; to the latest values they had.  These reflect the summary buffer
2461   ;; that was in action when the last article was fetched.
2462   (when (eq major-mode 'gnus-summary-mode)
2463     (setq gnus-summary-buffer (current-buffer))
2464     (let ((name gnus-newsgroup-name)
2465           (marked gnus-newsgroup-marked)
2466           (unread gnus-newsgroup-unreads)
2467           (headers gnus-current-headers)
2468           (data gnus-newsgroup-data)
2469           (summary gnus-summary-buffer)
2470           (article-buffer gnus-article-buffer)
2471           (original gnus-original-article-buffer)
2472           (gac gnus-article-current)
2473           (reffed gnus-reffed-article-number)
2474           (score-file gnus-current-score-file)
2475           (default-charset gnus-newsgroup-charset))
2476       (save-excursion
2477         (set-buffer gnus-group-buffer)
2478         (setq gnus-newsgroup-name name
2479               gnus-newsgroup-marked marked
2480               gnus-newsgroup-unreads unread
2481               gnus-current-headers headers
2482               gnus-newsgroup-data data
2483               gnus-article-current gac
2484               gnus-summary-buffer summary
2485               gnus-article-buffer article-buffer
2486               gnus-original-article-buffer original
2487               gnus-reffed-article-number reffed
2488               gnus-current-score-file score-file
2489               gnus-newsgroup-charset default-charset)
2490         ;; The article buffer also has local variables.
2491         (when (gnus-buffer-live-p gnus-article-buffer)
2492           (set-buffer gnus-article-buffer)
2493           (setq gnus-summary-buffer summary))))))
2494
2495 (defun gnus-summary-article-unread-p (article)
2496   "Say whether ARTICLE is unread or not."
2497   (memq article gnus-newsgroup-unreads))
2498
2499 (defun gnus-summary-first-article-p (&optional article)
2500   "Return whether ARTICLE is the first article in the buffer."
2501   (if (not (setq article (or article (gnus-summary-article-number))))
2502       nil
2503     (eq article (caar gnus-newsgroup-data))))
2504
2505 (defun gnus-summary-last-article-p (&optional article)
2506   "Return whether ARTICLE is the last article in the buffer."
2507   (if (not (setq article (or article (gnus-summary-article-number))))
2508       ;; All non-existent numbers are the last article.  :-)
2509       t
2510     (not (cdr (gnus-data-find-list article)))))
2511
2512 (defun gnus-make-thread-indent-array ()
2513   (let ((n 200))
2514     (unless (and gnus-thread-indent-array
2515                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2516       (setq gnus-thread-indent-array (make-vector 201 "")
2517             gnus-thread-indent-array-level gnus-thread-indent-level)
2518       (while (>= n 0)
2519         (aset gnus-thread-indent-array n
2520               (make-string (* n gnus-thread-indent-level) ? ))
2521         (setq n (1- n))))))
2522
2523 (defun gnus-update-summary-mark-positions ()
2524   "Compute where the summary marks are to go."
2525   (save-excursion
2526     (when (gnus-buffer-exists-p gnus-summary-buffer)
2527       (set-buffer gnus-summary-buffer))
2528     (let ((gnus-replied-mark 129)
2529           (gnus-score-below-mark 130)
2530           (gnus-score-over-mark 130)
2531           (gnus-download-mark 131)
2532           (spec gnus-summary-line-format-spec)
2533           gnus-visual pos)
2534       (save-excursion
2535         (gnus-set-work-buffer)
2536         (let ((gnus-summary-line-format-spec spec)
2537               (gnus-newsgroup-downloadable '((0 . t))))
2538           (gnus-summary-insert-line
2539            (make-full-mail-header 0 "" "" "" "" "" 0 0 "" nil)
2540            0 nil 128 t nil "" nil 1)
2541           (goto-char (point-min))
2542           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2543                                              (- (point) 2)))))
2544           (goto-char (point-min))
2545           (push (cons 'replied (and (search-forward "\201" nil t)
2546                                     (- (point) 2)))
2547                 pos)
2548           (goto-char (point-min))
2549           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2550                 pos)
2551           (goto-char (point-min))
2552           (push (cons 'download
2553                       (and (search-forward "\203" nil t) (- (point) 2)))
2554                 pos)))
2555       (setq gnus-summary-mark-positions pos))))
2556
2557 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2558   "Insert a dummy root in the summary buffer."
2559   (beginning-of-line)
2560   (gnus-add-text-properties
2561    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2562    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2563
2564 (defun gnus-summary-from-or-to-or-newsgroups (header)
2565   (let ((to (cdr (assq 'To (mail-header-extra header))))
2566         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2567         (default-mime-charset (with-current-buffer gnus-summary-buffer
2568                                 default-mime-charset)))
2569     (cond
2570      ((and to
2571            gnus-ignored-from-addresses
2572            (string-match gnus-ignored-from-addresses
2573                          (mail-header-from header)))
2574       (concat "-> "
2575               (or (car (funcall gnus-extract-address-components
2576                                 (funcall
2577                                  gnus-decode-encoded-word-function to)))
2578                   (funcall gnus-decode-encoded-word-function to))))
2579      ((and newsgroups
2580            gnus-ignored-from-addresses
2581            (string-match gnus-ignored-from-addresses
2582                          (mail-header-from header)))
2583       (concat "=> " newsgroups))
2584      (t
2585       (or (car (funcall gnus-extract-address-components
2586                         (mail-header-from header)))
2587           (mail-header-from header))))))
2588
2589 (defun gnus-summary-insert-line (gnus-tmp-header
2590                                  gnus-tmp-level gnus-tmp-current
2591                                  gnus-tmp-unread gnus-tmp-replied
2592                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2593                                  &optional gnus-tmp-dummy gnus-tmp-score
2594                                  gnus-tmp-process)
2595   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2596          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2597          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2598          (gnus-tmp-score-char
2599           (if (or (null gnus-summary-default-score)
2600                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2601                       gnus-summary-zcore-fuzz))
2602               ? ;Whitespace
2603             (if (< gnus-tmp-score gnus-summary-default-score)
2604                 gnus-score-below-mark gnus-score-over-mark)))
2605          (gnus-tmp-replied
2606           (cond (gnus-tmp-process gnus-process-mark)
2607                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2608                  gnus-cached-mark)
2609                 (gnus-tmp-replied gnus-replied-mark)
2610                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2611                  gnus-saved-mark)
2612                 (t gnus-unread-mark)))
2613          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2614          (gnus-tmp-name
2615           (cond
2616            ((string-match "<[^>]+> *$" gnus-tmp-from)
2617             (let ((beg (match-beginning 0)))
2618               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2619                        (substring gnus-tmp-from (1+ (match-beginning 0))
2620                                   (1- (match-end 0))))
2621                   (substring gnus-tmp-from 0 beg))))
2622            ((string-match "(.+)" gnus-tmp-from)
2623             (substring gnus-tmp-from
2624                        (1+ (match-beginning 0)) (1- (match-end 0))))
2625            (t gnus-tmp-from)))
2626          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2627          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2628          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2629          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2630          (buffer-read-only nil))
2631     (when (string= gnus-tmp-name "")
2632       (setq gnus-tmp-name gnus-tmp-from))
2633     (unless (numberp gnus-tmp-lines)
2634       (setq gnus-tmp-lines 0))
2635     (gnus-put-text-property-excluding-characters-with-faces
2636      (point)
2637      (progn (eval gnus-summary-line-format-spec) (point))
2638      'gnus-number gnus-tmp-number)
2639     (when (gnus-visual-p 'summary-highlight 'highlight)
2640       (forward-line -1)
2641       (gnus-run-hooks 'gnus-summary-update-hook)
2642       (forward-line 1))))
2643
2644 (defun gnus-summary-update-line (&optional dont-update)
2645   ;; Update summary line after change.
2646   (when (and gnus-summary-default-score
2647              (not gnus-summary-inhibit-highlight))
2648     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2649            (article (gnus-summary-article-number))
2650            (score (gnus-summary-article-score article)))
2651       (unless dont-update
2652         (if (and gnus-summary-mark-below
2653                  (< (gnus-summary-article-score)
2654                     gnus-summary-mark-below))
2655             ;; This article has a low score, so we mark it as read.
2656             (when (memq article gnus-newsgroup-unreads)
2657               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2658           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2659             ;; This article was previously marked as read on account
2660             ;; of a low score, but now it has risen, so we mark it as
2661             ;; unread.
2662             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2663         (gnus-summary-update-mark
2664          (if (or (null gnus-summary-default-score)
2665                  (<= (abs (- score gnus-summary-default-score))
2666                      gnus-summary-zcore-fuzz))
2667              ? ;Whitespace
2668            (if (< score gnus-summary-default-score)
2669                gnus-score-below-mark gnus-score-over-mark))
2670          'score))
2671       ;; Do visual highlighting.
2672       (when (gnus-visual-p 'summary-highlight 'highlight)
2673         (gnus-run-hooks 'gnus-summary-update-hook)))))
2674
2675 (defvar gnus-tmp-new-adopts nil)
2676
2677 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2678   "Return the number of articles in THREAD.
2679 This may be 0 in some cases -- if none of the articles in
2680 the thread are to be displayed."
2681   (let* ((number
2682           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2683           (cond
2684            ((not (listp thread))
2685             1)
2686            ((and (consp thread) (cdr thread))
2687             (apply
2688              '+ 1 (mapcar
2689                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2690            ((null thread)
2691             1)
2692            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2693             1)
2694            (t 0))))
2695     (when (and level (zerop level) gnus-tmp-new-adopts)
2696       (incf number
2697             (apply '+ (mapcar
2698                        'gnus-summary-number-of-articles-in-thread
2699                        gnus-tmp-new-adopts))))
2700     (if char
2701         (if (> number 1) gnus-not-empty-thread-mark
2702           gnus-empty-thread-mark)
2703       number)))
2704
2705 (defun gnus-summary-set-local-parameters (group)
2706   "Go through the local params of GROUP and set all variable specs in that list."
2707   (let ((params (gnus-group-find-parameter group))
2708         elem)
2709     (while params
2710       (setq elem (car params)
2711             params (cdr params))
2712       (and (consp elem)                 ; Has to be a cons.
2713            (consp (cdr elem))           ; The cdr has to be a list.
2714            (symbolp (car elem))         ; Has to be a symbol in there.
2715            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2716            (ignore-errors               ; So we set it.
2717              (make-local-variable (car elem))
2718              (set (car elem) (eval (nth 1 elem))))))))
2719
2720 (defun gnus-summary-read-group (group &optional show-all no-article
2721                                       kill-buffer no-display backward
2722                                       select-articles)
2723   "Start reading news in newsgroup GROUP.
2724 If SHOW-ALL is non-nil, already read articles are also listed.
2725 If NO-ARTICLE is non-nil, no article is selected initially.
2726 If NO-DISPLAY, don't generate a summary buffer."
2727   (let (result)
2728     (while (and group
2729                 (null (setq result
2730                             (let ((gnus-auto-select-next nil))
2731                               (or (gnus-summary-read-group-1
2732                                    group show-all no-article
2733                                    kill-buffer no-display
2734                                    select-articles)
2735                                   (setq show-all nil
2736                                         select-articles nil)))))
2737                 (eq gnus-auto-select-next 'quietly))
2738       (set-buffer gnus-group-buffer)
2739       ;; The entry function called above goes to the next
2740       ;; group automatically, so we go two groups back
2741       ;; if we are searching for the previous group.
2742       (when backward
2743         (gnus-group-prev-unread-group 2))
2744       (if (not (equal group (gnus-group-group-name)))
2745           (setq group (gnus-group-group-name))
2746         (setq group nil)))
2747     result))
2748
2749 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2750   "Directly jump to the other GROUP from summary buffer.
2751 If SHOW-ALL is non-nil, already read articles are also listed."
2752   (interactive
2753    (if (eq gnus-summary-buffer (current-buffer))
2754        (list (completing-read
2755               "Group: " gnus-active-hashtb nil t
2756               (when (and gnus-newsgroup-name
2757                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2758                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2759               'gnus-group-history)
2760              current-prefix-arg)
2761      (error "%s must be invoked from a gnus summary buffer." this-command)))
2762   (unless (or (zerop (length group))
2763               (and gnus-newsgroup-name
2764                    (string-equal gnus-newsgroup-name group)))
2765     (gnus-summary-exit)
2766     (gnus-summary-read-group group show-all
2767                              gnus-dont-select-after-jump-to-other-group)))
2768
2769 (defun gnus-summary-read-group-1 (group show-all no-article
2770                                         kill-buffer no-display
2771                                         &optional select-articles)
2772   ;; Killed foreign groups can't be entered.
2773   (when (and (not (gnus-group-native-p group))
2774              (not (gnus-gethash group gnus-newsrc-hashtb)))
2775     (error "Dead non-native groups can't be entered"))
2776   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2777   (let* ((new-group (gnus-summary-setup-buffer group))
2778          (quit-config (gnus-group-quit-config group))
2779          (did-select (and new-group (gnus-select-newsgroup
2780                                      group show-all select-articles))))
2781     (cond
2782      ;; This summary buffer exists already, so we just select it.
2783      ((not new-group)
2784       (gnus-set-global-variables)
2785       (when kill-buffer
2786         (gnus-kill-or-deaden-summary kill-buffer))
2787       (gnus-configure-windows 'summary 'force)
2788       (gnus-set-mode-line 'summary)
2789       (gnus-summary-position-point)
2790       (message "")
2791       t)
2792      ;; We couldn't select this group.
2793      ((null did-select)
2794       (when (and (eq major-mode 'gnus-summary-mode)
2795                  (not (equal (current-buffer) kill-buffer)))
2796         (kill-buffer (current-buffer))
2797         (if (not quit-config)
2798             (progn
2799               ;; Update the info -- marks might need to be removed,
2800               ;; for instance.
2801               (gnus-summary-update-info)
2802               (set-buffer gnus-group-buffer)
2803               (gnus-group-jump-to-group group)
2804               (gnus-group-next-unread-group 1))
2805           (gnus-handle-ephemeral-exit quit-config)))
2806       (gnus-message 3 "Can't select group")
2807       nil)
2808      ;; The user did a `C-g' while prompting for number of articles,
2809      ;; so we exit this group.
2810      ((eq did-select 'quit)
2811       (and (eq major-mode 'gnus-summary-mode)
2812            (not (equal (current-buffer) kill-buffer))
2813            (kill-buffer (current-buffer)))
2814       (when kill-buffer
2815         (gnus-kill-or-deaden-summary kill-buffer))
2816       (if (not quit-config)
2817           (progn
2818             (set-buffer gnus-group-buffer)
2819             (gnus-group-jump-to-group group)
2820             (gnus-group-next-unread-group 1)
2821             (gnus-configure-windows 'group 'force))
2822         (gnus-handle-ephemeral-exit quit-config))
2823       ;; Finally signal the quit.
2824       (signal 'quit nil))
2825      ;; The group was successfully selected.
2826      (t
2827       (gnus-set-global-variables)
2828       ;; Save the active value in effect when the group was entered.
2829       (setq gnus-newsgroup-active
2830             (gnus-copy-sequence
2831              (gnus-active gnus-newsgroup-name)))
2832       ;; You can change the summary buffer in some way with this hook.
2833       (gnus-run-hooks 'gnus-select-group-hook)
2834       ;; Set any local variables in the group parameters.
2835       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2836       (gnus-update-format-specifications
2837        nil 'summary 'summary-mode 'summary-dummy)
2838       (gnus-update-summary-mark-positions)
2839       ;; Do score processing.
2840       (when gnus-use-scoring
2841         (gnus-possibly-score-headers))
2842       ;; Check whether to fill in the gaps in the threads.
2843       (when gnus-build-sparse-threads
2844         (gnus-build-sparse-threads))
2845       ;; Find the initial limit.
2846       (if gnus-show-threads
2847           (if show-all
2848               (let ((gnus-newsgroup-dormant nil))
2849                 (gnus-summary-initial-limit show-all))
2850             (gnus-summary-initial-limit show-all))
2851         (setq gnus-newsgroup-limit
2852               (mapcar
2853                (lambda (header) (mail-header-number header))
2854                gnus-newsgroup-headers)))
2855       ;; Generate the summary buffer.
2856       (unless no-display
2857         (gnus-summary-prepare))
2858       (when gnus-use-trees
2859         (gnus-tree-open group)
2860         (setq gnus-summary-highlight-line-function
2861               'gnus-tree-highlight-article))
2862       ;; If the summary buffer is empty, but there are some low-scored
2863       ;; articles or some excluded dormants, we include these in the
2864       ;; buffer.
2865       (when (and (zerop (buffer-size))
2866                  (not no-display))
2867         (cond (gnus-newsgroup-dormant
2868                (gnus-summary-limit-include-dormant))
2869               ((and gnus-newsgroup-scored show-all)
2870                (gnus-summary-limit-include-expunged t))))
2871       ;; Function `gnus-apply-kill-file' must be called in this hook.
2872       (gnus-run-hooks 'gnus-apply-kill-hook)
2873       (if (and (zerop (buffer-size))
2874                (not no-display))
2875           (progn
2876             ;; This newsgroup is empty.
2877             (gnus-summary-catchup-and-exit nil t)
2878             (gnus-message 6 "No unread news")
2879             (when kill-buffer
2880               (gnus-kill-or-deaden-summary kill-buffer))
2881             ;; Return nil from this function.
2882             nil)
2883         ;; Hide conversation thread subtrees.  We cannot do this in
2884         ;; gnus-summary-prepare-hook since kill processing may not
2885         ;; work with hidden articles.
2886         (and gnus-show-threads
2887              gnus-thread-hide-subtree
2888              (gnus-summary-hide-all-threads))
2889         (when kill-buffer
2890           (gnus-kill-or-deaden-summary kill-buffer))
2891         ;; Show first unread article if requested.
2892         (if (and (not no-article)
2893                  (not no-display)
2894                  gnus-newsgroup-unreads
2895                  gnus-auto-select-first)
2896             (progn
2897               (gnus-configure-windows 'summary)
2898               (cond
2899                ((eq gnus-auto-select-first 'best)
2900                 (gnus-summary-best-unread-article))
2901                ((eq gnus-auto-select-first t)
2902                 (gnus-summary-first-unread-article))
2903                ((gnus-functionp gnus-auto-select-first)
2904                 (funcall gnus-auto-select-first))))
2905           ;; Don't select any articles, just move point to the first
2906           ;; article in the group.
2907           (goto-char (point-min))
2908           (gnus-summary-position-point)
2909           (gnus-configure-windows 'summary 'force)
2910           (gnus-set-mode-line 'summary))
2911         (when (get-buffer-window gnus-group-buffer t)
2912           ;; Gotta use windows, because recenter does weird stuff if
2913           ;; the current buffer ain't the displayed window.
2914           (let ((owin (selected-window)))
2915             (select-window (get-buffer-window gnus-group-buffer t))
2916             (when (gnus-group-goto-group group)
2917               (recenter))
2918             (select-window owin)))
2919         ;; Mark this buffer as "prepared".
2920         (setq gnus-newsgroup-prepared t)
2921         (gnus-run-hooks 'gnus-summary-prepared-hook)
2922         t)))))
2923
2924 (defun gnus-summary-prepare ()
2925   "Generate the summary buffer."
2926   (interactive)
2927   (let ((buffer-read-only nil))
2928     (erase-buffer)
2929     (setq gnus-newsgroup-data nil
2930           gnus-newsgroup-data-reverse nil)
2931     (gnus-run-hooks 'gnus-summary-generate-hook)
2932     ;; Generate the buffer, either with threads or without.
2933     (when gnus-newsgroup-headers
2934       (gnus-summary-prepare-threads
2935        (if gnus-show-threads
2936            (gnus-sort-gathered-threads
2937             (funcall gnus-summary-thread-gathering-function
2938                      (gnus-sort-threads
2939                       (gnus-cut-threads (gnus-make-threads)))))
2940          ;; Unthreaded display.
2941          (gnus-sort-articles gnus-newsgroup-headers))))
2942     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2943     ;; Call hooks for modifying summary buffer.
2944     (goto-char (point-min))
2945     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2946
2947 (defsubst gnus-general-simplify-subject (subject)
2948   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2949   (setq subject
2950         (cond
2951          ;; Truncate the subject.
2952          (gnus-simplify-subject-functions
2953           (gnus-map-function gnus-simplify-subject-functions subject))
2954          ((numberp gnus-summary-gather-subject-limit)
2955           (setq subject (gnus-simplify-subject-re subject))
2956           (if (> (length subject) gnus-summary-gather-subject-limit)
2957               (substring subject 0 gnus-summary-gather-subject-limit)
2958             subject))
2959          ;; Fuzzily simplify it.
2960          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2961           (gnus-simplify-subject-fuzzy subject))
2962          ;; Just remove the leading "Re:".
2963          (t
2964           (gnus-simplify-subject-re subject))))
2965
2966   (if (and gnus-summary-gather-exclude-subject
2967            (string-match gnus-summary-gather-exclude-subject subject))
2968       nil                               ; This article shouldn't be gathered
2969     subject))
2970
2971 (defun gnus-summary-simplify-subject-query ()
2972   "Query where the respool algorithm would put this article."
2973   (interactive)
2974   (gnus-summary-select-article)
2975   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2976
2977 (defun gnus-gather-threads-by-subject (threads)
2978   "Gather threads by looking at Subject headers."
2979   (if (not gnus-summary-make-false-root)
2980       threads
2981     (let ((hashtb (gnus-make-hashtable 1024))
2982           (prev threads)
2983           (result threads)
2984           subject hthread whole-subject)
2985       (while threads
2986         (setq subject (gnus-general-simplify-subject
2987                        (setq whole-subject (mail-header-subject
2988                                             (caar threads)))))
2989         (when subject
2990           (if (setq hthread (gnus-gethash subject hashtb))
2991               (progn
2992                 ;; We enter a dummy root into the thread, if we
2993                 ;; haven't done that already.
2994                 (unless (stringp (caar hthread))
2995                   (setcar hthread (list whole-subject (car hthread))))
2996                 ;; We add this new gathered thread to this gathered
2997                 ;; thread.
2998                 (setcdr (car hthread)
2999                         (nconc (cdar hthread) (list (car threads))))
3000                 ;; Remove it from the list of threads.
3001                 (setcdr prev (cdr threads))
3002                 (setq threads prev))
3003             ;; Enter this thread into the hash table.
3004             (gnus-sethash subject threads hashtb)))
3005         (setq prev threads)
3006         (setq threads (cdr threads)))
3007       result)))
3008
3009 (defun gnus-gather-threads-by-references (threads)
3010   "Gather threads by looking at References headers."
3011   (let ((idhashtb (gnus-make-hashtable 1024))
3012         (thhashtb (gnus-make-hashtable 1024))
3013         (prev threads)
3014         (result threads)
3015         ids references id gthread gid entered ref)
3016     (while threads
3017       (when (setq references (mail-header-references (caar threads)))
3018         (setq id (mail-header-id (caar threads))
3019               ids (gnus-split-references references)
3020               entered nil)
3021         (while (setq ref (pop ids))
3022           (setq ids (delete ref ids))
3023           (if (not (setq gid (gnus-gethash ref idhashtb)))
3024               (progn
3025                 (gnus-sethash ref id idhashtb)
3026                 (gnus-sethash id threads thhashtb))
3027             (setq gthread (gnus-gethash gid thhashtb))
3028             (unless entered
3029               ;; We enter a dummy root into the thread, if we
3030               ;; haven't done that already.
3031               (unless (stringp (caar gthread))
3032                 (setcar gthread (list (mail-header-subject (caar gthread))
3033                                       (car gthread))))
3034               ;; We add this new gathered thread to this gathered
3035               ;; thread.
3036               (setcdr (car gthread)
3037                       (nconc (cdar gthread) (list (car threads)))))
3038             ;; Add it into the thread hash table.
3039             (gnus-sethash id gthread thhashtb)
3040             (setq entered t)
3041             ;; Remove it from the list of threads.
3042             (setcdr prev (cdr threads))
3043             (setq threads prev))))
3044       (setq prev threads)
3045       (setq threads (cdr threads)))
3046     result))
3047
3048 (defun gnus-sort-gathered-threads (threads)
3049   "Sort subtreads inside each gathered thread by article number."
3050   (let ((result threads))
3051     (while threads
3052       (when (stringp (caar threads))
3053         (setcdr (car threads)
3054                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3055       (setq threads (cdr threads)))
3056     result))
3057
3058 (defun gnus-thread-loop-p (root thread)
3059   "Say whether ROOT is in THREAD."
3060   (let ((stack (list thread))
3061         (infloop 0)
3062         th)
3063     (while (setq thread (pop stack))
3064       (setq th (cdr thread))
3065       (while (and th
3066                   (not (eq (caar th) root)))
3067         (pop th))
3068       (if th
3069           ;; We have found a loop.
3070           (let (ref-dep)
3071             (setcdr thread (delq (car th) (cdr thread)))
3072             (if (boundp (setq ref-dep (intern "none"
3073                                               gnus-newsgroup-dependencies)))
3074                 (setcdr (symbol-value ref-dep)
3075                         (nconc (cdr (symbol-value ref-dep))
3076                                (list (car th))))
3077               (set ref-dep (list nil (car th))))
3078             (setq infloop 1
3079                   stack nil))
3080         ;; Push all the subthreads onto the stack.
3081         (push (cdr thread) stack)))
3082     infloop))
3083
3084 (defun gnus-make-threads ()
3085   "Go through the dependency hashtb and find the roots.  Return all threads."
3086   (let (threads)
3087     (while (catch 'infloop
3088              (mapatoms
3089               (lambda (refs)
3090                 ;; Deal with self-referencing References loops.
3091                 (when (and (car (symbol-value refs))
3092                            (not (zerop
3093                                  (apply
3094                                   '+
3095                                   (mapcar
3096                                    (lambda (thread)
3097                                      (gnus-thread-loop-p
3098                                       (car (symbol-value refs)) thread))
3099                                    (cdr (symbol-value refs)))))))
3100                   (setq threads nil)
3101                   (throw 'infloop t))
3102                 (unless (car (symbol-value refs))
3103                   ;; These threads do not refer back to any other articles,
3104                   ;; so they're roots.
3105                   (setq threads (append (cdr (symbol-value refs)) threads))))
3106               gnus-newsgroup-dependencies)))
3107     threads))
3108
3109 ;; Build the thread tree.
3110 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3111   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3112
3113 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3114 if it was already present.
3115
3116 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3117 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3118 Message-IDs will be renamed be renamed to a unique Message-ID before
3119 being entered.
3120
3121 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3122   (let* ((id (mail-header-id header))
3123          (id-dep (and id (intern id dependencies)))
3124          ref ref-dep ref-header)
3125     ;; Enter this `header' in the `dependencies' table.
3126     (cond
3127      ((not id-dep)
3128       (setq header nil))
3129      ;; The first two cases do the normal part: enter a new `header'
3130      ;; in the `dependencies' table.
3131      ((not (boundp id-dep))
3132       (set id-dep (list header)))
3133      ((null (car (symbol-value id-dep)))
3134       (setcar (symbol-value id-dep) header))
3135
3136      ;; From here the `header' was already present in the
3137      ;; `dependencies' table.
3138      (force-new
3139       ;; Overrides an existing entry;
3140       ;; just set the header part of the entry.
3141       (setcar (symbol-value id-dep) header))
3142
3143      ;; Renames the existing `header' to a unique Message-ID.
3144      ((not gnus-summary-ignore-duplicates)
3145       ;; An article with this Message-ID has already been seen.
3146       ;; We rename the Message-ID.
3147       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3148            (list header))
3149       (mail-header-set-id header id))
3150
3151      ;; The last case ignores an existing entry, except it adds any
3152      ;; additional Xrefs (in case the two articles came from different
3153      ;; servers.
3154      ;; Also sets `header' to `nil' meaning that the `dependencies'
3155      ;; table was *not* modified.
3156      (t
3157       (mail-header-set-xref
3158        (car (symbol-value id-dep))
3159        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3160                    "")
3161                (or (mail-header-xref header) "")))
3162       (setq header nil)))
3163
3164     (when header
3165       ;; First check if that we are not creating a References loop.
3166       (setq ref (gnus-parent-id (mail-header-references header)))
3167       (while (and ref
3168                   (setq ref-dep (intern-soft ref dependencies))
3169                   (boundp ref-dep)
3170                   (setq ref-header (car (symbol-value ref-dep))))
3171         (if (string= id ref)
3172             ;; Yuk!  This is a reference loop.  Make the article be a
3173             ;; root article.
3174             (progn
3175               (mail-header-set-references (car (symbol-value id-dep)) "none")
3176               (setq ref nil))
3177           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3178       (setq ref (gnus-parent-id (mail-header-references header)))
3179       (setq ref-dep (intern (or ref "none") dependencies))
3180       (if (boundp ref-dep)
3181           (setcdr (symbol-value ref-dep)
3182                   (nconc (cdr (symbol-value ref-dep))
3183                          (list (symbol-value id-dep))))
3184         (set ref-dep (list nil (symbol-value id-dep)))))
3185     header))
3186
3187 (defun gnus-build-sparse-threads ()
3188   (let ((headers gnus-newsgroup-headers)
3189         (gnus-summary-ignore-duplicates t)
3190         header references generation relations
3191         subject child end new-child date)
3192     ;; First we create an alist of generations/relations, where
3193     ;; generations is how much we trust the relation, and the relation
3194     ;; is parent/child.
3195     (gnus-message 7 "Making sparse threads...")
3196     (save-excursion
3197       (nnheader-set-temp-buffer " *gnus sparse threads*")
3198       (while (setq header (pop headers))
3199         (when (and (setq references (mail-header-references header))
3200                    (not (string= references "")))
3201           (insert references)
3202           (setq child (mail-header-id header)
3203                 subject (mail-header-subject header)
3204                 date (mail-header-date header)
3205                 generation 0)
3206           (while (search-backward ">" nil t)
3207             (setq end (1+ (point)))
3208             (when (search-backward "<" nil t)
3209               (setq new-child (buffer-substring (point) end))
3210               (push (list (incf generation)
3211                           child (setq child new-child)
3212                           subject date)
3213                     relations)))
3214           (when child
3215             (push (list (1+ generation) child nil subject) relations))
3216           (erase-buffer)))
3217       (kill-buffer (current-buffer)))
3218     ;; Sort over trustworthiness.
3219     (mapcar
3220      (lambda (relation)
3221        (when (gnus-dependencies-add-header
3222               (make-full-mail-header
3223                gnus-reffed-article-number
3224                (nth 3 relation) "" (or (nth 4 relation) "")
3225                (nth 1 relation)
3226                (or (nth 2 relation) "") 0 0 "")
3227               gnus-newsgroup-dependencies nil)
3228          (push gnus-reffed-article-number gnus-newsgroup-limit)
3229          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3230          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3231                gnus-newsgroup-reads)
3232          (decf gnus-reffed-article-number)))
3233      (sort relations 'car-less-than-car))
3234     (gnus-message 7 "Making sparse threads...done")))
3235
3236 (defun gnus-build-old-threads ()
3237   ;; Look at all the articles that refer back to old articles, and
3238   ;; fetch the headers for the articles that aren't there.  This will
3239   ;; build complete threads - if the roots haven't been expired by the
3240   ;; server, that is.
3241   (let (id heads)
3242     (mapatoms
3243      (lambda (refs)
3244        (when (not (car (symbol-value refs)))
3245          (setq heads (cdr (symbol-value refs)))
3246          (while heads
3247            (if (memq (mail-header-number (caar heads))
3248                      gnus-newsgroup-dormant)
3249                (setq heads (cdr heads))
3250              (setq id (symbol-name refs))
3251              (while (and (setq id (gnus-build-get-header id))
3252                          (not (car (gnus-id-to-thread id)))))
3253              (setq heads nil)))))
3254      gnus-newsgroup-dependencies)))
3255
3256 ;; This function has to be called with point after the article number
3257 ;; on the beginning of the line.
3258 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3259   (let ((eol (gnus-point-at-eol))
3260         (buffer (current-buffer))
3261         header)
3262
3263     ;; overview: [num subject from date id refs chars lines misc]
3264     (unless (eobp)
3265       (forward-char))
3266
3267     (setq header
3268           (make-full-mail-header
3269            number                               ; number
3270            (nnheader-nov-field)                 ; subject
3271            (nnheader-nov-field)                 ; from
3272            (nnheader-nov-field)                 ; date
3273            (nnheader-nov-read-message-id)       ; id
3274            (nnheader-nov-field)                 ; refs
3275            (nnheader-nov-read-integer)          ; chars
3276            (nnheader-nov-read-integer)          ; lines
3277            (unless (eobp)
3278              (nnheader-nov-field))              ; misc
3279            (nnheader-nov-parse-extra)))         ; extra
3280
3281     (when gnus-alter-header-function
3282       (funcall gnus-alter-header-function header))
3283     (gnus-dependencies-add-header header dependencies force-new)))
3284
3285 (defun gnus-build-get-header (id)
3286   ;; Look through the buffer of NOV lines and find the header to
3287   ;; ID.  Enter this line into the dependencies hash table, and return
3288   ;; the id of the parent article (if any).
3289   (let ((deps gnus-newsgroup-dependencies)
3290         found header)
3291     (prog1
3292         (save-excursion
3293           (set-buffer nntp-server-buffer)
3294           (let ((case-fold-search nil))
3295             (goto-char (point-min))
3296             (while (and (not found)
3297                         (search-forward id nil t))
3298               (beginning-of-line)
3299               (setq found (looking-at
3300                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3301                                    (regexp-quote id))))
3302               (or found (beginning-of-line 2)))
3303             (when found
3304               (beginning-of-line)
3305               (and
3306                (setq header (gnus-nov-parse-line
3307                              (read (current-buffer)) deps))
3308                (gnus-parent-id (mail-header-references header))))))
3309       (when header
3310         (let ((number (mail-header-number header)))
3311           (push number gnus-newsgroup-limit)
3312           (push header gnus-newsgroup-headers)
3313           (if (memq number gnus-newsgroup-unselected)
3314               (progn
3315                 (push number gnus-newsgroup-unreads)
3316                 (setq gnus-newsgroup-unselected
3317                       (delq number gnus-newsgroup-unselected)))
3318             (push number gnus-newsgroup-ancient)))))))
3319
3320 (defun gnus-build-all-threads ()
3321   "Read all the headers."
3322   (let ((gnus-summary-ignore-duplicates t)
3323         (dependencies gnus-newsgroup-dependencies)
3324         header article)
3325     (save-excursion
3326       (set-buffer nntp-server-buffer)
3327       (let ((case-fold-search nil))
3328         (goto-char (point-min))
3329         (while (not (eobp))
3330           (ignore-errors
3331             (setq article (read (current-buffer))
3332                   header (gnus-nov-parse-line
3333                           article dependencies)))
3334           (when header
3335             (save-excursion
3336               (set-buffer gnus-summary-buffer)
3337               (push header gnus-newsgroup-headers)
3338               (if (memq (setq article (mail-header-number header))
3339                         gnus-newsgroup-unselected)
3340                   (progn
3341                     (push article gnus-newsgroup-unreads)
3342                     (setq gnus-newsgroup-unselected
3343                           (delq article gnus-newsgroup-unselected)))
3344                 (push article gnus-newsgroup-ancient)))
3345             (forward-line 1)))))))
3346
3347 (defun gnus-summary-update-article-line (article header)
3348   "Update the line for ARTICLE using HEADERS."
3349   (let* ((id (mail-header-id header))
3350          (thread (gnus-id-to-thread id)))
3351     (unless thread
3352       (error "Article in no thread"))
3353     ;; Update the thread.
3354     (setcar thread header)
3355     (gnus-summary-goto-subject article)
3356     (let* ((datal (gnus-data-find-list article))
3357            (data (car datal))
3358            (length (when (cdr datal)
3359                      (- (gnus-data-pos data)
3360                         (gnus-data-pos (cadr datal)))))
3361            (buffer-read-only nil)
3362            (level (gnus-summary-thread-level)))
3363       (gnus-delete-line)
3364       (gnus-summary-insert-line
3365        header level nil (gnus-article-mark article)
3366        (memq article gnus-newsgroup-replied)
3367        (memq article gnus-newsgroup-expirable)
3368        ;; Only insert the Subject string when it's different
3369        ;; from the previous Subject string.
3370        (if (gnus-subject-equal
3371             (condition-case ()
3372                 (mail-header-subject
3373                  (gnus-data-header
3374                   (cadr
3375                    (gnus-data-find-list
3376                     article
3377                     (gnus-data-list t)))))
3378               ;; Error on the side of excessive subjects.
3379               (error ""))
3380             (mail-header-subject header))
3381            ""
3382          (mail-header-subject header))
3383        nil (cdr (assq article gnus-newsgroup-scored))
3384        (memq article gnus-newsgroup-processable))
3385       (when length
3386         (gnus-data-update-list
3387          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3388
3389 (defun gnus-summary-update-article (article &optional iheader)
3390   "Update ARTICLE in the summary buffer."
3391   (set-buffer gnus-summary-buffer)
3392   (let* ((header (gnus-summary-article-header article))
3393          (id (mail-header-id header))
3394          (data (gnus-data-find article))
3395          (thread (gnus-id-to-thread id))
3396          (references (mail-header-references header))
3397          (parent
3398           (gnus-id-to-thread
3399            (or (gnus-parent-id
3400                 (when (and references
3401                            (not (equal "" references)))
3402                   references))
3403                "none")))
3404          (buffer-read-only nil)
3405          (old (car thread)))
3406     (when thread
3407       (unless iheader
3408         (setcar thread nil)
3409         (when parent
3410           (delq thread parent)))
3411       (if (gnus-summary-insert-subject id header)
3412           ;; Set the (possibly) new article number in the data structure.
3413           (gnus-data-set-number data (gnus-id-to-article id))
3414         (setcar thread old)
3415         nil))))
3416
3417 (defun gnus-rebuild-thread (id &optional line)
3418   "Rebuild the thread containing ID.
3419 If LINE, insert the rebuilt thread starting on line LINE."
3420   (let ((buffer-read-only nil)
3421         old-pos current thread data)
3422     (if (not gnus-show-threads)
3423         (setq thread (list (car (gnus-id-to-thread id))))
3424       ;; Get the thread this article is part of.
3425       (setq thread (gnus-remove-thread id)))
3426     (setq old-pos (gnus-point-at-bol))
3427     (setq current (save-excursion
3428                     (and (zerop (forward-line -1))
3429                          (gnus-summary-article-number))))
3430     ;; If this is a gathered thread, we have to go some re-gathering.
3431     (when (stringp (car thread))
3432       (let ((subject (car thread))
3433             roots thr)
3434         (setq thread (cdr thread))
3435         (while thread
3436           (unless (memq (setq thr (gnus-id-to-thread
3437                                    (gnus-root-id
3438                                     (mail-header-id (caar thread)))))
3439                         roots)
3440             (push thr roots))
3441           (setq thread (cdr thread)))
3442         ;; We now have all (unique) roots.
3443         (if (= (length roots) 1)
3444             ;; All the loose roots are now one solid root.
3445             (setq thread (car roots))
3446           (setq thread (cons subject (gnus-sort-threads roots))))))
3447     (let (threads)
3448       ;; We then insert this thread into the summary buffer.
3449       (when line
3450         (goto-char (point-min))
3451         (forward-line (1- line)))
3452       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3453         (if gnus-show-threads
3454             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3455           (gnus-summary-prepare-unthreaded thread))
3456         (setq data (nreverse gnus-newsgroup-data))
3457         (setq threads gnus-newsgroup-threads))
3458       ;; We splice the new data into the data structure.
3459       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3460       ;;!!! then we want to insert at the beginning of the buffer.
3461       ;;!!! That happens to be true with Gnus now, but that may
3462       ;;!!! change in the future.  Perhaps.
3463       (gnus-data-enter-list
3464        (if line nil current) data (- (point) old-pos))
3465       (setq gnus-newsgroup-threads
3466             (nconc threads gnus-newsgroup-threads))
3467       (gnus-data-compute-positions))))
3468
3469 (defun gnus-number-to-header (number)
3470   "Return the header for article NUMBER."
3471   (let ((headers gnus-newsgroup-headers))
3472     (while (and headers
3473                 (not (= number (mail-header-number (car headers)))))
3474       (pop headers))
3475     (when headers
3476       (car headers))))
3477
3478 (defun gnus-parent-headers (in-headers &optional generation)
3479   "Return the headers of the GENERATIONeth parent of HEADERS."
3480   (unless generation
3481     (setq generation 1))
3482   (let ((parent t)
3483         (headers in-headers)
3484         references)
3485     (while (and parent
3486                 (not (zerop generation))
3487                 (setq references (mail-header-references headers)))
3488       (setq headers (if (and references
3489                              (setq parent (gnus-parent-id references)))
3490                         (car (gnus-id-to-thread parent))
3491                       nil))
3492       (decf generation))
3493     (and (not (eq headers in-headers))
3494          headers)))
3495
3496 (defun gnus-id-to-thread (id)
3497   "Return the (sub-)thread where ID appears."
3498   (gnus-gethash id gnus-newsgroup-dependencies))
3499
3500 (defun gnus-id-to-article (id)
3501   "Return the article number of ID."
3502   (let ((thread (gnus-id-to-thread id)))
3503     (when (and thread
3504                (car thread))
3505       (mail-header-number (car thread)))))
3506
3507 (defun gnus-id-to-header (id)
3508   "Return the article headers of ID."
3509   (car (gnus-id-to-thread id)))
3510
3511 (defun gnus-article-displayed-root-p (article)
3512   "Say whether ARTICLE is a root(ish) article."
3513   (let ((level (gnus-summary-thread-level article))
3514         (refs (mail-header-references  (gnus-summary-article-header article)))
3515         particle)
3516     (cond
3517      ((null level) nil)
3518      ((zerop level) t)
3519      ((null refs) t)
3520      ((null (gnus-parent-id refs)) t)
3521      ((and (= 1 level)
3522            (null (setq particle (gnus-id-to-article
3523                                  (gnus-parent-id refs))))
3524            (null (gnus-summary-thread-level particle)))))))
3525
3526 (defun gnus-root-id (id)
3527   "Return the id of the root of the thread where ID appears."
3528   (let (last-id prev)
3529     (while (and id (setq prev (car (gnus-id-to-thread id))))
3530       (setq last-id id
3531             id (gnus-parent-id (mail-header-references prev))))
3532     last-id))
3533
3534 (defun gnus-articles-in-thread (thread)
3535   "Return the list of articles in THREAD."
3536   (cons (mail-header-number (car thread))
3537         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3538
3539 (defun gnus-remove-thread (id &optional dont-remove)
3540   "Remove the thread that has ID in it."
3541   (let (headers thread last-id)
3542     ;; First go up in this thread until we find the root.
3543     (setq last-id (gnus-root-id id)
3544           headers (message-flatten-list (gnus-id-to-thread last-id)))
3545     ;; We have now found the real root of this thread.  It might have
3546     ;; been gathered into some loose thread, so we have to search
3547     ;; through the threads to find the thread we wanted.
3548     (let ((threads gnus-newsgroup-threads)
3549           sub)
3550       (while threads
3551         (setq sub (car threads))
3552         (if (stringp (car sub))
3553             ;; This is a gathered thread, so we look at the roots
3554             ;; below it to find whether this article is in this
3555             ;; gathered root.
3556             (progn
3557               (setq sub (cdr sub))
3558               (while sub
3559                 (when (member (caar sub) headers)
3560                   (setq thread (car threads)
3561                         threads nil
3562                         sub nil))
3563                 (setq sub (cdr sub))))
3564           ;; It's an ordinary thread, so we check it.
3565           (when (eq (car sub) (car headers))
3566             (setq thread sub
3567                   threads nil)))
3568         (setq threads (cdr threads)))
3569       ;; If this article is in no thread, then it's a root.
3570       (if thread
3571           (unless dont-remove
3572             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3573         (setq thread (gnus-id-to-thread last-id)))
3574       (when thread
3575         (prog1
3576             thread                      ; We return this thread.
3577           (unless dont-remove
3578             (if (stringp (car thread))
3579                 (progn
3580                   ;; If we use dummy roots, then we have to remove the
3581                   ;; dummy root as well.
3582                   (when (eq gnus-summary-make-false-root 'dummy)
3583                     ;; We go to the dummy root by going to
3584                     ;; the first sub-"thread", and then one line up.
3585                     (gnus-summary-goto-article
3586                      (mail-header-number (caadr thread)))
3587                     (forward-line -1)
3588                     (gnus-delete-line)
3589                     (gnus-data-compute-positions))
3590                   (setq thread (cdr thread))
3591                   (while thread
3592                     (gnus-remove-thread-1 (car thread))
3593                     (setq thread (cdr thread))))
3594               (gnus-summary-show-all-threads)
3595               (gnus-remove-thread-1 thread))))))))
3596
3597 (defun gnus-remove-thread-1 (thread)
3598   "Remove the thread THREAD recursively."
3599   (let ((number (mail-header-number (pop thread)))
3600         d)
3601     (setq thread (reverse thread))
3602     (while thread
3603       (gnus-remove-thread-1 (pop thread)))
3604     (when (setq d (gnus-data-find number))
3605       (goto-char (gnus-data-pos d))
3606       (gnus-data-remove
3607        number
3608        (- (gnus-point-at-bol)
3609           (prog1
3610               (1+ (gnus-point-at-eol))
3611             (gnus-delete-line)))))))
3612
3613 (defun gnus-sort-threads (threads)
3614   "Sort THREADS."
3615   (if (not gnus-thread-sort-functions)
3616       threads
3617     (gnus-message 8 "Sorting threads...")
3618     (prog1
3619         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3620       (gnus-message 8 "Sorting threads...done"))))
3621
3622 (defun gnus-sort-articles (articles)
3623   "Sort ARTICLES."
3624   (when gnus-article-sort-functions
3625     (gnus-message 7 "Sorting articles...")
3626     (prog1
3627         (setq gnus-newsgroup-headers
3628               (sort articles (gnus-make-sort-function
3629                               gnus-article-sort-functions)))
3630       (gnus-message 7 "Sorting articles...done"))))
3631
3632 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3633 (defmacro gnus-thread-header (thread)
3634   ;; Return header of first article in THREAD.
3635   ;; Note that THREAD must never, ever be anything else than a variable -
3636   ;; using some other form will lead to serious barfage.
3637   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3638   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3639   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3640         (vector thread) 2))
3641
3642 (defsubst gnus-article-sort-by-number (h1 h2)
3643   "Sort articles by article number."
3644   (< (mail-header-number h1)
3645      (mail-header-number h2)))
3646
3647 (defun gnus-thread-sort-by-number (h1 h2)
3648   "Sort threads by root article number."
3649   (gnus-article-sort-by-number
3650    (gnus-thread-header h1) (gnus-thread-header h2)))
3651
3652 (defsubst gnus-article-sort-by-lines (h1 h2)
3653   "Sort articles by article Lines header."
3654   (< (mail-header-lines h1)
3655      (mail-header-lines h2)))
3656
3657 (defun gnus-thread-sort-by-lines (h1 h2)
3658   "Sort threads by root article Lines header."
3659   (gnus-article-sort-by-lines
3660    (gnus-thread-header h1) (gnus-thread-header h2)))
3661
3662 (defsubst gnus-article-sort-by-chars (h1 h2)
3663   "Sort articles by octet length."
3664   (< (mail-header-chars h1)
3665      (mail-header-chars h2)))
3666
3667 (defun gnus-thread-sort-by-chars (h1 h2)
3668   "Sort threads by root article octet length."
3669   (gnus-article-sort-by-chars
3670    (gnus-thread-header h1) (gnus-thread-header h2)))
3671
3672 (defsubst gnus-article-sort-by-author (h1 h2)
3673   "Sort articles by root author."
3674   (string-lessp
3675    (let ((addr (car (mime-read-field 'From h1))))
3676      (or (std11-full-name-string addr)
3677          (std11-address-string addr)
3678          ""))
3679    (let ((addr (car (mime-read-field 'From h2))))
3680      (or (std11-full-name-string addr)
3681          (std11-address-string addr)
3682          ""))
3683    ))
3684
3685 (defun gnus-thread-sort-by-author (h1 h2)
3686   "Sort threads by root author."
3687   (gnus-article-sort-by-author
3688    (gnus-thread-header h1)  (gnus-thread-header h2)))
3689
3690 (defsubst gnus-article-sort-by-subject (h1 h2)
3691   "Sort articles by root subject."
3692   (string-lessp
3693    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3694    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3695
3696 (defun gnus-thread-sort-by-subject (h1 h2)
3697   "Sort threads by root subject."
3698   (gnus-article-sort-by-subject
3699    (gnus-thread-header h1) (gnus-thread-header h2)))
3700
3701 (defsubst gnus-article-sort-by-date (h1 h2)
3702   "Sort articles by root article date."
3703   (time-less-p
3704    (gnus-date-get-time (mail-header-date h1))
3705    (gnus-date-get-time (mail-header-date h2))))
3706
3707 (defun gnus-thread-sort-by-date (h1 h2)
3708   "Sort threads by root article date."
3709   (gnus-article-sort-by-date
3710    (gnus-thread-header h1) (gnus-thread-header h2)))
3711
3712 (defsubst gnus-article-sort-by-score (h1 h2)
3713   "Sort articles by root article score.
3714 Unscored articles will be counted as having a score of zero."
3715   (> (or (cdr (assq (mail-header-number h1)
3716                     gnus-newsgroup-scored))
3717          gnus-summary-default-score 0)
3718      (or (cdr (assq (mail-header-number h2)
3719                     gnus-newsgroup-scored))
3720          gnus-summary-default-score 0)))
3721
3722 (defun gnus-thread-sort-by-score (h1 h2)
3723   "Sort threads by root article score."
3724   (gnus-article-sort-by-score
3725    (gnus-thread-header h1) (gnus-thread-header h2)))
3726
3727 (defun gnus-thread-sort-by-total-score (h1 h2)
3728   "Sort threads by the sum of all scores in the thread.
3729 Unscored articles will be counted as having a score of zero."
3730   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3731
3732 (defun gnus-thread-total-score (thread)
3733   ;; This function find the total score of THREAD.
3734   (cond ((null thread)
3735          0)
3736         ((consp thread)
3737          (if (stringp (car thread))
3738              (apply gnus-thread-score-function 0
3739                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3740            (gnus-thread-total-score-1 thread)))
3741         (t
3742          (gnus-thread-total-score-1 (list thread)))))
3743
3744 (defun gnus-thread-total-score-1 (root)
3745   ;; This function find the total score of the thread below ROOT.
3746   (setq root (car root))
3747   (apply gnus-thread-score-function
3748          (or (append
3749               (mapcar 'gnus-thread-total-score
3750                       (cdr (gnus-id-to-thread (mail-header-id root))))
3751               (when (> (mail-header-number root) 0)
3752                 (list (or (cdr (assq (mail-header-number root)
3753                                      gnus-newsgroup-scored))
3754                           gnus-summary-default-score 0))))
3755              (list gnus-summary-default-score)
3756              '(0))))
3757
3758 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3759 (defvar gnus-tmp-prev-subject nil)
3760 (defvar gnus-tmp-false-parent nil)
3761 (defvar gnus-tmp-root-expunged nil)
3762 (defvar gnus-tmp-dummy-line nil)
3763
3764 (defvar gnus-tmp-header)
3765 (defun gnus-extra-header (type &optional header)
3766   "Return the extra header of TYPE."
3767   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3768       ""))
3769
3770 (defun gnus-summary-prepare-threads (threads)
3771   "Prepare summary buffer from THREADS and indentation LEVEL.
3772 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3773 or a straight list of headers."
3774   (gnus-message 7 "Generating summary...")
3775
3776   (setq gnus-newsgroup-threads threads)
3777   (beginning-of-line)
3778
3779   (let ((gnus-tmp-level 0)
3780         (default-score (or gnus-summary-default-score 0))
3781         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3782         thread number subject stack state gnus-tmp-gathered beg-match
3783         new-roots gnus-tmp-new-adopts thread-end
3784         gnus-tmp-header gnus-tmp-unread
3785         gnus-tmp-replied gnus-tmp-subject-or-nil
3786         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3787         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3788         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3789
3790     (setq gnus-tmp-prev-subject nil)
3791
3792     (if (vectorp (car threads))
3793         ;; If this is a straight (sic) list of headers, then a
3794         ;; threaded summary display isn't required, so we just create
3795         ;; an unthreaded one.
3796         (gnus-summary-prepare-unthreaded threads)
3797
3798       ;; Do the threaded display.
3799
3800       (while (or threads stack gnus-tmp-new-adopts new-roots)
3801
3802         (if (and (= gnus-tmp-level 0)
3803                  (or (not stack)
3804                      (= (caar stack) 0))
3805                  (not gnus-tmp-false-parent)
3806                  (or gnus-tmp-new-adopts new-roots))
3807             (if gnus-tmp-new-adopts
3808                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3809                       thread (list (car gnus-tmp-new-adopts))
3810                       gnus-tmp-header (caar thread)
3811                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3812               (when new-roots
3813                 (setq thread (list (car new-roots))
3814                       gnus-tmp-header (caar thread)
3815                       new-roots (cdr new-roots))))
3816
3817           (if threads
3818               ;; If there are some threads, we do them before the
3819               ;; threads on the stack.
3820               (setq thread threads
3821                     gnus-tmp-header (caar thread))
3822             ;; There were no current threads, so we pop something off
3823             ;; the stack.
3824             (setq state (car stack)
3825                   gnus-tmp-level (car state)
3826                   thread (cdr state)
3827                   stack (cdr stack)
3828                   gnus-tmp-header (caar thread))))
3829
3830         (setq gnus-tmp-false-parent nil)
3831         (setq gnus-tmp-root-expunged nil)
3832         (setq thread-end nil)
3833
3834         (if (stringp gnus-tmp-header)
3835             ;; The header is a dummy root.
3836             (cond
3837              ((eq gnus-summary-make-false-root 'adopt)
3838               ;; We let the first article adopt the rest.
3839               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3840                                                (cddar thread)))
3841               (setq gnus-tmp-gathered
3842                     (nconc (mapcar
3843                             (lambda (h) (mail-header-number (car h)))
3844                             (cddar thread))
3845                            gnus-tmp-gathered))
3846               (setq thread (cons (list (caar thread)
3847                                        (cadar thread))
3848                                  (cdr thread)))
3849               (setq gnus-tmp-level -1
3850                     gnus-tmp-false-parent t))
3851              ((eq gnus-summary-make-false-root 'empty)
3852               ;; We print adopted articles with empty subject fields.
3853               (setq gnus-tmp-gathered
3854                     (nconc (mapcar
3855                             (lambda (h) (mail-header-number (car h)))
3856                             (cddar thread))
3857                            gnus-tmp-gathered))
3858               (setq gnus-tmp-level -1))
3859              ((eq gnus-summary-make-false-root 'dummy)
3860               ;; We remember that we probably want to output a dummy
3861               ;; root.
3862               (setq gnus-tmp-dummy-line gnus-tmp-header)
3863               (setq gnus-tmp-prev-subject gnus-tmp-header))
3864              (t
3865               ;; We do not make a root for the gathered
3866               ;; sub-threads at all.
3867               (setq gnus-tmp-level -1)))
3868
3869           (setq number (mail-header-number gnus-tmp-header)
3870                 subject (mail-header-subject gnus-tmp-header))
3871
3872           (cond
3873            ;; If the thread has changed subject, we might want to make
3874            ;; this subthread into a root.
3875            ((and (null gnus-thread-ignore-subject)
3876                  (not (zerop gnus-tmp-level))
3877                  gnus-tmp-prev-subject
3878                  (not (inline
3879                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3880             (setq new-roots (nconc new-roots (list (car thread)))
3881                   thread-end t
3882                   gnus-tmp-header nil))
3883            ;; If the article lies outside the current limit,
3884            ;; then we do not display it.
3885            ((not (memq number gnus-newsgroup-limit))
3886             (setq gnus-tmp-gathered
3887                   (nconc (mapcar
3888                           (lambda (h) (mail-header-number (car h)))
3889                           (cdar thread))
3890                          gnus-tmp-gathered))
3891             (setq gnus-tmp-new-adopts (if (cdar thread)
3892                                           (append gnus-tmp-new-adopts
3893                                                   (cdar thread))
3894                                         gnus-tmp-new-adopts)
3895                   thread-end t
3896                   gnus-tmp-header nil)
3897             (when (zerop gnus-tmp-level)
3898               (setq gnus-tmp-root-expunged t)))
3899            ;; Perhaps this article is to be marked as read?
3900            ((and gnus-summary-mark-below
3901                  (< (or (cdr (assq number gnus-newsgroup-scored))
3902                         default-score)
3903                     gnus-summary-mark-below)
3904                  ;; Don't touch sparse articles.
3905                  (not (gnus-summary-article-sparse-p number))
3906                  (not (gnus-summary-article-ancient-p number)))
3907             (setq gnus-newsgroup-unreads
3908                   (delq number gnus-newsgroup-unreads))
3909             (if gnus-newsgroup-auto-expire
3910                 (push number gnus-newsgroup-expirable)
3911               (push (cons number gnus-low-score-mark)
3912                     gnus-newsgroup-reads))))
3913
3914           (when gnus-tmp-header
3915             ;; We may have an old dummy line to output before this
3916             ;; article.
3917             (when (and gnus-tmp-dummy-line
3918                        (gnus-subject-equal
3919                         gnus-tmp-dummy-line
3920                         (mail-header-subject gnus-tmp-header)))
3921               (gnus-summary-insert-dummy-line
3922                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3923               (setq gnus-tmp-dummy-line nil))
3924
3925             ;; Compute the mark.
3926             (setq gnus-tmp-unread (gnus-article-mark number))
3927
3928             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3929                                   gnus-tmp-header gnus-tmp-level)
3930                   gnus-newsgroup-data)
3931
3932             ;; Actually insert the line.
3933             (setq
3934              gnus-tmp-subject-or-nil
3935              (cond
3936               ((and gnus-thread-ignore-subject
3937                     gnus-tmp-prev-subject
3938                     (not (inline (gnus-subject-equal
3939                                   gnus-tmp-prev-subject subject))))
3940                subject)
3941               ((zerop gnus-tmp-level)
3942                (if (and (eq gnus-summary-make-false-root 'empty)
3943                         (memq number gnus-tmp-gathered)
3944                         gnus-tmp-prev-subject
3945                         (inline (gnus-subject-equal
3946                                  gnus-tmp-prev-subject subject)))
3947                    gnus-summary-same-subject
3948                  subject))
3949               (t gnus-summary-same-subject)))
3950             (if (and (eq gnus-summary-make-false-root 'adopt)
3951                      (= gnus-tmp-level 1)
3952                      (memq number gnus-tmp-gathered))
3953                 (setq gnus-tmp-opening-bracket ?\<
3954                       gnus-tmp-closing-bracket ?\>)
3955               (setq gnus-tmp-opening-bracket ?\[
3956                     gnus-tmp-closing-bracket ?\]))
3957             (setq
3958              gnus-tmp-indentation
3959              (aref gnus-thread-indent-array gnus-tmp-level)
3960              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3961              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3962                                 gnus-summary-default-score 0)
3963              gnus-tmp-score-char
3964              (if (or (null gnus-summary-default-score)
3965                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3966                          gnus-summary-zcore-fuzz))
3967                  ? ;Whitespace
3968                (if (< gnus-tmp-score gnus-summary-default-score)
3969                    gnus-score-below-mark gnus-score-over-mark))
3970              gnus-tmp-replied
3971              (cond ((memq number gnus-newsgroup-processable)
3972                     gnus-process-mark)
3973                    ((memq number gnus-newsgroup-cached)
3974                     gnus-cached-mark)
3975                    ((memq number gnus-newsgroup-replied)
3976                     gnus-replied-mark)
3977                    ((memq number gnus-newsgroup-saved)
3978                     gnus-saved-mark)
3979                    (t gnus-unread-mark))
3980              gnus-tmp-from (mail-header-from gnus-tmp-header)
3981              gnus-tmp-name
3982              (cond
3983               ((string-match "<[^>]+> *$" gnus-tmp-from)
3984                (setq beg-match (match-beginning 0))
3985                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3986                         (substring gnus-tmp-from (1+ (match-beginning 0))
3987                                    (1- (match-end 0))))
3988                    (substring gnus-tmp-from 0 beg-match)))
3989               ((string-match "(.+)" gnus-tmp-from)
3990                (substring gnus-tmp-from
3991                           (1+ (match-beginning 0)) (1- (match-end 0))))
3992               (t gnus-tmp-from)))
3993             (when (string= gnus-tmp-name "")
3994               (setq gnus-tmp-name gnus-tmp-from))
3995             (unless (numberp gnus-tmp-lines)
3996               (setq gnus-tmp-lines 0))
3997             (gnus-put-text-property
3998              (point)
3999              (progn (eval gnus-summary-line-format-spec) (point))
4000              'gnus-number number)
4001             (when gnus-visual-p
4002               (forward-line -1)
4003               (gnus-run-hooks 'gnus-summary-update-hook)
4004               (forward-line 1))
4005
4006             (setq gnus-tmp-prev-subject subject)))
4007
4008         (when (nth 1 thread)
4009           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4010         (incf gnus-tmp-level)
4011         (setq threads (if thread-end nil (cdar thread)))
4012         (unless threads
4013           (setq gnus-tmp-level 0)))))
4014   (gnus-message 7 "Generating summary...done"))
4015
4016 (defun gnus-summary-prepare-unthreaded (headers)
4017   "Generate an unthreaded summary buffer based on HEADERS."
4018   (let (header number mark)
4019
4020     (beginning-of-line)
4021
4022     (while headers
4023       ;; We may have to root out some bad articles...
4024       (when (memq (setq number (mail-header-number
4025                                 (setq header (pop headers))))
4026                   gnus-newsgroup-limit)
4027         ;; Mark article as read when it has a low score.
4028         (when (and gnus-summary-mark-below
4029                    (< (or (cdr (assq number gnus-newsgroup-scored))
4030                           gnus-summary-default-score 0)
4031                       gnus-summary-mark-below)
4032                    (not (gnus-summary-article-ancient-p number)))
4033           (setq gnus-newsgroup-unreads
4034                 (delq number gnus-newsgroup-unreads))
4035           (if gnus-newsgroup-auto-expire
4036               (push number gnus-newsgroup-expirable)
4037             (push (cons number gnus-low-score-mark)
4038                   gnus-newsgroup-reads)))
4039
4040         (setq mark (gnus-article-mark number))
4041         (push (gnus-data-make number mark (1+ (point)) header 0)
4042               gnus-newsgroup-data)
4043         (gnus-summary-insert-line
4044          header 0 number
4045          mark (memq number gnus-newsgroup-replied)
4046          (memq number gnus-newsgroup-expirable)
4047          (mail-header-subject header) nil
4048          (cdr (assq number gnus-newsgroup-scored))
4049          (memq number gnus-newsgroup-processable))))))
4050
4051 (defun gnus-summary-remove-list-identifiers ()
4052   "Remove list identifiers in `gnus-list-identifiers' from articles in
4053 the current group."
4054   (let ((regexp (if (stringp gnus-list-identifiers)
4055                     gnus-list-identifiers
4056                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4057     (when regexp
4058       (dolist (header gnus-newsgroup-headers)
4059         (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4060                             (mail-header-subject header))
4061           (mail-header-set-subject
4062            header (concat (substring (mail-header-subject header)
4063                                      0 (match-beginning 2))
4064                           (substring (mail-header-subject header)
4065                                      (match-end 2)))))))))
4066
4067 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4068   "Select newsgroup GROUP.
4069 If READ-ALL is non-nil, all articles in the group are selected.
4070 If SELECT-ARTICLES, only select those articles from GROUP."
4071   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4072          ;;!!! Dirty hack; should be removed.
4073          (gnus-summary-ignore-duplicates
4074           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4075               t
4076             gnus-summary-ignore-duplicates))
4077          (info (nth 2 entry))
4078          articles fetched-articles cached)
4079
4080     (unless (gnus-check-server
4081              (setq gnus-current-select-method
4082                    (gnus-find-method-for-group group)))
4083       (error "Couldn't open server"))
4084
4085     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4086         (gnus-activate-group group)     ; Or we can activate it...
4087         (progn                          ; Or we bug out.
4088           (when (equal major-mode 'gnus-summary-mode)
4089             (kill-buffer (current-buffer)))
4090           (error "Couldn't request group %s: %s"
4091                  group (gnus-status-message group))))
4092
4093     (unless (gnus-request-group group t)
4094       (when (equal major-mode 'gnus-summary-mode)
4095         (kill-buffer (current-buffer)))
4096       (error "Couldn't request group %s: %s"
4097              group (gnus-status-message group)))
4098
4099     (setq gnus-newsgroup-name group)
4100     (setq gnus-newsgroup-unselected nil)
4101     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4102     (gnus-summary-setup-default-charset)
4103
4104     ;; Adjust and set lists of article marks.
4105     (when info
4106       (gnus-adjust-marked-articles info))
4107
4108     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4109     (setq cached
4110           (if (gnus-virtual-group-p group)
4111               gnus-newsgroup-cached
4112             (gnus-cache-articles-in-group group)))
4113
4114     (setq gnus-newsgroup-unreads
4115           (gnus-set-difference
4116            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4117            gnus-newsgroup-dormant))
4118
4119     (setq gnus-newsgroup-processable nil)
4120
4121     (gnus-update-read-articles group gnus-newsgroup-unreads)
4122
4123     (if (setq articles select-articles)
4124         (setq gnus-newsgroup-unselected
4125               (gnus-sorted-intersection
4126                gnus-newsgroup-unreads
4127                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4128       (setq articles (gnus-articles-to-read group read-all)))
4129
4130     (cond
4131      ((null articles)
4132       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4133       'quit)
4134      ((eq articles 0) nil)
4135      (t
4136       ;; Init the dependencies hash table.
4137       (setq gnus-newsgroup-dependencies
4138             (gnus-make-hashtable (length articles)))
4139       (gnus-set-global-variables)
4140       ;; Retrieve the headers and read them in.
4141       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4142       (setq gnus-newsgroup-headers
4143             (gnus-retrieve-parsed-headers
4144              articles gnus-newsgroup-name
4145              ;; We might want to fetch old headers, but
4146              ;; not if there is only 1 article.
4147              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4148                            (not (numberp gnus-fetch-old-headers)))
4149                       (> (length articles) 1))
4150                   gnus-fetch-old-headers)))
4151       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4152
4153       ;; Suppress duplicates?
4154       (when gnus-suppress-duplicates
4155         (gnus-dup-suppress-articles))
4156
4157       ;; Set the initial limit.
4158       (setq gnus-newsgroup-limit (copy-sequence articles))
4159       ;; Remove canceled articles from the list of unread articles.
4160       (setq gnus-newsgroup-unreads
4161             (gnus-set-sorted-intersection
4162              gnus-newsgroup-unreads
4163              (setq fetched-articles
4164                    (mapcar (lambda (headers) (mail-header-number headers))
4165                            gnus-newsgroup-headers))))
4166       ;; Removed marked articles that do not exist.
4167       (gnus-update-missing-marks
4168        (gnus-sorted-complement fetched-articles articles))
4169
4170       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4171       (when cached
4172         (setq gnus-newsgroup-cached cached))
4173
4174       ;; We might want to build some more threads first.
4175       (when (and gnus-fetch-old-headers
4176                  (eq gnus-headers-retrieved-by 'nov))
4177         (if (eq gnus-fetch-old-headers 'invisible)
4178             (gnus-build-all-threads)
4179           (gnus-build-old-threads)))
4180       ;; Let the Gnus agent mark articles as read.
4181       (when gnus-agent
4182         (gnus-agent-get-undownloaded-list))
4183       ;; Remove list identifiers from subject
4184       (when gnus-list-identifiers
4185         (gnus-summary-remove-list-identifiers))
4186       ;; Check whether auto-expire is to be done in this group.
4187       (setq gnus-newsgroup-auto-expire
4188             (gnus-group-auto-expirable-p group))
4189       ;; Set up the article buffer now, if necessary.
4190       (unless gnus-single-article-buffer
4191         (gnus-article-setup-buffer))
4192       ;; First and last article in this newsgroup.
4193       (when gnus-newsgroup-headers
4194         (setq gnus-newsgroup-begin
4195               (mail-header-number (car gnus-newsgroup-headers))
4196               gnus-newsgroup-end
4197               (mail-header-number
4198                (gnus-last-element gnus-newsgroup-headers))))
4199       ;; GROUP is successfully selected.
4200       (or gnus-newsgroup-headers t)))))
4201
4202 (defun gnus-articles-to-read (group &optional read-all)
4203   ;; Find out what articles the user wants to read.
4204   (let* ((articles
4205           ;; Select all articles if `read-all' is non-nil, or if there
4206           ;; are no unread articles.
4207           (if (or read-all
4208                   (and (zerop (length gnus-newsgroup-marked))
4209                        (zerop (length gnus-newsgroup-unreads)))
4210                   (eq (gnus-group-find-parameter group 'display)
4211                       'all))
4212               (or
4213                (gnus-uncompress-range (gnus-active group))
4214                (gnus-cache-articles-in-group group))
4215             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4216                           (copy-sequence gnus-newsgroup-unreads))
4217                   '<)))
4218          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4219          (scored (length scored-list))
4220          (number (length articles))
4221          (marked (+ (length gnus-newsgroup-marked)
4222                     (length gnus-newsgroup-dormant)))
4223          (select
4224           (cond
4225            ((numberp read-all)
4226             read-all)
4227            (t
4228             (condition-case ()
4229                 (cond
4230                  ((and (or (<= scored marked) (= scored number))
4231                        (natnump gnus-large-newsgroup)
4232                        (> number gnus-large-newsgroup))
4233                   (let ((input (read-from-minibuffer
4234                                 (format
4235                                  "How many articles from %s (max %d): "
4236                                  (gnus-limit-string gnus-newsgroup-name 35)
4237                                  number)
4238                                 (static-if (< emacs-major-version 20)
4239                                     (number-to-string gnus-large-newsgroup)
4240                                   (cons
4241                                    (number-to-string gnus-large-newsgroup)
4242                                    0)))))
4243                     (if (string-match "^[ \t]*$" input)
4244                         number
4245                       input)))
4246                  ((and (> scored marked) (< scored number)
4247                        (> (- scored number) 20))
4248                   (let ((input
4249                          (read-string
4250                           (format "%s %s (%d scored, %d total): "
4251                                   "How many articles from"
4252                                   group scored number))))
4253                     (if (string-match "^[ \t]*$" input)
4254                         number input)))
4255                  (t number))
4256               (quit nil))))))
4257     (setq select (if (stringp select) (string-to-number select) select))
4258     (if (or (null select) (zerop select))
4259         select
4260       (if (and (not (zerop scored)) (<= (abs select) scored))
4261           (progn
4262             (setq articles (sort scored-list '<))
4263             (setq number (length articles)))
4264         (setq articles (copy-sequence articles)))
4265
4266       (when (< (abs select) number)
4267         (if (< select 0)
4268             ;; Select the N oldest articles.
4269             (setcdr (nthcdr (1- (abs select)) articles) nil)
4270           ;; Select the N most recent articles.
4271           (setq articles (nthcdr (- number select) articles))))
4272       (setq gnus-newsgroup-unselected
4273             (gnus-sorted-intersection
4274              gnus-newsgroup-unreads
4275              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4276       articles)))
4277
4278 (defun gnus-killed-articles (killed articles)
4279   (let (out)
4280     (while articles
4281       (when (inline (gnus-member-of-range (car articles) killed))
4282         (push (car articles) out))
4283       (setq articles (cdr articles)))
4284     out))
4285
4286 (defun gnus-uncompress-marks (marks)
4287   "Uncompress the mark ranges in MARKS."
4288   (let ((uncompressed '(score bookmark))
4289         out)
4290     (while marks
4291       (if (memq (caar marks) uncompressed)
4292           (push (car marks) out)
4293         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4294       (setq marks (cdr marks)))
4295     out))
4296
4297 (defun gnus-adjust-marked-articles (info)
4298   "Set all article lists and remove all marks that are no longer valid."
4299   (let* ((marked-lists (gnus-info-marks info))
4300          (active (gnus-active (gnus-info-group info)))
4301          (min (car active))
4302          (max (cdr active))
4303          (types gnus-article-mark-lists)
4304          (uncompressed '(score bookmark killed))
4305          marks var articles article mark)
4306
4307     (while marked-lists
4308       (setq marks (pop marked-lists))
4309       (set (setq var (intern (format "gnus-newsgroup-%s"
4310                                      (car (rassq (setq mark (car marks))
4311                                                  types)))))
4312            (if (memq (car marks) uncompressed) (cdr marks)
4313              (gnus-uncompress-range (cdr marks))))
4314
4315       (setq articles (symbol-value var))
4316
4317       ;; All articles have to be subsets of the active articles.
4318       (cond
4319        ;; Adjust "simple" lists.
4320        ((memq mark '(tick dormant expire reply save))
4321         (while articles
4322           (when (or (< (setq article (pop articles)) min) (> article max))
4323             (set var (delq article (symbol-value var))))))
4324        ;; Adjust assocs.
4325        ((memq mark uncompressed)
4326         (when (not (listp (cdr (symbol-value var))))
4327           (set var (list (symbol-value var))))
4328         (when (not (listp (cdr articles)))
4329           (setq articles (list articles)))
4330         (while articles
4331           (when (or (not (consp (setq article (pop articles))))
4332                     (< (car article) min)
4333                     (> (car article) max))
4334             (set var (delq article (symbol-value var))))))))))
4335
4336 (defun gnus-update-missing-marks (missing)
4337   "Go through the list of MISSING articles and remove them from the mark lists."
4338   (when missing
4339     (let ((types gnus-article-mark-lists)
4340           var m)
4341       ;; Go through all types.
4342       (while types
4343         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4344         (when (symbol-value var)
4345           ;; This list has articles.  So we delete all missing articles
4346           ;; from it.
4347           (setq m missing)
4348           (while m
4349             (set var (delq (pop m) (symbol-value var)))))))))
4350
4351 (defun gnus-update-marks ()
4352   "Enter the various lists of marked articles into the newsgroup info list."
4353   (let ((types gnus-article-mark-lists)
4354         (info (gnus-get-info gnus-newsgroup-name))
4355         (uncompressed '(score bookmark killed))
4356         type list newmarked symbol delta-marks)
4357     (when info
4358       ;; Add all marks lists to the list of marks lists.
4359       (while (setq type (pop types))
4360         (setq list (symbol-value
4361                           (setq symbol
4362                                 (intern (format "gnus-newsgroup-%s"
4363                                                 (car type))))))
4364
4365         (when list
4366           ;; Get rid of the entries of the articles that have the
4367           ;; default score.
4368           (when (and (eq (cdr type) 'score)
4369                      gnus-save-score
4370                      list)
4371             (let* ((arts list)
4372                    (prev (cons nil list))
4373                    (all prev))
4374               (while arts
4375                 (if (or (not (consp (car arts)))
4376                         (= (cdar arts) gnus-summary-default-score))
4377                     (setcdr prev (cdr arts))
4378                   (setq prev arts))
4379                 (setq arts (cdr arts)))
4380               (setq list (cdr all)))))
4381
4382        (or (memq (cdr type) uncompressed)
4383            (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4384        
4385        (when (gnus-check-backend-function 'request-set-mark
4386                                           gnus-newsgroup-name)
4387          ;; uncompressed:s are not proper flags (they are cons cells)
4388          ;; cache is a internal gnus flag
4389          (unless (memq (cdr type) (cons 'cache uncompressed))
4390            (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4391                   (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4392                   (add (gnus-remove-from-range (gnus-copy-sequence list) old)))
4393              (if add
4394                  (push (list add 'add (list (cdr type))) delta-marks))
4395              (if del
4396                  (push (list del 'del (list (cdr type))) delta-marks)))))
4397           
4398         (when list
4399          (push (cons (cdr type) list) newmarked)))
4400
4401       (when delta-marks
4402         (unless (gnus-check-group gnus-newsgroup-name)
4403           (error "Can't open server for %s" gnus-newsgroup-name))
4404         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4405           
4406       ;; Enter these new marks into the info of the group.
4407       (if (nthcdr 3 info)
4408           (setcar (nthcdr 3 info) newmarked)
4409         ;; Add the marks lists to the end of the info.
4410         (when newmarked
4411           (setcdr (nthcdr 2 info) (list newmarked))))
4412
4413       ;; Cut off the end of the info if there's nothing else there.
4414       (let ((i 5))
4415         (while (and (> i 2)
4416                     (not (nth i info)))
4417           (when (nthcdr (decf i) info)
4418             (setcdr (nthcdr i info) nil)))))))
4419
4420 (defun gnus-set-mode-line (where)
4421   "This function sets the mode line of the article or summary buffers.
4422 If WHERE is `summary', the summary mode line format will be used."
4423   ;; Is this mode line one we keep updated?
4424   (when (and (memq where gnus-updated-mode-lines)
4425              (symbol-value
4426               (intern (format "gnus-%s-mode-line-format-spec" where))))
4427     (let (mode-string)
4428       (save-excursion
4429         ;; We evaluate this in the summary buffer since these
4430         ;; variables are buffer-local to that buffer.
4431         (set-buffer gnus-summary-buffer)
4432         ;; We bind all these variables that are used in the `eval' form
4433         ;; below.
4434         (let* ((mformat (symbol-value
4435                          (intern
4436                           (format "gnus-%s-mode-line-format-spec" where))))
4437                (gnus-tmp-group-name gnus-newsgroup-name)
4438                (gnus-tmp-article-number (or gnus-current-article 0))
4439                (gnus-tmp-unread gnus-newsgroup-unreads)
4440                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4441                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4442                (gnus-tmp-unread-and-unselected
4443                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4444                             (zerop gnus-tmp-unselected))
4445                        "")
4446                       ((zerop gnus-tmp-unselected)
4447                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4448                       (t (format "{%d(+%d) more}"
4449                                  gnus-tmp-unread-and-unticked
4450                                  gnus-tmp-unselected))))
4451                (gnus-tmp-subject
4452                 (if (and gnus-current-headers
4453                          (vectorp gnus-current-headers))
4454                     (gnus-mode-string-quote
4455                      (mail-header-subject gnus-current-headers))
4456                   ""))
4457                bufname-length max-len
4458                gnus-tmp-header);; passed as argument to any user-format-funcs
4459           (setq mode-string (eval mformat))
4460           (setq bufname-length (if (string-match "%b" mode-string)
4461                                    (- (length
4462                                        (buffer-name
4463                                         (if (eq where 'summary)
4464                                             nil
4465                                           (get-buffer gnus-article-buffer))))
4466                                       2)
4467                                  0))
4468           (setq max-len (max 4 (if gnus-mode-non-string-length
4469                                    (- (window-width)
4470                                       gnus-mode-non-string-length
4471                                       bufname-length)
4472                                  (length mode-string))))
4473           ;; We might have to chop a bit of the string off...
4474           (when (> (length mode-string) max-len)
4475             (setq mode-string
4476                   (concat (gnus-truncate-string mode-string (- max-len 3))
4477                           "...")))
4478           ;; Pad the mode string a bit.
4479           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4480       ;; Update the mode line.
4481       (setq mode-line-buffer-identification
4482             (gnus-mode-line-buffer-identification (list mode-string)))
4483       (set-buffer-modified-p t))))
4484
4485 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4486   "Go through the HEADERS list and add all Xrefs to a hash table.
4487 The resulting hash table is returned, or nil if no Xrefs were found."
4488   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4489          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4490          (xref-hashtb (gnus-make-hashtable))
4491          start group entry number xrefs header)
4492     (while headers
4493       (setq header (pop headers))
4494       (when (and (setq xrefs (mail-header-xref header))
4495                  (not (memq (setq number (mail-header-number header))
4496                             unreads)))
4497         (setq start 0)
4498         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4499           (setq start (match-end 0))
4500           (setq group (if prefix
4501                           (concat prefix (substring xrefs (match-beginning 1)
4502                                                     (match-end 1)))
4503                         (substring xrefs (match-beginning 1) (match-end 1))))
4504           (setq number
4505                 (string-to-int (substring xrefs (match-beginning 2)
4506                                           (match-end 2))))
4507           (if (setq entry (gnus-gethash group xref-hashtb))
4508               (setcdr entry (cons number (cdr entry)))
4509             (gnus-sethash group (cons number nil) xref-hashtb)))))
4510     (and start xref-hashtb)))
4511
4512 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4513   "Look through all the headers and mark the Xrefs as read."
4514   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4515         name entry info xref-hashtb idlist method nth4)
4516     (save-excursion
4517       (set-buffer gnus-group-buffer)
4518       (when (setq xref-hashtb
4519                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4520         (mapatoms
4521          (lambda (group)
4522            (unless (string= from-newsgroup (setq name (symbol-name group)))
4523              (setq idlist (symbol-value group))
4524              ;; Dead groups are not updated.
4525              (and (prog1
4526                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4527                             info (nth 2 entry))
4528                     (when (stringp (setq nth4 (gnus-info-method info)))
4529                       (setq nth4 (gnus-server-to-method nth4))))
4530                   ;; Only do the xrefs if the group has the same
4531                   ;; select method as the group we have just read.
4532                   (or (gnus-methods-equal-p
4533                        nth4 (gnus-find-method-for-group from-newsgroup))
4534                       virtual
4535                       (equal nth4 (setq method (gnus-find-method-for-group
4536                                                 from-newsgroup)))
4537                       (and (equal (car nth4) (car method))
4538                            (equal (nth 1 nth4) (nth 1 method))))
4539                   gnus-use-cross-reference
4540                   (or (not (eq gnus-use-cross-reference t))
4541                       virtual
4542                       ;; Only do cross-references on subscribed
4543                       ;; groups, if that is what is wanted.
4544                       (<= (gnus-info-level info) gnus-level-subscribed))
4545                   (gnus-group-make-articles-read name idlist))))
4546          xref-hashtb)))))
4547
4548 (defun gnus-compute-read-articles (group articles)
4549   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4550          (info (nth 2 entry))
4551          (active (gnus-active group))
4552          ninfo)
4553     (when entry
4554       ;; First peel off all invalid article numbers.
4555       (when active
4556         (let ((ids articles)
4557               id first)
4558           (while (setq id (pop ids))
4559             (when (and first (> id (cdr active)))
4560               ;; We'll end up in this situation in one particular
4561               ;; obscure situation.  If you re-scan a group and get
4562               ;; a new article that is cross-posted to a different
4563               ;; group that has not been re-scanned, you might get
4564               ;; crossposted article that has a higher number than
4565               ;; Gnus believes possible.  So we re-activate this
4566               ;; group as well.  This might mean doing the
4567               ;; crossposting thingy will *increase* the number
4568               ;; of articles in some groups.  Tsk, tsk.
4569               (setq active (or (gnus-activate-group group) active)))
4570             (when (or (> id (cdr active))
4571                       (< id (car active)))
4572               (setq articles (delq id articles))))))
4573       ;; If the read list is nil, we init it.
4574       (if (and active
4575                (null (gnus-info-read info))
4576                (> (car active) 1))
4577           (setq ninfo (cons 1 (1- (car active))))
4578         (setq ninfo (gnus-info-read info)))
4579       ;; Then we add the read articles to the range.
4580       (gnus-add-to-range
4581        ninfo (setq articles (sort articles '<))))))
4582
4583 (defun gnus-group-make-articles-read (group articles)
4584   "Update the info of GROUP to say that ARTICLES are read."
4585   (let* ((num 0)
4586          (entry (gnus-gethash group gnus-newsrc-hashtb))
4587          (info (nth 2 entry))
4588          (active (gnus-active group))
4589          range)
4590     (when entry
4591       (setq range (gnus-compute-read-articles group articles))
4592       (save-excursion
4593         (set-buffer gnus-group-buffer)
4594         (gnus-undo-register
4595           `(progn
4596              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4597              (gnus-info-set-read ',info ',(gnus-info-read info))
4598              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4599              (gnus-group-update-group ,group t))))
4600       ;; Add the read articles to the range.
4601       (gnus-info-set-read info range)
4602       ;; Then we have to re-compute how many unread
4603       ;; articles there are in this group.
4604       (when active
4605         (cond
4606          ((not range)
4607           (setq num (- (1+ (cdr active)) (car active))))
4608          ((not (listp (cdr range)))
4609           (setq num (- (cdr active) (- (1+ (cdr range))
4610                                        (car range)))))
4611          (t
4612           (while range
4613             (if (numberp (car range))
4614                 (setq num (1+ num))
4615               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4616             (setq range (cdr range)))
4617           (setq num (- (cdr active) num))))
4618         ;; Update the number of unread articles.
4619         (setcar entry num)
4620         ;; Update the group buffer.
4621         (gnus-group-update-group group t)))))
4622
4623 (defvar gnus-newsgroup-none-id 0)
4624
4625 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4626   (let ((cur nntp-server-buffer)
4627         (dependencies
4628          (or dependencies
4629              (save-excursion (set-buffer gnus-summary-buffer)
4630                              gnus-newsgroup-dependencies)))
4631         headers id end ref
4632         (mail-parse-charset gnus-newsgroup-charset)
4633         (mail-parse-ignored-charsets
4634          (save-excursion (condition-case nil
4635                              (set-buffer gnus-summary-buffer)
4636                            (error))
4637                          gnus-newsgroup-ignored-charsets)))
4638     (save-excursion
4639       (set-buffer nntp-server-buffer)
4640       ;; Translate all TAB characters into SPACE characters.
4641       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4642       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4643       (gnus-run-hooks 'gnus-parse-headers-hook)
4644       (let ((case-fold-search t)
4645             in-reply-to header p lines chars ctype)
4646         (goto-char (point-min))
4647         ;; Search to the beginning of the next header.  Error messages
4648         ;; do not begin with 2 or 3.
4649         (while (re-search-forward "^[23][0-9]+ " nil t)
4650           (setq id nil
4651                 ref nil)
4652           ;; This implementation of this function, with nine
4653           ;; search-forwards instead of the one re-search-forward and
4654           ;; a case (which basically was the old function) is actually
4655           ;; about twice as fast, even though it looks messier.  You
4656           ;; can't have everything, I guess.  Speed and elegance
4657           ;; doesn't always go hand in hand.
4658           (setq
4659            header
4660            (make-full-mail-header
4661             ;; Number.
4662             (prog1
4663                 (read cur)
4664               (end-of-line)
4665               (setq p (point))
4666               (narrow-to-region (point)
4667                                 (or (and (search-forward "\n.\n" nil t)
4668                                          (- (point) 2))
4669                                     (point))))
4670             ;; Subject.
4671             (progn
4672               (goto-char p)
4673               (if (search-forward "\nsubject: " nil t)
4674                   (buffer-substring (match-end 0) (std11-field-end))
4675                 "(none)"))
4676             ;; From.
4677             (progn
4678               (goto-char p)
4679               (if (search-forward "\nfrom: " nil t)
4680                   (buffer-substring (match-end 0) (std11-field-end))
4681                 "(nobody)"))
4682             ;; Date.
4683             (progn
4684               (goto-char p)
4685               (if (search-forward "\ndate: " nil t)
4686                   (buffer-substring (match-end 0) (std11-field-end))
4687                 ""))
4688             ;; Message-ID.
4689             (progn
4690               (goto-char p)
4691               (setq id (if (re-search-forward
4692                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4693                            ;; We do it this way to make sure the Message-ID
4694                            ;; is (somewhat) syntactically valid.
4695                            (buffer-substring (match-beginning 1)
4696                                              (match-end 1))
4697                          ;; If there was no message-id, we just fake one
4698                          ;; to make subsequent routines simpler.
4699                          (nnheader-generate-fake-message-id))))
4700             ;; References.
4701             (progn
4702               (goto-char p)
4703               (if (search-forward "\nreferences: " nil t)
4704                   (progn
4705                     (setq end (point))
4706                     (prog1
4707                         (buffer-substring (match-end 0) (std11-field-end))
4708                       (setq ref
4709                             (buffer-substring
4710                              (progn
4711                                ;; (end-of-line)
4712                                (search-backward ">" end t)
4713                                (1+ (point)))
4714                              (progn
4715                                (search-backward "<" end t)
4716                                (point))))))
4717                 ;; Get the references from the in-reply-to header if there
4718                 ;; were no references and the in-reply-to header looks
4719                 ;; promising.
4720                 (if (and (search-forward "\nin-reply-to: " nil t)
4721                          (setq in-reply-to
4722                                (buffer-substring (match-end 0)
4723                                                  (std11-field-end)))
4724                          (string-match "<[^>]+>" in-reply-to))
4725                     (let (ref2)
4726                       (setq ref (substring in-reply-to (match-beginning 0)
4727                                            (match-end 0)))
4728                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4729                         (setq ref2 (substring in-reply-to (match-beginning 0)
4730                                               (match-end 0)))
4731                         (when (> (length ref2) (length ref))
4732                           (setq ref ref2)))
4733                       ref)
4734                   (setq ref nil))))
4735             ;; Chars.
4736             (progn
4737               (goto-char p)
4738               (if (search-forward "\nchars: " nil t)
4739                   (if (numberp (setq chars (ignore-errors (read cur))))
4740                       chars 0)
4741                 0))
4742             ;; Lines.
4743             (progn
4744               (goto-char p)
4745               (if (search-forward "\nlines: " nil t)
4746                   (if (numberp (setq lines (ignore-errors (read cur))))
4747                       lines 0)
4748                 0))
4749             ;; Xref.
4750             (progn
4751               (goto-char p)
4752               (and (search-forward "\nxref: " nil t)
4753                    (buffer-substring (match-end 0) (std11-field-end))))
4754             ;; Extra.
4755             (when gnus-extra-headers
4756               (let ((extra gnus-extra-headers)
4757                     out)
4758                 (while extra
4759                   (goto-char p)
4760                   (when (search-forward
4761                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4762                     (push (cons (car extra)
4763                                 (buffer-substring (match-end 0)
4764                                                   (std11-field-end)))
4765                           out))
4766                   (pop extra))
4767                 out))))
4768           (goto-char p)
4769           (if (and (search-forward "\ncontent-type: " nil t)
4770                    (setq ctype
4771                          (buffer-substring (match-end 0) (std11-field-end))))
4772               (mime-entity-set-content-type-internal
4773                header (mime-parse-Content-Type ctype)))
4774           (when (equal id ref)
4775             (setq ref nil))
4776
4777           (when gnus-alter-header-function
4778             (funcall gnus-alter-header-function header)
4779             (setq id (mail-header-id header)
4780                   ref (gnus-parent-id (mail-header-references header))))
4781
4782           (when (setq header
4783                       (gnus-dependencies-add-header
4784                        header dependencies force-new))
4785             (push header headers))
4786           (goto-char (point-max))
4787           (widen))
4788         (nreverse headers)))))
4789
4790 ;; Goes through the xover lines and returns a list of vectors
4791 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4792                                                   force-new dependencies
4793                                                   group also-fetch-heads)
4794   "Parse the news overview data in the server buffer, and return a
4795 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4796   ;; Get the Xref when the users reads the articles since most/some
4797   ;; NNTP servers do not include Xrefs when using XOVER.
4798   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4799   (let ((mail-parse-charset gnus-newsgroup-charset)
4800         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4801         (cur nntp-server-buffer)
4802         (dependencies (or dependencies gnus-newsgroup-dependencies))
4803         number headers header)
4804     (save-excursion
4805       (set-buffer nntp-server-buffer)
4806       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4807       ;; Allow the user to mangle the headers before parsing them.
4808       (gnus-run-hooks 'gnus-parse-headers-hook)
4809       (goto-char (point-min))
4810       (while (not (eobp))
4811         (condition-case ()
4812             (while (and sequence (not (eobp)))
4813               (setq number (read cur))
4814               (while (and sequence
4815                           (< (car sequence) number))
4816                 (setq sequence (cdr sequence)))
4817               (and sequence
4818                    (eq number (car sequence))
4819                    (progn
4820                      (setq sequence (cdr sequence))
4821                      (setq header (inline
4822                                     (gnus-nov-parse-line
4823                                      number dependencies force-new))))
4824                    (push header headers))
4825               (forward-line 1))
4826           (error
4827            (gnus-error 4 "Strange nov line (%d)"
4828                        (count-lines (point-min) (point)))))
4829         (forward-line 1))
4830       ;; A common bug in inn is that if you have posted an article and
4831       ;; then retrieves the active file, it will answer correctly --
4832       ;; the new article is included.  However, a NOV entry for the
4833       ;; article may not have been generated yet, so this may fail.
4834       ;; We work around this problem by retrieving the last few
4835       ;; headers using HEAD.
4836       (if (or (not also-fetch-heads)
4837               (not sequence))
4838           ;; We (probably) got all the headers.
4839           (nreverse headers)
4840         (let ((gnus-nov-is-evil t))
4841           (nconc
4842            (nreverse headers)
4843            (gnus-retrieve-parsed-headers sequence group)
4844            ))))))
4845
4846 (defun gnus-article-get-xrefs ()
4847   "Fill in the Xref value in `gnus-current-headers', if necessary.
4848 This is meant to be called in `gnus-article-internal-prepare-hook'."
4849   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4850                                  gnus-current-headers)))
4851     (or (not gnus-use-cross-reference)
4852         (not headers)
4853         (and (mail-header-xref headers)
4854              (not (string= (mail-header-xref headers) "")))
4855         (let ((case-fold-search t)
4856               xref)
4857           (save-restriction
4858             (nnheader-narrow-to-headers)
4859             (goto-char (point-min))
4860             (when (or (and (eq (downcase (char-after)) ?x)
4861                            (looking-at "Xref:"))
4862                       (search-forward "\nXref:" nil t))
4863               (goto-char (1+ (match-end 0)))
4864               (setq xref (buffer-substring (point)
4865                                            (progn (end-of-line) (point))))
4866               (mail-header-set-xref headers xref)))))))
4867
4868 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4869   "Find article ID and insert the summary line for that article.
4870 OLD-HEADER can either be a header or a line number to insert
4871 the subject line on."
4872   (let* ((line (and (numberp old-header) old-header))
4873          (old-header (and (vectorp old-header) old-header))
4874          (header (cond ((and old-header use-old-header)
4875                         old-header)
4876                        ((and (numberp id)
4877                              (gnus-number-to-header id))
4878                         (gnus-number-to-header id))
4879                        (t
4880                         (gnus-read-header id))))
4881          (number (and (numberp id) id))
4882          d)
4883     (when header
4884       ;; Rebuild the thread that this article is part of and go to the
4885       ;; article we have fetched.
4886       (when (and (not gnus-show-threads)
4887                  old-header)
4888         (when (and number
4889                    (setq d (gnus-data-find (mail-header-number old-header))))
4890           (goto-char (gnus-data-pos d))
4891           (gnus-data-remove
4892            number
4893            (- (gnus-point-at-bol)
4894               (prog1
4895                   (1+ (gnus-point-at-eol))
4896                 (gnus-delete-line))))))
4897       (when old-header
4898         (mail-header-set-number header (mail-header-number old-header)))
4899       (setq gnus-newsgroup-sparse
4900             (delq (setq number (mail-header-number header))
4901                   gnus-newsgroup-sparse))
4902       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4903       (push number gnus-newsgroup-limit)
4904       (gnus-rebuild-thread (mail-header-id header) line)
4905       (gnus-summary-goto-subject number nil t))
4906     (when (and (numberp number)
4907                (> number 0))
4908       ;; We have to update the boundaries even if we can't fetch the
4909       ;; article if ID is a number -- so that the next `P' or `N'
4910       ;; command will fetch the previous (or next) article even
4911       ;; if the one we tried to fetch this time has been canceled.
4912       (when (> number gnus-newsgroup-end)
4913         (setq gnus-newsgroup-end number))
4914       (when (< number gnus-newsgroup-begin)
4915         (setq gnus-newsgroup-begin number))
4916       (setq gnus-newsgroup-unselected
4917             (delq number gnus-newsgroup-unselected)))
4918     ;; Report back a success?
4919     (and header (mail-header-number header))))
4920
4921 ;;; Process/prefix in the summary buffer
4922
4923 (defun gnus-summary-work-articles (n)
4924   "Return a list of articles to be worked upon.
4925 The prefix argument, the list of process marked articles, and the
4926 current article will be taken into consideration."
4927   (save-excursion
4928     (set-buffer gnus-summary-buffer)
4929     (cond
4930      (n
4931       ;; A numerical prefix has been given.
4932       (setq n (prefix-numeric-value n))
4933       (let ((backward (< n 0))
4934             (n (abs (prefix-numeric-value n)))
4935             articles article)
4936         (save-excursion
4937           (while
4938               (and (> n 0)
4939                    (push (setq article (gnus-summary-article-number))
4940                          articles)
4941                    (if backward
4942                        (gnus-summary-find-prev nil article)
4943                      (gnus-summary-find-next nil article)))
4944             (decf n)))
4945         (nreverse articles)))
4946      ((and (gnus-region-active-p) (mark))
4947       (message "region active")
4948       ;; Work on the region between point and mark.
4949       (let ((max (max (point) (mark)))
4950             articles article)
4951         (save-excursion
4952           (goto-char (min (point) (mark)))
4953           (while
4954               (and
4955                (push (setq article (gnus-summary-article-number)) articles)
4956                (gnus-summary-find-next nil article)
4957                (< (point) max)))
4958           (nreverse articles))))
4959      (gnus-newsgroup-processable
4960       ;; There are process-marked articles present.
4961       ;; Save current state.
4962       (gnus-summary-save-process-mark)
4963       ;; Return the list.
4964       (reverse gnus-newsgroup-processable))
4965      (t
4966       ;; Just return the current article.
4967       (list (gnus-summary-article-number))))))
4968
4969 (defmacro gnus-summary-iterate (arg &rest forms)
4970   "Iterate over the process/prefixed articles and do FORMS.
4971 ARG is the interactive prefix given to the command.  FORMS will be
4972 executed with point over the summary line of the articles."
4973   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4974     `(let ((,articles (gnus-summary-work-articles ,arg)))
4975        (while ,articles
4976          (gnus-summary-goto-subject (car ,articles))
4977          ,@forms
4978          (pop ,articles)))))
4979
4980 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4981 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4982
4983 (defun gnus-summary-save-process-mark ()
4984   "Push the current set of process marked articles on the stack."
4985   (interactive)
4986   (push (copy-sequence gnus-newsgroup-processable)
4987         gnus-newsgroup-process-stack))
4988
4989 (defun gnus-summary-kill-process-mark ()
4990   "Push the current set of process marked articles on the stack and unmark."
4991   (interactive)
4992   (gnus-summary-save-process-mark)
4993   (gnus-summary-unmark-all-processable))
4994
4995 (defun gnus-summary-yank-process-mark ()
4996   "Pop the last process mark state off the stack and restore it."
4997   (interactive)
4998   (unless gnus-newsgroup-process-stack
4999     (error "Empty mark stack"))
5000   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5001
5002 (defun gnus-summary-process-mark-set (set)
5003   "Make SET into the current process marked articles."
5004   (gnus-summary-unmark-all-processable)
5005   (while set
5006     (gnus-summary-set-process-mark (pop set))))
5007
5008 ;;; Searching and stuff
5009
5010 (defun gnus-summary-search-group (&optional backward use-level)
5011   "Search for next unread newsgroup.
5012 If optional argument BACKWARD is non-nil, search backward instead."
5013   (save-excursion
5014     (set-buffer gnus-group-buffer)
5015     (when (gnus-group-search-forward
5016            backward nil (if use-level (gnus-group-group-level) nil))
5017       (gnus-group-group-name))))
5018
5019 (defun gnus-summary-best-group (&optional exclude-group)
5020   "Find the name of the best unread group.
5021 If EXCLUDE-GROUP, do not go to this group."
5022   (save-excursion
5023     (set-buffer gnus-group-buffer)
5024     (save-excursion
5025       (gnus-group-best-unread-group exclude-group))))
5026
5027 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5028   (if backward (gnus-summary-find-prev)
5029     (let* ((dummy (gnus-summary-article-intangible-p))
5030            (article (or article (gnus-summary-article-number)))
5031            (arts (gnus-data-find-list article))
5032            result)
5033       (when (and (not dummy)
5034                  (or (not gnus-summary-check-current)
5035                      (not unread)
5036                      (not (gnus-data-unread-p (car arts)))))
5037         (setq arts (cdr arts)))
5038       (when (setq result
5039                   (if unread
5040                       (progn
5041                         (while arts
5042                           (when (or (and undownloaded
5043                                          (eq gnus-undownloaded-mark
5044                                              (gnus-data-mark (car arts))))
5045                                     (gnus-data-unread-p (car arts)))
5046                             (setq result (car arts)
5047                                   arts nil))
5048                           (setq arts (cdr arts)))
5049                         result)
5050                     (car arts)))
5051         (goto-char (gnus-data-pos result))
5052         (gnus-data-number result)))))
5053
5054 (defun gnus-summary-find-prev (&optional unread article)
5055   (let* ((eobp (eobp))
5056          (article (or article (gnus-summary-article-number)))
5057          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5058          result)
5059     (when (and (not eobp)
5060                (or (not gnus-summary-check-current)
5061                    (not unread)
5062                    (not (gnus-data-unread-p (car arts)))))
5063       (setq arts (cdr arts)))
5064     (when (setq result
5065                 (if unread
5066                     (progn
5067                       (while arts
5068                         (when (gnus-data-unread-p (car arts))
5069                           (setq result (car arts)
5070                                 arts nil))
5071                         (setq arts (cdr arts)))
5072                       result)
5073                   (car arts)))
5074       (goto-char (gnus-data-pos result))
5075       (gnus-data-number result))))
5076
5077 (defun gnus-summary-find-subject (subject &optional unread backward article)
5078   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5079          (article (or article (gnus-summary-article-number)))
5080          (articles (gnus-data-list backward))
5081          (arts (gnus-data-find-list article articles))
5082          result)
5083     (when (or (not gnus-summary-check-current)
5084               (not unread)
5085               (not (gnus-data-unread-p (car arts))))
5086       (setq arts (cdr arts)))
5087     (while arts
5088       (and (or (not unread)
5089                (gnus-data-unread-p (car arts)))
5090            (vectorp (gnus-data-header (car arts)))
5091            (gnus-subject-equal
5092             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5093            (setq result (car arts)
5094                  arts nil))
5095       (setq arts (cdr arts)))
5096     (and result
5097          (goto-char (gnus-data-pos result))
5098          (gnus-data-number result))))
5099
5100 (defun gnus-summary-search-forward (&optional unread subject backward)
5101   "Search forward for an article.
5102 If UNREAD, look for unread articles.  If SUBJECT, look for
5103 articles with that subject.  If BACKWARD, search backward instead."
5104   (cond (subject (gnus-summary-find-subject subject unread backward))
5105         (backward (gnus-summary-find-prev unread))
5106         (t (gnus-summary-find-next unread))))
5107
5108 (defun gnus-recenter (&optional n)
5109   "Center point in window and redisplay frame.
5110 Also do horizontal recentering."
5111   (interactive "P")
5112   (when (and gnus-auto-center-summary
5113              (not (eq gnus-auto-center-summary 'vertical)))
5114     (gnus-horizontal-recenter))
5115   (recenter n))
5116
5117 (defun gnus-summary-recenter ()
5118   "Center point in the summary window.
5119 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5120 displayed, no centering will be performed."
5121   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5122   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5123   (let* ((top (cond ((< (window-height) 4) 0)
5124                     ((< (window-height) 7) 1)
5125                     (t (if (numberp gnus-auto-center-summary)
5126                            gnus-auto-center-summary
5127                          2))))
5128          (height (1- (window-height)))
5129          (bottom (save-excursion (goto-char (point-max))
5130                                  (forward-line (- height))
5131                                  (point)))
5132          (window (get-buffer-window (current-buffer))))
5133     ;; The user has to want it.
5134     (when gnus-auto-center-summary
5135       (when (get-buffer-window gnus-article-buffer)
5136         ;; Only do recentering when the article buffer is displayed,
5137         ;; Set the window start to either `bottom', which is the biggest
5138         ;; possible valid number, or the second line from the top,
5139         ;; whichever is the least.
5140         (set-window-start
5141          window (min bottom (save-excursion
5142                               (forward-line (- top)) (point)))))
5143       ;; Do horizontal recentering while we're at it.
5144       (when (and (get-buffer-window (current-buffer) t)
5145                  (not (eq gnus-auto-center-summary 'vertical)))
5146         (let ((selected (selected-window)))
5147           (select-window (get-buffer-window (current-buffer) t))
5148           (gnus-summary-position-point)
5149           (gnus-horizontal-recenter)
5150           (select-window selected))))))
5151
5152 (defun gnus-summary-jump-to-group (newsgroup)
5153   "Move point to NEWSGROUP in group mode buffer."
5154   ;; Keep update point of group mode buffer if visible.
5155   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5156       (save-window-excursion
5157         ;; Take care of tree window mode.
5158         (when (get-buffer-window gnus-group-buffer)
5159           (pop-to-buffer gnus-group-buffer))
5160         (gnus-group-jump-to-group newsgroup))
5161     (save-excursion
5162       ;; Take care of tree window mode.
5163       (if (get-buffer-window gnus-group-buffer)
5164           (pop-to-buffer gnus-group-buffer)
5165         (set-buffer gnus-group-buffer))
5166       (gnus-group-jump-to-group newsgroup))))
5167
5168 ;; This function returns a list of article numbers based on the
5169 ;; difference between the ranges of read articles in this group and
5170 ;; the range of active articles.
5171 (defun gnus-list-of-unread-articles (group)
5172   (let* ((read (gnus-info-read (gnus-get-info group)))
5173          (active (or (gnus-active group) (gnus-activate-group group)))
5174          (last (cdr active))
5175          first nlast unread)
5176     ;; If none are read, then all are unread.
5177     (if (not read)
5178         (setq first (car active))
5179       ;; If the range of read articles is a single range, then the
5180       ;; first unread article is the article after the last read
5181       ;; article.  Sounds logical, doesn't it?
5182       (if (not (listp (cdr read)))
5183           (setq first (max (car active) (1+ (cdr read))))
5184         ;; `read' is a list of ranges.
5185         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5186                                   (caar read)))
5187                   1)
5188           (setq first (car active)))
5189         (while read
5190           (when first
5191             (while (< first nlast)
5192               (push first unread)
5193               (setq first (1+ first))))
5194           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5195           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5196           (setq read (cdr read)))))
5197     ;; And add the last unread articles.
5198     (while (<= first last)
5199       (push first unread)
5200       (setq first (1+ first)))
5201     ;; Return the list of unread articles.
5202     (delq 0 (nreverse unread))))
5203
5204 (defun gnus-list-of-read-articles (group)
5205   "Return a list of unread, unticked and non-dormant articles."
5206   (let* ((info (gnus-get-info group))
5207          (marked (gnus-info-marks info))
5208          (active (gnus-active group)))
5209     (and info active
5210          (gnus-set-difference
5211           (gnus-sorted-complement
5212            (gnus-uncompress-range active)
5213            (gnus-list-of-unread-articles group))
5214           (append
5215            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5216            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5217
5218 ;; Various summary commands
5219
5220 (defun gnus-summary-select-article-buffer ()
5221   "Reconfigure windows to show article buffer."
5222   (interactive)
5223   (if (not (gnus-buffer-live-p gnus-article-buffer))
5224       (error "There is no article buffer for this summary buffer")
5225     (gnus-configure-windows 'article)
5226     (select-window (get-buffer-window gnus-article-buffer))))
5227
5228 (defun gnus-summary-universal-argument (arg)
5229   "Perform any operation on all articles that are process/prefixed."
5230   (interactive "P")
5231   (let ((articles (gnus-summary-work-articles arg))
5232         func article)
5233     (if (eq
5234          (setq
5235           func
5236           (key-binding
5237            (read-key-sequence
5238             (substitute-command-keys
5239              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5240              ))))
5241          'undefined)
5242         (gnus-error 1 "Undefined key")
5243       (save-excursion
5244         (while articles
5245           (gnus-summary-goto-subject (setq article (pop articles)))
5246           (let (gnus-newsgroup-processable)
5247             (command-execute func))
5248           (gnus-summary-remove-process-mark article)))))
5249   (gnus-summary-position-point))
5250
5251 (defun gnus-summary-toggle-truncation (&optional arg)
5252   "Toggle truncation of summary lines.
5253 With arg, turn line truncation on iff arg is positive."
5254   (interactive "P")
5255   (setq truncate-lines
5256         (if (null arg) (not truncate-lines)
5257           (> (prefix-numeric-value arg) 0)))
5258   (redraw-display))
5259
5260 (defun gnus-summary-reselect-current-group (&optional all rescan)
5261   "Rescan the current newsgroup, exit and then reselect it.
5262 The prefix argument ALL means to select all articles."
5263   (interactive "P")
5264   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5265     (error "Ephemeral groups can't be reselected"))
5266   (let ((current-subject (gnus-summary-article-number))
5267         (group gnus-newsgroup-name))
5268     (save-excursion
5269       (set-buffer gnus-group-buffer)
5270       ;; We have to adjust the point of group mode buffer because
5271       ;; point was moved to the next unread newsgroup by exiting.
5272       (gnus-summary-jump-to-group group)
5273       (when rescan
5274         (save-excursion
5275           (gnus-group-get-new-news-this-group 1))))
5276     (setq gnus-newsgroup-begin nil)
5277     (gnus-summary-exit)
5278     (gnus-group-read-group all t group)
5279     (gnus-summary-goto-subject current-subject nil t)))
5280
5281 (defun gnus-summary-rescan-group (&optional all)
5282   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5283   (interactive "P")
5284   (gnus-summary-reselect-current-group all t))
5285
5286 (defun gnus-summary-update-info (&optional non-destructive)
5287   (save-excursion
5288     (let ((group gnus-newsgroup-name))
5289       (when group
5290         (when gnus-newsgroup-kill-headers
5291           (setq gnus-newsgroup-killed
5292                 (gnus-compress-sequence
5293                  (nconc
5294                   (gnus-set-sorted-intersection
5295                    (gnus-uncompress-range gnus-newsgroup-killed)
5296                    (setq gnus-newsgroup-unselected
5297                          (sort gnus-newsgroup-unselected '<)))
5298                   (setq gnus-newsgroup-unreads
5299                         (sort gnus-newsgroup-unreads '<)))
5300                  t)))
5301         (unless (listp (cdr gnus-newsgroup-killed))
5302           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5303         (let ((headers gnus-newsgroup-headers))
5304           ;; Set the new ranges of read articles.
5305           (save-excursion
5306             (set-buffer gnus-group-buffer)
5307             (gnus-undo-force-boundary))
5308           (gnus-update-read-articles
5309            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5310           ;; Set the current article marks.
5311           (let ((gnus-newsgroup-scored
5312                  (if (and (not gnus-save-score)
5313                           (not non-destructive))
5314                      nil
5315                    gnus-newsgroup-scored)))
5316             (save-excursion
5317               (gnus-update-marks)))
5318           ;; Do the cross-ref thing.
5319           (when gnus-use-cross-reference
5320             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5321           ;; Do not switch windows but change the buffer to work.
5322           (set-buffer gnus-group-buffer)
5323           (unless (gnus-ephemeral-group-p group)
5324             (gnus-group-update-group group)))))))
5325
5326 (defun gnus-summary-save-newsrc (&optional force)
5327   "Save the current number of read/marked articles in the dribble buffer.
5328 The dribble buffer will then be saved.
5329 If FORCE (the prefix), also save the .newsrc file(s)."
5330   (interactive "P")
5331   (gnus-summary-update-info t)
5332   (if force
5333       (gnus-save-newsrc-file)
5334     (gnus-dribble-save)))
5335
5336 (defun gnus-summary-exit (&optional temporary)
5337   "Exit reading current newsgroup, and then return to group selection mode.
5338 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5339   (interactive)
5340   (gnus-set-global-variables)
5341   (gnus-kill-save-kill-buffer)
5342   (gnus-async-halt-prefetch)
5343   (let* ((group gnus-newsgroup-name)
5344          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5345          (mode major-mode)
5346          (group-point nil)
5347          (buf (current-buffer)))
5348     (unless quit-config
5349       ;; Do adaptive scoring, and possibly save score files.
5350       (when gnus-newsgroup-adaptive
5351         (gnus-score-adaptive))
5352       (when gnus-use-scoring
5353         (gnus-score-save)))
5354     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5355     ;; If we have several article buffers, we kill them at exit.
5356     (unless gnus-single-article-buffer
5357       (gnus-kill-buffer gnus-original-article-buffer)
5358       (setq gnus-article-current nil))
5359     (when gnus-use-cache
5360       (gnus-cache-possibly-remove-articles)
5361       (gnus-cache-save-buffers))
5362     (gnus-async-prefetch-remove-group group)
5363     (when gnus-suppress-duplicates
5364       (gnus-dup-enter-articles))
5365     (when gnus-use-trees
5366       (gnus-tree-close group))
5367     ;; Remove entries for this group.
5368     (nnmail-purge-split-history (gnus-group-real-name group))
5369     ;; Make all changes in this group permanent.
5370     (unless quit-config
5371       (gnus-run-hooks 'gnus-exit-group-hook)
5372       (gnus-summary-update-info))
5373     (gnus-close-group group)
5374     ;; Make sure where we were, and go to next newsgroup.
5375     (set-buffer gnus-group-buffer)
5376     (unless quit-config
5377       (gnus-group-jump-to-group group))
5378     (gnus-run-hooks 'gnus-summary-exit-hook)
5379     (unless (or quit-config
5380                 ;; If this group has disappeared from the summary
5381                 ;; buffer, don't skip forwards.
5382                 (not (string= group (gnus-group-group-name))))
5383       (gnus-group-next-unread-group 1))
5384     (setq group-point (point))
5385     (if temporary
5386         nil                             ;Nothing to do.
5387       ;; If we have several article buffers, we kill them at exit.
5388       (unless gnus-single-article-buffer
5389         (gnus-kill-buffer gnus-article-buffer)
5390         (gnus-kill-buffer gnus-original-article-buffer)
5391         (setq gnus-article-current nil))
5392       (set-buffer buf)
5393       (if (not gnus-kill-summary-on-exit)
5394           (gnus-deaden-summary)
5395         ;; We set all buffer-local variables to nil.  It is unclear why
5396         ;; this is needed, but if we don't, buffer-local variables are
5397         ;; not garbage-collected, it seems.  This would the lead to en
5398         ;; ever-growing Emacs.
5399         (gnus-summary-clear-local-variables)
5400         (when (get-buffer gnus-article-buffer)
5401           (bury-buffer gnus-article-buffer))
5402         ;; We clear the global counterparts of the buffer-local
5403         ;; variables as well, just to be on the safe side.
5404         (set-buffer gnus-group-buffer)
5405         (gnus-summary-clear-local-variables)
5406         ;; Return to group mode buffer.
5407         (when (eq mode 'gnus-summary-mode)
5408           (gnus-kill-buffer buf)))
5409       (setq gnus-current-select-method gnus-select-method)
5410       (pop-to-buffer gnus-group-buffer)
5411       (if (not quit-config)
5412           (progn
5413             (goto-char group-point)
5414             (gnus-configure-windows 'group 'force))
5415         (gnus-handle-ephemeral-exit quit-config))
5416       ;; Clear the current group name.
5417       (unless quit-config
5418         (setq gnus-newsgroup-name nil)))))
5419
5420 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5421 (defun gnus-summary-exit-no-update (&optional no-questions)
5422   "Quit reading current newsgroup without updating read article info."
5423   (interactive)
5424   (let* ((group gnus-newsgroup-name)
5425          (quit-config (gnus-group-quit-config group)))
5426     (when (or no-questions
5427               gnus-expert-user
5428               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5429       (gnus-async-halt-prefetch)
5430       (mapcar 'funcall
5431               (delq 'gnus-summary-expire-articles
5432                     (copy-list gnus-summary-prepare-exit-hook)))
5433       ;; If we have several article buffers, we kill them at exit.
5434       (unless gnus-single-article-buffer
5435         (gnus-kill-buffer gnus-article-buffer)
5436         (gnus-kill-buffer gnus-original-article-buffer)
5437         (setq gnus-article-current nil))
5438       (if (not gnus-kill-summary-on-exit)
5439           (gnus-deaden-summary)
5440         (gnus-close-group group)
5441         (gnus-summary-clear-local-variables)
5442         (set-buffer gnus-group-buffer)
5443         (gnus-summary-clear-local-variables)
5444         (when (get-buffer gnus-summary-buffer)
5445           (kill-buffer gnus-summary-buffer)))
5446       (unless gnus-single-article-buffer
5447         (setq gnus-article-current nil))
5448       (when gnus-use-trees
5449         (gnus-tree-close group))
5450       (gnus-async-prefetch-remove-group group)
5451       (when (get-buffer gnus-article-buffer)
5452         (bury-buffer gnus-article-buffer))
5453       ;; Return to the group buffer.
5454       (gnus-configure-windows 'group 'force)
5455       ;; Clear the current group name.
5456       (setq gnus-newsgroup-name nil)
5457       (when (equal (gnus-group-group-name) group)
5458         (gnus-group-next-unread-group 1))
5459       (when quit-config
5460         (gnus-handle-ephemeral-exit quit-config)))))
5461
5462 (defun gnus-handle-ephemeral-exit (quit-config)
5463   "Handle movement when leaving an ephemeral group.
5464 The state which existed when entering the ephemeral is reset."
5465   (if (not (buffer-name (car quit-config)))
5466       (gnus-configure-windows 'group 'force)
5467     (set-buffer (car quit-config))
5468     (cond ((eq major-mode 'gnus-summary-mode)
5469            (gnus-set-global-variables))
5470           ((eq major-mode 'gnus-article-mode)
5471            (save-excursion
5472              ;; The `gnus-summary-buffer' variable may point
5473              ;; to the old summary buffer when using a single
5474              ;; article buffer.
5475              (unless (gnus-buffer-live-p gnus-summary-buffer)
5476                (set-buffer gnus-group-buffer))
5477              (set-buffer gnus-summary-buffer)
5478              (gnus-set-global-variables))))
5479     (if (or (eq (cdr quit-config) 'article)
5480             (eq (cdr quit-config) 'pick))
5481         (progn
5482           ;; The current article may be from the ephemeral group
5483           ;; thus it is best that we reload this article
5484           (gnus-summary-show-article)
5485           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5486               (gnus-configure-windows 'pick 'force)
5487             (gnus-configure-windows (cdr quit-config) 'force)))
5488       (gnus-configure-windows (cdr quit-config) 'force))
5489     (when (eq major-mode 'gnus-summary-mode)
5490       (gnus-summary-next-subject 1 nil t)
5491       (gnus-summary-recenter)
5492       (gnus-summary-position-point))))
5493
5494 (defun gnus-summary-preview-mime-message ()
5495   "MIME decode and play this message."
5496   (interactive)
5497   (let ((gnus-break-pages nil)
5498         (gnus-show-mime t))
5499     (gnus-summary-select-article gnus-show-all-headers t))
5500   (select-window (get-buffer-window gnus-article-buffer)))
5501
5502 ;;; Dead summaries.
5503
5504 (defvar gnus-dead-summary-mode-map nil)
5505
5506 (unless gnus-dead-summary-mode-map
5507   (setq gnus-dead-summary-mode-map (make-keymap))
5508   (suppress-keymap gnus-dead-summary-mode-map)
5509   (substitute-key-definition
5510    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5511   (let ((keys '("\C-d" "\r" "\177" [delete])))
5512     (while keys
5513       (define-key gnus-dead-summary-mode-map
5514         (pop keys) 'gnus-summary-wake-up-the-dead))))
5515
5516 (defvar gnus-dead-summary-mode nil
5517   "Minor mode for Gnus summary buffers.")
5518
5519 (defun gnus-dead-summary-mode (&optional arg)
5520   "Minor mode for Gnus summary buffers."
5521   (interactive "P")
5522   (when (eq major-mode 'gnus-summary-mode)
5523     (make-local-variable 'gnus-dead-summary-mode)
5524     (setq gnus-dead-summary-mode
5525           (if (null arg) (not gnus-dead-summary-mode)
5526             (> (prefix-numeric-value arg) 0)))
5527     (when gnus-dead-summary-mode
5528       (gnus-add-minor-mode
5529        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5530
5531 (defun gnus-deaden-summary ()
5532   "Make the current summary buffer into a dead summary buffer."
5533   ;; Kill any previous dead summary buffer.
5534   (when (and gnus-dead-summary
5535              (buffer-name gnus-dead-summary))
5536     (save-excursion
5537       (set-buffer gnus-dead-summary)
5538       (when gnus-dead-summary-mode
5539         (kill-buffer (current-buffer)))))
5540   ;; Make this the current dead summary.
5541   (setq gnus-dead-summary (current-buffer))
5542   (gnus-dead-summary-mode 1)
5543   (let ((name (buffer-name)))
5544     (when (string-match "Summary" name)
5545       (rename-buffer
5546        (concat (substring name 0 (match-beginning 0)) "Dead "
5547                (substring name (match-beginning 0)))
5548        t))))
5549
5550 (defun gnus-kill-or-deaden-summary (buffer)
5551   "Kill or deaden the summary BUFFER."
5552   (save-excursion
5553     (when (and (buffer-name buffer)
5554                (not gnus-single-article-buffer))
5555       (save-excursion
5556         (set-buffer buffer)
5557         (gnus-kill-buffer gnus-article-buffer)
5558         (gnus-kill-buffer gnus-original-article-buffer)))
5559     (cond (gnus-kill-summary-on-exit
5560            (when (and gnus-use-trees
5561                       (gnus-buffer-exists-p buffer))
5562              (save-excursion
5563                (set-buffer buffer)
5564                (gnus-tree-close gnus-newsgroup-name)))
5565            (gnus-kill-buffer buffer))
5566           ((gnus-buffer-exists-p buffer)
5567            (save-excursion
5568              (set-buffer buffer)
5569              (gnus-deaden-summary))))))
5570
5571 (defun gnus-summary-wake-up-the-dead (&rest args)
5572   "Wake up the dead summary buffer."
5573   (interactive)
5574   (gnus-dead-summary-mode -1)
5575   (let ((name (buffer-name)))
5576     (when (string-match "Dead " name)
5577       (rename-buffer
5578        (concat (substring name 0 (match-beginning 0))
5579                (substring name (match-end 0)))
5580        t)))
5581   (gnus-message 3 "This dead summary is now alive again"))
5582
5583 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5584 (defun gnus-summary-fetch-faq (&optional faq-dir)
5585   "Fetch the FAQ for the current group.
5586 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5587 in."
5588   (interactive
5589    (list
5590     (when current-prefix-arg
5591       (completing-read
5592        "Faq dir: " (and (listp gnus-group-faq-directory)
5593                         (mapcar (lambda (file) (list file))
5594                                 gnus-group-faq-directory))))))
5595   (let (gnus-faq-buffer)
5596     (when (setq gnus-faq-buffer
5597                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5598       (gnus-configure-windows 'summary-faq))))
5599
5600 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5601 (defun gnus-summary-describe-group (&optional force)
5602   "Describe the current newsgroup."
5603   (interactive "P")
5604   (gnus-group-describe-group force gnus-newsgroup-name))
5605
5606 (defun gnus-summary-describe-briefly ()
5607   "Describe summary mode commands briefly."
5608   (interactive)
5609   (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")))
5610
5611 ;; Walking around group mode buffer from summary mode.
5612
5613 (defun gnus-summary-next-group (&optional no-article target-group backward)
5614   "Exit current newsgroup and then select next unread newsgroup.
5615 If prefix argument NO-ARTICLE is non-nil, no article is selected
5616 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5617 previous group instead."
5618   (interactive "P")
5619   ;; Stop pre-fetching.
5620   (gnus-async-halt-prefetch)
5621   (let ((current-group gnus-newsgroup-name)
5622         (current-buffer (current-buffer))
5623         entered)
5624     ;; First we semi-exit this group to update Xrefs and all variables.
5625     ;; We can't do a real exit, because the window conf must remain
5626     ;; the same in case the user is prompted for info, and we don't
5627     ;; want the window conf to change before that...
5628     (gnus-summary-exit t)
5629     (while (not entered)
5630       ;; Then we find what group we are supposed to enter.
5631       (set-buffer gnus-group-buffer)
5632       (gnus-group-jump-to-group current-group)
5633       (setq target-group
5634             (or target-group
5635                 (if (eq gnus-keep-same-level 'best)
5636                     (gnus-summary-best-group gnus-newsgroup-name)
5637                   (gnus-summary-search-group backward gnus-keep-same-level))))
5638       (if (not target-group)
5639           ;; There are no further groups, so we return to the group
5640           ;; buffer.
5641           (progn
5642             (gnus-message 5 "Returning to the group buffer")
5643             (setq entered t)
5644             (when (gnus-buffer-live-p current-buffer)
5645               (set-buffer current-buffer)
5646               (gnus-summary-exit))
5647             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5648         ;; We try to enter the target group.
5649         (gnus-group-jump-to-group target-group)
5650         (let ((unreads (gnus-group-group-unread)))
5651           (if (and (or (eq t unreads)
5652                        (and unreads (not (zerop unreads))))
5653                    (gnus-summary-read-group
5654                     target-group nil no-article
5655                     (and (buffer-name current-buffer) current-buffer)
5656                     nil backward))
5657               (setq entered t)
5658             (setq current-group target-group
5659                   target-group nil)))))))
5660
5661 (defun gnus-summary-prev-group (&optional no-article)
5662   "Exit current newsgroup and then select previous unread newsgroup.
5663 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5664   (interactive "P")
5665   (gnus-summary-next-group no-article nil t))
5666
5667 ;; Walking around summary lines.
5668
5669 (defun gnus-summary-first-subject (&optional unread undownloaded)
5670   "Go to the first unread subject.
5671 If UNREAD is non-nil, go to the first unread article.
5672 Returns the article selected or nil if there are no unread articles."
5673   (interactive "P")
5674   (prog1
5675       (cond
5676        ;; Empty summary.
5677        ((null gnus-newsgroup-data)
5678         (gnus-message 3 "No articles in the group")
5679         nil)
5680        ;; Pick the first article.
5681        ((not unread)
5682         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5683         (gnus-data-number (car gnus-newsgroup-data)))
5684        ;; No unread articles.
5685        ((null gnus-newsgroup-unreads)
5686         (gnus-message 3 "No more unread articles")
5687         nil)
5688        ;; Find the first unread article.
5689        (t
5690         (let ((data gnus-newsgroup-data))
5691           (while (and data
5692                       (and (not (and undownloaded
5693                                      (eq gnus-undownloaded-mark
5694                                          (gnus-data-mark (car data)))))
5695                            (not (gnus-data-unread-p (car data)))))
5696             (setq data (cdr data)))
5697           (when data
5698             (goto-char (gnus-data-pos (car data)))
5699             (gnus-data-number (car data))))))
5700     (gnus-summary-position-point)))
5701
5702 (defun gnus-summary-next-subject (n &optional unread dont-display)
5703   "Go to next N'th summary line.
5704 If N is negative, go to the previous N'th subject line.
5705 If UNREAD is non-nil, only unread articles are selected.
5706 The difference between N and the actual number of steps taken is
5707 returned."
5708   (interactive "p")
5709   (let ((backward (< n 0))
5710         (n (abs n)))
5711     (while (and (> n 0)
5712                 (if backward
5713                     (gnus-summary-find-prev unread)
5714                   (gnus-summary-find-next unread)))
5715       (gnus-summary-show-thread)
5716       (setq n (1- n)))
5717     (when (/= 0 n)
5718       (gnus-message 7 "No more%s articles"
5719                     (if unread " unread" "")))
5720     (unless dont-display
5721       (gnus-summary-recenter)
5722       (gnus-summary-position-point))
5723     n))
5724
5725 (defun gnus-summary-next-unread-subject (n)
5726   "Go to next N'th unread summary line."
5727   (interactive "p")
5728   (gnus-summary-next-subject n t))
5729
5730 (defun gnus-summary-prev-subject (n &optional unread)
5731   "Go to previous N'th summary line.
5732 If optional argument UNREAD is non-nil, only unread article is selected."
5733   (interactive "p")
5734   (gnus-summary-next-subject (- n) unread))
5735
5736 (defun gnus-summary-prev-unread-subject (n)
5737   "Go to previous N'th unread summary line."
5738   (interactive "p")
5739   (gnus-summary-next-subject (- n) t))
5740
5741 (defun gnus-summary-goto-subject (article &optional force silent)
5742   "Go the subject line of ARTICLE.
5743 If FORCE, also allow jumping to articles not currently shown."
5744   (interactive "nArticle number: ")
5745   (let ((b (point))
5746         (data (gnus-data-find article)))
5747     ;; We read in the article if we have to.
5748     (and (not data)
5749          force
5750          (gnus-summary-insert-subject
5751           article
5752           (if (or (numberp force) (vectorp force)) force)
5753           t)
5754          (setq data (gnus-data-find article)))
5755     (goto-char b)
5756     (if (not data)
5757         (progn
5758           (unless silent
5759             (gnus-message 3 "Can't find article %d" article))
5760           nil)
5761       (goto-char (gnus-data-pos data))
5762       (gnus-summary-position-point)
5763       article)))
5764
5765 ;; Walking around summary lines with displaying articles.
5766
5767 (defun gnus-summary-expand-window (&optional arg)
5768   "Make the summary buffer take up the entire Emacs frame.
5769 Given a prefix, will force an `article' buffer configuration."
5770   (interactive "P")
5771   (if arg
5772       (gnus-configure-windows 'article 'force)
5773     (gnus-configure-windows 'summary 'force)))
5774
5775 (defun gnus-summary-display-article (article &optional all-header)
5776   "Display ARTICLE in article buffer."
5777   (gnus-set-global-variables)
5778   (if (null article)
5779       nil
5780     (prog1
5781         (if gnus-summary-display-article-function
5782             (funcall gnus-summary-display-article-function article all-header)
5783           (gnus-article-prepare article all-header))
5784       (with-current-buffer gnus-article-buffer
5785         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5786              nil))
5787       (gnus-run-hooks 'gnus-select-article-hook)
5788       (when (and gnus-current-article
5789                  (not (zerop gnus-current-article)))
5790         (gnus-summary-goto-subject gnus-current-article))
5791       (gnus-summary-recenter)
5792       (when (and gnus-use-trees gnus-show-threads)
5793         (gnus-possibly-generate-tree article)
5794         (gnus-highlight-selected-tree article))
5795       ;; Successfully display article.
5796       (gnus-article-set-window-start
5797        (cdr (assq article gnus-newsgroup-bookmarks))))))
5798
5799 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5800   "Select the current article.
5801 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5802 non-nil, the article will be re-fetched even if it already present in
5803 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5804 be displayed."
5805   ;; Make sure we are in the summary buffer to work around bbdb bug.
5806   (unless (eq major-mode 'gnus-summary-mode)
5807     (set-buffer gnus-summary-buffer))
5808   (let ((article (or article (gnus-summary-article-number)))
5809         (all-headers (not (not all-headers))) ;Must be T or NIL.
5810         gnus-summary-display-article-function
5811         did)
5812     (and (not pseudo)
5813          (gnus-summary-article-pseudo-p article)
5814          (error "This is a pseudo-article"))
5815     (prog1
5816         (save-excursion
5817           (set-buffer gnus-summary-buffer)
5818           (if (or (and gnus-single-article-buffer
5819                        (or (null gnus-current-article)
5820                            (null gnus-article-current)
5821                            (null (get-buffer gnus-article-buffer))
5822                            (not (eq article (cdr gnus-article-current)))
5823                            (not (equal (car gnus-article-current)
5824                                        gnus-newsgroup-name))))
5825                   (and (not gnus-single-article-buffer)
5826                        (or (null gnus-current-article)
5827                            (not (eq gnus-current-article article))))
5828                   force)
5829               ;; The requested article is different from the current article.
5830               (prog1
5831                   (gnus-summary-display-article article all-headers)
5832                 (setq did article)
5833                 (when (or all-headers gnus-show-all-headers)
5834                   (gnus-article-show-all-headers)))
5835             (when (or all-headers gnus-show-all-headers)
5836               (gnus-article-show-all-headers))
5837             'old))
5838       (when did
5839         (gnus-article-set-window-start
5840          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5841
5842 (defun gnus-summary-set-current-mark (&optional current-mark)
5843   "Obsolete function."
5844   nil)
5845
5846 (defun gnus-summary-next-article (&optional unread subject backward push)
5847   "Select the next article.
5848 If UNREAD, only unread articles are selected.
5849 If SUBJECT, only articles with SUBJECT are selected.
5850 If BACKWARD, the previous article is selected instead of the next."
5851   (interactive "P")
5852   (cond
5853    ;; Is there such an article?
5854    ((and (gnus-summary-search-forward unread subject backward)
5855          (or (gnus-summary-display-article (gnus-summary-article-number))
5856              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5857     (gnus-summary-position-point))
5858    ;; If not, we try the first unread, if that is wanted.
5859    ((and subject
5860          gnus-auto-select-same
5861          (gnus-summary-first-unread-article))
5862     (gnus-summary-position-point)
5863     (gnus-message 6 "Wrapped"))
5864    ;; Try to get next/previous article not displayed in this group.
5865    ((and gnus-auto-extend-newsgroup
5866          (not unread) (not subject))
5867     (gnus-summary-goto-article
5868      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5869      nil (count-lines (point-min) (point))))
5870    ;; Go to next/previous group.
5871    (t
5872     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5873       (gnus-summary-jump-to-group gnus-newsgroup-name))
5874     (let ((cmd last-command-char)
5875           (point
5876            (save-excursion
5877              (set-buffer gnus-group-buffer)
5878              (point)))
5879           (group
5880            (if (eq gnus-keep-same-level 'best)
5881                (gnus-summary-best-group gnus-newsgroup-name)
5882              (gnus-summary-search-group backward gnus-keep-same-level))))
5883       ;; For some reason, the group window gets selected.  We change
5884       ;; it back.
5885       (select-window (get-buffer-window (current-buffer)))
5886       ;; Select next unread newsgroup automagically.
5887       (cond
5888        ((or (not gnus-auto-select-next)
5889             (not cmd))
5890         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5891        ((or (eq gnus-auto-select-next 'quietly)
5892             (and (eq gnus-auto-select-next 'slightly-quietly)
5893                  push)
5894             (and (eq gnus-auto-select-next 'almost-quietly)
5895                  (gnus-summary-last-article-p)))
5896         ;; Select quietly.
5897         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5898             (gnus-summary-exit)
5899           (gnus-message 7 "No more%s articles (%s)..."
5900                         (if unread " unread" "")
5901                         (if group (concat "selecting " group)
5902                           "exiting"))
5903           (gnus-summary-next-group nil group backward)))
5904        (t
5905         (when (gnus-key-press-event-p last-input-event)
5906           (gnus-summary-walk-group-buffer
5907            gnus-newsgroup-name cmd unread backward point))))))))
5908
5909 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5910   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5911                       (?\C-p (gnus-group-prev-unread-group 1))))
5912         (cursor-in-echo-area t)
5913         keve key group ended)
5914     (save-excursion
5915       (set-buffer gnus-group-buffer)
5916       (goto-char start)
5917       (setq group
5918             (if (eq gnus-keep-same-level 'best)
5919                 (gnus-summary-best-group gnus-newsgroup-name)
5920               (gnus-summary-search-group backward gnus-keep-same-level))))
5921     (while (not ended)
5922       (gnus-message
5923        5 "No more%s articles%s" (if unread " unread" "")
5924        (if (and group
5925                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5926            (format " (Type %s for %s [%s])"
5927                    (single-key-description cmd) group
5928                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5929          (format " (Type %s to exit %s)"
5930                  (single-key-description cmd)
5931                  gnus-newsgroup-name)))
5932       ;; Confirm auto selection.
5933       (setq key (car (setq keve (gnus-read-event-char))))
5934       (setq ended t)
5935       (cond
5936        ((assq key keystrokes)
5937         (let ((obuf (current-buffer)))
5938           (switch-to-buffer gnus-group-buffer)
5939           (when group
5940             (gnus-group-jump-to-group group))
5941           (eval (cadr (assq key keystrokes)))
5942           (setq group (gnus-group-group-name))
5943           (switch-to-buffer obuf))
5944         (setq ended nil))
5945        ((equal key cmd)
5946         (if (or (not group)
5947                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5948             (gnus-summary-exit)
5949           (gnus-summary-next-group nil group backward)))
5950        (t
5951         (push (cdr keve) unread-command-events))))))
5952
5953 (defun gnus-summary-next-unread-article ()
5954   "Select unread article after current one."
5955   (interactive)
5956   (gnus-summary-next-article
5957    (or (not (eq gnus-summary-goto-unread 'never))
5958        (gnus-summary-last-article-p (gnus-summary-article-number)))
5959    (and gnus-auto-select-same
5960         (gnus-summary-article-subject))))
5961
5962 (defun gnus-summary-prev-article (&optional unread subject)
5963   "Select the article after the current one.
5964 If UNREAD is non-nil, only unread articles are selected."
5965   (interactive "P")
5966   (gnus-summary-next-article unread subject t))
5967
5968 (defun gnus-summary-prev-unread-article ()
5969   "Select unread article before current one."
5970   (interactive)
5971   (gnus-summary-prev-article
5972    (or (not (eq gnus-summary-goto-unread 'never))
5973        (gnus-summary-first-article-p (gnus-summary-article-number)))
5974    (and gnus-auto-select-same
5975         (gnus-summary-article-subject))))
5976
5977 (defun gnus-summary-next-page (&optional lines circular)
5978   "Show next page of the selected article.
5979 If at the end of the current article, select the next article.
5980 LINES says how many lines should be scrolled up.
5981
5982 If CIRCULAR is non-nil, go to the start of the article instead of
5983 selecting the next article when reaching the end of the current
5984 article."
5985   (interactive "P")
5986   (setq gnus-summary-buffer (current-buffer))
5987   (gnus-set-global-variables)
5988   (let ((article (gnus-summary-article-number))
5989         (article-window (get-buffer-window gnus-article-buffer t))
5990         endp)
5991     ;; If the buffer is empty, we have no article.
5992     (unless article
5993       (error "No article to select"))
5994     (gnus-configure-windows 'article)
5995     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5996         (if (and (eq gnus-summary-goto-unread 'never)
5997                  (not (gnus-summary-last-article-p article)))
5998             (gnus-summary-next-article)
5999           (gnus-summary-next-unread-article))
6000       (if (or (null gnus-current-article)
6001               (null gnus-article-current)
6002               (/= article (cdr gnus-article-current))
6003               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6004           ;; Selected subject is different from current article's.
6005           (gnus-summary-display-article article)
6006         (when article-window
6007           (gnus-eval-in-buffer-window gnus-article-buffer
6008             (setq endp (gnus-article-next-page lines)))
6009           (when endp
6010             (cond (circular
6011                    (gnus-summary-beginning-of-article))
6012                   (lines
6013                    (gnus-message 3 "End of message"))
6014                   ((null lines)
6015                    (if (and (eq gnus-summary-goto-unread 'never)
6016                             (not (gnus-summary-last-article-p article)))
6017                        (gnus-summary-next-article)
6018                      (gnus-summary-next-unread-article))))))))
6019     (gnus-summary-recenter)
6020     (gnus-summary-position-point)))
6021
6022 (defun gnus-summary-prev-page (&optional lines move)
6023   "Show previous page of selected article.
6024 Argument LINES specifies lines to be scrolled down.
6025 If MOVE, move to the previous unread article if point is at
6026 the beginning of the buffer."
6027   (interactive "P")
6028   (let ((article (gnus-summary-article-number))
6029         (article-window (get-buffer-window gnus-article-buffer t))
6030         endp)
6031     (gnus-configure-windows 'article)
6032     (if (or (null gnus-current-article)
6033             (null gnus-article-current)
6034             (/= article (cdr gnus-article-current))
6035             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6036         ;; Selected subject is different from current article's.
6037         (gnus-summary-display-article article)
6038       (gnus-summary-recenter)
6039       (when article-window
6040         (gnus-eval-in-buffer-window gnus-article-buffer
6041           (setq endp (gnus-article-prev-page lines)))
6042         (when (and move endp)
6043           (cond (lines
6044                  (gnus-message 3 "Beginning of message"))
6045                 ((null lines)
6046                  (if (and (eq gnus-summary-goto-unread 'never)
6047                           (not (gnus-summary-first-article-p article)))
6048                      (gnus-summary-prev-article)
6049                    (gnus-summary-prev-unread-article))))))))
6050   (gnus-summary-position-point))
6051
6052 (defun gnus-summary-prev-page-or-article (&optional lines)
6053   "Show previous page of selected article.
6054 Argument LINES specifies lines to be scrolled down.
6055 If at the beginning of the article, go to the next article."
6056   (interactive "P")
6057   (gnus-summary-prev-page lines t))
6058
6059 (defun gnus-summary-scroll-up (lines)
6060   "Scroll up (or down) one line current article.
6061 Argument LINES specifies lines to be scrolled up (or down if negative)."
6062   (interactive "p")
6063   (gnus-configure-windows 'article)
6064   (gnus-summary-show-thread)
6065   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6066     (gnus-eval-in-buffer-window gnus-article-buffer
6067       (cond ((> lines 0)
6068              (when (gnus-article-next-page lines)
6069                (gnus-message 3 "End of message")))
6070             ((< lines 0)
6071              (gnus-article-prev-page (- lines))))))
6072   (gnus-summary-recenter)
6073   (gnus-summary-position-point))
6074
6075 (defun gnus-summary-scroll-down (lines)
6076   "Scroll down (or up) one line current article.
6077 Argument LINES specifies lines to be scrolled down (or up if negative)."
6078   (interactive "p")
6079   (gnus-summary-scroll-up (- lines)))
6080
6081 (defun gnus-summary-next-same-subject ()
6082   "Select next article which has the same subject as current one."
6083   (interactive)
6084   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6085
6086 (defun gnus-summary-prev-same-subject ()
6087   "Select previous article which has the same subject as current one."
6088   (interactive)
6089   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6090
6091 (defun gnus-summary-next-unread-same-subject ()
6092   "Select next unread article which has the same subject as current one."
6093   (interactive)
6094   (gnus-summary-next-article t (gnus-summary-article-subject)))
6095
6096 (defun gnus-summary-prev-unread-same-subject ()
6097   "Select previous unread article which has the same subject as current one."
6098   (interactive)
6099   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6100
6101 (defun gnus-summary-first-unread-article ()
6102   "Select the first unread article.
6103 Return nil if there are no unread articles."
6104   (interactive)
6105   (prog1
6106       (when (gnus-summary-first-subject t)
6107         (gnus-summary-show-thread)
6108         (gnus-summary-first-subject t)
6109         (gnus-summary-display-article (gnus-summary-article-number)))
6110     (gnus-summary-position-point)))
6111
6112 (defun gnus-summary-first-unread-subject ()
6113   "Place the point on the subject line of the first unread article.
6114 Return nil if there are no unread articles."
6115   (interactive)
6116   (prog1
6117       (when (gnus-summary-first-subject t)
6118         (gnus-summary-show-thread)
6119         (gnus-summary-first-subject t))
6120     (gnus-summary-position-point)))
6121
6122 (defun gnus-summary-first-article ()
6123   "Select the first article.
6124 Return nil if there are no articles."
6125   (interactive)
6126   (prog1
6127       (when (gnus-summary-first-subject)
6128         (gnus-summary-show-thread)
6129         (gnus-summary-first-subject)
6130         (gnus-summary-display-article (gnus-summary-article-number)))
6131     (gnus-summary-position-point)))
6132
6133 (defun gnus-summary-best-unread-article ()
6134   "Select the unread article with the highest score."
6135   (interactive)
6136   (let ((best -1000000)
6137         (data gnus-newsgroup-data)
6138         article score)
6139     (while data
6140       (and (gnus-data-unread-p (car data))
6141            (> (setq score
6142                     (gnus-summary-article-score (gnus-data-number (car data))))
6143               best)
6144            (setq best score
6145                  article (gnus-data-number (car data))))
6146       (setq data (cdr data)))
6147     (prog1
6148         (if article
6149             (gnus-summary-goto-article article)
6150           (error "No unread articles"))
6151       (gnus-summary-position-point))))
6152
6153 (defun gnus-summary-last-subject ()
6154   "Go to the last displayed subject line in the group."
6155   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6156     (when article
6157       (gnus-summary-goto-subject article))))
6158
6159 (defun gnus-summary-goto-article (article &optional all-headers force)
6160   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6161 If ALL-HEADERS is non-nil, no header lines are hidden.
6162 If FORCE, go to the article even if it isn't displayed.  If FORCE
6163 is a number, it is the line the article is to be displayed on."
6164   (interactive
6165    (list
6166     (completing-read
6167      "Article number or Message-ID: "
6168      (mapcar (lambda (number) (list (int-to-string number)))
6169              gnus-newsgroup-limit))
6170     current-prefix-arg
6171     t))
6172   (prog1
6173       (if (and (stringp article)
6174                (string-match "@" article))
6175           (gnus-summary-refer-article article)
6176         (when (stringp article)
6177           (setq article (string-to-number article)))
6178         (if (gnus-summary-goto-subject article force)
6179             (gnus-summary-display-article article all-headers)
6180           (gnus-message 4 "Couldn't go to article %s" article) nil))
6181     (gnus-summary-position-point)))
6182
6183 (defun gnus-summary-goto-last-article ()
6184   "Go to the previously read article."
6185   (interactive)
6186   (prog1
6187       (when gnus-last-article
6188         (gnus-summary-goto-article gnus-last-article nil t))
6189     (gnus-summary-position-point)))
6190
6191 (defun gnus-summary-pop-article (number)
6192   "Pop one article off the history and go to the previous.
6193 NUMBER articles will be popped off."
6194   (interactive "p")
6195   (let (to)
6196     (setq gnus-newsgroup-history
6197           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6198     (if to
6199         (gnus-summary-goto-article (car to) nil t)
6200       (error "Article history empty")))
6201   (gnus-summary-position-point))
6202
6203 ;; Summary commands and functions for limiting the summary buffer.
6204
6205 (defun gnus-summary-limit-to-articles (n)
6206   "Limit the summary buffer to the next N articles.
6207 If not given a prefix, use the process marked articles instead."
6208   (interactive "P")
6209   (prog1
6210       (let ((articles (gnus-summary-work-articles n)))
6211         (setq gnus-newsgroup-processable nil)
6212         (gnus-summary-limit articles))
6213     (gnus-summary-position-point)))
6214
6215 (defun gnus-summary-pop-limit (&optional total)
6216   "Restore the previous limit.
6217 If given a prefix, remove all limits."
6218   (interactive "P")
6219   (when total
6220     (setq gnus-newsgroup-limits
6221           (list (mapcar (lambda (h) (mail-header-number h))
6222                         gnus-newsgroup-headers))))
6223   (unless gnus-newsgroup-limits
6224     (error "No limit to pop"))
6225   (prog1
6226       (gnus-summary-limit nil 'pop)
6227     (gnus-summary-position-point)))
6228
6229 (defun gnus-summary-limit-to-subject (subject &optional header)
6230   "Limit the summary buffer to articles that have subjects that match a regexp."
6231   (interactive "sLimit to subject (regexp): ")
6232   (unless header
6233     (setq header "subject"))
6234   (when (not (equal "" subject))
6235     (prog1
6236         (let ((articles (gnus-summary-find-matching
6237                          (or header "subject") subject 'all)))
6238           (unless articles
6239             (error "Found no matches for \"%s\"" subject))
6240           (gnus-summary-limit articles))
6241       (gnus-summary-position-point))))
6242
6243 (defun gnus-summary-limit-to-author (from)
6244   "Limit the summary buffer to articles that have authors that match a regexp."
6245   (interactive "sLimit to author (regexp): ")
6246   (gnus-summary-limit-to-subject from "from"))
6247
6248 (defun gnus-summary-limit-to-age (age &optional younger-p)
6249   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6250 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6251 articles that are younger than AGE days."
6252   (interactive
6253    (let ((younger current-prefix-arg)
6254          (days-got nil)
6255          days)
6256      (while (not days-got)
6257        (setq days (if younger
6258                       (read-string "Limit to articles within (in days): ")
6259                     (read-string "Limit to articles old than (in days): ")))
6260        (when (> (length days) 0)
6261          (setq days (read days)))
6262        (if (numberp days)
6263            (setq days-got t)
6264          (message "Please enter a number.")
6265          (sleep-for 1)))
6266      (list days younger)))
6267   (prog1
6268       (let ((data gnus-newsgroup-data)
6269             (cutoff (days-to-time age))
6270             articles d date is-younger)
6271         (while (setq d (pop data))
6272           (when (and (vectorp (gnus-data-header d))
6273                      (setq date (mail-header-date (gnus-data-header d))))
6274             (setq is-younger (time-less-p
6275                               (time-since (date-to-time date))
6276                               cutoff))
6277             (when (if younger-p
6278                       is-younger
6279                     (not is-younger))
6280               (push (gnus-data-number d) articles))))
6281         (gnus-summary-limit (nreverse articles)))
6282     (gnus-summary-position-point)))
6283
6284 (defun gnus-summary-limit-to-extra (header regexp)
6285   "Limit the summary buffer to articles that match an 'extra' header."
6286   (interactive
6287    (let ((header
6288           (intern
6289            (gnus-completing-read
6290             (symbol-name (car gnus-extra-headers))      
6291             "Limit extra header:"       
6292             (mapcar (lambda (x) 
6293                       (cons (symbol-name x) x))
6294                     gnus-extra-headers)
6295             nil                 
6296             t))))
6297      (list header
6298            (read-string (format "Limit to header %s (regexp): " header)))))
6299   (when (not (equal "" regexp))
6300     (prog1
6301         (let ((articles (gnus-summary-find-matching
6302                          (cons 'extra header) regexp 'all)))
6303           (unless articles
6304             (error "Found no matches for \"%s\"" regexp))
6305           (gnus-summary-limit articles))
6306       (gnus-summary-position-point))))
6307
6308 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6309 (make-obsolete
6310  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6311
6312 (defun gnus-summary-limit-to-unread (&optional all)
6313   "Limit the summary buffer to articles that are not marked as read.
6314 If ALL is non-nil, limit strictly to unread articles."
6315   (interactive "P")
6316   (if all
6317       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6318     (gnus-summary-limit-to-marks
6319      ;; Concat all the marks that say that an article is read and have
6320      ;; those removed.
6321      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6322            gnus-killed-mark gnus-kill-file-mark
6323            gnus-low-score-mark gnus-expirable-mark
6324            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6325            gnus-duplicate-mark gnus-souped-mark)
6326      'reverse)))
6327
6328 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6329 (make-obsolete 'gnus-summary-delete-marked-with
6330                'gnus-summary-limit-exlude-marks)
6331
6332 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6333   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6334 If REVERSE, limit the summary buffer to articles that are marked
6335 with MARKS.  MARKS can either be a string of marks or a list of marks.
6336 Returns how many articles were removed."
6337   (interactive "sMarks: ")
6338   (gnus-summary-limit-to-marks marks t))
6339
6340 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6341   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6342 If REVERSE (the prefix), limit the summary buffer to articles that are
6343 not marked with MARKS.  MARKS can either be a string of marks or a
6344 list of marks.
6345 Returns how many articles were removed."
6346   (interactive "sMarks: \nP")
6347   (prog1
6348       (let ((data gnus-newsgroup-data)
6349             (marks (if (listp marks) marks
6350                      (append marks nil))) ; Transform to list.
6351             articles)
6352         (while data
6353           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6354                   (memq (gnus-data-mark (car data)) marks))
6355             (push (gnus-data-number (car data)) articles))
6356           (setq data (cdr data)))
6357         (gnus-summary-limit articles))
6358     (gnus-summary-position-point)))
6359
6360 (defun gnus-summary-limit-to-score (&optional score)
6361   "Limit to articles with score at or above SCORE."
6362   (interactive "P")
6363   (setq score (if score
6364                   (prefix-numeric-value score)
6365                 (or gnus-summary-default-score 0)))
6366   (let ((data gnus-newsgroup-data)
6367         articles)
6368     (while data
6369       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6370                 score)
6371         (push (gnus-data-number (car data)) articles))
6372       (setq data (cdr data)))
6373     (prog1
6374         (gnus-summary-limit articles)
6375       (gnus-summary-position-point))))
6376
6377 (defun gnus-summary-limit-include-thread (id)
6378   "Display all the hidden articles that in the current thread."
6379   (interactive (list (mail-header-id (gnus-summary-article-header))))
6380   (let ((articles (gnus-articles-in-thread
6381                    (gnus-id-to-thread (gnus-root-id id)))))
6382     (prog1
6383         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6384       (gnus-summary-position-point))))
6385
6386 (defun gnus-summary-limit-include-dormant ()
6387   "Display all the hidden articles that are marked as dormant.
6388 Note that this command only works on a subset of the articles currently
6389 fetched for this group."
6390   (interactive)
6391   (unless gnus-newsgroup-dormant
6392     (error "There are no dormant articles in this group"))
6393   (prog1
6394       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6395     (gnus-summary-position-point)))
6396
6397 (defun gnus-summary-limit-exclude-dormant ()
6398   "Hide all dormant articles."
6399   (interactive)
6400   (prog1
6401       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6402     (gnus-summary-position-point)))
6403
6404 (defun gnus-summary-limit-exclude-childless-dormant ()
6405   "Hide all dormant articles that have no children."
6406   (interactive)
6407   (let ((data (gnus-data-list t))
6408         articles d children)
6409     ;; Find all articles that are either not dormant or have
6410     ;; children.
6411     (while (setq d (pop data))
6412       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6413                 (and (setq children
6414                            (gnus-article-children (gnus-data-number d)))
6415                      (let (found)
6416                        (while children
6417                          (when (memq (car children) articles)
6418                            (setq children nil
6419                                  found t))
6420                          (pop children))
6421                        found)))
6422         (push (gnus-data-number d) articles)))
6423     ;; Do the limiting.
6424     (prog1
6425         (gnus-summary-limit articles)
6426       (gnus-summary-position-point))))
6427
6428 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6429   "Mark all unread excluded articles as read.
6430 If ALL, mark even excluded ticked and dormants as read."
6431   (interactive "P")
6432   (let ((articles (gnus-sorted-complement
6433                    (sort
6434                     (mapcar (lambda (h) (mail-header-number h))
6435                             gnus-newsgroup-headers)
6436                     '<)
6437                    (sort gnus-newsgroup-limit '<)))
6438         article)
6439     (setq gnus-newsgroup-unreads
6440           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6441     (if all
6442         (setq gnus-newsgroup-dormant nil
6443               gnus-newsgroup-marked nil
6444               gnus-newsgroup-reads
6445               (nconc
6446                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6447                gnus-newsgroup-reads))
6448       (while (setq article (pop articles))
6449         (unless (or (memq article gnus-newsgroup-dormant)
6450                     (memq article gnus-newsgroup-marked))
6451           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6452
6453 (defun gnus-summary-limit (articles &optional pop)
6454   (if pop
6455       ;; We pop the previous limit off the stack and use that.
6456       (setq articles (car gnus-newsgroup-limits)
6457             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6458     ;; We use the new limit, so we push the old limit on the stack.
6459     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6460   ;; Set the limit.
6461   (setq gnus-newsgroup-limit articles)
6462   (let ((total (length gnus-newsgroup-data))
6463         (data (gnus-data-find-list (gnus-summary-article-number)))
6464         (gnus-summary-mark-below nil)   ; Inhibit this.
6465         found)
6466     ;; This will do all the work of generating the new summary buffer
6467     ;; according to the new limit.
6468     (gnus-summary-prepare)
6469     ;; Hide any threads, possibly.
6470     (and gnus-show-threads
6471          gnus-thread-hide-subtree
6472          (gnus-summary-hide-all-threads))
6473     ;; Try to return to the article you were at, or one in the
6474     ;; neighborhood.
6475     (when data
6476       ;; We try to find some article after the current one.
6477       (while data
6478         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6479           (setq data nil
6480                 found t))
6481         (setq data (cdr data))))
6482     (unless found
6483       ;; If there is no data, that means that we were after the last
6484       ;; article.  The same goes when we can't find any articles
6485       ;; after the current one.
6486       (goto-char (point-max))
6487       (gnus-summary-find-prev))
6488     (gnus-set-mode-line 'summary)
6489     ;; We return how many articles were removed from the summary
6490     ;; buffer as a result of the new limit.
6491     (- total (length gnus-newsgroup-data))))
6492
6493 (defsubst gnus-invisible-cut-children (threads)
6494   (let ((num 0))
6495     (while threads
6496       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6497         (incf num))
6498       (pop threads))
6499     (< num 2)))
6500
6501 (defsubst gnus-cut-thread (thread)
6502   "Go forwards in the thread until we find an article that we want to display."
6503   (when (or (eq gnus-fetch-old-headers 'some)
6504             (eq gnus-fetch-old-headers 'invisible)
6505             (eq gnus-build-sparse-threads 'some)
6506             (eq gnus-build-sparse-threads 'more))
6507     ;; Deal with old-fetched headers and sparse threads.
6508     (while (and
6509             thread
6510             (or
6511              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6512              (gnus-summary-article-ancient-p
6513               (mail-header-number (car thread))))
6514             (if (or (<= (length (cdr thread)) 1)
6515                     (eq gnus-fetch-old-headers 'invisible))
6516                 (setq gnus-newsgroup-limit
6517                       (delq (mail-header-number (car thread))
6518                             gnus-newsgroup-limit)
6519                       thread (cadr thread))
6520               (when (gnus-invisible-cut-children (cdr thread))
6521                 (let ((th (cdr thread)))
6522                   (while th
6523                     (if (memq (mail-header-number (caar th))
6524                               gnus-newsgroup-limit)
6525                         (setq thread (car th)
6526                               th nil)
6527                       (setq th (cdr th))))))))))
6528   thread)
6529
6530 (defun gnus-cut-threads (threads)
6531   "Cut off all uninteresting articles from the beginning of threads."
6532   (when (or (eq gnus-fetch-old-headers 'some)
6533             (eq gnus-fetch-old-headers 'invisible)
6534             (eq gnus-build-sparse-threads 'some)
6535             (eq gnus-build-sparse-threads 'more))
6536     (let ((th threads))
6537       (while th
6538         (setcar th (gnus-cut-thread (car th)))
6539         (setq th (cdr th)))))
6540   ;; Remove nixed out threads.
6541   (delq nil threads))
6542
6543 (defun gnus-summary-initial-limit (&optional show-if-empty)
6544   "Figure out what the initial limit is supposed to be on group entry.
6545 This entails weeding out unwanted dormants, low-scored articles,
6546 fetch-old-headers verbiage, and so on."
6547   ;; Most groups have nothing to remove.
6548   (if (or gnus-inhibit-limiting
6549           (and (null gnus-newsgroup-dormant)
6550                (not (eq gnus-fetch-old-headers 'some))
6551                (not (eq gnus-fetch-old-headers 'invisible))
6552                (null gnus-summary-expunge-below)
6553                (not (eq gnus-build-sparse-threads 'some))
6554                (not (eq gnus-build-sparse-threads 'more))
6555                (null gnus-thread-expunge-below)
6556                (not gnus-use-nocem)))
6557       ()                                ; Do nothing.
6558     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6559     (setq gnus-newsgroup-limit nil)
6560     (mapatoms
6561      (lambda (node)
6562        (unless (car (symbol-value node))
6563          ;; These threads have no parents -- they are roots.
6564          (let ((nodes (cdr (symbol-value node)))
6565                thread)
6566            (while nodes
6567              (if (and gnus-thread-expunge-below
6568                       (< (gnus-thread-total-score (car nodes))
6569                          gnus-thread-expunge-below))
6570                  (gnus-expunge-thread (pop nodes))
6571                (setq thread (pop nodes))
6572                (gnus-summary-limit-children thread))))))
6573      gnus-newsgroup-dependencies)
6574     ;; If this limitation resulted in an empty group, we might
6575     ;; pop the previous limit and use it instead.
6576     (when (and (not gnus-newsgroup-limit)
6577                show-if-empty)
6578       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6579     gnus-newsgroup-limit))
6580
6581 (defun gnus-summary-limit-children (thread)
6582   "Return 1 if this subthread is visible and 0 if it is not."
6583   ;; First we get the number of visible children to this thread.  This
6584   ;; is done by recursing down the thread using this function, so this
6585   ;; will really go down to a leaf article first, before slowly
6586   ;; working its way up towards the root.
6587   (when thread
6588     (let ((children
6589            (if (cdr thread)
6590                (apply '+ (mapcar 'gnus-summary-limit-children
6591                                  (cdr thread)))
6592              0))
6593           (number (mail-header-number (car thread)))
6594           score)
6595       (if (and
6596            (not (memq number gnus-newsgroup-marked))
6597            (or
6598             ;; If this article is dormant and has absolutely no visible
6599             ;; children, then this article isn't visible.
6600             (and (memq number gnus-newsgroup-dormant)
6601                  (zerop children))
6602             ;; If this is "fetch-old-headered" and there is no
6603             ;; visible children, then we don't want this article.
6604             (and (eq gnus-fetch-old-headers 'some)
6605                  (gnus-summary-article-ancient-p number)
6606                  (zerop children))
6607             ;; If this is "fetch-old-headered" and `invisible', then
6608             ;; we don't want this article.
6609             (and (eq gnus-fetch-old-headers 'invisible)
6610                  (gnus-summary-article-ancient-p number))
6611             ;; If this is a sparsely inserted article with no children,
6612             ;; we don't want it.
6613             (and (eq gnus-build-sparse-threads 'some)
6614                  (gnus-summary-article-sparse-p number)
6615                  (zerop children))
6616             ;; If we use expunging, and this article is really
6617             ;; low-scored, then we don't want this article.
6618             (when (and gnus-summary-expunge-below
6619                        (< (setq score
6620                                 (or (cdr (assq number gnus-newsgroup-scored))
6621                                     gnus-summary-default-score))
6622                           gnus-summary-expunge-below))
6623               ;; We increase the expunge-tally here, but that has
6624               ;; nothing to do with the limits, really.
6625               (incf gnus-newsgroup-expunged-tally)
6626               ;; We also mark as read here, if that's wanted.
6627               (when (and gnus-summary-mark-below
6628                          (< score gnus-summary-mark-below))
6629                 (setq gnus-newsgroup-unreads
6630                       (delq number gnus-newsgroup-unreads))
6631                 (if gnus-newsgroup-auto-expire
6632                     (push number gnus-newsgroup-expirable)
6633                   (push (cons number gnus-low-score-mark)
6634                         gnus-newsgroup-reads)))
6635               t)
6636             ;; Check NoCeM things.
6637             (if (and gnus-use-nocem
6638                      (gnus-nocem-unwanted-article-p
6639                       (mail-header-id (car thread))))
6640                 (progn
6641                   (setq gnus-newsgroup-unreads
6642                         (delq number gnus-newsgroup-unreads))
6643                   t))))
6644           ;; Nope, invisible article.
6645           0
6646         ;; Ok, this article is to be visible, so we add it to the limit
6647         ;; and return 1.
6648         (push number gnus-newsgroup-limit)
6649         1))))
6650
6651 (defun gnus-expunge-thread (thread)
6652   "Mark all articles in THREAD as read."
6653   (let* ((number (mail-header-number (car thread))))
6654     (incf gnus-newsgroup-expunged-tally)
6655     ;; We also mark as read here, if that's wanted.
6656     (setq gnus-newsgroup-unreads
6657           (delq number gnus-newsgroup-unreads))
6658     (if gnus-newsgroup-auto-expire
6659         (push number gnus-newsgroup-expirable)
6660       (push (cons number gnus-low-score-mark)
6661             gnus-newsgroup-reads)))
6662   ;; Go recursively through all subthreads.
6663   (mapcar 'gnus-expunge-thread (cdr thread)))
6664
6665 ;; Summary article oriented commands
6666
6667 (defun gnus-summary-refer-parent-article (n)
6668   "Refer parent article N times.
6669 If N is negative, go to ancestor -N instead.
6670 The difference between N and the number of articles fetched is returned."
6671   (interactive "p")
6672   (let ((skip 1)
6673         error header ref)
6674     (when (not (natnump n))
6675       (setq skip (abs n)
6676             n 1))
6677     (while (and (> n 0)
6678                 (not error))
6679       (setq header (gnus-summary-article-header))
6680       (if (and (eq (mail-header-number header)
6681                    (cdr gnus-article-current))
6682                (equal gnus-newsgroup-name
6683                       (car gnus-article-current)))
6684           ;; If we try to find the parent of the currently
6685           ;; displayed article, then we take a look at the actual
6686           ;; References header, since this is slightly more
6687           ;; reliable than the References field we got from the
6688           ;; server.
6689           (save-excursion
6690             (set-buffer gnus-original-article-buffer)
6691             (nnheader-narrow-to-headers)
6692             (unless (setq ref (message-fetch-field "references"))
6693               (setq ref (message-fetch-field "in-reply-to")))
6694             (widen))
6695         (setq ref
6696               ;; It's not the current article, so we take a bet on
6697               ;; the value we got from the server.
6698               (mail-header-references header)))
6699       (if (and ref
6700                (not (equal ref "")))
6701           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6702             (gnus-message 1 "Couldn't find parent"))
6703         (gnus-message 1 "No references in article %d"
6704                       (gnus-summary-article-number))
6705         (setq error t))
6706       (decf n))
6707     (gnus-summary-position-point)
6708     n))
6709
6710 (defun gnus-summary-refer-references ()
6711   "Fetch all articles mentioned in the References header.
6712 Return the number of articles fetched."
6713   (interactive)
6714   (let ((ref (mail-header-references (gnus-summary-article-header)))
6715         (current (gnus-summary-article-number))
6716         (n 0))
6717     (if (or (not ref)
6718             (equal ref ""))
6719         (error "No References in the current article")
6720       ;; For each Message-ID in the References header...
6721       (while (string-match "<[^>]*>" ref)
6722         (incf n)
6723         ;; ... fetch that article.
6724         (gnus-summary-refer-article
6725          (prog1 (match-string 0 ref)
6726            (setq ref (substring ref (match-end 0))))))
6727       (gnus-summary-goto-subject current)
6728       (gnus-summary-position-point)
6729       n)))
6730
6731 (defun gnus-summary-refer-thread (&optional limit)
6732   "Fetch all articles in the current thread.
6733 If LIMIT (the numerical prefix), fetch that many old headers instead
6734 of what's specified by the `gnus-refer-thread-limit' variable."
6735   (interactive "P")
6736   (let ((id (mail-header-id (gnus-summary-article-header)))
6737         (limit (if limit (prefix-numeric-value limit)
6738                  gnus-refer-thread-limit)))
6739     ;; We want to fetch LIMIT *old* headers, but we also have to
6740     ;; re-fetch all the headers in the current buffer, because many of
6741     ;; them may be undisplayed.  So we adjust LIMIT.
6742     (when (numberp limit)
6743       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6744     (unless (eq gnus-fetch-old-headers 'invisible)
6745       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6746       ;; Retrieve the headers and read them in.
6747       (if (eq (gnus-retrieve-headers
6748                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6749               'nov)
6750           (gnus-build-all-threads)
6751         (error "Can't fetch thread from backends that don't support NOV"))
6752       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6753     (gnus-summary-limit-include-thread id)))
6754
6755 (defun gnus-summary-refer-article (message-id &optional arg)
6756   "Fetch an article specified by MESSAGE-ID.
6757 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6758 or `gnus-select-method', no matter what backend the article comes from."
6759   (interactive "sMessage-ID: \nP")
6760   (when (and (stringp message-id)
6761              (not (zerop (length message-id))))
6762     ;; Construct the correct Message-ID if necessary.
6763     ;; Suggested by tale@pawl.rpi.edu.
6764     (unless (string-match "^<" message-id)
6765       (setq message-id (concat "<" message-id)))
6766     (unless (string-match ">$" message-id)
6767       (setq message-id (concat message-id ">")))
6768     (let* ((header (gnus-id-to-header message-id))
6769            (sparse (and header
6770                         (gnus-summary-article-sparse-p
6771                          (mail-header-number header))
6772                         (memq (mail-header-number header)
6773                               gnus-newsgroup-limit))))
6774       (cond
6775        ;; If the article is present in the buffer we just go to it.
6776        ((and header
6777              (or (not (gnus-summary-article-sparse-p
6778                        (mail-header-number header)))
6779                  sparse))
6780         (prog1
6781             (gnus-summary-goto-article
6782              (mail-header-number header) nil t)
6783           (when sparse
6784             (gnus-summary-update-article (mail-header-number header)))))
6785        (t
6786         ;; We fetch the article
6787         (let ((gnus-override-method
6788                (cond ((gnus-news-group-p gnus-newsgroup-name)
6789                       gnus-refer-article-method)
6790                      (arg
6791                       (or gnus-refer-article-method gnus-select-method))
6792                      (t nil)))
6793               number)
6794           ;; Start the special refer-article method, if necessary.
6795           (when (and gnus-refer-article-method
6796                      (gnus-news-group-p gnus-newsgroup-name))
6797             (gnus-check-server gnus-refer-article-method))
6798           ;; Fetch the header, and display the article.
6799           (if (setq number (gnus-summary-insert-subject message-id))
6800               (gnus-summary-select-article nil nil nil number)
6801             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6802
6803 (defun gnus-summary-edit-parameters ()
6804   "Edit the group parameters of the current group."
6805   (interactive)
6806   (gnus-group-edit-group gnus-newsgroup-name 'params))
6807
6808 (defun gnus-summary-customize-parameters ()
6809   "Customize the group parameters of the current group."
6810   (interactive)
6811   (gnus-group-customize gnus-newsgroup-name))
6812
6813 (defun gnus-summary-enter-digest-group (&optional force)
6814   "Enter an nndoc group based on the current article.
6815 If FORCE, force a digest interpretation.  If not, try
6816 to guess what the document format is."
6817   (interactive "P")
6818   (let ((conf gnus-current-window-configuration))
6819     (save-excursion
6820       (gnus-summary-select-article))
6821     (setq gnus-current-window-configuration conf)
6822     (let* ((name (format "%s-%d"
6823                          (gnus-group-prefixed-name
6824                           gnus-newsgroup-name (list 'nndoc ""))
6825                          (save-excursion
6826                            (set-buffer gnus-summary-buffer)
6827                            gnus-current-article)))
6828            (ogroup gnus-newsgroup-name)
6829            (params (append (gnus-info-params (gnus-get-info ogroup))
6830                            (list (cons 'to-group ogroup))
6831                            (list (cons 'save-article-group ogroup))))
6832            (case-fold-search t)
6833            (buf (current-buffer))
6834            dig)
6835       (save-excursion
6836         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6837         (insert-buffer-substring gnus-original-article-buffer)
6838         ;; Remove lines that may lead nndoc to misinterpret the
6839         ;; document type.
6840         (narrow-to-region
6841          (goto-char (point-min))
6842          (or (search-forward "\n\n" nil t) (point)))
6843         (goto-char (point-min))
6844         (delete-matching-lines "^Path:\\|^From ")
6845         (widen))
6846       (unwind-protect
6847           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6848                     (gnus-newsgroup-ephemeral-ignored-charsets
6849                      gnus-newsgroup-ignored-charsets))
6850                 (gnus-group-read-ephemeral-group
6851                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6852                               (nndoc-article-type
6853                                ,(if force 'mbox 'guess))) t))
6854               ;; Make all postings to this group go to the parent group.
6855               (nconc (gnus-info-params (gnus-get-info name))
6856                      params)
6857             ;; Couldn't select this doc group.
6858             (switch-to-buffer buf)
6859             (gnus-set-global-variables)
6860             (gnus-configure-windows 'summary)
6861             (gnus-message 3 "Article couldn't be entered?"))
6862         (kill-buffer dig)))))
6863
6864 (defun gnus-summary-read-document (n)
6865   "Open a new group based on the current article(s).
6866 This will allow you to read digests and other similar
6867 documents as newsgroups.
6868 Obeys the standard process/prefix convention."
6869   (interactive "P")
6870   (let* ((articles (gnus-summary-work-articles n))
6871          (ogroup gnus-newsgroup-name)
6872          (params (append (gnus-info-params (gnus-get-info ogroup))
6873                          (list (cons 'to-group ogroup))))
6874          article group egroup groups vgroup)
6875     (while (setq article (pop articles))
6876       (setq group (format "%s-%d" gnus-newsgroup-name article))
6877       (gnus-summary-remove-process-mark article)
6878       (when (gnus-summary-display-article article)
6879         (save-excursion
6880           (with-temp-buffer
6881             (insert-buffer-substring gnus-original-article-buffer)
6882             ;; Remove some headers that may lead nndoc to make
6883             ;; the wrong guess.
6884             (message-narrow-to-head)
6885             (goto-char (point-min))
6886             (delete-matching-lines "^\\(Path\\):\\|^From ")
6887             (widen)
6888             (if (setq egroup
6889                       (gnus-group-read-ephemeral-group
6890                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6891                                      (nndoc-article-type guess))
6892                        t nil t))
6893                 (progn
6894                   ;; Make all postings to this group go to the parent group.
6895                   (nconc (gnus-info-params (gnus-get-info egroup))
6896                          params)
6897                   (push egroup groups))
6898               ;; Couldn't select this doc group.
6899               (gnus-error 3 "Article couldn't be entered"))))))
6900     ;; Now we have selected all the documents.
6901     (cond
6902      ((not groups)
6903       (error "None of the articles could be interpreted as documents"))
6904      ((gnus-group-read-ephemeral-group
6905        (setq vgroup (format
6906                      "nnvirtual:%s-%s" gnus-newsgroup-name
6907                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6908        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6909        t
6910        (cons (current-buffer) 'summary)))
6911      (t
6912       (error "Couldn't select virtual nndoc group")))))
6913
6914 (defun gnus-summary-isearch-article (&optional regexp-p)
6915   "Do incremental search forward on the current article.
6916 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6917   (interactive "P")
6918   (let* ((gnus-inhibit-treatment t)
6919          (old (gnus-summary-select-article)))
6920     (gnus-configure-windows 'article)
6921     (gnus-eval-in-buffer-window gnus-article-buffer
6922       (save-restriction
6923         (widen)
6924         (when (eq 'old old)
6925           (gnus-article-show-all-headers))
6926         (goto-char (point-min))
6927         (isearch-forward regexp-p)))))
6928
6929 (defun gnus-summary-search-article-forward (regexp &optional backward)
6930   "Search for an article containing REGEXP forward.
6931 If BACKWARD, search backward instead."
6932   (interactive
6933    (list (read-string
6934           (format "Search article %s (regexp%s): "
6935                   (if current-prefix-arg "backward" "forward")
6936                   (if gnus-last-search-regexp
6937                       (concat ", default " gnus-last-search-regexp)
6938                     "")))
6939          current-prefix-arg))
6940   (if (string-equal regexp "")
6941       (setq regexp (or gnus-last-search-regexp ""))
6942     (setq gnus-last-search-regexp regexp))
6943   (if (gnus-summary-search-article regexp backward)
6944       (gnus-summary-show-thread)
6945     (error "Search failed: \"%s\"" regexp)))
6946
6947 (defun gnus-summary-search-article-backward (regexp)
6948   "Search for an article containing REGEXP backward."
6949   (interactive
6950    (list (read-string
6951           (format "Search article backward (regexp%s): "
6952                   (if gnus-last-search-regexp
6953                       (concat ", default " gnus-last-search-regexp)
6954                     "")))))
6955   (gnus-summary-search-article-forward regexp 'backward))
6956
6957 (eval-when-compile
6958   (defmacro gnus-summary-search-article-position-point (regexp backward)
6959     "Dehighlight the last matched text and goto the beginning position."
6960     (` (if (and gnus-summary-search-article-matched-data
6961                 (let ((text (caddr gnus-summary-search-article-matched-data))
6962                       (inhibit-read-only t)
6963                       buffer-read-only)
6964                   (delete-region
6965                    (goto-char (car gnus-summary-search-article-matched-data))
6966                    (cadr gnus-summary-search-article-matched-data))
6967                   (insert text)
6968                   (string-match (, regexp) text)))
6969            (if (, backward) (beginning-of-line) (end-of-line))
6970          (goto-char (if (, backward) (point-max) (point-min))))))
6971
6972   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
6973     "Place point where X-Face image is displayed."
6974     (if (featurep 'xemacs)
6975         (` (let ((end (if (search-forward "\n\n" nil t)
6976                           (goto-char (1- (point)))
6977                         (point-min)))
6978                  extent)
6979              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
6980              (unless (and (re-search-forward "^From:" end t)
6981                           (setq extent (extent-at (point)))
6982                           (extent-begin-glyph extent))
6983                (goto-char (, opoint)))))
6984       (` (let ((end (if (search-forward "\n\n" nil t)
6985                         (goto-char (1- (point)))
6986                       (point-min))))
6987            (goto-char
6988             (or (text-property-any (or (search-backward "\n\n" nil t)
6989                                        (point-min))
6990                                    end 'x-face-mule-bitmap-image t)
6991                 (, opoint)))))))
6992
6993   (defmacro gnus-summary-search-article-highlight-matched-text
6994     (backward treated x-face)
6995     "Highlight matched text in the function `gnus-summary-search-article'."
6996     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
6997              (end (set-marker (make-marker) (match-end 0)))
6998              (inhibit-read-only t)
6999              buffer-read-only)
7000          (unless treated
7001            (let ((,@
7002                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7003                     (mapcar
7004                      (lambda (item) (setq items (delq item items)))
7005                      '(gnus-treat-buttonize
7006                        gnus-treat-fill-article
7007                        gnus-treat-fill-long-lines
7008                        gnus-treat-emphasize
7009                        gnus-treat-highlight-headers
7010                        gnus-treat-highlight-citation
7011                        gnus-treat-highlight-signature
7012                        gnus-treat-overstrike
7013                        gnus-treat-display-xface
7014                        gnus-treat-buttonize-head
7015                        gnus-treat-decode-article-as-default-mime-charset))
7016                     (static-if (featurep 'xemacs)
7017                         items
7018                       (cons '(x-face-mule-delete-x-face-field
7019                               (quote never))
7020                             items))))
7021                  (gnus-treat-display-xface
7022                   (when (, x-face) gnus-treat-display-xface)))
7023              (gnus-article-prepare-mime-display)))
7024          (goto-char (if (, backward) start end))
7025          (when (, x-face)
7026            (gnus-summary-search-article-highlight-goto-x-face (point)))
7027          (setq gnus-summary-search-article-matched-data
7028                (list start end (buffer-substring start end)))
7029          (unless (eq start end);; matched text has been deleted. :-<
7030            (put-text-property start end 'face
7031                               (or (find-face 'isearch)
7032                                   'secondary-selection))))))
7033   )
7034
7035 (defun gnus-summary-search-article (regexp &optional backward)
7036   "Search for an article containing REGEXP.
7037 Optional argument BACKWARD means do search for backward.
7038 `gnus-select-article-hook' is not called during the search."
7039   ;; We have to require this here to make sure that the following
7040   ;; dynamic binding isn't shadowed by autoloading.
7041   (require 'gnus-async)
7042   (require 'gnus-art)
7043   (let ((gnus-select-article-hook nil)  ;Disable hook.
7044         (gnus-article-display-hook nil)
7045         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7046         (gnus-use-article-prefetch nil)
7047         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7048         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7049         (sum (current-buffer))
7050         (found nil)
7051         point treated)
7052     (gnus-save-hidden-threads
7053       (static-if (featurep 'xemacs)
7054           (let ((gnus-inhibit-treatment t))
7055             (setq treated (eq 'old (gnus-summary-select-article)))
7056             (when (and treated
7057                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7058                                  (window-live-p (get-buffer-window
7059                                                  gnus-article-buffer t)))))
7060               (gnus-summary-select-article nil t)
7061               (setq treated nil)))
7062         (let ((gnus-inhibit-treatment t)
7063               (x-face-mule-delete-x-face-field 'never))
7064           (setq treated (eq 'old (gnus-summary-select-article)))
7065           (when (and treated
7066                      (not
7067                       (and (gnus-buffer-live-p gnus-article-buffer)
7068                            (window-live-p (get-buffer-window
7069                                            gnus-article-buffer t))
7070                            (or (not (string-match "^\\^X-Face:" regexp))
7071                                (with-current-buffer gnus-article-buffer
7072                                  gnus-summary-search-article-matched-data)))))
7073             (gnus-summary-select-article nil t)
7074             (setq treated nil))))
7075       (set-buffer gnus-article-buffer)
7076       (widen)
7077       (if treated
7078           (progn
7079             (gnus-article-show-all-headers)
7080             (gnus-summary-search-article-position-point regexp backward))
7081         (goto-char (if backward (point-max) (point-min))))
7082       (while (not found)
7083         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7084         (if (if backward
7085                 (re-search-backward regexp nil t)
7086               (re-search-forward regexp nil t))
7087             ;; We found the regexp.
7088             (progn
7089               (gnus-summary-search-article-highlight-matched-text
7090                backward treated (string-match "^\\^X-Face:" regexp))
7091               (setq found 'found)
7092               (forward-line
7093                (/ (- 2 (window-height
7094                         (get-buffer-window gnus-article-buffer t)))
7095                   2))
7096               (set-window-start
7097                (get-buffer-window (current-buffer))
7098                (point))
7099               (set-buffer sum)
7100               (setq point (point)))
7101           ;; We didn't find it, so we go to the next article.
7102           (set-buffer sum)
7103           (setq found 'not)
7104           (while (eq found 'not)
7105             (if (not (if backward (gnus-summary-find-prev)
7106                        (gnus-summary-find-next)))
7107                 ;; No more articles.
7108                 (setq found t)
7109               ;; Select the next article and adjust point.
7110               (unless (gnus-summary-article-sparse-p
7111                        (gnus-summary-article-number))
7112                 (setq found nil)
7113                 (let ((gnus-inhibit-treatment t))
7114                   (gnus-summary-select-article))
7115                 (setq treated nil)
7116                 (set-buffer gnus-article-buffer)
7117                 (widen)
7118                 (goto-char (if backward (point-max) (point-min))))))))
7119       (gnus-message 7 ""))
7120     ;; Return whether we found the regexp.
7121     (when (eq found 'found)
7122       (goto-char point)
7123       (gnus-summary-show-thread)
7124       (gnus-summary-goto-subject gnus-current-article)
7125       (gnus-summary-position-point)
7126       t)))
7127
7128 (defun gnus-summary-find-matching (header regexp &optional backward unread
7129                                           not-case-fold)
7130   "Return a list of all articles that match REGEXP on HEADER.
7131 The search stars on the current article and goes forwards unless
7132 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7133 If UNREAD is non-nil, only unread articles will
7134 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7135 in the comparisons."
7136   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7137                 (gnus-data-find-list
7138                  (gnus-summary-article-number) (gnus-data-list backward))))
7139         (case-fold-search (not not-case-fold))
7140         articles d func)
7141     (if (consp header)
7142         (if (eq (car header) 'extra)
7143             (setq func
7144                   `(lambda (h)
7145                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7146                          "")))
7147           (error "%s is an invalid header" header))
7148       (unless (fboundp (intern (concat "mail-header-" header)))
7149         (error "%s is not a valid header" header))
7150       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7151     (while data
7152       (setq d (car data))
7153       (and (or (not unread)             ; We want all articles...
7154                (gnus-data-unread-p d))  ; Or just unreads.
7155            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7156            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7157            (push (gnus-data-number d) articles)) ; Success!
7158       (setq data (cdr data)))
7159     (nreverse articles)))
7160
7161 (defun gnus-summary-execute-command (header regexp command &optional backward)
7162   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7163 If HEADER is an empty string (or nil), the match is done on the entire
7164 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7165   (interactive
7166    (list (let ((completion-ignore-case t))
7167            (completing-read
7168             "Header name: "
7169             (mapcar (lambda (string) (list string))
7170                     '("Number" "Subject" "From" "Lines" "Date"
7171                       "Message-ID" "Xref" "References" "Body"))
7172             nil 'require-match))
7173          (read-string "Regexp: ")
7174          (read-key-sequence "Command: ")
7175          current-prefix-arg))
7176   (when (equal header "Body")
7177     (setq header ""))
7178   ;; Hidden thread subtrees must be searched as well.
7179   (gnus-summary-show-all-threads)
7180   ;; We don't want to change current point nor window configuration.
7181   (save-excursion
7182     (save-window-excursion
7183       (gnus-message 6 "Executing %s..." (key-description command))
7184       ;; We'd like to execute COMMAND interactively so as to give arguments.
7185       (gnus-execute header regexp
7186                     `(call-interactively ',(key-binding command))
7187                     backward)
7188       (gnus-message 6 "Executing %s...done" (key-description command)))))
7189
7190 (defun gnus-summary-beginning-of-article ()
7191   "Scroll the article back to the beginning."
7192   (interactive)
7193   (gnus-summary-select-article)
7194   (gnus-configure-windows 'article)
7195   (gnus-eval-in-buffer-window gnus-article-buffer
7196     (widen)
7197     (goto-char (point-min))
7198     (when gnus-page-broken
7199       (gnus-narrow-to-page))))
7200
7201 (defun gnus-summary-end-of-article ()
7202   "Scroll to the end of the article."
7203   (interactive)
7204   (gnus-summary-select-article)
7205   (gnus-configure-windows 'article)
7206   (gnus-eval-in-buffer-window gnus-article-buffer
7207     (widen)
7208     (goto-char (point-max))
7209     (recenter -3)
7210     (when gnus-page-broken
7211       (gnus-narrow-to-page))))
7212
7213 (defun gnus-summary-print-article (&optional filename n)
7214   "Generate and print a PostScript image of the N next (mail) articles.
7215
7216 If N is negative, print the N previous articles.  If N is nil and articles
7217 have been marked with the process mark, print these instead.
7218
7219 If the optional first argument FILENAME is nil, send the image to the
7220 printer.  If FILENAME is a string, save the PostScript image in a file with
7221 that name.  If FILENAME is a number, prompt the user for the name of the file
7222 to save in."
7223   (interactive (list (ps-print-preprint current-prefix-arg)
7224                      current-prefix-arg))
7225   (dolist (article (gnus-summary-work-articles n))
7226     (gnus-summary-select-article nil nil 'pseudo article)
7227     (gnus-eval-in-buffer-window gnus-article-buffer
7228       (let ((buffer (generate-new-buffer " *print*")))
7229         (unwind-protect
7230             (progn
7231               (copy-to-buffer buffer (point-min) (point-max))
7232               (set-buffer buffer)
7233               (gnus-article-delete-invisible-text)
7234               (let ((ps-left-header
7235                      (list
7236                       (concat "("
7237                               (mail-header-subject gnus-current-headers) ")")
7238                       (concat "("
7239                               (mail-header-from gnus-current-headers) ")")))
7240                     (ps-right-header
7241                      (list
7242                       "/pagenumberstring load"
7243                       (concat "("
7244                               (mail-header-date gnus-current-headers) ")"))))
7245                 (gnus-run-hooks 'gnus-ps-print-hook)
7246                 (save-excursion
7247                   (ps-print-buffer-with-faces filename))))
7248           (kill-buffer buffer))))))
7249
7250 (defun gnus-summary-show-article (&optional arg)
7251   "Force re-fetching of the current article.
7252 If ARG (the prefix) is non-nil, show the raw article without any
7253 article massaging functions being run."
7254   (interactive "P")
7255   (if (not arg)
7256       ;; Select the article the normal way.
7257       (gnus-summary-select-article nil 'force)
7258     ;; We have to require this here to make sure that the following
7259     ;; dynamic binding isn't shadowed by autoloading.
7260     (require 'gnus-async)
7261     (require 'gnus-art)
7262     ;; Bind the article treatment functions to nil.
7263     (let ((gnus-have-all-headers t)
7264           gnus-article-display-hook
7265           gnus-article-prepare-hook
7266           gnus-article-decode-hook
7267           gnus-break-pages
7268           gnus-show-mime
7269           gnus-visual)
7270       (gnus-summary-select-article nil 'force)))
7271   (gnus-summary-goto-subject gnus-current-article)
7272   (gnus-summary-position-point))
7273
7274 (defun gnus-summary-verbose-headers (&optional arg)
7275   "Toggle permanent full header display.
7276 If ARG is a positive number, turn header display on.
7277 If ARG is a negative number, turn header display off."
7278   (interactive "P")
7279   (setq gnus-show-all-headers
7280         (cond ((or (not (numberp arg))
7281                    (zerop arg))
7282                (not gnus-show-all-headers))
7283               ((natnump arg)
7284                t)))
7285   (gnus-summary-show-article))
7286
7287 (defun gnus-summary-toggle-header (&optional arg)
7288   "Show the headers if they are hidden, or hide them if they are shown.
7289 If ARG is a positive number, show the entire header.
7290 If ARG is a negative number, hide the unwanted header lines."
7291   (interactive "P")
7292   (save-excursion
7293     (set-buffer gnus-article-buffer)
7294     (save-restriction
7295       (let* ((buffer-read-only nil)
7296              (inhibit-point-motion-hooks t)
7297              hidden e)
7298         (save-restriction 
7299           (article-narrow-to-head)
7300           (setq hidden (gnus-article-hidden-text-p 'headers)))
7301         (goto-char (point-min))
7302         (when (search-forward "\n\n" nil t)
7303           (delete-region (point-min) (1- (point))))
7304         (goto-char (point-min))
7305         (save-excursion
7306           (set-buffer gnus-original-article-buffer)
7307           (goto-char (point-min))
7308           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7309         (insert-buffer-substring gnus-original-article-buffer 1 e)
7310         (save-restriction
7311           (narrow-to-region (point-min) (point))
7312           (article-decode-encoded-words)
7313           (if (or hidden
7314                   (and (numberp arg) (< arg 0)))
7315               (let ((gnus-treat-hide-headers nil)
7316                     (gnus-treat-hide-boring-headers nil))
7317                 (gnus-treat-article 'head))
7318             (gnus-treat-article 'head)))))))
7319
7320 (defun gnus-summary-show-all-headers ()
7321   "Make all header lines visible."
7322   (interactive)
7323   (gnus-article-show-all-headers))
7324
7325 (defun gnus-summary-toggle-mime (&optional arg)
7326   "Toggle MIME processing.
7327 If ARG is a positive number, turn MIME processing on."
7328   (interactive "P")
7329   (setq gnus-show-mime
7330         (if (null arg)
7331             (not gnus-show-mime)
7332           (> (prefix-numeric-value arg) 0)))
7333   (gnus-summary-select-article t 'force))
7334
7335 (defun gnus-summary-caesar-message (&optional arg)
7336   "Caesar rotate the current article by 13.
7337 The numerical prefix specifies how many places to rotate each letter
7338 forward."
7339   (interactive "P")
7340   (gnus-summary-select-article)
7341   (let ((mail-header-separator ""))
7342     (gnus-eval-in-buffer-window gnus-article-buffer
7343       (save-restriction
7344         (widen)
7345         (let ((start (window-start))
7346               buffer-read-only)
7347           (message-caesar-buffer-body arg)
7348           (set-window-start (get-buffer-window (current-buffer)) start))))))
7349
7350 (defun gnus-summary-stop-page-breaking ()
7351   "Stop page breaking in the current article."
7352   (interactive)
7353   (gnus-summary-select-article)
7354   (gnus-eval-in-buffer-window gnus-article-buffer
7355     (widen)
7356     (when (gnus-visual-p 'page-marker)
7357       (let ((buffer-read-only nil))
7358         (gnus-remove-text-with-property 'gnus-prev)
7359         (gnus-remove-text-with-property 'gnus-next))
7360       (setq gnus-page-broken nil))))
7361
7362 (defun gnus-summary-move-article (&optional n to-newsgroup
7363                                             select-method action)
7364   "Move the current article to a different newsgroup.
7365 If N is a positive number, move the N next articles.
7366 If N is a negative number, move the N previous articles.
7367 If N is nil and any articles have been marked with the process mark,
7368 move those articles instead.
7369 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7370 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7371 re-spool using this method.
7372
7373 For this function to work, both the current newsgroup and the
7374 newsgroup that you want to move to have to support the `request-move'
7375 and `request-accept' functions."
7376   (interactive "P")
7377   (unless action
7378     (setq action 'move))
7379   ;; Disable marking as read.
7380   (let (gnus-mark-article-hook)
7381     (save-window-excursion
7382       (gnus-summary-select-article)))
7383   ;; Check whether the source group supports the required functions.
7384   (cond ((and (eq action 'move)
7385               (not (gnus-check-backend-function
7386                     'request-move-article gnus-newsgroup-name)))
7387          (error "The current group does not support article moving"))
7388         ((and (eq action 'crosspost)
7389               (not (gnus-check-backend-function
7390                     'request-replace-article gnus-newsgroup-name)))
7391          (error "The current group does not support article editing")))
7392   (let ((articles (gnus-summary-work-articles n))
7393         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7394         (names '((move "Move" "Moving")
7395                  (copy "Copy" "Copying")
7396                  (crosspost "Crosspost" "Crossposting")))
7397         (copy-buf (save-excursion
7398                     (nnheader-set-temp-buffer " *copy article*")))
7399         (default-marks gnus-article-mark-lists)
7400         (no-expire-marks (delete '(expirable . expire)
7401                                  (copy-sequence gnus-article-mark-lists)))
7402         art-group to-method new-xref article to-groups)
7403     (unless (assq action names)
7404       (error "Unknown action %s" action))
7405     ;; Read the newsgroup name.
7406     (when (and (not to-newsgroup)
7407                (not select-method))
7408       (setq to-newsgroup
7409             (gnus-read-move-group-name
7410              (cadr (assq action names))
7411              (symbol-value (intern (format "gnus-current-%s-group" action)))
7412              articles prefix))
7413       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7414     (setq to-method (or select-method
7415                         (gnus-group-name-to-method to-newsgroup)))
7416     ;; Check the method we are to move this article to...
7417     (unless (gnus-check-backend-function
7418              'request-accept-article (car to-method))
7419       (error "%s does not support article copying" (car to-method)))
7420     (unless (gnus-check-server to-method)
7421       (error "Can't open server %s" (car to-method)))
7422     (gnus-message 6 "%s to %s: %s..."
7423                   (caddr (assq action names))
7424                   (or (car select-method) to-newsgroup) articles)
7425     (while articles
7426       (setq article (pop articles))
7427       (setq
7428        art-group
7429        (cond
7430         ;; Move the article.
7431         ((eq action 'move)
7432          ;; Remove this article from future suppression.
7433          (gnus-dup-unsuppress-article article)
7434          (gnus-request-move-article
7435           article                       ; Article to move
7436           gnus-newsgroup-name           ; From newsgroup
7437           (nth 1 (gnus-find-method-for-group
7438                   gnus-newsgroup-name)) ; Server
7439           (list 'gnus-request-accept-article
7440                 to-newsgroup (list 'quote select-method)
7441                 (not articles) t)               ; Accept form
7442           (not articles)))              ; Only save nov last time
7443         ;; Copy the article.
7444         ((eq action 'copy)
7445          (save-excursion
7446            (set-buffer copy-buf)
7447            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7448              (gnus-request-accept-article
7449               to-newsgroup select-method (not articles) t))))
7450         ;; Crosspost the article.
7451         ((eq action 'crosspost)
7452          (let ((xref (message-tokenize-header
7453                       (mail-header-xref (gnus-summary-article-header article))
7454                       " ")))
7455            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7456                                   ":" article))
7457            (unless xref
7458              (setq xref (list (system-name))))
7459            (setq new-xref
7460                  (concat
7461                   (mapconcat 'identity
7462                              (delete "Xref:" (delete new-xref xref))
7463                              " ")
7464                   " " new-xref))
7465            (save-excursion
7466              (set-buffer copy-buf)
7467              ;; First put the article in the destination group.
7468              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7469              (when (consp (setq art-group
7470                                 (gnus-request-accept-article
7471                                  to-newsgroup select-method (not articles))))
7472                (setq new-xref (concat new-xref " " (car art-group)
7473                                       ":" (cdr art-group)))
7474                ;; Now we have the new Xrefs header, so we insert
7475                ;; it and replace the new article.
7476                (nnheader-replace-header "Xref" new-xref)
7477                (gnus-request-replace-article
7478                 (cdr art-group) to-newsgroup (current-buffer))
7479                art-group))))))
7480       (cond
7481        ((not art-group)
7482        (gnus-message 1 "Couldn't %s article %s: %s"
7483                      (cadr (assq action names)) article
7484                      (nnheader-get-report (car to-method))))
7485        ((and (eq art-group 'junk)
7486              (eq action 'move))
7487         (gnus-summary-mark-article article gnus-canceled-mark)
7488         (gnus-message 4 "Deleted article %s" article))
7489        (t
7490         (let* ((pto-group (gnus-group-prefixed-name
7491                            (car art-group) to-method))
7492                (entry
7493                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7494                (info (nth 2 entry))
7495                (to-group (gnus-info-group info))
7496                to-marks)
7497           ;; Update the group that has been moved to.
7498           (when (and info
7499                      (memq action '(move copy)))
7500             (unless (member to-group to-groups)
7501               (push to-group to-groups))
7502
7503             (unless (memq article gnus-newsgroup-unreads)
7504               (push 'read to-marks)
7505               (gnus-info-set-read
7506                info (gnus-add-to-range (gnus-info-read info)
7507                                        (list (cdr art-group)))))
7508
7509             ;; Copy any marks over to the new group.
7510             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7511                              default-marks
7512                            no-expire-marks))
7513                   (to-article (cdr art-group)))
7514
7515               ;; See whether the article is to be put in the cache.
7516               (when gnus-use-cache
7517                 (gnus-cache-possibly-enter-article
7518                  to-group to-article
7519                  (let ((header (copy-sequence
7520                                 (gnus-summary-article-header article))))
7521                    (mail-header-set-number header to-article)
7522                    header)
7523                  (memq article gnus-newsgroup-marked)
7524                  (memq article gnus-newsgroup-dormant)
7525                  (memq article gnus-newsgroup-unreads)))
7526
7527               (when (and (equal to-group gnus-newsgroup-name)
7528                          (not (memq article gnus-newsgroup-unreads)))
7529                 ;; Mark this article as read in this group.
7530                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7531                 (setcdr (gnus-active to-group) to-article)
7532                 (setcdr gnus-newsgroup-active to-article))
7533
7534               (while marks
7535                 (when (memq article (symbol-value
7536                                      (intern (format "gnus-newsgroup-%s"
7537                                                      (caar marks)))))
7538                   (push (cdar marks) to-marks)
7539                   ;; If the other group is the same as this group,
7540                   ;; then we have to add the mark to the list.
7541                   (when (equal to-group gnus-newsgroup-name)
7542                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7543                          (cons to-article
7544                                (symbol-value
7545                                 (intern (format "gnus-newsgroup-%s"
7546                                                 (caar marks)))))))
7547                   ;; Copy the marks to other group.
7548                   (gnus-add-marked-articles
7549                    to-group (cdar marks) (list to-article) info))
7550                 (setq marks (cdr marks)))
7551
7552               (gnus-request-set-mark to-group (list (list (list to-article)
7553                                                           'set
7554                                                           to-marks)))
7555
7556               (gnus-dribble-enter
7557                (concat "(gnus-group-set-info '"
7558                        (gnus-prin1-to-string (gnus-get-info to-group))
7559                        ")"))))
7560
7561           ;; Update the Xref header in this article to point to
7562           ;; the new crossposted article we have just created.
7563           (when (eq action 'crosspost)
7564             (save-excursion
7565               (set-buffer copy-buf)
7566               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7567               (nnheader-replace-header "Xref" new-xref)
7568               (gnus-request-replace-article
7569                article gnus-newsgroup-name (current-buffer)))))
7570
7571         ;;;!!!Why is this necessary?
7572         (set-buffer gnus-summary-buffer)
7573
7574         (gnus-summary-goto-subject article)
7575         (when (eq action 'move)
7576           (gnus-summary-mark-article article gnus-canceled-mark))))
7577       (gnus-summary-remove-process-mark article))
7578     ;; Re-activate all groups that have been moved to.
7579     (while to-groups
7580       (save-excursion
7581         (set-buffer gnus-group-buffer)
7582         (when (gnus-group-goto-group (car to-groups) t)
7583           (gnus-group-get-new-news-this-group 1 t))
7584         (pop to-groups)))
7585
7586     (gnus-kill-buffer copy-buf)
7587     (gnus-summary-position-point)
7588     (gnus-set-mode-line 'summary)))
7589
7590 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7591   "Move the current article to a different newsgroup.
7592 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7593 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7594 re-spool using this method."
7595   (interactive "P")
7596   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7597
7598 (defun gnus-summary-crosspost-article (&optional n)
7599   "Crosspost the current article to some other group."
7600   (interactive "P")
7601   (gnus-summary-move-article n nil nil 'crosspost))
7602
7603 (defcustom gnus-summary-respool-default-method nil
7604   "Default method for respooling an article.
7605 If nil, use to the current newsgroup method."
7606   :type '(choice (gnus-select-method :value (nnml ""))
7607                  (const nil))
7608   :group 'gnus-summary-mail)
7609
7610 (defun gnus-summary-respool-article (&optional n method)
7611   "Respool the current article.
7612 The article will be squeezed through the mail spooling process again,
7613 which means that it will be put in some mail newsgroup or other
7614 depending on `nnmail-split-methods'.
7615 If N is a positive number, respool the N next articles.
7616 If N is a negative number, respool the N previous articles.
7617 If N is nil and any articles have been marked with the process mark,
7618 respool those articles instead.
7619
7620 Respooling can be done both from mail groups and \"real\" newsgroups.
7621 In the former case, the articles in question will be moved from the
7622 current group into whatever groups they are destined to.  In the
7623 latter case, they will be copied into the relevant groups."
7624   (interactive
7625    (list current-prefix-arg
7626          (let* ((methods (gnus-methods-using 'respool))
7627                 (methname
7628                  (symbol-name (or gnus-summary-respool-default-method
7629                                   (car (gnus-find-method-for-group
7630                                         gnus-newsgroup-name)))))
7631                 (method
7632                  (gnus-completing-read
7633                   methname "What backend do you want to use when respooling?"
7634                   methods nil t nil 'gnus-mail-method-history))
7635                 ms)
7636            (cond
7637             ((zerop (length (setq ms (gnus-servers-using-backend
7638                                       (intern method)))))
7639              (list (intern method) ""))
7640             ((= 1 (length ms))
7641              (car ms))
7642             (t
7643              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7644                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7645                            ms-alist))))))))
7646   (unless method
7647     (error "No method given for respooling"))
7648   (if (assoc (symbol-name
7649               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7650              (gnus-methods-using 'respool))
7651       (gnus-summary-move-article n nil method)
7652     (gnus-summary-copy-article n nil method)))
7653
7654 (defun gnus-summary-import-article (file)
7655   "Import an arbitrary file into a mail newsgroup."
7656   (interactive "fImport file: ")
7657   (let ((group gnus-newsgroup-name)
7658         (now (current-time))
7659         atts lines)
7660     (unless (gnus-check-backend-function 'request-accept-article group)
7661       (error "%s does not support article importing" group))
7662     (or (file-readable-p file)
7663         (not (file-regular-p file))
7664         (error "Can't read %s" file))
7665     (save-excursion
7666       (set-buffer (gnus-get-buffer-create " *import file*"))
7667       (erase-buffer)
7668       (nnheader-insert-file-contents file)
7669       (goto-char (point-min))
7670       (unless (nnheader-article-p)
7671         ;; This doesn't look like an article, so we fudge some headers.
7672         (setq atts (file-attributes file)
7673               lines (count-lines (point-min) (point-max)))
7674         (insert "From: " (read-string "From: ") "\n"
7675                 "Subject: " (read-string "Subject: ") "\n"
7676                 "Date: " (message-make-date (nth 5 atts))
7677                 "\n"
7678                 "Message-ID: " (message-make-message-id) "\n"
7679                 "Lines: " (int-to-string lines) "\n"
7680                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7681       (gnus-request-accept-article group nil t)
7682       (kill-buffer (current-buffer)))))
7683
7684 (defun gnus-summary-article-posted-p ()
7685   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7686 This will be the case if the article has both been mailed and posted."
7687   (interactive)
7688   (let ((id (mail-header-references (gnus-summary-article-header)))
7689         (gnus-override-method
7690          (or gnus-refer-article-method gnus-select-method)))
7691     (if (gnus-request-head id "")
7692         (gnus-message 2 "The current message was found on %s"
7693                       gnus-override-method)
7694       (gnus-message 2 "The current message couldn't be found on %s"
7695                     gnus-override-method)
7696       nil)))
7697
7698 (defun gnus-summary-expire-articles (&optional now)
7699   "Expire all articles that are marked as expirable in the current group."
7700   (interactive)
7701   (when (gnus-check-backend-function
7702          'request-expire-articles gnus-newsgroup-name)
7703     ;; This backend supports expiry.
7704     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7705            (expirable (if total
7706                           (progn
7707                             ;; We need to update the info for
7708                             ;; this group for `gnus-list-of-read-articles'
7709                             ;; to give us the right answer.
7710                             (gnus-run-hooks 'gnus-exit-group-hook)
7711                             (gnus-summary-update-info)
7712                             (gnus-list-of-read-articles gnus-newsgroup-name))
7713                         (setq gnus-newsgroup-expirable
7714                               (sort gnus-newsgroup-expirable '<))))
7715            (expiry-wait (if now 'immediate
7716                           (gnus-group-find-parameter
7717                            gnus-newsgroup-name 'expiry-wait)))
7718            es)
7719       (when expirable
7720         ;; There are expirable articles in this group, so we run them
7721         ;; through the expiry process.
7722         (gnus-message 6 "Expiring articles...")
7723         (unless (gnus-check-group gnus-newsgroup-name)
7724           (error "Can't open server for %s" gnus-newsgroup-name))
7725         ;; The list of articles that weren't expired is returned.
7726         (save-excursion
7727           (if expiry-wait
7728               (let ((nnmail-expiry-wait-function nil)
7729                     (nnmail-expiry-wait expiry-wait))
7730                 (setq es (gnus-request-expire-articles
7731                           expirable gnus-newsgroup-name)))
7732             (setq es (gnus-request-expire-articles
7733                       expirable gnus-newsgroup-name))))
7734         (unless total
7735           (setq gnus-newsgroup-expirable es))
7736         ;; We go through the old list of expirable, and mark all
7737         ;; really expired articles as nonexistent.
7738         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7739           (let ((gnus-use-cache nil))
7740             (while expirable
7741               (unless (memq (car expirable) es)
7742                 (when (gnus-data-find (car expirable))
7743                   (gnus-summary-mark-article
7744                    (car expirable) gnus-canceled-mark)))
7745               (setq expirable (cdr expirable)))))
7746         (gnus-message 6 "Expiring articles...done")))))
7747
7748 (defun gnus-summary-expire-articles-now ()
7749   "Expunge all expirable articles in the current group.
7750 This means that *all* articles that are marked as expirable will be
7751 deleted forever, right now."
7752   (interactive)
7753   (or gnus-expert-user
7754       (gnus-yes-or-no-p
7755        "Are you really, really, really sure you want to delete all these messages? ")
7756       (error "Phew!"))
7757   (gnus-summary-expire-articles t))
7758
7759 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7760 (defun gnus-summary-delete-article (&optional n)
7761   "Delete the N next (mail) articles.
7762 This command actually deletes articles.  This is not a marking
7763 command.  The article will disappear forever from your life, never to
7764 return.
7765 If N is negative, delete backwards.
7766 If N is nil and articles have been marked with the process mark,
7767 delete these instead."
7768   (interactive "P")
7769   (unless (gnus-check-backend-function 'request-expire-articles
7770                                        gnus-newsgroup-name)
7771     (error "The current newsgroup does not support article deletion"))
7772   ;; Compute the list of articles to delete.
7773   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7774         not-deleted)
7775     (if (and gnus-novice-user
7776              (not (gnus-yes-or-no-p
7777                    (format "Do you really want to delete %s forever? "
7778                            (if (> (length articles) 1)
7779                                (format "these %s articles" (length articles))
7780                              "this article")))))
7781         ()
7782       ;; Delete the articles.
7783       (setq not-deleted (gnus-request-expire-articles
7784                          articles gnus-newsgroup-name 'force))
7785       (while articles
7786         (gnus-summary-remove-process-mark (car articles))
7787         ;; The backend might not have been able to delete the article
7788         ;; after all.
7789         (unless (memq (car articles) not-deleted)
7790           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7791         (setq articles (cdr articles)))
7792       (when not-deleted
7793         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7794     (gnus-summary-position-point)
7795     (gnus-set-mode-line 'summary)
7796     not-deleted))
7797
7798 (defun gnus-summary-edit-article (&optional force)
7799   "Edit the current article.
7800 This will have permanent effect only in mail groups.
7801 If FORCE is non-nil, allow editing of articles even in read-only
7802 groups."
7803   (interactive "P")
7804   (save-excursion
7805     (set-buffer gnus-summary-buffer)
7806     (let ((mail-parse-charset gnus-newsgroup-charset)
7807           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7808       (gnus-set-global-variables)
7809       (when (and (not force)
7810                  (gnus-group-read-only-p))
7811         (error "The current newsgroup does not support article editing"))
7812       (gnus-summary-show-article t)
7813       (gnus-article-edit-article
7814        'ignore
7815        `(lambda (no-highlight)
7816           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7817                 (mail-parse-ignored-charsets
7818                  ',gnus-newsgroup-ignored-charsets))
7819             (gnus-summary-edit-article-done
7820              ,(or (mail-header-references gnus-current-headers) "")
7821              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7822
7823 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7824
7825 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7826                                                  no-highlight)
7827   "Make edits to the current article permanent."
7828   (interactive)
7829   ;; Replace the article.
7830   (let ((buf (current-buffer)))
7831     (with-temp-buffer
7832       (insert-buffer-substring buf)
7833       (if (and (not read-only)
7834                (not (gnus-request-replace-article
7835                      (cdr gnus-article-current) (car gnus-article-current)
7836                      (current-buffer) t)))
7837           (error "Couldn't replace article")
7838         ;; Update the summary buffer.
7839         (if (and references
7840                  (equal (message-tokenize-header references " ")
7841                         (message-tokenize-header
7842                          (or (message-fetch-field "references") "") " ")))
7843             ;; We only have to update this line.
7844             (save-excursion
7845               (save-restriction
7846                 (message-narrow-to-head)
7847                 (let ((head (buffer-string))
7848                       header)
7849                   (with-temp-buffer
7850                     (insert (format "211 %d Article retrieved.\n"
7851                                     (cdr gnus-article-current)))
7852                     (insert head)
7853                     (insert ".\n")
7854                     (let ((nntp-server-buffer (current-buffer)))
7855                       (setq header (car (gnus-get-newsgroup-headers
7856                                          (save-excursion
7857                                            (set-buffer gnus-summary-buffer)
7858                                            gnus-newsgroup-dependencies)
7859                                          t))))
7860                     (save-excursion
7861                       (set-buffer gnus-summary-buffer)
7862                       (gnus-data-set-header
7863                        (gnus-data-find (cdr gnus-article-current))
7864                        header)
7865                       (gnus-summary-update-article-line
7866                        (cdr gnus-article-current) header))))))
7867           ;; Update threads.
7868           (set-buffer (or buffer gnus-summary-buffer))
7869           (gnus-summary-update-article (cdr gnus-article-current)))
7870         ;; Prettify the article buffer again.
7871         (unless no-highlight
7872           (save-excursion
7873             (set-buffer gnus-article-buffer)
7874             ;;;!!! Fix this -- article should be rehighlighted.
7875             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7876             (set-buffer gnus-original-article-buffer)
7877             (gnus-request-article
7878              (cdr gnus-article-current)
7879              (car gnus-article-current) (current-buffer))))
7880         ;; Prettify the summary buffer line.
7881         (when (gnus-visual-p 'summary-highlight 'highlight)
7882           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7883
7884 (defun gnus-summary-edit-wash (key)
7885   "Perform editing command KEY in the article buffer."
7886   (interactive
7887    (list
7888     (progn
7889       (message "%s" (concat (this-command-keys) "- "))
7890       (read-char))))
7891   (message "")
7892   (gnus-summary-edit-article)
7893   (execute-kbd-macro (concat (this-command-keys) key))
7894   (gnus-article-edit-done))
7895
7896 ;;; Respooling
7897
7898 (defun gnus-summary-respool-query (&optional silent trace)
7899   "Query where the respool algorithm would put this article."
7900   (interactive)
7901   (let (gnus-mark-article-hook)
7902     (gnus-summary-select-article)
7903     (save-excursion
7904       (set-buffer gnus-original-article-buffer)
7905       (save-restriction
7906         (message-narrow-to-head)
7907         (let ((groups (nnmail-article-group 'identity trace)))
7908           (unless silent
7909             (if groups
7910                 (message "This message would go to %s"
7911                          (mapconcat 'car groups ", "))
7912               (message "This message would go to no groups"))
7913             groups))))))
7914
7915 (defun gnus-summary-respool-trace ()
7916   "Trace where the respool algorithm would put this article.
7917 Display a buffer showing all fancy splitting patterns which matched."
7918   (interactive)
7919   (gnus-summary-respool-query nil t))
7920
7921 ;; Summary marking commands.
7922
7923 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7924   "Mark articles which has the same subject as read, and then select the next.
7925 If UNMARK is positive, remove any kind of mark.
7926 If UNMARK is negative, tick articles."
7927   (interactive "P")
7928   (when unmark
7929     (setq unmark (prefix-numeric-value unmark)))
7930   (let ((count
7931          (gnus-summary-mark-same-subject
7932           (gnus-summary-article-subject) unmark)))
7933     ;; Select next unread article.  If auto-select-same mode, should
7934     ;; select the first unread article.
7935     (gnus-summary-next-article t (and gnus-auto-select-same
7936                                       (gnus-summary-article-subject)))
7937     (gnus-message 7 "%d article%s marked as %s"
7938                   count (if (= count 1) " is" "s are")
7939                   (if unmark "unread" "read"))))
7940
7941 (defun gnus-summary-kill-same-subject (&optional unmark)
7942   "Mark articles which has the same subject as read.
7943 If UNMARK is positive, remove any kind of mark.
7944 If UNMARK is negative, tick articles."
7945   (interactive "P")
7946   (when unmark
7947     (setq unmark (prefix-numeric-value unmark)))
7948   (let ((count
7949          (gnus-summary-mark-same-subject
7950           (gnus-summary-article-subject) unmark)))
7951     ;; If marked as read, go to next unread subject.
7952     (when (null unmark)
7953       ;; Go to next unread subject.
7954       (gnus-summary-next-subject 1 t))
7955     (gnus-message 7 "%d articles are marked as %s"
7956                   count (if unmark "unread" "read"))))
7957
7958 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7959   "Mark articles with same SUBJECT as read, and return marked number.
7960 If optional argument UNMARK is positive, remove any kinds of marks.
7961 If optional argument UNMARK is negative, mark articles as unread instead."
7962   (let ((count 1))
7963     (save-excursion
7964       (cond
7965        ((null unmark)                   ; Mark as read.
7966         (while (and
7967                 (progn
7968                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7969                   (gnus-summary-show-thread) t)
7970                 (gnus-summary-find-subject subject))
7971           (setq count (1+ count))))
7972        ((> unmark 0)                    ; Tick.
7973         (while (and
7974                 (progn
7975                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7976                   (gnus-summary-show-thread) t)
7977                 (gnus-summary-find-subject subject))
7978           (setq count (1+ count))))
7979        (t                               ; Mark as unread.
7980         (while (and
7981                 (progn
7982                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7983                   (gnus-summary-show-thread) t)
7984                 (gnus-summary-find-subject subject))
7985           (setq count (1+ count)))))
7986       (gnus-set-mode-line 'summary)
7987       ;; Return the number of marked articles.
7988       count)))
7989
7990 (defun gnus-summary-mark-as-processable (n &optional unmark)
7991   "Set the process mark on the next N articles.
7992 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7993 the process mark instead.  The difference between N and the actual
7994 number of articles marked is returned."
7995   (interactive "p")
7996   (let ((backward (< n 0))
7997         (n (abs n)))
7998     (while (and
7999             (> n 0)
8000             (if unmark
8001                 (gnus-summary-remove-process-mark
8002                  (gnus-summary-article-number))
8003               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8004             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8005       (setq n (1- n)))
8006     (when (/= 0 n)
8007       (gnus-message 7 "No more articles"))
8008     (gnus-summary-recenter)
8009     (gnus-summary-position-point)
8010     n))
8011
8012 (defun gnus-summary-unmark-as-processable (n)
8013   "Remove the process mark from the next N articles.
8014 If N is negative, unmark backward instead.  The difference between N and
8015 the actual number of articles unmarked is returned."
8016   (interactive "p")
8017   (gnus-summary-mark-as-processable n t))
8018
8019 (defun gnus-summary-unmark-all-processable ()
8020   "Remove the process mark from all articles."
8021   (interactive)
8022   (save-excursion
8023     (while gnus-newsgroup-processable
8024       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8025   (gnus-summary-position-point))
8026
8027 (defun gnus-summary-mark-as-expirable (n)
8028   "Mark N articles forward as expirable.
8029 If N is negative, mark backward instead.  The difference between N and
8030 the actual number of articles marked is returned."
8031   (interactive "p")
8032   (gnus-summary-mark-forward n gnus-expirable-mark))
8033
8034 (defun gnus-summary-mark-article-as-replied (article)
8035   "Mark ARTICLE replied and update the summary line."
8036   (push article gnus-newsgroup-replied)
8037   (let ((buffer-read-only nil))
8038     (when (gnus-summary-goto-subject article nil t)
8039       (gnus-summary-update-secondary-mark article))))
8040
8041 (defun gnus-summary-set-bookmark (article)
8042   "Set a bookmark in current article."
8043   (interactive (list (gnus-summary-article-number)))
8044   (when (or (not (get-buffer gnus-article-buffer))
8045             (not gnus-current-article)
8046             (not gnus-article-current)
8047             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8048     (error "No current article selected"))
8049   ;; Remove old bookmark, if one exists.
8050   (let ((old (assq article gnus-newsgroup-bookmarks)))
8051     (when old
8052       (setq gnus-newsgroup-bookmarks
8053             (delq old gnus-newsgroup-bookmarks))))
8054   ;; Set the new bookmark, which is on the form
8055   ;; (article-number . line-number-in-body).
8056   (push
8057    (cons article
8058          (save-excursion
8059            (set-buffer gnus-article-buffer)
8060            (count-lines
8061             (min (point)
8062                  (save-excursion
8063                    (goto-char (point-min))
8064                    (search-forward "\n\n" nil t)
8065                    (point)))
8066             (point))))
8067    gnus-newsgroup-bookmarks)
8068   (gnus-message 6 "A bookmark has been added to the current article."))
8069
8070 (defun gnus-summary-remove-bookmark (article)
8071   "Remove the bookmark from the current article."
8072   (interactive (list (gnus-summary-article-number)))
8073   ;; Remove old bookmark, if one exists.
8074   (let ((old (assq article gnus-newsgroup-bookmarks)))
8075     (if old
8076         (progn
8077           (setq gnus-newsgroup-bookmarks
8078                 (delq old gnus-newsgroup-bookmarks))
8079           (gnus-message 6 "Removed bookmark."))
8080       (gnus-message 6 "No bookmark in current article."))))
8081
8082 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8083 (defun gnus-summary-mark-as-dormant (n)
8084   "Mark N articles forward as dormant.
8085 If N is negative, mark backward instead.  The difference between N and
8086 the actual number of articles marked is returned."
8087   (interactive "p")
8088   (gnus-summary-mark-forward n gnus-dormant-mark))
8089
8090 (defun gnus-summary-set-process-mark (article)
8091   "Set the process mark on ARTICLE and update the summary line."
8092   (setq gnus-newsgroup-processable
8093         (cons article
8094               (delq article gnus-newsgroup-processable)))
8095   (when (gnus-summary-goto-subject article)
8096     (gnus-summary-show-thread)
8097     (gnus-summary-goto-subject article)
8098     (gnus-summary-update-secondary-mark article)))
8099
8100 (defun gnus-summary-remove-process-mark (article)
8101   "Remove the process mark from ARTICLE and update the summary line."
8102   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8103   (when (gnus-summary-goto-subject article)
8104     (gnus-summary-show-thread)
8105     (gnus-summary-goto-subject article)
8106     (gnus-summary-update-secondary-mark article)))
8107
8108 (defun gnus-summary-set-saved-mark (article)
8109   "Set the process mark on ARTICLE and update the summary line."
8110   (push article gnus-newsgroup-saved)
8111   (when (gnus-summary-goto-subject article)
8112     (gnus-summary-update-secondary-mark article)))
8113
8114 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8115   "Mark N articles as read forwards.
8116 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8117 The difference between N and the actual number of articles marked is
8118 returned."
8119   (interactive "p")
8120   (gnus-summary-show-thread)
8121   (let ((backward (< n 0))
8122         (gnus-summary-goto-unread
8123          (and gnus-summary-goto-unread
8124               (not (eq gnus-summary-goto-unread 'never))
8125               (not (memq mark (list gnus-unread-mark
8126                                     gnus-ticked-mark gnus-dormant-mark)))))
8127         (n (abs n))
8128         (mark (or mark gnus-del-mark)))
8129     (while (and (> n 0)
8130                 (gnus-summary-mark-article nil mark no-expire)
8131                 (zerop (gnus-summary-next-subject
8132                         (if backward -1 1)
8133                         (and gnus-summary-goto-unread
8134                              (not (eq gnus-summary-goto-unread 'never)))
8135                         t)))
8136       (setq n (1- n)))
8137     (when (/= 0 n)
8138       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8139     (gnus-summary-recenter)
8140     (gnus-summary-position-point)
8141     (gnus-set-mode-line 'summary)
8142     n))
8143
8144 (defun gnus-summary-mark-article-as-read (mark)
8145   "Mark the current article quickly as read with MARK."
8146   (let ((article (gnus-summary-article-number)))
8147     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8148     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8149     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8150     (push (cons article mark) gnus-newsgroup-reads)
8151     ;; Possibly remove from cache, if that is used.
8152     (when gnus-use-cache
8153       (gnus-cache-enter-remove-article article))
8154     ;; Allow the backend to change the mark.
8155     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8156     ;; Check for auto-expiry.
8157     (when (and gnus-newsgroup-auto-expire
8158                (memq mark gnus-auto-expirable-marks))
8159       (setq mark gnus-expirable-mark)
8160       ;; Let the backend know about the mark change.
8161       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8162       (push article gnus-newsgroup-expirable))
8163     ;; Set the mark in the buffer.
8164     (gnus-summary-update-mark mark 'unread)
8165     t))
8166
8167 (defun gnus-summary-mark-article-as-unread (mark)
8168   "Mark the current article quickly as unread with MARK."
8169   (let* ((article (gnus-summary-article-number))
8170          (old-mark (gnus-summary-article-mark article)))
8171     ;; Allow the backend to change the mark.
8172     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8173     (if (eq mark old-mark)
8174         t
8175       (if (<= article 0)
8176           (progn
8177             (gnus-error 1 "Can't mark negative article numbers")
8178             nil)
8179         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8180         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8181         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8182         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8183         (cond ((= mark gnus-ticked-mark)
8184                (push article gnus-newsgroup-marked))
8185               ((= mark gnus-dormant-mark)
8186                (push article gnus-newsgroup-dormant))
8187               (t
8188                (push article gnus-newsgroup-unreads)))
8189         (gnus-pull article gnus-newsgroup-reads)
8190
8191         ;; See whether the article is to be put in the cache.
8192         (and gnus-use-cache
8193              (vectorp (gnus-summary-article-header article))
8194              (save-excursion
8195                (gnus-cache-possibly-enter-article
8196                 gnus-newsgroup-name article
8197                 (gnus-summary-article-header article)
8198                 (= mark gnus-ticked-mark)
8199                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8200
8201         ;; Fix the mark.
8202         (gnus-summary-update-mark mark 'unread)
8203         t))))
8204
8205 (defun gnus-summary-mark-article (&optional article mark no-expire)
8206   "Mark ARTICLE with MARK.  MARK can be any character.
8207 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8208 `??' (dormant) and `?E' (expirable).
8209 If MARK is nil, then the default character `?r' is used.
8210 If ARTICLE is nil, then the article on the current line will be
8211 marked."
8212   ;; The mark might be a string.
8213   (when (stringp mark)
8214     (setq mark (aref mark 0)))
8215   ;; If no mark is given, then we check auto-expiring.
8216   (when (null mark)
8217     (setq mark gnus-del-mark))
8218   (when (and (not no-expire)
8219              gnus-newsgroup-auto-expire
8220              (memq mark gnus-auto-expirable-marks))
8221     (setq mark gnus-expirable-mark))
8222   (let ((article (or article (gnus-summary-article-number)))
8223         (old-mark (gnus-summary-article-mark article)))
8224     ;; Allow the backend to change the mark.
8225     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8226     (if (eq mark old-mark)
8227         t
8228       (unless article
8229         (error "No article on current line"))
8230       (if (not (if (or (= mark gnus-unread-mark)
8231                        (= mark gnus-ticked-mark)
8232                        (= mark gnus-dormant-mark))
8233                    (gnus-mark-article-as-unread article mark)
8234                  (gnus-mark-article-as-read article mark)))
8235           t
8236         ;; See whether the article is to be put in the cache.
8237         (and gnus-use-cache
8238              (not (= mark gnus-canceled-mark))
8239              (vectorp (gnus-summary-article-header article))
8240              (save-excursion
8241                (gnus-cache-possibly-enter-article
8242                 gnus-newsgroup-name article
8243                 (gnus-summary-article-header article)
8244                 (= mark gnus-ticked-mark)
8245                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8246
8247         (when (gnus-summary-goto-subject article nil t)
8248           (let ((buffer-read-only nil))
8249             (gnus-summary-show-thread)
8250             ;; Fix the mark.
8251             (gnus-summary-update-mark mark 'unread)
8252             t))))))
8253
8254 (defun gnus-summary-update-secondary-mark (article)
8255   "Update the secondary (read, process, cache) mark."
8256   (gnus-summary-update-mark
8257    (cond ((memq article gnus-newsgroup-processable)
8258           gnus-process-mark)
8259          ((memq article gnus-newsgroup-cached)
8260           gnus-cached-mark)
8261          ((memq article gnus-newsgroup-replied)
8262           gnus-replied-mark)
8263          ((memq article gnus-newsgroup-saved)
8264           gnus-saved-mark)
8265          (t gnus-unread-mark))
8266    'replied)
8267   (when (gnus-visual-p 'summary-highlight 'highlight)
8268     (gnus-run-hooks 'gnus-summary-update-hook))
8269   t)
8270
8271 (defun gnus-summary-update-mark (mark type)
8272   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8273         (buffer-read-only nil))
8274     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8275     (when forward
8276       (when (looking-at "\r")
8277         (incf forward))
8278       (when (<= (+ forward (point)) (point-max))
8279         ;; Go to the right position on the line.
8280         (goto-char (+ forward (point)))
8281         ;; Replace the old mark with the new mark.
8282         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8283         ;; Optionally update the marks by some user rule.
8284         (when (eq type 'unread)
8285           (gnus-data-set-mark
8286            (gnus-data-find (gnus-summary-article-number)) mark)
8287           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8288
8289 (defun gnus-mark-article-as-read (article &optional mark)
8290   "Enter ARTICLE in the pertinent lists and remove it from others."
8291   ;; Make the article expirable.
8292   (let ((mark (or mark gnus-del-mark)))
8293     (if (= mark gnus-expirable-mark)
8294         (push article gnus-newsgroup-expirable)
8295       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8296     ;; Remove from unread and marked lists.
8297     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8298     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8299     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8300     (push (cons article mark) gnus-newsgroup-reads)
8301     ;; Possibly remove from cache, if that is used.
8302     (when gnus-use-cache
8303       (gnus-cache-enter-remove-article article))
8304     t))
8305
8306 (defun gnus-mark-article-as-unread (article &optional mark)
8307   "Enter ARTICLE in the pertinent lists and remove it from others."
8308   (let ((mark (or mark gnus-ticked-mark)))
8309     (if (<= article 0)
8310         (progn
8311           (gnus-error 1 "Can't mark negative article numbers")
8312           nil)
8313       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8314             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8315             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8316             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8317
8318       ;; Unsuppress duplicates?
8319       (when gnus-suppress-duplicates
8320         (gnus-dup-unsuppress-article article))
8321
8322       (cond ((= mark gnus-ticked-mark)
8323              (push article gnus-newsgroup-marked))
8324             ((= mark gnus-dormant-mark)
8325              (push article gnus-newsgroup-dormant))
8326             (t
8327              (push article gnus-newsgroup-unreads)))
8328       (gnus-pull article gnus-newsgroup-reads)
8329       t)))
8330
8331 (defalias 'gnus-summary-mark-as-unread-forward
8332   'gnus-summary-tick-article-forward)
8333 (make-obsolete 'gnus-summary-mark-as-unread-forward
8334                'gnus-summary-tick-article-forward)
8335 (defun gnus-summary-tick-article-forward (n)
8336   "Tick N articles forwards.
8337 If N is negative, tick backwards instead.
8338 The difference between N and the number of articles ticked is returned."
8339   (interactive "p")
8340   (gnus-summary-mark-forward n gnus-ticked-mark))
8341
8342 (defalias 'gnus-summary-mark-as-unread-backward
8343   'gnus-summary-tick-article-backward)
8344 (make-obsolete 'gnus-summary-mark-as-unread-backward
8345                'gnus-summary-tick-article-backward)
8346 (defun gnus-summary-tick-article-backward (n)
8347   "Tick N articles backwards.
8348 The difference between N and the number of articles ticked is returned."
8349   (interactive "p")
8350   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8351
8352 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8353 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8354 (defun gnus-summary-tick-article (&optional article clear-mark)
8355   "Mark current article as unread.
8356 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8357 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8358   (interactive)
8359   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8360                                        gnus-ticked-mark)))
8361
8362 (defun gnus-summary-mark-as-read-forward (n)
8363   "Mark N articles as read forwards.
8364 If N is negative, mark backwards instead.
8365 The difference between N and the actual number of articles marked is
8366 returned."
8367   (interactive "p")
8368   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8369
8370 (defun gnus-summary-mark-as-read-backward (n)
8371   "Mark the N articles as read backwards.
8372 The difference between N and the actual number of articles marked is
8373 returned."
8374   (interactive "p")
8375   (gnus-summary-mark-forward
8376    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8377
8378 (defun gnus-summary-mark-as-read (&optional article mark)
8379   "Mark current article as read.
8380 ARTICLE specifies the article to be marked as read.
8381 MARK specifies a string to be inserted at the beginning of the line."
8382   (gnus-summary-mark-article article mark))
8383
8384 (defun gnus-summary-clear-mark-forward (n)
8385   "Clear marks from N articles forward.
8386 If N is negative, clear backward instead.
8387 The difference between N and the number of marks cleared is returned."
8388   (interactive "p")
8389   (gnus-summary-mark-forward n gnus-unread-mark))
8390
8391 (defun gnus-summary-clear-mark-backward (n)
8392   "Clear marks from N articles backward.
8393 The difference between N and the number of marks cleared is returned."
8394   (interactive "p")
8395   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8396
8397 (defun gnus-summary-mark-unread-as-read ()
8398   "Intended to be used by `gnus-summary-mark-article-hook'."
8399   (when (memq gnus-current-article gnus-newsgroup-unreads)
8400     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8401
8402 (defun gnus-summary-mark-read-and-unread-as-read ()
8403   "Intended to be used by `gnus-summary-mark-article-hook'."
8404   (let ((mark (gnus-summary-article-mark)))
8405     (when (or (gnus-unread-mark-p mark)
8406               (gnus-read-mark-p mark))
8407       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8408
8409 (defun gnus-summary-mark-region-as-read (point mark all)
8410   "Mark all unread articles between point and mark as read.
8411 If given a prefix, mark all articles between point and mark as read,
8412 even ticked and dormant ones."
8413   (interactive "r\nP")
8414   (save-excursion
8415     (let (article)
8416       (goto-char point)
8417       (beginning-of-line)
8418       (while (and
8419               (< (point) mark)
8420               (progn
8421                 (when (or all
8422                           (memq (setq article (gnus-summary-article-number))
8423                                 gnus-newsgroup-unreads))
8424                   (gnus-summary-mark-article article gnus-del-mark))
8425                 t)
8426               (gnus-summary-find-next))))))
8427
8428 (defun gnus-summary-mark-below (score mark)
8429   "Mark articles with score less than SCORE with MARK."
8430   (interactive "P\ncMark: ")
8431   (setq score (if score
8432                   (prefix-numeric-value score)
8433                 (or gnus-summary-default-score 0)))
8434   (save-excursion
8435     (set-buffer gnus-summary-buffer)
8436     (goto-char (point-min))
8437     (while
8438         (progn
8439           (and (< (gnus-summary-article-score) score)
8440                (gnus-summary-mark-article nil mark))
8441           (gnus-summary-find-next)))))
8442
8443 (defun gnus-summary-kill-below (&optional score)
8444   "Mark articles with score below SCORE as read."
8445   (interactive "P")
8446   (gnus-summary-mark-below score gnus-killed-mark))
8447
8448 (defun gnus-summary-clear-above (&optional score)
8449   "Clear all marks from articles with score above SCORE."
8450   (interactive "P")
8451   (gnus-summary-mark-above score gnus-unread-mark))
8452
8453 (defun gnus-summary-tick-above (&optional score)
8454   "Tick all articles with score above SCORE."
8455   (interactive "P")
8456   (gnus-summary-mark-above score gnus-ticked-mark))
8457
8458 (defun gnus-summary-mark-above (score mark)
8459   "Mark articles with score over SCORE with MARK."
8460   (interactive "P\ncMark: ")
8461   (setq score (if score
8462                   (prefix-numeric-value score)
8463                 (or gnus-summary-default-score 0)))
8464   (save-excursion
8465     (set-buffer gnus-summary-buffer)
8466     (goto-char (point-min))
8467     (while (and (progn
8468                   (when (> (gnus-summary-article-score) score)
8469                     (gnus-summary-mark-article nil mark))
8470                   t)
8471                 (gnus-summary-find-next)))))
8472
8473 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8474 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8475 (defun gnus-summary-limit-include-expunged (&optional no-error)
8476   "Display all the hidden articles that were expunged for low scores."
8477   (interactive)
8478   (let ((buffer-read-only nil))
8479     (let ((scored gnus-newsgroup-scored)
8480           headers h)
8481       (while scored
8482         (unless (gnus-summary-goto-subject (caar scored))
8483           (and (setq h (gnus-summary-article-header (caar scored)))
8484                (< (cdar scored) gnus-summary-expunge-below)
8485                (push h headers)))
8486         (setq scored (cdr scored)))
8487       (if (not headers)
8488           (when (not no-error)
8489             (error "No expunged articles hidden"))
8490         (goto-char (point-min))
8491         (gnus-summary-prepare-unthreaded (nreverse headers))
8492         (goto-char (point-min))
8493         (gnus-summary-position-point)
8494         t))))
8495
8496 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8497   "Mark all unread articles in this newsgroup as read.
8498 If prefix argument ALL is non-nil, ticked and dormant articles will
8499 also be marked as read.
8500 If QUIETLY is non-nil, no questions will be asked.
8501 If TO-HERE is non-nil, it should be a point in the buffer.  All
8502 articles before this point will be marked as read.
8503 Note that this function will only catch up the unread article
8504 in the current summary buffer limitation.
8505 The number of articles marked as read is returned."
8506   (interactive "P")
8507   (prog1
8508       (save-excursion
8509         (when (or quietly
8510                   (not gnus-interactive-catchup) ;Without confirmation?
8511                   gnus-expert-user
8512                   (gnus-y-or-n-p
8513                    (if all
8514                        "Mark absolutely all articles as read? "
8515                      "Mark all unread articles as read? ")))
8516           (if (and not-mark
8517                    (not gnus-newsgroup-adaptive)
8518                    (not gnus-newsgroup-auto-expire)
8519                    (not gnus-suppress-duplicates)
8520                    (or (not gnus-use-cache)
8521                        (eq gnus-use-cache 'passive)))
8522               (progn
8523                 (when all
8524                   (setq gnus-newsgroup-marked nil
8525                         gnus-newsgroup-dormant nil))
8526                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8527             ;; We actually mark all articles as canceled, which we
8528             ;; have to do when using auto-expiry or adaptive scoring.
8529             (gnus-summary-show-all-threads)
8530             (when (gnus-summary-first-subject (not all) t)
8531               (while (and
8532                       (if to-here (< (point) to-here) t)
8533                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8534                       (gnus-summary-find-next (not all) nil nil t))))
8535             (gnus-set-mode-line 'summary))
8536           t))
8537     (gnus-summary-position-point)))
8538
8539 (defun gnus-summary-catchup-to-here (&optional all)
8540   "Mark all unticked articles before the current one as read.
8541 If ALL is non-nil, also mark ticked and dormant articles as read."
8542   (interactive "P")
8543   (save-excursion
8544     (gnus-save-hidden-threads
8545       (let ((beg (point)))
8546         ;; We check that there are unread articles.
8547         (when (or all (gnus-summary-find-prev))
8548           (gnus-summary-catchup all t beg)))))
8549   (gnus-summary-position-point))
8550
8551 (defun gnus-summary-catchup-all (&optional quietly)
8552   "Mark all articles in this newsgroup as read."
8553   (interactive "P")
8554   (gnus-summary-catchup t quietly))
8555
8556 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8557   "Mark all unread articles in this group as read, then exit.
8558 If prefix argument ALL is non-nil, all articles are marked as read."
8559   (interactive "P")
8560   (when (gnus-summary-catchup all quietly nil 'fast)
8561     ;; Select next newsgroup or exit.
8562     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8563              (eq gnus-auto-select-next 'quietly))
8564         (gnus-summary-next-group nil)
8565       (gnus-summary-exit))))
8566
8567 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8568   "Mark all articles in this newsgroup as read, and then exit."
8569   (interactive "P")
8570   (gnus-summary-catchup-and-exit t quietly))
8571
8572 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8573 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8574   "Mark all articles in this group as read and select the next group.
8575 If given a prefix, mark all articles, unread as well as ticked, as
8576 read."
8577   (interactive "P")
8578   (save-excursion
8579     (gnus-summary-catchup all))
8580   (gnus-summary-next-group t nil nil))
8581
8582 ;; Thread-based commands.
8583
8584 (defun gnus-summary-articles-in-thread (&optional article)
8585   "Return a list of all articles in the current thread.
8586 If ARTICLE is non-nil, return all articles in the thread that starts
8587 with that article."
8588   (let* ((article (or article (gnus-summary-article-number)))
8589          (data (gnus-data-find-list article))
8590          (top-level (gnus-data-level (car data)))
8591          (top-subject
8592           (cond ((null gnus-thread-operation-ignore-subject)
8593                  (gnus-simplify-subject-re
8594                   (mail-header-subject (gnus-data-header (car data)))))
8595                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8596                  (gnus-simplify-subject-fuzzy
8597                   (mail-header-subject (gnus-data-header (car data)))))
8598                 (t nil)))
8599          (end-point (save-excursion
8600                       (if (gnus-summary-go-to-next-thread)
8601                           (point) (point-max))))
8602          articles)
8603     (while (and data
8604                 (< (gnus-data-pos (car data)) end-point))
8605       (when (or (not top-subject)
8606                 (string= top-subject
8607                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8608                              (gnus-simplify-subject-fuzzy
8609                               (mail-header-subject
8610                                (gnus-data-header (car data))))
8611                            (gnus-simplify-subject-re
8612                             (mail-header-subject
8613                              (gnus-data-header (car data)))))))
8614         (push (gnus-data-number (car data)) articles))
8615       (unless (and (setq data (cdr data))
8616                    (> (gnus-data-level (car data)) top-level))
8617         (setq data nil)))
8618     ;; Return the list of articles.
8619     (nreverse articles)))
8620
8621 (defun gnus-summary-rethread-current ()
8622   "Rethread the thread the current article is part of."
8623   (interactive)
8624   (let* ((gnus-show-threads t)
8625          (article (gnus-summary-article-number))
8626          (id (mail-header-id (gnus-summary-article-header)))
8627          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8628     (unless id
8629       (error "No article on the current line"))
8630     (gnus-rebuild-thread id)
8631     (gnus-summary-goto-subject article)))
8632
8633 (defun gnus-summary-reparent-thread ()
8634   "Make the current article child of the marked (or previous) article.
8635
8636 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8637 is non-nil or the Subject: of both articles are the same."
8638   (interactive)
8639   (unless (not (gnus-group-read-only-p))
8640     (error "The current newsgroup does not support article editing"))
8641   (unless (<= (length gnus-newsgroup-processable) 1)
8642     (error "No more than one article may be marked"))
8643   (save-window-excursion
8644     (let ((gnus-article-buffer " *reparent*")
8645           (current-article (gnus-summary-article-number))
8646           ;; First grab the marked article, otherwise one line up.
8647           (parent-article (if (not (null gnus-newsgroup-processable))
8648                               (car gnus-newsgroup-processable)
8649                             (save-excursion
8650                               (if (eq (forward-line -1) 0)
8651                                   (gnus-summary-article-number)
8652                                 (error "Beginning of summary buffer"))))))
8653       (unless (not (eq current-article parent-article))
8654         (error "An article may not be self-referential"))
8655       (let ((message-id (mail-header-id
8656                          (gnus-summary-article-header parent-article))))
8657         (unless (and message-id (not (equal message-id "")))
8658           (error "No message-id in desired parent"))
8659         (gnus-with-article current-article
8660           (goto-char (point-min))
8661           (if (re-search-forward "^References: " nil t)
8662               (progn
8663                 (re-search-forward "^[^ \t]" nil t)
8664                 (forward-line -1)
8665                 (end-of-line)
8666                 (insert " " message-id))
8667             (insert "References: " message-id "\n")))
8668         (set-buffer gnus-summary-buffer)
8669         (gnus-summary-unmark-all-processable)
8670         (gnus-summary-update-article current-article)
8671         (gnus-summary-rethread-current)
8672         (gnus-message 3 "Article %d is now the child of article %d"
8673                       current-article parent-article)))))
8674
8675 (defun gnus-summary-toggle-threads (&optional arg)
8676   "Toggle showing conversation threads.
8677 If ARG is positive number, turn showing conversation threads on."
8678   (interactive "P")
8679   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8680     (setq gnus-show-threads
8681           (if (null arg) (not gnus-show-threads)
8682             (> (prefix-numeric-value arg) 0)))
8683     (gnus-summary-prepare)
8684     (gnus-summary-goto-subject current)
8685     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8686     (gnus-summary-position-point)))
8687
8688 (defun gnus-summary-show-all-threads ()
8689   "Show all threads."
8690   (interactive)
8691   (save-excursion
8692     (let ((buffer-read-only nil))
8693       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8694   (gnus-summary-position-point))
8695
8696 (defun gnus-summary-show-thread ()
8697   "Show thread subtrees.
8698 Returns nil if no thread was there to be shown."
8699   (interactive)
8700   (let ((buffer-read-only nil)
8701         (orig (point))
8702         ;; first goto end then to beg, to have point at beg after let
8703         (end (progn (end-of-line) (point)))
8704         (beg (progn (beginning-of-line) (point))))
8705     (prog1
8706         ;; Any hidden lines here?
8707         (search-forward "\r" end t)
8708       (subst-char-in-region beg end ?\^M ?\n t)
8709       (goto-char orig)
8710       (gnus-summary-position-point))))
8711
8712 (defun gnus-summary-hide-all-threads ()
8713   "Hide all thread subtrees."
8714   (interactive)
8715   (save-excursion
8716     (goto-char (point-min))
8717     (gnus-summary-hide-thread)
8718     (while (zerop (gnus-summary-next-thread 1 t))
8719       (gnus-summary-hide-thread)))
8720   (gnus-summary-position-point))
8721
8722 (defun gnus-summary-hide-thread ()
8723   "Hide thread subtrees.
8724 Returns nil if no threads were there to be hidden."
8725   (interactive)
8726   (let ((buffer-read-only nil)
8727         (start (point))
8728         (article (gnus-summary-article-number)))
8729     (goto-char start)
8730     ;; Go forward until either the buffer ends or the subthread
8731     ;; ends.
8732     (when (and (not (eobp))
8733                (or (zerop (gnus-summary-next-thread 1 t))
8734                    (goto-char (point-max))))
8735       (prog1
8736           (if (and (> (point) start)
8737                    (search-backward "\n" start t))
8738               (progn
8739                 (subst-char-in-region start (point) ?\n ?\^M)
8740                 (gnus-summary-goto-subject article))
8741             (goto-char start)
8742             nil)
8743         ;;(gnus-summary-position-point)
8744         ))))
8745
8746 (defun gnus-summary-go-to-next-thread (&optional previous)
8747   "Go to the same level (or less) next thread.
8748 If PREVIOUS is non-nil, go to previous thread instead.
8749 Return the article number moved to, or nil if moving was impossible."
8750   (let ((level (gnus-summary-thread-level))
8751         (way (if previous -1 1))
8752         (beg (point)))
8753     (forward-line way)
8754     (while (and (not (eobp))
8755                 (< level (gnus-summary-thread-level)))
8756       (forward-line way))
8757     (if (eobp)
8758         (progn
8759           (goto-char beg)
8760           nil)
8761       (setq beg (point))
8762       (prog1
8763           (gnus-summary-article-number)
8764         (goto-char beg)))))
8765
8766 (defun gnus-summary-next-thread (n &optional silent)
8767   "Go to the same level next N'th thread.
8768 If N is negative, search backward instead.
8769 Returns the difference between N and the number of skips actually
8770 done.
8771
8772 If SILENT, don't output messages."
8773   (interactive "p")
8774   (let ((backward (< n 0))
8775         (n (abs n)))
8776     (while (and (> n 0)
8777                 (gnus-summary-go-to-next-thread backward))
8778       (decf n))
8779     (unless silent
8780       (gnus-summary-position-point))
8781     (when (and (not silent) (/= 0 n))
8782       (gnus-message 7 "No more threads"))
8783     n))
8784
8785 (defun gnus-summary-prev-thread (n)
8786   "Go to the same level previous N'th thread.
8787 Returns the difference between N and the number of skips actually
8788 done."
8789   (interactive "p")
8790   (gnus-summary-next-thread (- n)))
8791
8792 (defun gnus-summary-go-down-thread ()
8793   "Go down one level in the current thread."
8794   (let ((children (gnus-summary-article-children)))
8795     (when children
8796       (gnus-summary-goto-subject (car children)))))
8797
8798 (defun gnus-summary-go-up-thread ()
8799   "Go up one level in the current thread."
8800   (let ((parent (gnus-summary-article-parent)))
8801     (when parent
8802       (gnus-summary-goto-subject parent))))
8803
8804 (defun gnus-summary-down-thread (n)
8805   "Go down thread N steps.
8806 If N is negative, go up instead.
8807 Returns the difference between N and how many steps down that were
8808 taken."
8809   (interactive "p")
8810   (let ((up (< n 0))
8811         (n (abs n)))
8812     (while (and (> n 0)
8813                 (if up (gnus-summary-go-up-thread)
8814                   (gnus-summary-go-down-thread)))
8815       (setq n (1- n)))
8816     (gnus-summary-position-point)
8817     (when (/= 0 n)
8818       (gnus-message 7 "Can't go further"))
8819     n))
8820
8821 (defun gnus-summary-up-thread (n)
8822   "Go up thread N steps.
8823 If N is negative, go up instead.
8824 Returns the difference between N and how many steps down that were
8825 taken."
8826   (interactive "p")
8827   (gnus-summary-down-thread (- n)))
8828
8829 (defun gnus-summary-top-thread ()
8830   "Go to the top of the thread."
8831   (interactive)
8832   (while (gnus-summary-go-up-thread))
8833   (gnus-summary-article-number))
8834
8835 (defun gnus-summary-kill-thread (&optional unmark)
8836   "Mark articles under current thread as read.
8837 If the prefix argument is positive, remove any kinds of marks.
8838 If the prefix argument is negative, tick articles instead."
8839   (interactive "P")
8840   (when unmark
8841     (setq unmark (prefix-numeric-value unmark)))
8842   (let ((articles (gnus-summary-articles-in-thread)))
8843     (save-excursion
8844       ;; Expand the thread.
8845       (gnus-summary-show-thread)
8846       ;; Mark all the articles.
8847       (while articles
8848         (gnus-summary-goto-subject (car articles))
8849         (cond ((null unmark)
8850                (gnus-summary-mark-article-as-read gnus-killed-mark))
8851               ((> unmark 0)
8852                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8853               (t
8854                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8855         (setq articles (cdr articles))))
8856     ;; Hide killed subtrees.
8857     (and (null unmark)
8858          gnus-thread-hide-killed
8859          (gnus-summary-hide-thread))
8860     ;; If marked as read, go to next unread subject.
8861     (when (null unmark)
8862       ;; Go to next unread subject.
8863       (gnus-summary-next-subject 1 t)))
8864   (gnus-set-mode-line 'summary))
8865
8866 ;; Summary sorting commands
8867
8868 (defun gnus-summary-sort-by-number (&optional reverse)
8869   "Sort the summary buffer by article number.
8870 Argument REVERSE means reverse order."
8871   (interactive "P")
8872   (gnus-summary-sort 'number reverse))
8873
8874 (defun gnus-summary-sort-by-author (&optional reverse)
8875   "Sort the summary buffer by author name alphabetically.
8876 If case-fold-search is non-nil, case of letters is ignored.
8877 Argument REVERSE means reverse order."
8878   (interactive "P")
8879   (gnus-summary-sort 'author reverse))
8880
8881 (defun gnus-summary-sort-by-subject (&optional reverse)
8882   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8883 If case-fold-search is non-nil, case of letters is ignored.
8884 Argument REVERSE means reverse order."
8885   (interactive "P")
8886   (gnus-summary-sort 'subject reverse))
8887
8888 (defun gnus-summary-sort-by-date (&optional reverse)
8889   "Sort the summary buffer by date.
8890 Argument REVERSE means reverse order."
8891   (interactive "P")
8892   (gnus-summary-sort 'date reverse))
8893
8894 (defun gnus-summary-sort-by-score (&optional reverse)
8895   "Sort the summary buffer by score.
8896 Argument REVERSE means reverse order."
8897   (interactive "P")
8898   (gnus-summary-sort 'score reverse))
8899
8900 (defun gnus-summary-sort-by-lines (&optional reverse)
8901   "Sort the summary buffer by the number of lines.
8902 Argument REVERSE means reverse order."
8903   (interactive "P")
8904   (gnus-summary-sort 'lines reverse))
8905
8906 (defun gnus-summary-sort-by-chars (&optional reverse)
8907   "Sort the summary buffer by article length.
8908 Argument REVERSE means reverse order."
8909   (interactive "P")
8910   (gnus-summary-sort 'chars reverse))   
8911
8912 (defun gnus-summary-sort (predicate reverse)
8913   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8914   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8915          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8916          (gnus-thread-sort-functions
8917           (if (not reverse)
8918               thread
8919             `(lambda (t1 t2)
8920                (,thread t2 t1))))
8921          (gnus-article-sort-functions
8922           (if (not reverse)
8923               article
8924             `(lambda (t1 t2)
8925                (,article t2 t1))))
8926          (buffer-read-only)
8927          (gnus-summary-prepare-hook nil))
8928     ;; We do the sorting by regenerating the threads.
8929     (gnus-summary-prepare)
8930     ;; Hide subthreads if needed.
8931     (when (and gnus-show-threads gnus-thread-hide-subtree)
8932       (gnus-summary-hide-all-threads))))
8933
8934 ;; Summary saving commands.
8935
8936 (defun gnus-summary-save-article (&optional n not-saved)
8937   "Save the current article using the default saver function.
8938 If N is a positive number, save the N next articles.
8939 If N is a negative number, save the N previous articles.
8940 If N is nil and any articles have been marked with the process mark,
8941 save those articles instead.
8942 The variable `gnus-default-article-saver' specifies the saver function."
8943   (interactive "P")
8944   (let* ((articles (gnus-summary-work-articles n))
8945          (save-buffer (save-excursion
8946                         (nnheader-set-temp-buffer " *Gnus Save*")))
8947          (num (length articles))
8948          header file)
8949     (dolist (article articles)
8950       (setq header (gnus-summary-article-header article))
8951       (if (not (vectorp header))
8952           ;; This is a pseudo-article.
8953           (if (assq 'name header)
8954               (gnus-copy-file (cdr (assq 'name header)))
8955             (gnus-message 1 "Article %d is unsaveable" article))
8956         ;; This is a real article.
8957         (save-window-excursion
8958           (gnus-summary-select-article t nil nil article))
8959         (save-excursion
8960           (set-buffer save-buffer)
8961           (erase-buffer)
8962           (insert-buffer-substring gnus-original-article-buffer))
8963         (setq file (gnus-article-save save-buffer file num))
8964         (gnus-summary-remove-process-mark article)
8965         (unless not-saved
8966           (gnus-summary-set-saved-mark article))))
8967     (gnus-kill-buffer save-buffer)
8968     (gnus-summary-position-point)
8969     (gnus-set-mode-line 'summary)
8970     n))
8971
8972 (defun gnus-summary-pipe-output (&optional arg)
8973   "Pipe the current article to a subprocess.
8974 If N is a positive number, pipe the N next articles.
8975 If N is a negative number, pipe the N previous articles.
8976 If N is nil and any articles have been marked with the process mark,
8977 pipe those articles instead."
8978   (interactive "P")
8979   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8980     (gnus-summary-save-article arg t))
8981   (gnus-configure-windows 'pipe))
8982
8983 (defun gnus-summary-save-article-mail (&optional arg)
8984   "Append the current article to an mail file.
8985 If N is a positive number, save the N next articles.
8986 If N is a negative number, save the N previous articles.
8987 If N is nil and any articles have been marked with the process mark,
8988 save those articles instead."
8989   (interactive "P")
8990   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8991     (gnus-summary-save-article arg)))
8992
8993 (defun gnus-summary-save-article-rmail (&optional arg)
8994   "Append the current article to an rmail file.
8995 If N is a positive number, save the N next articles.
8996 If N is a negative number, save the N previous articles.
8997 If N is nil and any articles have been marked with the process mark,
8998 save those articles instead."
8999   (interactive "P")
9000   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9001     (gnus-summary-save-article arg)))
9002
9003 (defun gnus-summary-save-article-file (&optional arg)
9004   "Append the current article to a file.
9005 If N is a positive number, save the N next articles.
9006 If N is a negative number, save the N previous articles.
9007 If N is nil and any articles have been marked with the process mark,
9008 save those articles instead."
9009   (interactive "P")
9010   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9011     (gnus-summary-save-article arg)))
9012
9013 (defun gnus-summary-write-article-file (&optional arg)
9014   "Write the current article to a file, deleting the previous file.
9015 If N is a positive number, save the N next articles.
9016 If N is a negative number, save the N previous articles.
9017 If N is nil and any articles have been marked with the process mark,
9018 save those articles instead."
9019   (interactive "P")
9020   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9021     (gnus-summary-save-article arg)))
9022
9023 (defun gnus-summary-save-article-body-file (&optional arg)
9024   "Append the current article body to a file.
9025 If N is a positive number, save the N next articles.
9026 If N is a negative number, save the N previous articles.
9027 If N is nil and any articles have been marked with the process mark,
9028 save those articles instead."
9029   (interactive "P")
9030   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9031     (gnus-summary-save-article arg)))
9032
9033 (defun gnus-summary-pipe-message (program)
9034   "Pipe the current article through PROGRAM."
9035   (interactive "sProgram: ")
9036   (gnus-summary-select-article)
9037   (let ((mail-header-separator ""))
9038     (gnus-eval-in-buffer-window gnus-article-buffer
9039       (save-restriction
9040         (widen)
9041         (let ((start (window-start))
9042               buffer-read-only)
9043           (message-pipe-buffer-body program)
9044           (set-window-start (get-buffer-window (current-buffer)) start))))))
9045
9046 (defun gnus-get-split-value (methods)
9047   "Return a value based on the split METHODS."
9048   (let (split-name method result match)
9049     (when methods
9050       (save-excursion
9051         (set-buffer gnus-original-article-buffer)
9052         (save-restriction
9053           (nnheader-narrow-to-headers)
9054           (while methods
9055             (goto-char (point-min))
9056             (setq method (pop methods))
9057             (setq match (car method))
9058             (when (cond
9059                    ((stringp match)
9060                     ;; Regular expression.
9061                     (ignore-errors
9062                       (re-search-forward match nil t)))
9063                    ((gnus-functionp match)
9064                     ;; Function.
9065                     (save-restriction
9066                       (widen)
9067                       (setq result (funcall match gnus-newsgroup-name))))
9068                    ((consp match)
9069                     ;; Form.
9070                     (save-restriction
9071                       (widen)
9072                       (setq result (eval match)))))
9073               (setq split-name (append (cdr method) split-name))
9074               (cond ((stringp result)
9075                      (push (expand-file-name
9076                             result gnus-article-save-directory)
9077                            split-name))
9078                     ((consp result)
9079                      (setq split-name (append result split-name)))))))))
9080     (nreverse split-name)))
9081
9082 (defun gnus-valid-move-group-p (group)
9083   (and (boundp group)
9084        (symbol-name group)
9085        (symbol-value group)
9086        (gnus-get-function (gnus-find-method-for-group
9087                            (symbol-name group)) 'request-accept-article t)))
9088
9089 (defun gnus-read-move-group-name (prompt default articles prefix)
9090   "Read a group name."
9091   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9092          (minibuffer-confirm-incomplete nil) ; XEmacs
9093          (prom
9094           (format "%s %s to:"
9095                   prompt
9096                   (if (> (length articles) 1)
9097                       (format "these %d articles" (length articles))
9098                     "this article")))
9099          (to-newsgroup
9100           (cond
9101            ((null split-name)
9102             (gnus-completing-read default prom
9103                                   gnus-active-hashtb
9104                                   'gnus-valid-move-group-p
9105                                   nil prefix
9106                                   'gnus-group-history))
9107            ((= 1 (length split-name))
9108             (gnus-completing-read (car split-name) prom
9109                                   gnus-active-hashtb
9110                                   'gnus-valid-move-group-p
9111                                   nil nil
9112                                   'gnus-group-history))
9113            (t
9114             (gnus-completing-read nil prom
9115                                   (mapcar (lambda (el) (list el))
9116                                           (nreverse split-name))
9117                                   nil nil nil
9118                                   'gnus-group-history)))))
9119     (when to-newsgroup
9120       (if (or (string= to-newsgroup "")
9121               (string= to-newsgroup prefix))
9122           (setq to-newsgroup default))
9123       (unless to-newsgroup
9124         (error "No group name entered"))
9125       (or (gnus-active to-newsgroup)
9126           (gnus-activate-group to-newsgroup)
9127           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9128                                      to-newsgroup))
9129               (or (and (gnus-request-create-group
9130                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
9131                        (gnus-activate-group to-newsgroup nil nil
9132                                             (gnus-group-name-to-method
9133                                              to-newsgroup)))
9134                   (error "Couldn't create group %s" to-newsgroup)))
9135           (error "No such group: %s" to-newsgroup)))
9136     to-newsgroup))
9137
9138 (defun gnus-summary-save-parts (type dir n reverse)
9139   "Save parts matching TYPE to DIR.
9140 If REVERSE, save parts that do not match TYPE."
9141   (interactive
9142    (list (read-string "Save parts of type: " "image/.*")
9143          (read-file-name "Save to directory: " t nil t)
9144          current-prefix-arg))
9145   (gnus-summary-iterate n
9146     (let ((gnus-display-mime-function nil)
9147           (gnus-inhibit-treatment t))
9148       (gnus-summary-select-article))
9149     (save-excursion
9150       (set-buffer gnus-article-buffer)
9151       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9152         (when handles
9153           (gnus-summary-save-parts-1 type dir handles reverse)
9154           (mm-destroy-parts handles))))))
9155
9156 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9157   (if (stringp (car handle))
9158       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9159               (cdr handle))
9160     (when (if reverse
9161               (not (string-match type (mm-handle-media-type handle)))
9162             (string-match type (mm-handle-media-type handle)))
9163       (let ((file (expand-file-name
9164                    (file-name-nondirectory
9165                     (or
9166                      (mail-content-type-get
9167                       (mm-handle-disposition handle) 'filename)
9168                      (concat gnus-newsgroup-name "." gnus-current-article)))
9169                    dir)))
9170         (unless (file-exists-p file)
9171           (mm-save-part-to-file handle file))))))
9172
9173 ;; Summary extract commands
9174
9175 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9176   (let ((buffer-read-only nil)
9177         (article (gnus-summary-article-number))
9178         after-article b e)
9179     (unless (gnus-summary-goto-subject article)
9180       (error "No such article: %d" article))
9181     (gnus-summary-position-point)
9182     ;; If all commands are to be bunched up on one line, we collect
9183     ;; them here.
9184     (unless gnus-view-pseudos-separately
9185       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9186             files action)
9187         (while ps
9188           (setq action (cdr (assq 'action (car ps))))
9189           (setq files (list (cdr (assq 'name (car ps)))))
9190           (while (and ps (cdr ps)
9191                       (string= (or action "1")
9192                                (or (cdr (assq 'action (cadr ps))) "2")))
9193             (push (cdr (assq 'name (cadr ps))) files)
9194             (setcdr ps (cddr ps)))
9195           (when files
9196             (when (not (string-match "%s" action))
9197               (push " " files))
9198             (push " " files)
9199             (when (assq 'execute (car ps))
9200               (setcdr (assq 'execute (car ps))
9201                       (funcall (if (string-match "%s" action)
9202                                    'format 'concat)
9203                                action
9204                                (mapconcat
9205                                 (lambda (f)
9206                                   (if (equal f " ")
9207                                       f
9208                                     (gnus-quote-arg-for-sh-or-csh f)))
9209                                 files " ")))))
9210           (setq ps (cdr ps)))))
9211     (if (and gnus-view-pseudos (not not-view))
9212         (while pslist
9213           (when (assq 'execute (car pslist))
9214             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9215                                   (eq gnus-view-pseudos 'not-confirm)))
9216           (setq pslist (cdr pslist)))
9217       (save-excursion
9218         (while pslist
9219           (setq after-article (or (cdr (assq 'article (car pslist)))
9220                                   (gnus-summary-article-number)))
9221           (gnus-summary-goto-subject after-article)
9222           (forward-line 1)
9223           (setq b (point))
9224           (insert "    " (file-name-nondirectory
9225                           (cdr (assq 'name (car pslist))))
9226                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9227           (setq e (point))
9228           (forward-line -1)             ; back to `b'
9229           (gnus-add-text-properties
9230            b (1- e) (list 'gnus-number gnus-reffed-article-number
9231                           gnus-mouse-face-prop gnus-mouse-face))
9232           (gnus-data-enter
9233            after-article gnus-reffed-article-number
9234            gnus-unread-mark b (car pslist) 0 (- e b))
9235           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9236           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9237           (setq pslist (cdr pslist)))))))
9238
9239 (defun gnus-pseudos< (p1 p2)
9240   (let ((c1 (cdr (assq 'action p1)))
9241         (c2 (cdr (assq 'action p2))))
9242     (and c1 c2 (string< c1 c2))))
9243
9244 (defun gnus-request-pseudo-article (props)
9245   (cond ((assq 'execute props)
9246          (gnus-execute-command (cdr (assq 'execute props)))))
9247   (let ((gnus-current-article (gnus-summary-article-number)))
9248     (gnus-run-hooks 'gnus-mark-article-hook)))
9249
9250 (defun gnus-execute-command (command &optional automatic)
9251   (save-excursion
9252     (gnus-article-setup-buffer)
9253     (set-buffer gnus-article-buffer)
9254     (setq buffer-read-only nil)
9255     (let ((command (if automatic command
9256                      (read-string "Command: " (cons command 0)))))
9257       (erase-buffer)
9258       (insert "$ " command "\n\n")
9259       (if gnus-view-pseudo-asynchronously
9260           (start-process "gnus-execute" (current-buffer) shell-file-name
9261                          shell-command-switch command)
9262         (call-process shell-file-name nil t nil
9263                       shell-command-switch command)))))
9264
9265 ;; Summary kill commands.
9266
9267 (defun gnus-summary-edit-global-kill (article)
9268   "Edit the \"global\" kill file."
9269   (interactive (list (gnus-summary-article-number)))
9270   (gnus-group-edit-global-kill article))
9271
9272 (defun gnus-summary-edit-local-kill ()
9273   "Edit a local kill file applied to the current newsgroup."
9274   (interactive)
9275   (setq gnus-current-headers (gnus-summary-article-header))
9276   (gnus-group-edit-local-kill
9277    (gnus-summary-article-number) gnus-newsgroup-name))
9278
9279 ;;; Header reading.
9280
9281 (defun gnus-read-header (id &optional header)
9282   "Read the headers of article ID and enter them into the Gnus system."
9283   (let ((group gnus-newsgroup-name)
9284         (gnus-override-method
9285          (and (gnus-news-group-p gnus-newsgroup-name)
9286               gnus-refer-article-method))
9287         where)
9288     ;; First we check to see whether the header in question is already
9289     ;; fetched.
9290     (if (stringp id)
9291         ;; This is a Message-ID.
9292         (setq header (or header (gnus-id-to-header id)))
9293       ;; This is an article number.
9294       (setq header (or header (gnus-summary-article-header id))))
9295     (if (and header
9296              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9297         ;; We have found the header.
9298         header
9299       ;; If this is a sparse article, we have to nix out its
9300       ;; previous entry in the thread hashtb.
9301       (when (and header
9302                  (gnus-summary-article-sparse-p (mail-header-number header)))
9303         (let* ((parent (gnus-parent-id (mail-header-references header)))
9304                (thread (and parent (gnus-id-to-thread parent))))
9305           (when thread
9306             (delq (assq header thread) thread))))
9307       ;; We have to really fetch the header to this article.
9308       (save-excursion
9309         (set-buffer nntp-server-buffer)
9310         (when (setq where (gnus-request-head id group))
9311           (nnheader-fold-continuation-lines)
9312           (goto-char (point-max))
9313           (insert ".\n")
9314           (goto-char (point-min))
9315           (insert "211 ")
9316           (princ (cond
9317                   ((numberp id) id)
9318                   ((cdr where) (cdr where))
9319                   (header (mail-header-number header))
9320                   (t gnus-reffed-article-number))
9321                  (current-buffer))
9322           (insert " Article retrieved.\n"))
9323         (if (or (not where)
9324                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9325             ()                          ; Malformed head.
9326           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9327             (when (and (stringp id)
9328                        (not (string= (gnus-group-real-name group)
9329                                      (car where))))
9330               ;; If we fetched by Message-ID and the article came
9331               ;; from a different group, we fudge some bogus article
9332               ;; numbers for this article.
9333               (mail-header-set-number header gnus-reffed-article-number))
9334             (save-excursion
9335               (set-buffer gnus-summary-buffer)
9336               (decf gnus-reffed-article-number)
9337               (gnus-remove-header (mail-header-number header))
9338               (push header gnus-newsgroup-headers)
9339               (setq gnus-current-headers header)
9340               (push (mail-header-number header) gnus-newsgroup-limit)))
9341           header)))))
9342
9343 (defun gnus-remove-header (number)
9344   "Remove header NUMBER from `gnus-newsgroup-headers'."
9345   (if (and gnus-newsgroup-headers
9346            (= number (mail-header-number (car gnus-newsgroup-headers))))
9347       (pop gnus-newsgroup-headers)
9348     (let ((headers gnus-newsgroup-headers))
9349       (while (and (cdr headers)
9350                   (not (= number (mail-header-number (cadr headers)))))
9351         (pop headers))
9352       (when (cdr headers)
9353         (setcdr headers (cddr headers))))))
9354
9355 ;;;
9356 ;;; summary highlights
9357 ;;;
9358
9359 (defun gnus-highlight-selected-summary ()
9360   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9361   ;; Highlight selected article in summary buffer
9362   (when gnus-summary-selected-face
9363     (save-excursion
9364       (let* ((beg (progn (beginning-of-line) (point)))
9365              (end (progn (end-of-line) (point)))
9366              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9367              (from (if (get-text-property beg gnus-mouse-face-prop)
9368                        beg
9369                      (or (next-single-property-change
9370                           beg gnus-mouse-face-prop nil end)
9371                          beg)))
9372              (to
9373               (if (= from end)
9374                   (- from 2)
9375                 (or (next-single-property-change
9376                      from gnus-mouse-face-prop nil end)
9377                     end))))
9378         ;; If no mouse-face prop on line we will have to = from = end,
9379         ;; so we highlight the entire line instead.
9380         (when (= (+ to 2) from)
9381           (setq from beg)
9382           (setq to end))
9383         (if gnus-newsgroup-selected-overlay
9384             ;; Move old overlay.
9385             (gnus-move-overlay
9386              gnus-newsgroup-selected-overlay from to (current-buffer))
9387           ;; Create new overlay.
9388           (gnus-overlay-put
9389            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9390            'face gnus-summary-selected-face))))))
9391
9392 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9393 (defun gnus-summary-highlight-line ()
9394   "Highlight current line according to `gnus-summary-highlight'."
9395   (let* ((list gnus-summary-highlight)
9396          (p (point))
9397          (end (progn (end-of-line) (point)))
9398          ;; now find out where the line starts and leave point there.
9399          (beg (progn (beginning-of-line) (point)))
9400          (article (gnus-summary-article-number))
9401          (score (or (cdr (assq (or article gnus-current-article)
9402                                gnus-newsgroup-scored))
9403                     gnus-summary-default-score 0))
9404          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9405          (inhibit-read-only t))
9406     ;; Eval the cars of the lists until we find a match.
9407     (let ((default gnus-summary-default-score))
9408       (while (and list
9409                   (not (eval (caar list))))
9410         (setq list (cdr list))))
9411     (let ((face (cdar list)))
9412       (unless (eq face (get-text-property beg 'face))
9413         (gnus-put-text-property-excluding-characters-with-faces
9414          beg end 'face
9415          (setq face (if (boundp face) (symbol-value face) face)))
9416         (when gnus-summary-highlight-line-function
9417           (funcall gnus-summary-highlight-line-function article face))))
9418     (goto-char p)))
9419
9420 (defun gnus-update-read-articles (group unread &optional compute)
9421   "Update the list of read articles in GROUP."
9422   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9423          (entry (gnus-gethash group gnus-newsrc-hashtb))
9424          (info (nth 2 entry))
9425          (prev 1)
9426          (unread (sort (copy-sequence unread) '<))
9427          read)
9428     (if (or (not info) (not active))
9429         ;; There is no info on this group if it was, in fact,
9430         ;; killed.  Gnus stores no information on killed groups, so
9431         ;; there's nothing to be done.
9432         ;; One could store the information somewhere temporarily,
9433         ;; perhaps...  Hmmm...
9434         ()
9435       ;; Remove any negative articles numbers.
9436       (while (and unread (< (car unread) 0))
9437         (setq unread (cdr unread)))
9438       ;; Remove any expired article numbers
9439       (while (and unread (< (car unread) (car active)))
9440         (setq unread (cdr unread)))
9441       ;; Compute the ranges of read articles by looking at the list of
9442       ;; unread articles.
9443       (while unread
9444         (when (/= (car unread) prev)
9445           (push (if (= prev (1- (car unread))) prev
9446                   (cons prev (1- (car unread))))
9447                 read))
9448         (setq prev (1+ (car unread)))
9449         (setq unread (cdr unread)))
9450       (when (<= prev (cdr active))
9451         (push (cons prev (cdr active)) read))
9452       (setq read (if (> (length read) 1) (nreverse read) read))
9453       (if compute
9454           read
9455         (save-excursion
9456           (set-buffer gnus-group-buffer)
9457           (gnus-undo-register
9458             `(progn
9459                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9460                (gnus-info-set-read ',info ',(gnus-info-read info))
9461                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9462                (gnus-group-update-group ,group t))))
9463        ;; Propagate the read marks to the backend.
9464        (if (gnus-check-backend-function 'request-set-mark group)
9465            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9466                  (add (gnus-remove-from-range read (gnus-info-read info))))
9467              (when (or add del)
9468                (unless (gnus-check-group group)
9469                  (error "Can't open server for %s" group))
9470                (gnus-request-set-mark
9471                 group (delq nil (list (if add (list add 'add '(read)))
9472                                       (if del (list del 'del '(read)))))))))
9473         ;; Enter this list into the group info.
9474         (gnus-info-set-read info read)
9475         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9476         (gnus-get-unread-articles-in-group info (gnus-active group))
9477         t))))
9478
9479 (defun gnus-offer-save-summaries ()
9480   "Offer to save all active summary buffers."
9481   (save-excursion
9482     (let ((buflist (buffer-list))
9483           buffers bufname)
9484       ;; Go through all buffers and find all summaries.
9485       (while buflist
9486         (and (setq bufname (buffer-name (car buflist)))
9487              (string-match "Summary" bufname)
9488              (save-excursion
9489                (set-buffer bufname)
9490                ;; We check that this is, indeed, a summary buffer.
9491                (and (eq major-mode 'gnus-summary-mode)
9492                     ;; Also make sure this isn't bogus.
9493                     gnus-newsgroup-prepared
9494                     ;; Also make sure that this isn't a dead summary buffer.
9495                     (not gnus-dead-summary-mode)))
9496              (push bufname buffers))
9497         (setq buflist (cdr buflist)))
9498       ;; Go through all these summary buffers and offer to save them.
9499       (when buffers
9500         (map-y-or-n-p
9501          "Update summary buffer %s? "
9502          (lambda (buf)
9503            (switch-to-buffer buf)
9504            (gnus-summary-exit))
9505          buffers)))))
9506
9507
9508 ;;; @ for mime-partial
9509 ;;;
9510
9511 (defun gnus-request-partial-message ()
9512   (save-excursion
9513     (let ((number (gnus-summary-article-number))
9514           (group gnus-newsgroup-name)
9515           (mother gnus-article-buffer))
9516       (set-buffer (get-buffer-create " *Partial Article*"))
9517       (erase-buffer)
9518       (setq mime-preview-buffer mother)
9519       (gnus-request-article-this-buffer number group)
9520       (mime-parse-buffer)
9521       )))
9522
9523 (autoload 'mime-combine-message/partial-pieces-automatically
9524   "mime-partial"
9525   "Internal method to combine message/partial messages automatically.")
9526
9527 (mime-add-condition
9528  'action '((type . message)(subtype . partial)
9529            (major-mode . gnus-original-article-mode)
9530            (method . mime-combine-message/partial-pieces-automatically)
9531            (summary-buffer-exp . gnus-summary-buffer)
9532            (request-partial-message-method . gnus-request-partial-message)
9533            ))
9534
9535
9536 ;;; @ for message/rfc822
9537 ;;;
9538
9539 (defun gnus-mime-extract-message/rfc822 (entity situation)
9540   (let (group article num cwin swin cur)
9541     (with-current-buffer (mime-entity-buffer entity)
9542       (save-restriction
9543         (narrow-to-region (mime-entity-body-start entity)
9544                           (mime-entity-body-end entity))
9545         (setq group (or (cdr (assq 'group situation))
9546                         (completing-read "Group: "
9547                                          gnus-active-hashtb
9548                                          nil
9549                                          (gnus-read-active-file-p)
9550                                          gnus-newsgroup-name))
9551               article (gnus-request-accept-article group)
9552               )
9553         ))
9554     (when (and (consp article)
9555                (numberp (setq article (cdr article))))
9556       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9557             cwin (get-buffer-window (current-buffer) t)
9558             )
9559       (save-window-excursion
9560         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9561             (select-window swin)
9562           (set-buffer gnus-summary-buffer)
9563           )
9564         (setq cur gnus-current-article)
9565         (forward-line num)
9566         (let (gnus-show-threads)
9567           (gnus-summary-goto-subject article t)
9568           )
9569         (gnus-summary-clear-mark-forward 1)
9570         (gnus-summary-goto-subject cur)
9571         )
9572       (when (and cwin (window-frame cwin))
9573         (select-frame (window-frame cwin))
9574         )
9575       (when (boundp 'mime-acting-situation-to-override)
9576         (set-alist 'mime-acting-situation-to-override
9577                    'group
9578                    group)
9579         (set-alist 'mime-acting-situation-to-override
9580                    'after-method
9581                    `(progn
9582                       (save-current-buffer
9583                         (set-buffer gnus-group-buffer)
9584                         (gnus-activate-group ,group)
9585                         )
9586                       (gnus-summary-goto-article ,cur
9587                                                  gnus-show-all-headers)
9588                       ))
9589         (set-alist 'mime-acting-situation-to-override
9590                    'number num)
9591         )
9592       )))
9593
9594 (mime-add-condition
9595  'action '((type . message)(subtype . rfc822)
9596            (major-mode . gnus-original-article-mode)
9597            (method . gnus-mime-extract-message/rfc822)
9598            (mode . "extract")
9599            ))
9600
9601 (mime-add-condition
9602  'action '((type . message)(subtype . news)
9603            (major-mode . gnus-original-article-mode)
9604            (method . gnus-mime-extract-message/rfc822)
9605            (mode . "extract")
9606            ))
9607
9608 (defun gnus-mime-extract-multipart (entity situation)
9609   (let ((children (mime-entity-children entity))
9610         mime-acting-situation-to-override
9611         f)
9612     (while children
9613       (mime-play-entity (car children)
9614                         (cons (assq 'mode situation)
9615                               mime-acting-situation-to-override))
9616       (setq children (cdr children)))
9617     (if (setq f (cdr (assq 'after-method
9618                            mime-acting-situation-to-override)))
9619         (eval f)
9620       )))  
9621
9622 (mime-add-condition
9623  'action '((type . multipart)
9624            (method . gnus-mime-extract-multipart)
9625            (mode . "extract")
9626            )
9627  'with-default)
9628
9629
9630 ;;; @ end
9631 ;;;
9632
9633 (defun gnus-summary-setup-default-charset ()
9634   "Setup newsgroup default charset."
9635   (if (equal gnus-newsgroup-name "nndraft:drafts")
9636       (setq gnus-newsgroup-charset nil)
9637   (let* ((name (and gnus-newsgroup-name
9638                    (gnus-group-real-name gnus-newsgroup-name)))
9639          (ignored-charsets 
9640           (or gnus-newsgroup-ephemeral-ignored-charsets
9641               (append
9642                (and gnus-newsgroup-name
9643                     (or (gnus-group-find-parameter gnus-newsgroup-name
9644                                                    'ignored-charsets t)
9645                         (let ((alist gnus-group-ignored-charsets-alist)
9646                            elem (charsets nil))
9647                           (while (setq elem (pop alist))
9648                             (when (and name
9649                                        (string-match (car elem) name))
9650                               (setq alist nil
9651                                     charsets (cdr elem))))
9652                           charsets))))
9653               gnus-newsgroup-ignored-charsets)))
9654     (setq gnus-newsgroup-charset
9655           (or gnus-newsgroup-ephemeral-charset
9656               (and gnus-newsgroup-name
9657                    (or (gnus-group-find-parameter gnus-newsgroup-name
9658                                                   'charset)
9659                        (let ((alist gnus-group-charset-alist)
9660                              elem (charset nil))
9661                          (while (setq elem (pop alist))
9662                            (when (and name
9663                                       (string-match (car elem) name))
9664                              (setq alist nil
9665                                    charset (cadr elem))))
9666                          charset)))
9667               gnus-default-charset))
9668     (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9669          ignored-charsets))))
9670
9671 ;;;
9672 ;;; Mime Commands
9673 ;;;
9674
9675 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9676   "Display the current article buffer fully MIME-buttonized.
9677 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9678 treated as multipart/mixed."
9679   (interactive "P")
9680   (require 'gnus-art)
9681   (let ((gnus-unbuttonized-mime-types nil)
9682         (gnus-mime-display-multipart-as-mixed show-all-parts))
9683     (gnus-summary-show-article)))
9684
9685 (defun gnus-summary-repair-multipart (article)
9686   "Add a Content-Type header to a multipart article without one."
9687   (interactive (list (gnus-summary-article-number)))
9688   (gnus-with-article article
9689     (message-narrow-to-head)
9690     (goto-char (point-max))
9691     (widen)
9692     (when (search-forward "\n--" nil t)
9693       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9694         (message-narrow-to-head)
9695         (message-remove-header "Mime-Version")
9696         (message-remove-header "Content-Type")
9697         (goto-char (point-max))
9698         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9699                         separator))
9700         (insert "Mime-Version: 1.0\n")
9701         (widen))))
9702   (let (gnus-mark-article-hook)
9703     (gnus-summary-select-article t t nil article)))
9704
9705 (defun gnus-summary-toggle-display-buttonized ()
9706   "Toggle the buttonizing of the article buffer."
9707   (interactive)
9708   (require 'gnus-art)
9709   (if (setq gnus-inhibit-mime-unbuttonizing
9710             (not gnus-inhibit-mime-unbuttonizing))
9711       (let ((gnus-unbuttonized-mime-types nil))
9712         (gnus-summary-show-article))
9713     (gnus-summary-show-article)))
9714
9715 ;;;
9716 ;;; Intelli-mouse commmands
9717 ;;;
9718
9719 (defun gnus-wheel-summary-scroll (event)
9720   (interactive "e")
9721   (let ((amount (if (memq 'shift (event-modifiers event))
9722                     (car gnus-wheel-scroll-amount)
9723                   (cdr gnus-wheel-scroll-amount)))
9724         (direction (- (* (static-if (featurep 'xemacs)
9725                              (event-button event)
9726                            (cond ((eq 'mouse-4 (event-basic-type event))
9727                                   4)
9728                                  ((eq 'mouse-5 (event-basic-type event))
9729                                   5)))
9730                          2) 9))
9731         edge)
9732     (gnus-summary-scroll-up (* amount direction))
9733     (when (gnus-eval-in-buffer-window gnus-article-buffer
9734             (save-restriction
9735               (widen)
9736               (and (if (< 0 direction)
9737                        (gnus-article-next-page 0)
9738                      (gnus-article-prev-page 0)
9739                      (bobp))
9740                    (if (setq edge (get-text-property
9741                                    (point-min) 'gnus-wheel-edge))
9742                        (setq edge (* edge direction))
9743                      (setq edge -1))
9744                    (or (plusp edge)
9745                        (let ((buffer-read-only nil)
9746                              (inhibit-read-only t))
9747                          (put-text-property (point-min) (point-max)
9748                                             'gnus-wheel-edge direction)
9749                          nil))
9750                    (or (> edge gnus-wheel-edge-resistance)
9751                        (let ((buffer-read-only nil)
9752                              (inhibit-read-only t))
9753                          (put-text-property (point-min) (point-max)
9754                                             'gnus-wheel-edge
9755                                             (* (1+ edge) direction))
9756                          nil))
9757                    (eq last-command 'gnus-wheel-summary-scroll))
9758               ))
9759       (gnus-summary-next-article nil nil (minusp direction)))
9760     ))
9761
9762 (defun gnus-wheel-install ()
9763   "Enable mouse wheel support on summary window."
9764   (when gnus-use-wheel
9765     (let ((keys 
9766            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
9767       (dolist (key keys)
9768         (define-key gnus-summary-mode-map key
9769           'gnus-wheel-summary-scroll)))))
9770
9771 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
9772
9773 ;;;
9774 ;;; with article
9775 ;;;
9776
9777 (defmacro gnus-with-article (article &rest forms)
9778   "Select ARTICLE and perform FORMS in the original article buffer.
9779 Then replace the article with the result."
9780   `(progn
9781      ;; We don't want the article to be marked as read.
9782      (let (gnus-mark-article-hook)
9783        (gnus-summary-select-article t t nil ,article))
9784      (set-buffer gnus-original-article-buffer)
9785      ,@forms
9786      (if (not (gnus-check-backend-function
9787                'request-replace-article (car gnus-article-current)))
9788          (gnus-message 5 "Read-only group; not replacing")
9789        (unless (gnus-request-replace-article
9790                 ,article (car gnus-article-current)
9791                 (current-buffer) t)
9792          (error "Couldn't replace article")))
9793      ;; The cache and backlog have to be flushed somewhat.
9794      (when gnus-keep-backlog
9795        (gnus-backlog-remove-article
9796         (car gnus-article-current) (cdr gnus-article-current)))
9797      (when gnus-use-cache
9798        (gnus-cache-update-article
9799         (car gnus-article-current) (cdr gnus-article-current)))))
9800
9801 (put 'gnus-with-article 'lisp-indent-function 1)
9802 (put 'gnus-with-article 'edebug-form-spec '(form body))
9803
9804 ;;;
9805 ;;; Generic summary marking commands
9806 ;;;
9807
9808 (defvar gnus-summary-marking-alist
9809   '((read gnus-del-mark "d")
9810     (unread gnus-unread-mark "u")
9811     (ticked gnus-ticked-mark "!")
9812     (dormant gnus-dormant-mark "?")
9813     (expirable gnus-expirable-mark "e"))
9814   "An alist of names/marks/keystrokes.")
9815
9816 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9817 (defvar gnus-summary-mark-map)
9818
9819 (defun gnus-summary-make-all-marking-commands ()
9820   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9821   (dolist (elem gnus-summary-marking-alist)
9822     (apply 'gnus-summary-make-marking-command elem)))
9823
9824 (defun gnus-summary-make-marking-command (name mark keystroke)
9825   (let ((map (make-sparse-keymap)))
9826     (define-key gnus-summary-generic-mark-map keystroke map)
9827     (dolist (lway `((next "next" next nil "n")
9828                     (next-unread "next unread" next t "N")
9829                     (prev "previous" prev nil "p")
9830                     (prev-unread "previous unread" prev t "P")
9831                     (nomove "" nil nil ,keystroke)))
9832       (let ((func (gnus-summary-make-marking-command-1
9833                    mark (car lway) lway name)))
9834         (setq func (eval func))
9835         (define-key map (nth 4 lway) func)))))
9836       
9837 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9838   `(defun ,(intern
9839             (format "gnus-summary-put-mark-as-%s%s"
9840                     name (if (eq way 'nomove)
9841                              ""
9842                            (concat "-" (symbol-name way)))))
9843      (n)
9844      ,(format
9845        "Mark the current article as %s%s.
9846 If N, the prefix, then repeat N times.
9847 If N is negative, move in reverse order.
9848 The difference between N and the actual number of articles marked is
9849 returned."
9850        name (cadr lway))
9851      (interactive "p")
9852      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9853     
9854 (defun gnus-summary-generic-mark (n mark move unread)
9855   "Mark N articles with MARK."
9856   (unless (eq major-mode 'gnus-summary-mode)
9857     (error "This command can only be used in the summary buffer"))
9858   (gnus-summary-show-thread)
9859   (let ((nummove
9860          (cond
9861           ((eq move 'next) 1)
9862           ((eq move 'prev) -1)
9863           (t 0))))
9864     (if (zerop nummove)
9865         (setq n 1)
9866       (when (< n 0)
9867         (setq n (abs n)
9868               nummove (* -1 nummove))))
9869     (while (and (> n 0)
9870                 (gnus-summary-mark-article nil mark)
9871                 (zerop (gnus-summary-next-subject nummove unread t)))
9872       (setq n (1- n)))
9873     (when (/= 0 n)
9874       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9875     (gnus-summary-recenter)
9876     (gnus-summary-position-point)
9877     (gnus-set-mode-line 'summary)
9878     n))
9879
9880 (gnus-summary-make-all-marking-commands)
9881
9882 (gnus-ems-redefine)
9883
9884 (provide 'gnus-sum)
9885
9886 (run-hooks 'gnus-sum-load-hook)
9887
9888 ;;; gnus-sum.el ends here