* lisp/gnus.el (gnus-version-number): Update to 6.10.024.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996,97,98 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: mail, news, MIME
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mime-view)
39
40 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
41 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
42
43 (defcustom gnus-kill-summary-on-exit t
44   "*If non-nil, kill the summary buffer when you exit from it.
45 If nil, the summary will become a \"*Dead Summary*\" buffer, and
46 it will be killed sometime later."
47   :group 'gnus-summary-exit
48   :type 'boolean)
49
50 (defcustom gnus-fetch-old-headers nil
51   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
52 If an unread article in the group refers to an older, already read (or
53 just marked as read) article, the old article will not normally be
54 displayed in the Summary buffer.  If this variable is non-nil, Gnus
55 will attempt to grab the headers to the old articles, and thereby
56 build complete threads.  If it has the value `some', only enough
57 headers to connect otherwise loose threads will be displayed.  This
58 variable can also be a number.  In that case, no more than that number
59 of old headers will be fetched.  If it has the value `invisible', all
60 old headers will be fetched, but none will be displayed.
61
62 The server has to support NOV for any of this to work."
63   :group 'gnus-thread
64   :type '(choice (const :tag "off" nil)
65                  (const some)
66                  number
67                  (sexp :menu-tag "other" t)))
68
69 (defcustom gnus-refer-thread-limit 200
70   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
71 If t, fetch all the available old headers."
72   :group 'gnus-thread
73   :type '(choice number
74                  (sexp :menu-tag "other" t)))
75
76 (defcustom gnus-summary-make-false-root 'adopt
77   "*nil means that Gnus won't gather loose threads.
78 If the root of a thread has expired or been read in a previous
79 session, the information necessary to build a complete thread has been
80 lost.  Instead of having many small sub-threads from this original thread
81 scattered all over the summary buffer, Gnus can gather them.
82
83 If non-nil, Gnus will try to gather all loose sub-threads from an
84 original thread into one large thread.
85
86 If this variable is non-nil, it should be one of `none', `adopt',
87 `dummy' or `empty'.
88
89 If this variable is `none', Gnus will not make a false root, but just
90 present the sub-threads after another.
91 If this variable is `dummy', Gnus will create a dummy root that will
92 have all the sub-threads as children.
93 If this variable is `adopt', Gnus will make one of the \"children\"
94 the parent and mark all the step-children as such.
95 If this variable is `empty', the \"children\" are printed with empty
96 subject fields.  (Or rather, they will be printed with a string
97 given by the `gnus-summary-same-subject' variable.)"
98   :group 'gnus-thread
99   :type '(choice (const :tag "off" nil)
100                  (const none)
101                  (const dummy)
102                  (const adopt)
103                  (const empty)))
104
105 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
106   "*A regexp to match subjects to be excluded from loose thread gathering.
107 As loose thread gathering is done on subjects only, that means that
108 there can be many false gatherings performed.  By rooting out certain
109 common subjects, gathering might become saner."
110   :group 'gnus-thread
111   :type 'regexp)
112
113 (defcustom gnus-summary-gather-subject-limit nil
114   "*Maximum length of subject comparisons when gathering loose threads.
115 Use nil to compare full subjects.  Setting this variable to a low
116 number will help gather threads that have been corrupted by
117 newsreaders chopping off subject lines, but it might also mean that
118 unrelated articles that have subject that happen to begin with the
119 same few characters will be incorrectly gathered.
120
121 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
122 comparing subjects."
123   :group 'gnus-thread
124   :type '(choice (const :tag "off" nil)
125                  (const fuzzy)
126                  (sexp :menu-tag "on" t)))
127
128 (defcustom gnus-simplify-subject-functions nil
129   "List of functions taking a string argument that simplify subjects.
130 The functions are applied recursively.
131
132 Useful functions to put in this list include: `gnus-simplify-subject-re',
133 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
134   :group 'gnus-thread
135   :type '(repeat function))
136
137 (defcustom gnus-simplify-ignored-prefixes nil
138   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
139   :group 'gnus-thread
140   :type '(choice (const :tag "off" nil)
141                  regexp))
142
143 (defcustom gnus-build-sparse-threads nil
144   "*If non-nil, fill in the gaps in threads.
145 If `some', only fill in the gaps that are needed to tie loose threads
146 together.  If `more', fill in all leaf nodes that Gnus can find.  If
147 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
148   :group 'gnus-thread
149   :type '(choice (const :tag "off" nil)
150                  (const some)
151                  (const more)
152                  (sexp :menu-tag "all" t)))
153
154 (defcustom gnus-summary-thread-gathering-function
155   'gnus-gather-threads-by-subject
156   "*Function used for gathering loose threads.
157 There are two pre-defined functions: `gnus-gather-threads-by-subject',
158 which only takes Subjects into consideration; and
159 `gnus-gather-threads-by-references', which compared the References
160 headers of the articles to find matches."
161   :group 'gnus-thread
162   :type '(radio (function-item gnus-gather-threads-by-subject)
163                 (function-item gnus-gather-threads-by-references)
164                 (function :tag "other")))
165
166 (defcustom gnus-summary-same-subject ""
167   "*String indicating that the current article has the same subject as the previous.
168 This variable will only be used if the value of
169 `gnus-summary-make-false-root' is `empty'."
170   :group 'gnus-summary-format
171   :type 'string)
172
173 (defcustom gnus-summary-goto-unread t
174   "*If t, marking commands will go to the next unread article.
175 If `never', commands that usually go to the next unread article, will
176 go to the next article, whether it is read or not.
177 If nil, only the marking commands will go to the next (un)read article."
178   :group 'gnus-summary-marks
179   :link '(custom-manual "(gnus)Setting Marks")
180   :type '(choice (const :tag "off" nil)
181                  (const never)
182                  (sexp :menu-tag "on" t)))
183
184 (defcustom gnus-summary-default-score 0
185   "*Default article score level.
186 All scores generated by the score files will be added to this score.
187 If this variable is nil, scoring will be disabled."
188   :group 'gnus-score-default
189   :type '(choice (const :tag "disable")
190                  integer))
191
192 (defcustom gnus-summary-zcore-fuzz 0
193   "*Fuzziness factor for the zcore in the summary buffer.
194 Articles with scores closer than this to `gnus-summary-default-score'
195 will not be marked."
196   :group 'gnus-summary-format
197   :type 'integer)
198
199 (defcustom gnus-simplify-subject-fuzzy-regexp nil
200   "*Strings to be removed when doing fuzzy matches.
201 This can either be a regular expression or list of regular expressions
202 that will be removed from subject strings if fuzzy subject
203 simplification is selected."
204   :group 'gnus-thread
205   :type '(repeat regexp))
206
207 (defcustom gnus-show-threads t
208   "*If non-nil, display threads in summary mode."
209   :group 'gnus-thread
210   :type 'boolean)
211
212 (defcustom gnus-thread-hide-subtree nil
213   "*If non-nil, hide all threads initially.
214 If threads are hidden, you have to run the command
215 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
216 to expose hidden threads."
217   :group 'gnus-thread
218   :type 'boolean)
219
220 (defcustom gnus-thread-hide-killed t
221   "*If non-nil, hide killed threads automatically."
222   :group 'gnus-thread
223   :type 'boolean)
224
225 (defcustom gnus-thread-ignore-subject t
226   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
227 If nil, articles that have different subjects from their parents will
228 start separate threads."
229   :group 'gnus-thread
230   :type 'boolean)
231
232 (defcustom gnus-thread-operation-ignore-subject t
233   "*If non-nil, subjects will be ignored when doing thread commands.
234 This affects commands like `gnus-summary-kill-thread' and
235 `gnus-summary-lower-thread'.
236
237 If this variable is nil, articles in the same thread with different
238 subjects will not be included in the operation in question.  If this
239 variable is `fuzzy', only articles that have subjects that are fuzzily
240 equal will be included."
241   :group 'gnus-thread
242   :type '(choice (const :tag "off" nil)
243                  (const fuzzy)
244                  (sexp :tag "on" t)))
245
246 (defcustom gnus-thread-indent-level 4
247   "*Number that says how much each sub-thread should be indented."
248   :group 'gnus-thread
249   :type 'integer)
250
251 (defcustom gnus-auto-extend-newsgroup t
252   "*If non-nil, extend newsgroup forward and backward when requested."
253   :group 'gnus-summary-choose
254   :type 'boolean)
255
256 (defcustom gnus-auto-select-first t
257   "*If nil, don't select the first unread article when entering a group.
258 If this variable is `best', select the highest-scored unread article
259 in the group.  If t, select the first unread article.
260
261 This variable can also be a function to place point on a likely
262 subject line.  Useful values include `gnus-summary-first-unread-subject', 
263 `gnus-summary-first-unread-article' and 
264 `gnus-summary-best-unread-article'.
265
266 If you want to prevent automatic selection of the first unread article
267 in some newsgroups, set the variable to nil in
268 `gnus-select-group-hook'."
269   :group 'gnus-group-select
270   :type '(choice (const :tag "none" nil)
271                  (const best)
272                  (sexp :menu-tag "first" t)
273                  (function-item gnus-summary-first-unread-subject)
274                  (function-item gnus-summary-first-unread-article)
275                  (function-item gnus-summary-best-unread-article)))
276
277 (defcustom gnus-auto-select-next t
278   "*If non-nil, offer to go to the next group from the end of the previous.
279 If the value is t and the next newsgroup is empty, Gnus will exit
280 summary mode and go back to group mode.  If the value is neither nil
281 nor t, Gnus will select the following unread newsgroup.  In
282 particular, if the value is the symbol `quietly', the next unread
283 newsgroup will be selected without any confirmation, and if it is
284 `almost-quietly', the next group will be selected without any
285 confirmation if you are located on the last article in the group.
286 Finally, if this variable is `slightly-quietly', the `Z n' command
287 will go to the next group without confirmation."
288   :group 'gnus-summary-maneuvering
289   :type '(choice (const :tag "off" nil)
290                  (const quietly)
291                  (const almost-quietly)
292                  (const slightly-quietly)
293                  (sexp :menu-tag "on" t)))
294
295 (defcustom gnus-auto-select-same nil
296   "*If non-nil, select the next article with the same subject.
297 If there are no more articles with the same subject, go to
298 the first unread article."
299   :group 'gnus-summary-maneuvering
300   :type 'boolean)
301
302 (defcustom gnus-summary-check-current nil
303   "*If non-nil, consider the current article when moving.
304 The \"unread\" movement commands will stay on the same line if the
305 current article is unread."
306   :group 'gnus-summary-maneuvering
307   :type 'boolean)
308
309 (defcustom gnus-auto-center-summary t
310   "*If non-nil, always center the current summary buffer.
311 In particular, if `vertical' do only vertical recentering.  If non-nil
312 and non-`vertical', do both horizontal and vertical recentering."
313   :group 'gnus-summary-maneuvering
314   :type '(choice (const :tag "none" nil)
315                  (const vertical)
316                  (sexp :menu-tag "both" t)))
317
318 (defcustom gnus-show-all-headers nil
319   "*If non-nil, don't hide any headers."
320   :group 'gnus-article-hiding
321   :group 'gnus-article-headers
322   :type 'boolean)
323
324 (defcustom gnus-summary-ignore-duplicates nil
325   "*If non-nil, ignore articles with identical Message-ID headers."
326   :group 'gnus-summary
327   :type 'boolean)
328
329 (defcustom gnus-single-article-buffer t
330   "*If non-nil, display all articles in the same buffer.
331 If nil, each group will get its own article buffer."
332   :group 'gnus-article-various
333   :type 'boolean)
334
335 (defcustom gnus-break-pages t
336   "*If non-nil, do page breaking on articles.
337 The page delimiter is specified by the `gnus-page-delimiter'
338 variable."
339   :group 'gnus-article-various
340   :type 'boolean)
341
342 (defcustom gnus-show-mime t
343   "*If non-nil, do mime processing of articles.
344 The articles will simply be fed to the function given by
345 `gnus-article-display-method-for-mime'."
346   :group 'gnus-article-mime
347   :type 'boolean)
348
349 (defcustom gnus-move-split-methods nil
350   "*Variable used to suggest where articles are to be moved to.
351 It uses the same syntax as the `gnus-split-methods' variable."
352   :group 'gnus-summary-mail
353   :type '(repeat (choice (list :value (fun) function)
354                          (cons :value ("" "") regexp (repeat string))
355                          (sexp :value nil))))
356
357 (defcustom gnus-unread-mark ? 
358   "*Mark used for unread articles."
359   :group 'gnus-summary-marks
360   :type 'character)
361
362 (defcustom gnus-ticked-mark ?!
363   "*Mark used for ticked articles."
364   :group 'gnus-summary-marks
365   :type 'character)
366
367 (defcustom gnus-dormant-mark ??
368   "*Mark used for dormant articles."
369   :group 'gnus-summary-marks
370   :type 'character)
371
372 (defcustom gnus-del-mark ?r
373   "*Mark used for del'd articles."
374   :group 'gnus-summary-marks
375   :type 'character)
376
377 (defcustom gnus-read-mark ?R
378   "*Mark used for read articles."
379   :group 'gnus-summary-marks
380   :type 'character)
381
382 (defcustom gnus-expirable-mark ?E
383   "*Mark used for expirable articles."
384   :group 'gnus-summary-marks
385   :type 'character)
386
387 (defcustom gnus-killed-mark ?K
388   "*Mark used for killed articles."
389   :group 'gnus-summary-marks
390   :type 'character)
391
392 (defcustom gnus-souped-mark ?F
393   "*Mark used for killed articles."
394   :group 'gnus-summary-marks
395   :type 'character)
396
397 (defcustom gnus-kill-file-mark ?X
398   "*Mark used for articles killed by kill files."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-low-score-mark ?Y
403   "*Mark used for articles with a low score."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-catchup-mark ?C
408   "*Mark used for articles that are caught up."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-replied-mark ?A
413   "*Mark used for articles that have been replied to."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-cached-mark ?*
418   "*Mark used for articles that are in the cache."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-saved-mark ?S
423   "*Mark used for articles that have been saved to."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-ancient-mark ?O
428   "*Mark used for ancient articles."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-sparse-mark ?Q
433   "*Mark used for sparsely reffed articles."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-canceled-mark ?G
438   "*Mark used for canceled articles."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-duplicate-mark ?M
443   "*Mark used for duplicate articles."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-undownloaded-mark ?@
448   "*Mark used for articles that weren't downloaded."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-downloadable-mark ?%
453   "*Mark used for articles that are to be downloaded."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-unsendable-mark ?=
458   "*Mark used for articles that won't be sent."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-score-over-mark ?+
463   "*Score mark used for articles with high scores."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-score-below-mark ?-
468   "*Score mark used for articles with low scores."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-empty-thread-mark ? 
473   "*There is no thread under the article."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-not-empty-thread-mark ?=
478   "*There is a thread under the article."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-view-pseudo-asynchronously nil
483   "*If non-nil, Gnus will view pseudo-articles asynchronously."
484   :group 'gnus-extract-view
485   :type 'boolean)
486
487 (defcustom gnus-view-pseudos nil
488   "*If `automatic', pseudo-articles will be viewed automatically.
489 If `not-confirm', pseudos will be viewed automatically, and the user
490 will not be asked to confirm the command."
491   :group 'gnus-extract-view
492   :type '(choice (const :tag "off" nil)
493                  (const automatic)
494                  (const not-confirm)))
495
496 (defcustom gnus-view-pseudos-separately t
497   "*If non-nil, one pseudo-article will be created for each file to be viewed.
498 If nil, all files that use the same viewing command will be given as a
499 list of parameters to that command."
500   :group 'gnus-extract-view
501   :type 'boolean)
502
503 (defcustom gnus-insert-pseudo-articles t
504   "*If non-nil, insert pseudo-articles when decoding articles."
505   :group 'gnus-extract-view
506   :type 'boolean)
507
508 (defcustom gnus-summary-dummy-line-format
509   "  %(:                          :%) %S\n"
510   "*The format specification for the dummy roots in the summary buffer.
511 It works along the same lines as a normal formatting string,
512 with some simple extensions.
513
514 %S  The subject"
515   :group 'gnus-threading
516   :type 'string)
517
518 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
519   "*The format specification for the summary mode line.
520 It works along the same lines as a normal formatting string,
521 with some simple extensions:
522
523 %G  Group name
524 %p  Unprefixed group name
525 %A  Current article number
526 %z  Current article score
527 %V  Gnus version
528 %U  Number of unread articles in the group
529 %e  Number of unselected articles in the group
530 %Z  A string with unread/unselected article counts
531 %g  Shortish group name
532 %S  Subject of the current article
533 %u  User-defined spec
534 %s  Current score file name
535 %d  Number of dormant articles
536 %r  Number of articles that have been marked as read in this session
537 %E  Number of articles expunged by the score files"
538   :group 'gnus-summary-format
539   :type 'string)
540
541 (defcustom gnus-summary-mark-below 0
542   "*Mark all articles with a score below this variable as read.
543 This variable is local to each summary buffer and usually set by the
544 score file."
545   :group 'gnus-score-default
546   :type 'integer)
547
548 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
549   "*List of functions used for sorting articles in the summary buffer.
550 This variable is only used when not using a threaded display."
551   :group 'gnus-summary-sort
552   :type '(repeat (choice (function-item gnus-article-sort-by-number)
553                          (function-item gnus-article-sort-by-author)
554                          (function-item gnus-article-sort-by-subject)
555                          (function-item gnus-article-sort-by-date)
556                          (function-item gnus-article-sort-by-score)
557                          (function :tag "other"))))
558
559 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
560   "*List of functions used for sorting threads in the summary buffer.
561 By default, threads are sorted by article number.
562
563 Each function takes two threads and return non-nil if the first thread
564 should be sorted before the other.  If you use more than one function,
565 the primary sort function should be the last.  You should probably
566 always include `gnus-thread-sort-by-number' in the list of sorting
567 functions -- preferably first.
568
569 Ready-made functions include `gnus-thread-sort-by-number',
570 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
571 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
572 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
573   :group 'gnus-summary-sort
574   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
575                          (function-item gnus-thread-sort-by-author)
576                          (function-item gnus-thread-sort-by-subject)
577                          (function-item gnus-thread-sort-by-date)
578                          (function-item gnus-thread-sort-by-score)
579                          (function-item gnus-thread-sort-by-total-score)
580                          (function :tag "other"))))
581
582 (defcustom gnus-thread-score-function '+
583   "*Function used for calculating the total score of a thread.
584
585 The function is called with the scores of the article and each
586 subthread and should then return the score of the thread.
587
588 Some functions you can use are `+', `max', or `min'."
589   :group 'gnus-summary-sort
590   :type 'function)
591
592 (defcustom gnus-summary-expunge-below nil
593   "All articles that have a score less than this variable will be expunged.
594 This variable is local to the summary buffers."
595   :group 'gnus-score-default
596   :type '(choice (const :tag "off" nil)
597                  integer))
598
599 (defcustom gnus-thread-expunge-below nil
600   "All threads that have a total score less than this variable will be expunged.
601 See `gnus-thread-score-function' for en explanation of what a
602 \"thread score\" is.
603
604 This variable is local to the summary buffers."
605   :group 'gnus-treading
606   :group 'gnus-score-default
607   :type '(choice (const :tag "off" nil)
608                  integer))
609
610 (defcustom gnus-summary-mode-hook nil
611   "*A hook for Gnus summary mode.
612 This hook is run before any variables are set in the summary buffer."
613   :group 'gnus-summary-various
614   :type 'hook)
615
616 (defcustom gnus-summary-menu-hook nil
617   "*Hook run after the creation of the summary mode menu."
618   :group 'gnus-summary-visual
619   :type 'hook)
620
621 (defcustom gnus-summary-exit-hook nil
622   "*A hook called on exit from the summary buffer.
623 It will be called with point in the group buffer."
624   :group 'gnus-summary-exit
625   :type 'hook)
626
627 (defcustom gnus-summary-prepare-hook nil
628   "*A hook called after the summary buffer has been generated.
629 If you want to modify the summary buffer, you can use this hook."
630   :group 'gnus-summary-various
631   :type 'hook)
632
633 (defcustom gnus-summary-prepared-hook nil
634   "*A hook called as the last thing after the summary buffer has been generated."
635   :group 'gnus-summary-various
636   :type 'hook)
637
638 (defcustom gnus-summary-generate-hook nil
639   "*A hook run just before generating the summary buffer.
640 This hook is commonly used to customize threading variables and the
641 like."
642   :group 'gnus-summary-various
643   :type 'hook)
644
645 (defcustom gnus-select-group-hook nil
646   "*A hook called when a newsgroup is selected.
647
648 If you'd like to simplify subjects like the
649 `gnus-summary-next-same-subject' command does, you can use the
650 following hook:
651
652  (setq gnus-select-group-hook
653       (list
654         (lambda ()
655           (mapcar (lambda (header)
656                      (mail-header-set-subject
657                       header
658                       (gnus-simplify-subject
659                        (mail-header-subject header) 're-only)))
660                   gnus-newsgroup-headers))))"
661   :group 'gnus-group-select
662   :type 'hook)
663
664 (defcustom gnus-select-article-hook nil
665   "*A hook called when an article is selected."
666   :group 'gnus-summary-choose
667   :type 'hook)
668
669 (defcustom gnus-visual-mark-article-hook
670   (list 'gnus-highlight-selected-summary)
671   "*Hook run after selecting an article in the summary buffer.
672 It is meant to be used for highlighting the article in some way.  It
673 is not run if `gnus-visual' is nil."
674   :group 'gnus-summary-visual
675   :type 'hook)
676
677 (defcustom gnus-structured-field-decoder
678   #'eword-decode-and-unfold-structured-field
679   "Function to decode non-ASCII characters in structured field for summary."
680   :group 'gnus-various
681   :type 'function)
682
683 (defcustom gnus-unstructured-field-decoder
684   (function
685    (lambda (string)
686      (eword-decode-unstructured-field-body
687       (std11-unfold-string string) 'must-unfold)
688      ))
689   "Function to decode non-ASCII characters in unstructured field for summary."
690   :group 'gnus-various
691   :type 'function)
692
693 (defcustom gnus-parse-headers-hook
694   '(gnus-set-summary-default-charset)
695   "*A hook called before parsing the headers."
696   :group 'gnus-various
697   :type 'hook)
698
699 (defcustom gnus-exit-group-hook nil
700   "*A hook called when exiting (not quitting) summary mode."
701   :group 'gnus-various
702   :type 'hook)
703
704 (defcustom gnus-summary-update-hook
705   (list 'gnus-summary-highlight-line)
706   "*A hook called when a summary line is changed.
707 The hook will not be called if `gnus-visual' is nil.
708
709 The default function `gnus-summary-highlight-line' will
710 highlight the line according to the `gnus-summary-highlight'
711 variable."
712   :group 'gnus-summary-visual
713   :type 'hook)
714
715 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
716   "*A hook called when an article is selected for the first time.
717 The hook is intended to mark an article as read (or unread)
718 automatically when it is selected."
719   :group 'gnus-summary-choose
720   :type 'hook)
721
722 (defcustom gnus-group-no-more-groups-hook nil
723   "*A hook run when returning to group mode having no more (unread) groups."
724   :group 'gnus-group-select
725   :type 'hook)
726
727 (defcustom gnus-ps-print-hook nil
728   "*A hook run before ps-printing something from Gnus."
729   :group 'gnus-summary
730   :type 'hook)
731
732 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
733   "Face used for highlighting the current article in the summary buffer."
734   :group 'gnus-summary-visual
735   :type 'face)
736
737 (defcustom gnus-summary-highlight
738   '(((= mark gnus-canceled-mark)
739      . gnus-summary-cancelled-face)
740     ((and (> score default)
741           (or (= mark gnus-dormant-mark)
742               (= mark gnus-ticked-mark)))
743      . gnus-summary-high-ticked-face)
744     ((and (< score default)
745           (or (= mark gnus-dormant-mark)
746               (= mark gnus-ticked-mark)))
747      . gnus-summary-low-ticked-face)
748     ((or (= mark gnus-dormant-mark)
749          (= mark gnus-ticked-mark))
750      . gnus-summary-normal-ticked-face)
751     ((and (> score default) (= mark gnus-ancient-mark))
752      . gnus-summary-high-ancient-face)
753     ((and (< score default) (= mark gnus-ancient-mark))
754      . gnus-summary-low-ancient-face)
755     ((= mark gnus-ancient-mark)
756      . gnus-summary-normal-ancient-face)
757     ((and (> score default) (= mark gnus-unread-mark))
758      . gnus-summary-high-unread-face)
759     ((and (< score default) (= mark gnus-unread-mark))
760      . gnus-summary-low-unread-face)
761     ((= mark gnus-unread-mark)
762      . gnus-summary-normal-unread-face)
763     ((and (> score default) (memq mark (list gnus-downloadable-mark
764                                              gnus-undownloaded-mark)))
765      . gnus-summary-high-unread-face)
766     ((and (< score default) (memq mark (list gnus-downloadable-mark
767                                              gnus-undownloaded-mark)))
768      . gnus-summary-low-unread-face)
769     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
770      . gnus-summary-normal-unread-face)
771     ((> score default)
772      . gnus-summary-high-read-face)
773     ((< score default)
774      . gnus-summary-low-read-face)
775     (t
776      . gnus-summary-normal-read-face))
777   "*Controls the highlighting of summary buffer lines.
778
779 A list of (FORM . FACE) pairs.  When deciding how a a particular
780 summary line should be displayed, each form is evaluated.  The content
781 of the face field after the first true form is used.  You can change
782 how those summary lines are displayed, by editing the face field.
783
784 You can use the following variables in the FORM field.
785
786 score:   The articles score
787 default: The default article score.
788 below:   The score below which articles are automatically marked as read.
789 mark:    The articles mark."
790   :group 'gnus-summary-visual
791   :type '(repeat (cons (sexp :tag "Form" nil)
792                        face)))
793
794 (defcustom gnus-alter-header-function nil
795   "Function called to allow alteration of article header structures.
796 The function is called with one parameter, the article header vector,
797 which it may alter in any way.")
798
799 ;;; Internal variables
800
801 (defvar gnus-scores-exclude-files nil)
802 (defvar gnus-page-broken nil)
803
804 (defvar gnus-original-article nil)
805 (defvar gnus-article-internal-prepare-hook nil)
806 (defvar gnus-newsgroup-process-stack nil)
807
808 (defvar gnus-thread-indent-array nil)
809 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
810
811 ;; Avoid highlighting in kill files.
812 (defvar gnus-summary-inhibit-highlight nil)
813 (defvar gnus-newsgroup-selected-overlay nil)
814 (defvar gnus-inhibit-limiting nil)
815 (defvar gnus-newsgroup-adaptive-score-file nil)
816 (defvar gnus-current-score-file nil)
817 (defvar gnus-current-move-group nil)
818 (defvar gnus-current-copy-group nil)
819 (defvar gnus-current-crosspost-group nil)
820
821 (defvar gnus-newsgroup-dependencies nil)
822 (defvar gnus-newsgroup-adaptive nil)
823 (defvar gnus-summary-display-article-function nil)
824 (defvar gnus-summary-highlight-line-function nil
825   "Function called after highlighting a summary line.")
826
827 (defvar gnus-summary-line-format-alist
828   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
829     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
830     (?s gnus-tmp-subject-or-nil ?s)
831     (?n gnus-tmp-name ?s)
832     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
833         ?s)
834     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
835             gnus-tmp-from) ?s)
836     (?F gnus-tmp-from ?s)
837     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
838     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
839     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
840     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
841     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
842     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
843     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
844     (?L gnus-tmp-lines ?d)
845     (?I gnus-tmp-indentation ?s)
846     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
847     (?R gnus-tmp-replied ?c)
848     (?\[ gnus-tmp-opening-bracket ?c)
849     (?\] gnus-tmp-closing-bracket ?c)
850     (?\> (make-string gnus-tmp-level ? ) ?s)
851     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
852     (?i gnus-tmp-score ?d)
853     (?z gnus-tmp-score-char ?c)
854     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
855     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
856     (?U gnus-tmp-unread ?c)
857     (?t (gnus-summary-number-of-articles-in-thread
858          (and (boundp 'thread) (car thread)) gnus-tmp-level)
859         ?d)
860     (?e (gnus-summary-number-of-articles-in-thread
861          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
862         ?c)
863     (?u gnus-tmp-user-defined ?s)
864     (?P (gnus-pick-line-number) ?d))
865   "An alist of format specifications that can appear in summary lines,
866 and what variables they correspond with, along with the type of the
867 variable (string, integer, character, etc).")
868
869 (defvar gnus-summary-dummy-line-format-alist
870   `((?S gnus-tmp-subject ?s)
871     (?N gnus-tmp-number ?d)
872     (?u gnus-tmp-user-defined ?s)))
873
874 (defvar gnus-summary-mode-line-format-alist
875   `((?G gnus-tmp-group-name ?s)
876     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
877     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
878     (?A gnus-tmp-article-number ?d)
879     (?Z gnus-tmp-unread-and-unselected ?s)
880     (?V gnus-version ?s)
881     (?U gnus-tmp-unread-and-unticked ?d)
882     (?S gnus-tmp-subject ?s)
883     (?e gnus-tmp-unselected ?d)
884     (?u gnus-tmp-user-defined ?s)
885     (?d (length gnus-newsgroup-dormant) ?d)
886     (?t (length gnus-newsgroup-marked) ?d)
887     (?r (length gnus-newsgroup-reads) ?d)
888     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
889     (?E gnus-newsgroup-expunged-tally ?d)
890     (?s (gnus-current-score-file-nondirectory) ?s)))
891
892 (defvar gnus-last-search-regexp nil
893   "Default regexp for article search command.")
894
895 (defvar gnus-last-shell-command nil
896   "Default shell command on article.")
897
898 (defvar gnus-newsgroup-begin nil)
899 (defvar gnus-newsgroup-end nil)
900 (defvar gnus-newsgroup-last-rmail nil)
901 (defvar gnus-newsgroup-last-mail nil)
902 (defvar gnus-newsgroup-last-folder nil)
903 (defvar gnus-newsgroup-last-file nil)
904 (defvar gnus-newsgroup-auto-expire nil)
905 (defvar gnus-newsgroup-active nil)
906
907 (defvar gnus-newsgroup-data nil)
908 (defvar gnus-newsgroup-data-reverse nil)
909 (defvar gnus-newsgroup-limit nil)
910 (defvar gnus-newsgroup-limits nil)
911
912 (defvar gnus-newsgroup-unreads nil
913   "List of unread articles in the current newsgroup.")
914
915 (defvar gnus-newsgroup-unselected nil
916   "List of unselected unread articles in the current newsgroup.")
917
918 (defvar gnus-newsgroup-reads nil
919   "Alist of read articles and article marks in the current newsgroup.")
920
921 (defvar gnus-newsgroup-expunged-tally nil)
922
923 (defvar gnus-newsgroup-marked nil
924   "List of ticked articles in the current newsgroup (a subset of unread art).")
925
926 (defvar gnus-newsgroup-killed nil
927   "List of ranges of articles that have been through the scoring process.")
928
929 (defvar gnus-newsgroup-cached nil
930   "List of articles that come from the article cache.")
931
932 (defvar gnus-newsgroup-saved nil
933   "List of articles that have been saved.")
934
935 (defvar gnus-newsgroup-kill-headers nil)
936
937 (defvar gnus-newsgroup-replied nil
938   "List of articles that have been replied to in the current newsgroup.")
939
940 (defvar gnus-newsgroup-expirable nil
941   "List of articles in the current newsgroup that can be expired.")
942
943 (defvar gnus-newsgroup-processable nil
944   "List of articles in the current newsgroup that can be processed.")
945
946 (defvar gnus-newsgroup-downloadable nil
947   "List of articles in the current newsgroup that can be processed.")
948
949 (defvar gnus-newsgroup-undownloaded nil
950   "List of articles in the current newsgroup that haven't been downloaded..")
951
952 (defvar gnus-newsgroup-unsendable nil
953   "List of articles in the current newsgroup that won't be sent.")
954
955 (defvar gnus-newsgroup-bookmarks nil
956   "List of articles in the current newsgroup that have bookmarks.")
957
958 (defvar gnus-newsgroup-dormant nil
959   "List of dormant articles in the current newsgroup.")
960
961 (defvar gnus-newsgroup-scored nil
962   "List of scored articles in the current newsgroup.")
963
964 (defvar gnus-newsgroup-headers nil
965   "List of article headers in the current newsgroup.")
966
967 (defvar gnus-newsgroup-threads nil)
968
969 (defvar gnus-newsgroup-prepared nil
970   "Whether the current group has been prepared properly.")
971
972 (defvar gnus-newsgroup-ancient nil
973   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
974
975 (defvar gnus-newsgroup-sparse nil)
976
977 (defvar gnus-current-article nil)
978 (defvar gnus-article-current nil)
979 (defvar gnus-current-headers nil)
980 (defvar gnus-have-all-headers nil)
981 (defvar gnus-last-article nil)
982 (defvar gnus-newsgroup-history nil)
983
984 (defconst gnus-summary-local-variables
985   '(gnus-newsgroup-name
986     gnus-newsgroup-begin gnus-newsgroup-end
987     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
988     gnus-newsgroup-last-folder gnus-newsgroup-last-file
989     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
990     gnus-newsgroup-unselected gnus-newsgroup-marked
991     gnus-newsgroup-reads gnus-newsgroup-saved
992     gnus-newsgroup-replied gnus-newsgroup-expirable
993     gnus-newsgroup-processable gnus-newsgroup-killed
994     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
995     gnus-newsgroup-unsendable
996     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
997     gnus-newsgroup-headers gnus-newsgroup-threads
998     gnus-newsgroup-prepared gnus-summary-highlight-line-function
999     gnus-current-article gnus-current-headers gnus-have-all-headers
1000     gnus-last-article gnus-article-internal-prepare-hook
1001     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1002     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1003     gnus-thread-expunge-below
1004     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1005     (gnus-summary-mark-below . global)
1006     gnus-newsgroup-active gnus-scores-exclude-files
1007     gnus-newsgroup-history gnus-newsgroup-ancient
1008     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1009     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1010     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1011     (gnus-newsgroup-expunged-tally . 0)
1012     gnus-cache-removable-articles gnus-newsgroup-cached
1013     gnus-newsgroup-data gnus-newsgroup-data-reverse
1014     gnus-newsgroup-limit gnus-newsgroup-limits)
1015   "Variables that are buffer-local to the summary buffers.")
1016
1017 ;; Byte-compiler warning.
1018 (defvar gnus-article-mode-map)
1019
1020 ;; MIME stuff.
1021
1022 (defvar gnus-encoded-word-method-alist
1023   '(("chinese" mail-decode-encoded-word-string rfc1843-decode-string)
1024     (".*" mail-decode-encoded-word-string))
1025   "Alist of regexps (to match group names) and lists of functions to be applied.")
1026
1027 (defun gnus-multi-decode-encoded-word-string (string)
1028   "Apply the functions from `gnus-encoded-word-method-alist' that match."
1029   (let ((alist gnus-encoded-word-method-alist)
1030         elem)
1031     (while (setq elem (pop alist))
1032       (when (string-match (car elem) gnus-newsgroup-name)
1033         (pop elem)
1034         (while elem
1035           (setq string (funcall (pop elem) string)))
1036         (setq alist nil)))
1037     string))
1038
1039 ;; Subject simplification.
1040
1041 (defun gnus-simplify-whitespace (str)
1042   "Remove excessive whitespace."
1043   (let ((mystr str))
1044     ;; Multiple spaces.
1045     (while (string-match "[ \t][ \t]+" mystr)
1046       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1047                           " "
1048                           (substring mystr (match-end 0)))))
1049     ;; Leading spaces.
1050     (when (string-match "^[ \t]+" mystr)
1051       (setq mystr (substring mystr (match-end 0))))
1052     ;; Trailing spaces.
1053     (when (string-match "[ \t]+$" mystr)
1054       (setq mystr (substring mystr 0 (match-beginning 0))))
1055     mystr))
1056
1057 (defsubst gnus-simplify-subject-re (subject)
1058   "Remove \"Re:\" from subject lines."
1059   (if (string-match "^[Rr][Ee]: *" subject)
1060       (substring subject (match-end 0))
1061     subject))
1062
1063 (defun gnus-simplify-subject (subject &optional re-only)
1064   "Remove `Re:' and words in parentheses.
1065 If RE-ONLY is non-nil, strip leading `Re:'s only."
1066   (let ((case-fold-search t))           ;Ignore case.
1067     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1068     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1069       (setq subject (substring subject (match-end 0))))
1070     ;; Remove uninteresting prefixes.
1071     (when (and (not re-only)
1072                gnus-simplify-ignored-prefixes
1073                (string-match gnus-simplify-ignored-prefixes subject))
1074       (setq subject (substring subject (match-end 0))))
1075     ;; Remove words in parentheses from end.
1076     (unless re-only
1077       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1078         (setq subject (substring subject 0 (match-beginning 0)))))
1079     ;; Return subject string.
1080     subject))
1081
1082 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1083 ;; all whitespace.
1084 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1085   (goto-char (point-min))
1086   (while (re-search-forward regexp nil t)
1087       (replace-match (or newtext ""))))
1088
1089 (defun gnus-simplify-buffer-fuzzy ()
1090   "Simplify string in the buffer fuzzily.
1091 The string in the accessible portion of the current buffer is simplified.
1092 It is assumed to be a single-line subject.
1093 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1094 matter is removed.  Additional things can be deleted by setting
1095 gnus-simplify-subject-fuzzy-regexp."
1096   (let ((case-fold-search t)
1097         (modified-tick))
1098     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1099
1100     (while (not (eq modified-tick (buffer-modified-tick)))
1101       (setq modified-tick (buffer-modified-tick))
1102       (cond
1103        ((listp gnus-simplify-subject-fuzzy-regexp)
1104         (mapcar 'gnus-simplify-buffer-fuzzy-step
1105                 gnus-simplify-subject-fuzzy-regexp))
1106        (gnus-simplify-subject-fuzzy-regexp
1107         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1108       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1109       (gnus-simplify-buffer-fuzzy-step
1110        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1111       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1112
1113     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1114     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1115     (gnus-simplify-buffer-fuzzy-step " $")
1116     (gnus-simplify-buffer-fuzzy-step "^ +")))
1117
1118 (defun gnus-simplify-subject-fuzzy (subject)
1119   "Simplify a subject string fuzzily.
1120 See `gnus-simplify-buffer-fuzzy' for details."
1121   (save-excursion
1122     (gnus-set-work-buffer)
1123     (let ((case-fold-search t))
1124       ;; Remove uninteresting prefixes.
1125       (when (and gnus-simplify-ignored-prefixes
1126                  (string-match gnus-simplify-ignored-prefixes subject))
1127         (setq subject (substring subject (match-end 0))))
1128       (insert subject)
1129       (inline (gnus-simplify-buffer-fuzzy))
1130       (buffer-string))))
1131
1132 (defsubst gnus-simplify-subject-fully (subject)
1133   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1134   (cond
1135    (gnus-simplify-subject-functions
1136     (gnus-map-function gnus-simplify-subject-functions subject))
1137    ((null gnus-summary-gather-subject-limit)
1138     (gnus-simplify-subject-re subject))
1139    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1140     (gnus-simplify-subject-fuzzy subject))
1141    ((numberp gnus-summary-gather-subject-limit)
1142     (gnus-limit-string (gnus-simplify-subject-re subject)
1143                        gnus-summary-gather-subject-limit))
1144    (t
1145     subject)))
1146
1147 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1148   "Check whether two subjects are equal.
1149 If optional argument simple-first is t, first argument is already
1150 simplified."
1151   (cond
1152    ((null simple-first)
1153     (equal (gnus-simplify-subject-fully s1)
1154            (gnus-simplify-subject-fully s2)))
1155    (t
1156     (equal s1
1157            (gnus-simplify-subject-fully s2)))))
1158
1159 (defun gnus-summary-bubble-group ()
1160   "Increase the score of the current group.
1161 This is a handy function to add to `gnus-summary-exit-hook' to
1162 increase the score of each group you read."
1163   (gnus-group-add-score gnus-newsgroup-name))
1164
1165 \f
1166 ;;;
1167 ;;; Gnus summary mode
1168 ;;;
1169
1170 (put 'gnus-summary-mode 'mode-class 'special)
1171
1172 (when t
1173   ;; Non-orthogonal keys
1174
1175   (gnus-define-keys gnus-summary-mode-map
1176     " " gnus-summary-next-page
1177     "\177" gnus-summary-prev-page
1178     [delete] gnus-summary-prev-page
1179     [backspace] gnus-summary-prev-page
1180     "\r" gnus-summary-scroll-up
1181     "\M-\r" gnus-summary-scroll-down
1182     "n" gnus-summary-next-unread-article
1183     "p" gnus-summary-prev-unread-article
1184     "N" gnus-summary-next-article
1185     "P" gnus-summary-prev-article
1186     "\M-\C-n" gnus-summary-next-same-subject
1187     "\M-\C-p" gnus-summary-prev-same-subject
1188     "\M-n" gnus-summary-next-unread-subject
1189     "\M-p" gnus-summary-prev-unread-subject
1190     "." gnus-summary-first-unread-article
1191     "," gnus-summary-best-unread-article
1192     "\M-s" gnus-summary-search-article-forward
1193     "\M-r" gnus-summary-search-article-backward
1194     "<" gnus-summary-beginning-of-article
1195     ">" gnus-summary-end-of-article
1196     "j" gnus-summary-goto-article
1197     "^" gnus-summary-refer-parent-article
1198     "\M-^" gnus-summary-refer-article
1199     "u" gnus-summary-tick-article-forward
1200     "!" gnus-summary-tick-article-forward
1201     "U" gnus-summary-tick-article-backward
1202     "d" gnus-summary-mark-as-read-forward
1203     "D" gnus-summary-mark-as-read-backward
1204     "E" gnus-summary-mark-as-expirable
1205     "\M-u" gnus-summary-clear-mark-forward
1206     "\M-U" gnus-summary-clear-mark-backward
1207     "k" gnus-summary-kill-same-subject-and-select
1208     "\C-k" gnus-summary-kill-same-subject
1209     "\M-\C-k" gnus-summary-kill-thread
1210     "\M-\C-l" gnus-summary-lower-thread
1211     "e" gnus-summary-edit-article
1212     "#" gnus-summary-mark-as-processable
1213     "\M-#" gnus-summary-unmark-as-processable
1214     "\M-\C-t" gnus-summary-toggle-threads
1215     "\M-\C-s" gnus-summary-show-thread
1216     "\M-\C-h" gnus-summary-hide-thread
1217     "\M-\C-f" gnus-summary-next-thread
1218     "\M-\C-b" gnus-summary-prev-thread
1219     "\M-\C-u" gnus-summary-up-thread
1220     "\M-\C-d" gnus-summary-down-thread
1221     "&" gnus-summary-execute-command
1222     "c" gnus-summary-catchup-and-exit
1223     "\C-w" gnus-summary-mark-region-as-read
1224     "\C-t" gnus-summary-toggle-truncation
1225     "?" gnus-summary-mark-as-dormant
1226     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1227     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1228     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1229     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1230     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1231     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1232     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1233     "=" gnus-summary-expand-window
1234     "\C-x\C-s" gnus-summary-reselect-current-group
1235     "\M-g" gnus-summary-rescan-group
1236     "w" gnus-summary-stop-page-breaking
1237     "\C-c\C-r" gnus-summary-caesar-message
1238     "\M-t" gnus-summary-toggle-mime
1239     "f" gnus-summary-followup
1240     "F" gnus-summary-followup-with-original
1241     "C" gnus-summary-cancel-article
1242     "r" gnus-summary-reply
1243     "R" gnus-summary-reply-with-original
1244     "\C-c\C-f" gnus-summary-mail-forward
1245     "o" gnus-summary-save-article
1246     "\C-o" gnus-summary-save-article-mail
1247     "|" gnus-summary-pipe-output
1248     "\M-k" gnus-summary-edit-local-kill
1249     "\M-K" gnus-summary-edit-global-kill
1250     ;; "V" gnus-version
1251     "\C-c\C-d" gnus-summary-describe-group
1252     "q" gnus-summary-exit
1253     "Q" gnus-summary-exit-no-update
1254     "\C-c\C-i" gnus-info-find-node
1255     gnus-mouse-2 gnus-mouse-pick-article
1256     "m" gnus-summary-mail-other-window
1257     "a" gnus-summary-post-news
1258     "x" gnus-summary-limit-to-unread
1259     "s" gnus-summary-isearch-article
1260     "t" gnus-article-hide-headers
1261     "g" gnus-summary-show-article
1262     "l" gnus-summary-goto-last-article
1263     "v" gnus-summary-preview-mime-message
1264     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1265     "\C-d" gnus-summary-enter-digest-group
1266     "\M-\C-d" gnus-summary-read-document
1267     "\M-\C-e" gnus-summary-edit-parameters
1268     "\C-c\C-b" gnus-bug
1269     "*" gnus-cache-enter-article
1270     "\M-*" gnus-cache-remove-article
1271     "\M-&" gnus-summary-universal-argument
1272     "\C-l" gnus-recenter
1273     "I" gnus-summary-increase-score
1274     "L" gnus-summary-lower-score
1275     "\M-i" gnus-symbolic-argument
1276     "h" gnus-summary-select-article-buffer
1277     "b" gnus-article-view-part
1278     
1279     "V" gnus-summary-score-map
1280     "X" gnus-uu-extract-map
1281     "S" gnus-summary-send-map)
1282
1283   ;; Sort of orthogonal keymap
1284   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1285     "t" gnus-summary-tick-article-forward
1286     "!" gnus-summary-tick-article-forward
1287     "d" gnus-summary-mark-as-read-forward
1288     "r" gnus-summary-mark-as-read-forward
1289     "c" gnus-summary-clear-mark-forward
1290     " " gnus-summary-clear-mark-forward
1291     "e" gnus-summary-mark-as-expirable
1292     "x" gnus-summary-mark-as-expirable
1293     "?" gnus-summary-mark-as-dormant
1294     "b" gnus-summary-set-bookmark
1295     "B" gnus-summary-remove-bookmark
1296     "#" gnus-summary-mark-as-processable
1297     "\M-#" gnus-summary-unmark-as-processable
1298     "S" gnus-summary-limit-include-expunged
1299     "C" gnus-summary-catchup
1300     "H" gnus-summary-catchup-to-here
1301     "\C-c" gnus-summary-catchup-all
1302     "k" gnus-summary-kill-same-subject-and-select
1303     "K" gnus-summary-kill-same-subject
1304     "P" gnus-uu-mark-map)
1305
1306   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1307     "c" gnus-summary-clear-above
1308     "u" gnus-summary-tick-above
1309     "m" gnus-summary-mark-above
1310     "k" gnus-summary-kill-below)
1311
1312   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1313     "/" gnus-summary-limit-to-subject
1314     "n" gnus-summary-limit-to-articles
1315     "w" gnus-summary-pop-limit
1316     "s" gnus-summary-limit-to-subject
1317     "a" gnus-summary-limit-to-author
1318     "u" gnus-summary-limit-to-unread
1319     "m" gnus-summary-limit-to-marks
1320     "v" gnus-summary-limit-to-score
1321     "*" gnus-summary-limit-include-cached
1322     "D" gnus-summary-limit-include-dormant
1323     "T" gnus-summary-limit-include-thread
1324     "d" gnus-summary-limit-exclude-dormant
1325     "t" gnus-summary-limit-to-age
1326     "E" gnus-summary-limit-include-expunged
1327     "c" gnus-summary-limit-exclude-childless-dormant
1328     "C" gnus-summary-limit-mark-excluded-as-read)
1329
1330   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1331     "n" gnus-summary-next-unread-article
1332     "p" gnus-summary-prev-unread-article
1333     "N" gnus-summary-next-article
1334     "P" gnus-summary-prev-article
1335     "\C-n" gnus-summary-next-same-subject
1336     "\C-p" gnus-summary-prev-same-subject
1337     "\M-n" gnus-summary-next-unread-subject
1338     "\M-p" gnus-summary-prev-unread-subject
1339     "f" gnus-summary-first-unread-article
1340     "b" gnus-summary-best-unread-article
1341     "j" gnus-summary-goto-article
1342     "g" gnus-summary-goto-subject
1343     "l" gnus-summary-goto-last-article
1344     "o" gnus-summary-pop-article)
1345
1346   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1347     "k" gnus-summary-kill-thread
1348     "l" gnus-summary-lower-thread
1349     "i" gnus-summary-raise-thread
1350     "T" gnus-summary-toggle-threads
1351     "t" gnus-summary-rethread-current
1352     "^" gnus-summary-reparent-thread
1353     "s" gnus-summary-show-thread
1354     "S" gnus-summary-show-all-threads
1355     "h" gnus-summary-hide-thread
1356     "H" gnus-summary-hide-all-threads
1357     "n" gnus-summary-next-thread
1358     "p" gnus-summary-prev-thread
1359     "u" gnus-summary-up-thread
1360     "o" gnus-summary-top-thread
1361     "d" gnus-summary-down-thread
1362     "#" gnus-uu-mark-thread
1363     "\M-#" gnus-uu-unmark-thread)
1364
1365   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1366     "g" gnus-summary-prepare
1367     "c" gnus-summary-insert-cached-articles)
1368
1369   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1370     "c" gnus-summary-catchup-and-exit
1371     "C" gnus-summary-catchup-all-and-exit
1372     "E" gnus-summary-exit-no-update
1373     "Q" gnus-summary-exit
1374     "Z" gnus-summary-exit
1375     "n" gnus-summary-catchup-and-goto-next-group
1376     "R" gnus-summary-reselect-current-group
1377     "G" gnus-summary-rescan-group
1378     "N" gnus-summary-next-group
1379     "s" gnus-summary-save-newsrc
1380     "P" gnus-summary-prev-group)
1381
1382   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1383     " " gnus-summary-next-page
1384     "n" gnus-summary-next-page
1385     "\177" gnus-summary-prev-page
1386     [delete] gnus-summary-prev-page
1387     "p" gnus-summary-prev-page
1388     "\r" gnus-summary-scroll-up
1389     "\M-\r" gnus-summary-scroll-down
1390     "<" gnus-summary-beginning-of-article
1391     ">" gnus-summary-end-of-article
1392     "b" gnus-summary-beginning-of-article
1393     "e" gnus-summary-end-of-article
1394     "^" gnus-summary-refer-parent-article
1395     "r" gnus-summary-refer-parent-article
1396     "R" gnus-summary-refer-references
1397     "T" gnus-summary-refer-thread
1398     "g" gnus-summary-show-article
1399     "s" gnus-summary-isearch-article
1400     "P" gnus-summary-print-article)
1401
1402   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1403     "b" gnus-article-add-buttons
1404     "B" gnus-article-add-buttons-to-head
1405     "o" gnus-article-treat-overstrike
1406     "e" gnus-article-emphasize
1407     "w" gnus-article-fill-cited-article
1408     "c" gnus-article-remove-cr
1409     "f" gnus-article-display-x-face
1410     "l" gnus-summary-stop-page-breaking
1411     "r" gnus-summary-caesar-message
1412     "t" gnus-article-hide-headers
1413     "v" gnus-summary-verbose-headers
1414     "m" gnus-summary-toggle-mime
1415     "h" gnus-article-treat-html
1416     "d" gnus-article-treat-dumbquotes)
1417
1418   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1419     "a" gnus-article-hide
1420     "h" gnus-article-hide-headers
1421     "b" gnus-article-hide-boring-headers
1422     "s" gnus-article-hide-signature
1423     "c" gnus-article-hide-citation
1424     "C" gnus-article-hide-citation-in-followups
1425     "p" gnus-article-hide-pgp
1426     "P" gnus-article-hide-pem
1427     "\C-c" gnus-article-hide-citation-maybe)
1428
1429   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1430     "a" gnus-article-highlight
1431     "h" gnus-article-highlight-headers
1432     "c" gnus-article-highlight-citation
1433     "s" gnus-article-highlight-signature)
1434
1435   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1436     "z" gnus-article-date-ut
1437     "u" gnus-article-date-ut
1438     "l" gnus-article-date-local
1439     "e" gnus-article-date-lapsed
1440     "o" gnus-article-date-original
1441     "i" gnus-article-date-iso8601
1442     "s" gnus-article-date-user)
1443
1444   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1445     "t" gnus-article-remove-trailing-blank-lines
1446     "l" gnus-article-strip-leading-blank-lines
1447     "m" gnus-article-strip-multiple-blank-lines
1448     "a" gnus-article-strip-blank-lines
1449     "A" gnus-article-strip-all-blank-lines
1450     "s" gnus-article-strip-leading-space)
1451
1452   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1453     "v" gnus-version
1454     "f" gnus-summary-fetch-faq
1455     "d" gnus-summary-describe-group
1456     "h" gnus-summary-describe-briefly
1457     "i" gnus-info-find-node)
1458
1459   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1460     "e" gnus-summary-expire-articles
1461     "\M-\C-e" gnus-summary-expire-articles-now
1462     "\177" gnus-summary-delete-article
1463     [delete] gnus-summary-delete-article
1464     "m" gnus-summary-move-article
1465     "r" gnus-summary-respool-article
1466     "w" gnus-summary-edit-article
1467     "c" gnus-summary-copy-article
1468     "B" gnus-summary-crosspost-article
1469     "q" gnus-summary-respool-query
1470     "t" gnus-summary-respool-trace
1471     "i" gnus-summary-import-article
1472     "p" gnus-summary-article-posted-p)
1473
1474   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1475     "o" gnus-summary-save-article
1476     "m" gnus-summary-save-article-mail
1477     "F" gnus-summary-write-article-file
1478     "r" gnus-summary-save-article-rmail
1479     "f" gnus-summary-save-article-file
1480     "b" gnus-summary-save-article-body-file
1481     "h" gnus-summary-save-article-folder
1482     "v" gnus-summary-save-article-vm
1483     "p" gnus-summary-pipe-output
1484     "s" gnus-soup-add-article))
1485
1486 (defun gnus-summary-make-menu-bar ()
1487   (gnus-turn-off-edit-menu 'summary)
1488
1489   (unless (boundp 'gnus-summary-misc-menu)
1490
1491     (easy-menu-define
1492      gnus-summary-kill-menu gnus-summary-mode-map ""
1493      (cons
1494       "Score"
1495       (nconc
1496        (list
1497         ["Enter score..." gnus-summary-score-entry t]
1498         ["Customize" gnus-score-customize t])
1499        (gnus-make-score-map 'increase)
1500        (gnus-make-score-map 'lower)
1501        '(("Mark"
1502           ["Kill below" gnus-summary-kill-below t]
1503           ["Mark above" gnus-summary-mark-above t]
1504           ["Tick above" gnus-summary-tick-above t]
1505           ["Clear above" gnus-summary-clear-above t])
1506          ["Current score" gnus-summary-current-score t]
1507          ["Set score" gnus-summary-set-score t]
1508          ["Switch current score file..." gnus-score-change-score-file t]
1509          ["Set mark below..." gnus-score-set-mark-below t]
1510          ["Set expunge below..." gnus-score-set-expunge-below t]
1511          ["Edit current score file" gnus-score-edit-current-scores t]
1512          ["Edit score file" gnus-score-edit-file t]
1513          ["Trace score" gnus-score-find-trace t]
1514          ["Find words" gnus-score-find-favourite-words t]
1515          ["Rescore buffer" gnus-summary-rescore t]
1516          ["Increase score..." gnus-summary-increase-score t]
1517          ["Lower score..." gnus-summary-lower-score t]))))
1518
1519     ;; Define both the Article menu in the summary buffer and the equivalent
1520     ;; Commands menu in the article buffer here for consistency.
1521     (let ((innards
1522            '(("Hide"
1523               ["All" gnus-article-hide t]
1524               ["Headers" gnus-article-hide-headers t]
1525               ["Signature" gnus-article-hide-signature t]
1526               ["Citation" gnus-article-hide-citation t]
1527               ["PGP" gnus-article-hide-pgp t]
1528               ["Boring headers" gnus-article-hide-boring-headers t])
1529              ("Highlight"
1530               ["All" gnus-article-highlight t]
1531               ["Headers" gnus-article-highlight-headers t]
1532               ["Signature" gnus-article-highlight-signature t]
1533               ["Citation" gnus-article-highlight-citation t])
1534              ("Date"
1535               ["Local" gnus-article-date-local t]
1536               ["ISO8601" gnus-article-date-iso8601 t]
1537               ["UT" gnus-article-date-ut t]
1538               ["Original" gnus-article-date-original t]
1539               ["Lapsed" gnus-article-date-lapsed t]
1540               ["User-defined" gnus-article-date-user t])
1541              ("Washing"
1542               ("Remove Blanks"
1543                ["Leading" gnus-article-strip-leading-blank-lines t]
1544                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1545                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1546                ["All of the above" gnus-article-strip-blank-lines t]
1547                ["All" gnus-article-strip-all-blank-lines t]
1548                ["Leading space" gnus-article-strip-leading-space t])
1549               ["Overstrike" gnus-article-treat-overstrike t]
1550               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1551               ["Emphasis" gnus-article-emphasize t]
1552               ["Word wrap" gnus-article-fill-cited-article t]
1553               ["CR" gnus-article-remove-cr t]
1554               ["Show X-Face" gnus-article-display-x-face t]
1555               ["UnHTMLize" gnus-article-treat-html t]
1556               ["Rot 13" gnus-summary-caesar-message t]
1557               ["Unix pipe" gnus-summary-pipe-message t]
1558               ["Add buttons" gnus-article-add-buttons t]
1559               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1560               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1561               ["Toggle MIME" gnus-summary-toggle-mime t]
1562               ["Verbose header" gnus-summary-verbose-headers t]
1563               ["Toggle header" gnus-summary-toggle-header t])
1564              ("Output"
1565               ["Save in default format" gnus-summary-save-article t]
1566               ["Save in file" gnus-summary-save-article-file t]
1567               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1568               ["Save in MH folder" gnus-summary-save-article-folder t]
1569               ["Save in VM folder" gnus-summary-save-article-vm t]
1570               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1571               ["Save body in file" gnus-summary-save-article-body-file t]
1572               ["Pipe through a filter" gnus-summary-pipe-output t]
1573               ["Add to SOUP packet" gnus-soup-add-article t]
1574               ["Print" gnus-summary-print-article t])
1575              ("Backend"
1576               ["Respool article..." gnus-summary-respool-article t]
1577               ["Move article..." gnus-summary-move-article
1578                (gnus-check-backend-function
1579                 'request-move-article gnus-newsgroup-name)]
1580               ["Copy article..." gnus-summary-copy-article t]
1581               ["Crosspost article..." gnus-summary-crosspost-article
1582                (gnus-check-backend-function
1583                 'request-replace-article gnus-newsgroup-name)]
1584               ["Import file..." gnus-summary-import-article t]
1585               ["Check if posted" gnus-summary-article-posted-p t]
1586               ["Edit article" gnus-summary-edit-article
1587                (not (gnus-group-read-only-p))]
1588               ["Delete article" gnus-summary-delete-article
1589                (gnus-check-backend-function
1590                 'request-expire-articles gnus-newsgroup-name)]
1591               ["Query respool" gnus-summary-respool-query t]
1592               ["Trace respool" gnus-summary-respool-trace t]
1593               ["Delete expirable articles" gnus-summary-expire-articles-now
1594                (gnus-check-backend-function
1595                 'request-expire-articles gnus-newsgroup-name)])
1596              ("Extract"
1597               ["Uudecode" gnus-uu-decode-uu t]
1598               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1599               ["Unshar" gnus-uu-decode-unshar t]
1600               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1601               ["Save" gnus-uu-decode-save t]
1602               ["Binhex" gnus-uu-decode-binhex t]
1603               ["Postscript" gnus-uu-decode-postscript t])
1604              ("Cache"
1605               ["Enter article" gnus-cache-enter-article t]
1606               ["Remove article" gnus-cache-remove-article t])
1607              ["Select article buffer" gnus-summary-select-article-buffer t]
1608              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1609              ["Isearch article..." gnus-summary-isearch-article t]
1610              ["Beginning of the article" gnus-summary-beginning-of-article t]
1611              ["End of the article" gnus-summary-end-of-article t]
1612              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1613              ["Fetch referenced articles" gnus-summary-refer-references t]
1614              ["Fetch current thread" gnus-summary-refer-thread t]
1615              ["Fetch article with id..." gnus-summary-refer-article t]
1616              ["Redisplay" gnus-summary-show-article t])))
1617       (easy-menu-define
1618        gnus-summary-article-menu gnus-summary-mode-map ""
1619        (cons "Article" innards))
1620
1621       (easy-menu-define
1622        gnus-article-commands-menu gnus-article-mode-map ""
1623        (cons "Commands" innards)))
1624
1625     (easy-menu-define
1626      gnus-summary-thread-menu gnus-summary-mode-map ""
1627      '("Threads"
1628        ["Toggle threading" gnus-summary-toggle-threads t]
1629        ["Hide threads" gnus-summary-hide-all-threads t]
1630        ["Show threads" gnus-summary-show-all-threads t]
1631        ["Hide thread" gnus-summary-hide-thread t]
1632        ["Show thread" gnus-summary-show-thread t]
1633        ["Go to next thread" gnus-summary-next-thread t]
1634        ["Go to previous thread" gnus-summary-prev-thread t]
1635        ["Go down thread" gnus-summary-down-thread t]
1636        ["Go up thread" gnus-summary-up-thread t]
1637        ["Top of thread" gnus-summary-top-thread t]
1638        ["Mark thread as read" gnus-summary-kill-thread t]
1639        ["Lower thread score" gnus-summary-lower-thread t]
1640        ["Raise thread score" gnus-summary-raise-thread t]
1641        ["Rethread current" gnus-summary-rethread-current t]
1642        ))
1643
1644     (easy-menu-define
1645      gnus-summary-post-menu gnus-summary-mode-map ""
1646      '("Post"
1647        ["Post an article" gnus-summary-post-news t]
1648        ["Followup" gnus-summary-followup t]
1649        ["Followup and yank" gnus-summary-followup-with-original t]
1650        ["Supersede article" gnus-summary-supersede-article t]
1651        ["Cancel article" gnus-summary-cancel-article t]
1652        ["Reply" gnus-summary-reply t]
1653        ["Reply and yank" gnus-summary-reply-with-original t]
1654        ["Wide reply" gnus-summary-wide-reply t]
1655        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1656        ["Mail forward" gnus-summary-mail-forward t]
1657        ["Post forward" gnus-summary-post-forward t]
1658        ["Digest and mail" gnus-summary-mail-digest t]
1659        ["Digest and post" gnus-summary-post-digest t]
1660        ["Resend message" gnus-summary-resend-message t]
1661        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1662        ["Send a mail" gnus-summary-mail-other-window t]
1663        ["Uuencode and post" gnus-uu-post-news t]
1664        ["Followup via news" gnus-summary-followup-to-mail t]
1665        ["Followup via news and yank"
1666         gnus-summary-followup-to-mail-with-original t]
1667        ;;("Draft"
1668        ;;["Send" gnus-summary-send-draft t]
1669        ;;["Send bounced" gnus-resend-bounced-mail t])
1670        ))
1671
1672     (easy-menu-define
1673      gnus-summary-misc-menu gnus-summary-mode-map ""
1674      '("Misc"
1675        ("Mark Read"
1676         ["Mark as read" gnus-summary-mark-as-read-forward t]
1677         ["Mark same subject and select"
1678          gnus-summary-kill-same-subject-and-select t]
1679         ["Mark same subject" gnus-summary-kill-same-subject t]
1680         ["Catchup" gnus-summary-catchup t]
1681         ["Catchup all" gnus-summary-catchup-all t]
1682         ["Catchup to here" gnus-summary-catchup-to-here t]
1683         ["Catchup region" gnus-summary-mark-region-as-read t]
1684         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1685        ("Mark Various"
1686         ["Tick" gnus-summary-tick-article-forward t]
1687         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1688         ["Remove marks" gnus-summary-clear-mark-forward t]
1689         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1690         ["Set bookmark" gnus-summary-set-bookmark t]
1691         ["Remove bookmark" gnus-summary-remove-bookmark t])
1692        ("Mark Limit"
1693         ["Marks..." gnus-summary-limit-to-marks t]
1694         ["Subject..." gnus-summary-limit-to-subject t]
1695         ["Author..." gnus-summary-limit-to-author t]
1696         ["Age..." gnus-summary-limit-to-age t]
1697         ["Score" gnus-summary-limit-to-score t]
1698         ["Unread" gnus-summary-limit-to-unread t]
1699         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1700         ["Articles" gnus-summary-limit-to-articles t]
1701         ["Pop limit" gnus-summary-pop-limit t]
1702         ["Show dormant" gnus-summary-limit-include-dormant t]
1703         ["Hide childless dormant"
1704          gnus-summary-limit-exclude-childless-dormant t]
1705         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1706         ["Show expunged" gnus-summary-show-all-expunged t])
1707        ("Process Mark"
1708         ["Set mark" gnus-summary-mark-as-processable t]
1709         ["Remove mark" gnus-summary-unmark-as-processable t]
1710         ["Remove all marks" gnus-summary-unmark-all-processable t]
1711         ["Mark above" gnus-uu-mark-over t]
1712         ["Mark series" gnus-uu-mark-series t]
1713         ["Mark region" gnus-uu-mark-region t]
1714         ["Unmark region" gnus-uu-unmark-region t]
1715         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1716         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1717         ["Mark all" gnus-uu-mark-all t]
1718         ["Mark buffer" gnus-uu-mark-buffer t]
1719         ["Mark sparse" gnus-uu-mark-sparse t]
1720         ["Mark thread" gnus-uu-mark-thread t]
1721         ["Unmark thread" gnus-uu-unmark-thread t]
1722         ("Process Mark Sets"
1723          ["Kill" gnus-summary-kill-process-mark t]
1724          ["Yank" gnus-summary-yank-process-mark
1725           gnus-newsgroup-process-stack]
1726          ["Save" gnus-summary-save-process-mark t]))
1727        ("Scroll article"
1728         ["Page forward" gnus-summary-next-page t]
1729         ["Page backward" gnus-summary-prev-page t]
1730         ["Line forward" gnus-summary-scroll-up t])
1731        ("Move"
1732         ["Next unread article" gnus-summary-next-unread-article t]
1733         ["Previous unread article" gnus-summary-prev-unread-article t]
1734         ["Next article" gnus-summary-next-article t]
1735         ["Previous article" gnus-summary-prev-article t]
1736         ["Next unread subject" gnus-summary-next-unread-subject t]
1737         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1738         ["Next article same subject" gnus-summary-next-same-subject t]
1739         ["Previous article same subject" gnus-summary-prev-same-subject t]
1740         ["First unread article" gnus-summary-first-unread-article t]
1741         ["Best unread article" gnus-summary-best-unread-article t]
1742         ["Go to subject number..." gnus-summary-goto-subject t]
1743         ["Go to article number..." gnus-summary-goto-article t]
1744         ["Go to the last article" gnus-summary-goto-last-article t]
1745         ["Pop article off history" gnus-summary-pop-article t])
1746        ("Sort"
1747         ["Sort by number" gnus-summary-sort-by-number t]
1748         ["Sort by author" gnus-summary-sort-by-author t]
1749         ["Sort by subject" gnus-summary-sort-by-subject t]
1750         ["Sort by date" gnus-summary-sort-by-date t]
1751         ["Sort by score" gnus-summary-sort-by-score t]
1752         ["Sort by lines" gnus-summary-sort-by-lines t])
1753        ("Help"
1754         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1755         ["Describe group" gnus-summary-describe-group t]
1756         ["Read manual" gnus-info-find-node t])
1757        ("Modes"
1758         ["Pick and read" gnus-pick-mode t]
1759         ["Binary" gnus-binary-mode t])
1760        ("Regeneration"
1761         ["Regenerate" gnus-summary-prepare t]
1762         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1763         ["Toggle threading" gnus-summary-toggle-threads t])
1764        ["Filter articles..." gnus-summary-execute-command t]
1765        ["Run command on subjects..." gnus-summary-universal-argument t]
1766        ["Search articles forward..." gnus-summary-search-article-forward t]
1767        ["Search articles backward..." gnus-summary-search-article-backward t]
1768        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1769        ["Expand window" gnus-summary-expand-window t]
1770        ["Expire expirable articles" gnus-summary-expire-articles
1771         (gnus-check-backend-function
1772          'request-expire-articles gnus-newsgroup-name)]
1773        ["Edit local kill file" gnus-summary-edit-local-kill t]
1774        ["Edit main kill file" gnus-summary-edit-global-kill t]
1775        ["Edit group parameters" gnus-summary-edit-parameters t]
1776        ["Send a bug report" gnus-bug t]
1777        ("Exit"
1778         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1779         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1780         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1781         ["Exit group" gnus-summary-exit t]
1782         ["Exit group without updating" gnus-summary-exit-no-update t]
1783         ["Exit and goto next group" gnus-summary-next-group t]
1784         ["Exit and goto prev group" gnus-summary-prev-group t]
1785         ["Reselect group" gnus-summary-reselect-current-group t]
1786         ["Rescan group" gnus-summary-rescan-group t]
1787         ["Update dribble" gnus-summary-save-newsrc t])))
1788
1789     (gnus-run-hooks 'gnus-summary-menu-hook)))
1790
1791 (defun gnus-score-set-default (var value)
1792   "A version of set that updates the GNU Emacs menu-bar."
1793   (set var value)
1794   ;; It is the message that forces the active status to be updated.
1795   (message ""))
1796
1797 (defun gnus-make-score-map (type)
1798   "Make a summary score map of type TYPE."
1799   (if t
1800       nil
1801     (let ((headers '(("author" "from" string)
1802                      ("subject" "subject" string)
1803                      ("article body" "body" string)
1804                      ("article head" "head" string)
1805                      ("xref" "xref" string)
1806                      ("lines" "lines" number)
1807                      ("followups to author" "followup" string)))
1808           (types '((number ("less than" <)
1809                            ("greater than" >)
1810                            ("equal" =))
1811                    (string ("substring" s)
1812                            ("exact string" e)
1813                            ("fuzzy string" f)
1814                            ("regexp" r))))
1815           (perms '(("temporary" (current-time-string))
1816                    ("permanent" nil)
1817                    ("immediate" now)))
1818           header)
1819       (list
1820        (apply
1821         'nconc
1822         (list
1823          (if (eq type 'lower)
1824              "Lower score"
1825            "Increase score"))
1826         (let (outh)
1827           (while headers
1828             (setq header (car headers))
1829             (setq outh
1830                   (cons
1831                    (apply
1832                     'nconc
1833                     (list (car header))
1834                     (let ((ts (cdr (assoc (nth 2 header) types)))
1835                           outt)
1836                       (while ts
1837                         (setq outt
1838                               (cons
1839                                (apply
1840                                 'nconc
1841                                 (list (caar ts))
1842                                 (let ((ps perms)
1843                                       outp)
1844                                   (while ps
1845                                     (setq outp
1846                                           (cons
1847                                            (vector
1848                                             (caar ps)
1849                                             (list
1850                                              'gnus-summary-score-entry
1851                                              (nth 1 header)
1852                                              (if (or (string= (nth 1 header)
1853                                                               "head")
1854                                                      (string= (nth 1 header)
1855                                                               "body"))
1856                                                  ""
1857                                                (list 'gnus-summary-header
1858                                                      (nth 1 header)))
1859                                              (list 'quote (nth 1 (car ts)))
1860                                              (list 'gnus-score-default nil)
1861                                              (nth 1 (car ps))
1862                                              t)
1863                                             t)
1864                                            outp))
1865                                     (setq ps (cdr ps)))
1866                                   (list (nreverse outp))))
1867                                outt))
1868                         (setq ts (cdr ts)))
1869                       (list (nreverse outt))))
1870                    outh))
1871             (setq headers (cdr headers)))
1872           (list (nreverse outh))))))))
1873
1874 \f
1875
1876 (defun gnus-summary-mode (&optional group)
1877   "Major mode for reading articles.
1878
1879 All normal editing commands are switched off.
1880 \\<gnus-summary-mode-map>
1881 Each line in this buffer represents one article.  To read an
1882 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1883 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1884 respectively.
1885
1886 You can also post articles and send mail from this buffer.  To
1887 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1888 of an article, type `\\[gnus-summary-reply]'.
1889
1890 There are approx. one gazillion commands you can execute in this
1891 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1892
1893 The following commands are available:
1894
1895 \\{gnus-summary-mode-map}"
1896   (interactive)
1897   (when (gnus-visual-p 'summary-menu 'menu)
1898     (gnus-summary-make-menu-bar))
1899   (kill-all-local-variables)
1900   (gnus-summary-make-local-variables)
1901   (gnus-make-thread-indent-array)
1902   (gnus-simplify-mode-line)
1903   (setq major-mode 'gnus-summary-mode)
1904   (setq mode-name "Summary")
1905   (make-local-variable 'minor-mode-alist)
1906   (use-local-map gnus-summary-mode-map)
1907   (buffer-disable-undo)
1908   (setq buffer-read-only t)             ;Disable modification
1909   (setq truncate-lines t)
1910   (setq selective-display t)
1911   (setq selective-display-ellipses t)   ;Display `...'
1912   (gnus-summary-set-display-table)
1913   (gnus-set-default-directory)
1914   (setq gnus-newsgroup-name group)
1915   (make-local-variable 'gnus-summary-line-format)
1916   (make-local-variable 'gnus-summary-line-format-spec)
1917   (make-local-variable 'gnus-summary-dummy-line-format)
1918   (make-local-variable 'gnus-summary-dummy-line-format-spec)
1919   (make-local-variable 'gnus-summary-mark-positions)
1920   (make-local-hook 'post-command-hook)
1921   (add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
1922   (make-local-hook 'pre-command-hook)
1923   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
1924   (gnus-run-hooks 'gnus-summary-mode-hook)
1925   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
1926   (gnus-update-summary-mark-positions))
1927
1928 (defun gnus-summary-make-local-variables ()
1929   "Make all the local summary buffer variables."
1930   (let ((locals gnus-summary-local-variables)
1931         global local)
1932     (while (setq local (pop locals))
1933       (if (consp local)
1934           (progn
1935             (if (eq (cdr local) 'global)
1936                 ;; Copy the global value of the variable.
1937                 (setq global (symbol-value (car local)))
1938               ;; Use the value from the list.
1939               (setq global (eval (cdr local))))
1940             (make-local-variable (car local))
1941             (set (car local) global))
1942         ;; Simple nil-valued local variable.
1943         (make-local-variable local)
1944         (set local nil)))))
1945
1946 (defun gnus-summary-clear-local-variables ()
1947   (let ((locals gnus-summary-local-variables))
1948     (while locals
1949       (if (consp (car locals))
1950           (and (vectorp (caar locals))
1951                (set (caar locals) nil))
1952         (and (vectorp (car locals))
1953              (set (car locals) nil)))
1954       (setq locals (cdr locals)))))
1955
1956 ;; Summary data functions.
1957
1958 (defmacro gnus-data-number (data)
1959   `(car ,data))
1960
1961 (defmacro gnus-data-set-number (data number)
1962   `(setcar ,data ,number))
1963
1964 (defmacro gnus-data-mark (data)
1965   `(nth 1 ,data))
1966
1967 (defmacro gnus-data-set-mark (data mark)
1968   `(setcar (nthcdr 1 ,data) ,mark))
1969
1970 (defmacro gnus-data-pos (data)
1971   `(nth 2 ,data))
1972
1973 (defmacro gnus-data-set-pos (data pos)
1974   `(setcar (nthcdr 2 ,data) ,pos))
1975
1976 (defmacro gnus-data-header (data)
1977   `(nth 3 ,data))
1978
1979 (defmacro gnus-data-set-header (data header)
1980   `(setf (nth 3 ,data) ,header))
1981
1982 (defmacro gnus-data-level (data)
1983   `(nth 4 ,data))
1984
1985 (defmacro gnus-data-unread-p (data)
1986   `(= (nth 1 ,data) gnus-unread-mark))
1987
1988 (defmacro gnus-data-read-p (data)
1989   `(/= (nth 1 ,data) gnus-unread-mark))
1990
1991 (defmacro gnus-data-pseudo-p (data)
1992   `(consp (nth 3 ,data)))
1993
1994 (defmacro gnus-data-find (number)
1995   `(assq ,number gnus-newsgroup-data))
1996
1997 (defmacro gnus-data-find-list (number &optional data)
1998   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
1999      (memq (assq ,number bdata)
2000            bdata)))
2001
2002 (defmacro gnus-data-make (number mark pos header level)
2003   `(list ,number ,mark ,pos ,header ,level))
2004
2005 (defun gnus-data-enter (after-article number mark pos header level offset)
2006   (let ((data (gnus-data-find-list after-article)))
2007     (unless data
2008       (error "No such article: %d" after-article))
2009     (setcdr data (cons (gnus-data-make number mark pos header level)
2010                        (cdr data)))
2011     (setq gnus-newsgroup-data-reverse nil)
2012     (gnus-data-update-list (cddr data) offset)))
2013
2014 (defun gnus-data-enter-list (after-article list &optional offset)
2015   (when list
2016     (let ((data (and after-article (gnus-data-find-list after-article)))
2017           (ilist list))
2018       (if (not (or data
2019                    after-article))
2020           (let ((odata gnus-newsgroup-data))
2021             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2022             (when offset
2023               (gnus-data-update-list odata offset)))
2024         ;; Find the last element in the list to be spliced into the main
2025         ;; list.
2026         (while (cdr list)
2027           (setq list (cdr list)))
2028         (if (not data)
2029             (progn
2030               (setcdr list gnus-newsgroup-data)
2031               (setq gnus-newsgroup-data ilist)
2032               (when offset
2033                 (gnus-data-update-list (cdr list) offset)))
2034           (setcdr list (cdr data))
2035           (setcdr data ilist)
2036           (when offset
2037             (gnus-data-update-list (cdr list) offset))))
2038       (setq gnus-newsgroup-data-reverse nil))))
2039
2040 (defun gnus-data-remove (article &optional offset)
2041   (let ((data gnus-newsgroup-data))
2042     (if (= (gnus-data-number (car data)) article)
2043         (progn
2044           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2045                 gnus-newsgroup-data-reverse nil)
2046           (when offset
2047             (gnus-data-update-list gnus-newsgroup-data offset)))
2048       (while (cdr data)
2049         (when (= (gnus-data-number (cadr data)) article)
2050           (setcdr data (cddr data))
2051           (when offset
2052             (gnus-data-update-list (cdr data) offset))
2053           (setq data nil
2054                 gnus-newsgroup-data-reverse nil))
2055         (setq data (cdr data))))))
2056
2057 (defmacro gnus-data-list (backward)
2058   `(if ,backward
2059        (or gnus-newsgroup-data-reverse
2060            (setq gnus-newsgroup-data-reverse
2061                  (reverse gnus-newsgroup-data)))
2062      gnus-newsgroup-data))
2063
2064 (defun gnus-data-update-list (data offset)
2065   "Add OFFSET to the POS of all data entries in DATA."
2066   (setq gnus-newsgroup-data-reverse nil)
2067   (while data
2068     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2069     (setq data (cdr data))))
2070
2071 (defun gnus-summary-article-pseudo-p (article)
2072   "Say whether this article is a pseudo article or not."
2073   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2074
2075 (defmacro gnus-summary-article-sparse-p (article)
2076   "Say whether this article is a sparse article or not."
2077   `(memq ,article gnus-newsgroup-sparse))
2078
2079 (defmacro gnus-summary-article-ancient-p (article)
2080   "Say whether this article is a sparse article or not."
2081   `(memq ,article gnus-newsgroup-ancient))
2082
2083 (defun gnus-article-parent-p (number)
2084   "Say whether this article is a parent or not."
2085   (let ((data (gnus-data-find-list number)))
2086     (and (cdr data)                     ; There has to be an article after...
2087          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2088             (gnus-data-level (nth 1 data))))))
2089
2090 (defun gnus-article-children (number)
2091   "Return a list of all children to NUMBER."
2092   (let* ((data (gnus-data-find-list number))
2093          (level (gnus-data-level (car data)))
2094          children)
2095     (setq data (cdr data))
2096     (while (and data
2097                 (= (gnus-data-level (car data)) (1+ level)))
2098       (push (gnus-data-number (car data)) children)
2099       (setq data (cdr data)))
2100     children))
2101
2102 (defmacro gnus-summary-skip-intangible ()
2103   "If the current article is intangible, then jump to a different article."
2104   '(let ((to (get-text-property (point) 'gnus-intangible)))
2105      (and to (gnus-summary-goto-subject to))))
2106
2107 (defmacro gnus-summary-article-intangible-p ()
2108   "Say whether this article is intangible or not."
2109   '(get-text-property (point) 'gnus-intangible))
2110
2111 (defun gnus-article-read-p (article)
2112   "Say whether ARTICLE is read or not."
2113   (not (or (memq article gnus-newsgroup-marked)
2114            (memq article gnus-newsgroup-unreads)
2115            (memq article gnus-newsgroup-unselected)
2116            (memq article gnus-newsgroup-dormant))))
2117
2118 ;; Some summary mode macros.
2119
2120 (defmacro gnus-summary-article-number ()
2121   "The article number of the article on the current line.
2122 If there isn's an article number here, then we return the current
2123 article number."
2124   '(progn
2125      (gnus-summary-skip-intangible)
2126      (or (get-text-property (point) 'gnus-number)
2127          (gnus-summary-last-subject))))
2128
2129 (defmacro gnus-summary-article-header (&optional number)
2130   "Return the header of article NUMBER."
2131   `(gnus-data-header (gnus-data-find
2132                       ,(or number '(gnus-summary-article-number)))))
2133
2134 (defmacro gnus-summary-thread-level (&optional number)
2135   "Return the level of thread that starts with article NUMBER."
2136   `(if (and (eq gnus-summary-make-false-root 'dummy)
2137             (get-text-property (point) 'gnus-intangible))
2138        0
2139      (gnus-data-level (gnus-data-find
2140                        ,(or number '(gnus-summary-article-number))))))
2141
2142 (defmacro gnus-summary-article-mark (&optional number)
2143   "Return the mark of article NUMBER."
2144   `(gnus-data-mark (gnus-data-find
2145                     ,(or number '(gnus-summary-article-number)))))
2146
2147 (defmacro gnus-summary-article-pos (&optional number)
2148   "Return the position of the line of article NUMBER."
2149   `(gnus-data-pos (gnus-data-find
2150                    ,(or number '(gnus-summary-article-number)))))
2151
2152 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2153 (defmacro gnus-summary-article-subject (&optional number)
2154   "Return current subject string or nil if nothing."
2155   `(let ((headers
2156           ,(if number
2157                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2158              '(gnus-data-header (assq (gnus-summary-article-number)
2159                                       gnus-newsgroup-data)))))
2160      (and headers
2161           (vectorp headers)
2162           (mail-header-subject headers))))
2163
2164 (defmacro gnus-summary-article-score (&optional number)
2165   "Return current article score."
2166   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2167                   gnus-newsgroup-scored))
2168        gnus-summary-default-score 0))
2169
2170 (defun gnus-summary-article-children (&optional number)
2171   "Return a list of article numbers that are children of article NUMBER."
2172   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2173          (level (gnus-data-level (car data)))
2174          l children)
2175     (while (and (setq data (cdr data))
2176                 (> (setq l (gnus-data-level (car data))) level))
2177       (and (= (1+ level) l)
2178            (push (gnus-data-number (car data))
2179                  children)))
2180     (nreverse children)))
2181
2182 (defun gnus-summary-article-parent (&optional number)
2183   "Return the article number of the parent of article NUMBER."
2184   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2185                                     (gnus-data-list t)))
2186          (level (gnus-data-level (car data))))
2187     (if (zerop level)
2188         ()                              ; This is a root.
2189       ;; We search until we find an article with a level less than
2190       ;; this one.  That function has to be the parent.
2191       (while (and (setq data (cdr data))
2192                   (not (< (gnus-data-level (car data)) level))))
2193       (and data (gnus-data-number (car data))))))
2194
2195 (defun gnus-unread-mark-p (mark)
2196   "Say whether MARK is the unread mark."
2197   (= mark gnus-unread-mark))
2198
2199 (defun gnus-read-mark-p (mark)
2200   "Say whether MARK is one of the marks that mark as read.
2201 This is all marks except unread, ticked, dormant, and expirable."
2202   (not (or (= mark gnus-unread-mark)
2203            (= mark gnus-ticked-mark)
2204            (= mark gnus-dormant-mark)
2205            (= mark gnus-expirable-mark))))
2206
2207 (defmacro gnus-article-mark (number)
2208   "Return the MARK of article NUMBER.
2209 This macro should only be used when computing the mark the \"first\"
2210 time; i.e., when generating the summary lines.  After that,
2211 `gnus-summary-article-mark' should be used to examine the
2212 marks of articles."
2213   `(cond
2214     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2215     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2216     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2217     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2218     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2219     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2220     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2221     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2222            gnus-ancient-mark))))
2223
2224 ;; Saving hidden threads.
2225
2226 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2227 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2228
2229 (defmacro gnus-save-hidden-threads (&rest forms)
2230   "Save hidden threads, eval FORMS, and restore the hidden threads."
2231   (let ((config (make-symbol "config")))
2232     `(let ((,config (gnus-hidden-threads-configuration)))
2233        (unwind-protect
2234            (save-excursion
2235              ,@forms)
2236          (gnus-restore-hidden-threads-configuration ,config)))))
2237
2238 (defun gnus-data-compute-positions ()
2239   "Compute the positions of all articles."
2240   (setq gnus-newsgroup-data-reverse nil)
2241   (let ((data gnus-newsgroup-data))
2242     (save-excursion
2243       (gnus-save-hidden-threads
2244         (gnus-summary-show-all-threads)
2245         (goto-char (point-min))
2246         (while data
2247           (while (get-text-property (point) 'gnus-intangible)
2248             (forward-line 1))
2249           (gnus-data-set-pos (car data) (+ (point) 3))
2250           (setq data (cdr data))
2251           (forward-line 1))))))
2252
2253 (defun gnus-hidden-threads-configuration ()
2254   "Return the current hidden threads configuration."
2255   (save-excursion
2256     (let (config)
2257       (goto-char (point-min))
2258       (while (search-forward "\r" nil t)
2259         (push (1- (point)) config))
2260       config)))
2261
2262 (defun gnus-restore-hidden-threads-configuration (config)
2263   "Restore hidden threads configuration from CONFIG."
2264   (let (point buffer-read-only)
2265     (while (setq point (pop config))
2266       (when (and (< point (point-max))
2267                  (goto-char point)
2268                  (= (following-char) ?\n))
2269         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2270
2271 ;; Various summary mode internalish functions.
2272
2273 (defun gnus-mouse-pick-article (e)
2274   (interactive "e")
2275   (mouse-set-point e)
2276   (gnus-summary-next-page nil t))
2277
2278 (defun gnus-summary-set-display-table ()
2279   ;; Change the display table.  Odd characters have a tendency to mess
2280   ;; up nicely formatted displays - we make all possible glyphs
2281   ;; display only a single character.
2282
2283   ;; We start from the standard display table, if any.
2284   (let ((table (or (copy-sequence standard-display-table)
2285                    (make-display-table)))
2286         (i 32))
2287     ;; Nix out all the control chars...
2288     (while (>= (setq i (1- i)) 0)
2289       (aset table i [??]))
2290     ;; ... but not newline and cr, of course.  (cr is necessary for the
2291     ;; selective display).
2292     (aset table ?\n nil)
2293     (aset table ?\r nil)
2294     ;; We keep TAB as well.
2295     (aset table ?\t nil)
2296     ;; We nix out any glyphs over 126 that are not set already.
2297     (let ((i 256))
2298       (while (>= (setq i (1- i)) 127)
2299         ;; Only modify if the entry is nil.
2300         (unless (aref table i)
2301           (aset table i [??]))))
2302     (setq buffer-display-table table)))
2303
2304 (defun gnus-summary-setup-buffer (group)
2305   "Initialize summary buffer."
2306   (let ((buffer (concat "*Summary " group "*")))
2307     (if (get-buffer buffer)
2308         (progn
2309           (set-buffer buffer)
2310           (setq gnus-summary-buffer (current-buffer))
2311           (not gnus-newsgroup-prepared))
2312       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2313       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2314       (gnus-summary-mode group)
2315       (when gnus-carpal
2316         (gnus-carpal-setup-buffer 'summary))
2317       (unless gnus-single-article-buffer
2318         (make-local-variable 'gnus-article-buffer)
2319         (make-local-variable 'gnus-article-current)
2320         (make-local-variable 'gnus-original-article-buffer))
2321       (setq gnus-newsgroup-name group)
2322       t)))
2323
2324 (defun gnus-set-global-variables ()
2325   ;; Set the global equivalents of the summary buffer-local variables
2326   ;; to the latest values they had.  These reflect the summary buffer
2327   ;; that was in action when the last article was fetched.
2328   (when (eq major-mode 'gnus-summary-mode)
2329     (setq gnus-summary-buffer (current-buffer))
2330     (let ((name gnus-newsgroup-name)
2331           (marked gnus-newsgroup-marked)
2332           (unread gnus-newsgroup-unreads)
2333           (headers gnus-current-headers)
2334           (data gnus-newsgroup-data)
2335           (summary gnus-summary-buffer)
2336           (article-buffer gnus-article-buffer)
2337           (original gnus-original-article-buffer)
2338           (gac gnus-article-current)
2339           (reffed gnus-reffed-article-number)
2340           (score-file gnus-current-score-file))
2341       (save-excursion
2342         (set-buffer gnus-group-buffer)
2343         (setq gnus-newsgroup-name name
2344               gnus-newsgroup-marked marked
2345               gnus-newsgroup-unreads unread
2346               gnus-current-headers headers
2347               gnus-newsgroup-data data
2348               gnus-article-current gac
2349               gnus-summary-buffer summary
2350               gnus-article-buffer article-buffer
2351               gnus-original-article-buffer original
2352               gnus-reffed-article-number reffed
2353               gnus-current-score-file score-file)
2354         ;; The article buffer also has local variables.
2355         (when (gnus-buffer-live-p gnus-article-buffer)
2356           (set-buffer gnus-article-buffer)
2357           (setq gnus-summary-buffer summary))))))
2358
2359 (defun gnus-summary-article-unread-p (article)
2360   "Say whether ARTICLE is unread or not."
2361   (memq article gnus-newsgroup-unreads))
2362
2363 (defun gnus-summary-first-article-p (&optional article)
2364   "Return whether ARTICLE is the first article in the buffer."
2365   (if (not (setq article (or article (gnus-summary-article-number))))
2366       nil
2367     (eq article (caar gnus-newsgroup-data))))
2368
2369 (defun gnus-summary-last-article-p (&optional article)
2370   "Return whether ARTICLE is the last article in the buffer."
2371   (if (not (setq article (or article (gnus-summary-article-number))))
2372       t         ; All non-existent numbers are the last article.  :-)
2373     (not (cdr (gnus-data-find-list article)))))
2374
2375 (defun gnus-make-thread-indent-array ()
2376   (let ((n 200))
2377     (unless (and gnus-thread-indent-array
2378                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2379       (setq gnus-thread-indent-array (make-vector 201 "")
2380             gnus-thread-indent-array-level gnus-thread-indent-level)
2381       (while (>= n 0)
2382         (aset gnus-thread-indent-array n
2383               (make-string (* n gnus-thread-indent-level) ? ))
2384         (setq n (1- n))))))
2385
2386 (defun gnus-update-summary-mark-positions ()
2387   "Compute where the summary marks are to go."
2388   (save-excursion
2389     (when (gnus-buffer-exists-p gnus-summary-buffer)
2390       (set-buffer gnus-summary-buffer))
2391     (let ((gnus-replied-mark 129)
2392           (gnus-score-below-mark 130)
2393           (gnus-score-over-mark 130)
2394           (gnus-download-mark 131)
2395           (spec gnus-summary-line-format-spec)
2396           gnus-visual pos)
2397       (save-excursion
2398         (gnus-set-work-buffer)
2399         (let ((gnus-summary-line-format-spec spec)
2400               (gnus-newsgroup-downloadable '((0 . t))))
2401           (gnus-summary-insert-line
2402            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2403           (goto-char (point-min))
2404           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2405                                              (- (point) 2)))))
2406           (goto-char (point-min))
2407           (push (cons 'replied (and (search-forward "\201" nil t)
2408                                     (- (point) 2)))
2409                 pos)
2410           (goto-char (point-min))
2411           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2412                 pos)
2413           (goto-char (point-min))
2414           (push (cons 'download
2415                       (and (search-forward "\203" nil t) (- (point) 2)))
2416                 pos)))
2417       (setq gnus-summary-mark-positions pos))))
2418
2419 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2420   "Insert a dummy root in the summary buffer."
2421   (beginning-of-line)
2422   (gnus-add-text-properties
2423    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2424    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2425
2426 (defun gnus-summary-insert-line (gnus-tmp-header
2427                                  gnus-tmp-level gnus-tmp-current
2428                                  gnus-tmp-unread gnus-tmp-replied
2429                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2430                                  &optional gnus-tmp-dummy gnus-tmp-score
2431                                  gnus-tmp-process)
2432   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2433          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2434          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2435          (gnus-tmp-score-char
2436           (if (or (null gnus-summary-default-score)
2437                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2438                       gnus-summary-zcore-fuzz))
2439               ? 
2440             (if (< gnus-tmp-score gnus-summary-default-score)
2441                 gnus-score-below-mark gnus-score-over-mark)))
2442          (gnus-tmp-replied
2443           (cond (gnus-tmp-process gnus-process-mark)
2444                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2445                  gnus-cached-mark)
2446                 (gnus-tmp-replied gnus-replied-mark)
2447                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2448                  gnus-saved-mark)
2449                 (t gnus-unread-mark)))
2450          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2451          (gnus-tmp-name
2452           (cond
2453            ((string-match "<[^>]+> *$" gnus-tmp-from)
2454             (let ((beg (match-beginning 0)))
2455               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2456                        (substring gnus-tmp-from (1+ (match-beginning 0))
2457                                   (1- (match-end 0))))
2458                   (substring gnus-tmp-from 0 beg))))
2459            ((string-match "(.+)" gnus-tmp-from)
2460             (substring gnus-tmp-from
2461                        (1+ (match-beginning 0)) (1- (match-end 0))))
2462            (t gnus-tmp-from)))
2463          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2464          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2465          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2466          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2467          (buffer-read-only nil))
2468     (when (string= gnus-tmp-name "")
2469       (setq gnus-tmp-name gnus-tmp-from))
2470     (unless (numberp gnus-tmp-lines)
2471       (setq gnus-tmp-lines 0))
2472     (gnus-put-text-property
2473      (point)
2474      (progn (eval gnus-summary-line-format-spec) (point))
2475      'gnus-number gnus-tmp-number)
2476     (when (gnus-visual-p 'summary-highlight 'highlight)
2477       (forward-line -1)
2478       (gnus-run-hooks 'gnus-summary-update-hook)
2479       (forward-line 1))))
2480
2481 (defun gnus-summary-update-line (&optional dont-update)
2482   ;; Update summary line after change.
2483   (when (and gnus-summary-default-score
2484              (not gnus-summary-inhibit-highlight))
2485     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2486            (article (gnus-summary-article-number))
2487            (score (gnus-summary-article-score article)))
2488       (unless dont-update
2489         (if (and gnus-summary-mark-below
2490                  (< (gnus-summary-article-score)
2491                     gnus-summary-mark-below))
2492             ;; This article has a low score, so we mark it as read.
2493             (when (memq article gnus-newsgroup-unreads)
2494               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2495           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2496             ;; This article was previously marked as read on account
2497             ;; of a low score, but now it has risen, so we mark it as
2498             ;; unread.
2499             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2500         (gnus-summary-update-mark
2501          (if (or (null gnus-summary-default-score)
2502                  (<= (abs (- score gnus-summary-default-score))
2503                      gnus-summary-zcore-fuzz))
2504              ? 
2505            (if (< score gnus-summary-default-score)
2506                gnus-score-below-mark gnus-score-over-mark))
2507          'score))
2508       ;; Do visual highlighting.
2509       (when (gnus-visual-p 'summary-highlight 'highlight)
2510         (gnus-run-hooks 'gnus-summary-update-hook)))))
2511
2512 (defvar gnus-tmp-new-adopts nil)
2513
2514 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2515   "Return the number of articles in THREAD.
2516 This may be 0 in some cases -- if none of the articles in
2517 the thread are to be displayed."
2518   (let* ((number
2519           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2520           (cond
2521            ((not (listp thread))
2522             1)
2523            ((and (consp thread) (cdr thread))
2524             (apply
2525              '+ 1 (mapcar
2526                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2527            ((null thread)
2528             1)
2529            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2530             1)
2531            (t 0))))
2532     (when (and level (zerop level) gnus-tmp-new-adopts)
2533       (incf number
2534             (apply '+ (mapcar
2535                        'gnus-summary-number-of-articles-in-thread
2536                        gnus-tmp-new-adopts))))
2537     (if char
2538         (if (> number 1) gnus-not-empty-thread-mark
2539           gnus-empty-thread-mark)
2540       number)))
2541
2542 (defun gnus-summary-set-local-parameters (group)
2543   "Go through the local params of GROUP and set all variable specs in that list."
2544   (let ((params (gnus-group-find-parameter group))
2545         elem)
2546     (while params
2547       (setq elem (car params)
2548             params (cdr params))
2549       (and (consp elem)                 ; Has to be a cons.
2550            (consp (cdr elem))           ; The cdr has to be a list.
2551            (symbolp (car elem))         ; Has to be a symbol in there.
2552            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2553            (ignore-errors               ; So we set it.
2554              (make-local-variable (car elem))
2555              (set (car elem) (eval (nth 1 elem))))))))
2556
2557 (defun gnus-summary-read-group (group &optional show-all no-article
2558                                       kill-buffer no-display backward
2559                                       select-articles)
2560   "Start reading news in newsgroup GROUP.
2561 If SHOW-ALL is non-nil, already read articles are also listed.
2562 If NO-ARTICLE is non-nil, no article is selected initially.
2563 If NO-DISPLAY, don't generate a summary buffer."
2564   (let (result)
2565     (while (and group
2566                 (null (setq result
2567                             (let ((gnus-auto-select-next nil))
2568                               (or (gnus-summary-read-group-1
2569                                    group show-all no-article
2570                                    kill-buffer no-display
2571                                    select-articles)
2572                                   (setq show-all nil
2573                                    select-articles nil)))))
2574                 (eq gnus-auto-select-next 'quietly))
2575       (set-buffer gnus-group-buffer)
2576       ;; The entry function called above goes to the next
2577       ;; group automatically, so we go two groups back
2578       ;; if we are searching for the previous group.
2579       (when backward
2580         (gnus-group-prev-unread-group 2))
2581       (if (not (equal group (gnus-group-group-name)))
2582           (setq group (gnus-group-group-name))
2583         (setq group nil)))
2584     result))
2585
2586 (defun gnus-summary-read-group-1 (group show-all no-article
2587                                         kill-buffer no-display
2588                                         &optional select-articles)
2589   ;; Killed foreign groups can't be entered.
2590   (when (and (not (gnus-group-native-p group))
2591              (not (gnus-gethash group gnus-newsrc-hashtb)))
2592     (error "Dead non-native groups can't be entered"))
2593   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2594   (let* ((new-group (gnus-summary-setup-buffer group))
2595          (quit-config (gnus-group-quit-config group))
2596          (did-select (and new-group (gnus-select-newsgroup
2597                                      group show-all select-articles))))
2598     (cond
2599      ;; This summary buffer exists already, so we just select it.
2600      ((not new-group)
2601       (gnus-set-global-variables)
2602       (when kill-buffer
2603         (gnus-kill-or-deaden-summary kill-buffer))
2604       (gnus-configure-windows 'summary 'force)
2605       (gnus-set-mode-line 'summary)
2606       (gnus-summary-position-point)
2607       (message "")
2608       t)
2609      ;; We couldn't select this group.
2610      ((null did-select)
2611       (when (and (eq major-mode 'gnus-summary-mode)
2612                  (not (equal (current-buffer) kill-buffer)))
2613         (kill-buffer (current-buffer))
2614         (if (not quit-config)
2615             (progn
2616               ;; Update the info -- marks might need to be removed,
2617               ;; for instance.
2618               (gnus-summary-update-info)
2619               (set-buffer gnus-group-buffer)
2620               (gnus-group-jump-to-group group)
2621               (gnus-group-next-unread-group 1))
2622           (gnus-handle-ephemeral-exit quit-config)))
2623       (gnus-message 3 "Can't select group")
2624       nil)
2625      ;; The user did a `C-g' while prompting for number of articles,
2626      ;; so we exit this group.
2627      ((eq did-select 'quit)
2628       (and (eq major-mode 'gnus-summary-mode)
2629            (not (equal (current-buffer) kill-buffer))
2630            (kill-buffer (current-buffer)))
2631       (when kill-buffer
2632         (gnus-kill-or-deaden-summary kill-buffer))
2633       (if (not quit-config)
2634           (progn
2635             (set-buffer gnus-group-buffer)
2636             (gnus-group-jump-to-group group)
2637             (gnus-group-next-unread-group 1)
2638             (gnus-configure-windows 'group 'force))
2639         (gnus-handle-ephemeral-exit quit-config))
2640       ;; Finally signal the quit.
2641       (signal 'quit nil))
2642      ;; The group was successfully selected.
2643      (t
2644       (gnus-set-global-variables)
2645       ;; Save the active value in effect when the group was entered.
2646       (setq gnus-newsgroup-active
2647             (gnus-copy-sequence
2648              (gnus-active gnus-newsgroup-name)))
2649       ;; You can change the summary buffer in some way with this hook.
2650       (gnus-run-hooks 'gnus-select-group-hook)
2651       ;; Set any local variables in the group parameters.
2652       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2653       (gnus-update-format-specifications
2654        nil 'summary 'summary-mode 'summary-dummy)
2655       ;; Do score processing.
2656       (when gnus-use-scoring
2657         (gnus-possibly-score-headers))
2658       ;; Check whether to fill in the gaps in the threads.
2659       (when gnus-build-sparse-threads
2660         (gnus-build-sparse-threads))
2661       ;; Find the initial limit.
2662       (if gnus-show-threads
2663           (if show-all
2664               (let ((gnus-newsgroup-dormant nil))
2665                 (gnus-summary-initial-limit show-all))
2666             (gnus-summary-initial-limit show-all))
2667         (setq gnus-newsgroup-limit
2668               (mapcar
2669                (lambda (header) (mail-header-number header))
2670                gnus-newsgroup-headers)))
2671       ;; Generate the summary buffer.
2672       (unless no-display
2673         (gnus-summary-prepare))
2674       (when gnus-use-trees
2675         (gnus-tree-open group)
2676         (setq gnus-summary-highlight-line-function
2677               'gnus-tree-highlight-article))
2678       ;; If the summary buffer is empty, but there are some low-scored
2679       ;; articles or some excluded dormants, we include these in the
2680       ;; buffer.
2681       (when (and (zerop (buffer-size))
2682                  (not no-display))
2683         (cond (gnus-newsgroup-dormant
2684                (gnus-summary-limit-include-dormant))
2685               ((and gnus-newsgroup-scored show-all)
2686                (gnus-summary-limit-include-expunged t))))
2687       ;; Function `gnus-apply-kill-file' must be called in this hook.
2688       (gnus-run-hooks 'gnus-apply-kill-hook)
2689       (if (and (zerop (buffer-size))
2690                (not no-display))
2691           (progn
2692             ;; This newsgroup is empty.
2693             (gnus-summary-catchup-and-exit nil t)
2694             (gnus-message 6 "No unread news")
2695             (when kill-buffer
2696               (gnus-kill-or-deaden-summary kill-buffer))
2697             ;; Return nil from this function.
2698             nil)
2699         ;; Hide conversation thread subtrees.  We cannot do this in
2700         ;; gnus-summary-prepare-hook since kill processing may not
2701         ;; work with hidden articles.
2702         (and gnus-show-threads
2703              gnus-thread-hide-subtree
2704              (gnus-summary-hide-all-threads))
2705         (when kill-buffer
2706           (gnus-kill-or-deaden-summary kill-buffer))
2707         ;; Show first unread article if requested.
2708         (if (and (not no-article)
2709                  (not no-display)
2710                  gnus-newsgroup-unreads
2711                  gnus-auto-select-first)
2712             (progn
2713               (gnus-configure-windows 'summary)
2714               (cond
2715                ((eq gnus-auto-select-first 'best)
2716                 (gnus-summary-best-unread-article))
2717                ((eq gnus-auto-select-first t)
2718                 (gnus-summary-first-unread-article))
2719                ((gnus-functionp gnus-auto-select-first)
2720                 (funcall gnus-auto-select-first))))
2721           ;; Don't select any articles, just move point to the first
2722           ;; article in the group.
2723           (goto-char (point-min))
2724           (gnus-summary-position-point)
2725           (gnus-configure-windows 'summary 'force)
2726           (gnus-set-mode-line 'summary))
2727         (when (get-buffer-window gnus-group-buffer t)
2728           ;; Gotta use windows, because recenter does weird stuff if
2729           ;; the current buffer ain't the displayed window.
2730           (let ((owin (selected-window)))
2731             (select-window (get-buffer-window gnus-group-buffer t))
2732             (when (gnus-group-goto-group group)
2733               (recenter))
2734             (select-window owin)))
2735         ;; Mark this buffer as "prepared".
2736         (setq gnus-newsgroup-prepared t)
2737         (gnus-run-hooks 'gnus-summary-prepared-hook)
2738         t)))))
2739
2740 (defun gnus-summary-prepare ()
2741   "Generate the summary buffer."
2742   (interactive)
2743   (let ((buffer-read-only nil))
2744     (erase-buffer)
2745     (setq gnus-newsgroup-data nil
2746           gnus-newsgroup-data-reverse nil)
2747     (gnus-run-hooks 'gnus-summary-generate-hook)
2748     ;; Generate the buffer, either with threads or without.
2749     (when gnus-newsgroup-headers
2750       (gnus-summary-prepare-threads
2751        (if gnus-show-threads
2752            (gnus-sort-gathered-threads
2753             (funcall gnus-summary-thread-gathering-function
2754                      (gnus-sort-threads
2755                       (gnus-cut-threads (gnus-make-threads)))))
2756          ;; Unthreaded display.
2757          (gnus-sort-articles gnus-newsgroup-headers))))
2758     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2759     ;; Call hooks for modifying summary buffer.
2760     (goto-char (point-min))
2761     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2762
2763 (defsubst gnus-general-simplify-subject (subject)
2764   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2765   (setq subject
2766         (cond
2767          ;; Truncate the subject.
2768          (gnus-simplify-subject-functions
2769           (gnus-map-function gnus-simplify-subject-functions subject))
2770          ((numberp gnus-summary-gather-subject-limit)
2771           (setq subject (gnus-simplify-subject-re subject))
2772           (if (> (length subject) gnus-summary-gather-subject-limit)
2773               (substring subject 0 gnus-summary-gather-subject-limit)
2774             subject))
2775          ;; Fuzzily simplify it.
2776          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2777           (gnus-simplify-subject-fuzzy subject))
2778          ;; Just remove the leading "Re:".
2779          (t
2780           (gnus-simplify-subject-re subject))))
2781
2782   (if (and gnus-summary-gather-exclude-subject
2783            (string-match gnus-summary-gather-exclude-subject subject))
2784       nil                               ; This article shouldn't be gathered
2785     subject))
2786
2787 (defun gnus-summary-simplify-subject-query ()
2788   "Query where the respool algorithm would put this article."
2789   (interactive)
2790   (gnus-summary-select-article)
2791   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2792
2793 (defun gnus-gather-threads-by-subject (threads)
2794   "Gather threads by looking at Subject headers."
2795   (if (not gnus-summary-make-false-root)
2796       threads
2797     (let ((hashtb (gnus-make-hashtable 1024))
2798           (prev threads)
2799           (result threads)
2800           subject hthread whole-subject)
2801       (while threads
2802         (setq subject (gnus-general-simplify-subject
2803                        (setq whole-subject (mail-header-subject
2804                                             (caar threads)))))
2805         (when subject
2806           (if (setq hthread (gnus-gethash subject hashtb))
2807               (progn
2808                 ;; We enter a dummy root into the thread, if we
2809                 ;; haven't done that already.
2810                 (unless (stringp (caar hthread))
2811                   (setcar hthread (list whole-subject (car hthread))))
2812                 ;; We add this new gathered thread to this gathered
2813                 ;; thread.
2814                 (setcdr (car hthread)
2815                         (nconc (cdar hthread) (list (car threads))))
2816                 ;; Remove it from the list of threads.
2817                 (setcdr prev (cdr threads))
2818                 (setq threads prev))
2819             ;; Enter this thread into the hash table.
2820             (gnus-sethash subject threads hashtb)))
2821         (setq prev threads)
2822         (setq threads (cdr threads)))
2823       result)))
2824
2825 (defun gnus-gather-threads-by-references (threads)
2826   "Gather threads by looking at References headers."
2827   (let ((idhashtb (gnus-make-hashtable 1024))
2828         (thhashtb (gnus-make-hashtable 1024))
2829         (prev threads)
2830         (result threads)
2831         ids references id gthread gid entered ref)
2832     (while threads
2833       (when (setq references (mail-header-references (caar threads)))
2834         (setq id (mail-header-id (caar threads))
2835               ids (gnus-split-references references)
2836               entered nil)
2837         (while (setq ref (pop ids))
2838           (setq ids (delete ref ids))
2839           (if (not (setq gid (gnus-gethash ref idhashtb)))
2840               (progn
2841                 (gnus-sethash ref id idhashtb)
2842                 (gnus-sethash id threads thhashtb))
2843             (setq gthread (gnus-gethash gid thhashtb))
2844             (unless entered
2845               ;; We enter a dummy root into the thread, if we
2846               ;; haven't done that already.
2847               (unless (stringp (caar gthread))
2848                 (setcar gthread (list (mail-header-subject (caar gthread))
2849                                       (car gthread))))
2850               ;; We add this new gathered thread to this gathered
2851               ;; thread.
2852               (setcdr (car gthread)
2853                       (nconc (cdar gthread) (list (car threads)))))
2854             ;; Add it into the thread hash table.
2855             (gnus-sethash id gthread thhashtb)
2856             (setq entered t)
2857             ;; Remove it from the list of threads.
2858             (setcdr prev (cdr threads))
2859             (setq threads prev))))
2860       (setq prev threads)
2861       (setq threads (cdr threads)))
2862     result))
2863
2864 (defun gnus-sort-gathered-threads (threads)
2865   "Sort subtreads inside each gathered thread by article number."
2866   (let ((result threads))
2867     (while threads
2868       (when (stringp (caar threads))
2869         (setcdr (car threads)
2870                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2871       (setq threads (cdr threads)))
2872     result))
2873
2874 (defun gnus-thread-loop-p (root thread)
2875   "Say whether ROOT is in THREAD."
2876   (let ((stack (list thread))
2877         (infloop 0)
2878         th)
2879     (while (setq thread (pop stack))
2880       (setq th (cdr thread))
2881       (while (and th
2882                   (not (eq (caar th) root)))
2883         (pop th))
2884       (if th
2885           ;; We have found a loop.
2886           (let (ref-dep)
2887             (setcdr thread (delq (car th) (cdr thread)))
2888             (if (boundp (setq ref-dep (intern "none"
2889                                               gnus-newsgroup-dependencies)))
2890                 (setcdr (symbol-value ref-dep)
2891                         (nconc (cdr (symbol-value ref-dep))
2892                                (list (car th))))
2893               (set ref-dep (list nil (car th))))
2894             (setq infloop 1
2895                   stack nil))
2896         ;; Push all the subthreads onto the stack.
2897         (push (cdr thread) stack)))
2898     infloop))
2899
2900 (defun gnus-make-threads ()
2901   "Go through the dependency hashtb and find the roots.  Return all threads."
2902   (let (threads)
2903     (while (catch 'infloop
2904              (mapatoms
2905               (lambda (refs)
2906                 ;; Deal with self-referencing References loops.
2907                 (when (and (car (symbol-value refs))
2908                            (not (zerop
2909                                  (apply
2910                                   '+
2911                                   (mapcar
2912                                    (lambda (thread)
2913                                      (gnus-thread-loop-p
2914                                       (car (symbol-value refs)) thread))
2915                                    (cdr (symbol-value refs)))))))
2916                   (setq threads nil)
2917                   (throw 'infloop t))
2918                 (unless (car (symbol-value refs))
2919                   ;; These threads do not refer back to any other articles,
2920                   ;; so they're roots.
2921                   (setq threads (append (cdr (symbol-value refs)) threads))))
2922               gnus-newsgroup-dependencies)))
2923     threads))
2924
2925 ;; Build the thread tree.
2926 (defsubst gnus-dependencies-add-header (header dependencies force-new)
2927   "Enter HEADER into the DEPENDENCIES table if it is not already there.
2928
2929 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
2930 if it was already present.
2931
2932 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
2933 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
2934 Message-IDs will be renamed be renamed to a unique Message-ID before
2935 being entered.
2936
2937 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
2938   (let* ((id (mail-header-id header))
2939          (id-dep (and id (intern id dependencies)))
2940          ref ref-dep ref-header)
2941     ;; Enter this `header' in the `dependencies' table.
2942     (cond
2943      ((not id-dep)
2944       (setq header nil))
2945      ;; The first two cases do the normal part: enter a new `header'
2946      ;; in the `dependencies' table.
2947      ((not (boundp id-dep))
2948       (set id-dep (list header)))
2949      ((null (car (symbol-value id-dep)))
2950       (setcar (symbol-value id-dep) header))
2951
2952      ;; From here the `header' was already present in the
2953      ;; `dependencies' table.
2954      (force-new
2955       ;; Overrides an existing entry;
2956       ;; just set the header part of the entry.
2957       (setcar (symbol-value id-dep) header))
2958
2959      ;; Renames the existing `header' to a unique Message-ID.
2960      ((not gnus-summary-ignore-duplicates)
2961       ;; An article with this Message-ID has already been seen.
2962       ;; We rename the Message-ID.
2963       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
2964            (list header))
2965       (mail-header-set-id header id))
2966
2967      ;; The last case ignores an existing entry, except it adds any
2968      ;; additional Xrefs (in case the two articles came from different
2969      ;; servers.
2970      ;; Also sets `header' to `nil' meaning that the `dependencies'
2971      ;; table was *not* modified.
2972      (t
2973       (mail-header-set-xref
2974        (car (symbol-value id-dep))
2975        (concat (or (mail-header-xref (car (symbol-value id-dep)))
2976                    "")
2977                (or (mail-header-xref header) "")))
2978       (setq header nil)))
2979
2980     (when header
2981       ;; First check if that we are not creating a References loop.
2982       (setq ref (gnus-parent-id (mail-header-references header)))
2983       (while (and ref
2984                   (setq ref-dep (intern-soft ref dependencies))
2985                   (boundp ref-dep)
2986                   (setq ref-header (car (symbol-value ref-dep))))
2987         (if (string= id ref)
2988             ;; Yuk!  This is a reference loop.  Make the article be a
2989             ;; root article.
2990             (progn
2991               (mail-header-set-references (car (symbol-value id-dep)) "none")
2992               (setq ref nil))
2993           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
2994       (setq ref (gnus-parent-id (mail-header-references header)))
2995       (setq ref-dep (intern (or ref "none") dependencies))
2996       (if (boundp ref-dep)
2997           (setcdr (symbol-value ref-dep)
2998                   (nconc (cdr (symbol-value ref-dep))
2999                          (list (symbol-value id-dep))))
3000         (set ref-dep (list nil (symbol-value id-dep)))))
3001     header))
3002
3003 (defun gnus-build-sparse-threads ()
3004   (let ((headers gnus-newsgroup-headers)
3005         (gnus-summary-ignore-duplicates t)
3006         header references generation relations
3007         subject child end new-child date)
3008     ;; First we create an alist of generations/relations, where
3009     ;; generations is how much we trust the relation, and the relation
3010     ;; is parent/child.
3011     (gnus-message 7 "Making sparse threads...")
3012     (save-excursion
3013       (nnheader-set-temp-buffer " *gnus sparse threads*")
3014       (while (setq header (pop headers))
3015         (when (and (setq references (mail-header-references header))
3016                    (not (string= references "")))
3017           (insert references)
3018           (setq child (mail-header-id header)
3019                 subject (mail-header-subject header)
3020                 date (mail-header-date header)
3021                 generation 0)
3022           (while (search-backward ">" nil t)
3023             (setq end (1+ (point)))
3024             (when (search-backward "<" nil t)
3025               (setq new-child (buffer-substring (point) end))
3026               (push (list (incf generation)
3027                           child (setq child new-child)
3028                           subject date)
3029                     relations)))
3030           (when child
3031             (push (list (1+ generation) child nil subject) relations))
3032           (erase-buffer)))
3033       (kill-buffer (current-buffer)))
3034     ;; Sort over trustworthiness.
3035     (mapcar
3036      (lambda (relation)
3037        (when (gnus-dependencies-add-header
3038               (make-full-mail-header
3039                gnus-reffed-article-number
3040                (nth 3 relation) "" (or (nth 4 relation) "")
3041                (nth 1 relation)
3042                (or (nth 2 relation) "") 0 0 "")
3043               gnus-newsgroup-dependencies nil)
3044          (push gnus-reffed-article-number gnus-newsgroup-limit)
3045          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3046          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3047                gnus-newsgroup-reads)
3048          (decf gnus-reffed-article-number)))
3049      (sort relations 'car-less-than-car))
3050     (gnus-message 7 "Making sparse threads...done")))
3051
3052 (defun gnus-build-old-threads ()
3053   ;; Look at all the articles that refer back to old articles, and
3054   ;; fetch the headers for the articles that aren't there.  This will
3055   ;; build complete threads - if the roots haven't been expired by the
3056   ;; server, that is.
3057   (let (id heads)
3058     (mapatoms
3059      (lambda (refs)
3060        (when (not (car (symbol-value refs)))
3061          (setq heads (cdr (symbol-value refs)))
3062          (while heads
3063            (if (memq (mail-header-number (caar heads))
3064                      gnus-newsgroup-dormant)
3065                (setq heads (cdr heads))
3066              (setq id (symbol-name refs))
3067              (while (and (setq id (gnus-build-get-header id))
3068                          (not (car (gnus-id-to-thread id)))))
3069              (setq heads nil)))))
3070      gnus-newsgroup-dependencies)))
3071
3072 ;; The following macros and functions were written by Felix Lee
3073 ;; <flee@cse.psu.edu>.
3074
3075 (defmacro gnus-nov-read-integer ()
3076   '(prog1
3077        (if (= (following-char) ?\t)
3078            0
3079          (let ((num (ignore-errors (read buffer))))
3080            (if (numberp num) num 0)))
3081      (unless (eobp)
3082        (search-forward "\t" eol 'move))))
3083
3084 (defmacro gnus-nov-skip-field ()
3085   '(search-forward "\t" eol 'move))
3086
3087 (defmacro gnus-nov-field ()
3088   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
3089
3090 ;; This function has to be called with point after the article number
3091 ;; on the beginning of the line.
3092 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3093   (let ((eol (gnus-point-at-eol))
3094         (buffer (current-buffer))
3095         header rawtext decoded)
3096
3097     ;; overview: [num subject from date id refs chars lines misc]
3098     (unwind-protect
3099         (progn
3100           (narrow-to-region (point) eol)
3101           (unless (eobp)
3102             (forward-char))
3103
3104           (setq header
3105                 (make-full-mail-header
3106                  number                 ; number
3107                  (progn
3108                    (setq rawtext (gnus-nov-field) ; subject
3109                          decoded (funcall
3110                                   gnus-unstructured-field-decoder rawtext))
3111                    (if (string= rawtext decoded)
3112                        rawtext
3113                      (put-text-property 0 (length decoded) 'raw-text rawtext decoded)
3114                      decoded))
3115                  (progn
3116                    (setq rawtext (gnus-nov-field) ; from
3117                          decoded (funcall
3118                                   gnus-structured-field-decoder rawtext))
3119                    (if (string= rawtext decoded)
3120                        rawtext
3121                      (put-text-property 0 (length decoded) 'raw-text rawtext decoded)
3122                      decoded))
3123                  (gnus-nov-field)       ; date
3124                  (or (gnus-nov-field)
3125                      (nnheader-generate-fake-message-id)) ; id
3126                  (gnus-nov-field)       ; refs
3127                  (gnus-nov-read-integer) ; chars
3128                  (gnus-nov-read-integer) ; lines
3129                  (unless (= (following-char) ?\n)
3130                    (gnus-nov-field))))) ; misc
3131
3132       (widen))
3133
3134     (when gnus-alter-header-function
3135       (funcall gnus-alter-header-function header))
3136     (gnus-dependencies-add-header header dependencies force-new)))
3137
3138 (defun gnus-build-get-header (id)
3139   ;; Look through the buffer of NOV lines and find the header to
3140   ;; ID.  Enter this line into the dependencies hash table, and return
3141   ;; the id of the parent article (if any).
3142   (let ((deps gnus-newsgroup-dependencies)
3143         found header)
3144     (prog1
3145         (save-excursion
3146           (set-buffer nntp-server-buffer)
3147           (let ((case-fold-search nil))
3148             (goto-char (point-min))
3149             (while (and (not found)
3150                         (search-forward id nil t))
3151               (beginning-of-line)
3152               (setq found (looking-at
3153                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3154                                    (regexp-quote id))))
3155               (or found (beginning-of-line 2)))
3156             (when found
3157               (beginning-of-line)
3158               (and
3159                (setq header (gnus-nov-parse-line
3160                              (read (current-buffer)) deps))
3161                (gnus-parent-id (mail-header-references header))))))
3162       (when header
3163         (let ((number (mail-header-number header)))
3164           (push number gnus-newsgroup-limit)
3165           (push header gnus-newsgroup-headers)
3166           (if (memq number gnus-newsgroup-unselected)
3167               (progn
3168                 (push number gnus-newsgroup-unreads)
3169                 (setq gnus-newsgroup-unselected
3170                       (delq number gnus-newsgroup-unselected)))
3171             (push number gnus-newsgroup-ancient)))))))
3172
3173 (defun gnus-build-all-threads ()
3174   "Read all the headers."
3175   (let ((gnus-summary-ignore-duplicates t)
3176         (dependencies gnus-newsgroup-dependencies)
3177         header article)
3178     (save-excursion
3179       (set-buffer nntp-server-buffer)
3180       (let ((case-fold-search nil))
3181         (goto-char (point-min))
3182         (while (not (eobp))
3183           (ignore-errors
3184             (setq article (read (current-buffer))
3185                   header (gnus-nov-parse-line
3186                           article dependencies)))
3187           (when header
3188             (save-excursion
3189               (set-buffer gnus-summary-buffer)
3190               (push header gnus-newsgroup-headers)
3191               (if (memq (setq article (mail-header-number header))
3192                         gnus-newsgroup-unselected)
3193                   (progn
3194                     (push article gnus-newsgroup-unreads)
3195                     (setq gnus-newsgroup-unselected
3196                           (delq article gnus-newsgroup-unselected)))
3197                 (push article gnus-newsgroup-ancient)))
3198             (forward-line 1)))))))
3199
3200 (defun gnus-summary-update-article-line (article header)
3201   "Update the line for ARTICLE using HEADERS."
3202   (let* ((id (mail-header-id header))
3203          (thread (gnus-id-to-thread id)))
3204     (unless thread
3205       (error "Article in no thread"))
3206     ;; Update the thread.
3207     (setcar thread header)
3208     (gnus-summary-goto-subject article)
3209     (let* ((datal (gnus-data-find-list article))
3210            (data (car datal))
3211            (length (when (cdr datal)
3212                      (- (gnus-data-pos data)
3213                         (gnus-data-pos (cadr datal)))))
3214            (buffer-read-only nil)
3215            (level (gnus-summary-thread-level)))
3216       (gnus-delete-line)
3217       (gnus-summary-insert-line
3218        header level nil (gnus-article-mark article)
3219        (memq article gnus-newsgroup-replied)
3220        (memq article gnus-newsgroup-expirable)
3221        ;; Only insert the Subject string when it's different
3222        ;; from the previous Subject string.
3223        (if (gnus-subject-equal
3224             (condition-case ()
3225                 (mail-header-subject
3226                  (gnus-data-header
3227                   (cadr
3228                    (gnus-data-find-list
3229                     article
3230                     (gnus-data-list t)))))
3231               ;; Error on the side of excessive subjects.
3232               (error ""))
3233             (mail-header-subject header))
3234            ""
3235          (mail-header-subject header))
3236        nil (cdr (assq article gnus-newsgroup-scored))
3237        (memq article gnus-newsgroup-processable))
3238       (when length
3239         (gnus-data-update-list
3240          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3241
3242 (defun gnus-summary-update-article (article &optional iheader)
3243   "Update ARTICLE in the summary buffer."
3244   (set-buffer gnus-summary-buffer)
3245   (let* ((header (gnus-summary-article-header article))
3246          (id (mail-header-id header))
3247          (data (gnus-data-find article))
3248          (thread (gnus-id-to-thread id))
3249          (references (mail-header-references header))
3250          (parent
3251           (gnus-id-to-thread
3252            (or (gnus-parent-id
3253                 (when (and references
3254                            (not (equal "" references)))
3255                   references))
3256                "none")))
3257          (buffer-read-only nil)
3258          (old (car thread)))
3259     (when thread
3260       (unless iheader
3261         (setcar thread nil)
3262         (when parent
3263           (delq thread parent)))
3264       (if (gnus-summary-insert-subject id header)
3265           ;; Set the (possibly) new article number in the data structure.
3266           (gnus-data-set-number data (gnus-id-to-article id))
3267         (setcar thread old)
3268         nil))))
3269
3270 (defun gnus-rebuild-thread (id &optional line)
3271   "Rebuild the thread containing ID.
3272 If LINE, insert the rebuilt thread starting on line LINE."
3273   (let ((buffer-read-only nil)
3274         old-pos current thread data)
3275     (if (not gnus-show-threads)
3276         (setq thread (list (car (gnus-id-to-thread id))))
3277       ;; Get the thread this article is part of.
3278       (setq thread (gnus-remove-thread id)))
3279     (setq old-pos (gnus-point-at-bol))
3280     (setq current (save-excursion
3281                     (and (zerop (forward-line -1))
3282                          (gnus-summary-article-number))))
3283     ;; If this is a gathered thread, we have to go some re-gathering.
3284     (when (stringp (car thread))
3285       (let ((subject (car thread))
3286             roots thr)
3287         (setq thread (cdr thread))
3288         (while thread
3289           (unless (memq (setq thr (gnus-id-to-thread
3290                                    (gnus-root-id
3291                                     (mail-header-id (caar thread)))))
3292                         roots)
3293             (push thr roots))
3294           (setq thread (cdr thread)))
3295         ;; We now have all (unique) roots.
3296         (if (= (length roots) 1)
3297             ;; All the loose roots are now one solid root.
3298             (setq thread (car roots))
3299           (setq thread (cons subject (gnus-sort-threads roots))))))
3300     (let (threads)
3301       ;; We then insert this thread into the summary buffer.
3302       (when line
3303         (goto-char (point-min))
3304         (forward-line (1- line)))
3305       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3306         (if gnus-show-threads
3307             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3308           (gnus-summary-prepare-unthreaded thread))
3309         (setq data (nreverse gnus-newsgroup-data))
3310         (setq threads gnus-newsgroup-threads))
3311       ;; We splice the new data into the data structure.
3312       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3313       ;;!!! then we want to insert at the beginning of the buffer.
3314       ;;!!! That happens to be true with Gnus now, but that may
3315       ;;!!! change in the future.  Perhaps.
3316       (gnus-data-enter-list
3317        (if line nil current) data (- (point) old-pos))
3318       (setq gnus-newsgroup-threads
3319             (nconc threads gnus-newsgroup-threads))
3320       (gnus-data-compute-positions))))
3321
3322 (defun gnus-number-to-header (number)
3323   "Return the header for article NUMBER."
3324   (let ((headers gnus-newsgroup-headers))
3325     (while (and headers
3326                 (not (= number (mail-header-number (car headers)))))
3327       (pop headers))
3328     (when headers
3329       (car headers))))
3330
3331 (defun gnus-parent-headers (in-headers &optional generation)
3332   "Return the headers of the GENERATIONeth parent of HEADERS."
3333   (unless generation
3334     (setq generation 1))
3335   (let ((parent t)
3336         (headers in-headers)
3337         references)
3338     (while (and parent
3339                 (not (zerop generation))
3340                 (setq references (mail-header-references headers)))
3341       (setq headers (if (and references
3342                              (setq parent (gnus-parent-id references)))
3343                         (car (gnus-id-to-thread parent))
3344                       nil))
3345       (decf generation))
3346     (and (not (eq headers in-headers))
3347          headers)))
3348
3349 (defun gnus-id-to-thread (id)
3350   "Return the (sub-)thread where ID appears."
3351   (gnus-gethash id gnus-newsgroup-dependencies))
3352
3353 (defun gnus-id-to-article (id)
3354   "Return the article number of ID."
3355   (let ((thread (gnus-id-to-thread id)))
3356     (when (and thread
3357                (car thread))
3358       (mail-header-number (car thread)))))
3359
3360 (defun gnus-id-to-header (id)
3361   "Return the article headers of ID."
3362   (car (gnus-id-to-thread id)))
3363
3364 (defun gnus-article-displayed-root-p (article)
3365   "Say whether ARTICLE is a root(ish) article."
3366   (let ((level (gnus-summary-thread-level article))
3367         (refs (mail-header-references  (gnus-summary-article-header article)))
3368         particle)
3369     (cond
3370      ((null level) nil)
3371      ((zerop level) t)
3372      ((null refs) t)
3373      ((null (gnus-parent-id refs)) t)
3374      ((and (= 1 level)
3375            (null (setq particle (gnus-id-to-article
3376                                  (gnus-parent-id refs))))
3377            (null (gnus-summary-thread-level particle)))))))
3378
3379 (defun gnus-root-id (id)
3380   "Return the id of the root of the thread where ID appears."
3381   (let (last-id prev)
3382     (while (and id (setq prev (car (gnus-id-to-thread id))))
3383       (setq last-id id
3384             id (gnus-parent-id (mail-header-references prev))))
3385     last-id))
3386
3387 (defun gnus-articles-in-thread (thread)
3388   "Return the list of articles in THREAD."
3389   (cons (mail-header-number (car thread))
3390         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3391
3392 (defun gnus-remove-thread (id &optional dont-remove)
3393   "Remove the thread that has ID in it."
3394   (let (headers thread last-id)
3395     ;; First go up in this thread until we find the root.
3396     (setq last-id (gnus-root-id id)
3397           headers (message-flatten-list (gnus-id-to-thread last-id)))
3398     ;; We have now found the real root of this thread.  It might have
3399     ;; been gathered into some loose thread, so we have to search
3400     ;; through the threads to find the thread we wanted.
3401     (let ((threads gnus-newsgroup-threads)
3402           sub)
3403       (while threads
3404         (setq sub (car threads))
3405         (if (stringp (car sub))
3406             ;; This is a gathered thread, so we look at the roots
3407             ;; below it to find whether this article is in this
3408             ;; gathered root.
3409             (progn
3410               (setq sub (cdr sub))
3411               (while sub
3412                 (when (member (caar sub) headers)
3413                   (setq thread (car threads)
3414                         threads nil
3415                         sub nil))
3416                 (setq sub (cdr sub))))
3417           ;; It's an ordinary thread, so we check it.
3418           (when (eq (car sub) (car headers))
3419             (setq thread sub
3420                   threads nil)))
3421         (setq threads (cdr threads)))
3422       ;; If this article is in no thread, then it's a root.
3423       (if thread
3424           (unless dont-remove
3425             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3426         (setq thread (gnus-id-to-thread last-id)))
3427       (when thread
3428         (prog1
3429             thread                      ; We return this thread.
3430           (unless dont-remove
3431             (if (stringp (car thread))
3432                 (progn
3433                   ;; If we use dummy roots, then we have to remove the
3434                   ;; dummy root as well.
3435                   (when (eq gnus-summary-make-false-root 'dummy)
3436                     ;; We go to the dummy root by going to
3437                     ;; the first sub-"thread", and then one line up.
3438                     (gnus-summary-goto-article
3439                      (mail-header-number (caadr thread)))
3440                     (forward-line -1)
3441                     (gnus-delete-line)
3442                     (gnus-data-compute-positions))
3443                   (setq thread (cdr thread))
3444                   (while thread
3445                     (gnus-remove-thread-1 (car thread))
3446                     (setq thread (cdr thread))))
3447               (gnus-summary-show-all-threads)
3448               (gnus-remove-thread-1 thread))))))))
3449
3450 (defun gnus-remove-thread-1 (thread)
3451   "Remove the thread THREAD recursively."
3452   (let ((number (mail-header-number (pop thread)))
3453         d)
3454     (setq thread (reverse thread))
3455     (while thread
3456       (gnus-remove-thread-1 (pop thread)))
3457     (when (setq d (gnus-data-find number))
3458       (goto-char (gnus-data-pos d))
3459       (gnus-data-remove
3460        number
3461        (- (gnus-point-at-bol)
3462           (prog1
3463               (1+ (gnus-point-at-eol))
3464             (gnus-delete-line)))))))
3465
3466 (defun gnus-sort-threads (threads)
3467   "Sort THREADS."
3468   (if (not gnus-thread-sort-functions)
3469       threads
3470     (gnus-message 8 "Sorting threads...")
3471     (prog1
3472         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3473       (gnus-message 8 "Sorting threads...done"))))
3474
3475 (defun gnus-sort-articles (articles)
3476   "Sort ARTICLES."
3477   (when gnus-article-sort-functions
3478     (gnus-message 7 "Sorting articles...")
3479     (prog1
3480         (setq gnus-newsgroup-headers
3481               (sort articles (gnus-make-sort-function
3482                               gnus-article-sort-functions)))
3483       (gnus-message 7 "Sorting articles...done"))))
3484
3485 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3486 (defmacro gnus-thread-header (thread)
3487   ;; Return header of first article in THREAD.
3488   ;; Note that THREAD must never, ever be anything else than a variable -
3489   ;; using some other form will lead to serious barfage.
3490   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3491   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3492   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3493         (vector thread) 2))
3494
3495 (defsubst gnus-article-sort-by-number (h1 h2)
3496   "Sort articles by article number."
3497   (< (mail-header-number h1)
3498      (mail-header-number h2)))
3499
3500 (defun gnus-thread-sort-by-number (h1 h2)
3501   "Sort threads by root article number."
3502   (gnus-article-sort-by-number
3503    (gnus-thread-header h1) (gnus-thread-header h2)))
3504
3505 (defsubst gnus-article-sort-by-lines (h1 h2)
3506   "Sort articles by article Lines header."
3507   (< (mail-header-lines h1)
3508      (mail-header-lines h2)))
3509
3510 (defun gnus-thread-sort-by-lines (h1 h2)
3511   "Sort threads by root article Lines header."
3512   (gnus-article-sort-by-lines
3513    (gnus-thread-header h1) (gnus-thread-header h2)))
3514
3515 (defsubst gnus-article-sort-by-author (h1 h2)
3516   "Sort articles by root author."
3517   (string-lessp
3518    (let ((extract (funcall
3519                    gnus-extract-address-components
3520                    (mail-header-from h1))))
3521      (or (car extract) (cadr extract) ""))
3522    (let ((extract (funcall
3523                    gnus-extract-address-components
3524                    (mail-header-from h2))))
3525      (or (car extract) (cadr extract) ""))))
3526
3527 (defun gnus-thread-sort-by-author (h1 h2)
3528   "Sort threads by root author."
3529   (gnus-article-sort-by-author
3530    (gnus-thread-header h1)  (gnus-thread-header h2)))
3531
3532 (defsubst gnus-article-sort-by-subject (h1 h2)
3533   "Sort articles by root subject."
3534   (string-lessp
3535    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3536    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3537
3538 (defun gnus-thread-sort-by-subject (h1 h2)
3539   "Sort threads by root subject."
3540   (gnus-article-sort-by-subject
3541    (gnus-thread-header h1) (gnus-thread-header h2)))
3542
3543 (defsubst gnus-article-sort-by-date (h1 h2)
3544   "Sort articles by root article date."
3545   (time-less-p
3546    (gnus-date-get-time (mail-header-date h1))
3547    (gnus-date-get-time (mail-header-date h2))))
3548
3549 (defun gnus-thread-sort-by-date (h1 h2)
3550   "Sort threads by root article date."
3551   (gnus-article-sort-by-date
3552    (gnus-thread-header h1) (gnus-thread-header h2)))
3553
3554 (defsubst gnus-article-sort-by-score (h1 h2)
3555   "Sort articles by root article score.
3556 Unscored articles will be counted as having a score of zero."
3557   (> (or (cdr (assq (mail-header-number h1)
3558                     gnus-newsgroup-scored))
3559          gnus-summary-default-score 0)
3560      (or (cdr (assq (mail-header-number h2)
3561                     gnus-newsgroup-scored))
3562          gnus-summary-default-score 0)))
3563
3564 (defun gnus-thread-sort-by-score (h1 h2)
3565   "Sort threads by root article score."
3566   (gnus-article-sort-by-score
3567    (gnus-thread-header h1) (gnus-thread-header h2)))
3568
3569 (defun gnus-thread-sort-by-total-score (h1 h2)
3570   "Sort threads by the sum of all scores in the thread.
3571 Unscored articles will be counted as having a score of zero."
3572   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3573
3574 (defun gnus-thread-total-score (thread)
3575   ;;  This function find the total score of THREAD.
3576   (cond ((null thread)
3577          0)
3578         ((consp thread)
3579          (if (stringp (car thread))
3580              (apply gnus-thread-score-function 0
3581                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3582            (gnus-thread-total-score-1 thread)))
3583         (t
3584          (gnus-thread-total-score-1 (list thread)))))
3585
3586 (defun gnus-thread-total-score-1 (root)
3587   ;; This function find the total score of the thread below ROOT.
3588   (setq root (car root))
3589   (apply gnus-thread-score-function
3590          (or (append
3591               (mapcar 'gnus-thread-total-score
3592                       (cdr (gnus-id-to-thread (mail-header-id root))))
3593               (when (> (mail-header-number root) 0)
3594                 (list (or (cdr (assq (mail-header-number root)
3595                                      gnus-newsgroup-scored))
3596                           gnus-summary-default-score 0))))
3597              (list gnus-summary-default-score)
3598              '(0))))
3599
3600 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3601 (defvar gnus-tmp-prev-subject nil)
3602 (defvar gnus-tmp-false-parent nil)
3603 (defvar gnus-tmp-root-expunged nil)
3604 (defvar gnus-tmp-dummy-line nil)
3605
3606 (defun gnus-summary-prepare-threads (threads)
3607   "Prepare summary buffer from THREADS and indentation LEVEL.
3608 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3609 or a straight list of headers."
3610   (gnus-message 7 "Generating summary...")
3611
3612   (setq gnus-newsgroup-threads threads)
3613   (beginning-of-line)
3614
3615   (let ((gnus-tmp-level 0)
3616         (default-score (or gnus-summary-default-score 0))
3617         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3618         thread number subject stack state gnus-tmp-gathered beg-match
3619         new-roots gnus-tmp-new-adopts thread-end
3620         gnus-tmp-header gnus-tmp-unread
3621         gnus-tmp-replied gnus-tmp-subject-or-nil
3622         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3623         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3624         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3625
3626     (setq gnus-tmp-prev-subject nil)
3627
3628     (if (vectorp (car threads))
3629         ;; If this is a straight (sic) list of headers, then a
3630         ;; threaded summary display isn't required, so we just create
3631         ;; an unthreaded one.
3632         (gnus-summary-prepare-unthreaded threads)
3633
3634       ;; Do the threaded display.
3635
3636       (while (or threads stack gnus-tmp-new-adopts new-roots)
3637
3638         (if (and (= gnus-tmp-level 0)
3639                  (or (not stack)
3640                      (= (caar stack) 0))
3641                  (not gnus-tmp-false-parent)
3642                  (or gnus-tmp-new-adopts new-roots))
3643             (if gnus-tmp-new-adopts
3644                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3645                       thread (list (car gnus-tmp-new-adopts))
3646                       gnus-tmp-header (caar thread)
3647                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3648               (when new-roots
3649                 (setq thread (list (car new-roots))
3650                       gnus-tmp-header (caar thread)
3651                       new-roots (cdr new-roots))))
3652
3653           (if threads
3654               ;; If there are some threads, we do them before the
3655               ;; threads on the stack.
3656               (setq thread threads
3657                     gnus-tmp-header (caar thread))
3658             ;; There were no current threads, so we pop something off
3659             ;; the stack.
3660             (setq state (car stack)
3661                   gnus-tmp-level (car state)
3662                   thread (cdr state)
3663                   stack (cdr stack)
3664                   gnus-tmp-header (caar thread))))
3665
3666         (setq gnus-tmp-false-parent nil)
3667         (setq gnus-tmp-root-expunged nil)
3668         (setq thread-end nil)
3669
3670         (if (stringp gnus-tmp-header)
3671             ;; The header is a dummy root.
3672             (cond
3673              ((eq gnus-summary-make-false-root 'adopt)
3674               ;; We let the first article adopt the rest.
3675               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3676                                                (cddar thread)))
3677               (setq gnus-tmp-gathered
3678                     (nconc (mapcar
3679                             (lambda (h) (mail-header-number (car h)))
3680                             (cddar thread))
3681                            gnus-tmp-gathered))
3682               (setq thread (cons (list (caar thread)
3683                                        (cadar thread))
3684                                  (cdr thread)))
3685               (setq gnus-tmp-level -1
3686                     gnus-tmp-false-parent t))
3687              ((eq gnus-summary-make-false-root 'empty)
3688               ;; We print adopted articles with empty subject fields.
3689               (setq gnus-tmp-gathered
3690                     (nconc (mapcar
3691                             (lambda (h) (mail-header-number (car h)))
3692                             (cddar thread))
3693                            gnus-tmp-gathered))
3694               (setq gnus-tmp-level -1))
3695              ((eq gnus-summary-make-false-root 'dummy)
3696               ;; We remember that we probably want to output a dummy
3697               ;; root.
3698               (setq gnus-tmp-dummy-line gnus-tmp-header)
3699               (setq gnus-tmp-prev-subject gnus-tmp-header))
3700              (t
3701               ;; We do not make a root for the gathered
3702               ;; sub-threads at all.
3703               (setq gnus-tmp-level -1)))
3704
3705           (setq number (mail-header-number gnus-tmp-header)
3706                 subject (mail-header-subject gnus-tmp-header))
3707
3708           (cond
3709            ;; If the thread has changed subject, we might want to make
3710            ;; this subthread into a root.
3711            ((and (null gnus-thread-ignore-subject)
3712                  (not (zerop gnus-tmp-level))
3713                  gnus-tmp-prev-subject
3714                  (not (inline
3715                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3716             (setq new-roots (nconc new-roots (list (car thread)))
3717                   thread-end t
3718                   gnus-tmp-header nil))
3719            ;; If the article lies outside the current limit,
3720            ;; then we do not display it.
3721            ((not (memq number gnus-newsgroup-limit))
3722             (setq gnus-tmp-gathered
3723                   (nconc (mapcar
3724                           (lambda (h) (mail-header-number (car h)))
3725                           (cdar thread))
3726                          gnus-tmp-gathered))
3727             (setq gnus-tmp-new-adopts (if (cdar thread)
3728                                           (append gnus-tmp-new-adopts
3729                                                   (cdar thread))
3730                                         gnus-tmp-new-adopts)
3731                   thread-end t
3732                   gnus-tmp-header nil)
3733             (when (zerop gnus-tmp-level)
3734               (setq gnus-tmp-root-expunged t)))
3735            ;; Perhaps this article is to be marked as read?
3736            ((and gnus-summary-mark-below
3737                  (< (or (cdr (assq number gnus-newsgroup-scored))
3738                         default-score)
3739                     gnus-summary-mark-below)
3740                  ;; Don't touch sparse articles.
3741                  (not (gnus-summary-article-sparse-p number))
3742                  (not (gnus-summary-article-ancient-p number)))
3743             (setq gnus-newsgroup-unreads
3744                   (delq number gnus-newsgroup-unreads))
3745             (if gnus-newsgroup-auto-expire
3746                 (push number gnus-newsgroup-expirable)
3747               (push (cons number gnus-low-score-mark)
3748                     gnus-newsgroup-reads))))
3749
3750           (when gnus-tmp-header
3751             ;; We may have an old dummy line to output before this
3752             ;; article.
3753             (when (and gnus-tmp-dummy-line
3754                        (gnus-subject-equal
3755                         gnus-tmp-dummy-line
3756                         (mail-header-subject gnus-tmp-header)))
3757               (gnus-summary-insert-dummy-line
3758                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3759               (setq gnus-tmp-dummy-line nil))
3760
3761             ;; Compute the mark.
3762             (setq gnus-tmp-unread (gnus-article-mark number))
3763
3764             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3765                                   gnus-tmp-header gnus-tmp-level)
3766                   gnus-newsgroup-data)
3767
3768             ;; Actually insert the line.
3769             (setq
3770              gnus-tmp-subject-or-nil
3771              (cond
3772               ((and gnus-thread-ignore-subject
3773                     gnus-tmp-prev-subject
3774                     (not (inline (gnus-subject-equal
3775                                   gnus-tmp-prev-subject subject))))
3776                subject)
3777               ((zerop gnus-tmp-level)
3778                (if (and (eq gnus-summary-make-false-root 'empty)
3779                         (memq number gnus-tmp-gathered)
3780                         gnus-tmp-prev-subject
3781                         (inline (gnus-subject-equal
3782                                  gnus-tmp-prev-subject subject)))
3783                    gnus-summary-same-subject
3784                  subject))
3785               (t gnus-summary-same-subject)))
3786             (if (and (eq gnus-summary-make-false-root 'adopt)
3787                      (= gnus-tmp-level 1)
3788                      (memq number gnus-tmp-gathered))
3789                 (setq gnus-tmp-opening-bracket ?\<
3790                       gnus-tmp-closing-bracket ?\>)
3791               (setq gnus-tmp-opening-bracket ?\[
3792                     gnus-tmp-closing-bracket ?\]))
3793             (setq
3794              gnus-tmp-indentation
3795              (aref gnus-thread-indent-array gnus-tmp-level)
3796              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3797              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3798                                 gnus-summary-default-score 0)
3799              gnus-tmp-score-char
3800              (if (or (null gnus-summary-default-score)
3801                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3802                          gnus-summary-zcore-fuzz))
3803                  ? 
3804                (if (< gnus-tmp-score gnus-summary-default-score)
3805                    gnus-score-below-mark gnus-score-over-mark))
3806              gnus-tmp-replied
3807              (cond ((memq number gnus-newsgroup-processable)
3808                     gnus-process-mark)
3809                    ((memq number gnus-newsgroup-cached)
3810                     gnus-cached-mark)
3811                    ((memq number gnus-newsgroup-replied)
3812                     gnus-replied-mark)
3813                    ((memq number gnus-newsgroup-saved)
3814                     gnus-saved-mark)
3815                    (t gnus-unread-mark))
3816              gnus-tmp-from (mail-header-from gnus-tmp-header)
3817              gnus-tmp-name
3818              (cond
3819               ((string-match "<[^>]+> *$" gnus-tmp-from)
3820                (setq beg-match (match-beginning 0))
3821                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3822                         (substring gnus-tmp-from (1+ (match-beginning 0))
3823                                    (1- (match-end 0))))
3824                    (substring gnus-tmp-from 0 beg-match)))
3825               ((string-match "(.+)" gnus-tmp-from)
3826                (substring gnus-tmp-from
3827                           (1+ (match-beginning 0)) (1- (match-end 0))))
3828               (t gnus-tmp-from)))
3829             (when (string= gnus-tmp-name "")
3830               (setq gnus-tmp-name gnus-tmp-from))
3831             (unless (numberp gnus-tmp-lines)
3832               (setq gnus-tmp-lines 0))
3833             (gnus-put-text-property
3834              (point)
3835              (progn (eval gnus-summary-line-format-spec) (point))
3836              'gnus-number number)
3837             (when gnus-visual-p
3838               (forward-line -1)
3839               (gnus-run-hooks 'gnus-summary-update-hook)
3840               (forward-line 1))
3841
3842             (setq gnus-tmp-prev-subject subject)))
3843
3844         (when (nth 1 thread)
3845           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3846         (incf gnus-tmp-level)
3847         (setq threads (if thread-end nil (cdar thread)))
3848         (unless threads
3849           (setq gnus-tmp-level 0)))))
3850   (gnus-message 7 "Generating summary...done"))
3851
3852 (defun gnus-summary-prepare-unthreaded (headers)
3853   "Generate an unthreaded summary buffer based on HEADERS."
3854   (let (header number mark)
3855
3856     (beginning-of-line)
3857
3858     (while headers
3859       ;; We may have to root out some bad articles...
3860       (when (memq (setq number (mail-header-number
3861                                 (setq header (pop headers))))
3862                   gnus-newsgroup-limit)
3863         ;; Mark article as read when it has a low score.
3864         (when (and gnus-summary-mark-below
3865                    (< (or (cdr (assq number gnus-newsgroup-scored))
3866                           gnus-summary-default-score 0)
3867                       gnus-summary-mark-below)
3868                    (not (gnus-summary-article-ancient-p number)))
3869           (setq gnus-newsgroup-unreads
3870                 (delq number gnus-newsgroup-unreads))
3871           (if gnus-newsgroup-auto-expire
3872               (push number gnus-newsgroup-expirable)
3873             (push (cons number gnus-low-score-mark)
3874                   gnus-newsgroup-reads)))
3875
3876         (setq mark (gnus-article-mark number))
3877         (push (gnus-data-make number mark (1+ (point)) header 0)
3878               gnus-newsgroup-data)
3879         (gnus-summary-insert-line
3880          header 0 number
3881          mark (memq number gnus-newsgroup-replied)
3882          (memq number gnus-newsgroup-expirable)
3883          (mail-header-subject header) nil
3884          (cdr (assq number gnus-newsgroup-scored))
3885          (memq number gnus-newsgroup-processable))))))
3886
3887 (defun gnus-select-newsgroup (group &optional read-all select-articles)
3888   "Select newsgroup GROUP.
3889 If READ-ALL is non-nil, all articles in the group are selected.
3890 If SELECT-ARTICLES, only select those articles from GROUP."
3891   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3892          ;;!!! Dirty hack; should be removed.
3893          (gnus-summary-ignore-duplicates
3894           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
3895               t
3896             gnus-summary-ignore-duplicates))
3897          (info (nth 2 entry))
3898          articles fetched-articles cached)
3899
3900     (unless (gnus-check-server
3901              (setq gnus-current-select-method
3902                    (gnus-find-method-for-group group)))
3903       (error "Couldn't open server"))
3904
3905     (or (and entry (not (eq (car entry) t))) ; Either it's active...
3906         (gnus-activate-group group)     ; Or we can activate it...
3907         (progn                          ; Or we bug out.
3908           (when (equal major-mode 'gnus-summary-mode)
3909             (kill-buffer (current-buffer)))
3910           (error "Couldn't request group %s: %s"
3911                  group (gnus-status-message group))))
3912
3913     (unless (gnus-request-group group t)
3914       (when (equal major-mode 'gnus-summary-mode)
3915         (kill-buffer (current-buffer)))
3916       (error "Couldn't request group %s: %s"
3917              group (gnus-status-message group)))
3918
3919     (setq gnus-newsgroup-name group)
3920     (setq gnus-newsgroup-unselected nil)
3921     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
3922
3923     ;; Adjust and set lists of article marks.
3924     (when info
3925       (gnus-adjust-marked-articles info))
3926
3927     ;; Kludge to avoid having cached articles nixed out in virtual groups.
3928     (when (gnus-virtual-group-p group)
3929       (setq cached gnus-newsgroup-cached))
3930
3931     (setq gnus-newsgroup-unreads
3932           (gnus-set-difference
3933            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
3934            gnus-newsgroup-dormant))
3935
3936     (setq gnus-newsgroup-processable nil)
3937
3938     (gnus-update-read-articles group gnus-newsgroup-unreads)
3939
3940     (if (setq articles select-articles)
3941         (setq gnus-newsgroup-unselected
3942               (gnus-sorted-intersection
3943                gnus-newsgroup-unreads
3944                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
3945       (setq articles (gnus-articles-to-read group read-all)))
3946
3947     (cond
3948      ((null articles)
3949       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
3950       'quit)
3951      ((eq articles 0) nil)
3952      (t
3953       ;; Init the dependencies hash table.
3954       (setq gnus-newsgroup-dependencies
3955             (gnus-make-hashtable (length articles)))
3956       (gnus-set-global-variables)
3957       ;; Retrieve the headers and read them in.
3958       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
3959       (setq gnus-newsgroup-headers
3960             (if (eq 'nov
3961                     (setq gnus-headers-retrieved-by
3962                           (gnus-retrieve-headers
3963                            articles gnus-newsgroup-name
3964                            ;; We might want to fetch old headers, but
3965                            ;; not if there is only 1 article.
3966                            (and (or (and
3967                                      (not (eq gnus-fetch-old-headers 'some))
3968                                      (not (numberp gnus-fetch-old-headers)))
3969                                     (> (length articles) 1))
3970                                 gnus-fetch-old-headers))))
3971                 (gnus-get-newsgroup-headers-xover
3972                  articles nil nil gnus-newsgroup-name t)
3973               (gnus-get-newsgroup-headers)))
3974       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
3975
3976       ;; Kludge to avoid having cached articles nixed out in virtual groups.
3977       (when cached
3978         (setq gnus-newsgroup-cached cached))
3979
3980       ;; Suppress duplicates?
3981       (when gnus-suppress-duplicates
3982         (gnus-dup-suppress-articles))
3983
3984       ;; Set the initial limit.
3985       (setq gnus-newsgroup-limit (copy-sequence articles))
3986       ;; Remove canceled articles from the list of unread articles.
3987       (setq gnus-newsgroup-unreads
3988             (gnus-set-sorted-intersection
3989              gnus-newsgroup-unreads
3990              (setq fetched-articles
3991                    (mapcar (lambda (headers) (mail-header-number headers))
3992                            gnus-newsgroup-headers))))
3993       ;; Removed marked articles that do not exist.
3994       (gnus-update-missing-marks
3995        (gnus-sorted-complement fetched-articles articles))
3996       ;; We might want to build some more threads first.
3997       (when (and gnus-fetch-old-headers
3998                  (eq gnus-headers-retrieved-by 'nov))
3999         (if (eq gnus-fetch-old-headers 'invisible)
4000             (gnus-build-all-threads)
4001           (gnus-build-old-threads)))
4002       ;; Let the Gnus agent mark articles as read.
4003       (when gnus-agent
4004         (gnus-agent-get-undownloaded-list))
4005       ;; Check whether auto-expire is to be done in this group.
4006       (setq gnus-newsgroup-auto-expire
4007             (gnus-group-auto-expirable-p group))
4008       ;; Set up the article buffer now, if necessary.
4009       (unless gnus-single-article-buffer
4010         (gnus-article-setup-buffer))
4011       ;; First and last article in this newsgroup.
4012       (when gnus-newsgroup-headers
4013         (setq gnus-newsgroup-begin
4014               (mail-header-number (car gnus-newsgroup-headers))
4015               gnus-newsgroup-end
4016               (mail-header-number
4017                (gnus-last-element gnus-newsgroup-headers))))
4018       ;; GROUP is successfully selected.
4019       (or gnus-newsgroup-headers t)))))
4020
4021 (defun gnus-articles-to-read (group &optional read-all)
4022   ;; Find out what articles the user wants to read.
4023   (let* ((articles
4024           ;; Select all articles if `read-all' is non-nil, or if there
4025           ;; are no unread articles.
4026           (if (or read-all
4027                   (and (zerop (length gnus-newsgroup-marked))
4028                        (zerop (length gnus-newsgroup-unreads)))
4029                   (eq (gnus-group-find-parameter group 'display)
4030                       'all))
4031               (gnus-uncompress-range (gnus-active group))
4032             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4033                           (copy-sequence gnus-newsgroup-unreads))
4034                   '<)))
4035          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4036          (scored (length scored-list))
4037          (number (length articles))
4038          (marked (+ (length gnus-newsgroup-marked)
4039                     (length gnus-newsgroup-dormant)))
4040          (select
4041           (cond
4042            ((numberp read-all)
4043             read-all)
4044            (t
4045             (condition-case ()
4046                 (cond
4047                  ((and (or (<= scored marked) (= scored number))
4048                        (numberp gnus-large-newsgroup)
4049                        (> number gnus-large-newsgroup))
4050                   (let ((input
4051                          (read-string
4052                           (format
4053                            "How many articles from %s (default %d): "
4054                            (gnus-limit-string gnus-newsgroup-name 35)
4055                            number))))
4056                     (if (string-match "^[ \t]*$" input) number input)))
4057                  ((and (> scored marked) (< scored number)
4058                        (> (- scored number) 20))
4059                   (let ((input
4060                          (read-string
4061                           (format "%s %s (%d scored, %d total): "
4062                                   "How many articles from"
4063                                   group scored number))))
4064                     (if (string-match "^[ \t]*$" input)
4065                         number input)))
4066                  (t number))
4067               (quit nil))))))
4068     (setq select (if (stringp select) (string-to-number select) select))
4069     (if (or (null select) (zerop select))
4070         select
4071       (if (and (not (zerop scored)) (<= (abs select) scored))
4072           (progn
4073             (setq articles (sort scored-list '<))
4074             (setq number (length articles)))
4075         (setq articles (copy-sequence articles)))
4076
4077       (when (< (abs select) number)
4078         (if (< select 0)
4079             ;; Select the N oldest articles.
4080             (setcdr (nthcdr (1- (abs select)) articles) nil)
4081           ;; Select the N most recent articles.
4082           (setq articles (nthcdr (- number select) articles))))
4083       (setq gnus-newsgroup-unselected
4084             (gnus-sorted-intersection
4085              gnus-newsgroup-unreads
4086              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4087       articles)))
4088
4089 (defun gnus-killed-articles (killed articles)
4090   (let (out)
4091     (while articles
4092       (when (inline (gnus-member-of-range (car articles) killed))
4093         (push (car articles) out))
4094       (setq articles (cdr articles)))
4095     out))
4096
4097 (defun gnus-uncompress-marks (marks)
4098   "Uncompress the mark ranges in MARKS."
4099   (let ((uncompressed '(score bookmark))
4100         out)
4101     (while marks
4102       (if (memq (caar marks) uncompressed)
4103           (push (car marks) out)
4104         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4105       (setq marks (cdr marks)))
4106     out))
4107
4108 (defun gnus-adjust-marked-articles (info)
4109   "Set all article lists and remove all marks that are no longer legal."
4110   (let* ((marked-lists (gnus-info-marks info))
4111          (active (gnus-active (gnus-info-group info)))
4112          (min (car active))
4113          (max (cdr active))
4114          (types gnus-article-mark-lists)
4115          (uncompressed '(score bookmark killed))
4116          marks var articles article mark)
4117
4118     (while marked-lists
4119       (setq marks (pop marked-lists))
4120       (set (setq var (intern (format "gnus-newsgroup-%s"
4121                                      (car (rassq (setq mark (car marks))
4122                                                  types)))))
4123            (if (memq (car marks) uncompressed) (cdr marks)
4124              (gnus-uncompress-range (cdr marks))))
4125
4126       (setq articles (symbol-value var))
4127
4128       ;; All articles have to be subsets of the active articles.
4129       (cond
4130        ;; Adjust "simple" lists.
4131        ((memq mark '(tick dormant expire reply save))
4132         (while articles
4133           (when (or (< (setq article (pop articles)) min) (> article max))
4134             (set var (delq article (symbol-value var))))))
4135        ;; Adjust assocs.
4136        ((memq mark uncompressed)
4137         (when (not (listp (cdr (symbol-value var))))
4138           (set var (list (symbol-value var))))
4139         (when (not (listp (cdr articles)))
4140           (setq articles (list articles)))
4141         (while articles
4142           (when (or (not (consp (setq article (pop articles))))
4143                     (< (car article) min)
4144                     (> (car article) max))
4145             (set var (delq article (symbol-value var))))))))))
4146
4147 (defun gnus-update-missing-marks (missing)
4148   "Go through the list of MISSING articles and remove them from the mark lists."
4149   (when missing
4150     (let ((types gnus-article-mark-lists)
4151           var m)
4152       ;; Go through all types.
4153       (while types
4154         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4155         (when (symbol-value var)
4156           ;; This list has articles.  So we delete all missing articles
4157           ;; from it.
4158           (setq m missing)
4159           (while m
4160             (set var (delq (pop m) (symbol-value var)))))))))
4161
4162 (defun gnus-update-marks ()
4163   "Enter the various lists of marked articles into the newsgroup info list."
4164   (let ((types gnus-article-mark-lists)
4165         (info (gnus-get-info gnus-newsgroup-name))
4166         (uncompressed '(score bookmark killed))
4167        type list newmarked symbol delta-marks)
4168     (when info
4169       ;; Add all marks lists that are non-nil to the list of marks lists.
4170       (while (setq type (pop types))
4171         (when (setq list (symbol-value
4172                           (setq symbol
4173                                 (intern (format "gnus-newsgroup-%s"
4174                                                 (car type))))))
4175
4176           ;; Get rid of the entries of the articles that have the
4177           ;; default score.
4178           (when (and (eq (cdr type) 'score)
4179                      gnus-save-score
4180                      list)
4181             (let* ((arts list)
4182                    (prev (cons nil list))
4183                    (all prev))
4184               (while arts
4185                 (if (or (not (consp (car arts)))
4186                         (= (cdar arts) gnus-summary-default-score))
4187                     (setcdr prev (cdr arts))
4188                   (setq prev arts))
4189                 (setq arts (cdr arts)))
4190               (setq list (cdr all))))
4191
4192           (push (cons (cdr type)
4193                       (if (memq (cdr type) uncompressed) list
4194                         (gnus-compress-sequence
4195                          (set symbol (sort list '<)) t)))
4196                 newmarked)))
4197
4198       ;; Enter these new marks into the info of the group.
4199       (if (nthcdr 3 info)
4200           (setcar (nthcdr 3 info) newmarked)
4201         ;; Add the marks lists to the end of the info.
4202         (when newmarked
4203           (setcdr (nthcdr 2 info) (list newmarked))))
4204
4205       ;; Cut off the end of the info if there's nothing else there.
4206       (let ((i 5))
4207         (while (and (> i 2)
4208                     (not (nth i info)))
4209           (when (nthcdr (decf i) info)
4210             (setcdr (nthcdr i info) nil)))))))
4211
4212 (defun gnus-set-mode-line (where)
4213   "This function sets the mode line of the article or summary buffers.
4214 If WHERE is `summary', the summary mode line format will be used."
4215   ;; Is this mode line one we keep updated?
4216   (when (memq where gnus-updated-mode-lines)
4217     (let (mode-string)
4218       (save-excursion
4219         ;; We evaluate this in the summary buffer since these
4220         ;; variables are buffer-local to that buffer.
4221         (set-buffer gnus-summary-buffer)
4222         ;; We bind all these variables that are used in the `eval' form
4223         ;; below.
4224         (let* ((mformat (symbol-value
4225                          (intern
4226                           (format "gnus-%s-mode-line-format-spec" where))))
4227                (gnus-tmp-group-name gnus-newsgroup-name)
4228                (gnus-tmp-article-number (or gnus-current-article 0))
4229                (gnus-tmp-unread gnus-newsgroup-unreads)
4230                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4231                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4232                (gnus-tmp-unread-and-unselected
4233                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4234                             (zerop gnus-tmp-unselected))
4235                        "")
4236                       ((zerop gnus-tmp-unselected)
4237                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4238                       (t (format "{%d(+%d) more}"
4239                                  gnus-tmp-unread-and-unticked
4240                                  gnus-tmp-unselected))))
4241                (gnus-tmp-subject
4242                 (if (and gnus-current-headers
4243                          (vectorp gnus-current-headers))
4244                     (gnus-mode-string-quote
4245                      (mail-header-subject gnus-current-headers))
4246                   ""))
4247                bufname-length max-len
4248                gnus-tmp-header);; passed as argument to any user-format-funcs
4249           (setq mode-string (eval mformat))
4250           (setq bufname-length (if (string-match "%b" mode-string)
4251                                    (- (length
4252                                        (buffer-name
4253                                         (if (eq where 'summary)
4254                                             nil
4255                                           (get-buffer gnus-article-buffer))))
4256                                       2)
4257                                  0))
4258           (setq max-len (max 4 (if gnus-mode-non-string-length
4259                                    (- (window-width)
4260                                       gnus-mode-non-string-length
4261                                       bufname-length)
4262                                  (length mode-string))))
4263           ;; We might have to chop a bit of the string off...
4264           (when (> (length mode-string) max-len)
4265             (setq mode-string
4266                   (concat (gnus-truncate-string mode-string (- max-len 3))
4267                           "...")))
4268           ;; Pad the mode string a bit.
4269           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4270       ;; Update the mode line.
4271       (setq mode-line-buffer-identification
4272             (gnus-mode-line-buffer-identification (list mode-string)))
4273       (set-buffer-modified-p t))))
4274
4275 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4276   "Go through the HEADERS list and add all Xrefs to a hash table.
4277 The resulting hash table is returned, or nil if no Xrefs were found."
4278   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4279          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4280          (xref-hashtb (gnus-make-hashtable))
4281          start group entry number xrefs header)
4282     (while headers
4283       (setq header (pop headers))
4284       (when (and (setq xrefs (mail-header-xref header))
4285                  (not (memq (setq number (mail-header-number header))
4286                             unreads)))
4287         (setq start 0)
4288         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4289           (setq start (match-end 0))
4290           (setq group (if prefix
4291                           (concat prefix (substring xrefs (match-beginning 1)
4292                                                     (match-end 1)))
4293                         (substring xrefs (match-beginning 1) (match-end 1))))
4294           (setq number
4295                 (string-to-int (substring xrefs (match-beginning 2)
4296                                           (match-end 2))))
4297           (if (setq entry (gnus-gethash group xref-hashtb))
4298               (setcdr entry (cons number (cdr entry)))
4299             (gnus-sethash group (cons number nil) xref-hashtb)))))
4300     (and start xref-hashtb)))
4301
4302 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4303   "Look through all the headers and mark the Xrefs as read."
4304   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4305         name entry info xref-hashtb idlist method nth4)
4306     (save-excursion
4307       (set-buffer gnus-group-buffer)
4308       (when (setq xref-hashtb
4309                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4310         (mapatoms
4311          (lambda (group)
4312            (unless (string= from-newsgroup (setq name (symbol-name group)))
4313              (setq idlist (symbol-value group))
4314              ;; Dead groups are not updated.
4315              (and (prog1
4316                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4317                             info (nth 2 entry))
4318                     (when (stringp (setq nth4 (gnus-info-method info)))
4319                       (setq nth4 (gnus-server-to-method nth4))))
4320                   ;; Only do the xrefs if the group has the same
4321                   ;; select method as the group we have just read.
4322                   (or (gnus-methods-equal-p
4323                        nth4 (gnus-find-method-for-group from-newsgroup))
4324                       virtual
4325                       (equal nth4 (setq method (gnus-find-method-for-group
4326                                                 from-newsgroup)))
4327                       (and (equal (car nth4) (car method))
4328                            (equal (nth 1 nth4) (nth 1 method))))
4329                   gnus-use-cross-reference
4330                   (or (not (eq gnus-use-cross-reference t))
4331                       virtual
4332                       ;; Only do cross-references on subscribed
4333                       ;; groups, if that is what is wanted.
4334                       (<= (gnus-info-level info) gnus-level-subscribed))
4335                   (gnus-group-make-articles-read name idlist))))
4336          xref-hashtb)))))
4337
4338 (defun gnus-compute-read-articles (group articles)
4339   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4340          (info (nth 2 entry))
4341          (active (gnus-active group))
4342          ninfo)
4343     (when entry
4344       ;; First peel off all illegal article numbers.
4345       (when active
4346         (let ((ids articles)
4347               id first)
4348           (while (setq id (pop ids))
4349             (when (and first (> id (cdr active)))
4350               ;; We'll end up in this situation in one particular
4351               ;; obscure situation.  If you re-scan a group and get
4352               ;; a new article that is cross-posted to a different
4353               ;; group that has not been re-scanned, you might get
4354               ;; crossposted article that has a higher number than
4355               ;; Gnus believes possible.  So we re-activate this
4356               ;; group as well.  This might mean doing the
4357               ;; crossposting thingy will *increase* the number
4358               ;; of articles in some groups.  Tsk, tsk.
4359               (setq active (or (gnus-activate-group group) active)))
4360             (when (or (> id (cdr active))
4361                       (< id (car active)))
4362               (setq articles (delq id articles))))))
4363       ;; If the read list is nil, we init it.
4364       (if (and active
4365                (null (gnus-info-read info))
4366                (> (car active) 1))
4367           (setq ninfo (cons 1 (1- (car active))))
4368         (setq ninfo (gnus-info-read info)))
4369       ;; Then we add the read articles to the range.
4370       (gnus-add-to-range
4371        ninfo (setq articles (sort articles '<))))))
4372
4373 (defun gnus-group-make-articles-read (group articles)
4374   "Update the info of GROUP to say that ARTICLES are read."
4375   (let* ((num 0)
4376          (entry (gnus-gethash group gnus-newsrc-hashtb))
4377          (info (nth 2 entry))
4378          (active (gnus-active group))
4379          range)
4380     (when entry
4381       (setq range (gnus-compute-read-articles group articles))
4382       (save-excursion
4383         (set-buffer gnus-group-buffer)
4384         (gnus-undo-register
4385           `(progn
4386              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4387              (gnus-info-set-read ',info ',(gnus-info-read info))
4388              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4389              (gnus-group-update-group ,group t))))
4390       ;; Add the read articles to the range.
4391       (gnus-info-set-read info range)
4392       ;; Then we have to re-compute how many unread
4393       ;; articles there are in this group.
4394       (when active
4395         (cond
4396          ((not range)
4397           (setq num (- (1+ (cdr active)) (car active))))
4398          ((not (listp (cdr range)))
4399           (setq num (- (cdr active) (- (1+ (cdr range))
4400                                        (car range)))))
4401          (t
4402           (while range
4403             (if (numberp (car range))
4404                 (setq num (1+ num))
4405               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4406             (setq range (cdr range)))
4407           (setq num (- (cdr active) num))))
4408         ;; Update the number of unread articles.
4409         (setcar entry num)
4410         ;; Update the group buffer.
4411         (gnus-group-update-group group t)))))
4412
4413 (defun gnus-methods-equal-p (m1 m2)
4414   (let ((m1 (or m1 gnus-select-method))
4415         (m2 (or m2 gnus-select-method)))
4416     (or (equal m1 m2)
4417         (and (eq (car m1) (car m2))
4418              (or (not (memq 'address (assoc (symbol-name (car m1))
4419                                             gnus-valid-select-methods)))
4420                  (equal (nth 1 m1) (nth 1 m2)))))))
4421
4422 (defvar gnus-newsgroup-none-id 0)
4423
4424 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4425   (let ((cur nntp-server-buffer)
4426         (dependencies
4427          (or dependencies
4428              (save-excursion (set-buffer gnus-summary-buffer)
4429                              gnus-newsgroup-dependencies)))
4430         headers id end ref)
4431     (save-excursion
4432       (set-buffer nntp-server-buffer)
4433       ;; Translate all TAB characters into SPACE characters.
4434       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4435       (gnus-run-hooks 'gnus-parse-headers-hook)
4436       (let ((case-fold-search t)
4437             rawtext decoded
4438             in-reply-to header p lines chars)
4439         (goto-char (point-min))
4440         ;; Search to the beginning of the next header.  Error messages
4441         ;; do not begin with 2 or 3.
4442         (while (re-search-forward "^[23][0-9]+ " nil t)
4443           (setq id nil
4444                 ref nil)
4445           ;; This implementation of this function, with nine
4446           ;; search-forwards instead of the one re-search-forward and
4447           ;; a case (which basically was the old function) is actually
4448           ;; about twice as fast, even though it looks messier.  You
4449           ;; can't have everything, I guess.  Speed and elegance
4450           ;; doesn't always go hand in hand.
4451           (setq
4452            header
4453            (vector
4454             ;; Number.
4455             (prog1
4456                 (read cur)
4457               (end-of-line)
4458               (setq p (point))
4459               (narrow-to-region (point)
4460                                 (or (and (search-forward "\n.\n" nil t)
4461                                          (- (point) 2))
4462                                     (point))))
4463             ;; Subject.
4464             (progn
4465               (goto-char p)
4466               (if (search-forward "\nsubject: " nil t)
4467                   (progn
4468                     (setq rawtext (nnheader-header-value)
4469                           decoded (funcall
4470                                    gnus-unstructured-field-decoder rawtext))
4471                     (if (string-equal rawtext decoded)
4472                         rawtext
4473                       (put-text-property 0 (length decoded) 'raw-text rawtext decoded)
4474                       decoded))
4475                 "(none)"))
4476             ;; From.
4477             (progn
4478               (goto-char p)
4479               (if (search-forward "\nfrom: " nil t)
4480                   (progn
4481                     (setq rawtext (nnheader-header-value)
4482                           decoded (funcall
4483                                    gnus-structured-field-decoder rawtext))
4484                     (if (string-equal rawtext decoded)
4485                         rawtext
4486                       (put-text-property 0 (length decoded) 'raw-text rawtext decoded)
4487                       decoded))
4488                 "(nobody)"))
4489             ;; Date.
4490             (progn
4491               (goto-char p)
4492               (if (search-forward "\ndate: " nil t)
4493                   (nnheader-header-value) ""))
4494             ;; Message-ID.
4495             (progn
4496               (goto-char p)
4497               (setq id (if (re-search-forward
4498                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4499                            ;; We do it this way to make sure the Message-ID
4500                            ;; is (somewhat) syntactically valid.
4501                            (buffer-substring (match-beginning 1)
4502                                              (match-end 1))
4503                          ;; If there was no message-id, we just fake one
4504                          ;; to make subsequent routines simpler.
4505                          (nnheader-generate-fake-message-id))))
4506             ;; References.
4507             (progn
4508               (goto-char p)
4509               (if (search-forward "\nreferences: " nil t)
4510                   (progn
4511                     (setq end (point))
4512                     (prog1
4513                         (nnheader-header-value)
4514                       (setq ref
4515                             (buffer-substring
4516                              (progn
4517                                (end-of-line)
4518                                (search-backward ">" end t)
4519                                (1+ (point)))
4520                              (progn
4521                                (search-backward "<" end t)
4522                                (point))))))
4523                 ;; Get the references from the in-reply-to header if there
4524                 ;; were no references and the in-reply-to header looks
4525                 ;; promising.
4526                 (if (and (search-forward "\nin-reply-to: " nil t)
4527                          (setq in-reply-to (nnheader-header-value))
4528                          (string-match "<[^>]+>" in-reply-to))
4529                     (let (ref2)
4530                       (setq ref (substring in-reply-to (match-beginning 0)
4531                                            (match-end 0)))
4532                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4533                         (setq ref2 (substring in-reply-to (match-beginning 0)
4534                                               (match-end 0)))
4535                         (when (> (length ref2) (length ref))
4536                           (setq ref ref2)))
4537                       ref)
4538                   (setq ref nil))))
4539             ;; Chars.
4540             (progn
4541               (goto-char p)
4542               (if (search-forward "\nchars: " nil t)
4543                   (if (numberp (setq chars (ignore-errors (read cur))))
4544                       chars 0)
4545                 0))
4546             ;; Lines.
4547             (progn
4548               (goto-char p)
4549               (if (search-forward "\nlines: " nil t)
4550                   (if (numberp (setq lines (ignore-errors (read cur))))
4551                       lines 0)
4552                 0))
4553             ;; Xref.
4554             (progn
4555               (goto-char p)
4556               (and (search-forward "\nxref: " nil t)
4557                    (nnheader-header-value)))))
4558           (when (equal id ref)
4559             (setq ref nil))
4560
4561           (when gnus-alter-header-function
4562             (funcall gnus-alter-header-function header)
4563             (setq id (mail-header-id header)
4564                   ref (gnus-parent-id (mail-header-references header))))
4565
4566           (when (setq header
4567                       (gnus-dependencies-add-header
4568                        header dependencies force-new))
4569             (push header headers))
4570           (goto-char (point-max))
4571           (widen))
4572         (nreverse headers)))))
4573
4574 ;; Goes through the xover lines and returns a list of vectors
4575 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4576                                                   force-new dependencies
4577                                                   group also-fetch-heads)
4578   "Parse the news overview data in the server buffer, and return a
4579 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4580   ;; Get the Xref when the users reads the articles since most/some
4581   ;; NNTP servers do not include Xrefs when using XOVER.
4582   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4583   (let ((cur nntp-server-buffer)
4584         (dependencies (or dependencies gnus-newsgroup-dependencies))
4585         number headers header)
4586     (save-excursion
4587       (set-buffer nntp-server-buffer)
4588       ;; Allow the user to mangle the headers before parsing them.
4589       (gnus-run-hooks 'gnus-parse-headers-hook)
4590       (goto-char (point-min))
4591       (while (not (eobp))
4592         (condition-case ()
4593             (while (and sequence (not (eobp)))
4594               (setq number (read cur))
4595               (while (and sequence
4596                           (< (car sequence) number))
4597                 (setq sequence (cdr sequence)))
4598               (and sequence
4599                    (eq number (car sequence))
4600                    (progn
4601                      (setq sequence (cdr sequence))
4602                      (setq header (inline
4603                                     (gnus-nov-parse-line
4604                                      number dependencies force-new))))
4605                    (push header headers))
4606               (forward-line 1))
4607           (error
4608            (gnus-error 4 "Strange nov line (%d)"
4609                        (count-lines (point-min) (point)))))
4610         (forward-line 1))
4611       ;; A common bug in inn is that if you have posted an article and
4612       ;; then retrieves the active file, it will answer correctly --
4613       ;; the new article is included.  However, a NOV entry for the
4614       ;; article may not have been generated yet, so this may fail.
4615       ;; We work around this problem by retrieving the last few
4616       ;; headers using HEAD.
4617       (if (or (not also-fetch-heads)
4618               (not sequence))
4619           ;; We (probably) got all the headers.
4620           (nreverse headers)
4621         (let ((gnus-nov-is-evil t))
4622           (nconc
4623            (nreverse headers)
4624            (when (gnus-retrieve-headers sequence group)
4625              (gnus-get-newsgroup-headers))))))))
4626
4627 (defun gnus-article-get-xrefs ()
4628   "Fill in the Xref value in `gnus-current-headers', if necessary.
4629 This is meant to be called in `gnus-article-internal-prepare-hook'."
4630   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4631                                  gnus-current-headers)))
4632     (or (not gnus-use-cross-reference)
4633         (not headers)
4634         (and (mail-header-xref headers)
4635              (not (string= (mail-header-xref headers) "")))
4636         (let ((case-fold-search t)
4637               xref)
4638           (save-restriction
4639             (nnheader-narrow-to-headers)
4640             (goto-char (point-min))
4641             (when (or (and (eq (downcase (following-char)) ?x)
4642                            (looking-at "Xref:"))
4643                       (search-forward "\nXref:" nil t))
4644               (goto-char (1+ (match-end 0)))
4645               (setq xref (buffer-substring (point)
4646                                            (progn (end-of-line) (point))))
4647               (mail-header-set-xref headers xref)))))))
4648
4649 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4650   "Find article ID and insert the summary line for that article.
4651 OLD-HEADER can either be a header or a line number to insert
4652 the subject line on."
4653   (let* ((line (and (numberp old-header) old-header))
4654          (old-header (and (vectorp old-header) old-header))
4655          (header (cond ((and old-header use-old-header)
4656                        old-header)
4657                       ((and (numberp id)
4658                             (gnus-number-to-header id))
4659                        (gnus-number-to-header id))
4660                       (t
4661                        (gnus-read-header id))))
4662         (number (and (numberp id) id))
4663         d)
4664     (when header
4665       ;; Rebuild the thread that this article is part of and go to the
4666       ;; article we have fetched.
4667       (when (and (not gnus-show-threads)
4668                  old-header)
4669         (when (and number
4670                    (setq d (gnus-data-find (mail-header-number old-header))))
4671           (goto-char (gnus-data-pos d))
4672           (gnus-data-remove
4673            number
4674            (- (gnus-point-at-bol)
4675               (prog1
4676                   (1+ (gnus-point-at-eol))
4677                 (gnus-delete-line))))))
4678       (when old-header
4679         (mail-header-set-number header (mail-header-number old-header)))
4680       (setq gnus-newsgroup-sparse
4681             (delq (setq number (mail-header-number header))
4682                   gnus-newsgroup-sparse))
4683       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4684       (push number gnus-newsgroup-limit)
4685       (gnus-rebuild-thread (mail-header-id header) line)
4686       (gnus-summary-goto-subject number nil t))
4687     (when (and (numberp number)
4688                (> number 0))
4689       ;; We have to update the boundaries even if we can't fetch the
4690       ;; article if ID is a number -- so that the next `P' or `N'
4691       ;; command will fetch the previous (or next) article even
4692       ;; if the one we tried to fetch this time has been canceled.
4693       (when (> number gnus-newsgroup-end)
4694         (setq gnus-newsgroup-end number))
4695       (when (< number gnus-newsgroup-begin)
4696         (setq gnus-newsgroup-begin number))
4697       (setq gnus-newsgroup-unselected
4698             (delq number gnus-newsgroup-unselected)))
4699     ;; Report back a success?
4700     (and header (mail-header-number header))))
4701
4702 ;;; Process/prefix in the summary buffer
4703
4704 (defun gnus-summary-work-articles (n)
4705   "Return a list of articles to be worked upon.
4706 The prefix argument, the list of process marked articles, and the
4707 current article will be taken into consideration."
4708   (save-excursion
4709     (set-buffer gnus-summary-buffer)
4710     (cond
4711      (n
4712       ;; A numerical prefix has been given.
4713       (setq n (prefix-numeric-value n))
4714       (let ((backward (< n 0))
4715             (n (abs (prefix-numeric-value n)))
4716             articles article)
4717         (save-excursion
4718           (while
4719               (and (> n 0)
4720                    (push (setq article (gnus-summary-article-number))
4721                          articles)
4722                    (if backward
4723                        (gnus-summary-find-prev nil article)
4724                      (gnus-summary-find-next nil article)))
4725             (decf n)))
4726         (nreverse articles)))
4727      ((and (gnus-region-active-p) (mark))
4728       (message "region active")
4729       ;; Work on the region between point and mark.
4730       (let ((max (max (point) (mark)))
4731             articles article)
4732         (save-excursion
4733           (goto-char (min (point) (mark)))
4734           (while
4735               (and
4736                (push (setq article (gnus-summary-article-number)) articles)
4737                (gnus-summary-find-next nil article)
4738                (< (point) max)))
4739           (nreverse articles))))
4740      (gnus-newsgroup-processable
4741       ;; There are process-marked articles present.
4742       ;; Save current state.
4743       (gnus-summary-save-process-mark)
4744       ;; Return the list.
4745       (reverse gnus-newsgroup-processable))
4746      (t
4747       ;; Just return the current article.
4748       (list (gnus-summary-article-number))))))
4749
4750 (defmacro gnus-summary-iterate (arg &rest forms)
4751   "Iterate over the process/prefixed articles and do FORMS.
4752 ARG is the interactive prefix given to the command.  FORMS will be
4753 executed with point over the summary line of the articles."
4754   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4755     `(let ((,articles (gnus-summary-work-articles ,arg)))
4756        (while ,articles
4757          (gnus-summary-goto-subject (car ,articles))
4758          ,@forms))))
4759
4760 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4761 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4762
4763 (defun gnus-summary-save-process-mark ()
4764   "Push the current set of process marked articles on the stack."
4765   (interactive)
4766   (push (copy-sequence gnus-newsgroup-processable)
4767         gnus-newsgroup-process-stack))
4768
4769 (defun gnus-summary-kill-process-mark ()
4770   "Push the current set of process marked articles on the stack and unmark."
4771   (interactive)
4772   (gnus-summary-save-process-mark)
4773   (gnus-summary-unmark-all-processable))
4774
4775 (defun gnus-summary-yank-process-mark ()
4776   "Pop the last process mark state off the stack and restore it."
4777   (interactive)
4778   (unless gnus-newsgroup-process-stack
4779     (error "Empty mark stack"))
4780   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4781
4782 (defun gnus-summary-process-mark-set (set)
4783   "Make SET into the current process marked articles."
4784   (gnus-summary-unmark-all-processable)
4785   (while set
4786     (gnus-summary-set-process-mark (pop set))))
4787
4788 ;;; Searching and stuff
4789
4790 (defun gnus-summary-search-group (&optional backward use-level)
4791   "Search for next unread newsgroup.
4792 If optional argument BACKWARD is non-nil, search backward instead."
4793   (save-excursion
4794     (set-buffer gnus-group-buffer)
4795     (when (gnus-group-search-forward
4796            backward nil (if use-level (gnus-group-group-level) nil))
4797       (gnus-group-group-name))))
4798
4799 (defun gnus-summary-best-group (&optional exclude-group)
4800   "Find the name of the best unread group.
4801 If EXCLUDE-GROUP, do not go to this group."
4802   (save-excursion
4803     (set-buffer gnus-group-buffer)
4804     (save-excursion
4805       (gnus-group-best-unread-group exclude-group))))
4806
4807 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4808   (if backward (gnus-summary-find-prev)
4809     (let* ((dummy (gnus-summary-article-intangible-p))
4810            (article (or article (gnus-summary-article-number)))
4811            (arts (gnus-data-find-list article))
4812            result)
4813       (when (and (not dummy)
4814                  (or (not gnus-summary-check-current)
4815                      (not unread)
4816                      (not (gnus-data-unread-p (car arts)))))
4817         (setq arts (cdr arts)))
4818       (when (setq result
4819                   (if unread
4820                       (progn
4821                         (while arts
4822                           (when (or (and undownloaded
4823                                          (eq gnus-undownloaded-mark
4824                                              (gnus-data-mark (car arts))))
4825                                     (gnus-data-unread-p (car arts)))
4826                             (setq result (car arts)
4827                                   arts nil))
4828                           (setq arts (cdr arts)))
4829                         result)
4830                     (car arts)))
4831         (goto-char (gnus-data-pos result))
4832         (gnus-data-number result)))))
4833
4834 (defun gnus-summary-find-prev (&optional unread article)
4835   (let* ((eobp (eobp))
4836          (article (or article (gnus-summary-article-number)))
4837          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4838          result)
4839     (when (and (not eobp)
4840                (or (not gnus-summary-check-current)
4841                    (not unread)
4842                    (not (gnus-data-unread-p (car arts)))))
4843       (setq arts (cdr arts)))
4844     (when (setq result
4845                 (if unread
4846                     (progn
4847                       (while arts
4848                         (when (gnus-data-unread-p (car arts))
4849                           (setq result (car arts)
4850                                 arts nil))
4851                         (setq arts (cdr arts)))
4852                       result)
4853                   (car arts)))
4854       (goto-char (gnus-data-pos result))
4855       (gnus-data-number result))))
4856
4857 (defun gnus-summary-find-subject (subject &optional unread backward article)
4858   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4859          (article (or article (gnus-summary-article-number)))
4860          (articles (gnus-data-list backward))
4861          (arts (gnus-data-find-list article articles))
4862          result)
4863     (when (or (not gnus-summary-check-current)
4864               (not unread)
4865               (not (gnus-data-unread-p (car arts))))
4866       (setq arts (cdr arts)))
4867     (while arts
4868       (and (or (not unread)
4869                (gnus-data-unread-p (car arts)))
4870            (vectorp (gnus-data-header (car arts)))
4871            (gnus-subject-equal
4872             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4873            (setq result (car arts)
4874                  arts nil))
4875       (setq arts (cdr arts)))
4876     (and result
4877          (goto-char (gnus-data-pos result))
4878          (gnus-data-number result))))
4879
4880 (defun gnus-summary-search-forward (&optional unread subject backward)
4881   "Search forward for an article.
4882 If UNREAD, look for unread articles.  If SUBJECT, look for
4883 articles with that subject.  If BACKWARD, search backward instead."
4884   (cond (subject (gnus-summary-find-subject subject unread backward))
4885         (backward (gnus-summary-find-prev unread))
4886         (t (gnus-summary-find-next unread))))
4887
4888 (defun gnus-recenter (&optional n)
4889   "Center point in window and redisplay frame.
4890 Also do horizontal recentering."
4891   (interactive "P")
4892   (when (and gnus-auto-center-summary
4893              (not (eq gnus-auto-center-summary 'vertical)))
4894     (gnus-horizontal-recenter))
4895   (recenter n))
4896
4897 (defun gnus-summary-recenter ()
4898   "Center point in the summary window.
4899 If `gnus-auto-center-summary' is nil, or the article buffer isn't
4900 displayed, no centering will be performed."
4901   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
4902   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
4903   (let* ((top (cond ((< (window-height) 4) 0)
4904                     ((< (window-height) 7) 1)
4905                     (t 2)))
4906          (height (1- (window-height)))
4907          (bottom (save-excursion (goto-char (point-max))
4908                                  (forward-line (- height))
4909                                  (point)))
4910          (window (get-buffer-window (current-buffer))))
4911     ;; The user has to want it.
4912     (when gnus-auto-center-summary
4913       (when (get-buffer-window gnus-article-buffer)
4914         ;; Only do recentering when the article buffer is displayed,
4915         ;; Set the window start to either `bottom', which is the biggest
4916         ;; possible valid number, or the second line from the top,
4917         ;; whichever is the least.
4918         (set-window-start
4919          window (min bottom (save-excursion
4920                               (forward-line (- top)) (point)))))
4921       ;; Do horizontal recentering while we're at it.
4922       (when (and (get-buffer-window (current-buffer) t)
4923                  (not (eq gnus-auto-center-summary 'vertical)))
4924         (let ((selected (selected-window)))
4925           (select-window (get-buffer-window (current-buffer) t))
4926           (gnus-summary-position-point)
4927           (gnus-horizontal-recenter)
4928           (select-window selected))))))
4929
4930 (defun gnus-summary-jump-to-group (newsgroup)
4931   "Move point to NEWSGROUP in group mode buffer."
4932   ;; Keep update point of group mode buffer if visible.
4933   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
4934       (save-window-excursion
4935         ;; Take care of tree window mode.
4936         (when (get-buffer-window gnus-group-buffer)
4937           (pop-to-buffer gnus-group-buffer))
4938         (gnus-group-jump-to-group newsgroup))
4939     (save-excursion
4940       ;; Take care of tree window mode.
4941       (if (get-buffer-window gnus-group-buffer)
4942           (pop-to-buffer gnus-group-buffer)
4943         (set-buffer gnus-group-buffer))
4944       (gnus-group-jump-to-group newsgroup))))
4945
4946 ;; This function returns a list of article numbers based on the
4947 ;; difference between the ranges of read articles in this group and
4948 ;; the range of active articles.
4949 (defun gnus-list-of-unread-articles (group)
4950   (let* ((read (gnus-info-read (gnus-get-info group)))
4951          (active (or (gnus-active group) (gnus-activate-group group)))
4952          (last (cdr active))
4953          first nlast unread)
4954     ;; If none are read, then all are unread.
4955     (if (not read)
4956         (setq first (car active))
4957       ;; If the range of read articles is a single range, then the
4958       ;; first unread article is the article after the last read
4959       ;; article.  Sounds logical, doesn't it?
4960       (if (not (listp (cdr read)))
4961           (setq first (max (car active) (1+ (cdr read))))
4962         ;; `read' is a list of ranges.
4963         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
4964                                   (caar read)))
4965                   1)
4966           (setq first (car active)))
4967         (while read
4968           (when first
4969             (while (< first nlast)
4970               (push first unread)
4971               (setq first (1+ first))))
4972           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
4973           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
4974           (setq read (cdr read)))))
4975     ;; And add the last unread articles.
4976     (while (<= first last)
4977       (push first unread)
4978       (setq first (1+ first)))
4979     ;; Return the list of unread articles.
4980     (delq 0 (nreverse unread))))
4981
4982 (defun gnus-list-of-read-articles (group)
4983   "Return a list of unread, unticked and non-dormant articles."
4984   (let* ((info (gnus-get-info group))
4985          (marked (gnus-info-marks info))
4986          (active (gnus-active group)))
4987     (and info active
4988          (gnus-set-difference
4989           (gnus-sorted-complement
4990            (gnus-uncompress-range active)
4991            (gnus-list-of-unread-articles group))
4992           (append
4993            (gnus-uncompress-range (cdr (assq 'dormant marked)))
4994            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
4995
4996 ;; Various summary commands
4997
4998 (defun gnus-summary-select-article-buffer ()
4999   "Reconfigure windows to show article buffer."
5000   (interactive)
5001   (if (not (gnus-buffer-live-p gnus-article-buffer))
5002       (error "There is no article buffer for this summary buffer")
5003     (gnus-configure-windows 'article)
5004     (select-window (get-buffer-window gnus-article-buffer))))
5005
5006 (defun gnus-summary-universal-argument (arg)
5007   "Perform any operation on all articles that are process/prefixed."
5008   (interactive "P")
5009   (let ((articles (gnus-summary-work-articles arg))
5010         func article)
5011     (if (eq
5012          (setq
5013           func
5014           (key-binding
5015            (read-key-sequence
5016             (substitute-command-keys
5017              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5018              ))))
5019          'undefined)
5020         (gnus-error 1 "Undefined key")
5021       (save-excursion
5022         (while articles
5023           (gnus-summary-goto-subject (setq article (pop articles)))
5024           (let (gnus-newsgroup-processable)
5025             (command-execute func))
5026           (gnus-summary-remove-process-mark article)))))
5027   (gnus-summary-position-point))
5028
5029 (defun gnus-summary-toggle-truncation (&optional arg)
5030   "Toggle truncation of summary lines.
5031 With arg, turn line truncation on iff arg is positive."
5032   (interactive "P")
5033   (setq truncate-lines
5034         (if (null arg) (not truncate-lines)
5035           (> (prefix-numeric-value arg) 0)))
5036   (redraw-display))
5037
5038 (defun gnus-summary-reselect-current-group (&optional all rescan)
5039   "Exit and then reselect the current newsgroup.
5040 The prefix argument ALL means to select all articles."
5041   (interactive "P")
5042   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5043     (error "Ephemeral groups can't be reselected"))
5044   (let ((current-subject (gnus-summary-article-number))
5045         (group gnus-newsgroup-name))
5046     (setq gnus-newsgroup-begin nil)
5047     (gnus-summary-exit)
5048     ;; We have to adjust the point of group mode buffer because
5049     ;; point was moved to the next unread newsgroup by exiting.
5050     (gnus-summary-jump-to-group group)
5051     (when rescan
5052       (save-excursion
5053         (gnus-group-get-new-news-this-group 1)))
5054     (gnus-group-read-group all t)
5055     (gnus-summary-goto-subject current-subject nil t)))
5056
5057 (defun gnus-summary-rescan-group (&optional all)
5058   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5059   (interactive "P")
5060   (gnus-summary-reselect-current-group all t))
5061
5062 (defun gnus-summary-update-info (&optional non-destructive)
5063   (save-excursion
5064     (let ((group gnus-newsgroup-name))
5065       (when group
5066         (when gnus-newsgroup-kill-headers
5067           (setq gnus-newsgroup-killed
5068                 (gnus-compress-sequence
5069                  (nconc
5070                   (gnus-set-sorted-intersection
5071                    (gnus-uncompress-range gnus-newsgroup-killed)
5072                    (setq gnus-newsgroup-unselected
5073                          (sort gnus-newsgroup-unselected '<)))
5074                   (setq gnus-newsgroup-unreads
5075                         (sort gnus-newsgroup-unreads '<)))
5076                  t)))
5077         (unless (listp (cdr gnus-newsgroup-killed))
5078           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5079         (let ((headers gnus-newsgroup-headers))
5080           ;; Set the new ranges of read articles.
5081           (save-excursion
5082             (set-buffer gnus-group-buffer)
5083             (gnus-undo-force-boundary))
5084           (gnus-update-read-articles
5085            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5086           ;; Set the current article marks.
5087           (let ((gnus-newsgroup-scored
5088                  (if (and (not gnus-save-score)
5089                           (not non-destructive))
5090                      nil
5091                    gnus-newsgroup-scored)))
5092             (save-excursion
5093               (gnus-update-marks)))
5094           ;; Do the cross-ref thing.
5095           (when gnus-use-cross-reference
5096             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5097           ;; Do not switch windows but change the buffer to work.
5098           (set-buffer gnus-group-buffer)
5099           (unless (gnus-ephemeral-group-p group)
5100             (gnus-group-update-group group)))))))
5101
5102 (defun gnus-summary-save-newsrc (&optional force)
5103   "Save the current number of read/marked articles in the dribble buffer.
5104 The dribble buffer will then be saved.
5105 If FORCE (the prefix), also save the .newsrc file(s)."
5106   (interactive "P")
5107   (gnus-summary-update-info t)
5108   (if force
5109       (gnus-save-newsrc-file)
5110     (gnus-dribble-save)))
5111
5112 (defun gnus-summary-exit (&optional temporary)
5113   "Exit reading current newsgroup, and then return to group selection mode.
5114 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5115   (interactive)
5116   (gnus-set-global-variables)
5117   (gnus-kill-save-kill-buffer)
5118   (gnus-async-halt-prefetch)
5119   (let* ((group gnus-newsgroup-name)
5120          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5121          (mode major-mode)
5122          (group-point nil)
5123          (buf (current-buffer)))
5124     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5125     ;; If we have several article buffers, we kill them at exit.
5126     (unless gnus-single-article-buffer
5127       (gnus-kill-buffer gnus-original-article-buffer)
5128       (setq gnus-article-current nil))
5129     (when gnus-use-cache
5130       (gnus-cache-possibly-remove-articles)
5131       (gnus-cache-save-buffers))
5132     (gnus-async-prefetch-remove-group group)
5133     (when gnus-suppress-duplicates
5134       (gnus-dup-enter-articles))
5135     (when gnus-use-trees
5136       (gnus-tree-close group))
5137     ;; Remove entries for this group.
5138     (nnmail-purge-split-history (gnus-group-real-name group))
5139     ;; Make all changes in this group permanent.
5140     (unless quit-config
5141       (gnus-run-hooks 'gnus-exit-group-hook)
5142       (gnus-summary-update-info)
5143       ;; Do adaptive scoring, and possibly save score files.
5144       (when gnus-newsgroup-adaptive
5145         (gnus-score-adaptive))
5146       (when gnus-use-scoring
5147         (gnus-score-save)))
5148     (gnus-close-group group)
5149     ;; Make sure where we were, and go to next newsgroup.
5150     (set-buffer gnus-group-buffer)
5151     (unless quit-config
5152       (gnus-group-jump-to-group group))
5153     (gnus-run-hooks 'gnus-summary-exit-hook)
5154     (unless (or quit-config
5155                 ;; If this group has disappeared from the summary
5156                 ;; buffer, don't skip forwards.
5157                 (not (string= group (gnus-group-group-name))))
5158       (gnus-group-next-unread-group 1))
5159     (setq group-point (point))
5160     (if temporary
5161         nil                             ;Nothing to do.
5162       ;; If we have several article buffers, we kill them at exit.
5163       (unless gnus-single-article-buffer
5164         (gnus-kill-buffer gnus-article-buffer)
5165         (gnus-kill-buffer gnus-original-article-buffer)
5166         (setq gnus-article-current nil))
5167       (set-buffer buf)
5168       (if (not gnus-kill-summary-on-exit)
5169           (gnus-deaden-summary)
5170         ;; We set all buffer-local variables to nil.  It is unclear why
5171         ;; this is needed, but if we don't, buffer-local variables are
5172         ;; not garbage-collected, it seems.  This would the lead to en
5173         ;; ever-growing Emacs.
5174         (gnus-summary-clear-local-variables)
5175         (when (get-buffer gnus-article-buffer)
5176           (bury-buffer gnus-article-buffer))
5177         ;; We clear the global counterparts of the buffer-local
5178         ;; variables as well, just to be on the safe side.
5179         (set-buffer gnus-group-buffer)
5180         (gnus-summary-clear-local-variables)
5181         ;; Return to group mode buffer.
5182         (when (eq mode 'gnus-summary-mode)
5183           (gnus-kill-buffer buf)))
5184       (setq gnus-current-select-method gnus-select-method)
5185       (pop-to-buffer gnus-group-buffer)
5186       (if (not quit-config)
5187           (progn
5188             (goto-char group-point)
5189             (gnus-configure-windows 'group 'force))
5190         (gnus-handle-ephemeral-exit quit-config))
5191       ;; Clear the current group name.
5192       (unless quit-config
5193         (setq gnus-newsgroup-name nil)))))
5194
5195 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5196 (defun gnus-summary-exit-no-update (&optional no-questions)
5197   "Quit reading current newsgroup without updating read article info."
5198   (interactive)
5199   (let* ((group gnus-newsgroup-name)
5200          (quit-config (gnus-group-quit-config group)))
5201     (when (or no-questions
5202               gnus-expert-user
5203               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5204       (gnus-async-halt-prefetch)
5205       (gnus-run-hooks (delq 'gnus-summary-expire-articles
5206                             (copy-list gnus-summary-prepare-exit-hook)))
5207       ;; If we have several article buffers, we kill them at exit.
5208       (unless gnus-single-article-buffer
5209         (gnus-kill-buffer gnus-article-buffer)
5210         (gnus-kill-buffer gnus-original-article-buffer)
5211         (setq gnus-article-current nil))
5212       (if (not gnus-kill-summary-on-exit)
5213           (gnus-deaden-summary)
5214         (gnus-close-group group)
5215         (gnus-summary-clear-local-variables)
5216         (set-buffer gnus-group-buffer)
5217         (gnus-summary-clear-local-variables)
5218         (when (get-buffer gnus-summary-buffer)
5219           (kill-buffer gnus-summary-buffer)))
5220       (unless gnus-single-article-buffer
5221         (setq gnus-article-current nil))
5222       (when gnus-use-trees
5223         (gnus-tree-close group))
5224       (gnus-async-prefetch-remove-group group)
5225       (when (get-buffer gnus-article-buffer)
5226         (bury-buffer gnus-article-buffer))
5227       ;; Return to the group buffer.
5228       (gnus-configure-windows 'group 'force)
5229       ;; Clear the current group name.
5230       (setq gnus-newsgroup-name nil)
5231       (when (equal (gnus-group-group-name) group)
5232         (gnus-group-next-unread-group 1))
5233       (when quit-config
5234         (gnus-handle-ephemeral-exit quit-config)))))
5235
5236 (defun gnus-handle-ephemeral-exit (quit-config)
5237   "Handle movement when leaving an ephemeral group.
5238 The state which existed when entering the ephemeral is reset."
5239   (if (not (buffer-name (car quit-config)))
5240       (gnus-configure-windows 'group 'force)
5241     (set-buffer (car quit-config))
5242     (cond ((eq major-mode 'gnus-summary-mode)
5243            (gnus-set-global-variables))
5244           ((eq major-mode 'gnus-article-mode)
5245            (save-excursion
5246              ;; The `gnus-summary-buffer' variable may point
5247              ;; to the old summary buffer when using a single
5248              ;; article buffer.
5249              (unless (gnus-buffer-live-p gnus-summary-buffer)
5250                (set-buffer gnus-group-buffer))
5251              (set-buffer gnus-summary-buffer)
5252              (gnus-set-global-variables))))
5253     (if (or (eq (cdr quit-config) 'article)
5254             (eq (cdr quit-config) 'pick))
5255         (progn
5256           ;; The current article may be from the ephemeral group
5257           ;; thus it is best that we reload this article
5258           (gnus-summary-show-article)
5259           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5260               (gnus-configure-windows 'pick 'force)
5261             (gnus-configure-windows (cdr quit-config) 'force)))
5262       (gnus-configure-windows (cdr quit-config) 'force))
5263     (when (eq major-mode 'gnus-summary-mode)
5264       (gnus-summary-next-subject 1 nil t)
5265       (gnus-summary-recenter)
5266       (gnus-summary-position-point))))
5267
5268 (defun gnus-summary-preview-mime-message (arg)
5269   "MIME decode and play this message."
5270   (interactive "P")
5271   (or gnus-show-mime
5272       (let ((gnus-break-pages nil)
5273             (gnus-show-mime t))
5274         (gnus-summary-select-article t t)
5275         ))
5276   (select-window (get-buffer-window gnus-article-buffer))
5277   )
5278
5279 ;;; Dead summaries.
5280
5281 (defvar gnus-dead-summary-mode-map nil)
5282
5283 (unless gnus-dead-summary-mode-map
5284   (setq gnus-dead-summary-mode-map (make-keymap))
5285   (suppress-keymap gnus-dead-summary-mode-map)
5286   (substitute-key-definition
5287    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5288   (let ((keys '("\C-d" "\r" "\177" [delete])))
5289     (while keys
5290       (define-key gnus-dead-summary-mode-map
5291         (pop keys) 'gnus-summary-wake-up-the-dead))))
5292
5293 (defvar gnus-dead-summary-mode nil
5294   "Minor mode for Gnus summary buffers.")
5295
5296 (defun gnus-dead-summary-mode (&optional arg)
5297   "Minor mode for Gnus summary buffers."
5298   (interactive "P")
5299   (when (eq major-mode 'gnus-summary-mode)
5300     (make-local-variable 'gnus-dead-summary-mode)
5301     (setq gnus-dead-summary-mode
5302           (if (null arg) (not gnus-dead-summary-mode)
5303             (> (prefix-numeric-value arg) 0)))
5304     (when gnus-dead-summary-mode
5305       (gnus-add-minor-mode
5306        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5307
5308 (defun gnus-deaden-summary ()
5309   "Make the current summary buffer into a dead summary buffer."
5310   ;; Kill any previous dead summary buffer.
5311   (when (and gnus-dead-summary
5312              (buffer-name gnus-dead-summary))
5313     (save-excursion
5314       (set-buffer gnus-dead-summary)
5315       (when gnus-dead-summary-mode
5316         (kill-buffer (current-buffer)))))
5317   ;; Make this the current dead summary.
5318   (setq gnus-dead-summary (current-buffer))
5319   (gnus-dead-summary-mode 1)
5320   (let ((name (buffer-name)))
5321     (when (string-match "Summary" name)
5322       (rename-buffer
5323        (concat (substring name 0 (match-beginning 0)) "Dead "
5324                (substring name (match-beginning 0)))
5325        t))))
5326
5327 (defun gnus-kill-or-deaden-summary (buffer)
5328   "Kill or deaden the summary BUFFER."
5329   (save-excursion
5330     (when (and (buffer-name buffer)
5331                (not gnus-single-article-buffer))
5332       (save-excursion
5333         (set-buffer buffer)
5334         (gnus-kill-buffer gnus-article-buffer)
5335         (gnus-kill-buffer gnus-original-article-buffer)))
5336     (cond (gnus-kill-summary-on-exit
5337            (when (and gnus-use-trees
5338                       (gnus-buffer-exists-p buffer))
5339              (save-excursion
5340                (set-buffer buffer)
5341                (gnus-tree-close gnus-newsgroup-name)))
5342            (gnus-kill-buffer buffer))
5343           ((gnus-buffer-exists-p buffer)
5344            (save-excursion
5345              (set-buffer buffer)
5346              (gnus-deaden-summary))))))
5347
5348 (defun gnus-summary-wake-up-the-dead (&rest args)
5349   "Wake up the dead summary buffer."
5350   (interactive)
5351   (gnus-dead-summary-mode -1)
5352   (let ((name (buffer-name)))
5353     (when (string-match "Dead " name)
5354       (rename-buffer
5355        (concat (substring name 0 (match-beginning 0))
5356                (substring name (match-end 0)))
5357        t)))
5358   (gnus-message 3 "This dead summary is now alive again"))
5359
5360 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5361 (defun gnus-summary-fetch-faq (&optional faq-dir)
5362   "Fetch the FAQ for the current group.
5363 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5364 in."
5365   (interactive
5366    (list
5367     (when current-prefix-arg
5368       (completing-read
5369        "Faq dir: " (and (listp gnus-group-faq-directory)
5370                         (mapcar (lambda (file) (list file))
5371                                 gnus-group-faq-directory))))))
5372   (let (gnus-faq-buffer)
5373     (when (setq gnus-faq-buffer
5374                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5375       (gnus-configure-windows 'summary-faq))))
5376
5377 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5378 (defun gnus-summary-describe-group (&optional force)
5379   "Describe the current newsgroup."
5380   (interactive "P")
5381   (gnus-group-describe-group force gnus-newsgroup-name))
5382
5383 (defun gnus-summary-describe-briefly ()
5384   "Describe summary mode commands briefly."
5385   (interactive)
5386   (gnus-message 6
5387                 (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")))
5388
5389 ;; Walking around group mode buffer from summary mode.
5390
5391 (defun gnus-summary-next-group (&optional no-article target-group backward)
5392   "Exit current newsgroup and then select next unread newsgroup.
5393 If prefix argument NO-ARTICLE is non-nil, no article is selected
5394 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5395 previous group instead."
5396   (interactive "P")
5397   ;; Stop pre-fetching.
5398   (gnus-async-halt-prefetch)
5399   (let ((current-group gnus-newsgroup-name)
5400         (current-buffer (current-buffer))
5401         entered)
5402     ;; First we semi-exit this group to update Xrefs and all variables.
5403     ;; We can't do a real exit, because the window conf must remain
5404     ;; the same in case the user is prompted for info, and we don't
5405     ;; want the window conf to change before that...
5406     (gnus-summary-exit t)
5407     (while (not entered)
5408       ;; Then we find what group we are supposed to enter.
5409       (set-buffer gnus-group-buffer)
5410       (gnus-group-jump-to-group current-group)
5411       (setq target-group
5412             (or target-group
5413                 (if (eq gnus-keep-same-level 'best)
5414                     (gnus-summary-best-group gnus-newsgroup-name)
5415                   (gnus-summary-search-group backward gnus-keep-same-level))))
5416       (if (not target-group)
5417           ;; There are no further groups, so we return to the group
5418           ;; buffer.
5419           (progn
5420             (gnus-message 5 "Returning to the group buffer")
5421             (setq entered t)
5422             (when (gnus-buffer-live-p current-buffer)
5423               (set-buffer current-buffer)
5424               (gnus-summary-exit))
5425             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5426         ;; We try to enter the target group.
5427         (gnus-group-jump-to-group target-group)
5428         (let ((unreads (gnus-group-group-unread)))
5429           (if (and (or (eq t unreads)
5430                        (and unreads (not (zerop unreads))))
5431                    (gnus-summary-read-group
5432                     target-group nil no-article
5433                     (and (buffer-name current-buffer) current-buffer)
5434                     nil backward))
5435               (setq entered t)
5436             (setq current-group target-group
5437                   target-group nil)))))))
5438
5439 (defun gnus-summary-prev-group (&optional no-article)
5440   "Exit current newsgroup and then select previous unread newsgroup.
5441 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5442   (interactive "P")
5443   (gnus-summary-next-group no-article nil t))
5444
5445 ;; Walking around summary lines.
5446
5447 (defun gnus-summary-first-subject (&optional unread undownloaded)
5448   "Go to the first unread subject.
5449 If UNREAD is non-nil, go to the first unread article.
5450 Returns the article selected or nil if there are no unread articles."
5451   (interactive "P")
5452   (prog1
5453       (cond
5454        ;; Empty summary.
5455        ((null gnus-newsgroup-data)
5456         (gnus-message 3 "No articles in the group")
5457         nil)
5458        ;; Pick the first article.
5459        ((not unread)
5460         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5461         (gnus-data-number (car gnus-newsgroup-data)))
5462        ;; No unread articles.
5463        ((null gnus-newsgroup-unreads)
5464         (gnus-message 3 "No more unread articles")
5465         nil)
5466        ;; Find the first unread article.
5467        (t
5468         (let ((data gnus-newsgroup-data))
5469           (while (and data
5470                       (and (not (and undownloaded
5471                                      (eq gnus-undownloaded-mark
5472                                          (gnus-data-mark (car data)))))
5473                            (not (gnus-data-unread-p (car data)))))
5474             (setq data (cdr data)))
5475           (when data
5476             (goto-char (gnus-data-pos (car data)))
5477             (gnus-data-number (car data))))))
5478     (gnus-summary-position-point)))
5479
5480 (defun gnus-summary-next-subject (n &optional unread dont-display)
5481   "Go to next N'th summary line.
5482 If N is negative, go to the previous N'th subject line.
5483 If UNREAD is non-nil, only unread articles are selected.
5484 The difference between N and the actual number of steps taken is
5485 returned."
5486   (interactive "p")
5487   (let ((backward (< n 0))
5488         (n (abs n)))
5489     (while (and (> n 0)
5490                 (if backward
5491                     (gnus-summary-find-prev unread)
5492                   (gnus-summary-find-next unread)))
5493       (gnus-summary-show-thread)
5494       (setq n (1- n)))
5495     (when (/= 0 n)
5496       (gnus-message 7 "No more%s articles"
5497                     (if unread " unread" "")))
5498     (unless dont-display
5499       (gnus-summary-recenter)
5500       (gnus-summary-position-point))
5501     n))
5502
5503 (defun gnus-summary-next-unread-subject (n)
5504   "Go to next N'th unread summary line."
5505   (interactive "p")
5506   (gnus-summary-next-subject n t))
5507
5508 (defun gnus-summary-prev-subject (n &optional unread)
5509   "Go to previous N'th summary line.
5510 If optional argument UNREAD is non-nil, only unread article is selected."
5511   (interactive "p")
5512   (gnus-summary-next-subject (- n) unread))
5513
5514 (defun gnus-summary-prev-unread-subject (n)
5515   "Go to previous N'th unread summary line."
5516   (interactive "p")
5517   (gnus-summary-next-subject (- n) t))
5518
5519 (defun gnus-summary-goto-subject (article &optional force silent)
5520   "Go the subject line of ARTICLE.
5521 If FORCE, also allow jumping to articles not currently shown."
5522   (interactive "nArticle number: ")
5523   (let ((b (point))
5524         (data (gnus-data-find article)))
5525     ;; We read in the article if we have to.
5526     (and (not data)
5527          force
5528          (gnus-summary-insert-subject
5529           article
5530           (if (or (numberp force) (vectorp force)) force)
5531           t)
5532          (setq data (gnus-data-find article)))
5533     (goto-char b)
5534     (if (not data)
5535         (progn
5536           (unless silent
5537             (gnus-message 3 "Can't find article %d" article))
5538           nil)
5539       (goto-char (gnus-data-pos data))
5540       (gnus-summary-position-point)
5541       article)))
5542
5543 ;; Walking around summary lines with displaying articles.
5544
5545 (defun gnus-summary-expand-window (&optional arg)
5546   "Make the summary buffer take up the entire Emacs frame.
5547 Given a prefix, will force an `article' buffer configuration."
5548   (interactive "P")
5549   (if arg
5550       (gnus-configure-windows 'article 'force)
5551     (gnus-configure-windows 'summary 'force)))
5552
5553 (defun gnus-summary-display-article (article &optional all-header)
5554   "Display ARTICLE in article buffer."
5555   (gnus-set-global-variables)
5556   (if (null article)
5557       nil
5558     (prog1
5559         (if gnus-summary-display-article-function
5560             (funcall gnus-summary-display-article-function article all-header)
5561           (gnus-article-prepare article all-header))
5562       (gnus-run-hooks 'gnus-select-article-hook)
5563       (when (and gnus-current-article
5564                  (not (zerop gnus-current-article)))
5565         (gnus-summary-goto-subject gnus-current-article))
5566       (gnus-summary-recenter)
5567       (when (and gnus-use-trees gnus-show-threads)
5568         (gnus-possibly-generate-tree article)
5569         (gnus-highlight-selected-tree article))
5570       ;; Successfully display article.
5571       (gnus-article-set-window-start
5572        (cdr (assq article gnus-newsgroup-bookmarks))))))
5573
5574 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5575   "Select the current article.
5576 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5577 non-nil, the article will be re-fetched even if it already present in
5578 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5579 be displayed."
5580   ;; Make sure we are in the summary buffer to work around bbdb bug.
5581   (unless (eq major-mode 'gnus-summary-mode)
5582     (set-buffer gnus-summary-buffer))
5583   (let ((article (or article (gnus-summary-article-number)))
5584         (all-headers (not (not all-headers))) ;Must be T or NIL.
5585         gnus-summary-display-article-function
5586         did)
5587     (and (not pseudo)
5588          (gnus-summary-article-pseudo-p article)
5589          (error "This is a pseudo-article"))
5590     (prog1
5591         (save-excursion
5592           (set-buffer gnus-summary-buffer)
5593           (if (or (and gnus-single-article-buffer
5594                        (or (null gnus-current-article)
5595                            (null gnus-article-current)
5596                            (null (get-buffer gnus-article-buffer))
5597                            (not (eq article (cdr gnus-article-current)))
5598                            (not (equal (car gnus-article-current)
5599                                        gnus-newsgroup-name))))
5600                   (and (not gnus-single-article-buffer)
5601                        (or (null gnus-current-article)
5602                            (not (eq gnus-current-article article))))
5603                   force)
5604               ;; The requested article is different from the current article.
5605               (prog1
5606                   (gnus-summary-display-article article all-headers)
5607                 (setq did article))
5608             (when (or all-headers gnus-show-all-headers)
5609               (gnus-article-show-all-headers))
5610             'old))
5611       (when did
5612         (gnus-article-set-window-start
5613          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5614
5615 (defun gnus-summary-set-current-mark (&optional current-mark)
5616   "Obsolete function."
5617   nil)
5618
5619 (defun gnus-summary-next-article (&optional unread subject backward push)
5620   "Select the next article.
5621 If UNREAD, only unread articles are selected.
5622 If SUBJECT, only articles with SUBJECT are selected.
5623 If BACKWARD, the previous article is selected instead of the next."
5624   (interactive "P")
5625   (cond
5626    ;; Is there such an article?
5627    ((and (gnus-summary-search-forward unread subject backward)
5628          (or (gnus-summary-display-article (gnus-summary-article-number))
5629              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5630     (gnus-summary-position-point))
5631    ;; If not, we try the first unread, if that is wanted.
5632    ((and subject
5633          gnus-auto-select-same
5634          (gnus-summary-first-unread-article))
5635     (gnus-summary-position-point)
5636     (gnus-message 6 "Wrapped"))
5637    ;; Try to get next/previous article not displayed in this group.
5638    ((and gnus-auto-extend-newsgroup
5639          (not unread) (not subject))
5640     (gnus-summary-goto-article
5641      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5642      nil (count-lines (point-min) (point))))
5643    ;; Go to next/previous group.
5644    (t
5645     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5646       (gnus-summary-jump-to-group gnus-newsgroup-name))
5647     (let ((cmd last-command-char)
5648           (point
5649            (save-excursion
5650              (set-buffer gnus-group-buffer)
5651              (point)))
5652           (group
5653            (if (eq gnus-keep-same-level 'best)
5654                (gnus-summary-best-group gnus-newsgroup-name)
5655              (gnus-summary-search-group backward gnus-keep-same-level))))
5656       ;; For some reason, the group window gets selected.  We change
5657       ;; it back.
5658       (select-window (get-buffer-window (current-buffer)))
5659       ;; Select next unread newsgroup automagically.
5660       (cond
5661        ((or (not gnus-auto-select-next)
5662             (not cmd))
5663         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5664        ((or (eq gnus-auto-select-next 'quietly)
5665             (and (eq gnus-auto-select-next 'slightly-quietly)
5666                  push)
5667             (and (eq gnus-auto-select-next 'almost-quietly)
5668                  (gnus-summary-last-article-p)))
5669         ;; Select quietly.
5670         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5671             (gnus-summary-exit)
5672           (gnus-message 7 "No more%s articles (%s)..."
5673                         (if unread " unread" "")
5674                         (if group (concat "selecting " group)
5675                           "exiting"))
5676           (gnus-summary-next-group nil group backward)))
5677        (t
5678         (when (gnus-key-press-event-p last-input-event)
5679           (gnus-summary-walk-group-buffer
5680            gnus-newsgroup-name cmd unread backward point))))))))
5681
5682 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5683   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5684                       (?\C-p (gnus-group-prev-unread-group 1))))
5685         (cursor-in-echo-area t)
5686         keve key group ended)
5687     (save-excursion
5688       (set-buffer gnus-group-buffer)
5689       (goto-char start)
5690       (setq group
5691             (if (eq gnus-keep-same-level 'best)
5692                 (gnus-summary-best-group gnus-newsgroup-name)
5693               (gnus-summary-search-group backward gnus-keep-same-level))))
5694     (while (not ended)
5695       (gnus-message
5696        5 "No more%s articles%s" (if unread " unread" "")
5697        (if (and group
5698                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5699            (format " (Type %s for %s [%s])"
5700                    (single-key-description cmd) group
5701                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5702          (format " (Type %s to exit %s)"
5703                  (single-key-description cmd)
5704                  gnus-newsgroup-name)))
5705       ;; Confirm auto selection.
5706       (setq key (car (setq keve (gnus-read-event-char))))
5707       (setq ended t)
5708       (cond
5709        ((assq key keystrokes)
5710         (let ((obuf (current-buffer)))
5711           (switch-to-buffer gnus-group-buffer)
5712           (when group
5713             (gnus-group-jump-to-group group))
5714           (eval (cadr (assq key keystrokes)))
5715           (setq group (gnus-group-group-name))
5716           (switch-to-buffer obuf))
5717         (setq ended nil))
5718        ((equal key cmd)
5719         (if (or (not group)
5720                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5721             (gnus-summary-exit)
5722           (gnus-summary-next-group nil group backward)))
5723        (t
5724         (push (cdr keve) unread-command-events))))))
5725
5726 (defun gnus-summary-next-unread-article ()
5727   "Select unread article after current one."
5728   (interactive)
5729   (gnus-summary-next-article
5730    (or (not (eq gnus-summary-goto-unread 'never))
5731        (gnus-summary-last-article-p (gnus-summary-article-number)))
5732    (and gnus-auto-select-same
5733         (gnus-summary-article-subject))))
5734
5735 (defun gnus-summary-prev-article (&optional unread subject)
5736   "Select the article after the current one.
5737 If UNREAD is non-nil, only unread articles are selected."
5738   (interactive "P")
5739   (gnus-summary-next-article unread subject t))
5740
5741 (defun gnus-summary-prev-unread-article ()
5742   "Select unread article before current one."
5743   (interactive)
5744   (gnus-summary-prev-article
5745    (or (not (eq gnus-summary-goto-unread 'never))
5746        (gnus-summary-first-article-p (gnus-summary-article-number)))
5747    (and gnus-auto-select-same
5748         (gnus-summary-article-subject))))
5749
5750 (defun gnus-summary-next-page (&optional lines circular)
5751   "Show next page of the selected article.
5752 If at the end of the current article, select the next article.
5753 LINES says how many lines should be scrolled up.
5754
5755 If CIRCULAR is non-nil, go to the start of the article instead of
5756 selecting the next article when reaching the end of the current
5757 article."
5758   (interactive "P")
5759   (setq gnus-summary-buffer (current-buffer))
5760   (gnus-set-global-variables)
5761   (let ((article (gnus-summary-article-number))
5762         (article-window (get-buffer-window gnus-article-buffer t))
5763         endp)
5764     ;; If the buffer is empty, we have no article.
5765     (unless article
5766       (error "No article to select"))
5767     (gnus-configure-windows 'article)
5768     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5769         (if (and (eq gnus-summary-goto-unread 'never)
5770                  (not (gnus-summary-last-article-p article)))
5771             (gnus-summary-next-article)
5772           (gnus-summary-next-unread-article))
5773       (if (or (null gnus-current-article)
5774               (null gnus-article-current)
5775               (/= article (cdr gnus-article-current))
5776               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5777           ;; Selected subject is different from current article's.
5778           (gnus-summary-display-article article)
5779         (when article-window
5780           (gnus-eval-in-buffer-window gnus-article-buffer
5781             (setq endp (gnus-article-next-page lines)))
5782           (when endp
5783             (cond (circular
5784                    (gnus-summary-beginning-of-article))
5785                   (lines
5786                    (gnus-message 3 "End of message"))
5787                   ((null lines)
5788                    (if (and (eq gnus-summary-goto-unread 'never)
5789                             (not (gnus-summary-last-article-p article)))
5790                        (gnus-summary-next-article)
5791                      (gnus-summary-next-unread-article))))))))
5792     (gnus-summary-recenter)
5793     (gnus-summary-position-point)))
5794
5795 (defun gnus-summary-prev-page (&optional lines move)
5796   "Show previous page of selected article.
5797 Argument LINES specifies lines to be scrolled down.
5798 If MOVE, move to the previous unread article if point is at
5799 the beginning of the buffer."
5800   (interactive "P")
5801   (let ((article (gnus-summary-article-number))
5802         (article-window (get-buffer-window gnus-article-buffer t))
5803         endp)
5804     (gnus-configure-windows 'article)
5805     (if (or (null gnus-current-article)
5806             (null gnus-article-current)
5807             (/= article (cdr gnus-article-current))
5808             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5809         ;; Selected subject is different from current article's.
5810         (gnus-summary-display-article article)
5811       (gnus-summary-recenter)
5812       (when article-window
5813         (gnus-eval-in-buffer-window gnus-article-buffer
5814           (setq endp (gnus-article-prev-page lines)))
5815         (when (and move endp)
5816           (cond (lines
5817                  (gnus-message 3 "Beginning of message"))
5818                 ((null lines)
5819                  (if (and (eq gnus-summary-goto-unread 'never)
5820                           (not (gnus-summary-first-article-p article)))
5821                      (gnus-summary-prev-article)
5822                    (gnus-summary-prev-unread-article))))))))
5823   (gnus-summary-position-point))
5824
5825 (defun gnus-summary-prev-page-or-article (&optional lines)
5826   "Show previous page of selected article.
5827 Argument LINES specifies lines to be scrolled down.
5828 If at the beginning of the article, go to the next article."
5829   (interactive "P")
5830   (gnus-summary-prev-page lines t))
5831
5832 (defun gnus-summary-scroll-up (lines)
5833   "Scroll up (or down) one line current article.
5834 Argument LINES specifies lines to be scrolled up (or down if negative)."
5835   (interactive "p")
5836   (gnus-configure-windows 'article)
5837   (gnus-summary-show-thread)
5838   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5839     (gnus-eval-in-buffer-window gnus-article-buffer
5840       (cond ((> lines 0)
5841              (when (gnus-article-next-page lines)
5842                (gnus-message 3 "End of message")))
5843             ((< lines 0)
5844              (gnus-article-prev-page (- lines))))))
5845   (gnus-summary-recenter)
5846   (gnus-summary-position-point))
5847
5848 (defun gnus-summary-scroll-down (lines)
5849   "Scroll down (or up) one line current article.
5850 Argument LINES specifies lines to be scrolled down (or up if negative)."
5851   (interactive "p")
5852   (gnus-summary-scroll-up (- lines)))
5853
5854 (defun gnus-summary-next-same-subject ()
5855   "Select next article which has the same subject as current one."
5856   (interactive)
5857   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5858
5859 (defun gnus-summary-prev-same-subject ()
5860   "Select previous article which has the same subject as current one."
5861   (interactive)
5862   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5863
5864 (defun gnus-summary-next-unread-same-subject ()
5865   "Select next unread article which has the same subject as current one."
5866   (interactive)
5867   (gnus-summary-next-article t (gnus-summary-article-subject)))
5868
5869 (defun gnus-summary-prev-unread-same-subject ()
5870   "Select previous unread article which has the same subject as current one."
5871   (interactive)
5872   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5873
5874 (defun gnus-summary-first-unread-article ()
5875   "Select the first unread article.
5876 Return nil if there are no unread articles."
5877   (interactive)
5878   (prog1
5879       (when (gnus-summary-first-subject t)
5880         (gnus-summary-show-thread)
5881         (gnus-summary-first-subject t)
5882         (gnus-summary-display-article (gnus-summary-article-number)))
5883     (gnus-summary-position-point)))
5884
5885 (defun gnus-summary-first-unread-subject ()
5886   "Place the point on the subject line of the first unread article.
5887 Return nil if there are no unread articles."
5888   (interactive)
5889   (prog1
5890       (when (gnus-summary-first-subject t)
5891         (gnus-summary-show-thread)
5892         (gnus-summary-first-subject t))
5893     (gnus-summary-position-point)))
5894
5895 (defun gnus-summary-first-article ()
5896   "Select the first article.
5897 Return nil if there are no articles."
5898   (interactive)
5899   (prog1
5900       (when (gnus-summary-first-subject)
5901       (gnus-summary-show-thread)
5902       (gnus-summary-first-subject)
5903       (gnus-summary-display-article (gnus-summary-article-number)))
5904     (gnus-summary-position-point)))
5905
5906 (defun gnus-summary-best-unread-article ()
5907   "Select the unread article with the highest score."
5908   (interactive)
5909   (let ((best -1000000)
5910         (data gnus-newsgroup-data)
5911         article score)
5912     (while data
5913       (and (gnus-data-unread-p (car data))
5914            (> (setq score
5915                     (gnus-summary-article-score (gnus-data-number (car data))))
5916               best)
5917            (setq best score
5918                  article (gnus-data-number (car data))))
5919       (setq data (cdr data)))
5920     (prog1
5921         (if article
5922             (gnus-summary-goto-article article)
5923           (error "No unread articles"))
5924       (gnus-summary-position-point))))
5925
5926 (defun gnus-summary-last-subject ()
5927   "Go to the last displayed subject line in the group."
5928   (let ((article (gnus-data-number (car (gnus-data-list t)))))
5929     (when article
5930       (gnus-summary-goto-subject article))))
5931
5932 (defun gnus-summary-goto-article (article &optional all-headers force)
5933   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
5934 If ALL-HEADERS is non-nil, no header lines are hidden.
5935 If FORCE, go to the article even if it isn't displayed.  If FORCE
5936 is a number, it is the line the article is to be displayed on."
5937   (interactive
5938    (list
5939     (completing-read
5940      "Article number or Message-ID: "
5941      (mapcar (lambda (number) (list (int-to-string number)))
5942              gnus-newsgroup-limit))
5943     current-prefix-arg
5944     t))
5945   (prog1
5946       (if (and (stringp article)
5947                (string-match "@" article))
5948           (gnus-summary-refer-article article)
5949         (when (stringp article)
5950           (setq article (string-to-number article)))
5951         (if (gnus-summary-goto-subject article force)
5952             (gnus-summary-display-article article all-headers)
5953           (gnus-message 4 "Couldn't go to article %s" article) nil))
5954     (gnus-summary-position-point)))
5955
5956 (defun gnus-summary-goto-last-article ()
5957   "Go to the previously read article."
5958   (interactive)
5959   (prog1
5960       (when gnus-last-article
5961         (gnus-summary-goto-article gnus-last-article nil t))
5962     (gnus-summary-position-point)))
5963
5964 (defun gnus-summary-pop-article (number)
5965   "Pop one article off the history and go to the previous.
5966 NUMBER articles will be popped off."
5967   (interactive "p")
5968   (let (to)
5969     (setq gnus-newsgroup-history
5970           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
5971     (if to
5972         (gnus-summary-goto-article (car to) nil t)
5973       (error "Article history empty")))
5974   (gnus-summary-position-point))
5975
5976 ;; Summary commands and functions for limiting the summary buffer.
5977
5978 (defun gnus-summary-limit-to-articles (n)
5979   "Limit the summary buffer to the next N articles.
5980 If not given a prefix, use the process marked articles instead."
5981   (interactive "P")
5982   (prog1
5983       (let ((articles (gnus-summary-work-articles n)))
5984         (setq gnus-newsgroup-processable nil)
5985         (gnus-summary-limit articles))
5986     (gnus-summary-position-point)))
5987
5988 (defun gnus-summary-pop-limit (&optional total)
5989   "Restore the previous limit.
5990 If given a prefix, remove all limits."
5991   (interactive "P")
5992   (when total
5993     (setq gnus-newsgroup-limits
5994           (list (mapcar (lambda (h) (mail-header-number h))
5995                         gnus-newsgroup-headers))))
5996   (unless gnus-newsgroup-limits
5997     (error "No limit to pop"))
5998   (prog1
5999       (gnus-summary-limit nil 'pop)
6000     (gnus-summary-position-point)))
6001
6002 (defun gnus-summary-limit-to-subject (subject &optional header)
6003   "Limit the summary buffer to articles that have subjects that match a regexp."
6004   (interactive "sLimit to subject (regexp): ")
6005   (unless header
6006     (setq header "subject"))
6007   (when (not (equal "" subject))
6008     (prog1
6009         (let ((articles (gnus-summary-find-matching
6010                          (or header "subject") subject 'all)))
6011           (unless articles
6012             (error "Found no matches for \"%s\"" subject))
6013           (gnus-summary-limit articles))
6014       (gnus-summary-position-point))))
6015
6016 (defun gnus-summary-limit-to-author (from)
6017   "Limit the summary buffer to articles that have authors that match a regexp."
6018   (interactive "sLimit to author (regexp): ")
6019   (gnus-summary-limit-to-subject from "from"))
6020
6021 (defun gnus-summary-limit-to-age (age &optional younger-p)
6022   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6023 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6024 articles that are younger than AGE days."
6025   (interactive "nTime in days: \nP")
6026   (prog1
6027       (let ((data gnus-newsgroup-data)
6028             (cutoff (days-to-time age))
6029             articles d date is-younger)
6030         (while (setq d (pop data))
6031           (when (and (vectorp (gnus-data-header d))
6032                      (setq date (mail-header-date (gnus-data-header d))))
6033             (setq is-younger (time-less-p
6034                               (time-since (date-to-time date))
6035                               cutoff))
6036             (when (if younger-p
6037                       is-younger
6038                     (not is-younger))
6039               (push (gnus-data-number d) articles))))
6040         (gnus-summary-limit (nreverse articles)))
6041     (gnus-summary-position-point)))
6042
6043 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6044 (make-obsolete
6045  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6046
6047 (defun gnus-summary-limit-to-unread (&optional all)
6048   "Limit the summary buffer to articles that are not marked as read.
6049 If ALL is non-nil, limit strictly to unread articles."
6050   (interactive "P")
6051   (if all
6052       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6053     (gnus-summary-limit-to-marks
6054      ;; Concat all the marks that say that an article is read and have
6055      ;; those removed.
6056      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6057            gnus-killed-mark gnus-kill-file-mark
6058            gnus-low-score-mark gnus-expirable-mark
6059            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6060            gnus-duplicate-mark gnus-souped-mark)
6061      'reverse)))
6062
6063 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6064 (make-obsolete 'gnus-summary-delete-marked-with
6065                'gnus-summary-limit-exlude-marks)
6066
6067 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6068   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6069 If REVERSE, limit the summary buffer to articles that are marked
6070 with MARKS.  MARKS can either be a string of marks or a list of marks.
6071 Returns how many articles were removed."
6072   (interactive "sMarks: ")
6073   (gnus-summary-limit-to-marks marks t))
6074
6075 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6076   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6077 If REVERSE (the prefix), limit the summary buffer to articles that are
6078 not marked with MARKS.  MARKS can either be a string of marks or a
6079 list of marks.
6080 Returns how many articles were removed."
6081   (interactive "sMarks: \nP")
6082   (prog1
6083       (let ((data gnus-newsgroup-data)
6084             (marks (if (listp marks) marks
6085                      (append marks nil))) ; Transform to list.
6086             articles)
6087         (while data
6088           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6089                   (memq (gnus-data-mark (car data)) marks))
6090             (push (gnus-data-number (car data)) articles))
6091           (setq data (cdr data)))
6092         (gnus-summary-limit articles))
6093     (gnus-summary-position-point)))
6094
6095 (defun gnus-summary-limit-to-score (&optional score)
6096   "Limit to articles with score at or above SCORE."
6097   (interactive "P")
6098   (setq score (if score
6099                   (prefix-numeric-value score)
6100                 (or gnus-summary-default-score 0)))
6101   (let ((data gnus-newsgroup-data)
6102         articles)
6103     (while data
6104       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6105                 score)
6106         (push (gnus-data-number (car data)) articles))
6107       (setq data (cdr data)))
6108     (prog1
6109         (gnus-summary-limit articles)
6110       (gnus-summary-position-point))))
6111
6112 (defun gnus-summary-limit-include-thread (id)
6113   "Display all the hidden articles that in the current thread."
6114   (interactive (list (mail-header-id (gnus-summary-article-header))))
6115   (let ((articles (gnus-articles-in-thread
6116                    (gnus-id-to-thread (gnus-root-id id)))))
6117     (prog1
6118         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6119       (gnus-summary-position-point))))
6120
6121 (defun gnus-summary-limit-include-dormant ()
6122   "Display all the hidden articles that are marked as dormant.
6123 Note that this command only works on a subset of the articles currently
6124 fetched for this group."
6125   (interactive)
6126   (unless gnus-newsgroup-dormant
6127     (error "There are no dormant articles in this group"))
6128   (prog1
6129       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6130     (gnus-summary-position-point)))
6131
6132 (defun gnus-summary-limit-exclude-dormant ()
6133   "Hide all dormant articles."
6134   (interactive)
6135   (prog1
6136       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6137     (gnus-summary-position-point)))
6138
6139 (defun gnus-summary-limit-exclude-childless-dormant ()
6140   "Hide all dormant articles that have no children."
6141   (interactive)
6142   (let ((data (gnus-data-list t))
6143         articles d children)
6144     ;; Find all articles that are either not dormant or have
6145     ;; children.
6146     (while (setq d (pop data))
6147       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6148                 (and (setq children
6149                            (gnus-article-children (gnus-data-number d)))
6150                      (let (found)
6151                        (while children
6152                          (when (memq (car children) articles)
6153                            (setq children nil
6154                                  found t))
6155                          (pop children))
6156                        found)))
6157         (push (gnus-data-number d) articles)))
6158     ;; Do the limiting.
6159     (prog1
6160         (gnus-summary-limit articles)
6161       (gnus-summary-position-point))))
6162
6163 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6164   "Mark all unread excluded articles as read.
6165 If ALL, mark even excluded ticked and dormants as read."
6166   (interactive "P")
6167   (let ((articles (gnus-sorted-complement
6168                    (sort
6169                     (mapcar (lambda (h) (mail-header-number h))
6170                             gnus-newsgroup-headers)
6171                     '<)
6172                    (sort gnus-newsgroup-limit '<)))
6173         article)
6174     (setq gnus-newsgroup-unreads
6175           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6176     (if all
6177         (setq gnus-newsgroup-dormant nil
6178               gnus-newsgroup-marked nil
6179               gnus-newsgroup-reads
6180               (nconc
6181                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6182                gnus-newsgroup-reads))
6183       (while (setq article (pop articles))
6184         (unless (or (memq article gnus-newsgroup-dormant)
6185                     (memq article gnus-newsgroup-marked))
6186           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6187
6188 (defun gnus-summary-limit (articles &optional pop)
6189   (if pop
6190       ;; We pop the previous limit off the stack and use that.
6191       (setq articles (car gnus-newsgroup-limits)
6192             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6193     ;; We use the new limit, so we push the old limit on the stack.
6194     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6195   ;; Set the limit.
6196   (setq gnus-newsgroup-limit articles)
6197   (let ((total (length gnus-newsgroup-data))
6198         (data (gnus-data-find-list (gnus-summary-article-number)))
6199         (gnus-summary-mark-below nil)   ; Inhibit this.
6200         found)
6201     ;; This will do all the work of generating the new summary buffer
6202     ;; according to the new limit.
6203     (gnus-summary-prepare)
6204     ;; Hide any threads, possibly.
6205     (and gnus-show-threads
6206          gnus-thread-hide-subtree
6207          (gnus-summary-hide-all-threads))
6208     ;; Try to return to the article you were at, or one in the
6209     ;; neighborhood.
6210     (when data
6211       ;; We try to find some article after the current one.
6212       (while data
6213         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6214           (setq data nil
6215                 found t))
6216         (setq data (cdr data))))
6217     (unless found
6218       ;; If there is no data, that means that we were after the last
6219       ;; article.  The same goes when we can't find any articles
6220       ;; after the current one.
6221       (goto-char (point-max))
6222       (gnus-summary-find-prev))
6223     (gnus-set-mode-line 'summary)
6224     ;; We return how many articles were removed from the summary
6225     ;; buffer as a result of the new limit.
6226     (- total (length gnus-newsgroup-data))))
6227
6228 (defsubst gnus-invisible-cut-children (threads)
6229   (let ((num 0))
6230     (while threads
6231       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6232         (incf num))
6233       (pop threads))
6234     (< num 2)))
6235
6236 (defsubst gnus-cut-thread (thread)
6237   "Go forwards in the thread until we find an article that we want to display."
6238   (when (or (eq gnus-fetch-old-headers 'some)
6239             (eq gnus-fetch-old-headers 'invisible)
6240             (eq gnus-build-sparse-threads 'some)
6241             (eq gnus-build-sparse-threads 'more))
6242     ;; Deal with old-fetched headers and sparse threads.
6243     (while (and
6244             thread
6245             (or
6246              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6247              (gnus-summary-article-ancient-p
6248               (mail-header-number (car thread))))
6249             (if (or (<= (length (cdr thread)) 1)
6250                     (eq gnus-fetch-old-headers 'invisible))
6251                 (setq gnus-newsgroup-limit
6252                       (delq (mail-header-number (car thread))
6253                             gnus-newsgroup-limit)
6254                       thread (cadr thread))
6255               (when (gnus-invisible-cut-children (cdr thread))
6256                 (let ((th (cdr thread)))
6257                   (while th
6258                     (if (memq (mail-header-number (caar th))
6259                               gnus-newsgroup-limit)
6260                         (setq thread (car th)
6261                               th nil)
6262                       (setq th (cdr th))))))))))
6263   thread)
6264
6265 (defun gnus-cut-threads (threads)
6266   "Cut off all uninteresting articles from the beginning of threads."
6267   (when (or (eq gnus-fetch-old-headers 'some)
6268             (eq gnus-fetch-old-headers 'invisible)
6269             (eq gnus-build-sparse-threads 'some)
6270             (eq gnus-build-sparse-threads 'more))
6271     (let ((th threads))
6272       (while th
6273         (setcar th (gnus-cut-thread (car th)))
6274         (setq th (cdr th)))))
6275   ;; Remove nixed out threads.
6276   (delq nil threads))
6277
6278 (defun gnus-summary-initial-limit (&optional show-if-empty)
6279   "Figure out what the initial limit is supposed to be on group entry.
6280 This entails weeding out unwanted dormants, low-scored articles,
6281 fetch-old-headers verbiage, and so on."
6282   ;; Most groups have nothing to remove.
6283   (if (or gnus-inhibit-limiting
6284           (and (null gnus-newsgroup-dormant)
6285                (not (eq gnus-fetch-old-headers 'some))
6286                (not (eq gnus-fetch-old-headers 'invisible))
6287                (null gnus-summary-expunge-below)
6288                (not (eq gnus-build-sparse-threads 'some))
6289                (not (eq gnus-build-sparse-threads 'more))
6290                (null gnus-thread-expunge-below)
6291                (not gnus-use-nocem)))
6292       ()                                ; Do nothing.
6293     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6294     (setq gnus-newsgroup-limit nil)
6295     (mapatoms
6296      (lambda (node)
6297        (unless (car (symbol-value node))
6298          ;; These threads have no parents -- they are roots.
6299          (let ((nodes (cdr (symbol-value node)))
6300                thread)
6301            (while nodes
6302              (if (and gnus-thread-expunge-below
6303                       (< (gnus-thread-total-score (car nodes))
6304                          gnus-thread-expunge-below))
6305                  (gnus-expunge-thread (pop nodes))
6306                (setq thread (pop nodes))
6307                (gnus-summary-limit-children thread))))))
6308      gnus-newsgroup-dependencies)
6309     ;; If this limitation resulted in an empty group, we might
6310     ;; pop the previous limit and use it instead.
6311     (when (and (not gnus-newsgroup-limit)
6312                show-if-empty)
6313       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6314     gnus-newsgroup-limit))
6315
6316 (defun gnus-summary-limit-children (thread)
6317   "Return 1 if this subthread is visible and 0 if it is not."
6318   ;; First we get the number of visible children to this thread.  This
6319   ;; is done by recursing down the thread using this function, so this
6320   ;; will really go down to a leaf article first, before slowly
6321   ;; working its way up towards the root.
6322   (when thread
6323     (let ((children
6324            (if (cdr thread)
6325                (apply '+ (mapcar 'gnus-summary-limit-children
6326                                  (cdr thread)))
6327              0))
6328           (number (mail-header-number (car thread)))
6329           score)
6330       (if (and
6331            (not (memq number gnus-newsgroup-marked))
6332            (or
6333             ;; If this article is dormant and has absolutely no visible
6334             ;; children, then this article isn't visible.
6335             (and (memq number gnus-newsgroup-dormant)
6336                  (zerop children))
6337             ;; If this is "fetch-old-headered" and there is no
6338             ;; visible children, then we don't want this article.
6339             (and (eq gnus-fetch-old-headers 'some)
6340                  (gnus-summary-article-ancient-p number)
6341                  (zerop children))
6342             ;; If this is "fetch-old-headered" and `invisible', then
6343             ;; we don't want this article.
6344             (and (eq gnus-fetch-old-headers 'invisible)
6345                  (gnus-summary-article-ancient-p number))
6346             ;; If this is a sparsely inserted article with no children,
6347             ;; we don't want it.
6348             (and (eq gnus-build-sparse-threads 'some)
6349                  (gnus-summary-article-sparse-p number)
6350                  (zerop children))
6351             ;; If we use expunging, and this article is really
6352             ;; low-scored, then we don't want this article.
6353             (when (and gnus-summary-expunge-below
6354                        (< (setq score
6355                                 (or (cdr (assq number gnus-newsgroup-scored))
6356                                     gnus-summary-default-score))
6357                           gnus-summary-expunge-below))
6358               ;; We increase the expunge-tally here, but that has
6359               ;; nothing to do with the limits, really.
6360               (incf gnus-newsgroup-expunged-tally)
6361               ;; We also mark as read here, if that's wanted.
6362               (when (and gnus-summary-mark-below
6363                          (< score gnus-summary-mark-below))
6364                 (setq gnus-newsgroup-unreads
6365                       (delq number gnus-newsgroup-unreads))
6366                 (if gnus-newsgroup-auto-expire
6367                     (push number gnus-newsgroup-expirable)
6368                   (push (cons number gnus-low-score-mark)
6369                         gnus-newsgroup-reads)))
6370               t)
6371             ;; Check NoCeM things.
6372             (if (and gnus-use-nocem
6373                      (gnus-nocem-unwanted-article-p
6374                       (mail-header-id (car thread))))
6375                 (progn
6376                   (setq gnus-newsgroup-unreads
6377                         (delq number gnus-newsgroup-unreads))
6378                   t))))
6379           ;; Nope, invisible article.
6380           0
6381         ;; Ok, this article is to be visible, so we add it to the limit
6382         ;; and return 1.
6383         (push number gnus-newsgroup-limit)
6384         1))))
6385
6386 (defun gnus-expunge-thread (thread)
6387   "Mark all articles in THREAD as read."
6388   (let* ((number (mail-header-number (car thread))))
6389     (incf gnus-newsgroup-expunged-tally)
6390     ;; We also mark as read here, if that's wanted.
6391     (setq gnus-newsgroup-unreads
6392           (delq number gnus-newsgroup-unreads))
6393     (if gnus-newsgroup-auto-expire
6394         (push number gnus-newsgroup-expirable)
6395       (push (cons number gnus-low-score-mark)
6396             gnus-newsgroup-reads)))
6397   ;; Go recursively through all subthreads.
6398   (mapcar 'gnus-expunge-thread (cdr thread)))
6399
6400 ;; Summary article oriented commands
6401
6402 (defun gnus-summary-refer-parent-article (n)
6403   "Refer parent article N times.
6404 If N is negative, go to ancestor -N instead.
6405 The difference between N and the number of articles fetched is returned."
6406   (interactive "p")
6407   (let ((skip 1)
6408         error header ref)
6409     (when (not (natnump n))
6410       (setq skip (abs n)
6411             n 1))
6412     (while (and (> n 0)
6413                 (not error))
6414       (setq header (gnus-summary-article-header))
6415       (if (and (eq (mail-header-number header)
6416                    (cdr gnus-article-current))
6417                (equal gnus-newsgroup-name
6418                       (car gnus-article-current)))
6419           ;; If we try to find the parent of the currently
6420           ;; displayed article, then we take a look at the actual
6421           ;; References header, since this is slightly more
6422           ;; reliable than the References field we got from the
6423           ;; server.
6424           (save-excursion
6425             (set-buffer gnus-original-article-buffer)
6426             (nnheader-narrow-to-headers)
6427             (unless (setq ref (message-fetch-field "references"))
6428               (setq ref (message-fetch-field "in-reply-to")))
6429             (widen))
6430         (setq ref
6431               ;; It's not the current article, so we take a bet on
6432               ;; the value we got from the server.
6433               (mail-header-references header)))
6434       (if (and ref
6435                (not (equal ref "")))
6436           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6437             (gnus-message 1 "Couldn't find parent"))
6438         (gnus-message 1 "No references in article %d"
6439                       (gnus-summary-article-number))
6440         (setq error t))
6441       (decf n))
6442     (gnus-summary-position-point)
6443     n))
6444
6445 (defun gnus-summary-refer-references ()
6446   "Fetch all articles mentioned in the References header.
6447 Return the number of articles fetched."
6448   (interactive)
6449   (let ((ref (mail-header-references (gnus-summary-article-header)))
6450         (current (gnus-summary-article-number))
6451         (n 0))
6452     (if (or (not ref)
6453             (equal ref ""))
6454         (error "No References in the current article")
6455       ;; For each Message-ID in the References header...
6456       (while (string-match "<[^>]*>" ref)
6457         (incf n)
6458         ;; ... fetch that article.
6459         (gnus-summary-refer-article
6460          (prog1 (match-string 0 ref)
6461            (setq ref (substring ref (match-end 0))))))
6462       (gnus-summary-goto-subject current)
6463       (gnus-summary-position-point)
6464       n)))
6465
6466 (defun gnus-summary-refer-thread (&optional limit)
6467   "Fetch all articles in the current thread.
6468 If LIMIT (the numerical prefix), fetch that many old headers instead
6469 of what's specified by the `gnus-refer-thread-limit' variable."
6470   (interactive "P")
6471   (let ((id (mail-header-id (gnus-summary-article-header)))
6472         (limit (if limit (prefix-numeric-value limit)
6473                  gnus-refer-thread-limit)))
6474     ;; We want to fetch LIMIT *old* headers, but we also have to
6475     ;; re-fetch all the headers in the current buffer, because many of
6476     ;; them may be undisplayed.  So we adjust LIMIT.
6477     (when (numberp limit)
6478       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6479     (unless (eq gnus-fetch-old-headers 'invisible)
6480       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6481       ;; Retrieve the headers and read them in.
6482       (if (eq (gnus-retrieve-headers
6483                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6484               'nov)
6485           (gnus-build-all-threads)
6486         (error "Can't fetch thread from backends that don't support NOV"))
6487       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6488     (gnus-summary-limit-include-thread id)))
6489
6490 (defun gnus-summary-refer-article (message-id &optional arg)
6491   "Fetch an article specified by MESSAGE-ID.
6492 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6493 or `gnus-select-method', no matter what backend the article comes from."
6494   (interactive "sMessage-ID: \nP")
6495   (when (and (stringp message-id)
6496              (not (zerop (length message-id))))
6497     ;; Construct the correct Message-ID if necessary.
6498     ;; Suggested by tale@pawl.rpi.edu.
6499     (unless (string-match "^<" message-id)
6500       (setq message-id (concat "<" message-id)))
6501     (unless (string-match ">$" message-id)
6502       (setq message-id (concat message-id ">")))
6503     (let* ((header (gnus-id-to-header message-id))
6504            (sparse (and header
6505                         (gnus-summary-article-sparse-p
6506                          (mail-header-number header))
6507                         (memq (mail-header-number header)
6508                               gnus-newsgroup-limit))))
6509       (cond
6510        ;; If the article is present in the buffer we just go to it.
6511        ((and header
6512              (or (not (gnus-summary-article-sparse-p
6513                        (mail-header-number header)))
6514                  sparse))
6515         (prog1
6516             (gnus-summary-goto-article
6517              (mail-header-number header) nil t)
6518           (when sparse
6519             (gnus-summary-update-article (mail-header-number header)))))
6520        (t
6521         ;; We fetch the article
6522         (let ((gnus-override-method
6523                (cond ((gnus-news-group-p gnus-newsgroup-name)
6524                       gnus-refer-article-method)
6525                      (arg
6526                       (or gnus-refer-article-method gnus-select-method))
6527                      (t nil)))
6528               number)
6529           ;; Start the special refer-article method, if necessary.
6530           (when (and gnus-refer-article-method
6531                      (gnus-news-group-p gnus-newsgroup-name))
6532             (gnus-check-server gnus-refer-article-method))
6533           ;; Fetch the header, and display the article.
6534           (if (setq number (gnus-summary-insert-subject message-id))
6535               (gnus-summary-select-article nil nil nil number)
6536             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6537
6538 (defun gnus-summary-edit-parameters ()
6539   "Edit the group parameters of the current group."
6540   (interactive)
6541   (gnus-group-edit-group gnus-newsgroup-name 'params))
6542
6543 (defun gnus-summary-enter-digest-group (&optional force)
6544   "Enter an nndoc group based on the current article.
6545 If FORCE, force a digest interpretation.  If not, try
6546 to guess what the document format is."
6547   (interactive "P")
6548   (let ((conf gnus-current-window-configuration))
6549     (save-excursion
6550       (gnus-summary-select-article))
6551     (setq gnus-current-window-configuration conf)
6552     (let* ((name (format "%s-%d"
6553                          (gnus-group-prefixed-name
6554                           gnus-newsgroup-name (list 'nndoc ""))
6555                          (save-excursion
6556                            (set-buffer gnus-summary-buffer)
6557                            gnus-current-article)))
6558            (ogroup gnus-newsgroup-name)
6559            (params (append (gnus-info-params (gnus-get-info ogroup))
6560                            (list (cons 'to-group ogroup))
6561                            (list (cons 'save-article-group ogroup))))
6562            (case-fold-search t)
6563            (buf (current-buffer))
6564            dig)
6565       (save-excursion
6566         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6567         (insert-buffer-substring gnus-original-article-buffer)
6568         ;; Remove lines that may lead nndoc to misinterpret the
6569         ;; document type.
6570         (narrow-to-region
6571          (goto-char (point-min))
6572          (or (search-forward "\n\n" nil t) (point)))
6573         (goto-char (point-min))
6574         (delete-matching-lines "^\\(Path\\):\\|^From ")
6575         (widen))
6576       (unwind-protect
6577           (if (gnus-group-read-ephemeral-group
6578                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6579                             (nndoc-article-type
6580                              ,(if force 'digest 'guess))) t)
6581               ;; Make all postings to this group go to the parent group.
6582               (nconc (gnus-info-params (gnus-get-info name))
6583                      params)
6584             ;; Couldn't select this doc group.
6585             (switch-to-buffer buf)
6586             (gnus-set-global-variables)
6587             (gnus-configure-windows 'summary)
6588             (gnus-message 3 "Article couldn't be entered?"))
6589         (kill-buffer dig)))))
6590
6591 (defun gnus-summary-read-document (n)
6592   "Open a new group based on the current article(s).
6593 This will allow you to read digests and other similar
6594 documents as newsgroups.
6595 Obeys the standard process/prefix convention."
6596   (interactive "P")
6597   (let* ((articles (gnus-summary-work-articles n))
6598          (ogroup gnus-newsgroup-name)
6599          (params (append (gnus-info-params (gnus-get-info ogroup))
6600                          (list (cons 'to-group ogroup))))
6601          article group egroup groups vgroup)
6602     (while (setq article (pop articles))
6603       (setq group (format "%s-%d" gnus-newsgroup-name article))
6604       (gnus-summary-remove-process-mark article)
6605       (when (gnus-summary-display-article article)
6606         (save-excursion
6607           (with-temp-buffer
6608             (insert-buffer-substring gnus-original-article-buffer)
6609             ;; Remove some headers that may lead nndoc to make
6610             ;; the wrong guess.
6611             (message-narrow-to-head)
6612             (goto-char (point-min))
6613             (delete-matching-lines "^\\(Path\\):\\|^From ")
6614             (widen)
6615             (if (setq egroup
6616                       (gnus-group-read-ephemeral-group
6617                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6618                                      (nndoc-article-type guess))
6619                        t nil t))
6620                 (progn
6621                   ;; Make all postings to this group go to the parent group.
6622                   (nconc (gnus-info-params (gnus-get-info egroup))
6623                          params)
6624                   (push egroup groups))
6625               ;; Couldn't select this doc group.
6626               (gnus-error 3 "Article couldn't be entered"))))))
6627     ;; Now we have selected all the documents.
6628     (cond
6629      ((not groups)
6630       (error "None of the articles could be interpreted as documents"))
6631      ((gnus-group-read-ephemeral-group
6632        (setq vgroup (format
6633                      "nnvirtual:%s-%s" gnus-newsgroup-name
6634                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6635        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6636        t
6637        (cons (current-buffer) 'summary)))
6638      (t
6639       (error "Couldn't select virtual nndoc group")))))
6640
6641 (defun gnus-summary-isearch-article (&optional regexp-p)
6642   "Do incremental search forward on the current article.
6643 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6644   (interactive "P")
6645   (gnus-summary-select-article)
6646   (gnus-configure-windows 'article)
6647   (gnus-eval-in-buffer-window gnus-article-buffer
6648     (save-restriction
6649       (widen)
6650       (isearch-forward regexp-p))))
6651
6652 (defun gnus-summary-search-article-forward (regexp &optional backward)
6653   "Search for an article containing REGEXP forward.
6654 If BACKWARD, search backward instead."
6655   (interactive
6656    (list (read-string
6657           (format "Search article %s (regexp%s): "
6658                   (if current-prefix-arg "backward" "forward")
6659                   (if gnus-last-search-regexp
6660                       (concat ", default " gnus-last-search-regexp)
6661                     "")))
6662          current-prefix-arg))
6663   (if (string-equal regexp "")
6664       (setq regexp (or gnus-last-search-regexp ""))
6665     (setq gnus-last-search-regexp regexp))
6666   (if (gnus-summary-search-article regexp backward)
6667       (gnus-summary-show-thread)
6668     (error "Search failed: \"%s\"" regexp)))
6669
6670 (defun gnus-summary-search-article-backward (regexp)
6671   "Search for an article containing REGEXP backward."
6672   (interactive
6673    (list (read-string
6674           (format "Search article backward (regexp%s): "
6675                   (if gnus-last-search-regexp
6676                       (concat ", default " gnus-last-search-regexp)
6677                     "")))))
6678   (gnus-summary-search-article-forward regexp 'backward))
6679
6680 (defun gnus-summary-search-article (regexp &optional backward)
6681   "Search for an article containing REGEXP.
6682 Optional argument BACKWARD means do search for backward.
6683 `gnus-select-article-hook' is not called during the search."
6684   ;; We have to require this here to make sure that the following
6685   ;; dynamic binding isn't shadowed by autoloading.
6686   (require 'gnus-async)
6687   (let ((gnus-select-article-hook nil)  ;Disable hook.
6688         (gnus-article-display-hook nil)
6689         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6690         (gnus-use-article-prefetch nil)
6691         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6692         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6693         (sum (current-buffer))
6694         (found nil)
6695         point)
6696     (gnus-save-hidden-threads
6697       (gnus-summary-select-article)
6698       (set-buffer gnus-article-buffer)
6699       (when backward
6700         (forward-line -1))
6701       (while (not found)
6702         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6703         (if (if backward
6704                 (re-search-backward regexp nil t)
6705               (re-search-forward regexp nil t))
6706             ;; We found the regexp.
6707             (progn
6708               (setq found 'found)
6709               (beginning-of-line)
6710               (set-window-start
6711                (get-buffer-window (current-buffer))
6712                (point))
6713               (forward-line 1)
6714               (set-buffer sum)
6715               (setq point (point)))
6716           ;; We didn't find it, so we go to the next article.
6717           (set-buffer sum)
6718           (setq found 'not)
6719           (while (eq found 'not)
6720             (if (not (if backward (gnus-summary-find-prev)
6721                        (gnus-summary-find-next)))
6722                 ;; No more articles.
6723                 (setq found t)
6724               ;; Select the next article and adjust point.
6725               (unless (gnus-summary-article-sparse-p
6726                        (gnus-summary-article-number))
6727                 (setq found nil)
6728                 (gnus-summary-select-article)
6729                 (set-buffer gnus-article-buffer)
6730                 (widen)
6731                 (goto-char (if backward (point-max) (point-min))))))))
6732       (gnus-message 7 ""))
6733     ;; Return whether we found the regexp.
6734     (when (eq found 'found)
6735       (goto-char point)
6736       (gnus-summary-show-thread)
6737       (gnus-summary-goto-subject gnus-current-article)
6738       (gnus-summary-position-point)
6739       t)))
6740
6741 (defun gnus-summary-find-matching (header regexp &optional backward unread
6742                                           not-case-fold)
6743   "Return a list of all articles that match REGEXP on HEADER.
6744 The search stars on the current article and goes forwards unless
6745 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6746 If UNREAD is non-nil, only unread articles will
6747 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6748 in the comparisons."
6749   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6750                 (gnus-data-find-list
6751                  (gnus-summary-article-number) (gnus-data-list backward))))
6752         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6753         (case-fold-search (not not-case-fold))
6754         articles d)
6755     (unless (fboundp (intern (concat "mail-header-" header)))
6756       (error "%s is not a valid header" header))
6757     (while data
6758       (setq d (car data))
6759       (and (or (not unread)             ; We want all articles...
6760                (gnus-data-unread-p d))  ; Or just unreads.
6761            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6762            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6763            (push (gnus-data-number d) articles)) ; Success!
6764       (setq data (cdr data)))
6765     (nreverse articles)))
6766
6767 (defun gnus-summary-execute-command (header regexp command &optional backward)
6768   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6769 If HEADER is an empty string (or nil), the match is done on the entire
6770 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6771   (interactive
6772    (list (let ((completion-ignore-case t))
6773            (completing-read
6774             "Header name: "
6775             (mapcar (lambda (string) (list string))
6776                     '("Number" "Subject" "From" "Lines" "Date"
6777                       "Message-ID" "Xref" "References" "Body"))
6778             nil 'require-match))
6779          (read-string "Regexp: ")
6780          (read-key-sequence "Command: ")
6781          current-prefix-arg))
6782   (when (equal header "Body")
6783     (setq header ""))
6784   ;; Hidden thread subtrees must be searched as well.
6785   (gnus-summary-show-all-threads)
6786   ;; We don't want to change current point nor window configuration.
6787   (save-excursion
6788     (save-window-excursion
6789       (gnus-message 6 "Executing %s..." (key-description command))
6790       ;; We'd like to execute COMMAND interactively so as to give arguments.
6791       (gnus-execute header regexp
6792                     `(call-interactively ',(key-binding command))
6793                     backward)
6794       (gnus-message 6 "Executing %s...done" (key-description command)))))
6795
6796 (defun gnus-summary-beginning-of-article ()
6797   "Scroll the article back to the beginning."
6798   (interactive)
6799   (gnus-summary-select-article)
6800   (gnus-configure-windows 'article)
6801   (gnus-eval-in-buffer-window gnus-article-buffer
6802     (widen)
6803     (goto-char (point-min))
6804     (when gnus-page-broken
6805       (gnus-narrow-to-page))))
6806
6807 (defun gnus-summary-end-of-article ()
6808   "Scroll to the end of the article."
6809   (interactive)
6810   (gnus-summary-select-article)
6811   (gnus-configure-windows 'article)
6812   (gnus-eval-in-buffer-window gnus-article-buffer
6813     (widen)
6814     (goto-char (point-max))
6815     (recenter -3)
6816     (when gnus-page-broken
6817       (gnus-narrow-to-page))))
6818
6819 (defun gnus-summary-print-article (&optional filename n)
6820   "Generate and print a PostScript image of the N next (mail) articles.
6821
6822 If N is negative, print the N previous articles.  If N is nil and articles
6823 have been marked with the process mark, print these instead.
6824
6825 If the optional second argument FILENAME is nil, send the image to the
6826 printer.  If FILENAME is a string, save the PostScript image in a file with
6827 that name.  If FILENAME is a number, prompt the user for the name of the file
6828 to save in."
6829   (interactive (list (ps-print-preprint current-prefix-arg)
6830                      current-prefix-arg))
6831   (dolist (article (gnus-summary-work-articles n))
6832     (gnus-summary-select-article nil nil 'pseudo article)
6833     (gnus-eval-in-buffer-window gnus-article-buffer
6834       (let ((buffer (generate-new-buffer " *print*")))
6835         (unwind-protect
6836             (progn
6837               (copy-to-buffer buffer (point-min) (point-max))
6838               (set-buffer buffer)
6839               (gnus-article-delete-invisible-text)
6840               (let ((ps-left-header
6841                      (list
6842                       (concat "("
6843                               (mail-header-subject gnus-current-headers) ")")
6844                       (concat "("
6845                               (mail-header-from gnus-current-headers) ")")))
6846                     (ps-right-header
6847                      (list
6848                       "/pagenumberstring load"
6849                       (concat "("
6850                               (mail-header-date gnus-current-headers) ")"))))
6851                 (gnus-run-hooks 'gnus-ps-print-hook)
6852                 (save-excursion
6853                   (ps-print-buffer-with-faces filename))))
6854           (kill-buffer buffer))))))
6855
6856 (defun gnus-summary-show-article (&optional arg)
6857   "Force re-fetching of the current article.
6858 If ARG (the prefix) is non-nil, show the raw article without any
6859 article massaging functions being run."
6860   (interactive "P")
6861   (if (not arg)
6862       ;; Select the article the normal way.
6863       (gnus-summary-select-article nil 'force)
6864     ;; Bind the article treatment functions to nil.
6865     (let ((gnus-have-all-headers t)
6866           gnus-article-display-hook
6867           gnus-article-prepare-hook
6868           gnus-article-decode-hook
6869           gnus-break-pages
6870           gnus-show-mime
6871           gnus-visual)
6872       (gnus-summary-select-article nil 'force)))
6873   (gnus-summary-goto-subject gnus-current-article)
6874   (gnus-summary-position-point))
6875
6876 (defun gnus-summary-verbose-headers (&optional arg)
6877   "Toggle permanent full header display.
6878 If ARG is a positive number, turn header display on.
6879 If ARG is a negative number, turn header display off."
6880   (interactive "P")
6881   (setq gnus-show-all-headers
6882         (cond ((or (not (numberp arg))
6883                    (zerop arg))
6884                (not gnus-show-all-headers))
6885               ((natnump arg)
6886                t)))
6887   (gnus-summary-show-article))
6888
6889 (defun gnus-summary-toggle-header (&optional arg)
6890   "Show the headers if they are hidden, or hide them if they are shown.
6891 If ARG is a positive number, show the entire header.
6892 If ARG is a negative number, hide the unwanted header lines."
6893   (interactive "P")
6894   (save-excursion
6895     (set-buffer gnus-article-buffer)
6896     (let* ((buffer-read-only nil)
6897            (inhibit-point-motion-hooks t)
6898            (hidden (text-property-any
6899                     (goto-char (point-min)) (search-forward "\n\n")
6900                     'invisible t))
6901            e)
6902       (goto-char (point-min))
6903       (when (search-forward "\n\n" nil t)
6904         (delete-region (point-min) (1- (point))))
6905       (goto-char (point-min))
6906       (save-excursion
6907         (set-buffer gnus-original-article-buffer)
6908         (goto-char (point-min))
6909         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
6910       (insert-buffer-substring gnus-original-article-buffer 1 e)
6911       (let ((article-inhibit-hiding t))
6912         (gnus-run-hooks 'gnus-article-display-hook))
6913       (when (or (not hidden) (and (numberp arg) (< arg 0)))
6914         (gnus-article-hide-headers)))))
6915
6916 (defun gnus-summary-show-all-headers ()
6917   "Make all header lines visible."
6918   (interactive)
6919   (gnus-article-show-all-headers))
6920
6921 (defun gnus-summary-toggle-mime (&optional arg)
6922   "Toggle MIME processing.
6923 If ARG is a positive number, turn MIME processing on."
6924   (interactive "P")
6925   (setq gnus-show-mime
6926         (if (null arg) (not gnus-show-mime)
6927           (> (prefix-numeric-value arg) 0)))
6928   (gnus-summary-select-article t 'force))
6929
6930 (defun gnus-summary-caesar-message (&optional arg)
6931   "Caesar rotate the current article by 13.
6932 The numerical prefix specifies how many places to rotate each letter
6933 forward."
6934   (interactive "P")
6935   (gnus-summary-select-article)
6936   (let ((mail-header-separator ""))
6937     (gnus-eval-in-buffer-window gnus-article-buffer
6938       (save-restriction
6939         (widen)
6940         (let ((start (window-start))
6941               buffer-read-only)
6942           (message-caesar-buffer-body arg)
6943           (set-window-start (get-buffer-window (current-buffer)) start))))))
6944
6945 (defun gnus-summary-stop-page-breaking ()
6946   "Stop page breaking in the current article."
6947   (interactive)
6948   (gnus-summary-select-article)
6949   (gnus-eval-in-buffer-window gnus-article-buffer
6950     (widen)
6951     (when (gnus-visual-p 'page-marker)
6952       (let ((buffer-read-only nil))
6953         (gnus-remove-text-with-property 'gnus-prev)
6954         (gnus-remove-text-with-property 'gnus-next))
6955       (setq gnus-page-broken nil))))
6956
6957 (defun gnus-summary-move-article (&optional n to-newsgroup
6958                                             select-method action)
6959   "Move the current article to a different newsgroup.
6960 If N is a positive number, move the N next articles.
6961 If N is a negative number, move the N previous articles.
6962 If N is nil and any articles have been marked with the process mark,
6963 move those articles instead.
6964 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
6965 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
6966 re-spool using this method.
6967
6968 For this function to work, both the current newsgroup and the
6969 newsgroup that you want to move to have to support the `request-move'
6970 and `request-accept' functions."
6971   (interactive "P")
6972   (unless action
6973     (setq action 'move))
6974   ;; Disable marking as read.
6975   (let (gnus-mark-article-hook)
6976     (save-window-excursion
6977       (gnus-summary-select-article)))
6978   ;; Check whether the source group supports the required functions.
6979   (cond ((and (eq action 'move)
6980               (not (gnus-check-backend-function
6981                     'request-move-article gnus-newsgroup-name)))
6982          (error "The current group does not support article moving"))
6983         ((and (eq action 'crosspost)
6984               (not (gnus-check-backend-function
6985                     'request-replace-article gnus-newsgroup-name)))
6986          (error "The current group does not support article editing")))
6987   (let ((articles (gnus-summary-work-articles n))
6988         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
6989         (names '((move "Move" "Moving")
6990                  (copy "Copy" "Copying")
6991                  (crosspost "Crosspost" "Crossposting")))
6992         (copy-buf (save-excursion
6993                     (nnheader-set-temp-buffer " *copy article*")))
6994         art-group to-method new-xref article to-groups)
6995     (unless (assq action names)
6996       (error "Unknown action %s" action))
6997     ;; Read the newsgroup name.
6998     (when (and (not to-newsgroup)
6999                (not select-method))
7000       (setq to-newsgroup
7001             (gnus-read-move-group-name
7002              (cadr (assq action names))
7003              (symbol-value (intern (format "gnus-current-%s-group" action)))
7004              articles prefix))
7005       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7006     (setq to-method (or select-method
7007                         (gnus-group-name-to-method to-newsgroup)))
7008     ;; Check the method we are to move this article to...
7009     (unless (gnus-check-backend-function
7010              'request-accept-article (car to-method))
7011       (error "%s does not support article copying" (car to-method)))
7012     (unless (gnus-check-server to-method)
7013       (error "Can't open server %s" (car to-method)))
7014     (gnus-message 6 "%s to %s: %s..."
7015                   (caddr (assq action names))
7016                   (or (car select-method) to-newsgroup) articles)
7017     (while articles
7018       (setq article (pop articles))
7019       (setq
7020        art-group
7021        (cond
7022         ;; Move the article.
7023         ((eq action 'move)
7024          ;; Remove this article from future suppression.
7025          (gnus-dup-unsuppress-article article)
7026          (gnus-request-move-article
7027           article                       ; Article to move
7028           gnus-newsgroup-name           ; From newsgroup
7029           (nth 1 (gnus-find-method-for-group
7030                   gnus-newsgroup-name)) ; Server
7031           (list 'gnus-request-accept-article
7032                 to-newsgroup (list 'quote select-method)
7033                 (not articles))         ; Accept form
7034           (not articles)))              ; Only save nov last time
7035         ;; Copy the article.
7036         ((eq action 'copy)
7037          (save-excursion
7038            (set-buffer copy-buf)
7039            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7040              (gnus-request-accept-article
7041               to-newsgroup select-method (not articles) t))))
7042         ;; Crosspost the article.
7043         ((eq action 'crosspost)
7044          (let ((xref (message-tokenize-header
7045                       (mail-header-xref (gnus-summary-article-header article))
7046                       " ")))
7047            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7048                                   ":" article))
7049            (unless xref
7050              (setq xref (list (system-name))))
7051            (setq new-xref
7052                  (concat
7053                   (mapconcat 'identity
7054                              (delete "Xref:" (delete new-xref xref))
7055                              " ")
7056                   " " new-xref))
7057            (save-excursion
7058              (set-buffer copy-buf)
7059              ;; First put the article in the destination group.
7060              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7061              (when (consp (setq art-group
7062                                 (gnus-request-accept-article
7063                                  to-newsgroup select-method (not articles))))
7064                (setq new-xref (concat new-xref " " (car art-group)
7065                                       ":" (cdr art-group)))
7066                ;; Now we have the new Xrefs header, so we insert
7067                ;; it and replace the new article.
7068                (nnheader-replace-header "Xref" new-xref)
7069                (gnus-request-replace-article
7070                 (cdr art-group) to-newsgroup (current-buffer))
7071                art-group))))))
7072       (cond
7073        ((not art-group)
7074         (gnus-message 1 "Couldn't %s article %s"
7075                       (cadr (assq action names)) article))
7076        ((and (eq art-group 'junk)
7077              (eq action 'move))
7078         (gnus-summary-mark-article article gnus-canceled-mark)
7079         (gnus-message 4 "Deleted article %s" article))
7080        (t
7081         (let* ((pto-group (gnus-group-prefixed-name
7082                            (car art-group) to-method))
7083                (entry
7084                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7085                (info (nth 2 entry))
7086                (to-group (gnus-info-group info)))
7087           ;; Update the group that has been moved to.
7088           (when (and info
7089                      (memq action '(move copy)))
7090             (unless (member to-group to-groups)
7091               (push to-group to-groups))
7092
7093             (unless (memq article gnus-newsgroup-unreads)
7094               (gnus-info-set-read
7095                info (gnus-add-to-range (gnus-info-read info)
7096                                        (list (cdr art-group)))))
7097
7098             ;; Copy any marks over to the new group.
7099             (let ((marks gnus-article-mark-lists)
7100                   (to-article (cdr art-group)))
7101
7102               ;; See whether the article is to be put in the cache.
7103               (when gnus-use-cache
7104                 (gnus-cache-possibly-enter-article
7105                  to-group to-article
7106                  (let ((header (copy-sequence
7107                                 (gnus-summary-article-header article))))
7108                    (mail-header-set-number header to-article)
7109                    header)
7110                  (memq article gnus-newsgroup-marked)
7111                  (memq article gnus-newsgroup-dormant)
7112                  (memq article gnus-newsgroup-unreads)))
7113
7114               (when (and (equal to-group gnus-newsgroup-name)
7115                          (not (memq article gnus-newsgroup-unreads)))
7116                 ;; Mark this article as read in this group.
7117                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7118                 (setcdr (gnus-active to-group) to-article)
7119                 (setcdr gnus-newsgroup-active to-article))
7120
7121               (while marks
7122                 (when (memq article (symbol-value
7123                                      (intern (format "gnus-newsgroup-%s"
7124                                                      (caar marks)))))
7125                   ;; If the other group is the same as this group,
7126                   ;; then we have to add the mark to the list.
7127                   (when (equal to-group gnus-newsgroup-name)
7128                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7129                          (cons to-article
7130                                (symbol-value
7131                                 (intern (format "gnus-newsgroup-%s"
7132                                                 (caar marks)))))))
7133                   ;; Copy the marks to other group.
7134                   (gnus-add-marked-articles
7135                    to-group (cdar marks) (list to-article) info))
7136                 (setq marks (cdr marks)))
7137
7138               (gnus-dribble-enter
7139                (concat "(gnus-group-set-info '"
7140                        (gnus-prin1-to-string (gnus-get-info to-group))
7141                        ")"))))
7142
7143           ;; Update the Xref header in this article to point to
7144           ;; the new crossposted article we have just created.
7145           (when (eq action 'crosspost)
7146             (save-excursion
7147               (set-buffer copy-buf)
7148               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7149               (nnheader-replace-header "Xref" new-xref)
7150               (gnus-request-replace-article
7151                article gnus-newsgroup-name (current-buffer)))))
7152
7153         ;;;!!!Why is this necessary?
7154         (set-buffer gnus-summary-buffer)
7155
7156         (gnus-summary-goto-subject article)
7157         (when (eq action 'move)
7158           (gnus-summary-mark-article article gnus-canceled-mark))))
7159       (gnus-summary-remove-process-mark article))
7160     ;; Re-activate all groups that have been moved to.
7161     (while to-groups
7162       (save-excursion
7163         (set-buffer gnus-group-buffer)
7164         (when (gnus-group-goto-group (car to-groups) t)
7165           (gnus-group-get-new-news-this-group 1 t))
7166         (pop to-groups)))
7167
7168     (gnus-kill-buffer copy-buf)
7169     (gnus-summary-position-point)
7170     (gnus-set-mode-line 'summary)))
7171
7172 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7173   "Move the current article to a different newsgroup.
7174 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7175 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7176 re-spool using this method."
7177   (interactive "P")
7178   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7179
7180 (defun gnus-summary-crosspost-article (&optional n)
7181   "Crosspost the current article to some other group."
7182   (interactive "P")
7183   (gnus-summary-move-article n nil nil 'crosspost))
7184
7185 (defcustom gnus-summary-respool-default-method nil
7186   "Default method for respooling an article.
7187 If nil, use to the current newsgroup method."
7188   :type '(choice (gnus-select-method :value (nnml ""))
7189                  (const nil))
7190   :group 'gnus-summary-mail)
7191
7192 (defun gnus-summary-respool-article (&optional n method)
7193   "Respool the current article.
7194 The article will be squeezed through the mail spooling process again,
7195 which means that it will be put in some mail newsgroup or other
7196 depending on `nnmail-split-methods'.
7197 If N is a positive number, respool the N next articles.
7198 If N is a negative number, respool the N previous articles.
7199 If N is nil and any articles have been marked with the process mark,
7200 respool those articles instead.
7201
7202 Respooling can be done both from mail groups and \"real\" newsgroups.
7203 In the former case, the articles in question will be moved from the
7204 current group into whatever groups they are destined to.  In the
7205 latter case, they will be copied into the relevant groups."
7206   (interactive
7207    (list current-prefix-arg
7208          (let* ((methods (gnus-methods-using 'respool))
7209                 (methname
7210                  (symbol-name (or gnus-summary-respool-default-method
7211                                   (car (gnus-find-method-for-group
7212                                         gnus-newsgroup-name)))))
7213                 (method
7214                  (gnus-completing-read
7215                   methname "What backend do you want to use when respooling?"
7216                   methods nil t nil 'gnus-mail-method-history))
7217                 ms)
7218            (cond
7219             ((zerop (length (setq ms (gnus-servers-using-backend
7220                                       (intern method)))))
7221              (list (intern method) ""))
7222             ((= 1 (length ms))
7223              (car ms))
7224             (t
7225              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7226                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7227                            ms-alist))))))))
7228   (unless method
7229     (error "No method given for respooling"))
7230   (if (assoc (symbol-name
7231               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7232              (gnus-methods-using 'respool))
7233       (gnus-summary-move-article n nil method)
7234     (gnus-summary-copy-article n nil method)))
7235
7236 (defun gnus-summary-import-article (file)
7237   "Import an arbitrary file into a mail newsgroup."
7238   (interactive "fImport file: ")
7239   (let ((group gnus-newsgroup-name)
7240         (now (current-time))
7241         atts lines)
7242     (unless (gnus-check-backend-function 'request-accept-article group)
7243       (error "%s does not support article importing" group))
7244     (or (file-readable-p file)
7245         (not (file-regular-p file))
7246         (error "Can't read %s" file))
7247     (save-excursion
7248       (set-buffer (gnus-get-buffer-create " *import file*"))
7249       (erase-buffer)
7250       (nnheader-insert-file-contents file)
7251       (goto-char (point-min))
7252       (unless (nnheader-article-p)
7253         ;; This doesn't look like an article, so we fudge some headers.
7254         (setq atts (file-attributes file)
7255               lines (count-lines (point-min) (point-max)))
7256         (insert "From: " (read-string "From: ") "\n"
7257                 "Subject: " (read-string "Subject: ") "\n"
7258                 "Date: " (message-make-date (nth 5 atts))
7259                 "\n"
7260                 "Message-ID: " (message-make-message-id) "\n"
7261                 "Lines: " (int-to-string lines) "\n"
7262                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7263       (gnus-request-accept-article group nil t)
7264       (kill-buffer (current-buffer)))))
7265
7266 (defun gnus-summary-article-posted-p ()
7267   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7268 This will be the case if the article has both been mailed and posted."
7269   (interactive)
7270   (let ((id (mail-header-references (gnus-summary-article-header)))
7271         (gnus-override-method
7272          (or gnus-refer-article-method gnus-select-method)))
7273     (if (gnus-request-head id "")
7274         (gnus-message 2 "The current message was found on %s"
7275                       gnus-override-method)
7276       (gnus-message 2 "The current message couldn't be found on %s"
7277                     gnus-override-method)
7278       nil)))
7279
7280 (defun gnus-summary-expire-articles (&optional now)
7281   "Expire all articles that are marked as expirable in the current group."
7282   (interactive)
7283   (when (gnus-check-backend-function
7284          'request-expire-articles gnus-newsgroup-name)
7285     ;; This backend supports expiry.
7286     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7287            (expirable (if total
7288                           (progn
7289                             ;; We need to update the info for
7290                             ;; this group for `gnus-list-of-read-articles'
7291                             ;; to give us the right answer.
7292                             (gnus-run-hooks 'gnus-exit-group-hook)
7293                             (gnus-summary-update-info)
7294                             (gnus-list-of-read-articles gnus-newsgroup-name))
7295                         (setq gnus-newsgroup-expirable
7296                               (sort gnus-newsgroup-expirable '<))))
7297            (expiry-wait (if now 'immediate
7298                           (gnus-group-find-parameter
7299                            gnus-newsgroup-name 'expiry-wait)))
7300            es)
7301       (when expirable
7302         ;; There are expirable articles in this group, so we run them
7303         ;; through the expiry process.
7304         (gnus-message 6 "Expiring articles...")
7305         ;; The list of articles that weren't expired is returned.
7306         (save-excursion
7307           (if expiry-wait
7308               (let ((nnmail-expiry-wait-function nil)
7309                     (nnmail-expiry-wait expiry-wait))
7310                 (setq es (gnus-request-expire-articles
7311                           expirable gnus-newsgroup-name)))
7312             (setq es (gnus-request-expire-articles
7313                       expirable gnus-newsgroup-name))))
7314         (unless total
7315           (setq gnus-newsgroup-expirable es))
7316         ;; We go through the old list of expirable, and mark all
7317         ;; really expired articles as nonexistent.
7318         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7319           (let ((gnus-use-cache nil))
7320             (while expirable
7321               (unless (memq (car expirable) es)
7322                 (when (gnus-data-find (car expirable))
7323                   (gnus-summary-mark-article
7324                    (car expirable) gnus-canceled-mark)))
7325               (setq expirable (cdr expirable)))))
7326         (gnus-message 6 "Expiring articles...done")))))
7327
7328 (defun gnus-summary-expire-articles-now ()
7329   "Expunge all expirable articles in the current group.
7330 This means that *all* articles that are marked as expirable will be
7331 deleted forever, right now."
7332   (interactive)
7333   (or gnus-expert-user
7334       (gnus-yes-or-no-p
7335        "Are you really, really, really sure you want to delete all these messages? ")
7336       (error "Phew!"))
7337   (gnus-summary-expire-articles t))
7338
7339 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7340 (defun gnus-summary-delete-article (&optional n)
7341   "Delete the N next (mail) articles.
7342 This command actually deletes articles.  This is not a marking
7343 command.  The article will disappear forever from your life, never to
7344 return.
7345 If N is negative, delete backwards.
7346 If N is nil and articles have been marked with the process mark,
7347 delete these instead."
7348   (interactive "P")
7349   (unless (gnus-check-backend-function 'request-expire-articles
7350                                        gnus-newsgroup-name)
7351     (error "The current newsgroup does not support article deletion"))
7352   ;; Compute the list of articles to delete.
7353   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7354         not-deleted)
7355     (if (and gnus-novice-user
7356              (not (gnus-yes-or-no-p
7357                    (format "Do you really want to delete %s forever? "
7358                            (if (> (length articles) 1)
7359                                (format "these %s articles" (length articles))
7360                              "this article")))))
7361         ()
7362       ;; Delete the articles.
7363       (setq not-deleted (gnus-request-expire-articles
7364                          articles gnus-newsgroup-name 'force))
7365       (while articles
7366         (gnus-summary-remove-process-mark (car articles))
7367         ;; The backend might not have been able to delete the article
7368         ;; after all.
7369         (unless (memq (car articles) not-deleted)
7370           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7371         (setq articles (cdr articles)))
7372       (when not-deleted
7373         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7374     (gnus-summary-position-point)
7375     (gnus-set-mode-line 'summary)
7376     not-deleted))
7377
7378 (defun gnus-summary-edit-article (&optional force)
7379   "Edit the current article.
7380 This will have permanent effect only in mail groups.
7381 If FORCE is non-nil, allow editing of articles even in read-only
7382 groups."
7383   (interactive "P")
7384   (save-excursion
7385     (set-buffer gnus-summary-buffer)
7386     (gnus-set-global-variables)
7387     (when (and (not force)
7388                (gnus-group-read-only-p))
7389       (error "The current newsgroup does not support article editing"))
7390     ;; Select article if needed.
7391     (unless (eq (gnus-summary-article-number)
7392                 gnus-current-article)
7393       (gnus-summary-select-article t))
7394     (gnus-article-date-original)
7395     (gnus-article-edit-article
7396      `(lambda (no-highlight)
7397         (gnus-summary-edit-article-done
7398          ,(or (mail-header-references gnus-current-headers) "")
7399          ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))
7400
7401 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7402
7403 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7404                                                  no-highlight)
7405   "Make edits to the current article permanent."
7406   (interactive)
7407   ;; Replace the article.
7408   (let ((buf (current-buffer)))
7409     (with-temp-buffer
7410       (insert-buffer buf)
7411       (if (and (not read-only)
7412                (not (gnus-request-replace-article
7413                      (cdr gnus-article-current) (car gnus-article-current)
7414                      (current-buffer)
7415                      (not gnus-article-decoded-p))))
7416           (error "Couldn't replace article")
7417         ;; Update the summary buffer.
7418         (if (and references
7419                  (equal (message-tokenize-header references " ")
7420                         (message-tokenize-header
7421                          (or (message-fetch-field "references") "") " ")))
7422             ;; We only have to update this line.
7423             (save-excursion
7424               (save-restriction
7425                 (message-narrow-to-head)
7426                 (let ((head (buffer-string))
7427                       header)
7428                   (with-temp-buffer
7429                     (insert (format "211 %d Article retrieved.\n"
7430                                     (cdr gnus-article-current)))
7431                     (insert head)
7432                     (insert ".\n")
7433                     (let ((nntp-server-buffer (current-buffer)))
7434                       (setq header (car (gnus-get-newsgroup-headers
7435                                          (save-excursion
7436                                            (set-buffer gnus-summary-buffer)
7437                                            gnus-newsgroup-dependencies)
7438                                          t))))
7439                     (save-excursion
7440                       (set-buffer gnus-summary-buffer)
7441                       (gnus-data-set-header
7442                        (gnus-data-find (cdr gnus-article-current))
7443                        header)
7444                       (gnus-summary-update-article-line
7445                        (cdr gnus-article-current) header))))))
7446           ;; Update threads.
7447           (set-buffer (or buffer gnus-summary-buffer))
7448           (gnus-summary-update-article (cdr gnus-article-current)))
7449         ;; Prettify the article buffer again.
7450         (unless no-highlight
7451           (save-excursion
7452             (set-buffer gnus-article-buffer)
7453             (gnus-run-hooks 'gnus-article-display-hook)
7454             (set-buffer gnus-original-article-buffer)
7455             (gnus-request-article
7456              (cdr gnus-article-current)
7457              (car gnus-article-current) (current-buffer))))
7458         ;; Prettify the summary buffer line.
7459         (when (gnus-visual-p 'summary-highlight 'highlight)
7460           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7461
7462 (defun gnus-summary-edit-wash (key)
7463   "Perform editing command KEY in the article buffer."
7464   (interactive
7465    (list
7466     (progn
7467       (message "%s" (concat (this-command-keys) "- "))
7468       (read-char))))
7469   (message "")
7470   (gnus-summary-edit-article)
7471   (execute-kbd-macro (concat (this-command-keys) key))
7472   (gnus-article-edit-done))
7473
7474 ;;; Respooling
7475
7476 (defun gnus-summary-respool-query (&optional silent trace)
7477   "Query where the respool algorithm would put this article."
7478   (interactive)
7479   (let (gnus-mark-article-hook)
7480     (gnus-summary-select-article)
7481     (save-excursion
7482       (set-buffer gnus-original-article-buffer)
7483       (save-restriction
7484         (message-narrow-to-head)
7485         (let ((groups (nnmail-article-group 'identity trace)))
7486           (unless silent
7487             (if groups
7488                 (message "This message would go to %s"
7489                          (mapconcat 'car groups ", "))
7490               (message "This message would go to no groups"))
7491             groups))))))
7492
7493 (defun gnus-summary-respool-trace ()
7494   "Trace where the respool algorithm would put this article.
7495 Display a buffer showing all fancy splitting patterns which matched."
7496   (interactive)
7497   (gnus-summary-respool-query nil t))
7498
7499 ;; Summary marking commands.
7500
7501 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7502   "Mark articles which has the same subject as read, and then select the next.
7503 If UNMARK is positive, remove any kind of mark.
7504 If UNMARK is negative, tick articles."
7505   (interactive "P")
7506   (when unmark
7507     (setq unmark (prefix-numeric-value unmark)))
7508   (let ((count
7509          (gnus-summary-mark-same-subject
7510           (gnus-summary-article-subject) unmark)))
7511     ;; Select next unread article.  If auto-select-same mode, should
7512     ;; select the first unread article.
7513     (gnus-summary-next-article t (and gnus-auto-select-same
7514                                       (gnus-summary-article-subject)))
7515     (gnus-message 7 "%d article%s marked as %s"
7516                   count (if (= count 1) " is" "s are")
7517                   (if unmark "unread" "read"))))
7518
7519 (defun gnus-summary-kill-same-subject (&optional unmark)
7520   "Mark articles which has the same subject as read.
7521 If UNMARK is positive, remove any kind of mark.
7522 If UNMARK is negative, tick articles."
7523   (interactive "P")
7524   (when unmark
7525     (setq unmark (prefix-numeric-value unmark)))
7526   (let ((count
7527          (gnus-summary-mark-same-subject
7528           (gnus-summary-article-subject) unmark)))
7529     ;; If marked as read, go to next unread subject.
7530     (when (null unmark)
7531       ;; Go to next unread subject.
7532       (gnus-summary-next-subject 1 t))
7533     (gnus-message 7 "%d articles are marked as %s"
7534                   count (if unmark "unread" "read"))))
7535
7536 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7537   "Mark articles with same SUBJECT as read, and return marked number.
7538 If optional argument UNMARK is positive, remove any kinds of marks.
7539 If optional argument UNMARK is negative, mark articles as unread instead."
7540   (let ((count 1))
7541     (save-excursion
7542       (cond
7543        ((null unmark)                   ; Mark as read.
7544         (while (and
7545                 (progn
7546                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7547                   (gnus-summary-show-thread) t)
7548                 (gnus-summary-find-subject subject))
7549           (setq count (1+ count))))
7550        ((> unmark 0)                    ; Tick.
7551         (while (and
7552                 (progn
7553                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7554                   (gnus-summary-show-thread) t)
7555                 (gnus-summary-find-subject subject))
7556           (setq count (1+ count))))
7557        (t                               ; Mark as unread.
7558         (while (and
7559                 (progn
7560                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7561                   (gnus-summary-show-thread) t)
7562                 (gnus-summary-find-subject subject))
7563           (setq count (1+ count)))))
7564       (gnus-set-mode-line 'summary)
7565       ;; Return the number of marked articles.
7566       count)))
7567
7568 (defun gnus-summary-mark-as-processable (n &optional unmark)
7569   "Set the process mark on the next N articles.
7570 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7571 the process mark instead.  The difference between N and the actual
7572 number of articles marked is returned."
7573   (interactive "p")
7574   (let ((backward (< n 0))
7575         (n (abs n)))
7576     (while (and
7577             (> n 0)
7578             (if unmark
7579                 (gnus-summary-remove-process-mark
7580                  (gnus-summary-article-number))
7581               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7582             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7583       (setq n (1- n)))
7584     (when (/= 0 n)
7585       (gnus-message 7 "No more articles"))
7586     (gnus-summary-recenter)
7587     (gnus-summary-position-point)
7588     n))
7589
7590 (defun gnus-summary-unmark-as-processable (n)
7591   "Remove the process mark from the next N articles.
7592 If N is negative, unmark backward instead.  The difference between N and
7593 the actual number of articles unmarked is returned."
7594   (interactive "p")
7595   (gnus-summary-mark-as-processable n t))
7596
7597 (defun gnus-summary-unmark-all-processable ()
7598   "Remove the process mark from all articles."
7599   (interactive)
7600   (save-excursion
7601     (while gnus-newsgroup-processable
7602       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7603   (gnus-summary-position-point))
7604
7605 (defun gnus-summary-mark-as-expirable (n)
7606   "Mark N articles forward as expirable.
7607 If N is negative, mark backward instead.  The difference between N and
7608 the actual number of articles marked is returned."
7609   (interactive "p")
7610   (gnus-summary-mark-forward n gnus-expirable-mark))
7611
7612 (defun gnus-summary-mark-article-as-replied (article)
7613   "Mark ARTICLE replied and update the summary line."
7614   (push article gnus-newsgroup-replied)
7615   (let ((buffer-read-only nil))
7616     (when (gnus-summary-goto-subject article nil t)
7617       (gnus-summary-update-secondary-mark article))))
7618
7619 (defun gnus-summary-set-bookmark (article)
7620   "Set a bookmark in current article."
7621   (interactive (list (gnus-summary-article-number)))
7622   (when (or (not (get-buffer gnus-article-buffer))
7623             (not gnus-current-article)
7624             (not gnus-article-current)
7625             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7626     (error "No current article selected"))
7627   ;; Remove old bookmark, if one exists.
7628   (let ((old (assq article gnus-newsgroup-bookmarks)))
7629     (when old
7630       (setq gnus-newsgroup-bookmarks
7631             (delq old gnus-newsgroup-bookmarks))))
7632   ;; Set the new bookmark, which is on the form
7633   ;; (article-number . line-number-in-body).
7634   (push
7635    (cons article
7636          (save-excursion
7637            (set-buffer gnus-article-buffer)
7638            (count-lines
7639             (min (point)
7640                  (save-excursion
7641                    (goto-char (point-min))
7642                    (search-forward "\n\n" nil t)
7643                    (point)))
7644             (point))))
7645    gnus-newsgroup-bookmarks)
7646   (gnus-message 6 "A bookmark has been added to the current article."))
7647
7648 (defun gnus-summary-remove-bookmark (article)
7649   "Remove the bookmark from the current article."
7650   (interactive (list (gnus-summary-article-number)))
7651   ;; Remove old bookmark, if one exists.
7652   (let ((old (assq article gnus-newsgroup-bookmarks)))
7653     (if old
7654         (progn
7655           (setq gnus-newsgroup-bookmarks
7656                 (delq old gnus-newsgroup-bookmarks))
7657           (gnus-message 6 "Removed bookmark."))
7658       (gnus-message 6 "No bookmark in current article."))))
7659
7660 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7661 (defun gnus-summary-mark-as-dormant (n)
7662   "Mark N articles forward as dormant.
7663 If N is negative, mark backward instead.  The difference between N and
7664 the actual number of articles marked is returned."
7665   (interactive "p")
7666   (gnus-summary-mark-forward n gnus-dormant-mark))
7667
7668 (defun gnus-summary-set-process-mark (article)
7669   "Set the process mark on ARTICLE and update the summary line."
7670   (setq gnus-newsgroup-processable
7671         (cons article
7672               (delq article gnus-newsgroup-processable)))
7673   (when (gnus-summary-goto-subject article)
7674     (gnus-summary-show-thread)
7675     (gnus-summary-goto-subject article)
7676     (gnus-summary-update-secondary-mark article)))
7677
7678 (defun gnus-summary-remove-process-mark (article)
7679   "Remove the process mark from ARTICLE and update the summary line."
7680   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7681   (when (gnus-summary-goto-subject article)
7682     (gnus-summary-show-thread)
7683     (gnus-summary-goto-subject article)
7684     (gnus-summary-update-secondary-mark article)))
7685
7686 (defun gnus-summary-set-saved-mark (article)
7687   "Set the process mark on ARTICLE and update the summary line."
7688   (push article gnus-newsgroup-saved)
7689   (when (gnus-summary-goto-subject article)
7690     (gnus-summary-update-secondary-mark article)))
7691
7692 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7693   "Mark N articles as read forwards.
7694 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7695 The difference between N and the actual number of articles marked is
7696 returned."
7697   (interactive "p")
7698   (let ((backward (< n 0))
7699         (gnus-summary-goto-unread
7700          (and gnus-summary-goto-unread
7701               (not (eq gnus-summary-goto-unread 'never))
7702               (not (memq mark (list gnus-unread-mark
7703                                     gnus-ticked-mark gnus-dormant-mark)))))
7704         (n (abs n))
7705         (mark (or mark gnus-del-mark)))
7706     (while (and (> n 0)
7707                 (gnus-summary-mark-article nil mark no-expire)
7708                 (zerop (gnus-summary-next-subject
7709                         (if backward -1 1)
7710                         (and gnus-summary-goto-unread
7711                              (not (eq gnus-summary-goto-unread 'never)))
7712                         t)))
7713       (setq n (1- n)))
7714     (when (/= 0 n)
7715       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7716     (gnus-summary-recenter)
7717     (gnus-summary-position-point)
7718     (gnus-set-mode-line 'summary)
7719     n))
7720
7721 (defun gnus-summary-mark-article-as-read (mark)
7722   "Mark the current article quickly as read with MARK."
7723   (let ((article (gnus-summary-article-number)))
7724     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7725     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7726     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7727     (push (cons article mark) gnus-newsgroup-reads)
7728     ;; Possibly remove from cache, if that is used.
7729     (when gnus-use-cache
7730       (gnus-cache-enter-remove-article article))
7731     ;; Allow the backend to change the mark.
7732     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7733     ;; Check for auto-expiry.
7734     (when (and gnus-newsgroup-auto-expire
7735                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
7736                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
7737                    (= mark gnus-ancient-mark)
7738                    (= mark gnus-read-mark) (= mark gnus-souped-mark)
7739                    (= mark gnus-duplicate-mark)))
7740       (setq mark gnus-expirable-mark)
7741       ;; Let the backend know about the mark change.
7742       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7743       (push article gnus-newsgroup-expirable))
7744     ;; Set the mark in the buffer.
7745     (gnus-summary-update-mark mark 'unread)
7746     t))
7747
7748 (defun gnus-summary-mark-article-as-unread (mark)
7749   "Mark the current article quickly as unread with MARK."
7750   (let* ((article (gnus-summary-article-number))
7751          (old-mark (gnus-summary-article-mark article)))
7752     ;; Allow the backend to change the mark.
7753     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7754     (if (eq mark old-mark)
7755         t
7756       (if (<= article 0)
7757           (progn
7758             (gnus-error 1 "Can't mark negative article numbers")
7759             nil)
7760         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7761         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7762         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7763         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7764         (cond ((= mark gnus-ticked-mark)
7765                (push article gnus-newsgroup-marked))
7766               ((= mark gnus-dormant-mark)
7767                (push article gnus-newsgroup-dormant))
7768               (t
7769                (push article gnus-newsgroup-unreads)))
7770         (gnus-pull article gnus-newsgroup-reads)
7771
7772         ;; See whether the article is to be put in the cache.
7773         (and gnus-use-cache
7774              (vectorp (gnus-summary-article-header article))
7775              (save-excursion
7776                (gnus-cache-possibly-enter-article
7777                 gnus-newsgroup-name article
7778                 (gnus-summary-article-header article)
7779                 (= mark gnus-ticked-mark)
7780                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7781
7782         ;; Fix the mark.
7783         (gnus-summary-update-mark mark 'unread)
7784         t))))
7785
7786 (defun gnus-summary-mark-article (&optional article mark no-expire)
7787   "Mark ARTICLE with MARK.  MARK can be any character.
7788 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7789 `??' (dormant) and `?E' (expirable).
7790 If MARK is nil, then the default character `?D' is used.
7791 If ARTICLE is nil, then the article on the current line will be
7792 marked."
7793   ;; The mark might be a string.
7794   (when (stringp mark)
7795     (setq mark (aref mark 0)))
7796   ;; If no mark is given, then we check auto-expiring.
7797   (and (not no-expire)
7798        gnus-newsgroup-auto-expire
7799        (or (not mark)
7800            (and (gnus-characterp mark)
7801                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
7802                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
7803                     (= mark gnus-read-mark) (= mark gnus-souped-mark)
7804                     (= mark gnus-duplicate-mark))))
7805        (setq mark gnus-expirable-mark))
7806   (let* ((mark (or mark gnus-del-mark))
7807          (article (or article (gnus-summary-article-number)))
7808          (old-mark (gnus-summary-article-mark article)))
7809     ;; Allow the backend to change the mark.
7810     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7811     (if (eq mark old-mark)
7812         t
7813       (unless article
7814         (error "No article on current line"))
7815       (if (not (if (or (= mark gnus-unread-mark)
7816                        (= mark gnus-ticked-mark)
7817                        (= mark gnus-dormant-mark))
7818                    (gnus-mark-article-as-unread article mark)
7819                  (gnus-mark-article-as-read article mark)))
7820           t
7821         ;; See whether the article is to be put in the cache.
7822         (and gnus-use-cache
7823              (not (= mark gnus-canceled-mark))
7824              (vectorp (gnus-summary-article-header article))
7825              (save-excursion
7826                (gnus-cache-possibly-enter-article
7827                 gnus-newsgroup-name article
7828                 (gnus-summary-article-header article)
7829                 (= mark gnus-ticked-mark)
7830                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7831
7832         (when (gnus-summary-goto-subject article nil t)
7833           (let ((buffer-read-only nil))
7834             (gnus-summary-show-thread)
7835             ;; Fix the mark.
7836             (gnus-summary-update-mark mark 'unread)
7837             t))))))
7838
7839 (defun gnus-summary-update-secondary-mark (article)
7840   "Update the secondary (read, process, cache) mark."
7841   (gnus-summary-update-mark
7842    (cond ((memq article gnus-newsgroup-processable)
7843           gnus-process-mark)
7844          ((memq article gnus-newsgroup-cached)
7845           gnus-cached-mark)
7846          ((memq article gnus-newsgroup-replied)
7847           gnus-replied-mark)
7848          ((memq article gnus-newsgroup-saved)
7849           gnus-saved-mark)
7850          (t gnus-unread-mark))
7851    'replied)
7852   (when (gnus-visual-p 'summary-highlight 'highlight)
7853     (gnus-run-hooks 'gnus-summary-update-hook))
7854   t)
7855
7856 (defun gnus-summary-update-mark (mark type)
7857   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7858         (buffer-read-only nil))
7859     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7860     (when (looking-at "\r")
7861       (incf forward))
7862     (when (and forward
7863                (<= (+ forward (point)) (point-max)))
7864       ;; Go to the right position on the line.
7865       (goto-char (+ forward (point)))
7866       ;; Replace the old mark with the new mark.
7867       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
7868       ;; Optionally update the marks by some user rule.
7869       (when (eq type 'unread)
7870         (gnus-data-set-mark
7871          (gnus-data-find (gnus-summary-article-number)) mark)
7872         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
7873
7874 (defun gnus-mark-article-as-read (article &optional mark)
7875   "Enter ARTICLE in the pertinent lists and remove it from others."
7876   ;; Make the article expirable.
7877   (let ((mark (or mark gnus-del-mark)))
7878     (if (= mark gnus-expirable-mark)
7879         (push article gnus-newsgroup-expirable)
7880       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7881     ;; Remove from unread and marked lists.
7882     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7883     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7884     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7885     (push (cons article mark) gnus-newsgroup-reads)
7886     ;; Possibly remove from cache, if that is used.
7887     (when gnus-use-cache
7888       (gnus-cache-enter-remove-article article))
7889     t))
7890
7891 (defun gnus-mark-article-as-unread (article &optional mark)
7892   "Enter ARTICLE in the pertinent lists and remove it from others."
7893   (let ((mark (or mark gnus-ticked-mark)))
7894     (if (<= article 0)
7895         (progn
7896           (gnus-error 1 "Can't mark negative article numbers")
7897           nil)
7898       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
7899             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
7900             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
7901             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7902
7903       ;; Unsuppress duplicates?
7904       (when gnus-suppress-duplicates
7905         (gnus-dup-unsuppress-article article))
7906
7907       (cond ((= mark gnus-ticked-mark)
7908              (push article gnus-newsgroup-marked))
7909             ((= mark gnus-dormant-mark)
7910              (push article gnus-newsgroup-dormant))
7911             (t
7912              (push article gnus-newsgroup-unreads)))
7913       (gnus-pull article gnus-newsgroup-reads)
7914       t)))
7915
7916 (defalias 'gnus-summary-mark-as-unread-forward
7917   'gnus-summary-tick-article-forward)
7918 (make-obsolete 'gnus-summary-mark-as-unread-forward
7919                'gnus-summary-tick-article-forward)
7920 (defun gnus-summary-tick-article-forward (n)
7921   "Tick N articles forwards.
7922 If N is negative, tick backwards instead.
7923 The difference between N and the number of articles ticked is returned."
7924   (interactive "p")
7925   (gnus-summary-mark-forward n gnus-ticked-mark))
7926
7927 (defalias 'gnus-summary-mark-as-unread-backward
7928   'gnus-summary-tick-article-backward)
7929 (make-obsolete 'gnus-summary-mark-as-unread-backward
7930                'gnus-summary-tick-article-backward)
7931 (defun gnus-summary-tick-article-backward (n)
7932   "Tick N articles backwards.
7933 The difference between N and the number of articles ticked is returned."
7934   (interactive "p")
7935   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
7936
7937 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7938 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7939 (defun gnus-summary-tick-article (&optional article clear-mark)
7940   "Mark current article as unread.
7941 Optional 1st argument ARTICLE specifies article number to be marked as unread.
7942 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
7943   (interactive)
7944   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
7945                                        gnus-ticked-mark)))
7946
7947 (defun gnus-summary-mark-as-read-forward (n)
7948   "Mark N articles as read forwards.
7949 If N is negative, mark backwards instead.
7950 The difference between N and the actual number of articles marked is
7951 returned."
7952   (interactive "p")
7953   (gnus-summary-mark-forward n gnus-del-mark t))
7954
7955 (defun gnus-summary-mark-as-read-backward (n)
7956   "Mark the N articles as read backwards.
7957 The difference between N and the actual number of articles marked is
7958 returned."
7959   (interactive "p")
7960   (gnus-summary-mark-forward (- n) gnus-del-mark t))
7961
7962 (defun gnus-summary-mark-as-read (&optional article mark)
7963   "Mark current article as read.
7964 ARTICLE specifies the article to be marked as read.
7965 MARK specifies a string to be inserted at the beginning of the line."
7966   (gnus-summary-mark-article article mark))
7967
7968 (defun gnus-summary-clear-mark-forward (n)
7969   "Clear marks from N articles forward.
7970 If N is negative, clear backward instead.
7971 The difference between N and the number of marks cleared is returned."
7972   (interactive "p")
7973   (gnus-summary-mark-forward n gnus-unread-mark))
7974
7975 (defun gnus-summary-clear-mark-backward (n)
7976   "Clear marks from N articles backward.
7977 The difference between N and the number of marks cleared is returned."
7978   (interactive "p")
7979   (gnus-summary-mark-forward (- n) gnus-unread-mark))
7980
7981 (defun gnus-summary-mark-unread-as-read ()
7982   "Intended to be used by `gnus-summary-mark-article-hook'."
7983   (when (memq gnus-current-article gnus-newsgroup-unreads)
7984     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
7985
7986 (defun gnus-summary-mark-read-and-unread-as-read ()
7987   "Intended to be used by `gnus-summary-mark-article-hook'."
7988   (let ((mark (gnus-summary-article-mark)))
7989     (when (or (gnus-unread-mark-p mark)
7990               (gnus-read-mark-p mark))
7991       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
7992
7993 (defun gnus-summary-mark-region-as-read (point mark all)
7994   "Mark all unread articles between point and mark as read.
7995 If given a prefix, mark all articles between point and mark as read,
7996 even ticked and dormant ones."
7997   (interactive "r\nP")
7998   (save-excursion
7999     (let (article)
8000       (goto-char point)
8001       (beginning-of-line)
8002       (while (and
8003               (< (point) mark)
8004               (progn
8005                 (when (or all
8006                           (memq (setq article (gnus-summary-article-number))
8007                                 gnus-newsgroup-unreads))
8008                   (gnus-summary-mark-article article gnus-del-mark))
8009                 t)
8010               (gnus-summary-find-next))))))
8011
8012 (defun gnus-summary-mark-below (score mark)
8013   "Mark articles with score less than SCORE with MARK."
8014   (interactive "P\ncMark: ")
8015   (setq score (if score
8016                   (prefix-numeric-value score)
8017                 (or gnus-summary-default-score 0)))
8018   (save-excursion
8019     (set-buffer gnus-summary-buffer)
8020     (goto-char (point-min))
8021     (while
8022         (progn
8023           (and (< (gnus-summary-article-score) score)
8024                (gnus-summary-mark-article nil mark))
8025           (gnus-summary-find-next)))))
8026
8027 (defun gnus-summary-kill-below (&optional score)
8028   "Mark articles with score below SCORE as read."
8029   (interactive "P")
8030   (gnus-summary-mark-below score gnus-killed-mark))
8031
8032 (defun gnus-summary-clear-above (&optional score)
8033   "Clear all marks from articles with score above SCORE."
8034   (interactive "P")
8035   (gnus-summary-mark-above score gnus-unread-mark))
8036
8037 (defun gnus-summary-tick-above (&optional score)
8038   "Tick all articles with score above SCORE."
8039   (interactive "P")
8040   (gnus-summary-mark-above score gnus-ticked-mark))
8041
8042 (defun gnus-summary-mark-above (score mark)
8043   "Mark articles with score over SCORE with MARK."
8044   (interactive "P\ncMark: ")
8045   (setq score (if score
8046                   (prefix-numeric-value score)
8047                 (or gnus-summary-default-score 0)))
8048   (save-excursion
8049     (set-buffer gnus-summary-buffer)
8050     (goto-char (point-min))
8051     (while (and (progn
8052                   (when (> (gnus-summary-article-score) score)
8053                     (gnus-summary-mark-article nil mark))
8054                   t)
8055                 (gnus-summary-find-next)))))
8056
8057 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8058 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8059 (defun gnus-summary-limit-include-expunged (&optional no-error)
8060   "Display all the hidden articles that were expunged for low scores."
8061   (interactive)
8062   (let ((buffer-read-only nil))
8063     (let ((scored gnus-newsgroup-scored)
8064           headers h)
8065       (while scored
8066         (unless (gnus-summary-goto-subject (caar scored))
8067           (and (setq h (gnus-summary-article-header (caar scored)))
8068                (< (cdar scored) gnus-summary-expunge-below)
8069                (push h headers)))
8070         (setq scored (cdr scored)))
8071       (if (not headers)
8072           (when (not no-error)
8073             (error "No expunged articles hidden"))
8074         (goto-char (point-min))
8075         (gnus-summary-prepare-unthreaded (nreverse headers))
8076         (goto-char (point-min))
8077         (gnus-summary-position-point)
8078         t))))
8079
8080 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8081   "Mark all unread articles in this newsgroup as read.
8082 If prefix argument ALL is non-nil, ticked and dormant articles will
8083 also be marked as read.
8084 If QUIETLY is non-nil, no questions will be asked.
8085 If TO-HERE is non-nil, it should be a point in the buffer.  All
8086 articles before this point will be marked as read.
8087 Note that this function will only catch up the unread article
8088 in the current summary buffer limitation.
8089 The number of articles marked as read is returned."
8090   (interactive "P")
8091   (prog1
8092       (save-excursion
8093         (when (or quietly
8094                   (not gnus-interactive-catchup) ;Without confirmation?
8095                   gnus-expert-user
8096                   (gnus-y-or-n-p
8097                    (if all
8098                        "Mark absolutely all articles as read? "
8099                      "Mark all unread articles as read? ")))
8100           (if (and not-mark
8101                    (not gnus-newsgroup-adaptive)
8102                    (not gnus-newsgroup-auto-expire)
8103                    (not gnus-suppress-duplicates)
8104                    (or (not gnus-use-cache)
8105                        (eq gnus-use-cache 'passive)))
8106               (progn
8107                 (when all
8108                   (setq gnus-newsgroup-marked nil
8109                         gnus-newsgroup-dormant nil))
8110                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8111             ;; We actually mark all articles as canceled, which we
8112             ;; have to do when using auto-expiry or adaptive scoring.
8113             (gnus-summary-show-all-threads)
8114             (when (gnus-summary-first-subject (not all) t)
8115               (while (and
8116                       (if to-here (< (point) to-here) t)
8117                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8118                       (gnus-summary-find-next (not all) nil nil t))))
8119             (gnus-set-mode-line 'summary))
8120           t))
8121     (gnus-summary-position-point)))
8122
8123 (defun gnus-summary-catchup-to-here (&optional all)
8124   "Mark all unticked articles before the current one as read.
8125 If ALL is non-nil, also mark ticked and dormant articles as read."
8126   (interactive "P")
8127   (save-excursion
8128     (gnus-save-hidden-threads
8129       (let ((beg (point)))
8130         ;; We check that there are unread articles.
8131         (when (or all (gnus-summary-find-prev))
8132           (gnus-summary-catchup all t beg)))))
8133   (gnus-summary-position-point))
8134
8135 (defun gnus-summary-catchup-all (&optional quietly)
8136   "Mark all articles in this newsgroup as read."
8137   (interactive "P")
8138   (gnus-summary-catchup t quietly))
8139
8140 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8141   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8142 If prefix argument ALL is non-nil, all articles are marked as read."
8143   (interactive "P")
8144   (when (gnus-summary-catchup all quietly nil 'fast)
8145     ;; Select next newsgroup or exit.
8146     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8147              (eq gnus-auto-select-next 'quietly))
8148         (gnus-summary-next-group nil)
8149       (gnus-summary-exit))))
8150
8151 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8152   "Mark all articles in this newsgroup as read, and then exit."
8153   (interactive "P")
8154   (gnus-summary-catchup-and-exit t quietly))
8155
8156 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8157 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8158   "Mark all articles in this group as read and select the next group.
8159 If given a prefix, mark all articles, unread as well as ticked, as
8160 read."
8161   (interactive "P")
8162   (save-excursion
8163     (gnus-summary-catchup all))
8164   (gnus-summary-next-article t nil nil t))
8165
8166 ;; Thread-based commands.
8167
8168 (defun gnus-summary-articles-in-thread (&optional article)
8169   "Return a list of all articles in the current thread.
8170 If ARTICLE is non-nil, return all articles in the thread that starts
8171 with that article."
8172   (let* ((article (or article (gnus-summary-article-number)))
8173          (data (gnus-data-find-list article))
8174          (top-level (gnus-data-level (car data)))
8175          (top-subject
8176           (cond ((null gnus-thread-operation-ignore-subject)
8177                  (gnus-simplify-subject-re
8178                   (mail-header-subject (gnus-data-header (car data)))))
8179                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8180                  (gnus-simplify-subject-fuzzy
8181                   (mail-header-subject (gnus-data-header (car data)))))
8182                 (t nil)))
8183          (end-point (save-excursion
8184                       (if (gnus-summary-go-to-next-thread)
8185                           (point) (point-max))))
8186          articles)
8187     (while (and data
8188                 (< (gnus-data-pos (car data)) end-point))
8189       (when (or (not top-subject)
8190                 (string= top-subject
8191                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8192                              (gnus-simplify-subject-fuzzy
8193                               (mail-header-subject
8194                                (gnus-data-header (car data))))
8195                            (gnus-simplify-subject-re
8196                             (mail-header-subject
8197                              (gnus-data-header (car data)))))))
8198         (push (gnus-data-number (car data)) articles))
8199       (unless (and (setq data (cdr data))
8200                    (> (gnus-data-level (car data)) top-level))
8201         (setq data nil)))
8202     ;; Return the list of articles.
8203     (nreverse articles)))
8204
8205 (defun gnus-summary-rethread-current ()
8206   "Rethread the thread the current article is part of."
8207   (interactive)
8208   (let* ((gnus-show-threads t)
8209          (article (gnus-summary-article-number))
8210          (id (mail-header-id (gnus-summary-article-header)))
8211          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8212     (unless id
8213       (error "No article on the current line"))
8214     (gnus-rebuild-thread id)
8215     (gnus-summary-goto-subject article)))
8216
8217 (defun gnus-summary-reparent-thread ()
8218   "Make the current article child of the marked (or previous) article.
8219
8220 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8221 is non-nil or the Subject: of both articles are the same."
8222   (interactive)
8223   (unless (not (gnus-group-read-only-p))
8224     (error "The current newsgroup does not support article editing"))
8225   (unless (<= (length gnus-newsgroup-processable) 1)
8226     (error "No more than one article may be marked"))
8227   (save-window-excursion
8228     (let ((gnus-article-buffer " *reparent*")
8229           (current-article (gnus-summary-article-number))
8230           ;; First grab the marked article, otherwise one line up.
8231           (parent-article (if (not (null gnus-newsgroup-processable))
8232                               (car gnus-newsgroup-processable)
8233                             (save-excursion
8234                               (if (eq (forward-line -1) 0)
8235                                   (gnus-summary-article-number)
8236                                 (error "Beginning of summary buffer"))))))
8237       (unless (not (eq current-article parent-article))
8238         (error "An article may not be self-referential"))
8239       (let ((message-id (mail-header-id
8240                          (gnus-summary-article-header parent-article))))
8241         (unless (and message-id (not (equal message-id "")))
8242           (error "No message-id in desired parent"))
8243         ;; We don't want the article to be marked as read.
8244         (let (gnus-mark-article-hook)
8245           (gnus-summary-select-article t t nil current-article))
8246         (set-buffer gnus-original-article-buffer)
8247         (let ((buf (format "%s" (buffer-string))))
8248           (with-temp-buffer
8249             (insert buf)
8250             (goto-char (point-min))
8251             (if (re-search-forward "^References: " nil t)
8252                 (progn
8253                   (re-search-forward "^[^ \t]" nil t)
8254                   (forward-line -1)
8255                   (end-of-line)
8256                   (insert " " message-id))
8257               (insert "References: " message-id "\n"))
8258             (unless (gnus-request-replace-article
8259                      current-article (car gnus-article-current)
8260                      (current-buffer))
8261               (error "Couldn't replace article"))))
8262         (set-buffer gnus-summary-buffer)
8263         (gnus-summary-unmark-all-processable)
8264         (gnus-summary-update-article current-article)
8265         (gnus-summary-rethread-current)
8266         (gnus-message 3 "Article %d is now the child of article %d"
8267                       current-article parent-article)))))
8268
8269 (defun gnus-summary-toggle-threads (&optional arg)
8270   "Toggle showing conversation threads.
8271 If ARG is positive number, turn showing conversation threads on."
8272   (interactive "P")
8273   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8274     (setq gnus-show-threads
8275           (if (null arg) (not gnus-show-threads)
8276             (> (prefix-numeric-value arg) 0)))
8277     (gnus-summary-prepare)
8278     (gnus-summary-goto-subject current)
8279     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8280     (gnus-summary-position-point)))
8281
8282 (defun gnus-summary-show-all-threads ()
8283   "Show all threads."
8284   (interactive)
8285   (save-excursion
8286     (let ((buffer-read-only nil))
8287       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8288   (gnus-summary-position-point))
8289
8290 (defun gnus-summary-show-thread ()
8291   "Show thread subtrees.
8292 Returns nil if no thread was there to be shown."
8293   (interactive)
8294   (let ((buffer-read-only nil)
8295         (orig (point))
8296         ;; first goto end then to beg, to have point at beg after let
8297         (end (progn (end-of-line) (point)))
8298         (beg (progn (beginning-of-line) (point))))
8299     (prog1
8300         ;; Any hidden lines here?
8301         (search-forward "\r" end t)
8302       (subst-char-in-region beg end ?\^M ?\n t)
8303       (goto-char orig)
8304       (gnus-summary-position-point))))
8305
8306 (defun gnus-summary-hide-all-threads ()
8307   "Hide all thread subtrees."
8308   (interactive)
8309   (save-excursion
8310     (goto-char (point-min))
8311     (gnus-summary-hide-thread)
8312     (while (zerop (gnus-summary-next-thread 1 t))
8313       (gnus-summary-hide-thread)))
8314   (gnus-summary-position-point))
8315
8316 (defun gnus-summary-hide-thread ()
8317   "Hide thread subtrees.
8318 Returns nil if no threads were there to be hidden."
8319   (interactive)
8320   (let ((buffer-read-only nil)
8321         (start (point))
8322         (article (gnus-summary-article-number)))
8323     (goto-char start)
8324     ;; Go forward until either the buffer ends or the subthread
8325     ;; ends.
8326     (when (and (not (eobp))
8327                (or (zerop (gnus-summary-next-thread 1 t))
8328                    (goto-char (point-max))))
8329       (prog1
8330           (if (and (> (point) start)
8331                    (search-backward "\n" start t))
8332               (progn
8333                 (subst-char-in-region start (point) ?\n ?\^M)
8334                 (gnus-summary-goto-subject article))
8335             (goto-char start)
8336             nil)
8337         ;;(gnus-summary-position-point)
8338         ))))
8339
8340 (defun gnus-summary-go-to-next-thread (&optional previous)
8341   "Go to the same level (or less) next thread.
8342 If PREVIOUS is non-nil, go to previous thread instead.
8343 Return the article number moved to, or nil if moving was impossible."
8344   (let ((level (gnus-summary-thread-level))
8345         (way (if previous -1 1))
8346         (beg (point)))
8347     (forward-line way)
8348     (while (and (not (eobp))
8349                 (< level (gnus-summary-thread-level)))
8350       (forward-line way))
8351     (if (eobp)
8352         (progn
8353           (goto-char beg)
8354           nil)
8355       (setq beg (point))
8356       (prog1
8357           (gnus-summary-article-number)
8358         (goto-char beg)))))
8359
8360 (defun gnus-summary-next-thread (n &optional silent)
8361   "Go to the same level next N'th thread.
8362 If N is negative, search backward instead.
8363 Returns the difference between N and the number of skips actually
8364 done.
8365
8366 If SILENT, don't output messages."
8367   (interactive "p")
8368   (let ((backward (< n 0))
8369         (n (abs n)))
8370     (while (and (> n 0)
8371                 (gnus-summary-go-to-next-thread backward))
8372       (decf n))
8373     (unless silent
8374       (gnus-summary-position-point))
8375     (when (and (not silent) (/= 0 n))
8376       (gnus-message 7 "No more threads"))
8377     n))
8378
8379 (defun gnus-summary-prev-thread (n)
8380   "Go to the same level previous N'th thread.
8381 Returns the difference between N and the number of skips actually
8382 done."
8383   (interactive "p")
8384   (gnus-summary-next-thread (- n)))
8385
8386 (defun gnus-summary-go-down-thread ()
8387   "Go down one level in the current thread."
8388   (let ((children (gnus-summary-article-children)))
8389     (when children
8390       (gnus-summary-goto-subject (car children)))))
8391
8392 (defun gnus-summary-go-up-thread ()
8393   "Go up one level in the current thread."
8394   (let ((parent (gnus-summary-article-parent)))
8395     (when parent
8396       (gnus-summary-goto-subject parent))))
8397
8398 (defun gnus-summary-down-thread (n)
8399   "Go down thread N steps.
8400 If N is negative, go up instead.
8401 Returns the difference between N and how many steps down that were
8402 taken."
8403   (interactive "p")
8404   (let ((up (< n 0))
8405         (n (abs n)))
8406     (while (and (> n 0)
8407                 (if up (gnus-summary-go-up-thread)
8408                   (gnus-summary-go-down-thread)))
8409       (setq n (1- n)))
8410     (gnus-summary-position-point)
8411     (when (/= 0 n)
8412       (gnus-message 7 "Can't go further"))
8413     n))
8414
8415 (defun gnus-summary-up-thread (n)
8416   "Go up thread N steps.
8417 If N is negative, go up instead.
8418 Returns the difference between N and how many steps down that were
8419 taken."
8420   (interactive "p")
8421   (gnus-summary-down-thread (- n)))
8422
8423 (defun gnus-summary-top-thread ()
8424   "Go to the top of the thread."
8425   (interactive)
8426   (while (gnus-summary-go-up-thread))
8427   (gnus-summary-article-number))
8428
8429 (defun gnus-summary-kill-thread (&optional unmark)
8430   "Mark articles under current thread as read.
8431 If the prefix argument is positive, remove any kinds of marks.
8432 If the prefix argument is negative, tick articles instead."
8433   (interactive "P")
8434   (when unmark
8435     (setq unmark (prefix-numeric-value unmark)))
8436   (let ((articles (gnus-summary-articles-in-thread)))
8437     (save-excursion
8438       ;; Expand the thread.
8439       (gnus-summary-show-thread)
8440       ;; Mark all the articles.
8441       (while articles
8442         (gnus-summary-goto-subject (car articles))
8443         (cond ((null unmark)
8444                (gnus-summary-mark-article-as-read gnus-killed-mark))
8445               ((> unmark 0)
8446                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8447               (t
8448                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8449         (setq articles (cdr articles))))
8450     ;; Hide killed subtrees.
8451     (and (null unmark)
8452          gnus-thread-hide-killed
8453          (gnus-summary-hide-thread))
8454     ;; If marked as read, go to next unread subject.
8455     (when (null unmark)
8456       ;; Go to next unread subject.
8457       (gnus-summary-next-subject 1 t)))
8458   (gnus-set-mode-line 'summary))
8459
8460 ;; Summary sorting commands
8461
8462 (defun gnus-summary-sort-by-number (&optional reverse)
8463   "Sort the summary buffer by article number.
8464 Argument REVERSE means reverse order."
8465   (interactive "P")
8466   (gnus-summary-sort 'number reverse))
8467
8468 (defun gnus-summary-sort-by-author (&optional reverse)
8469   "Sort the summary buffer by author name alphabetically.
8470 If case-fold-search is non-nil, case of letters is ignored.
8471 Argument REVERSE means reverse order."
8472   (interactive "P")
8473   (gnus-summary-sort 'author reverse))
8474
8475 (defun gnus-summary-sort-by-subject (&optional reverse)
8476   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8477 If case-fold-search is non-nil, case of letters is ignored.
8478 Argument REVERSE means reverse order."
8479   (interactive "P")
8480   (gnus-summary-sort 'subject reverse))
8481
8482 (defun gnus-summary-sort-by-date (&optional reverse)
8483   "Sort the summary buffer by date.
8484 Argument REVERSE means reverse order."
8485   (interactive "P")
8486   (gnus-summary-sort 'date reverse))
8487
8488 (defun gnus-summary-sort-by-score (&optional reverse)
8489   "Sort the summary buffer by score.
8490 Argument REVERSE means reverse order."
8491   (interactive "P")
8492   (gnus-summary-sort 'score reverse))
8493
8494 (defun gnus-summary-sort-by-lines (&optional reverse)
8495   "Sort the summary buffer by article length.
8496 Argument REVERSE means reverse order."
8497   (interactive "P")
8498   (gnus-summary-sort 'lines reverse))
8499
8500 (defun gnus-summary-sort (predicate reverse)
8501   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8502   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8503          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8504          (gnus-thread-sort-functions
8505           (list
8506            (if (not reverse)
8507                thread
8508              `(lambda (t1 t2)
8509                 (,thread t2 t1)))))
8510          (gnus-article-sort-functions
8511           (list
8512            (if (not reverse)
8513                article
8514              `(lambda (t1 t2)
8515                 (,article t2 t1)))))
8516          (buffer-read-only)
8517          (gnus-summary-prepare-hook nil))
8518     ;; We do the sorting by regenerating the threads.
8519     (gnus-summary-prepare)
8520     ;; Hide subthreads if needed.
8521     (when (and gnus-show-threads gnus-thread-hide-subtree)
8522       (gnus-summary-hide-all-threads))))
8523
8524 ;; Summary saving commands.
8525
8526 (defun gnus-summary-save-article (&optional n not-saved)
8527   "Save the current article using the default saver function.
8528 If N is a positive number, save the N next articles.
8529 If N is a negative number, save the N previous articles.
8530 If N is nil and any articles have been marked with the process mark,
8531 save those articles instead.
8532 The variable `gnus-default-article-saver' specifies the saver function."
8533   (interactive "P")
8534   (let* ((articles (gnus-summary-work-articles n))
8535          (save-buffer (save-excursion
8536                         (nnheader-set-temp-buffer " *Gnus Save*")))
8537          (num (length articles))
8538          header article file)
8539     (while articles
8540       (setq header (gnus-summary-article-header
8541                     (setq article (pop articles))))
8542       (if (not (vectorp header))
8543           ;; This is a pseudo-article.
8544           (if (assq 'name header)
8545               (gnus-copy-file (cdr (assq 'name header)))
8546             (gnus-message 1 "Article %d is unsaveable" article))
8547         ;; This is a real article.
8548         (save-window-excursion
8549           (gnus-summary-select-article t nil nil article))
8550         (save-excursion
8551           (set-buffer save-buffer)
8552           (erase-buffer)
8553           (insert-buffer-substring gnus-original-article-buffer))
8554         (setq file (gnus-article-save save-buffer file num))
8555         (gnus-summary-remove-process-mark article)
8556         (unless not-saved
8557           (gnus-summary-set-saved-mark article))))
8558     (gnus-kill-buffer save-buffer)
8559     (gnus-summary-position-point)
8560     (gnus-set-mode-line 'summary)
8561     n))
8562
8563 (defun gnus-summary-pipe-output (&optional arg)
8564   "Pipe the current article to a subprocess.
8565 If N is a positive number, pipe the N next articles.
8566 If N is a negative number, pipe the N previous articles.
8567 If N is nil and any articles have been marked with the process mark,
8568 pipe those articles instead."
8569   (interactive "P")
8570   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8571     (gnus-summary-save-article arg t))
8572   (gnus-configure-windows 'pipe))
8573
8574 (defun gnus-summary-save-article-mail (&optional arg)
8575   "Append the current article to an mail file.
8576 If N is a positive number, save the N next articles.
8577 If N is a negative number, save the N previous articles.
8578 If N is nil and any articles have been marked with the process mark,
8579 save those articles instead."
8580   (interactive "P")
8581   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8582     (gnus-summary-save-article arg)))
8583
8584 (defun gnus-summary-save-article-rmail (&optional arg)
8585   "Append the current article to an rmail file.
8586 If N is a positive number, save the N next articles.
8587 If N is a negative number, save the N previous articles.
8588 If N is nil and any articles have been marked with the process mark,
8589 save those articles instead."
8590   (interactive "P")
8591   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8592     (gnus-summary-save-article arg)))
8593
8594 (defun gnus-summary-save-article-file (&optional arg)
8595   "Append the current article to a file.
8596 If N is a positive number, save the N next articles.
8597 If N is a negative number, save the N previous articles.
8598 If N is nil and any articles have been marked with the process mark,
8599 save those articles instead."
8600   (interactive "P")
8601   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8602     (gnus-summary-save-article arg)))
8603
8604 (defun gnus-summary-write-article-file (&optional arg)
8605   "Write the current article to a file, deleting the previous file.
8606 If N is a positive number, save the N next articles.
8607 If N is a negative number, save the N previous articles.
8608 If N is nil and any articles have been marked with the process mark,
8609 save those articles instead."
8610   (interactive "P")
8611   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8612     (gnus-summary-save-article arg)))
8613
8614 (defun gnus-summary-save-article-body-file (&optional arg)
8615   "Append the current article body to a file.
8616 If N is a positive number, save the N next articles.
8617 If N is a negative number, save the N previous articles.
8618 If N is nil and any articles have been marked with the process mark,
8619 save those articles instead."
8620   (interactive "P")
8621   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8622     (gnus-summary-save-article arg)))
8623
8624 (defun gnus-summary-pipe-message (program)
8625   "Pipe the current article through PROGRAM."
8626   (interactive "sProgram: ")
8627   (gnus-summary-select-article)
8628   (let ((mail-header-separator ""))
8629     (gnus-eval-in-buffer-window gnus-article-buffer
8630       (save-restriction
8631         (widen)
8632         (let ((start (window-start))
8633               buffer-read-only)
8634           (message-pipe-buffer-body program)
8635           (set-window-start (get-buffer-window (current-buffer)) start))))))
8636
8637 (defun gnus-get-split-value (methods)
8638   "Return a value based on the split METHODS."
8639   (let (split-name method result match)
8640     (when methods
8641       (save-excursion
8642         (set-buffer gnus-original-article-buffer)
8643         (save-restriction
8644           (nnheader-narrow-to-headers)
8645           (while methods
8646             (goto-char (point-min))
8647             (setq method (pop methods))
8648             (setq match (car method))
8649             (when (cond
8650                    ((stringp match)
8651                     ;; Regular expression.
8652                     (ignore-errors
8653                       (re-search-forward match nil t)))
8654                    ((gnus-functionp match)
8655                     ;; Function.
8656                     (save-restriction
8657                       (widen)
8658                       (setq result (funcall match gnus-newsgroup-name))))
8659                    ((consp match)
8660                     ;; Form.
8661                     (save-restriction
8662                       (widen)
8663                       (setq result (eval match)))))
8664               (setq split-name (append (cdr method) split-name))
8665               (cond ((stringp result)
8666                      (push (expand-file-name
8667                             result gnus-article-save-directory)
8668                            split-name))
8669                     ((consp result)
8670                      (setq split-name (append result split-name)))))))))
8671     split-name))
8672
8673 (defun gnus-valid-move-group-p (group)
8674   (and (boundp group)
8675        (symbol-name group)
8676        (memq 'respool
8677              (assoc (symbol-name
8678                      (car (gnus-find-method-for-group
8679                            (symbol-name group))))
8680                     gnus-valid-select-methods))))
8681
8682 (defun gnus-read-move-group-name (prompt default articles prefix)
8683   "Read a group name."
8684   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8685          (minibuffer-confirm-incomplete nil) ; XEmacs
8686          (prom
8687           (format "%s %s to:"
8688                   prompt
8689                   (if (> (length articles) 1)
8690                       (format "these %d articles" (length articles))
8691                     "this article")))
8692          (to-newsgroup
8693           (cond
8694            ((null split-name)
8695             (gnus-completing-read default prom
8696                                   gnus-active-hashtb
8697                                   'gnus-valid-move-group-p
8698                                   nil prefix
8699                                   'gnus-group-history))
8700            ((= 1 (length split-name))
8701             (gnus-completing-read (car split-name) prom
8702                                   gnus-active-hashtb
8703                                   'gnus-valid-move-group-p
8704                                   nil nil
8705                                   'gnus-group-history))
8706            (t
8707             (gnus-completing-read nil prom
8708                                   (mapcar (lambda (el) (list el))
8709                                           (nreverse split-name))
8710                                   nil nil nil
8711                                   'gnus-group-history)))))
8712     (when to-newsgroup
8713       (if (or (string= to-newsgroup "")
8714               (string= to-newsgroup prefix))
8715           (setq to-newsgroup default))
8716       (unless to-newsgroup
8717         (error "No group name entered"))
8718       (or (gnus-active to-newsgroup)
8719           (gnus-activate-group to-newsgroup)
8720           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8721                                      to-newsgroup))
8722               (or (and (gnus-request-create-group
8723                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8724                        (gnus-activate-group to-newsgroup nil nil
8725                                             (gnus-group-name-to-method
8726                                              to-newsgroup)))
8727                   (error "Couldn't create group %s" to-newsgroup)))
8728           (error "No such group: %s" to-newsgroup)))
8729     to-newsgroup))
8730
8731 ;; Summary extract commands
8732
8733 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8734   (let ((buffer-read-only nil)
8735         (article (gnus-summary-article-number))
8736         after-article b e)
8737     (unless (gnus-summary-goto-subject article)
8738       (error "No such article: %d" article))
8739     (gnus-summary-position-point)
8740     ;; If all commands are to be bunched up on one line, we collect
8741     ;; them here.
8742     (unless gnus-view-pseudos-separately
8743       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8744             files action)
8745         (while ps
8746           (setq action (cdr (assq 'action (car ps))))
8747           (setq files (list (cdr (assq 'name (car ps)))))
8748           (while (and ps (cdr ps)
8749                       (string= (or action "1")
8750                                (or (cdr (assq 'action (cadr ps))) "2")))
8751             (push (cdr (assq 'name (cadr ps))) files)
8752             (setcdr ps (cddr ps)))
8753           (when files
8754             (when (not (string-match "%s" action))
8755               (push " " files))
8756             (push " " files)
8757             (when (assq 'execute (car ps))
8758               (setcdr (assq 'execute (car ps))
8759                       (funcall (if (string-match "%s" action)
8760                                    'format 'concat)
8761                                action
8762                                (mapconcat
8763                                 (lambda (f)
8764                                   (if (equal f " ")
8765                                       f
8766                                     (gnus-quote-arg-for-sh-or-csh f)))
8767                                 files " ")))))
8768           (setq ps (cdr ps)))))
8769     (if (and gnus-view-pseudos (not not-view))
8770         (while pslist
8771           (when (assq 'execute (car pslist))
8772             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8773                                   (eq gnus-view-pseudos 'not-confirm)))
8774           (setq pslist (cdr pslist)))
8775       (save-excursion
8776         (while pslist
8777           (setq after-article (or (cdr (assq 'article (car pslist)))
8778                                   (gnus-summary-article-number)))
8779           (gnus-summary-goto-subject after-article)
8780           (forward-line 1)
8781           (setq b (point))
8782           (insert "    " (file-name-nondirectory
8783                           (cdr (assq 'name (car pslist))))
8784                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8785           (setq e (point))
8786           (forward-line -1)             ; back to `b'
8787           (gnus-add-text-properties
8788            b (1- e) (list 'gnus-number gnus-reffed-article-number
8789                           gnus-mouse-face-prop gnus-mouse-face))
8790           (gnus-data-enter
8791            after-article gnus-reffed-article-number
8792            gnus-unread-mark b (car pslist) 0 (- e b))
8793           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8794           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8795           (setq pslist (cdr pslist)))))))
8796
8797 (defun gnus-pseudos< (p1 p2)
8798   (let ((c1 (cdr (assq 'action p1)))
8799         (c2 (cdr (assq 'action p2))))
8800     (and c1 c2 (string< c1 c2))))
8801
8802 (defun gnus-request-pseudo-article (props)
8803   (cond ((assq 'execute props)
8804          (gnus-execute-command (cdr (assq 'execute props)))))
8805   (let ((gnus-current-article (gnus-summary-article-number)))
8806     (gnus-run-hooks 'gnus-mark-article-hook)))
8807
8808 (defun gnus-execute-command (command &optional automatic)
8809   (save-excursion
8810     (gnus-article-setup-buffer)
8811     (set-buffer gnus-article-buffer)
8812     (setq buffer-read-only nil)
8813     (let ((command (if automatic command
8814                      (read-string "Command: " (cons command 0)))))
8815       (erase-buffer)
8816       (insert "$ " command "\n\n")
8817       (if gnus-view-pseudo-asynchronously
8818           (start-process "gnus-execute" (current-buffer) shell-file-name
8819                          shell-command-switch command)
8820         (call-process shell-file-name nil t nil
8821                       shell-command-switch command)))))
8822
8823 ;; Summary kill commands.
8824
8825 (defun gnus-summary-edit-global-kill (article)
8826   "Edit the \"global\" kill file."
8827   (interactive (list (gnus-summary-article-number)))
8828   (gnus-group-edit-global-kill article))
8829
8830 (defun gnus-summary-edit-local-kill ()
8831   "Edit a local kill file applied to the current newsgroup."
8832   (interactive)
8833   (setq gnus-current-headers (gnus-summary-article-header))
8834   (gnus-group-edit-local-kill
8835    (gnus-summary-article-number) gnus-newsgroup-name))
8836
8837 ;;; Header reading.
8838
8839 (defun gnus-read-header (id &optional header)
8840   "Read the headers of article ID and enter them into the Gnus system."
8841   (let ((group gnus-newsgroup-name)
8842         (gnus-override-method
8843          (and (gnus-news-group-p gnus-newsgroup-name)
8844               gnus-refer-article-method))
8845         where)
8846     ;; First we check to see whether the header in question is already
8847     ;; fetched.
8848     (if (stringp id)
8849         ;; This is a Message-ID.
8850         (setq header (or header (gnus-id-to-header id)))
8851       ;; This is an article number.
8852       (setq header (or header (gnus-summary-article-header id))))
8853     (if (and header
8854              (not (gnus-summary-article-sparse-p (mail-header-number header))))
8855         ;; We have found the header.
8856         header
8857       ;; If this is a sparse article, we have to nix out its
8858       ;; previous entry in the thread hashtb.
8859       (when (and header
8860                  (gnus-summary-article-sparse-p (mail-header-number header)))
8861         (let* ((parent (gnus-parent-id (mail-header-references header)))
8862                (thread (and parent (gnus-id-to-thread parent))))
8863           (when thread
8864             (delq (assq header thread) thread))))
8865       ;; We have to really fetch the header to this article.
8866       (save-excursion
8867         (set-buffer nntp-server-buffer)
8868         (when (setq where (gnus-request-head id group))
8869           (nnheader-fold-continuation-lines)
8870           (goto-char (point-max))
8871           (insert ".\n")
8872           (goto-char (point-min))
8873           (insert "211 ")
8874           (princ (cond
8875                   ((numberp id) id)
8876                   ((cdr where) (cdr where))
8877                   (header (mail-header-number header))
8878                   (t gnus-reffed-article-number))
8879                  (current-buffer))
8880           (insert " Article retrieved.\n"))
8881         (if (or (not where)
8882                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
8883             ()                          ; Malformed head.
8884           (unless (gnus-summary-article-sparse-p (mail-header-number header))
8885             (when (and (stringp id)
8886                        (not (string= (gnus-group-real-name group)
8887                                      (car where))))
8888               ;; If we fetched by Message-ID and the article came
8889               ;; from a different group, we fudge some bogus article
8890               ;; numbers for this article.
8891               (mail-header-set-number header gnus-reffed-article-number))
8892             (save-excursion
8893               (set-buffer gnus-summary-buffer)
8894               (decf gnus-reffed-article-number)
8895               (gnus-remove-header (mail-header-number header))
8896               (push header gnus-newsgroup-headers)
8897               (setq gnus-current-headers header)
8898               (push (mail-header-number header) gnus-newsgroup-limit)))
8899           header)))))
8900
8901 (defun gnus-remove-header (number)
8902   "Remove header NUMBER from `gnus-newsgroup-headers'."
8903   (if (and gnus-newsgroup-headers
8904            (= number (mail-header-number (car gnus-newsgroup-headers))))
8905       (pop gnus-newsgroup-headers)
8906     (let ((headers gnus-newsgroup-headers))
8907       (while (and (cdr headers)
8908                   (not (= number (mail-header-number (cadr headers)))))
8909         (pop headers))
8910       (when (cdr headers)
8911         (setcdr headers (cddr headers))))))
8912
8913 ;;;
8914 ;;; summary highlights
8915 ;;;
8916
8917 (defun gnus-highlight-selected-summary ()
8918   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8919   ;; Highlight selected article in summary buffer
8920   (when gnus-summary-selected-face
8921     (save-excursion
8922       (let* ((beg (progn (beginning-of-line) (point)))
8923              (end (progn (end-of-line) (point)))
8924              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
8925              (from (if (get-text-property beg gnus-mouse-face-prop)
8926                        beg
8927                      (or (next-single-property-change
8928                           beg gnus-mouse-face-prop nil end)
8929                          beg)))
8930              (to
8931               (if (= from end)
8932                   (- from 2)
8933                 (or (next-single-property-change
8934                      from gnus-mouse-face-prop nil end)
8935                     end))))
8936         ;; If no mouse-face prop on line we will have to = from = end,
8937         ;; so we highlight the entire line instead.
8938         (when (= (+ to 2) from)
8939           (setq from beg)
8940           (setq to end))
8941         (if gnus-newsgroup-selected-overlay
8942             ;; Move old overlay.
8943             (gnus-move-overlay
8944              gnus-newsgroup-selected-overlay from to (current-buffer))
8945           ;; Create new overlay.
8946           (gnus-overlay-put
8947            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
8948            'face gnus-summary-selected-face))))))
8949
8950 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
8951 (defun gnus-summary-highlight-line ()
8952   "Highlight current line according to `gnus-summary-highlight'."
8953   (let* ((list gnus-summary-highlight)
8954          (p (point))
8955          (end (progn (end-of-line) (point)))
8956          ;; now find out where the line starts and leave point there.
8957          (beg (progn (beginning-of-line) (point)))
8958          (article (gnus-summary-article-number))
8959          (score (or (cdr (assq (or article gnus-current-article)
8960                                gnus-newsgroup-scored))
8961                     gnus-summary-default-score 0))
8962          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
8963          (inhibit-read-only t))
8964     ;; Eval the cars of the lists until we find a match.
8965     (let ((default gnus-summary-default-score))
8966       (while (and list
8967                   (not (eval (caar list))))
8968         (setq list (cdr list))))
8969     (let ((face (cdar list)))
8970       (unless (eq face (get-text-property beg 'face))
8971         (gnus-put-text-property-excluding-characters-with-faces
8972          beg end 'face
8973          (setq face (if (boundp face) (symbol-value face) face)))
8974         (when gnus-summary-highlight-line-function
8975           (funcall gnus-summary-highlight-line-function article face))))
8976     (goto-char p)))
8977
8978 (defun gnus-update-read-articles (group unread &optional compute)
8979   "Update the list of read articles in GROUP."
8980   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
8981          (entry (gnus-gethash group gnus-newsrc-hashtb))
8982          (info (nth 2 entry))
8983          (prev 1)
8984          (unread (sort (copy-sequence unread) '<))
8985          read)
8986     (if (or (not info) (not active))
8987         ;; There is no info on this group if it was, in fact,
8988         ;; killed.  Gnus stores no information on killed groups, so
8989         ;; there's nothing to be done.
8990         ;; One could store the information somewhere temporarily,
8991         ;; perhaps...  Hmmm...
8992         ()
8993       ;; Remove any negative articles numbers.
8994       (while (and unread (< (car unread) 0))
8995         (setq unread (cdr unread)))
8996       ;; Remove any expired article numbers
8997       (while (and unread (< (car unread) (car active)))
8998         (setq unread (cdr unread)))
8999       ;; Compute the ranges of read articles by looking at the list of
9000       ;; unread articles.
9001       (while unread
9002         (when (/= (car unread) prev)
9003           (push (if (= prev (1- (car unread))) prev
9004                   (cons prev (1- (car unread))))
9005                 read))
9006         (setq prev (1+ (car unread)))
9007         (setq unread (cdr unread)))
9008       (when (<= prev (cdr active))
9009         (push (cons prev (cdr active)) read))
9010       (setq read (if (> (length read) 1) (nreverse read) read))
9011       (if compute
9012          read
9013         (save-excursion
9014           (set-buffer gnus-group-buffer)
9015           (gnus-undo-register
9016             `(progn
9017                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9018                (gnus-info-set-read ',info ',(gnus-info-read info))
9019                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9020                (gnus-group-update-group ,group t))))
9021         ;; Enter this list into the group info.
9022        (gnus-info-set-read info read)
9023         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9024         (gnus-get-unread-articles-in-group info (gnus-active group))
9025         t))))
9026
9027 (defun gnus-offer-save-summaries ()
9028   "Offer to save all active summary buffers."
9029   (save-excursion
9030     (let ((buflist (buffer-list))
9031           buffers bufname)
9032       ;; Go through all buffers and find all summaries.
9033       (while buflist
9034         (and (setq bufname (buffer-name (car buflist)))
9035              (string-match "Summary" bufname)
9036              (save-excursion
9037                (set-buffer bufname)
9038                ;; We check that this is, indeed, a summary buffer.
9039                (and (eq major-mode 'gnus-summary-mode)
9040                     ;; Also make sure this isn't bogus.
9041                     gnus-newsgroup-prepared
9042                     ;; Also make sure that this isn't a dead summary buffer.
9043                     (not gnus-dead-summary-mode)))
9044              (push bufname buffers))
9045         (setq buflist (cdr buflist)))
9046       ;; Go through all these summary buffers and offer to save them.
9047       (when buffers
9048         (map-y-or-n-p
9049          "Update summary buffer %s? "
9050          (lambda (buf)
9051            (switch-to-buffer buf)
9052            (gnus-summary-exit))
9053          buffers)))))
9054
9055
9056 ;;; @ for mime-partial
9057 ;;;
9058
9059 (defun gnus-request-partial-message ()
9060   (save-excursion
9061     (let ((number (gnus-summary-article-number))
9062           (group gnus-newsgroup-name)
9063           (mother gnus-article-buffer))
9064       (set-buffer (get-buffer-create " *Partial Article*"))
9065       (erase-buffer)
9066       (setq mime-preview-buffer mother)
9067       (gnus-request-article-this-buffer number group)
9068       (mime-parse-buffer)
9069       )))
9070
9071 (autoload 'mime-combine-message/partial-pieces-automatically
9072   "mime-partial"
9073   "Internal method to combine message/partial messages automatically.")
9074
9075 (mime-add-condition
9076  'action '((type . message)(subtype . partial)
9077            (major-mode . gnus-original-article-mode)
9078            (method . mime-combine-message/partial-pieces-automatically)
9079            (summary-buffer-exp . gnus-summary-buffer)
9080            (request-partial-message-method . gnus-request-partial-message)
9081            ))
9082
9083
9084 ;;; @ end
9085 ;;;
9086
9087 (gnus-ems-redefine)
9088
9089 (provide 'gnus-sum)
9090
9091 (run-hooks 'gnus-sum-load-hook)
9092
9093 ;;; gnus-sum.el ends here