* Sync up with chaos-1_12 branch. See ChangeLog for more details.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; 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 ;; Avoid byte-compile warnings.
41 (eval-when-compile
42   (defvar gnus-article-decoded-p)
43   (defvar gnus-decode-encoded-word-function)
44   )
45
46 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
47 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
48
49 (defcustom gnus-kill-summary-on-exit t
50   "*If non-nil, kill the summary buffer when you exit from it.
51 If nil, the summary will become a \"*Dead Summary*\" buffer, and
52 it will be killed sometime later."
53   :group 'gnus-summary-exit
54   :type 'boolean)
55
56 (defcustom gnus-fetch-old-headers nil
57   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
58 If an unread article in the group refers to an older, already read (or
59 just marked as read) article, the old article will not normally be
60 displayed in the Summary buffer.  If this variable is non-nil, Gnus
61 will attempt to grab the headers to the old articles, and thereby
62 build complete threads.  If it has the value `some', only enough
63 headers to connect otherwise loose threads will be displayed.  This
64 variable can also be a number.  In that case, no more than that number
65 of old headers will be fetched.  If it has the value `invisible', all
66 old headers will be fetched, but none will be displayed.
67
68 The server has to support NOV for any of this to work."
69   :group 'gnus-thread
70   :type '(choice (const :tag "off" nil)
71                  (const some)
72                  number
73                  (sexp :menu-tag "other" t)))
74
75 (defcustom gnus-refer-thread-limit 200
76   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
77 If t, fetch all the available old headers."
78   :group 'gnus-thread
79   :type '(choice number
80                  (sexp :menu-tag "other" t)))
81
82 (defcustom gnus-summary-make-false-root 'adopt
83   "*nil means that Gnus won't gather loose threads.
84 If the root of a thread has expired or been read in a previous
85 session, the information necessary to build a complete thread has been
86 lost.  Instead of having many small sub-threads from this original thread
87 scattered all over the summary buffer, Gnus can gather them.
88
89 If non-nil, Gnus will try to gather all loose sub-threads from an
90 original thread into one large thread.
91
92 If this variable is non-nil, it should be one of `none', `adopt',
93 `dummy' or `empty'.
94
95 If this variable is `none', Gnus will not make a false root, but just
96 present the sub-threads after another.
97 If this variable is `dummy', Gnus will create a dummy root that will
98 have all the sub-threads as children.
99 If this variable is `adopt', Gnus will make one of the \"children\"
100 the parent and mark all the step-children as such.
101 If this variable is `empty', the \"children\" are printed with empty
102 subject fields.  (Or rather, they will be printed with a string
103 given by the `gnus-summary-same-subject' variable.)"
104   :group 'gnus-thread
105   :type '(choice (const :tag "off" nil)
106                  (const none)
107                  (const dummy)
108                  (const adopt)
109                  (const empty)))
110
111 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
112   "*A regexp to match subjects to be excluded from loose thread gathering.
113 As loose thread gathering is done on subjects only, that means that
114 there can be many false gatherings performed.  By rooting out certain
115 common subjects, gathering might become saner."
116   :group 'gnus-thread
117   :type 'regexp)
118
119 (defcustom gnus-summary-gather-subject-limit nil
120   "*Maximum length of subject comparisons when gathering loose threads.
121 Use nil to compare full subjects.  Setting this variable to a low
122 number will help gather threads that have been corrupted by
123 newsreaders chopping off subject lines, but it might also mean that
124 unrelated articles that have subject that happen to begin with the
125 same few characters will be incorrectly gathered.
126
127 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
128 comparing subjects."
129   :group 'gnus-thread
130   :type '(choice (const :tag "off" nil)
131                  (const fuzzy)
132                  (sexp :menu-tag "on" t)))
133
134 (defcustom gnus-simplify-subject-functions nil
135   "List of functions taking a string argument that simplify subjects.
136 The functions are applied recursively.
137
138 Useful functions to put in this list include: `gnus-simplify-subject-re',
139 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
140   :group 'gnus-thread
141   :type '(repeat function))
142
143 (defcustom gnus-simplify-ignored-prefixes nil
144   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  regexp))
148
149 (defcustom gnus-build-sparse-threads nil
150   "*If non-nil, fill in the gaps in threads.
151 If `some', only fill in the gaps that are needed to tie loose threads
152 together.  If `more', fill in all leaf nodes that Gnus can find.  If
153 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
154   :group 'gnus-thread
155   :type '(choice (const :tag "off" nil)
156                  (const some)
157                  (const more)
158                  (sexp :menu-tag "all" t)))
159
160 (defcustom gnus-summary-thread-gathering-function
161   'gnus-gather-threads-by-subject
162   "*Function used for gathering loose threads.
163 There are two pre-defined functions: `gnus-gather-threads-by-subject',
164 which only takes Subjects into consideration; and
165 `gnus-gather-threads-by-references', which compared the References
166 headers of the articles to find matches."
167   :group 'gnus-thread
168   :type '(radio (function-item gnus-gather-threads-by-subject)
169                 (function-item gnus-gather-threads-by-references)
170                 (function :tag "other")))
171
172 (defcustom gnus-summary-same-subject ""
173   "*String indicating that the current article has the same subject as the previous.
174 This variable will only be used if the value of
175 `gnus-summary-make-false-root' is `empty'."
176   :group 'gnus-summary-format
177   :type 'string)
178
179 (defcustom gnus-summary-goto-unread t
180   "*If t, marking commands will go to the next unread article.
181 If `never', commands that usually go to the next unread article, will
182 go to the next article, whether it is read or not.
183 If nil, only the marking commands will go to the next (un)read article."
184   :group 'gnus-summary-marks
185   :link '(custom-manual "(gnus)Setting Marks")
186   :type '(choice (const :tag "off" nil)
187                  (const never)
188                  (sexp :menu-tag "on" t)))
189
190 (defcustom gnus-summary-default-score 0
191   "*Default article score level.
192 All scores generated by the score files will be added to this score.
193 If this variable is nil, scoring will be disabled."
194   :group 'gnus-score-default
195   :type '(choice (const :tag "disable")
196                  integer))
197
198 (defcustom gnus-summary-zcore-fuzz 0
199   "*Fuzziness factor for the zcore in the summary buffer.
200 Articles with scores closer than this to `gnus-summary-default-score'
201 will not be marked."
202   :group 'gnus-summary-format
203   :type 'integer)
204
205 (defcustom gnus-simplify-subject-fuzzy-regexp nil
206   "*Strings to be removed when doing fuzzy matches.
207 This can either be a regular expression or list of regular expressions
208 that will be removed from subject strings if fuzzy subject
209 simplification is selected."
210   :group 'gnus-thread
211   :type '(repeat regexp))
212
213 (defcustom gnus-show-threads t
214   "*If non-nil, display threads in summary mode."
215   :group 'gnus-thread
216   :type 'boolean)
217
218 (defcustom gnus-thread-hide-subtree nil
219   "*If non-nil, hide all threads initially.
220 If threads are hidden, you have to run the command
221 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
222 to expose hidden threads."
223   :group 'gnus-thread
224   :type 'boolean)
225
226 (defcustom gnus-thread-hide-killed t
227   "*If non-nil, hide killed threads automatically."
228   :group 'gnus-thread
229   :type 'boolean)
230
231 (defcustom gnus-thread-ignore-subject t
232   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
233 If nil, articles that have different subjects from their parents will
234 start separate threads."
235   :group 'gnus-thread
236   :type 'boolean)
237
238 (defcustom gnus-thread-operation-ignore-subject t
239   "*If non-nil, subjects will be ignored when doing thread commands.
240 This affects commands like `gnus-summary-kill-thread' and
241 `gnus-summary-lower-thread'.
242
243 If this variable is nil, articles in the same thread with different
244 subjects will not be included in the operation in question.  If this
245 variable is `fuzzy', only articles that have subjects that are fuzzily
246 equal will be included."
247   :group 'gnus-thread
248   :type '(choice (const :tag "off" nil)
249                  (const fuzzy)
250                  (sexp :tag "on" t)))
251
252 (defcustom gnus-thread-indent-level 4
253   "*Number that says how much each sub-thread should be indented."
254   :group 'gnus-thread
255   :type 'integer)
256
257 (defcustom gnus-auto-extend-newsgroup t
258   "*If non-nil, extend newsgroup forward and backward when requested."
259   :group 'gnus-summary-choose
260   :type 'boolean)
261
262 (defcustom gnus-auto-select-first t
263   "*If nil, don't select the first unread article when entering a group.
264 If this variable is `best', select the highest-scored unread article
265 in the group.  If t, select the first unread article.
266
267 This variable can also be a function to place point on a likely
268 subject line.  Useful values include `gnus-summary-first-unread-subject',
269 `gnus-summary-first-unread-article' and
270 `gnus-summary-best-unread-article'.
271
272 If you want to prevent automatic selection of the first unread article
273 in some newsgroups, set the variable to nil in
274 `gnus-select-group-hook'."
275   :group 'gnus-group-select
276   :type '(choice (const :tag "none" nil)
277                  (const best)
278                  (sexp :menu-tag "first" t)
279                  (function-item gnus-summary-first-unread-subject)
280                  (function-item gnus-summary-first-unread-article)
281                  (function-item gnus-summary-best-unread-article)))
282
283 (defcustom gnus-auto-select-next t
284   "*If non-nil, offer to go to the next group from the end of the previous.
285 If the value is t and the next newsgroup is empty, Gnus will exit
286 summary mode and go back to group mode.  If the value is neither nil
287 nor t, Gnus will select the following unread newsgroup.  In
288 particular, if the value is the symbol `quietly', the next unread
289 newsgroup will be selected without any confirmation, and if it is
290 `almost-quietly', the next group will be selected without any
291 confirmation if you are located on the last article in the group.
292 Finally, if this variable is `slightly-quietly', the `Z n' command
293 will go to the next group without confirmation."
294   :group 'gnus-summary-maneuvering
295   :type '(choice (const :tag "off" nil)
296                  (const quietly)
297                  (const almost-quietly)
298                  (const slightly-quietly)
299                  (sexp :menu-tag "on" t)))
300
301 (defcustom gnus-auto-select-same nil
302   "*If non-nil, select the next article with the same subject.
303 If there are no more articles with the same subject, go to
304 the first unread article."
305   :group 'gnus-summary-maneuvering
306   :type 'boolean)
307
308 (defcustom gnus-summary-check-current nil
309   "*If non-nil, consider the current article when moving.
310 The \"unread\" movement commands will stay on the same line if the
311 current article is unread."
312   :group 'gnus-summary-maneuvering
313   :type 'boolean)
314
315 (defcustom gnus-auto-center-summary t
316   "*If non-nil, always center the current summary buffer.
317 In particular, if `vertical' do only vertical recentering.  If non-nil
318 and non-`vertical', do both horizontal and vertical recentering."
319   :group 'gnus-summary-maneuvering
320   :type '(choice (const :tag "none" nil)
321                  (const vertical)
322                  (integer :tag "height")
323                  (sexp :menu-tag "both" t)))
324
325 (defcustom gnus-show-all-headers nil
326   "*If non-nil, don't hide any headers."
327   :group 'gnus-article-hiding
328   :group 'gnus-article-headers
329   :type 'boolean)
330
331 (defcustom gnus-summary-ignore-duplicates nil
332   "*If non-nil, ignore articles with identical Message-ID headers."
333   :group 'gnus-summary
334   :type 'boolean)
335
336 (defcustom gnus-single-article-buffer t
337   "*If non-nil, display all articles in the same buffer.
338 If nil, each group will get its own article buffer."
339   :group 'gnus-article-various
340   :type 'boolean)
341
342 (defcustom gnus-break-pages t
343   "*If non-nil, do page breaking on articles.
344 The page delimiter is specified by the `gnus-page-delimiter'
345 variable."
346   :group 'gnus-article-various
347   :type 'boolean)
348
349 (defcustom gnus-show-mime t
350   "*If non-nil, do mime processing of articles.
351 The articles will simply be fed to the function given by
352 `gnus-article-display-method-for-mime'."
353   :group 'gnus-article-mime
354   :type 'boolean)
355
356 (defcustom gnus-move-split-methods nil
357   "*Variable used to suggest where articles are to be moved to.
358 It uses the same syntax as the `gnus-split-methods' variable."
359   :group 'gnus-summary-mail
360   :type '(repeat (choice (list :value (fun) function)
361                          (cons :value ("" "") regexp (repeat string))
362                          (sexp :value nil))))
363
364 (defcustom gnus-unread-mark ? ;Whitespace
365   "*Mark used for unread articles."
366   :group 'gnus-summary-marks
367   :type 'character)
368
369 (defcustom gnus-ticked-mark ?!
370   "*Mark used for ticked articles."
371   :group 'gnus-summary-marks
372   :type 'character)
373
374 (defcustom gnus-dormant-mark ??
375   "*Mark used for dormant articles."
376   :group 'gnus-summary-marks
377   :type 'character)
378
379 (defcustom gnus-del-mark ?r
380   "*Mark used for del'd articles."
381   :group 'gnus-summary-marks
382   :type 'character)
383
384 (defcustom gnus-read-mark ?R
385   "*Mark used for read articles."
386   :group 'gnus-summary-marks
387   :type 'character)
388
389 (defcustom gnus-expirable-mark ?E
390   "*Mark used for expirable articles."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-killed-mark ?K
395   "*Mark used for killed articles."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-souped-mark ?F
400   "*Mark used for killed articles."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-kill-file-mark ?X
405   "*Mark used for articles killed by kill files."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-low-score-mark ?Y
410   "*Mark used for articles with a low score."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-catchup-mark ?C
415   "*Mark used for articles that are caught up."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-replied-mark ?A
420   "*Mark used for articles that have been replied to."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-cached-mark ?*
425   "*Mark used for articles that are in the cache."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-saved-mark ?S
430   "*Mark used for articles that have been saved to."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-ancient-mark ?O
435   "*Mark used for ancient articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-sparse-mark ?Q
440   "*Mark used for sparsely reffed articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-canceled-mark ?G
445   "*Mark used for canceled articles."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-duplicate-mark ?M
450   "*Mark used for duplicate articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-undownloaded-mark ?@
455   "*Mark used for articles that weren't downloaded."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-downloadable-mark ?%
460   "*Mark used for articles that are to be downloaded."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-unsendable-mark ?=
465   "*Mark used for articles that won't be sent."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-score-over-mark ?+
470   "*Score mark used for articles with high scores."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-score-below-mark ?-
475   "*Score mark used for articles with low scores."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-empty-thread-mark ? ;Whitespace
480   "*There is no thread under the article."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-not-empty-thread-mark ?=
485   "*There is a thread under the article."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-view-pseudo-asynchronously nil
490   "*If non-nil, Gnus will view pseudo-articles asynchronously."
491   :group 'gnus-extract-view
492   :type 'boolean)
493
494 (defcustom gnus-auto-expirable-marks
495   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
496         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
497         gnus-souped-mark gnus-duplicate-mark)
498   "*The list of marks converted into expiration if a group is auto-expirable."
499   :group 'gnus-summary
500   :type '(repeat character))
501
502 (defcustom gnus-inhibit-user-auto-expire t
503   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
504   :group 'gnus-summary
505   :type 'boolean)
506
507 (defcustom gnus-view-pseudos nil
508   "*If `automatic', pseudo-articles will be viewed automatically.
509 If `not-confirm', pseudos will be viewed automatically, and the user
510 will not be asked to confirm the command."
511   :group 'gnus-extract-view
512   :type '(choice (const :tag "off" nil)
513                  (const automatic)
514                  (const not-confirm)))
515
516 (defcustom gnus-view-pseudos-separately t
517   "*If non-nil, one pseudo-article will be created for each file to be viewed.
518 If nil, all files that use the same viewing command will be given as a
519 list of parameters to that command."
520   :group 'gnus-extract-view
521   :type 'boolean)
522
523 (defcustom gnus-insert-pseudo-articles t
524   "*If non-nil, insert pseudo-articles when decoding articles."
525   :group 'gnus-extract-view
526   :type 'boolean)
527
528 (defcustom gnus-summary-dummy-line-format
529   "  %(:                          :%) %S\n"
530   "*The format specification for the dummy roots in the summary buffer.
531 It works along the same lines as a normal formatting string,
532 with some simple extensions.
533
534 %S  The subject"
535   :group 'gnus-threading
536   :type 'string)
537
538 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
539   "*The format specification for the summary mode line.
540 It works along the same lines as a normal formatting string,
541 with some simple extensions:
542
543 %G  Group name
544 %p  Unprefixed group name
545 %A  Current article number
546 %z  Current article score
547 %V  Gnus version
548 %U  Number of unread articles in the group
549 %e  Number of unselected articles in the group
550 %Z  A string with unread/unselected article counts
551 %g  Shortish group name
552 %S  Subject of the current article
553 %u  User-defined spec
554 %s  Current score file name
555 %d  Number of dormant articles
556 %r  Number of articles that have been marked as read in this session
557 %E  Number of articles expunged by the score files"
558   :group 'gnus-summary-format
559   :type 'string)
560
561 (defcustom gnus-summary-mark-below 0
562   "*Mark all articles with a score below this variable as read.
563 This variable is local to each summary buffer and usually set by the
564 score file."
565   :group 'gnus-score-default
566   :type 'integer)
567
568 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
569   "*List of functions used for sorting articles in the summary buffer.
570 This variable is only used when not using a threaded display."
571   :group 'gnus-summary-sort
572   :type '(repeat (choice (function-item gnus-article-sort-by-number)
573                          (function-item gnus-article-sort-by-author)
574                          (function-item gnus-article-sort-by-subject)
575                          (function-item gnus-article-sort-by-date)
576                          (function-item gnus-article-sort-by-score)
577                          (function :tag "other"))))
578
579 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
580   "*List of functions used for sorting threads in the summary buffer.
581 By default, threads are sorted by article number.
582
583 Each function takes two threads and return non-nil if the first thread
584 should be sorted before the other.  If you use more than one function,
585 the primary sort function should be the last.  You should probably
586 always include `gnus-thread-sort-by-number' in the list of sorting
587 functions -- preferably first.
588
589 Ready-made functions include `gnus-thread-sort-by-number',
590 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
591 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
592 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
593   :group 'gnus-summary-sort
594   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
595                          (function-item gnus-thread-sort-by-author)
596                          (function-item gnus-thread-sort-by-subject)
597                          (function-item gnus-thread-sort-by-date)
598                          (function-item gnus-thread-sort-by-score)
599                          (function-item gnus-thread-sort-by-total-score)
600                          (function :tag "other"))))
601
602 (defcustom gnus-thread-score-function '+
603   "*Function used for calculating the total score of a thread.
604
605 The function is called with the scores of the article and each
606 subthread and should then return the score of the thread.
607
608 Some functions you can use are `+', `max', or `min'."
609   :group 'gnus-summary-sort
610   :type 'function)
611
612 (defcustom gnus-summary-expunge-below nil
613   "All articles that have a score less than this variable will be expunged.
614 This variable is local to the summary buffers."
615   :group 'gnus-score-default
616   :type '(choice (const :tag "off" nil)
617                  integer))
618
619 (defcustom gnus-thread-expunge-below nil
620   "All threads that have a total score less than this variable will be expunged.
621 See `gnus-thread-score-function' for en explanation of what a
622 \"thread score\" is.
623
624 This variable is local to the summary buffers."
625   :group 'gnus-treading
626   :group 'gnus-score-default
627   :type '(choice (const :tag "off" nil)
628                  integer))
629
630 (defcustom gnus-summary-mode-hook nil
631   "*A hook for Gnus summary mode.
632 This hook is run before any variables are set in the summary buffer."
633   :group 'gnus-summary-various
634   :type 'hook)
635
636 (defcustom gnus-summary-menu-hook nil
637   "*Hook run after the creation of the summary mode menu."
638   :group 'gnus-summary-visual
639   :type 'hook)
640
641 (defcustom gnus-summary-exit-hook nil
642   "*A hook called on exit from the summary buffer.
643 It will be called with point in the group buffer."
644   :group 'gnus-summary-exit
645   :type 'hook)
646
647 (defcustom gnus-summary-prepare-hook nil
648   "*A hook called after the summary buffer has been generated.
649 If you want to modify the summary buffer, you can use this hook."
650   :group 'gnus-summary-various
651   :type 'hook)
652
653 (defcustom gnus-summary-prepared-hook nil
654   "*A hook called as the last thing after the summary buffer has been generated."
655   :group 'gnus-summary-various
656   :type 'hook)
657
658 (defcustom gnus-summary-generate-hook nil
659   "*A hook run just before generating the summary buffer.
660 This hook is commonly used to customize threading variables and the
661 like."
662   :group 'gnus-summary-various
663   :type 'hook)
664
665 (defcustom gnus-select-group-hook nil
666   "*A hook called when a newsgroup is selected.
667
668 If you'd like to simplify subjects like the
669 `gnus-summary-next-same-subject' command does, you can use the
670 following hook:
671
672  (setq gnus-select-group-hook
673       (list
674         (lambda ()
675           (mapcar (lambda (header)
676                      (mail-header-set-subject
677                       header
678                       (gnus-simplify-subject
679                        (mail-header-subject header) 're-only)))
680                   gnus-newsgroup-headers))))"
681   :group 'gnus-group-select
682   :type 'hook)
683
684 (defcustom gnus-select-article-hook nil
685   "*A hook called when an article is selected."
686   :group 'gnus-summary-choose
687   :type 'hook)
688
689 (defcustom gnus-visual-mark-article-hook
690   (list 'gnus-highlight-selected-summary)
691   "*Hook run after selecting an article in the summary buffer.
692 It is meant to be used for highlighting the article in some way.  It
693 is not run if `gnus-visual' is nil."
694   :group 'gnus-summary-visual
695   :type 'hook)
696
697 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
698   "*A hook called before parsing the headers."
699   :group 'gnus-various
700   :type 'hook)
701
702 (defcustom gnus-exit-group-hook nil
703   "*A hook called when exiting (not quitting) summary mode."
704   :group 'gnus-various
705   :type 'hook)
706
707 (defcustom gnus-summary-update-hook
708   (list 'gnus-summary-highlight-line)
709   "*A hook called when a summary line is changed.
710 The hook will not be called if `gnus-visual' is nil.
711
712 The default function `gnus-summary-highlight-line' will
713 highlight the line according to the `gnus-summary-highlight'
714 variable."
715   :group 'gnus-summary-visual
716   :type 'hook)
717
718 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
719   "*A hook called when an article is selected for the first time.
720 The hook is intended to mark an article as read (or unread)
721 automatically when it is selected."
722   :group 'gnus-summary-choose
723   :type 'hook)
724
725 (defcustom gnus-group-no-more-groups-hook nil
726   "*A hook run when returning to group mode having no more (unread) groups."
727   :group 'gnus-group-select
728   :type 'hook)
729
730 (defcustom gnus-ps-print-hook nil
731   "*A hook run before ps-printing something from Gnus."
732   :group 'gnus-summary
733   :type 'hook)
734
735 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
736   "Face used for highlighting the current article in the summary buffer."
737   :group 'gnus-summary-visual
738   :type 'face)
739
740 (defcustom gnus-summary-highlight
741   '(((= mark gnus-canceled-mark)
742      . gnus-summary-cancelled-face)
743     ((and (> score default)
744           (or (= mark gnus-dormant-mark)
745               (= mark gnus-ticked-mark)))
746      . gnus-summary-high-ticked-face)
747     ((and (< score default)
748           (or (= mark gnus-dormant-mark)
749               (= mark gnus-ticked-mark)))
750      . gnus-summary-low-ticked-face)
751     ((or (= mark gnus-dormant-mark)
752          (= mark gnus-ticked-mark))
753      . gnus-summary-normal-ticked-face)
754     ((and (> score default) (= mark gnus-ancient-mark))
755      . gnus-summary-high-ancient-face)
756     ((and (< score default) (= mark gnus-ancient-mark))
757      . gnus-summary-low-ancient-face)
758     ((= mark gnus-ancient-mark)
759      . gnus-summary-normal-ancient-face)
760     ((and (> score default) (= mark gnus-unread-mark))
761      . gnus-summary-high-unread-face)
762     ((and (< score default) (= mark gnus-unread-mark))
763      . gnus-summary-low-unread-face)
764     ((= mark gnus-unread-mark)
765      . gnus-summary-normal-unread-face)
766     ((and (> score default) (memq mark (list gnus-downloadable-mark
767                                              gnus-undownloaded-mark)))
768      . gnus-summary-high-unread-face)
769     ((and (< score default) (memq mark (list gnus-downloadable-mark
770                                              gnus-undownloaded-mark)))
771      . gnus-summary-low-unread-face)
772     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
773      . gnus-summary-normal-unread-face)
774     ((> score default)
775      . gnus-summary-high-read-face)
776     ((< score default)
777      . gnus-summary-low-read-face)
778     (t
779      . gnus-summary-normal-read-face))
780   "*Controls the highlighting of summary buffer lines.
781
782 A list of (FORM . FACE) pairs.  When deciding how a a particular
783 summary line should be displayed, each form is evaluated.  The content
784 of the face field after the first true form is used.  You can change
785 how those summary lines are displayed, by editing the face field.
786
787 You can use the following variables in the FORM field.
788
789 score:   The articles score
790 default: The default article score.
791 below:   The score below which articles are automatically marked as read.
792 mark:    The articles mark."
793   :group 'gnus-summary-visual
794   :type '(repeat (cons (sexp :tag "Form" nil)
795                        face)))
796
797 (defcustom gnus-alter-header-function nil
798   "Function called to allow alteration of article header structures.
799 The function is called with one parameter, the article header vector,
800 which it may alter in any way.")
801
802 (defcustom gnus-extra-headers nil
803   "*Extra headers to parse."
804   :group 'gnus-summary
805   :type '(repeat symbol))
806
807 (defcustom gnus-ignored-from-addresses
808   (and user-mail-address (regexp-quote user-mail-address))
809   "*Regexp of From headers that may be suppressed in favor of To headers."
810   :group 'gnus-summary
811   :type 'regexp)
812
813 (defcustom gnus-group-charset-alist
814   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
815     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
816     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
817     ("^relcom\\>" koi8-r)
818     (".*" iso-8859-1))
819   "Alist of regexps (to match group names) and default charsets to be used."
820   :type '(repeat (list (regexp :tag "Group")
821                        (symbol :tag "Charset")))
822   :group 'gnus-charset)
823
824 ;;; Internal variables
825
826 (defvar gnus-scores-exclude-files nil)
827 (defvar gnus-page-broken nil)
828 (defvar gnus-inhibit-mime-unbuttonizing nil)
829
830 (defvar gnus-original-article nil)
831 (defvar gnus-article-internal-prepare-hook nil)
832 (defvar gnus-newsgroup-process-stack nil)
833
834 (defvar gnus-thread-indent-array nil)
835 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
836
837 ;; Avoid highlighting in kill files.
838 (defvar gnus-summary-inhibit-highlight nil)
839 (defvar gnus-newsgroup-selected-overlay nil)
840 (defvar gnus-inhibit-limiting nil)
841 (defvar gnus-newsgroup-adaptive-score-file nil)
842 (defvar gnus-current-score-file nil)
843 (defvar gnus-current-move-group nil)
844 (defvar gnus-current-copy-group nil)
845 (defvar gnus-current-crosspost-group nil)
846
847 (defvar gnus-newsgroup-dependencies nil)
848 (defvar gnus-newsgroup-adaptive nil)
849 (defvar gnus-summary-display-article-function nil)
850 (defvar gnus-summary-highlight-line-function nil
851   "Function called after highlighting a summary line.")
852
853 (defvar gnus-summary-line-format-alist
854   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
855     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
856     (?s gnus-tmp-subject-or-nil ?s)
857     (?n gnus-tmp-name ?s)
858     (?A (std11-address-string
859          (car (mime-read-field 'From gnus-tmp-header))) ?s)
860     (?a (or (std11-full-name-string
861              (car (mime-read-field 'From gnus-tmp-header)))
862             gnus-tmp-from) ?s)
863     (?F gnus-tmp-from ?s)
864     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
865     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
866     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
867     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
868     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
869     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
870     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
871     (?L gnus-tmp-lines ?d)
872     (?I gnus-tmp-indentation ?s)
873     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
874     (?R gnus-tmp-replied ?c)
875     (?\[ gnus-tmp-opening-bracket ?c)
876     (?\] gnus-tmp-closing-bracket ?c)
877     (?\> (make-string gnus-tmp-level ? ) ?s)
878     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
879     (?i gnus-tmp-score ?d)
880     (?z gnus-tmp-score-char ?c)
881     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
882     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
883     (?U gnus-tmp-unread ?c)
884     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
885     (?t (gnus-summary-number-of-articles-in-thread
886          (and (boundp 'thread) (car thread)) gnus-tmp-level)
887         ?d)
888     (?e (gnus-summary-number-of-articles-in-thread
889          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
890         ?c)
891     (?u gnus-tmp-user-defined ?s)
892     (?P (gnus-pick-line-number) ?d))
893   "An alist of format specifications that can appear in summary lines,
894 and what variables they correspond with, along with the type of the
895 variable (string, integer, character, etc).")
896
897 (defvar gnus-summary-dummy-line-format-alist
898   `((?S gnus-tmp-subject ?s)
899     (?N gnus-tmp-number ?d)
900     (?u gnus-tmp-user-defined ?s)))
901
902 (defvar gnus-summary-mode-line-format-alist
903   `((?G gnus-tmp-group-name ?s)
904     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
905     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
906     (?A gnus-tmp-article-number ?d)
907     (?Z gnus-tmp-unread-and-unselected ?s)
908     (?V gnus-version ?s)
909     (?U gnus-tmp-unread-and-unticked ?d)
910     (?S gnus-tmp-subject ?s)
911     (?e gnus-tmp-unselected ?d)
912     (?u gnus-tmp-user-defined ?s)
913     (?d (length gnus-newsgroup-dormant) ?d)
914     (?t (length gnus-newsgroup-marked) ?d)
915     (?r (length gnus-newsgroup-reads) ?d)
916     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
917     (?E gnus-newsgroup-expunged-tally ?d)
918     (?s (gnus-current-score-file-nondirectory) ?s)))
919
920 (defvar gnus-last-search-regexp nil
921   "Default regexp for article search command.")
922
923 (defvar gnus-last-shell-command nil
924   "Default shell command on article.")
925
926 (defvar gnus-newsgroup-begin nil)
927 (defvar gnus-newsgroup-end nil)
928 (defvar gnus-newsgroup-last-rmail nil)
929 (defvar gnus-newsgroup-last-mail nil)
930 (defvar gnus-newsgroup-last-folder nil)
931 (defvar gnus-newsgroup-last-file nil)
932 (defvar gnus-newsgroup-auto-expire nil)
933 (defvar gnus-newsgroup-active nil)
934
935 (defvar gnus-newsgroup-data nil)
936 (defvar gnus-newsgroup-data-reverse nil)
937 (defvar gnus-newsgroup-limit nil)
938 (defvar gnus-newsgroup-limits nil)
939
940 (defvar gnus-newsgroup-unreads nil
941   "List of unread articles in the current newsgroup.")
942
943 (defvar gnus-newsgroup-unselected nil
944   "List of unselected unread articles in the current newsgroup.")
945
946 (defvar gnus-newsgroup-reads nil
947   "Alist of read articles and article marks in the current newsgroup.")
948
949 (defvar gnus-newsgroup-expunged-tally nil)
950
951 (defvar gnus-newsgroup-marked nil
952   "List of ticked articles in the current newsgroup (a subset of unread art).")
953
954 (defvar gnus-newsgroup-killed nil
955   "List of ranges of articles that have been through the scoring process.")
956
957 (defvar gnus-newsgroup-cached nil
958   "List of articles that come from the article cache.")
959
960 (defvar gnus-newsgroup-saved nil
961   "List of articles that have been saved.")
962
963 (defvar gnus-newsgroup-kill-headers nil)
964
965 (defvar gnus-newsgroup-replied nil
966   "List of articles that have been replied to in the current newsgroup.")
967
968 (defvar gnus-newsgroup-expirable nil
969   "List of articles in the current newsgroup that can be expired.")
970
971 (defvar gnus-newsgroup-processable nil
972   "List of articles in the current newsgroup that can be processed.")
973
974 (defvar gnus-newsgroup-downloadable nil
975   "List of articles in the current newsgroup that can be processed.")
976
977 (defvar gnus-newsgroup-undownloaded nil
978   "List of articles in the current newsgroup that haven't been downloaded..")
979
980 (defvar gnus-newsgroup-unsendable nil
981   "List of articles in the current newsgroup that won't be sent.")
982
983 (defvar gnus-newsgroup-bookmarks nil
984   "List of articles in the current newsgroup that have bookmarks.")
985
986 (defvar gnus-newsgroup-dormant nil
987   "List of dormant articles in the current newsgroup.")
988
989 (defvar gnus-newsgroup-scored nil
990   "List of scored articles in the current newsgroup.")
991
992 (defvar gnus-newsgroup-headers nil
993   "List of article headers in the current newsgroup.")
994
995 (defvar gnus-newsgroup-threads nil)
996
997 (defvar gnus-newsgroup-prepared nil
998   "Whether the current group has been prepared properly.")
999
1000 (defvar gnus-newsgroup-ancient nil
1001   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1002
1003 (defvar gnus-newsgroup-sparse nil)
1004
1005 (defvar gnus-current-article nil)
1006 (defvar gnus-article-current nil)
1007 (defvar gnus-current-headers nil)
1008 (defvar gnus-have-all-headers nil)
1009 (defvar gnus-last-article nil)
1010 (defvar gnus-newsgroup-history nil)
1011 (defvar gnus-newsgroup-charset nil)
1012
1013 (defconst gnus-summary-local-variables
1014   '(gnus-newsgroup-name
1015     gnus-newsgroup-begin gnus-newsgroup-end
1016     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1017     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1018     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1019     gnus-newsgroup-unselected gnus-newsgroup-marked
1020     gnus-newsgroup-reads gnus-newsgroup-saved
1021     gnus-newsgroup-replied gnus-newsgroup-expirable
1022     gnus-newsgroup-processable gnus-newsgroup-killed
1023     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1024     gnus-newsgroup-unsendable
1025     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1026     gnus-newsgroup-headers gnus-newsgroup-threads
1027     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1028     gnus-current-article gnus-current-headers gnus-have-all-headers
1029     gnus-last-article gnus-article-internal-prepare-hook
1030     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1031     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1032     gnus-thread-expunge-below
1033     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1034     (gnus-summary-mark-below . global)
1035     gnus-newsgroup-active gnus-scores-exclude-files
1036     gnus-newsgroup-history gnus-newsgroup-ancient
1037     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1038     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1039     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1040     (gnus-newsgroup-expunged-tally . 0)
1041     gnus-cache-removable-articles gnus-newsgroup-cached
1042     gnus-newsgroup-data gnus-newsgroup-data-reverse
1043     gnus-newsgroup-limit gnus-newsgroup-limits
1044     gnus-newsgroup-charset)
1045   "Variables that are buffer-local to the summary buffers.")
1046
1047 ;; Byte-compiler warning.
1048 (defvar gnus-article-mode-map)
1049
1050 ;; Subject simplification.
1051
1052 (defun gnus-simplify-whitespace (str)
1053   "Remove excessive whitespace."
1054   (let ((mystr str))
1055     ;; Multiple spaces.
1056     (while (string-match "[ \t][ \t]+" mystr)
1057       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1058                           " "
1059                           (substring mystr (match-end 0)))))
1060     ;; Leading spaces.
1061     (when (string-match "^[ \t]+" mystr)
1062       (setq mystr (substring mystr (match-end 0))))
1063     ;; Trailing spaces.
1064     (when (string-match "[ \t]+$" mystr)
1065       (setq mystr (substring mystr 0 (match-beginning 0))))
1066     mystr))
1067
1068 (defsubst gnus-simplify-subject-re (subject)
1069   "Remove \"Re:\" from subject lines."
1070   (if (string-match "^[Rr][Ee]: *" subject)
1071       (substring subject (match-end 0))
1072     subject))
1073
1074 (defun gnus-simplify-subject (subject &optional re-only)
1075   "Remove `Re:' and words in parentheses.
1076 If RE-ONLY is non-nil, strip leading `Re:'s only."
1077   (let ((case-fold-search t))           ;Ignore case.
1078     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1079     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1080       (setq subject (substring subject (match-end 0))))
1081     ;; Remove uninteresting prefixes.
1082     (when (and (not re-only)
1083                gnus-simplify-ignored-prefixes
1084                (string-match gnus-simplify-ignored-prefixes subject))
1085       (setq subject (substring subject (match-end 0))))
1086     ;; Remove words in parentheses from end.
1087     (unless re-only
1088       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1089         (setq subject (substring subject 0 (match-beginning 0)))))
1090     ;; Return subject string.
1091     subject))
1092
1093 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1094 ;; all whitespace.
1095 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1096   (goto-char (point-min))
1097   (while (re-search-forward regexp nil t)
1098       (replace-match (or newtext ""))))
1099
1100 (defun gnus-simplify-buffer-fuzzy ()
1101   "Simplify string in the buffer fuzzily.
1102 The string in the accessible portion of the current buffer is simplified.
1103 It is assumed to be a single-line subject.
1104 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1105 matter is removed.  Additional things can be deleted by setting
1106 gnus-simplify-subject-fuzzy-regexp."
1107   (let ((case-fold-search t)
1108         (modified-tick))
1109     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1110
1111     (while (not (eq modified-tick (buffer-modified-tick)))
1112       (setq modified-tick (buffer-modified-tick))
1113       (cond
1114        ((listp gnus-simplify-subject-fuzzy-regexp)
1115         (mapcar 'gnus-simplify-buffer-fuzzy-step
1116                 gnus-simplify-subject-fuzzy-regexp))
1117        (gnus-simplify-subject-fuzzy-regexp
1118         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1119       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1120       (gnus-simplify-buffer-fuzzy-step
1121        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1122       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1123
1124     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1125     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1126     (gnus-simplify-buffer-fuzzy-step " $")
1127     (gnus-simplify-buffer-fuzzy-step "^ +")))
1128
1129 (defun gnus-simplify-subject-fuzzy (subject)
1130   "Simplify a subject string fuzzily.
1131 See `gnus-simplify-buffer-fuzzy' for details."
1132   (save-excursion
1133     (gnus-set-work-buffer)
1134     (let ((case-fold-search t))
1135       ;; Remove uninteresting prefixes.
1136       (when (and gnus-simplify-ignored-prefixes
1137                  (string-match gnus-simplify-ignored-prefixes subject))
1138         (setq subject (substring subject (match-end 0))))
1139       (insert subject)
1140       (inline (gnus-simplify-buffer-fuzzy))
1141       (buffer-string))))
1142
1143 (defsubst gnus-simplify-subject-fully (subject)
1144   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1145   (cond
1146    (gnus-simplify-subject-functions
1147     (gnus-map-function gnus-simplify-subject-functions subject))
1148    ((null gnus-summary-gather-subject-limit)
1149     (gnus-simplify-subject-re subject))
1150    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1151     (gnus-simplify-subject-fuzzy subject))
1152    ((numberp gnus-summary-gather-subject-limit)
1153     (gnus-limit-string (gnus-simplify-subject-re subject)
1154                        gnus-summary-gather-subject-limit))
1155    (t
1156     subject)))
1157
1158 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1159   "Check whether two subjects are equal.
1160 If optional argument simple-first is t, first argument is already
1161 simplified."
1162   (cond
1163    ((null simple-first)
1164     (equal (gnus-simplify-subject-fully s1)
1165            (gnus-simplify-subject-fully s2)))
1166    (t
1167     (equal s1
1168            (gnus-simplify-subject-fully s2)))))
1169
1170 (defun gnus-summary-bubble-group ()
1171   "Increase the score of the current group.
1172 This is a handy function to add to `gnus-summary-exit-hook' to
1173 increase the score of each group you read."
1174   (gnus-group-add-score gnus-newsgroup-name))
1175
1176 \f
1177 ;;;
1178 ;;; Gnus summary mode
1179 ;;;
1180
1181 (put 'gnus-summary-mode 'mode-class 'special)
1182
1183 (when t
1184   ;; Non-orthogonal keys
1185
1186   (gnus-define-keys gnus-summary-mode-map
1187     " " gnus-summary-next-page
1188     "\177" gnus-summary-prev-page
1189     [delete] gnus-summary-prev-page
1190     [backspace] gnus-summary-prev-page
1191     "\r" gnus-summary-scroll-up
1192     "\M-\r" gnus-summary-scroll-down
1193     "n" gnus-summary-next-unread-article
1194     "p" gnus-summary-prev-unread-article
1195     "N" gnus-summary-next-article
1196     "P" gnus-summary-prev-article
1197     "\M-\C-n" gnus-summary-next-same-subject
1198     "\M-\C-p" gnus-summary-prev-same-subject
1199     "\M-n" gnus-summary-next-unread-subject
1200     "\M-p" gnus-summary-prev-unread-subject
1201     "." gnus-summary-first-unread-article
1202     "," gnus-summary-best-unread-article
1203     "\M-s" gnus-summary-search-article-forward
1204     "\M-r" gnus-summary-search-article-backward
1205     "<" gnus-summary-beginning-of-article
1206     ">" gnus-summary-end-of-article
1207     "j" gnus-summary-goto-article
1208     "^" gnus-summary-refer-parent-article
1209     "\M-^" gnus-summary-refer-article
1210     "u" gnus-summary-tick-article-forward
1211     "!" gnus-summary-tick-article-forward
1212     "U" gnus-summary-tick-article-backward
1213     "d" gnus-summary-mark-as-read-forward
1214     "D" gnus-summary-mark-as-read-backward
1215     "E" gnus-summary-mark-as-expirable
1216     "\M-u" gnus-summary-clear-mark-forward
1217     "\M-U" gnus-summary-clear-mark-backward
1218     "k" gnus-summary-kill-same-subject-and-select
1219     "\C-k" gnus-summary-kill-same-subject
1220     "\M-\C-k" gnus-summary-kill-thread
1221     "\M-\C-l" gnus-summary-lower-thread
1222     "e" gnus-summary-edit-article
1223     "#" gnus-summary-mark-as-processable
1224     "\M-#" gnus-summary-unmark-as-processable
1225     "\M-\C-t" gnus-summary-toggle-threads
1226     "\M-\C-s" gnus-summary-show-thread
1227     "\M-\C-h" gnus-summary-hide-thread
1228     "\M-\C-f" gnus-summary-next-thread
1229     "\M-\C-b" gnus-summary-prev-thread
1230     "\M-\C-u" gnus-summary-up-thread
1231     "\M-\C-d" gnus-summary-down-thread
1232     "&" gnus-summary-execute-command
1233     "c" gnus-summary-catchup-and-exit
1234     "\C-w" gnus-summary-mark-region-as-read
1235     "\C-t" gnus-summary-toggle-truncation
1236     "?" gnus-summary-mark-as-dormant
1237     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1238     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1239     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1240     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1241     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1242     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1243     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1244     "=" gnus-summary-expand-window
1245     "\C-x\C-s" gnus-summary-reselect-current-group
1246     "\M-g" gnus-summary-rescan-group
1247     "w" gnus-summary-stop-page-breaking
1248     "\C-c\C-r" gnus-summary-caesar-message
1249     "\M-t" gnus-summary-toggle-mime
1250     "f" gnus-summary-followup
1251     "F" gnus-summary-followup-with-original
1252     "C" gnus-summary-cancel-article
1253     "r" gnus-summary-reply
1254     "R" gnus-summary-reply-with-original
1255     "\C-c\C-f" gnus-summary-mail-forward
1256     "o" gnus-summary-save-article
1257     "\C-o" gnus-summary-save-article-mail
1258     "|" gnus-summary-pipe-output
1259     "\M-k" gnus-summary-edit-local-kill
1260     "\M-K" gnus-summary-edit-global-kill
1261     ;; "V" gnus-version
1262     "\C-c\C-d" gnus-summary-describe-group
1263     "q" gnus-summary-exit
1264     "Q" gnus-summary-exit-no-update
1265     "\C-c\C-i" gnus-info-find-node
1266     gnus-mouse-2 gnus-mouse-pick-article
1267     "m" gnus-summary-mail-other-window
1268     "a" gnus-summary-post-news
1269     "x" gnus-summary-limit-to-unread
1270     "s" gnus-summary-isearch-article
1271     "t" gnus-article-hide-headers
1272     "g" gnus-summary-show-article
1273     "l" gnus-summary-goto-last-article
1274     "v" gnus-summary-preview-mime-message
1275     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1276     "\C-d" gnus-summary-enter-digest-group
1277     "\M-\C-d" gnus-summary-read-document
1278     "\M-\C-e" gnus-summary-edit-parameters
1279     "\M-\C-g" gnus-summary-customize-parameters
1280     "\C-c\C-b" gnus-bug
1281     "*" gnus-cache-enter-article
1282     "\M-*" gnus-cache-remove-article
1283     "\M-&" gnus-summary-universal-argument
1284     "\C-l" gnus-recenter
1285     "I" gnus-summary-increase-score
1286     "L" gnus-summary-lower-score
1287     "\M-i" gnus-symbolic-argument
1288     "h" gnus-summary-select-article-buffer
1289
1290     "V" gnus-summary-score-map
1291     "X" gnus-uu-extract-map
1292     "S" gnus-summary-send-map)
1293
1294   ;; Sort of orthogonal keymap
1295   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1296     "t" gnus-summary-tick-article-forward
1297     "!" gnus-summary-tick-article-forward
1298     "d" gnus-summary-mark-as-read-forward
1299     "r" gnus-summary-mark-as-read-forward
1300     "c" gnus-summary-clear-mark-forward
1301     " " gnus-summary-clear-mark-forward
1302     "e" gnus-summary-mark-as-expirable
1303     "x" gnus-summary-mark-as-expirable
1304     "?" gnus-summary-mark-as-dormant
1305     "b" gnus-summary-set-bookmark
1306     "B" gnus-summary-remove-bookmark
1307     "#" gnus-summary-mark-as-processable
1308     "\M-#" gnus-summary-unmark-as-processable
1309     "S" gnus-summary-limit-include-expunged
1310     "C" gnus-summary-catchup
1311     "H" gnus-summary-catchup-to-here
1312     "\C-c" gnus-summary-catchup-all
1313     "k" gnus-summary-kill-same-subject-and-select
1314     "K" gnus-summary-kill-same-subject
1315     "P" gnus-uu-mark-map)
1316
1317   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1318     "c" gnus-summary-clear-above
1319     "u" gnus-summary-tick-above
1320     "m" gnus-summary-mark-above
1321     "k" gnus-summary-kill-below)
1322
1323   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1324     "/" gnus-summary-limit-to-subject
1325     "n" gnus-summary-limit-to-articles
1326     "w" gnus-summary-pop-limit
1327     "s" gnus-summary-limit-to-subject
1328     "a" gnus-summary-limit-to-author
1329     "u" gnus-summary-limit-to-unread
1330     "m" gnus-summary-limit-to-marks
1331     "v" gnus-summary-limit-to-score
1332     "*" gnus-summary-limit-include-cached
1333     "D" gnus-summary-limit-include-dormant
1334     "T" gnus-summary-limit-include-thread
1335     "d" gnus-summary-limit-exclude-dormant
1336     "t" gnus-summary-limit-to-age
1337     "E" gnus-summary-limit-include-expunged
1338     "c" gnus-summary-limit-exclude-childless-dormant
1339     "C" gnus-summary-limit-mark-excluded-as-read)
1340
1341   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1342     "n" gnus-summary-next-unread-article
1343     "p" gnus-summary-prev-unread-article
1344     "N" gnus-summary-next-article
1345     "P" gnus-summary-prev-article
1346     "\C-n" gnus-summary-next-same-subject
1347     "\C-p" gnus-summary-prev-same-subject
1348     "\M-n" gnus-summary-next-unread-subject
1349     "\M-p" gnus-summary-prev-unread-subject
1350     "f" gnus-summary-first-unread-article
1351     "b" gnus-summary-best-unread-article
1352     "j" gnus-summary-goto-article
1353     "g" gnus-summary-goto-subject
1354     "l" gnus-summary-goto-last-article
1355     "o" gnus-summary-pop-article)
1356
1357   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1358     "k" gnus-summary-kill-thread
1359     "l" gnus-summary-lower-thread
1360     "i" gnus-summary-raise-thread
1361     "T" gnus-summary-toggle-threads
1362     "t" gnus-summary-rethread-current
1363     "^" gnus-summary-reparent-thread
1364     "s" gnus-summary-show-thread
1365     "S" gnus-summary-show-all-threads
1366     "h" gnus-summary-hide-thread
1367     "H" gnus-summary-hide-all-threads
1368     "n" gnus-summary-next-thread
1369     "p" gnus-summary-prev-thread
1370     "u" gnus-summary-up-thread
1371     "o" gnus-summary-top-thread
1372     "d" gnus-summary-down-thread
1373     "#" gnus-uu-mark-thread
1374     "\M-#" gnus-uu-unmark-thread)
1375
1376   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1377     "g" gnus-summary-prepare
1378     "c" gnus-summary-insert-cached-articles)
1379
1380   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1381     "c" gnus-summary-catchup-and-exit
1382     "C" gnus-summary-catchup-all-and-exit
1383     "E" gnus-summary-exit-no-update
1384     "Q" gnus-summary-exit
1385     "Z" gnus-summary-exit
1386     "n" gnus-summary-catchup-and-goto-next-group
1387     "R" gnus-summary-reselect-current-group
1388     "G" gnus-summary-rescan-group
1389     "N" gnus-summary-next-group
1390     "s" gnus-summary-save-newsrc
1391     "P" gnus-summary-prev-group)
1392
1393   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1394     " " gnus-summary-next-page
1395     "n" gnus-summary-next-page
1396     "\177" gnus-summary-prev-page
1397     [delete] gnus-summary-prev-page
1398     "p" gnus-summary-prev-page
1399     "\r" gnus-summary-scroll-up
1400     "\M-\r" gnus-summary-scroll-down
1401     "<" gnus-summary-beginning-of-article
1402     ">" gnus-summary-end-of-article
1403     "b" gnus-summary-beginning-of-article
1404     "e" gnus-summary-end-of-article
1405     "^" gnus-summary-refer-parent-article
1406     "r" gnus-summary-refer-parent-article
1407     "R" gnus-summary-refer-references
1408     "T" gnus-summary-refer-thread
1409     "g" gnus-summary-show-article
1410     "s" gnus-summary-isearch-article
1411     "P" gnus-summary-print-article)
1412
1413   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1414     "b" gnus-article-add-buttons
1415     "B" gnus-article-add-buttons-to-head
1416     "o" gnus-article-treat-overstrike
1417     "e" gnus-article-emphasize
1418     "w" gnus-article-fill-cited-article
1419     "Q" gnus-article-fill-long-lines
1420     "c" gnus-article-remove-cr
1421     "f" gnus-article-display-x-face
1422     "l" gnus-summary-stop-page-breaking
1423     "r" gnus-summary-caesar-message
1424     "t" gnus-article-hide-headers
1425     "v" gnus-summary-verbose-headers
1426     "m" gnus-summary-toggle-mime
1427     "h" gnus-article-treat-html
1428     "d" gnus-article-treat-dumbquotes)
1429
1430   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1431     "a" gnus-article-hide
1432     "h" gnus-article-hide-headers
1433     "b" gnus-article-hide-boring-headers
1434     "s" gnus-article-hide-signature
1435     "c" gnus-article-hide-citation
1436     "C" gnus-article-hide-citation-in-followups
1437     "p" gnus-article-hide-pgp
1438     "P" gnus-article-hide-pem
1439     "\C-c" gnus-article-hide-citation-maybe)
1440
1441   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1442     "a" gnus-article-highlight
1443     "h" gnus-article-highlight-headers
1444     "c" gnus-article-highlight-citation
1445     "s" gnus-article-highlight-signature)
1446
1447   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1448     "z" gnus-article-date-ut
1449     "u" gnus-article-date-ut
1450     "l" gnus-article-date-local
1451     "e" gnus-article-date-lapsed
1452     "o" gnus-article-date-original
1453     "i" gnus-article-date-iso8601
1454     "s" gnus-article-date-user)
1455
1456   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1457     "t" gnus-article-remove-trailing-blank-lines
1458     "l" gnus-article-strip-leading-blank-lines
1459     "m" gnus-article-strip-multiple-blank-lines
1460     "a" gnus-article-strip-blank-lines
1461     "A" gnus-article-strip-all-blank-lines
1462     "s" gnus-article-strip-leading-space
1463     "e" gnus-article-strip-trailing-space)
1464
1465   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1466     "v" gnus-version
1467     "f" gnus-summary-fetch-faq
1468     "d" gnus-summary-describe-group
1469     "h" gnus-summary-describe-briefly
1470     "i" gnus-info-find-node)
1471
1472   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1473     "e" gnus-summary-expire-articles
1474     "\M-\C-e" gnus-summary-expire-articles-now
1475     "\177" gnus-summary-delete-article
1476     [delete] gnus-summary-delete-article
1477     "m" gnus-summary-move-article
1478     "r" gnus-summary-respool-article
1479     "w" gnus-summary-edit-article
1480     "c" gnus-summary-copy-article
1481     "B" gnus-summary-crosspost-article
1482     "q" gnus-summary-respool-query
1483     "t" gnus-summary-respool-trace
1484     "i" gnus-summary-import-article
1485     "p" gnus-summary-article-posted-p)
1486
1487   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1488     "o" gnus-summary-save-article
1489     "m" gnus-summary-save-article-mail
1490     "F" gnus-summary-write-article-file
1491     "r" gnus-summary-save-article-rmail
1492     "f" gnus-summary-save-article-file
1493     "b" gnus-summary-save-article-body-file
1494     "h" gnus-summary-save-article-folder
1495     "v" gnus-summary-save-article-vm
1496     "p" gnus-summary-pipe-output
1497     "s" gnus-soup-add-article)
1498
1499   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1500     "b" gnus-summary-display-buttonized
1501     "m" gnus-summary-repair-multipart
1502     "v" gnus-article-view-part
1503     "o" gnus-article-save-part
1504     "c" gnus-article-copy-part
1505     "e" gnus-article-externalize-part
1506     "|" gnus-article-pipe-part)
1507   )
1508
1509 (defun gnus-summary-make-menu-bar ()
1510   (gnus-turn-off-edit-menu 'summary)
1511
1512   (unless (boundp 'gnus-summary-misc-menu)
1513
1514     (easy-menu-define
1515      gnus-summary-kill-menu gnus-summary-mode-map ""
1516      (cons
1517       "Score"
1518       (nconc
1519        (list
1520         ["Enter score..." gnus-summary-score-entry t]
1521         ["Customize" gnus-score-customize t])
1522        (gnus-make-score-map 'increase)
1523        (gnus-make-score-map 'lower)
1524        '(("Mark"
1525           ["Kill below" gnus-summary-kill-below t]
1526           ["Mark above" gnus-summary-mark-above t]
1527           ["Tick above" gnus-summary-tick-above t]
1528           ["Clear above" gnus-summary-clear-above t])
1529          ["Current score" gnus-summary-current-score t]
1530          ["Set score" gnus-summary-set-score t]
1531          ["Switch current score file..." gnus-score-change-score-file t]
1532          ["Set mark below..." gnus-score-set-mark-below t]
1533          ["Set expunge below..." gnus-score-set-expunge-below t]
1534          ["Edit current score file" gnus-score-edit-current-scores t]
1535          ["Edit score file" gnus-score-edit-file t]
1536          ["Trace score" gnus-score-find-trace t]
1537          ["Find words" gnus-score-find-favourite-words t]
1538          ["Rescore buffer" gnus-summary-rescore t]
1539          ["Increase score..." gnus-summary-increase-score t]
1540          ["Lower score..." gnus-summary-lower-score t]))))
1541
1542     ;; Define both the Article menu in the summary buffer and the equivalent
1543     ;; Commands menu in the article buffer here for consistency.
1544     (let ((innards
1545            '(("Hide"
1546               ["All" gnus-article-hide t]
1547               ["Headers" gnus-article-hide-headers t]
1548               ["Signature" gnus-article-hide-signature t]
1549               ["Citation" gnus-article-hide-citation t]
1550               ["PGP" gnus-article-hide-pgp t]
1551               ["Boring headers" gnus-article-hide-boring-headers t])
1552              ("Highlight"
1553               ["All" gnus-article-highlight t]
1554               ["Headers" gnus-article-highlight-headers t]
1555               ["Signature" gnus-article-highlight-signature t]
1556               ["Citation" gnus-article-highlight-citation t])
1557              ("Date"
1558               ["Local" gnus-article-date-local t]
1559               ["ISO8601" gnus-article-date-iso8601 t]
1560               ["UT" gnus-article-date-ut t]
1561               ["Original" gnus-article-date-original t]
1562               ["Lapsed" gnus-article-date-lapsed t]
1563               ["User-defined" gnus-article-date-user t])
1564              ("Washing"
1565               ("Remove Blanks"
1566                ["Leading" gnus-article-strip-leading-blank-lines t]
1567                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1568                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1569                ["All of the above" gnus-article-strip-blank-lines t]
1570                ["All" gnus-article-strip-all-blank-lines t]
1571                ["Leading space" gnus-article-strip-leading-space t]
1572                ["Trailing space" gnus-article-strip-trailing-space t])
1573               ["Overstrike" gnus-article-treat-overstrike t]
1574               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1575               ["Emphasis" gnus-article-emphasize t]
1576               ["Word wrap" gnus-article-fill-cited-article t]
1577               ["Fill long lines" gnus-article-fill-long-lines t]
1578               ["CR" gnus-article-remove-cr t]
1579               ["Show X-Face" gnus-article-display-x-face t]
1580               ["UnHTMLize" gnus-article-treat-html t]
1581               ["Rot 13" gnus-summary-caesar-message t]
1582               ["Unix pipe" gnus-summary-pipe-message t]
1583               ["Add buttons" gnus-article-add-buttons t]
1584               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1585               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1586               ["Toggle MIME" gnus-summary-toggle-mime t]
1587               ["Verbose header" gnus-summary-verbose-headers t]
1588               ["Toggle header" gnus-summary-toggle-header t])
1589              ("Output"
1590               ["Save in default format" gnus-summary-save-article t]
1591               ["Save in file" gnus-summary-save-article-file t]
1592               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1593               ["Save in MH folder" gnus-summary-save-article-folder t]
1594               ["Save in VM folder" gnus-summary-save-article-vm t]
1595               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1596               ["Save body in file" gnus-summary-save-article-body-file t]
1597               ["Pipe through a filter" gnus-summary-pipe-output t]
1598               ["Add to SOUP packet" gnus-soup-add-article t]
1599               ["Print" gnus-summary-print-article t])
1600              ("Backend"
1601               ["Respool article..." gnus-summary-respool-article t]
1602               ["Move article..." gnus-summary-move-article
1603                (gnus-check-backend-function
1604                 'request-move-article gnus-newsgroup-name)]
1605               ["Copy article..." gnus-summary-copy-article t]
1606               ["Crosspost article..." gnus-summary-crosspost-article
1607                (gnus-check-backend-function
1608                 'request-replace-article gnus-newsgroup-name)]
1609               ["Import file..." gnus-summary-import-article t]
1610               ["Check if posted" gnus-summary-article-posted-p t]
1611               ["Edit article" gnus-summary-edit-article
1612                (not (gnus-group-read-only-p))]
1613               ["Delete article" gnus-summary-delete-article
1614                (gnus-check-backend-function
1615                 'request-expire-articles gnus-newsgroup-name)]
1616               ["Query respool" gnus-summary-respool-query t]
1617               ["Trace respool" gnus-summary-respool-trace t]
1618               ["Delete expirable articles" gnus-summary-expire-articles-now
1619                (gnus-check-backend-function
1620                 'request-expire-articles gnus-newsgroup-name)])
1621              ("Extract"
1622               ["Uudecode" gnus-uu-decode-uu t]
1623               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1624               ["Unshar" gnus-uu-decode-unshar t]
1625               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1626               ["Save" gnus-uu-decode-save t]
1627               ["Binhex" gnus-uu-decode-binhex t]
1628               ["Postscript" gnus-uu-decode-postscript t])
1629              ("Cache"
1630               ["Enter article" gnus-cache-enter-article t]
1631               ["Remove article" gnus-cache-remove-article t])
1632              ["Select article buffer" gnus-summary-select-article-buffer t]
1633              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1634              ["Isearch article..." gnus-summary-isearch-article t]
1635              ["Beginning of the article" gnus-summary-beginning-of-article t]
1636              ["End of the article" gnus-summary-end-of-article t]
1637              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1638              ["Fetch referenced articles" gnus-summary-refer-references t]
1639              ["Fetch current thread" gnus-summary-refer-thread t]
1640              ["Fetch article with id..." gnus-summary-refer-article t]
1641              ["Redisplay" gnus-summary-show-article t])))
1642       (easy-menu-define
1643        gnus-summary-article-menu gnus-summary-mode-map ""
1644        (cons "Article" innards))
1645
1646       (easy-menu-define
1647        gnus-article-commands-menu gnus-article-mode-map ""
1648        (cons "Commands" innards)))
1649
1650     (easy-menu-define
1651      gnus-summary-thread-menu gnus-summary-mode-map ""
1652      '("Threads"
1653        ["Toggle threading" gnus-summary-toggle-threads t]
1654        ["Hide threads" gnus-summary-hide-all-threads t]
1655        ["Show threads" gnus-summary-show-all-threads t]
1656        ["Hide thread" gnus-summary-hide-thread t]
1657        ["Show thread" gnus-summary-show-thread t]
1658        ["Go to next thread" gnus-summary-next-thread t]
1659        ["Go to previous thread" gnus-summary-prev-thread t]
1660        ["Go down thread" gnus-summary-down-thread t]
1661        ["Go up thread" gnus-summary-up-thread t]
1662        ["Top of thread" gnus-summary-top-thread t]
1663        ["Mark thread as read" gnus-summary-kill-thread t]
1664        ["Lower thread score" gnus-summary-lower-thread t]
1665        ["Raise thread score" gnus-summary-raise-thread t]
1666        ["Rethread current" gnus-summary-rethread-current t]
1667        ))
1668
1669     (easy-menu-define
1670      gnus-summary-post-menu gnus-summary-mode-map ""
1671      '("Post"
1672        ["Post an article" gnus-summary-post-news t]
1673        ["Followup" gnus-summary-followup t]
1674        ["Followup and yank" gnus-summary-followup-with-original t]
1675        ["Supersede article" gnus-summary-supersede-article t]
1676        ["Cancel article" gnus-summary-cancel-article t]
1677        ["Reply" gnus-summary-reply t]
1678        ["Reply and yank" gnus-summary-reply-with-original t]
1679        ["Wide reply" gnus-summary-wide-reply t]
1680        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1681        ["Mail forward" gnus-summary-mail-forward t]
1682        ["Post forward" gnus-summary-post-forward t]
1683        ["Digest and mail" gnus-summary-mail-digest t]
1684        ["Digest and post" gnus-summary-post-digest t]
1685        ["Resend message" gnus-summary-resend-message t]
1686        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1687        ["Send a mail" gnus-summary-mail-other-window t]
1688        ["Uuencode and post" gnus-uu-post-news t]
1689        ["Followup via news" gnus-summary-followup-to-mail t]
1690        ["Followup via news and yank"
1691         gnus-summary-followup-to-mail-with-original t]
1692        ;;("Draft"
1693        ;;["Send" gnus-summary-send-draft t]
1694        ;;["Send bounced" gnus-resend-bounced-mail t])
1695        ))
1696
1697     (easy-menu-define
1698      gnus-summary-misc-menu gnus-summary-mode-map ""
1699      '("Misc"
1700        ("Mark Read"
1701         ["Mark as read" gnus-summary-mark-as-read-forward t]
1702         ["Mark same subject and select"
1703          gnus-summary-kill-same-subject-and-select t]
1704         ["Mark same subject" gnus-summary-kill-same-subject t]
1705         ["Catchup" gnus-summary-catchup t]
1706         ["Catchup all" gnus-summary-catchup-all t]
1707         ["Catchup to here" gnus-summary-catchup-to-here t]
1708         ["Catchup region" gnus-summary-mark-region-as-read t]
1709         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1710        ("Mark Various"
1711         ["Tick" gnus-summary-tick-article-forward t]
1712         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1713         ["Remove marks" gnus-summary-clear-mark-forward t]
1714         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1715         ["Set bookmark" gnus-summary-set-bookmark t]
1716         ["Remove bookmark" gnus-summary-remove-bookmark t])
1717        ("Mark Limit"
1718         ["Marks..." gnus-summary-limit-to-marks t]
1719         ["Subject..." gnus-summary-limit-to-subject t]
1720         ["Author..." gnus-summary-limit-to-author t]
1721         ["Age..." gnus-summary-limit-to-age t]
1722         ["Score" gnus-summary-limit-to-score t]
1723         ["Unread" gnus-summary-limit-to-unread t]
1724         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1725         ["Articles" gnus-summary-limit-to-articles t]
1726         ["Pop limit" gnus-summary-pop-limit t]
1727         ["Show dormant" gnus-summary-limit-include-dormant t]
1728         ["Hide childless dormant"
1729          gnus-summary-limit-exclude-childless-dormant t]
1730         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1731         ["Show expunged" gnus-summary-show-all-expunged t])
1732        ("Process Mark"
1733         ["Set mark" gnus-summary-mark-as-processable t]
1734         ["Remove mark" gnus-summary-unmark-as-processable t]
1735         ["Remove all marks" gnus-summary-unmark-all-processable t]
1736         ["Mark above" gnus-uu-mark-over t]
1737         ["Mark series" gnus-uu-mark-series t]
1738         ["Mark region" gnus-uu-mark-region t]
1739         ["Unmark region" gnus-uu-unmark-region t]
1740         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1741         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1742         ["Mark all" gnus-uu-mark-all t]
1743         ["Mark buffer" gnus-uu-mark-buffer t]
1744         ["Mark sparse" gnus-uu-mark-sparse t]
1745         ["Mark thread" gnus-uu-mark-thread t]
1746         ["Unmark thread" gnus-uu-unmark-thread t]
1747         ("Process Mark Sets"
1748          ["Kill" gnus-summary-kill-process-mark t]
1749          ["Yank" gnus-summary-yank-process-mark
1750           gnus-newsgroup-process-stack]
1751          ["Save" gnus-summary-save-process-mark t]))
1752        ("Scroll article"
1753         ["Page forward" gnus-summary-next-page t]
1754         ["Page backward" gnus-summary-prev-page t]
1755         ["Line forward" gnus-summary-scroll-up t])
1756        ("Move"
1757         ["Next unread article" gnus-summary-next-unread-article t]
1758         ["Previous unread article" gnus-summary-prev-unread-article t]
1759         ["Next article" gnus-summary-next-article t]
1760         ["Previous article" gnus-summary-prev-article t]
1761         ["Next unread subject" gnus-summary-next-unread-subject t]
1762         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1763         ["Next article same subject" gnus-summary-next-same-subject t]
1764         ["Previous article same subject" gnus-summary-prev-same-subject t]
1765         ["First unread article" gnus-summary-first-unread-article t]
1766         ["Best unread article" gnus-summary-best-unread-article t]
1767         ["Go to subject number..." gnus-summary-goto-subject t]
1768         ["Go to article number..." gnus-summary-goto-article t]
1769         ["Go to the last article" gnus-summary-goto-last-article t]
1770         ["Pop article off history" gnus-summary-pop-article t])
1771        ("Sort"
1772         ["Sort by number" gnus-summary-sort-by-number t]
1773         ["Sort by author" gnus-summary-sort-by-author t]
1774         ["Sort by subject" gnus-summary-sort-by-subject t]
1775         ["Sort by date" gnus-summary-sort-by-date t]
1776         ["Sort by score" gnus-summary-sort-by-score t]
1777         ["Sort by lines" gnus-summary-sort-by-lines t])
1778        ("Help"
1779         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1780         ["Describe group" gnus-summary-describe-group t]
1781         ["Read manual" gnus-info-find-node t])
1782        ("Modes"
1783         ["Pick and read" gnus-pick-mode t]
1784         ["Binary" gnus-binary-mode t])
1785        ("Regeneration"
1786         ["Regenerate" gnus-summary-prepare t]
1787         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1788         ["Toggle threading" gnus-summary-toggle-threads t])
1789        ["Filter articles..." gnus-summary-execute-command t]
1790        ["Run command on subjects..." gnus-summary-universal-argument t]
1791        ["Search articles forward..." gnus-summary-search-article-forward t]
1792        ["Search articles backward..." gnus-summary-search-article-backward t]
1793        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1794        ["Expand window" gnus-summary-expand-window t]
1795        ["Expire expirable articles" gnus-summary-expire-articles
1796         (gnus-check-backend-function
1797          'request-expire-articles gnus-newsgroup-name)]
1798        ["Edit local kill file" gnus-summary-edit-local-kill t]
1799        ["Edit main kill file" gnus-summary-edit-global-kill t]
1800        ["Edit group parameters" gnus-summary-edit-parameters t]
1801        ["Customize group parameters" gnus-summary-customize-parameters t]
1802        ["Send a bug report" gnus-bug t]
1803        ("Exit"
1804         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1805         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1806         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1807         ["Exit group" gnus-summary-exit t]
1808         ["Exit group without updating" gnus-summary-exit-no-update t]
1809         ["Exit and goto next group" gnus-summary-next-group t]
1810         ["Exit and goto prev group" gnus-summary-prev-group t]
1811         ["Reselect group" gnus-summary-reselect-current-group t]
1812         ["Rescan group" gnus-summary-rescan-group t]
1813         ["Update dribble" gnus-summary-save-newsrc t])))
1814
1815     (gnus-run-hooks 'gnus-summary-menu-hook)))
1816
1817 (defun gnus-score-set-default (var value)
1818   "A version of set that updates the GNU Emacs menu-bar."
1819   (set var value)
1820   ;; It is the message that forces the active status to be updated.
1821   (message ""))
1822
1823 (defun gnus-make-score-map (type)
1824   "Make a summary score map of type TYPE."
1825   (if t
1826       nil
1827     (let ((headers '(("author" "from" string)
1828                      ("subject" "subject" string)
1829                      ("article body" "body" string)
1830                      ("article head" "head" string)
1831                      ("xref" "xref" string)
1832                      ("extra header" "extra" string)
1833                      ("lines" "lines" number)
1834                      ("followups to author" "followup" string)))
1835           (types '((number ("less than" <)
1836                            ("greater than" >)
1837                            ("equal" =))
1838                    (string ("substring" s)
1839                            ("exact string" e)
1840                            ("fuzzy string" f)
1841                            ("regexp" r))))
1842           (perms '(("temporary" (current-time-string))
1843                    ("permanent" nil)
1844                    ("immediate" now)))
1845           header)
1846       (list
1847        (apply
1848         'nconc
1849         (list
1850          (if (eq type 'lower)
1851              "Lower score"
1852            "Increase score"))
1853         (let (outh)
1854           (while headers
1855             (setq header (car headers))
1856             (setq outh
1857                   (cons
1858                    (apply
1859                     'nconc
1860                     (list (car header))
1861                     (let ((ts (cdr (assoc (nth 2 header) types)))
1862                           outt)
1863                       (while ts
1864                         (setq outt
1865                               (cons
1866                                (apply
1867                                 'nconc
1868                                 (list (caar ts))
1869                                 (let ((ps perms)
1870                                       outp)
1871                                   (while ps
1872                                     (setq outp
1873                                           (cons
1874                                            (vector
1875                                             (caar ps)
1876                                             (list
1877                                              'gnus-summary-score-entry
1878                                              (nth 1 header)
1879                                              (if (or (string= (nth 1 header)
1880                                                               "head")
1881                                                      (string= (nth 1 header)
1882                                                               "body"))
1883                                                  ""
1884                                                (list 'gnus-summary-header
1885                                                      (nth 1 header)))
1886                                              (list 'quote (nth 1 (car ts)))
1887                                              (list 'gnus-score-default nil)
1888                                              (nth 1 (car ps))
1889                                              t)
1890                                             t)
1891                                            outp))
1892                                     (setq ps (cdr ps)))
1893                                   (list (nreverse outp))))
1894                                outt))
1895                         (setq ts (cdr ts)))
1896                       (list (nreverse outt))))
1897                    outh))
1898             (setq headers (cdr headers)))
1899           (list (nreverse outh))))))))
1900
1901 \f
1902
1903 (defun gnus-summary-mode (&optional group)
1904   "Major mode for reading articles.
1905
1906 All normal editing commands are switched off.
1907 \\<gnus-summary-mode-map>
1908 Each line in this buffer represents one article.  To read an
1909 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1910 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1911 respectively.
1912
1913 You can also post articles and send mail from this buffer.  To
1914 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1915 of an article, type `\\[gnus-summary-reply]'.
1916
1917 There are approx. one gazillion commands you can execute in this
1918 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1919
1920 The following commands are available:
1921
1922 \\{gnus-summary-mode-map}"
1923   (interactive)
1924   (when (gnus-visual-p 'summary-menu 'menu)
1925     (gnus-summary-make-menu-bar))
1926   (kill-all-local-variables)
1927   (gnus-summary-make-local-variables)
1928   (gnus-make-thread-indent-array)
1929   (gnus-simplify-mode-line)
1930   (setq major-mode 'gnus-summary-mode)
1931   (setq mode-name "Summary")
1932   (make-local-variable 'minor-mode-alist)
1933   (use-local-map gnus-summary-mode-map)
1934   (buffer-disable-undo)
1935   (setq buffer-read-only t)             ;Disable modification
1936   (setq truncate-lines t)
1937   (setq selective-display t)
1938   (setq selective-display-ellipses t)   ;Display `...'
1939   (gnus-summary-set-display-table)
1940   (gnus-set-default-directory)
1941   (setq gnus-newsgroup-name group)
1942   (make-local-variable 'gnus-summary-line-format)
1943   (make-local-variable 'gnus-summary-line-format-spec)
1944   (make-local-variable 'gnus-summary-dummy-line-format)
1945   (make-local-variable 'gnus-summary-dummy-line-format-spec)
1946   (make-local-variable 'gnus-summary-mark-positions)
1947   (make-local-hook 'pre-command-hook)
1948   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
1949   (gnus-run-hooks 'gnus-summary-mode-hook)
1950   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
1951   (gnus-update-summary-mark-positions))
1952
1953 (defun gnus-summary-make-local-variables ()
1954   "Make all the local summary buffer variables."
1955   (let ((locals gnus-summary-local-variables)
1956         global local)
1957     (while (setq local (pop locals))
1958       (if (consp local)
1959           (progn
1960             (if (eq (cdr local) 'global)
1961                 ;; Copy the global value of the variable.
1962                 (setq global (symbol-value (car local)))
1963               ;; Use the value from the list.
1964               (setq global (eval (cdr local))))
1965             (make-local-variable (car local))
1966             (set (car local) global))
1967         ;; Simple nil-valued local variable.
1968         (make-local-variable local)
1969         (set local nil)))))
1970
1971 (defun gnus-summary-clear-local-variables ()
1972   (let ((locals gnus-summary-local-variables))
1973     (while locals
1974       (if (consp (car locals))
1975           (and (vectorp (caar locals))
1976                (set (caar locals) nil))
1977         (and (vectorp (car locals))
1978              (set (car locals) nil)))
1979       (setq locals (cdr locals)))))
1980
1981 ;; Summary data functions.
1982
1983 (defmacro gnus-data-number (data)
1984   `(car ,data))
1985
1986 (defmacro gnus-data-set-number (data number)
1987   `(setcar ,data ,number))
1988
1989 (defmacro gnus-data-mark (data)
1990   `(nth 1 ,data))
1991
1992 (defmacro gnus-data-set-mark (data mark)
1993   `(setcar (nthcdr 1 ,data) ,mark))
1994
1995 (defmacro gnus-data-pos (data)
1996   `(nth 2 ,data))
1997
1998 (defmacro gnus-data-set-pos (data pos)
1999   `(setcar (nthcdr 2 ,data) ,pos))
2000
2001 (defmacro gnus-data-header (data)
2002   `(nth 3 ,data))
2003
2004 (defmacro gnus-data-set-header (data header)
2005   `(setf (nth 3 ,data) ,header))
2006
2007 (defmacro gnus-data-level (data)
2008   `(nth 4 ,data))
2009
2010 (defmacro gnus-data-unread-p (data)
2011   `(= (nth 1 ,data) gnus-unread-mark))
2012
2013 (defmacro gnus-data-read-p (data)
2014   `(/= (nth 1 ,data) gnus-unread-mark))
2015
2016 (defmacro gnus-data-pseudo-p (data)
2017   `(consp (nth 3 ,data)))
2018
2019 (defmacro gnus-data-find (number)
2020   `(assq ,number gnus-newsgroup-data))
2021
2022 (defmacro gnus-data-find-list (number &optional data)
2023   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2024      (memq (assq ,number bdata)
2025            bdata)))
2026
2027 (defmacro gnus-data-make (number mark pos header level)
2028   `(list ,number ,mark ,pos ,header ,level))
2029
2030 (defun gnus-data-enter (after-article number mark pos header level offset)
2031   (let ((data (gnus-data-find-list after-article)))
2032     (unless data
2033       (error "No such article: %d" after-article))
2034     (setcdr data (cons (gnus-data-make number mark pos header level)
2035                        (cdr data)))
2036     (setq gnus-newsgroup-data-reverse nil)
2037     (gnus-data-update-list (cddr data) offset)))
2038
2039 (defun gnus-data-enter-list (after-article list &optional offset)
2040   (when list
2041     (let ((data (and after-article (gnus-data-find-list after-article)))
2042           (ilist list))
2043       (if (not (or data
2044                    after-article))
2045           (let ((odata gnus-newsgroup-data))
2046             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2047             (when offset
2048               (gnus-data-update-list odata offset)))
2049         ;; Find the last element in the list to be spliced into the main
2050         ;; list.
2051         (while (cdr list)
2052           (setq list (cdr list)))
2053         (if (not data)
2054             (progn
2055               (setcdr list gnus-newsgroup-data)
2056               (setq gnus-newsgroup-data ilist)
2057               (when offset
2058                 (gnus-data-update-list (cdr list) offset)))
2059           (setcdr list (cdr data))
2060           (setcdr data ilist)
2061           (when offset
2062             (gnus-data-update-list (cdr list) offset))))
2063       (setq gnus-newsgroup-data-reverse nil))))
2064
2065 (defun gnus-data-remove (article &optional offset)
2066   (let ((data gnus-newsgroup-data))
2067     (if (= (gnus-data-number (car data)) article)
2068         (progn
2069           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2070                 gnus-newsgroup-data-reverse nil)
2071           (when offset
2072             (gnus-data-update-list gnus-newsgroup-data offset)))
2073       (while (cdr data)
2074         (when (= (gnus-data-number (cadr data)) article)
2075           (setcdr data (cddr data))
2076           (when offset
2077             (gnus-data-update-list (cdr data) offset))
2078           (setq data nil
2079                 gnus-newsgroup-data-reverse nil))
2080         (setq data (cdr data))))))
2081
2082 (defmacro gnus-data-list (backward)
2083   `(if ,backward
2084        (or gnus-newsgroup-data-reverse
2085            (setq gnus-newsgroup-data-reverse
2086                  (reverse gnus-newsgroup-data)))
2087      gnus-newsgroup-data))
2088
2089 (defun gnus-data-update-list (data offset)
2090   "Add OFFSET to the POS of all data entries in DATA."
2091   (setq gnus-newsgroup-data-reverse nil)
2092   (while data
2093     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2094     (setq data (cdr data))))
2095
2096 (defun gnus-summary-article-pseudo-p (article)
2097   "Say whether this article is a pseudo article or not."
2098   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2099
2100 (defmacro gnus-summary-article-sparse-p (article)
2101   "Say whether this article is a sparse article or not."
2102   `(memq ,article gnus-newsgroup-sparse))
2103
2104 (defmacro gnus-summary-article-ancient-p (article)
2105   "Say whether this article is a sparse article or not."
2106   `(memq ,article gnus-newsgroup-ancient))
2107
2108 (defun gnus-article-parent-p (number)
2109   "Say whether this article is a parent or not."
2110   (let ((data (gnus-data-find-list number)))
2111     (and (cdr data)                     ; There has to be an article after...
2112          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2113             (gnus-data-level (nth 1 data))))))
2114
2115 (defun gnus-article-children (number)
2116   "Return a list of all children to NUMBER."
2117   (let* ((data (gnus-data-find-list number))
2118          (level (gnus-data-level (car data)))
2119          children)
2120     (setq data (cdr data))
2121     (while (and data
2122                 (= (gnus-data-level (car data)) (1+ level)))
2123       (push (gnus-data-number (car data)) children)
2124       (setq data (cdr data)))
2125     children))
2126
2127 (defmacro gnus-summary-skip-intangible ()
2128   "If the current article is intangible, then jump to a different article."
2129   '(let ((to (get-text-property (point) 'gnus-intangible)))
2130      (and to (gnus-summary-goto-subject to))))
2131
2132 (defmacro gnus-summary-article-intangible-p ()
2133   "Say whether this article is intangible or not."
2134   '(get-text-property (point) 'gnus-intangible))
2135
2136 (defun gnus-article-read-p (article)
2137   "Say whether ARTICLE is read or not."
2138   (not (or (memq article gnus-newsgroup-marked)
2139            (memq article gnus-newsgroup-unreads)
2140            (memq article gnus-newsgroup-unselected)
2141            (memq article gnus-newsgroup-dormant))))
2142
2143 ;; Some summary mode macros.
2144
2145 (defmacro gnus-summary-article-number ()
2146   "The article number of the article on the current line.
2147 If there isn's an article number here, then we return the current
2148 article number."
2149   '(progn
2150      (gnus-summary-skip-intangible)
2151      (or (get-text-property (point) 'gnus-number)
2152          (gnus-summary-last-subject))))
2153
2154 (defmacro gnus-summary-article-header (&optional number)
2155   "Return the header of article NUMBER."
2156   `(gnus-data-header (gnus-data-find
2157                       ,(or number '(gnus-summary-article-number)))))
2158
2159 (defmacro gnus-summary-thread-level (&optional number)
2160   "Return the level of thread that starts with article NUMBER."
2161   `(if (and (eq gnus-summary-make-false-root 'dummy)
2162             (get-text-property (point) 'gnus-intangible))
2163        0
2164      (gnus-data-level (gnus-data-find
2165                        ,(or number '(gnus-summary-article-number))))))
2166
2167 (defmacro gnus-summary-article-mark (&optional number)
2168   "Return the mark of article NUMBER."
2169   `(gnus-data-mark (gnus-data-find
2170                     ,(or number '(gnus-summary-article-number)))))
2171
2172 (defmacro gnus-summary-article-pos (&optional number)
2173   "Return the position of the line of article NUMBER."
2174   `(gnus-data-pos (gnus-data-find
2175                    ,(or number '(gnus-summary-article-number)))))
2176
2177 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2178 (defmacro gnus-summary-article-subject (&optional number)
2179   "Return current subject string or nil if nothing."
2180   `(let ((headers
2181           ,(if number
2182                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2183              '(gnus-data-header (assq (gnus-summary-article-number)
2184                                       gnus-newsgroup-data)))))
2185      (and headers
2186           (vectorp headers)
2187           (mail-header-subject headers))))
2188
2189 (defmacro gnus-summary-article-score (&optional number)
2190   "Return current article score."
2191   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2192                   gnus-newsgroup-scored))
2193        gnus-summary-default-score 0))
2194
2195 (defun gnus-summary-article-children (&optional number)
2196   "Return a list of article numbers that are children of article NUMBER."
2197   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2198          (level (gnus-data-level (car data)))
2199          l children)
2200     (while (and (setq data (cdr data))
2201                 (> (setq l (gnus-data-level (car data))) level))
2202       (and (= (1+ level) l)
2203            (push (gnus-data-number (car data))
2204                  children)))
2205     (nreverse children)))
2206
2207 (defun gnus-summary-article-parent (&optional number)
2208   "Return the article number of the parent of article NUMBER."
2209   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2210                                     (gnus-data-list t)))
2211          (level (gnus-data-level (car data))))
2212     (if (zerop level)
2213         ()                              ; This is a root.
2214       ;; We search until we find an article with a level less than
2215       ;; this one.  That function has to be the parent.
2216       (while (and (setq data (cdr data))
2217                   (not (< (gnus-data-level (car data)) level))))
2218       (and data (gnus-data-number (car data))))))
2219
2220 (defun gnus-unread-mark-p (mark)
2221   "Say whether MARK is the unread mark."
2222   (= mark gnus-unread-mark))
2223
2224 (defun gnus-read-mark-p (mark)
2225   "Say whether MARK is one of the marks that mark as read.
2226 This is all marks except unread, ticked, dormant, and expirable."
2227   (not (or (= mark gnus-unread-mark)
2228            (= mark gnus-ticked-mark)
2229            (= mark gnus-dormant-mark)
2230            (= mark gnus-expirable-mark))))
2231
2232 (defmacro gnus-article-mark (number)
2233   "Return the MARK of article NUMBER.
2234 This macro should only be used when computing the mark the \"first\"
2235 time; i.e., when generating the summary lines.  After that,
2236 `gnus-summary-article-mark' should be used to examine the
2237 marks of articles."
2238   `(cond
2239     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2240     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2241     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2242     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2243     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2244     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2245     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2246     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2247            gnus-ancient-mark))))
2248
2249 ;; Saving hidden threads.
2250
2251 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2252 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2253
2254 (defmacro gnus-save-hidden-threads (&rest forms)
2255   "Save hidden threads, eval FORMS, and restore the hidden threads."
2256   (let ((config (make-symbol "config")))
2257     `(let ((,config (gnus-hidden-threads-configuration)))
2258        (unwind-protect
2259            (save-excursion
2260              ,@forms)
2261          (gnus-restore-hidden-threads-configuration ,config)))))
2262
2263 (defun gnus-data-compute-positions ()
2264   "Compute the positions of all articles."
2265   (setq gnus-newsgroup-data-reverse nil)
2266   (let ((data gnus-newsgroup-data))
2267     (save-excursion
2268       (gnus-save-hidden-threads
2269         (gnus-summary-show-all-threads)
2270         (goto-char (point-min))
2271         (while data
2272           (while (get-text-property (point) 'gnus-intangible)
2273             (forward-line 1))
2274           (gnus-data-set-pos (car data) (+ (point) 3))
2275           (setq data (cdr data))
2276           (forward-line 1))))))
2277
2278 (defun gnus-hidden-threads-configuration ()
2279   "Return the current hidden threads configuration."
2280   (save-excursion
2281     (let (config)
2282       (goto-char (point-min))
2283       (while (search-forward "\r" nil t)
2284         (push (1- (point)) config))
2285       config)))
2286
2287 (defun gnus-restore-hidden-threads-configuration (config)
2288   "Restore hidden threads configuration from CONFIG."
2289   (let (point buffer-read-only)
2290     (while (setq point (pop config))
2291       (when (and (< point (point-max))
2292                  (goto-char point)
2293                  (eq (char-after) ?\n))
2294         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2295
2296 ;; Various summary mode internalish functions.
2297
2298 (defun gnus-mouse-pick-article (e)
2299   (interactive "e")
2300   (mouse-set-point e)
2301   (gnus-summary-next-page nil t))
2302
2303 (defun gnus-summary-set-display-table ()
2304   ;; Change the display table.  Odd characters have a tendency to mess
2305   ;; up nicely formatted displays - we make all possible glyphs
2306   ;; display only a single character.
2307
2308   ;; We start from the standard display table, if any.
2309   (let ((table (or (copy-sequence standard-display-table)
2310                    (make-display-table)))
2311         (i 32))
2312     ;; Nix out all the control chars...
2313     (while (>= (setq i (1- i)) 0)
2314       (aset table i [??]))
2315     ;; ... but not newline and cr, of course.  (cr is necessary for the
2316     ;; selective display).
2317     (aset table ?\n nil)
2318     (aset table ?\r nil)
2319     ;; We keep TAB as well.
2320     (aset table ?\t nil)
2321     ;; We nix out any glyphs over 126 that are not set already.
2322     (let ((i 256))
2323       (while (>= (setq i (1- i)) 127)
2324         ;; Only modify if the entry is nil.
2325         (unless (aref table i)
2326           (aset table i [??]))))
2327     (setq buffer-display-table table)))
2328
2329 (defun gnus-summary-setup-buffer (group)
2330   "Initialize summary buffer."
2331   (let ((buffer (concat "*Summary " group "*")))
2332     (if (get-buffer buffer)
2333         (progn
2334           (set-buffer buffer)
2335           (setq gnus-summary-buffer (current-buffer))
2336           (not gnus-newsgroup-prepared))
2337       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2338       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2339       (gnus-summary-mode group)
2340       (when gnus-carpal
2341         (gnus-carpal-setup-buffer 'summary))
2342       (unless gnus-single-article-buffer
2343         (make-local-variable 'gnus-article-buffer)
2344         (make-local-variable 'gnus-article-current)
2345         (make-local-variable 'gnus-original-article-buffer))
2346       (setq gnus-newsgroup-name group)
2347       t)))
2348
2349 (defun gnus-set-global-variables ()
2350   ;; Set the global equivalents of the summary buffer-local variables
2351   ;; to the latest values they had.  These reflect the summary buffer
2352   ;; that was in action when the last article was fetched.
2353   (when (eq major-mode 'gnus-summary-mode)
2354     (setq gnus-summary-buffer (current-buffer))
2355     (let ((name gnus-newsgroup-name)
2356           (marked gnus-newsgroup-marked)
2357           (unread gnus-newsgroup-unreads)
2358           (headers gnus-current-headers)
2359           (data gnus-newsgroup-data)
2360           (summary gnus-summary-buffer)
2361           (article-buffer gnus-article-buffer)
2362           (original gnus-original-article-buffer)
2363           (gac gnus-article-current)
2364           (reffed gnus-reffed-article-number)
2365           (score-file gnus-current-score-file)
2366           (default-charset gnus-newsgroup-charset))
2367       (save-excursion
2368         (set-buffer gnus-group-buffer)
2369         (setq gnus-newsgroup-name name
2370               gnus-newsgroup-marked marked
2371               gnus-newsgroup-unreads unread
2372               gnus-current-headers headers
2373               gnus-newsgroup-data data
2374               gnus-article-current gac
2375               gnus-summary-buffer summary
2376               gnus-article-buffer article-buffer
2377               gnus-original-article-buffer original
2378               gnus-reffed-article-number reffed
2379               gnus-current-score-file score-file
2380               gnus-newsgroup-charset default-charset)
2381         ;; The article buffer also has local variables.
2382         (when (gnus-buffer-live-p gnus-article-buffer)
2383           (set-buffer gnus-article-buffer)
2384           (setq gnus-summary-buffer summary))))))
2385
2386 (defun gnus-summary-article-unread-p (article)
2387   "Say whether ARTICLE is unread or not."
2388   (memq article gnus-newsgroup-unreads))
2389
2390 (defun gnus-summary-first-article-p (&optional article)
2391   "Return whether ARTICLE is the first article in the buffer."
2392   (if (not (setq article (or article (gnus-summary-article-number))))
2393       nil
2394     (eq article (caar gnus-newsgroup-data))))
2395
2396 (defun gnus-summary-last-article-p (&optional article)
2397   "Return whether ARTICLE is the last article in the buffer."
2398   (if (not (setq article (or article (gnus-summary-article-number))))
2399       ;; All non-existent numbers are the last article.  :-)
2400       t
2401     (not (cdr (gnus-data-find-list article)))))
2402
2403 (defun gnus-make-thread-indent-array ()
2404   (let ((n 200))
2405     (unless (and gnus-thread-indent-array
2406                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2407       (setq gnus-thread-indent-array (make-vector 201 "")
2408             gnus-thread-indent-array-level gnus-thread-indent-level)
2409       (while (>= n 0)
2410         (aset gnus-thread-indent-array n
2411               (make-string (* n gnus-thread-indent-level) ? ))
2412         (setq n (1- n))))))
2413
2414 (defun gnus-update-summary-mark-positions ()
2415   "Compute where the summary marks are to go."
2416   (save-excursion
2417     (when (gnus-buffer-exists-p gnus-summary-buffer)
2418       (set-buffer gnus-summary-buffer))
2419     (let ((gnus-replied-mark 129)
2420           (gnus-score-below-mark 130)
2421           (gnus-score-over-mark 130)
2422           (gnus-download-mark 131)
2423           (spec gnus-summary-line-format-spec)
2424           gnus-visual pos)
2425       (save-excursion
2426         (gnus-set-work-buffer)
2427         (let ((gnus-summary-line-format-spec spec)
2428               (gnus-newsgroup-downloadable '((0 . t))))
2429           (gnus-summary-insert-line
2430            (make-full-mail-header 0 "" "" "" "" "" 0 0 "" nil)
2431            0 nil 128 t nil "" nil 1)
2432           (goto-char (point-min))
2433           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2434                                              (- (point) 2)))))
2435           (goto-char (point-min))
2436           (push (cons 'replied (and (search-forward "\201" nil t)
2437                                     (- (point) 2)))
2438                 pos)
2439           (goto-char (point-min))
2440           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2441                 pos)
2442           (goto-char (point-min))
2443           (push (cons 'download
2444                       (and (search-forward "\203" nil t) (- (point) 2)))
2445                 pos)))
2446       (setq gnus-summary-mark-positions pos))))
2447
2448 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2449   "Insert a dummy root in the summary buffer."
2450   (beginning-of-line)
2451   (gnus-add-text-properties
2452    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2453    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2454
2455 (defun gnus-summary-from-or-to-or-newsgroups (header)
2456   (let ((to (cdr (assq 'To (mail-header-extra header))))
2457         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2458         (mail-parse-charset gnus-newsgroup-charset))
2459     (cond
2460      ((and to
2461            gnus-ignored-from-addresses
2462            (string-match gnus-ignored-from-addresses
2463                          (mail-header-from header)))
2464       (concat "-> "
2465               (or (car (funcall gnus-extract-address-components
2466                                 (funcall
2467                                  gnus-decode-encoded-word-function to)))
2468                   (funcall gnus-decode-encoded-word-function to))))
2469      ((and newsgroups
2470            gnus-ignored-from-addresses
2471            (string-match gnus-ignored-from-addresses
2472                          (mail-header-from header)))
2473       (concat "=> " newsgroups))
2474      (t
2475       (or (car (funcall gnus-extract-address-components
2476                         (mail-header-from header)))
2477           (mail-header-from header))))))
2478
2479 (defun gnus-summary-insert-line (gnus-tmp-header
2480                                  gnus-tmp-level gnus-tmp-current
2481                                  gnus-tmp-unread gnus-tmp-replied
2482                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2483                                  &optional gnus-tmp-dummy gnus-tmp-score
2484                                  gnus-tmp-process)
2485   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2486          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2487          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2488          (gnus-tmp-score-char
2489           (if (or (null gnus-summary-default-score)
2490                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2491                       gnus-summary-zcore-fuzz))
2492               ? ;Whitespace
2493             (if (< gnus-tmp-score gnus-summary-default-score)
2494                 gnus-score-below-mark gnus-score-over-mark)))
2495          (gnus-tmp-replied
2496           (cond (gnus-tmp-process gnus-process-mark)
2497                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2498                  gnus-cached-mark)
2499                 (gnus-tmp-replied gnus-replied-mark)
2500                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2501                  gnus-saved-mark)
2502                 (t gnus-unread-mark)))
2503          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2504          (gnus-tmp-name
2505           (cond
2506            ((string-match "<[^>]+> *$" gnus-tmp-from)
2507             (let ((beg (match-beginning 0)))
2508               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2509                        (substring gnus-tmp-from (1+ (match-beginning 0))
2510                                   (1- (match-end 0))))
2511                   (substring gnus-tmp-from 0 beg))))
2512            ((string-match "(.+)" gnus-tmp-from)
2513             (substring gnus-tmp-from
2514                        (1+ (match-beginning 0)) (1- (match-end 0))))
2515            (t gnus-tmp-from)))
2516          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2517          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2518          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2519          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2520          (buffer-read-only nil))
2521     (when (string= gnus-tmp-name "")
2522       (setq gnus-tmp-name gnus-tmp-from))
2523     (unless (numberp gnus-tmp-lines)
2524       (setq gnus-tmp-lines 0))
2525     (gnus-put-text-property-excluding-characters-with-faces
2526      (point)
2527      (progn (eval gnus-summary-line-format-spec) (point))
2528      'gnus-number gnus-tmp-number)
2529     (when (gnus-visual-p 'summary-highlight 'highlight)
2530       (forward-line -1)
2531       (gnus-run-hooks 'gnus-summary-update-hook)
2532       (forward-line 1))))
2533
2534 (defun gnus-summary-update-line (&optional dont-update)
2535   ;; Update summary line after change.
2536   (when (and gnus-summary-default-score
2537              (not gnus-summary-inhibit-highlight))
2538     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2539            (article (gnus-summary-article-number))
2540            (score (gnus-summary-article-score article)))
2541       (unless dont-update
2542         (if (and gnus-summary-mark-below
2543                  (< (gnus-summary-article-score)
2544                     gnus-summary-mark-below))
2545             ;; This article has a low score, so we mark it as read.
2546             (when (memq article gnus-newsgroup-unreads)
2547               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2548           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2549             ;; This article was previously marked as read on account
2550             ;; of a low score, but now it has risen, so we mark it as
2551             ;; unread.
2552             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2553         (gnus-summary-update-mark
2554          (if (or (null gnus-summary-default-score)
2555                  (<= (abs (- score gnus-summary-default-score))
2556                      gnus-summary-zcore-fuzz))
2557              ? ;Whitespace
2558            (if (< score gnus-summary-default-score)
2559                gnus-score-below-mark gnus-score-over-mark))
2560          'score))
2561       ;; Do visual highlighting.
2562       (when (gnus-visual-p 'summary-highlight 'highlight)
2563         (gnus-run-hooks 'gnus-summary-update-hook)))))
2564
2565 (defvar gnus-tmp-new-adopts nil)
2566
2567 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2568   "Return the number of articles in THREAD.
2569 This may be 0 in some cases -- if none of the articles in
2570 the thread are to be displayed."
2571   (let* ((number
2572           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2573           (cond
2574            ((not (listp thread))
2575             1)
2576            ((and (consp thread) (cdr thread))
2577             (apply
2578              '+ 1 (mapcar
2579                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2580            ((null thread)
2581             1)
2582            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2583             1)
2584            (t 0))))
2585     (when (and level (zerop level) gnus-tmp-new-adopts)
2586       (incf number
2587             (apply '+ (mapcar
2588                        'gnus-summary-number-of-articles-in-thread
2589                        gnus-tmp-new-adopts))))
2590     (if char
2591         (if (> number 1) gnus-not-empty-thread-mark
2592           gnus-empty-thread-mark)
2593       number)))
2594
2595 (defun gnus-summary-set-local-parameters (group)
2596   "Go through the local params of GROUP and set all variable specs in that list."
2597   (let ((params (gnus-group-find-parameter group))
2598         elem)
2599     (while params
2600       (setq elem (car params)
2601             params (cdr params))
2602       (and (consp elem)                 ; Has to be a cons.
2603            (consp (cdr elem))           ; The cdr has to be a list.
2604            (symbolp (car elem))         ; Has to be a symbol in there.
2605            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2606            (ignore-errors               ; So we set it.
2607              (make-local-variable (car elem))
2608              (set (car elem) (eval (nth 1 elem))))))))
2609
2610 (defun gnus-summary-read-group (group &optional show-all no-article
2611                                       kill-buffer no-display backward
2612                                       select-articles)
2613   "Start reading news in newsgroup GROUP.
2614 If SHOW-ALL is non-nil, already read articles are also listed.
2615 If NO-ARTICLE is non-nil, no article is selected initially.
2616 If NO-DISPLAY, don't generate a summary buffer."
2617   (let (result)
2618     (while (and group
2619                 (null (setq result
2620                             (let ((gnus-auto-select-next nil))
2621                               (or (gnus-summary-read-group-1
2622                                    group show-all no-article
2623                                    kill-buffer no-display
2624                                    select-articles)
2625                                   (setq show-all nil
2626                                         select-articles nil)))))
2627                 (eq gnus-auto-select-next 'quietly))
2628       (set-buffer gnus-group-buffer)
2629       ;; The entry function called above goes to the next
2630       ;; group automatically, so we go two groups back
2631       ;; if we are searching for the previous group.
2632       (when backward
2633         (gnus-group-prev-unread-group 2))
2634       (if (not (equal group (gnus-group-group-name)))
2635           (setq group (gnus-group-group-name))
2636         (setq group nil)))
2637     result))
2638
2639 (defun gnus-summary-read-group-1 (group show-all no-article
2640                                         kill-buffer no-display
2641                                         &optional select-articles)
2642   ;; Killed foreign groups can't be entered.
2643   (when (and (not (gnus-group-native-p group))
2644              (not (gnus-gethash group gnus-newsrc-hashtb)))
2645     (error "Dead non-native groups can't be entered"))
2646   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2647   (let* ((new-group (gnus-summary-setup-buffer group))
2648          (quit-config (gnus-group-quit-config group))
2649          (did-select (and new-group (gnus-select-newsgroup
2650                                      group show-all select-articles))))
2651     (cond
2652      ;; This summary buffer exists already, so we just select it.
2653      ((not new-group)
2654       (gnus-set-global-variables)
2655       (when kill-buffer
2656         (gnus-kill-or-deaden-summary kill-buffer))
2657       (gnus-configure-windows 'summary 'force)
2658       (gnus-set-mode-line 'summary)
2659       (gnus-summary-position-point)
2660       (message "")
2661       t)
2662      ;; We couldn't select this group.
2663      ((null did-select)
2664       (when (and (eq major-mode 'gnus-summary-mode)
2665                  (not (equal (current-buffer) kill-buffer)))
2666         (kill-buffer (current-buffer))
2667         (if (not quit-config)
2668             (progn
2669               ;; Update the info -- marks might need to be removed,
2670               ;; for instance.
2671               (gnus-summary-update-info)
2672               (set-buffer gnus-group-buffer)
2673               (gnus-group-jump-to-group group)
2674               (gnus-group-next-unread-group 1))
2675           (gnus-handle-ephemeral-exit quit-config)))
2676       (gnus-message 3 "Can't select group")
2677       nil)
2678      ;; The user did a `C-g' while prompting for number of articles,
2679      ;; so we exit this group.
2680      ((eq did-select 'quit)
2681       (and (eq major-mode 'gnus-summary-mode)
2682            (not (equal (current-buffer) kill-buffer))
2683            (kill-buffer (current-buffer)))
2684       (when kill-buffer
2685         (gnus-kill-or-deaden-summary kill-buffer))
2686       (if (not quit-config)
2687           (progn
2688             (set-buffer gnus-group-buffer)
2689             (gnus-group-jump-to-group group)
2690             (gnus-group-next-unread-group 1)
2691             (gnus-configure-windows 'group 'force))
2692         (gnus-handle-ephemeral-exit quit-config))
2693       ;; Finally signal the quit.
2694       (signal 'quit nil))
2695      ;; The group was successfully selected.
2696      (t
2697       (gnus-set-global-variables)
2698       ;; Save the active value in effect when the group was entered.
2699       (setq gnus-newsgroup-active
2700             (gnus-copy-sequence
2701              (gnus-active gnus-newsgroup-name)))
2702       ;; You can change the summary buffer in some way with this hook.
2703       (gnus-run-hooks 'gnus-select-group-hook)
2704       ;; Set any local variables in the group parameters.
2705       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2706       (gnus-update-format-specifications
2707        nil 'summary 'summary-mode 'summary-dummy)
2708       ;; Do score processing.
2709       (when gnus-use-scoring
2710         (gnus-possibly-score-headers))
2711       ;; Check whether to fill in the gaps in the threads.
2712       (when gnus-build-sparse-threads
2713         (gnus-build-sparse-threads))
2714       ;; Find the initial limit.
2715       (if gnus-show-threads
2716           (if show-all
2717               (let ((gnus-newsgroup-dormant nil))
2718                 (gnus-summary-initial-limit show-all))
2719             (gnus-summary-initial-limit show-all))
2720         (setq gnus-newsgroup-limit
2721               (mapcar
2722                (lambda (header) (mail-header-number header))
2723                gnus-newsgroup-headers)))
2724       ;; Generate the summary buffer.
2725       (unless no-display
2726         (gnus-summary-prepare))
2727       (when gnus-use-trees
2728         (gnus-tree-open group)
2729         (setq gnus-summary-highlight-line-function
2730               'gnus-tree-highlight-article))
2731       ;; If the summary buffer is empty, but there are some low-scored
2732       ;; articles or some excluded dormants, we include these in the
2733       ;; buffer.
2734       (when (and (zerop (buffer-size))
2735                  (not no-display))
2736         (cond (gnus-newsgroup-dormant
2737                (gnus-summary-limit-include-dormant))
2738               ((and gnus-newsgroup-scored show-all)
2739                (gnus-summary-limit-include-expunged t))))
2740       ;; Function `gnus-apply-kill-file' must be called in this hook.
2741       (gnus-run-hooks 'gnus-apply-kill-hook)
2742       (if (and (zerop (buffer-size))
2743                (not no-display))
2744           (progn
2745             ;; This newsgroup is empty.
2746             (gnus-summary-catchup-and-exit nil t)
2747             (gnus-message 6 "No unread news")
2748             (when kill-buffer
2749               (gnus-kill-or-deaden-summary kill-buffer))
2750             ;; Return nil from this function.
2751             nil)
2752         ;; Hide conversation thread subtrees.  We cannot do this in
2753         ;; gnus-summary-prepare-hook since kill processing may not
2754         ;; work with hidden articles.
2755         (and gnus-show-threads
2756              gnus-thread-hide-subtree
2757              (gnus-summary-hide-all-threads))
2758         (when kill-buffer
2759           (gnus-kill-or-deaden-summary kill-buffer))
2760         ;; Show first unread article if requested.
2761         (if (and (not no-article)
2762                  (not no-display)
2763                  gnus-newsgroup-unreads
2764                  gnus-auto-select-first)
2765             (progn
2766               (gnus-configure-windows 'summary)
2767               (cond
2768                ((eq gnus-auto-select-first 'best)
2769                 (gnus-summary-best-unread-article))
2770                ((eq gnus-auto-select-first t)
2771                 (gnus-summary-first-unread-article))
2772                ((gnus-functionp gnus-auto-select-first)
2773                 (funcall gnus-auto-select-first))))
2774           ;; Don't select any articles, just move point to the first
2775           ;; article in the group.
2776           (goto-char (point-min))
2777           (gnus-summary-position-point)
2778           (gnus-configure-windows 'summary 'force)
2779           (gnus-set-mode-line 'summary))
2780         (when (get-buffer-window gnus-group-buffer t)
2781           ;; Gotta use windows, because recenter does weird stuff if
2782           ;; the current buffer ain't the displayed window.
2783           (let ((owin (selected-window)))
2784             (select-window (get-buffer-window gnus-group-buffer t))
2785             (when (gnus-group-goto-group group)
2786               (recenter))
2787             (select-window owin)))
2788         ;; Mark this buffer as "prepared".
2789         (setq gnus-newsgroup-prepared t)
2790         (gnus-run-hooks 'gnus-summary-prepared-hook)
2791         t)))))
2792
2793 (defun gnus-summary-prepare ()
2794   "Generate the summary buffer."
2795   (interactive)
2796   (let ((buffer-read-only nil))
2797     (erase-buffer)
2798     (setq gnus-newsgroup-data nil
2799           gnus-newsgroup-data-reverse nil)
2800     (gnus-run-hooks 'gnus-summary-generate-hook)
2801     ;; Generate the buffer, either with threads or without.
2802     (when gnus-newsgroup-headers
2803       (gnus-summary-prepare-threads
2804        (if gnus-show-threads
2805            (gnus-sort-gathered-threads
2806             (funcall gnus-summary-thread-gathering-function
2807                      (gnus-sort-threads
2808                       (gnus-cut-threads (gnus-make-threads)))))
2809          ;; Unthreaded display.
2810          (gnus-sort-articles gnus-newsgroup-headers))))
2811     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2812     ;; Call hooks for modifying summary buffer.
2813     (goto-char (point-min))
2814     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2815
2816 (defsubst gnus-general-simplify-subject (subject)
2817   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2818   (setq subject
2819         (cond
2820          ;; Truncate the subject.
2821          (gnus-simplify-subject-functions
2822           (gnus-map-function gnus-simplify-subject-functions subject))
2823          ((numberp gnus-summary-gather-subject-limit)
2824           (setq subject (gnus-simplify-subject-re subject))
2825           (if (> (length subject) gnus-summary-gather-subject-limit)
2826               (substring subject 0 gnus-summary-gather-subject-limit)
2827             subject))
2828          ;; Fuzzily simplify it.
2829          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2830           (gnus-simplify-subject-fuzzy subject))
2831          ;; Just remove the leading "Re:".
2832          (t
2833           (gnus-simplify-subject-re subject))))
2834
2835   (if (and gnus-summary-gather-exclude-subject
2836            (string-match gnus-summary-gather-exclude-subject subject))
2837       nil                               ; This article shouldn't be gathered
2838     subject))
2839
2840 (defun gnus-summary-simplify-subject-query ()
2841   "Query where the respool algorithm would put this article."
2842   (interactive)
2843   (gnus-summary-select-article)
2844   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2845
2846 (defun gnus-gather-threads-by-subject (threads)
2847   "Gather threads by looking at Subject headers."
2848   (if (not gnus-summary-make-false-root)
2849       threads
2850     (let ((hashtb (gnus-make-hashtable 1024))
2851           (prev threads)
2852           (result threads)
2853           subject hthread whole-subject)
2854       (while threads
2855         (setq subject (gnus-general-simplify-subject
2856                        (setq whole-subject (mail-header-subject
2857                                             (caar threads)))))
2858         (when subject
2859           (if (setq hthread (gnus-gethash subject hashtb))
2860               (progn
2861                 ;; We enter a dummy root into the thread, if we
2862                 ;; haven't done that already.
2863                 (unless (stringp (caar hthread))
2864                   (setcar hthread (list whole-subject (car hthread))))
2865                 ;; We add this new gathered thread to this gathered
2866                 ;; thread.
2867                 (setcdr (car hthread)
2868                         (nconc (cdar hthread) (list (car threads))))
2869                 ;; Remove it from the list of threads.
2870                 (setcdr prev (cdr threads))
2871                 (setq threads prev))
2872             ;; Enter this thread into the hash table.
2873             (gnus-sethash subject threads hashtb)))
2874         (setq prev threads)
2875         (setq threads (cdr threads)))
2876       result)))
2877
2878 (defun gnus-gather-threads-by-references (threads)
2879   "Gather threads by looking at References headers."
2880   (let ((idhashtb (gnus-make-hashtable 1024))
2881         (thhashtb (gnus-make-hashtable 1024))
2882         (prev threads)
2883         (result threads)
2884         ids references id gthread gid entered ref)
2885     (while threads
2886       (when (setq references (mail-header-references (caar threads)))
2887         (setq id (mail-header-id (caar threads))
2888               ids (gnus-split-references references)
2889               entered nil)
2890         (while (setq ref (pop ids))
2891           (setq ids (delete ref ids))
2892           (if (not (setq gid (gnus-gethash ref idhashtb)))
2893               (progn
2894                 (gnus-sethash ref id idhashtb)
2895                 (gnus-sethash id threads thhashtb))
2896             (setq gthread (gnus-gethash gid thhashtb))
2897             (unless entered
2898               ;; We enter a dummy root into the thread, if we
2899               ;; haven't done that already.
2900               (unless (stringp (caar gthread))
2901                 (setcar gthread (list (mail-header-subject (caar gthread))
2902                                       (car gthread))))
2903               ;; We add this new gathered thread to this gathered
2904               ;; thread.
2905               (setcdr (car gthread)
2906                       (nconc (cdar gthread) (list (car threads)))))
2907             ;; Add it into the thread hash table.
2908             (gnus-sethash id gthread thhashtb)
2909             (setq entered t)
2910             ;; Remove it from the list of threads.
2911             (setcdr prev (cdr threads))
2912             (setq threads prev))))
2913       (setq prev threads)
2914       (setq threads (cdr threads)))
2915     result))
2916
2917 (defun gnus-sort-gathered-threads (threads)
2918   "Sort subtreads inside each gathered thread by article number."
2919   (let ((result threads))
2920     (while threads
2921       (when (stringp (caar threads))
2922         (setcdr (car threads)
2923                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2924       (setq threads (cdr threads)))
2925     result))
2926
2927 (defun gnus-thread-loop-p (root thread)
2928   "Say whether ROOT is in THREAD."
2929   (let ((stack (list thread))
2930         (infloop 0)
2931         th)
2932     (while (setq thread (pop stack))
2933       (setq th (cdr thread))
2934       (while (and th
2935                   (not (eq (caar th) root)))
2936         (pop th))
2937       (if th
2938           ;; We have found a loop.
2939           (let (ref-dep)
2940             (setcdr thread (delq (car th) (cdr thread)))
2941             (if (boundp (setq ref-dep (intern "none"
2942                                               gnus-newsgroup-dependencies)))
2943                 (setcdr (symbol-value ref-dep)
2944                         (nconc (cdr (symbol-value ref-dep))
2945                                (list (car th))))
2946               (set ref-dep (list nil (car th))))
2947             (setq infloop 1
2948                   stack nil))
2949         ;; Push all the subthreads onto the stack.
2950         (push (cdr thread) stack)))
2951     infloop))
2952
2953 (defun gnus-make-threads ()
2954   "Go through the dependency hashtb and find the roots.  Return all threads."
2955   (let (threads)
2956     (while (catch 'infloop
2957              (mapatoms
2958               (lambda (refs)
2959                 ;; Deal with self-referencing References loops.
2960                 (when (and (car (symbol-value refs))
2961                            (not (zerop
2962                                  (apply
2963                                   '+
2964                                   (mapcar
2965                                    (lambda (thread)
2966                                      (gnus-thread-loop-p
2967                                       (car (symbol-value refs)) thread))
2968                                    (cdr (symbol-value refs)))))))
2969                   (setq threads nil)
2970                   (throw 'infloop t))
2971                 (unless (car (symbol-value refs))
2972                   ;; These threads do not refer back to any other articles,
2973                   ;; so they're roots.
2974                   (setq threads (append (cdr (symbol-value refs)) threads))))
2975               gnus-newsgroup-dependencies)))
2976     threads))
2977
2978 ;; Build the thread tree.
2979 (defsubst gnus-dependencies-add-header (header dependencies force-new)
2980   "Enter HEADER into the DEPENDENCIES table if it is not already there.
2981
2982 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
2983 if it was already present.
2984
2985 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
2986 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
2987 Message-IDs will be renamed be renamed to a unique Message-ID before
2988 being entered.
2989
2990 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
2991   (let* ((id (mail-header-id header))
2992          (id-dep (and id (intern id dependencies)))
2993          ref ref-dep ref-header)
2994     ;; Enter this `header' in the `dependencies' table.
2995     (cond
2996      ((not id-dep)
2997       (setq header nil))
2998      ;; The first two cases do the normal part: enter a new `header'
2999      ;; in the `dependencies' table.
3000      ((not (boundp id-dep))
3001       (set id-dep (list header)))
3002      ((null (car (symbol-value id-dep)))
3003       (setcar (symbol-value id-dep) header))
3004
3005      ;; From here the `header' was already present in the
3006      ;; `dependencies' table.
3007      (force-new
3008       ;; Overrides an existing entry;
3009       ;; just set the header part of the entry.
3010       (setcar (symbol-value id-dep) header))
3011
3012      ;; Renames the existing `header' to a unique Message-ID.
3013      ((not gnus-summary-ignore-duplicates)
3014       ;; An article with this Message-ID has already been seen.
3015       ;; We rename the Message-ID.
3016       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3017            (list header))
3018       (mail-header-set-id header id))
3019
3020      ;; The last case ignores an existing entry, except it adds any
3021      ;; additional Xrefs (in case the two articles came from different
3022      ;; servers.
3023      ;; Also sets `header' to `nil' meaning that the `dependencies'
3024      ;; table was *not* modified.
3025      (t
3026       (mail-header-set-xref
3027        (car (symbol-value id-dep))
3028        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3029                    "")
3030                (or (mail-header-xref header) "")))
3031       (setq header nil)))
3032
3033     (when header
3034       ;; First check if that we are not creating a References loop.
3035       (setq ref (gnus-parent-id (mail-header-references header)))
3036       (while (and ref
3037                   (setq ref-dep (intern-soft ref dependencies))
3038                   (boundp ref-dep)
3039                   (setq ref-header (car (symbol-value ref-dep))))
3040         (if (string= id ref)
3041             ;; Yuk!  This is a reference loop.  Make the article be a
3042             ;; root article.
3043             (progn
3044               (mail-header-set-references (car (symbol-value id-dep)) "none")
3045               (setq ref nil))
3046           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3047       (setq ref (gnus-parent-id (mail-header-references header)))
3048       (setq ref-dep (intern (or ref "none") dependencies))
3049       (if (boundp ref-dep)
3050           (setcdr (symbol-value ref-dep)
3051                   (nconc (cdr (symbol-value ref-dep))
3052                          (list (symbol-value id-dep))))
3053         (set ref-dep (list nil (symbol-value id-dep)))))
3054     header))
3055
3056 (defun gnus-build-sparse-threads ()
3057   (let ((headers gnus-newsgroup-headers)
3058         (gnus-summary-ignore-duplicates t)
3059         header references generation relations
3060         subject child end new-child date)
3061     ;; First we create an alist of generations/relations, where
3062     ;; generations is how much we trust the relation, and the relation
3063     ;; is parent/child.
3064     (gnus-message 7 "Making sparse threads...")
3065     (save-excursion
3066       (nnheader-set-temp-buffer " *gnus sparse threads*")
3067       (while (setq header (pop headers))
3068         (when (and (setq references (mail-header-references header))
3069                    (not (string= references "")))
3070           (insert references)
3071           (setq child (mail-header-id header)
3072                 subject (mail-header-subject header)
3073                 date (mail-header-date header)
3074                 generation 0)
3075           (while (search-backward ">" nil t)
3076             (setq end (1+ (point)))
3077             (when (search-backward "<" nil t)
3078               (setq new-child (buffer-substring (point) end))
3079               (push (list (incf generation)
3080                           child (setq child new-child)
3081                           subject date)
3082                     relations)))
3083           (when child
3084             (push (list (1+ generation) child nil subject) relations))
3085           (erase-buffer)))
3086       (kill-buffer (current-buffer)))
3087     ;; Sort over trustworthiness.
3088     (mapcar
3089      (lambda (relation)
3090        (when (gnus-dependencies-add-header
3091               (make-full-mail-header
3092                gnus-reffed-article-number
3093                (nth 3 relation) "" (or (nth 4 relation) "")
3094                (nth 1 relation)
3095                (or (nth 2 relation) "") 0 0 "")
3096               gnus-newsgroup-dependencies nil)
3097          (push gnus-reffed-article-number gnus-newsgroup-limit)
3098          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3099          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3100                gnus-newsgroup-reads)
3101          (decf gnus-reffed-article-number)))
3102      (sort relations 'car-less-than-car))
3103     (gnus-message 7 "Making sparse threads...done")))
3104
3105 (defun gnus-build-old-threads ()
3106   ;; Look at all the articles that refer back to old articles, and
3107   ;; fetch the headers for the articles that aren't there.  This will
3108   ;; build complete threads - if the roots haven't been expired by the
3109   ;; server, that is.
3110   (let (id heads)
3111     (mapatoms
3112      (lambda (refs)
3113        (when (not (car (symbol-value refs)))
3114          (setq heads (cdr (symbol-value refs)))
3115          (while heads
3116            (if (memq (mail-header-number (caar heads))
3117                      gnus-newsgroup-dormant)
3118                (setq heads (cdr heads))
3119              (setq id (symbol-name refs))
3120              (while (and (setq id (gnus-build-get-header id))
3121                          (not (car (gnus-id-to-thread id)))))
3122              (setq heads nil)))))
3123      gnus-newsgroup-dependencies)))
3124
3125 ;; This function has to be called with point after the article number
3126 ;; on the beginning of the line.
3127 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3128   (let ((eol (gnus-point-at-eol))
3129         (buffer (current-buffer))
3130         header)
3131
3132     ;; overview: [num subject from date id refs chars lines misc]
3133     (unless (eobp)
3134       (forward-char))
3135
3136     (setq header
3137           (make-full-mail-header
3138            number                               ; number
3139            (nnheader-nov-field)                 ; subject
3140            (nnheader-nov-field)                 ; from
3141            (nnheader-nov-field)                 ; date
3142            (or (nnheader-nov-field)             ; id
3143                (nnheader-generate-fake-message-id))
3144            (nnheader-nov-field)                 ; refs
3145            (nnheader-nov-read-integer)          ; chars
3146            (nnheader-nov-read-integer)          ; lines
3147            (unless (eq (char-after) ?\n)
3148              (nnheader-nov-field))              ; misc
3149            (nnheader-nov-parse-extra)))         ; extra
3150
3151     (when gnus-alter-header-function
3152       (funcall gnus-alter-header-function header))
3153     (gnus-dependencies-add-header header dependencies force-new)))
3154
3155 (defun gnus-build-get-header (id)
3156   ;; Look through the buffer of NOV lines and find the header to
3157   ;; ID.  Enter this line into the dependencies hash table, and return
3158   ;; the id of the parent article (if any).
3159   (let ((deps gnus-newsgroup-dependencies)
3160         found header)
3161     (prog1
3162         (save-excursion
3163           (set-buffer nntp-server-buffer)
3164           (let ((case-fold-search nil))
3165             (goto-char (point-min))
3166             (while (and (not found)
3167                         (search-forward id nil t))
3168               (beginning-of-line)
3169               (setq found (looking-at
3170                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3171                                    (regexp-quote id))))
3172               (or found (beginning-of-line 2)))
3173             (when found
3174               (beginning-of-line)
3175               (and
3176                (setq header (gnus-nov-parse-line
3177                              (read (current-buffer)) deps))
3178                (gnus-parent-id (mail-header-references header))))))
3179       (when header
3180         (let ((number (mail-header-number header)))
3181           (push number gnus-newsgroup-limit)
3182           (push header gnus-newsgroup-headers)
3183           (if (memq number gnus-newsgroup-unselected)
3184               (progn
3185                 (push number gnus-newsgroup-unreads)
3186                 (setq gnus-newsgroup-unselected
3187                       (delq number gnus-newsgroup-unselected)))
3188             (push number gnus-newsgroup-ancient)))))))
3189
3190 (defun gnus-build-all-threads ()
3191   "Read all the headers."
3192   (let ((gnus-summary-ignore-duplicates t)
3193         (dependencies gnus-newsgroup-dependencies)
3194         header article)
3195     (save-excursion
3196       (set-buffer nntp-server-buffer)
3197       (let ((case-fold-search nil))
3198         (goto-char (point-min))
3199         (while (not (eobp))
3200           (ignore-errors
3201             (setq article (read (current-buffer))
3202                   header (gnus-nov-parse-line
3203                           article dependencies)))
3204           (when header
3205             (save-excursion
3206               (set-buffer gnus-summary-buffer)
3207               (push header gnus-newsgroup-headers)
3208               (if (memq (setq article (mail-header-number header))
3209                         gnus-newsgroup-unselected)
3210                   (progn
3211                     (push article gnus-newsgroup-unreads)
3212                     (setq gnus-newsgroup-unselected
3213                           (delq article gnus-newsgroup-unselected)))
3214                 (push article gnus-newsgroup-ancient)))
3215             (forward-line 1)))))))
3216
3217 (defun gnus-summary-update-article-line (article header)
3218   "Update the line for ARTICLE using HEADERS."
3219   (let* ((id (mail-header-id header))
3220          (thread (gnus-id-to-thread id)))
3221     (unless thread
3222       (error "Article in no thread"))
3223     ;; Update the thread.
3224     (setcar thread header)
3225     (gnus-summary-goto-subject article)
3226     (let* ((datal (gnus-data-find-list article))
3227            (data (car datal))
3228            (length (when (cdr datal)
3229                      (- (gnus-data-pos data)
3230                         (gnus-data-pos (cadr datal)))))
3231            (buffer-read-only nil)
3232            (level (gnus-summary-thread-level)))
3233       (gnus-delete-line)
3234       (gnus-summary-insert-line
3235        header level nil (gnus-article-mark article)
3236        (memq article gnus-newsgroup-replied)
3237        (memq article gnus-newsgroup-expirable)
3238        ;; Only insert the Subject string when it's different
3239        ;; from the previous Subject string.
3240        (if (gnus-subject-equal
3241             (condition-case ()
3242                 (mail-header-subject
3243                  (gnus-data-header
3244                   (cadr
3245                    (gnus-data-find-list
3246                     article
3247                     (gnus-data-list t)))))
3248               ;; Error on the side of excessive subjects.
3249               (error ""))
3250             (mail-header-subject header))
3251            ""
3252          (mail-header-subject header))
3253        nil (cdr (assq article gnus-newsgroup-scored))
3254        (memq article gnus-newsgroup-processable))
3255       (when length
3256         (gnus-data-update-list
3257          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3258
3259 (defun gnus-summary-update-article (article &optional iheader)
3260   "Update ARTICLE in the summary buffer."
3261   (set-buffer gnus-summary-buffer)
3262   (let* ((header (gnus-summary-article-header article))
3263          (id (mail-header-id header))
3264          (data (gnus-data-find article))
3265          (thread (gnus-id-to-thread id))
3266          (references (mail-header-references header))
3267          (parent
3268           (gnus-id-to-thread
3269            (or (gnus-parent-id
3270                 (when (and references
3271                            (not (equal "" references)))
3272                   references))
3273                "none")))
3274          (buffer-read-only nil)
3275          (old (car thread)))
3276     (when thread
3277       (unless iheader
3278         (setcar thread nil)
3279         (when parent
3280           (delq thread parent)))
3281       (if (gnus-summary-insert-subject id header)
3282           ;; Set the (possibly) new article number in the data structure.
3283           (gnus-data-set-number data (gnus-id-to-article id))
3284         (setcar thread old)
3285         nil))))
3286
3287 (defun gnus-rebuild-thread (id &optional line)
3288   "Rebuild the thread containing ID.
3289 If LINE, insert the rebuilt thread starting on line LINE."
3290   (let ((buffer-read-only nil)
3291         old-pos current thread data)
3292     (if (not gnus-show-threads)
3293         (setq thread (list (car (gnus-id-to-thread id))))
3294       ;; Get the thread this article is part of.
3295       (setq thread (gnus-remove-thread id)))
3296     (setq old-pos (gnus-point-at-bol))
3297     (setq current (save-excursion
3298                     (and (zerop (forward-line -1))
3299                          (gnus-summary-article-number))))
3300     ;; If this is a gathered thread, we have to go some re-gathering.
3301     (when (stringp (car thread))
3302       (let ((subject (car thread))
3303             roots thr)
3304         (setq thread (cdr thread))
3305         (while thread
3306           (unless (memq (setq thr (gnus-id-to-thread
3307                                    (gnus-root-id
3308                                     (mail-header-id (caar thread)))))
3309                         roots)
3310             (push thr roots))
3311           (setq thread (cdr thread)))
3312         ;; We now have all (unique) roots.
3313         (if (= (length roots) 1)
3314             ;; All the loose roots are now one solid root.
3315             (setq thread (car roots))
3316           (setq thread (cons subject (gnus-sort-threads roots))))))
3317     (let (threads)
3318       ;; We then insert this thread into the summary buffer.
3319       (when line
3320         (goto-char (point-min))
3321         (forward-line (1- line)))
3322       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3323         (if gnus-show-threads
3324             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3325           (gnus-summary-prepare-unthreaded thread))
3326         (setq data (nreverse gnus-newsgroup-data))
3327         (setq threads gnus-newsgroup-threads))
3328       ;; We splice the new data into the data structure.
3329       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3330       ;;!!! then we want to insert at the beginning of the buffer.
3331       ;;!!! That happens to be true with Gnus now, but that may
3332       ;;!!! change in the future.  Perhaps.
3333       (gnus-data-enter-list
3334        (if line nil current) data (- (point) old-pos))
3335       (setq gnus-newsgroup-threads
3336             (nconc threads gnus-newsgroup-threads))
3337       (gnus-data-compute-positions))))
3338
3339 (defun gnus-number-to-header (number)
3340   "Return the header for article NUMBER."
3341   (let ((headers gnus-newsgroup-headers))
3342     (while (and headers
3343                 (not (= number (mail-header-number (car headers)))))
3344       (pop headers))
3345     (when headers
3346       (car headers))))
3347
3348 (defun gnus-parent-headers (in-headers &optional generation)
3349   "Return the headers of the GENERATIONeth parent of HEADERS."
3350   (unless generation
3351     (setq generation 1))
3352   (let ((parent t)
3353         (headers in-headers)
3354         references)
3355     (while (and parent
3356                 (not (zerop generation))
3357                 (setq references (mail-header-references headers)))
3358       (setq headers (if (and references
3359                              (setq parent (gnus-parent-id references)))
3360                         (car (gnus-id-to-thread parent))
3361                       nil))
3362       (decf generation))
3363     (and (not (eq headers in-headers))
3364          headers)))
3365
3366 (defun gnus-id-to-thread (id)
3367   "Return the (sub-)thread where ID appears."
3368   (gnus-gethash id gnus-newsgroup-dependencies))
3369
3370 (defun gnus-id-to-article (id)
3371   "Return the article number of ID."
3372   (let ((thread (gnus-id-to-thread id)))
3373     (when (and thread
3374                (car thread))
3375       (mail-header-number (car thread)))))
3376
3377 (defun gnus-id-to-header (id)
3378   "Return the article headers of ID."
3379   (car (gnus-id-to-thread id)))
3380
3381 (defun gnus-article-displayed-root-p (article)
3382   "Say whether ARTICLE is a root(ish) article."
3383   (let ((level (gnus-summary-thread-level article))
3384         (refs (mail-header-references  (gnus-summary-article-header article)))
3385         particle)
3386     (cond
3387      ((null level) nil)
3388      ((zerop level) t)
3389      ((null refs) t)
3390      ((null (gnus-parent-id refs)) t)
3391      ((and (= 1 level)
3392            (null (setq particle (gnus-id-to-article
3393                                  (gnus-parent-id refs))))
3394            (null (gnus-summary-thread-level particle)))))))
3395
3396 (defun gnus-root-id (id)
3397   "Return the id of the root of the thread where ID appears."
3398   (let (last-id prev)
3399     (while (and id (setq prev (car (gnus-id-to-thread id))))
3400       (setq last-id id
3401             id (gnus-parent-id (mail-header-references prev))))
3402     last-id))
3403
3404 (defun gnus-articles-in-thread (thread)
3405   "Return the list of articles in THREAD."
3406   (cons (mail-header-number (car thread))
3407         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3408
3409 (defun gnus-remove-thread (id &optional dont-remove)
3410   "Remove the thread that has ID in it."
3411   (let (headers thread last-id)
3412     ;; First go up in this thread until we find the root.
3413     (setq last-id (gnus-root-id id)
3414           headers (message-flatten-list (gnus-id-to-thread last-id)))
3415     ;; We have now found the real root of this thread.  It might have
3416     ;; been gathered into some loose thread, so we have to search
3417     ;; through the threads to find the thread we wanted.
3418     (let ((threads gnus-newsgroup-threads)
3419           sub)
3420       (while threads
3421         (setq sub (car threads))
3422         (if (stringp (car sub))
3423             ;; This is a gathered thread, so we look at the roots
3424             ;; below it to find whether this article is in this
3425             ;; gathered root.
3426             (progn
3427               (setq sub (cdr sub))
3428               (while sub
3429                 (when (member (caar sub) headers)
3430                   (setq thread (car threads)
3431                         threads nil
3432                         sub nil))
3433                 (setq sub (cdr sub))))
3434           ;; It's an ordinary thread, so we check it.
3435           (when (eq (car sub) (car headers))
3436             (setq thread sub
3437                   threads nil)))
3438         (setq threads (cdr threads)))
3439       ;; If this article is in no thread, then it's a root.
3440       (if thread
3441           (unless dont-remove
3442             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3443         (setq thread (gnus-id-to-thread last-id)))
3444       (when thread
3445         (prog1
3446             thread                      ; We return this thread.
3447           (unless dont-remove
3448             (if (stringp (car thread))
3449                 (progn
3450                   ;; If we use dummy roots, then we have to remove the
3451                   ;; dummy root as well.
3452                   (when (eq gnus-summary-make-false-root 'dummy)
3453                     ;; We go to the dummy root by going to
3454                     ;; the first sub-"thread", and then one line up.
3455                     (gnus-summary-goto-article
3456                      (mail-header-number (caadr thread)))
3457                     (forward-line -1)
3458                     (gnus-delete-line)
3459                     (gnus-data-compute-positions))
3460                   (setq thread (cdr thread))
3461                   (while thread
3462                     (gnus-remove-thread-1 (car thread))
3463                     (setq thread (cdr thread))))
3464               (gnus-summary-show-all-threads)
3465               (gnus-remove-thread-1 thread))))))))
3466
3467 (defun gnus-remove-thread-1 (thread)
3468   "Remove the thread THREAD recursively."
3469   (let ((number (mail-header-number (pop thread)))
3470         d)
3471     (setq thread (reverse thread))
3472     (while thread
3473       (gnus-remove-thread-1 (pop thread)))
3474     (when (setq d (gnus-data-find number))
3475       (goto-char (gnus-data-pos d))
3476       (gnus-data-remove
3477        number
3478        (- (gnus-point-at-bol)
3479           (prog1
3480               (1+ (gnus-point-at-eol))
3481             (gnus-delete-line)))))))
3482
3483 (defun gnus-sort-threads (threads)
3484   "Sort THREADS."
3485   (if (not gnus-thread-sort-functions)
3486       threads
3487     (gnus-message 8 "Sorting threads...")
3488     (prog1
3489         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3490       (gnus-message 8 "Sorting threads...done"))))
3491
3492 (defun gnus-sort-articles (articles)
3493   "Sort ARTICLES."
3494   (when gnus-article-sort-functions
3495     (gnus-message 7 "Sorting articles...")
3496     (prog1
3497         (setq gnus-newsgroup-headers
3498               (sort articles (gnus-make-sort-function
3499                               gnus-article-sort-functions)))
3500       (gnus-message 7 "Sorting articles...done"))))
3501
3502 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3503 (defmacro gnus-thread-header (thread)
3504   ;; Return header of first article in THREAD.
3505   ;; Note that THREAD must never, ever be anything else than a variable -
3506   ;; using some other form will lead to serious barfage.
3507   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3508   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3509   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3510         (vector thread) 2))
3511
3512 (defsubst gnus-article-sort-by-number (h1 h2)
3513   "Sort articles by article number."
3514   (< (mail-header-number h1)
3515      (mail-header-number h2)))
3516
3517 (defun gnus-thread-sort-by-number (h1 h2)
3518   "Sort threads by root article number."
3519   (gnus-article-sort-by-number
3520    (gnus-thread-header h1) (gnus-thread-header h2)))
3521
3522 (defsubst gnus-article-sort-by-lines (h1 h2)
3523   "Sort articles by article Lines header."
3524   (< (mail-header-lines h1)
3525      (mail-header-lines h2)))
3526
3527 (defun gnus-thread-sort-by-lines (h1 h2)
3528   "Sort threads by root article Lines header."
3529   (gnus-article-sort-by-lines
3530    (gnus-thread-header h1) (gnus-thread-header h2)))
3531
3532 (defsubst gnus-article-sort-by-author (h1 h2)
3533   "Sort articles by root author."
3534   (string-lessp
3535    (let ((addr (mime-read-field 'From h1)))
3536      (or (std11-full-name-string addr)
3537          (std11-address-string addr)
3538          ""))
3539    (let ((addr (mime-read-field 'From h2)))
3540      (or (std11-full-name-string addr)
3541          (std11-address-string addr)
3542          ""))
3543    ))
3544
3545 (defun gnus-thread-sort-by-author (h1 h2)
3546   "Sort threads by root author."
3547   (gnus-article-sort-by-author
3548    (gnus-thread-header h1)  (gnus-thread-header h2)))
3549
3550 (defsubst gnus-article-sort-by-subject (h1 h2)
3551   "Sort articles by root subject."
3552   (string-lessp
3553    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3554    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3555
3556 (defun gnus-thread-sort-by-subject (h1 h2)
3557   "Sort threads by root subject."
3558   (gnus-article-sort-by-subject
3559    (gnus-thread-header h1) (gnus-thread-header h2)))
3560
3561 (defsubst gnus-article-sort-by-date (h1 h2)
3562   "Sort articles by root article date."
3563   (time-less-p
3564    (gnus-date-get-time (mail-header-date h1))
3565    (gnus-date-get-time (mail-header-date h2))))
3566
3567 (defun gnus-thread-sort-by-date (h1 h2)
3568   "Sort threads by root article date."
3569   (gnus-article-sort-by-date
3570    (gnus-thread-header h1) (gnus-thread-header h2)))
3571
3572 (defsubst gnus-article-sort-by-score (h1 h2)
3573   "Sort articles by root article score.
3574 Unscored articles will be counted as having a score of zero."
3575   (> (or (cdr (assq (mail-header-number h1)
3576                     gnus-newsgroup-scored))
3577          gnus-summary-default-score 0)
3578      (or (cdr (assq (mail-header-number h2)
3579                     gnus-newsgroup-scored))
3580          gnus-summary-default-score 0)))
3581
3582 (defun gnus-thread-sort-by-score (h1 h2)
3583   "Sort threads by root article score."
3584   (gnus-article-sort-by-score
3585    (gnus-thread-header h1) (gnus-thread-header h2)))
3586
3587 (defun gnus-thread-sort-by-total-score (h1 h2)
3588   "Sort threads by the sum of all scores in the thread.
3589 Unscored articles will be counted as having a score of zero."
3590   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3591
3592 (defun gnus-thread-total-score (thread)
3593   ;; This function find the total score of THREAD.
3594   (cond ((null thread)
3595          0)
3596         ((consp thread)
3597          (if (stringp (car thread))
3598              (apply gnus-thread-score-function 0
3599                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3600            (gnus-thread-total-score-1 thread)))
3601         (t
3602          (gnus-thread-total-score-1 (list thread)))))
3603
3604 (defun gnus-thread-total-score-1 (root)
3605   ;; This function find the total score of the thread below ROOT.
3606   (setq root (car root))
3607   (apply gnus-thread-score-function
3608          (or (append
3609               (mapcar 'gnus-thread-total-score
3610                       (cdr (gnus-id-to-thread (mail-header-id root))))
3611               (when (> (mail-header-number root) 0)
3612                 (list (or (cdr (assq (mail-header-number root)
3613                                      gnus-newsgroup-scored))
3614                           gnus-summary-default-score 0))))
3615              (list gnus-summary-default-score)
3616              '(0))))
3617
3618 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3619 (defvar gnus-tmp-prev-subject nil)
3620 (defvar gnus-tmp-false-parent nil)
3621 (defvar gnus-tmp-root-expunged nil)
3622 (defvar gnus-tmp-dummy-line nil)
3623
3624 (defvar gnus-tmp-header)
3625 (defun gnus-extra-header (type &optional header)
3626   "Return the extra header of TYPE."
3627   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3628       ""))
3629
3630 (defun gnus-summary-prepare-threads (threads)
3631   "Prepare summary buffer from THREADS and indentation LEVEL.
3632 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3633 or a straight list of headers."
3634   (gnus-message 7 "Generating summary...")
3635
3636   (setq gnus-newsgroup-threads threads)
3637   (beginning-of-line)
3638
3639   (let ((gnus-tmp-level 0)
3640         (default-score (or gnus-summary-default-score 0))
3641         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3642         thread number subject stack state gnus-tmp-gathered beg-match
3643         new-roots gnus-tmp-new-adopts thread-end
3644         gnus-tmp-header gnus-tmp-unread
3645         gnus-tmp-replied gnus-tmp-subject-or-nil
3646         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3647         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3648         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3649
3650     (setq gnus-tmp-prev-subject nil)
3651
3652     (if (vectorp (car threads))
3653         ;; If this is a straight (sic) list of headers, then a
3654         ;; threaded summary display isn't required, so we just create
3655         ;; an unthreaded one.
3656         (gnus-summary-prepare-unthreaded threads)
3657
3658       ;; Do the threaded display.
3659
3660       (while (or threads stack gnus-tmp-new-adopts new-roots)
3661
3662         (if (and (= gnus-tmp-level 0)
3663                  (or (not stack)
3664                      (= (caar stack) 0))
3665                  (not gnus-tmp-false-parent)
3666                  (or gnus-tmp-new-adopts new-roots))
3667             (if gnus-tmp-new-adopts
3668                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3669                       thread (list (car gnus-tmp-new-adopts))
3670                       gnus-tmp-header (caar thread)
3671                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3672               (when new-roots
3673                 (setq thread (list (car new-roots))
3674                       gnus-tmp-header (caar thread)
3675                       new-roots (cdr new-roots))))
3676
3677           (if threads
3678               ;; If there are some threads, we do them before the
3679               ;; threads on the stack.
3680               (setq thread threads
3681                     gnus-tmp-header (caar thread))
3682             ;; There were no current threads, so we pop something off
3683             ;; the stack.
3684             (setq state (car stack)
3685                   gnus-tmp-level (car state)
3686                   thread (cdr state)
3687                   stack (cdr stack)
3688                   gnus-tmp-header (caar thread))))
3689
3690         (setq gnus-tmp-false-parent nil)
3691         (setq gnus-tmp-root-expunged nil)
3692         (setq thread-end nil)
3693
3694         (if (stringp gnus-tmp-header)
3695             ;; The header is a dummy root.
3696             (cond
3697              ((eq gnus-summary-make-false-root 'adopt)
3698               ;; We let the first article adopt the rest.
3699               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3700                                                (cddar thread)))
3701               (setq gnus-tmp-gathered
3702                     (nconc (mapcar
3703                             (lambda (h) (mail-header-number (car h)))
3704                             (cddar thread))
3705                            gnus-tmp-gathered))
3706               (setq thread (cons (list (caar thread)
3707                                        (cadar thread))
3708                                  (cdr thread)))
3709               (setq gnus-tmp-level -1
3710                     gnus-tmp-false-parent t))
3711              ((eq gnus-summary-make-false-root 'empty)
3712               ;; We print adopted articles with empty subject fields.
3713               (setq gnus-tmp-gathered
3714                     (nconc (mapcar
3715                             (lambda (h) (mail-header-number (car h)))
3716                             (cddar thread))
3717                            gnus-tmp-gathered))
3718               (setq gnus-tmp-level -1))
3719              ((eq gnus-summary-make-false-root 'dummy)
3720               ;; We remember that we probably want to output a dummy
3721               ;; root.
3722               (setq gnus-tmp-dummy-line gnus-tmp-header)
3723               (setq gnus-tmp-prev-subject gnus-tmp-header))
3724              (t
3725               ;; We do not make a root for the gathered
3726               ;; sub-threads at all.
3727               (setq gnus-tmp-level -1)))
3728
3729           (setq number (mail-header-number gnus-tmp-header)
3730                 subject (mail-header-subject gnus-tmp-header))
3731
3732           (cond
3733            ;; If the thread has changed subject, we might want to make
3734            ;; this subthread into a root.
3735            ((and (null gnus-thread-ignore-subject)
3736                  (not (zerop gnus-tmp-level))
3737                  gnus-tmp-prev-subject
3738                  (not (inline
3739                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3740             (setq new-roots (nconc new-roots (list (car thread)))
3741                   thread-end t
3742                   gnus-tmp-header nil))
3743            ;; If the article lies outside the current limit,
3744            ;; then we do not display it.
3745            ((not (memq number gnus-newsgroup-limit))
3746             (setq gnus-tmp-gathered
3747                   (nconc (mapcar
3748                           (lambda (h) (mail-header-number (car h)))
3749                           (cdar thread))
3750                          gnus-tmp-gathered))
3751             (setq gnus-tmp-new-adopts (if (cdar thread)
3752                                           (append gnus-tmp-new-adopts
3753                                                   (cdar thread))
3754                                         gnus-tmp-new-adopts)
3755                   thread-end t
3756                   gnus-tmp-header nil)
3757             (when (zerop gnus-tmp-level)
3758               (setq gnus-tmp-root-expunged t)))
3759            ;; Perhaps this article is to be marked as read?
3760            ((and gnus-summary-mark-below
3761                  (< (or (cdr (assq number gnus-newsgroup-scored))
3762                         default-score)
3763                     gnus-summary-mark-below)
3764                  ;; Don't touch sparse articles.
3765                  (not (gnus-summary-article-sparse-p number))
3766                  (not (gnus-summary-article-ancient-p number)))
3767             (setq gnus-newsgroup-unreads
3768                   (delq number gnus-newsgroup-unreads))
3769             (if gnus-newsgroup-auto-expire
3770                 (push number gnus-newsgroup-expirable)
3771               (push (cons number gnus-low-score-mark)
3772                     gnus-newsgroup-reads))))
3773
3774           (when gnus-tmp-header
3775             ;; We may have an old dummy line to output before this
3776             ;; article.
3777             (when (and gnus-tmp-dummy-line
3778                        (gnus-subject-equal
3779                         gnus-tmp-dummy-line
3780                         (mail-header-subject gnus-tmp-header)))
3781               (gnus-summary-insert-dummy-line
3782                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3783               (setq gnus-tmp-dummy-line nil))
3784
3785             ;; Compute the mark.
3786             (setq gnus-tmp-unread (gnus-article-mark number))
3787
3788             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3789                                   gnus-tmp-header gnus-tmp-level)
3790                   gnus-newsgroup-data)
3791
3792             ;; Actually insert the line.
3793             (setq
3794              gnus-tmp-subject-or-nil
3795              (cond
3796               ((and gnus-thread-ignore-subject
3797                     gnus-tmp-prev-subject
3798                     (not (inline (gnus-subject-equal
3799                                   gnus-tmp-prev-subject subject))))
3800                subject)
3801               ((zerop gnus-tmp-level)
3802                (if (and (eq gnus-summary-make-false-root 'empty)
3803                         (memq number gnus-tmp-gathered)
3804                         gnus-tmp-prev-subject
3805                         (inline (gnus-subject-equal
3806                                  gnus-tmp-prev-subject subject)))
3807                    gnus-summary-same-subject
3808                  subject))
3809               (t gnus-summary-same-subject)))
3810             (if (and (eq gnus-summary-make-false-root 'adopt)
3811                      (= gnus-tmp-level 1)
3812                      (memq number gnus-tmp-gathered))
3813                 (setq gnus-tmp-opening-bracket ?\<
3814                       gnus-tmp-closing-bracket ?\>)
3815               (setq gnus-tmp-opening-bracket ?\[
3816                     gnus-tmp-closing-bracket ?\]))
3817             (setq
3818              gnus-tmp-indentation
3819              (aref gnus-thread-indent-array gnus-tmp-level)
3820              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3821              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3822                                 gnus-summary-default-score 0)
3823              gnus-tmp-score-char
3824              (if (or (null gnus-summary-default-score)
3825                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3826                          gnus-summary-zcore-fuzz))
3827                  ? ;Whitespace
3828                (if (< gnus-tmp-score gnus-summary-default-score)
3829                    gnus-score-below-mark gnus-score-over-mark))
3830              gnus-tmp-replied
3831              (cond ((memq number gnus-newsgroup-processable)
3832                     gnus-process-mark)
3833                    ((memq number gnus-newsgroup-cached)
3834                     gnus-cached-mark)
3835                    ((memq number gnus-newsgroup-replied)
3836                     gnus-replied-mark)
3837                    ((memq number gnus-newsgroup-saved)
3838                     gnus-saved-mark)
3839                    (t gnus-unread-mark))
3840              gnus-tmp-from (mail-header-from gnus-tmp-header)
3841              gnus-tmp-name
3842              (cond
3843               ((string-match "<[^>]+> *$" gnus-tmp-from)
3844                (setq beg-match (match-beginning 0))
3845                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3846                         (substring gnus-tmp-from (1+ (match-beginning 0))
3847                                    (1- (match-end 0))))
3848                    (substring gnus-tmp-from 0 beg-match)))
3849               ((string-match "(.+)" gnus-tmp-from)
3850                (substring gnus-tmp-from
3851                           (1+ (match-beginning 0)) (1- (match-end 0))))
3852               (t gnus-tmp-from)))
3853             (when (string= gnus-tmp-name "")
3854               (setq gnus-tmp-name gnus-tmp-from))
3855             (unless (numberp gnus-tmp-lines)
3856               (setq gnus-tmp-lines 0))
3857             (gnus-put-text-property
3858              (point)
3859              (progn (eval gnus-summary-line-format-spec) (point))
3860              'gnus-number number)
3861             (when gnus-visual-p
3862               (forward-line -1)
3863               (gnus-run-hooks 'gnus-summary-update-hook)
3864               (forward-line 1))
3865
3866             (setq gnus-tmp-prev-subject subject)))
3867
3868         (when (nth 1 thread)
3869           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3870         (incf gnus-tmp-level)
3871         (setq threads (if thread-end nil (cdar thread)))
3872         (unless threads
3873           (setq gnus-tmp-level 0)))))
3874   (gnus-message 7 "Generating summary...done"))
3875
3876 (defun gnus-summary-prepare-unthreaded (headers)
3877   "Generate an unthreaded summary buffer based on HEADERS."
3878   (let (header number mark)
3879
3880     (beginning-of-line)
3881
3882     (while headers
3883       ;; We may have to root out some bad articles...
3884       (when (memq (setq number (mail-header-number
3885                                 (setq header (pop headers))))
3886                   gnus-newsgroup-limit)
3887         ;; Mark article as read when it has a low score.
3888         (when (and gnus-summary-mark-below
3889                    (< (or (cdr (assq number gnus-newsgroup-scored))
3890                           gnus-summary-default-score 0)
3891                       gnus-summary-mark-below)
3892                    (not (gnus-summary-article-ancient-p number)))
3893           (setq gnus-newsgroup-unreads
3894                 (delq number gnus-newsgroup-unreads))
3895           (if gnus-newsgroup-auto-expire
3896               (push number gnus-newsgroup-expirable)
3897             (push (cons number gnus-low-score-mark)
3898                   gnus-newsgroup-reads)))
3899
3900         (setq mark (gnus-article-mark number))
3901         (push (gnus-data-make number mark (1+ (point)) header 0)
3902               gnus-newsgroup-data)
3903         (gnus-summary-insert-line
3904          header 0 number
3905          mark (memq number gnus-newsgroup-replied)
3906          (memq number gnus-newsgroup-expirable)
3907          (mail-header-subject header) nil
3908          (cdr (assq number gnus-newsgroup-scored))
3909          (memq number gnus-newsgroup-processable))))))
3910
3911 (defun gnus-select-newsgroup (group &optional read-all select-articles)
3912   "Select newsgroup GROUP.
3913 If READ-ALL is non-nil, all articles in the group are selected.
3914 If SELECT-ARTICLES, only select those articles from GROUP."
3915   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3916          ;;!!! Dirty hack; should be removed.
3917          (gnus-summary-ignore-duplicates
3918           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
3919               t
3920             gnus-summary-ignore-duplicates))
3921          (info (nth 2 entry))
3922          articles fetched-articles cached)
3923
3924     (unless (gnus-check-server
3925              (setq gnus-current-select-method
3926                    (gnus-find-method-for-group group)))
3927       (error "Couldn't open server"))
3928
3929     (or (and entry (not (eq (car entry) t))) ; Either it's active...
3930         (gnus-activate-group group)     ; Or we can activate it...
3931         (progn                          ; Or we bug out.
3932           (when (equal major-mode 'gnus-summary-mode)
3933             (kill-buffer (current-buffer)))
3934           (error "Couldn't request group %s: %s"
3935                  group (gnus-status-message group))))
3936
3937     (unless (gnus-request-group group t)
3938       (when (equal major-mode 'gnus-summary-mode)
3939         (kill-buffer (current-buffer)))
3940       (error "Couldn't request group %s: %s"
3941              group (gnus-status-message group)))
3942
3943     (setq gnus-newsgroup-name group)
3944     (setq gnus-newsgroup-unselected nil)
3945     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
3946     (gnus-summary-setup-default-charset)
3947
3948     ;; Adjust and set lists of article marks.
3949     (when info
3950       (gnus-adjust-marked-articles info))
3951
3952     ;; Kludge to avoid having cached articles nixed out in virtual groups.
3953     (when (gnus-virtual-group-p group)
3954       (setq cached gnus-newsgroup-cached))
3955
3956     (setq gnus-newsgroup-unreads
3957           (gnus-set-difference
3958            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
3959            gnus-newsgroup-dormant))
3960
3961     (setq gnus-newsgroup-processable nil)
3962
3963     (gnus-update-read-articles group gnus-newsgroup-unreads)
3964
3965     (if (setq articles select-articles)
3966         (setq gnus-newsgroup-unselected
3967               (gnus-sorted-intersection
3968                gnus-newsgroup-unreads
3969                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
3970       (setq articles (gnus-articles-to-read group read-all)))
3971
3972     (cond
3973      ((null articles)
3974       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
3975       'quit)
3976      ((eq articles 0) nil)
3977      (t
3978       ;; Init the dependencies hash table.
3979       (setq gnus-newsgroup-dependencies
3980             (gnus-make-hashtable (length articles)))
3981       (gnus-set-global-variables)
3982       ;; Retrieve the headers and read them in.
3983       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
3984       (setq gnus-newsgroup-headers
3985             (gnus-retrieve-parsed-headers
3986              articles gnus-newsgroup-name
3987              ;; We might want to fetch old headers, but
3988              ;; not if there is only 1 article.
3989              (and (or (and (not (eq gnus-fetch-old-headers 'some))
3990                            (not (numberp gnus-fetch-old-headers)))
3991                       (> (length articles) 1))
3992                   gnus-fetch-old-headers)))
3993       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
3994
3995       ;; Kludge to avoid having cached articles nixed out in virtual groups.
3996       (when cached
3997         (setq gnus-newsgroup-cached cached))
3998
3999       ;; Suppress duplicates?
4000       (when gnus-suppress-duplicates
4001         (gnus-dup-suppress-articles))
4002
4003       ;; Set the initial limit.
4004       (setq gnus-newsgroup-limit (copy-sequence articles))
4005       ;; Remove canceled articles from the list of unread articles.
4006       (setq gnus-newsgroup-unreads
4007             (gnus-set-sorted-intersection
4008              gnus-newsgroup-unreads
4009              (setq fetched-articles
4010                    (mapcar (lambda (headers) (mail-header-number headers))
4011                            gnus-newsgroup-headers))))
4012       ;; Removed marked articles that do not exist.
4013       (gnus-update-missing-marks
4014        (gnus-sorted-complement fetched-articles articles))
4015       ;; We might want to build some more threads first.
4016       (when (and gnus-fetch-old-headers
4017                  (eq gnus-headers-retrieved-by 'nov))
4018         (if (eq gnus-fetch-old-headers 'invisible)
4019             (gnus-build-all-threads)
4020           (gnus-build-old-threads)))
4021       ;; Let the Gnus agent mark articles as read.
4022       (when gnus-agent
4023         (gnus-agent-get-undownloaded-list))
4024       ;; Check whether auto-expire is to be done in this group.
4025       (setq gnus-newsgroup-auto-expire
4026             (gnus-group-auto-expirable-p group))
4027       ;; Set up the article buffer now, if necessary.
4028       (unless gnus-single-article-buffer
4029         (gnus-article-setup-buffer))
4030       ;; First and last article in this newsgroup.
4031       (when gnus-newsgroup-headers
4032         (setq gnus-newsgroup-begin
4033               (mail-header-number (car gnus-newsgroup-headers))
4034               gnus-newsgroup-end
4035               (mail-header-number
4036                (gnus-last-element gnus-newsgroup-headers))))
4037       ;; GROUP is successfully selected.
4038       (or gnus-newsgroup-headers t)))))
4039
4040 (defun gnus-articles-to-read (group &optional read-all)
4041   ;; Find out what articles the user wants to read.
4042   (let* ((articles
4043           ;; Select all articles if `read-all' is non-nil, or if there
4044           ;; are no unread articles.
4045           (if (or read-all
4046                   (and (zerop (length gnus-newsgroup-marked))
4047                        (zerop (length gnus-newsgroup-unreads)))
4048                   (eq (gnus-group-find-parameter group 'display)
4049                       'all))
4050               (gnus-uncompress-range (gnus-active group))
4051             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4052                           (copy-sequence gnus-newsgroup-unreads))
4053                   '<)))
4054          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4055          (scored (length scored-list))
4056          (number (length articles))
4057          (marked (+ (length gnus-newsgroup-marked)
4058                     (length gnus-newsgroup-dormant)))
4059          (select
4060           (cond
4061            ((numberp read-all)
4062             read-all)
4063            (t
4064             (condition-case ()
4065                 (cond
4066                  ((and (or (<= scored marked) (= scored number))
4067                        (numberp gnus-large-newsgroup)
4068                        (> number gnus-large-newsgroup))
4069                   (let ((input
4070                          (read-string
4071                           (format
4072                            "How many articles from %s (default %d): "
4073                            (gnus-limit-string gnus-newsgroup-name 35)
4074                            number))))
4075                     (if (string-match "^[ \t]*$" input) number input)))
4076                  ((and (> scored marked) (< scored number)
4077                        (> (- scored number) 20))
4078                   (let ((input
4079                          (read-string
4080                           (format "%s %s (%d scored, %d total): "
4081                                   "How many articles from"
4082                                   group scored number))))
4083                     (if (string-match "^[ \t]*$" input)
4084                         number input)))
4085                  (t number))
4086               (quit nil))))))
4087     (setq select (if (stringp select) (string-to-number select) select))
4088     (if (or (null select) (zerop select))
4089         select
4090       (if (and (not (zerop scored)) (<= (abs select) scored))
4091           (progn
4092             (setq articles (sort scored-list '<))
4093             (setq number (length articles)))
4094         (setq articles (copy-sequence articles)))
4095
4096       (when (< (abs select) number)
4097         (if (< select 0)
4098             ;; Select the N oldest articles.
4099             (setcdr (nthcdr (1- (abs select)) articles) nil)
4100           ;; Select the N most recent articles.
4101           (setq articles (nthcdr (- number select) articles))))
4102       (setq gnus-newsgroup-unselected
4103             (gnus-sorted-intersection
4104              gnus-newsgroup-unreads
4105              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4106       articles)))
4107
4108 (defun gnus-killed-articles (killed articles)
4109   (let (out)
4110     (while articles
4111       (when (inline (gnus-member-of-range (car articles) killed))
4112         (push (car articles) out))
4113       (setq articles (cdr articles)))
4114     out))
4115
4116 (defun gnus-uncompress-marks (marks)
4117   "Uncompress the mark ranges in MARKS."
4118   (let ((uncompressed '(score bookmark))
4119         out)
4120     (while marks
4121       (if (memq (caar marks) uncompressed)
4122           (push (car marks) out)
4123         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4124       (setq marks (cdr marks)))
4125     out))
4126
4127 (defun gnus-adjust-marked-articles (info)
4128   "Set all article lists and remove all marks that are no longer valid."
4129   (let* ((marked-lists (gnus-info-marks info))
4130          (active (gnus-active (gnus-info-group info)))
4131          (min (car active))
4132          (max (cdr active))
4133          (types gnus-article-mark-lists)
4134          (uncompressed '(score bookmark killed))
4135          marks var articles article mark)
4136
4137     (while marked-lists
4138       (setq marks (pop marked-lists))
4139       (set (setq var (intern (format "gnus-newsgroup-%s"
4140                                      (car (rassq (setq mark (car marks))
4141                                                  types)))))
4142            (if (memq (car marks) uncompressed) (cdr marks)
4143              (gnus-uncompress-range (cdr marks))))
4144
4145       (setq articles (symbol-value var))
4146
4147       ;; All articles have to be subsets of the active articles.
4148       (cond
4149        ;; Adjust "simple" lists.
4150        ((memq mark '(tick dormant expire reply save))
4151         (while articles
4152           (when (or (< (setq article (pop articles)) min) (> article max))
4153             (set var (delq article (symbol-value var))))))
4154        ;; Adjust assocs.
4155        ((memq mark uncompressed)
4156         (when (not (listp (cdr (symbol-value var))))
4157           (set var (list (symbol-value var))))
4158         (when (not (listp (cdr articles)))
4159           (setq articles (list articles)))
4160         (while articles
4161           (when (or (not (consp (setq article (pop articles))))
4162                     (< (car article) min)
4163                     (> (car article) max))
4164             (set var (delq article (symbol-value var))))))))))
4165
4166 (defun gnus-update-missing-marks (missing)
4167   "Go through the list of MISSING articles and remove them from the mark lists."
4168   (when missing
4169     (let ((types gnus-article-mark-lists)
4170           var m)
4171       ;; Go through all types.
4172       (while types
4173         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4174         (when (symbol-value var)
4175           ;; This list has articles.  So we delete all missing articles
4176           ;; from it.
4177           (setq m missing)
4178           (while m
4179             (set var (delq (pop m) (symbol-value var)))))))))
4180
4181 (defun gnus-update-marks ()
4182   "Enter the various lists of marked articles into the newsgroup info list."
4183   (let ((types gnus-article-mark-lists)
4184         (info (gnus-get-info gnus-newsgroup-name))
4185         (uncompressed '(score bookmark killed))
4186         type list newmarked symbol delta-marks)
4187     (when info
4188       ;; Add all marks lists that are non-nil to the list of marks lists.
4189       (while (setq type (pop types))
4190         (when (setq list (symbol-value
4191                           (setq symbol
4192                                 (intern (format "gnus-newsgroup-%s"
4193                                                 (car type))))))
4194
4195           ;; Get rid of the entries of the articles that have the
4196           ;; default score.
4197           (when (and (eq (cdr type) 'score)
4198                      gnus-save-score
4199                      list)
4200             (let* ((arts list)
4201                    (prev (cons nil list))
4202                    (all prev))
4203               (while arts
4204                 (if (or (not (consp (car arts)))
4205                         (= (cdar arts) gnus-summary-default-score))
4206                     (setcdr prev (cdr arts))
4207                   (setq prev arts))
4208                 (setq arts (cdr arts)))
4209               (setq list (cdr all))))
4210
4211          (when (gnus-check-backend-function 'request-set-mark
4212                                             gnus-newsgroup-name)
4213            ;; score & bookmark are not proper flags (they are cons cells)
4214            ;; cache is a internal gnus flag
4215            (unless (memq (cdr type) '(cache score bookmark))
4216              (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4217                     (del (gnus-remove-from-range old list))
4218                     (add (gnus-remove-from-range list old)))
4219                (if add
4220                    (push (list add 'add (list (cdr type))) delta-marks))
4221                (if del
4222                    (push (list del 'del (list (cdr type))) delta-marks)))))
4223
4224           (push (cons (cdr type)
4225                       (if (memq (cdr type) uncompressed) list
4226                         (gnus-compress-sequence
4227                          (set symbol (sort list '<)) t)))
4228                 newmarked)))
4229
4230       (if delta-marks
4231          (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4232
4233       ;; Enter these new marks into the info of the group.
4234       (if (nthcdr 3 info)
4235           (setcar (nthcdr 3 info) newmarked)
4236         ;; Add the marks lists to the end of the info.
4237         (when newmarked
4238           (setcdr (nthcdr 2 info) (list newmarked))))
4239
4240       ;; Cut off the end of the info if there's nothing else there.
4241       (let ((i 5))
4242         (while (and (> i 2)
4243                     (not (nth i info)))
4244           (when (nthcdr (decf i) info)
4245             (setcdr (nthcdr i info) nil)))))))
4246
4247 (defun gnus-set-mode-line (where)
4248   "This function sets the mode line of the article or summary buffers.
4249 If WHERE is `summary', the summary mode line format will be used."
4250   ;; Is this mode line one we keep updated?
4251   (when (and (memq where gnus-updated-mode-lines)
4252              (symbol-value
4253               (intern (format "gnus-%s-mode-line-format-spec" where))))
4254     (let (mode-string)
4255       (save-excursion
4256         ;; We evaluate this in the summary buffer since these
4257         ;; variables are buffer-local to that buffer.
4258         (set-buffer gnus-summary-buffer)
4259         ;; We bind all these variables that are used in the `eval' form
4260         ;; below.
4261         (let* ((mformat (symbol-value
4262                          (intern
4263                           (format "gnus-%s-mode-line-format-spec" where))))
4264                (gnus-tmp-group-name gnus-newsgroup-name)
4265                (gnus-tmp-article-number (or gnus-current-article 0))
4266                (gnus-tmp-unread gnus-newsgroup-unreads)
4267                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4268                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4269                (gnus-tmp-unread-and-unselected
4270                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4271                             (zerop gnus-tmp-unselected))
4272                        "")
4273                       ((zerop gnus-tmp-unselected)
4274                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4275                       (t (format "{%d(+%d) more}"
4276                                  gnus-tmp-unread-and-unticked
4277                                  gnus-tmp-unselected))))
4278                (gnus-tmp-subject
4279                 (if (and gnus-current-headers
4280                          (vectorp gnus-current-headers))
4281                     (gnus-mode-string-quote
4282                      (mail-header-subject gnus-current-headers))
4283                   ""))
4284                bufname-length max-len
4285                gnus-tmp-header);; passed as argument to any user-format-funcs
4286           (setq mode-string (eval mformat))
4287           (setq bufname-length (if (string-match "%b" mode-string)
4288                                    (- (length
4289                                        (buffer-name
4290                                         (if (eq where 'summary)
4291                                             nil
4292                                           (get-buffer gnus-article-buffer))))
4293                                       2)
4294                                  0))
4295           (setq max-len (max 4 (if gnus-mode-non-string-length
4296                                    (- (window-width)
4297                                       gnus-mode-non-string-length
4298                                       bufname-length)
4299                                  (length mode-string))))
4300           ;; We might have to chop a bit of the string off...
4301           (when (> (length mode-string) max-len)
4302             (setq mode-string
4303                   (concat (gnus-truncate-string mode-string (- max-len 3))
4304                           "...")))
4305           ;; Pad the mode string a bit.
4306           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4307       ;; Update the mode line.
4308       (setq mode-line-buffer-identification
4309             (gnus-mode-line-buffer-identification (list mode-string)))
4310       (set-buffer-modified-p t))))
4311
4312 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4313   "Go through the HEADERS list and add all Xrefs to a hash table.
4314 The resulting hash table is returned, or nil if no Xrefs were found."
4315   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4316          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4317          (xref-hashtb (gnus-make-hashtable))
4318          start group entry number xrefs header)
4319     (while headers
4320       (setq header (pop headers))
4321       (when (and (setq xrefs (mail-header-xref header))
4322                  (not (memq (setq number (mail-header-number header))
4323                             unreads)))
4324         (setq start 0)
4325         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4326           (setq start (match-end 0))
4327           (setq group (if prefix
4328                           (concat prefix (substring xrefs (match-beginning 1)
4329                                                     (match-end 1)))
4330                         (substring xrefs (match-beginning 1) (match-end 1))))
4331           (setq number
4332                 (string-to-int (substring xrefs (match-beginning 2)
4333                                           (match-end 2))))
4334           (if (setq entry (gnus-gethash group xref-hashtb))
4335               (setcdr entry (cons number (cdr entry)))
4336             (gnus-sethash group (cons number nil) xref-hashtb)))))
4337     (and start xref-hashtb)))
4338
4339 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4340   "Look through all the headers and mark the Xrefs as read."
4341   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4342         name entry info xref-hashtb idlist method nth4)
4343     (save-excursion
4344       (set-buffer gnus-group-buffer)
4345       (when (setq xref-hashtb
4346                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4347         (mapatoms
4348          (lambda (group)
4349            (unless (string= from-newsgroup (setq name (symbol-name group)))
4350              (setq idlist (symbol-value group))
4351              ;; Dead groups are not updated.
4352              (and (prog1
4353                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4354                             info (nth 2 entry))
4355                     (when (stringp (setq nth4 (gnus-info-method info)))
4356                       (setq nth4 (gnus-server-to-method nth4))))
4357                   ;; Only do the xrefs if the group has the same
4358                   ;; select method as the group we have just read.
4359                   (or (gnus-methods-equal-p
4360                        nth4 (gnus-find-method-for-group from-newsgroup))
4361                       virtual
4362                       (equal nth4 (setq method (gnus-find-method-for-group
4363                                                 from-newsgroup)))
4364                       (and (equal (car nth4) (car method))
4365                            (equal (nth 1 nth4) (nth 1 method))))
4366                   gnus-use-cross-reference
4367                   (or (not (eq gnus-use-cross-reference t))
4368                       virtual
4369                       ;; Only do cross-references on subscribed
4370                       ;; groups, if that is what is wanted.
4371                       (<= (gnus-info-level info) gnus-level-subscribed))
4372                   (gnus-group-make-articles-read name idlist))))
4373          xref-hashtb)))))
4374
4375 (defun gnus-compute-read-articles (group articles)
4376   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4377          (info (nth 2 entry))
4378          (active (gnus-active group))
4379          ninfo)
4380     (when entry
4381       ;; First peel off all invalid article numbers.
4382       (when active
4383         (let ((ids articles)
4384               id first)
4385           (while (setq id (pop ids))
4386             (when (and first (> id (cdr active)))
4387               ;; We'll end up in this situation in one particular
4388               ;; obscure situation.  If you re-scan a group and get
4389               ;; a new article that is cross-posted to a different
4390               ;; group that has not been re-scanned, you might get
4391               ;; crossposted article that has a higher number than
4392               ;; Gnus believes possible.  So we re-activate this
4393               ;; group as well.  This might mean doing the
4394               ;; crossposting thingy will *increase* the number
4395               ;; of articles in some groups.  Tsk, tsk.
4396               (setq active (or (gnus-activate-group group) active)))
4397             (when (or (> id (cdr active))
4398                       (< id (car active)))
4399               (setq articles (delq id articles))))))
4400       ;; If the read list is nil, we init it.
4401       (if (and active
4402                (null (gnus-info-read info))
4403                (> (car active) 1))
4404           (setq ninfo (cons 1 (1- (car active))))
4405         (setq ninfo (gnus-info-read info)))
4406       ;; Then we add the read articles to the range.
4407       (gnus-add-to-range
4408        ninfo (setq articles (sort articles '<))))))
4409
4410 (defun gnus-group-make-articles-read (group articles)
4411   "Update the info of GROUP to say that ARTICLES are read."
4412   (let* ((num 0)
4413          (entry (gnus-gethash group gnus-newsrc-hashtb))
4414          (info (nth 2 entry))
4415          (active (gnus-active group))
4416          range)
4417     (when entry
4418       (setq range (gnus-compute-read-articles group articles))
4419       (save-excursion
4420         (set-buffer gnus-group-buffer)
4421         (gnus-undo-register
4422           `(progn
4423              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4424              (gnus-info-set-read ',info ',(gnus-info-read info))
4425              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4426              (gnus-group-update-group ,group t))))
4427       ;; Add the read articles to the range.
4428       (gnus-info-set-read info range)
4429       ;; Then we have to re-compute how many unread
4430       ;; articles there are in this group.
4431       (when active
4432         (cond
4433          ((not range)
4434           (setq num (- (1+ (cdr active)) (car active))))
4435          ((not (listp (cdr range)))
4436           (setq num (- (cdr active) (- (1+ (cdr range))
4437                                        (car range)))))
4438          (t
4439           (while range
4440             (if (numberp (car range))
4441                 (setq num (1+ num))
4442               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4443             (setq range (cdr range)))
4444           (setq num (- (cdr active) num))))
4445         ;; Update the number of unread articles.
4446         (setcar entry num)
4447         ;; Update the group buffer.
4448         (gnus-group-update-group group t)))))
4449
4450 (defun gnus-methods-equal-p (m1 m2)
4451   (let ((m1 (or m1 gnus-select-method))
4452         (m2 (or m2 gnus-select-method)))
4453     (or (equal m1 m2)
4454         (and (eq (car m1) (car m2))
4455              (or (not (memq 'address (assoc (symbol-name (car m1))
4456                                             gnus-valid-select-methods)))
4457                  (equal (nth 1 m1) (nth 1 m2)))))))
4458
4459 (defvar gnus-newsgroup-none-id 0)
4460
4461 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4462   (let ((cur nntp-server-buffer)
4463         (dependencies
4464          (or dependencies
4465              (save-excursion (set-buffer gnus-summary-buffer)
4466                              gnus-newsgroup-dependencies)))
4467         headers id end ref
4468         (mail-parse-charset gnus-newsgroup-charset))
4469     (save-excursion
4470       (set-buffer nntp-server-buffer)
4471       ;; Translate all TAB characters into SPACE characters.
4472       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4473       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4474       (gnus-run-hooks 'gnus-parse-headers-hook)
4475       (let ((case-fold-search t)
4476             in-reply-to header p lines chars ctype)
4477         (goto-char (point-min))
4478         ;; Search to the beginning of the next header.  Error messages
4479         ;; do not begin with 2 or 3.
4480         (while (re-search-forward "^[23][0-9]+ " nil t)
4481           (setq id nil
4482                 ref nil)
4483           ;; This implementation of this function, with nine
4484           ;; search-forwards instead of the one re-search-forward and
4485           ;; a case (which basically was the old function) is actually
4486           ;; about twice as fast, even though it looks messier.  You
4487           ;; can't have everything, I guess.  Speed and elegance
4488           ;; doesn't always go hand in hand.
4489           (setq
4490            header
4491            (make-full-mail-header
4492             ;; Number.
4493             (prog1
4494                 (read cur)
4495               (end-of-line)
4496               (setq p (point))
4497               (narrow-to-region (point)
4498                                 (or (and (search-forward "\n.\n" nil t)
4499                                          (- (point) 2))
4500                                     (point))))
4501             ;; Subject.
4502             (progn
4503               (goto-char p)
4504               (if (search-forward "\nsubject: " nil t)
4505                   (buffer-substring (match-end 0) (std11-field-end))
4506                 "(none)"))
4507             ;; From.
4508             (progn
4509               (goto-char p)
4510               (if (search-forward "\nfrom: " nil t)
4511                   (buffer-substring (match-end 0) (std11-field-end))
4512                 "(nobody)"))
4513             ;; Date.
4514             (progn
4515               (goto-char p)
4516               (if (search-forward "\ndate: " nil t)
4517                   (buffer-substring (match-end 0) (std11-field-end))
4518                 ""))
4519             ;; Message-ID.
4520             (progn
4521               (goto-char p)
4522               (setq id (if (re-search-forward
4523                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4524                            ;; We do it this way to make sure the Message-ID
4525                            ;; is (somewhat) syntactically valid.
4526                            (buffer-substring (match-beginning 1)
4527                                              (match-end 1))
4528                          ;; If there was no message-id, we just fake one
4529                          ;; to make subsequent routines simpler.
4530                          (nnheader-generate-fake-message-id))))
4531             ;; References.
4532             (progn
4533               (goto-char p)
4534               (if (search-forward "\nreferences: " nil t)
4535                   (progn
4536                     (setq end (point))
4537                     (prog1
4538                         (buffer-substring (match-end 0) (std11-field-end))
4539                       (setq ref
4540                             (buffer-substring
4541                              (progn
4542                                ;; (end-of-line)
4543                                (search-backward ">" end t)
4544                                (1+ (point)))
4545                              (progn
4546                                (search-backward "<" end t)
4547                                (point))))))
4548                 ;; Get the references from the in-reply-to header if there
4549                 ;; were no references and the in-reply-to header looks
4550                 ;; promising.
4551                 (if (and (search-forward "\nin-reply-to: " nil t)
4552                          (setq in-reply-to
4553                                (buffer-substring (match-end 0)
4554                                                  (std11-field-end)))
4555                          (string-match "<[^>]+>" in-reply-to))
4556                     (let (ref2)
4557                       (setq ref (substring in-reply-to (match-beginning 0)
4558                                            (match-end 0)))
4559                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4560                         (setq ref2 (substring in-reply-to (match-beginning 0)
4561                                               (match-end 0)))
4562                         (when (> (length ref2) (length ref))
4563                           (setq ref ref2)))
4564                       ref)
4565                   (setq ref nil))))
4566             ;; Chars.
4567             (progn
4568               (goto-char p)
4569               (if (search-forward "\nchars: " nil t)
4570                   (if (numberp (setq chars (ignore-errors (read cur))))
4571                       chars 0)
4572                 0))
4573             ;; Lines.
4574             (progn
4575               (goto-char p)
4576               (if (search-forward "\nlines: " nil t)
4577                   (if (numberp (setq lines (ignore-errors (read cur))))
4578                       lines 0)
4579                 0))
4580             ;; Xref.
4581             (progn
4582               (goto-char p)
4583               (and (search-forward "\nxref: " nil t)
4584                    (buffer-substring (match-end 0) (std11-field-end))))
4585             ;; Extra.
4586             (when gnus-extra-headers
4587               (let ((extra gnus-extra-headers)
4588                     out)
4589                 (while extra
4590                   (goto-char p)
4591                   (when (search-forward
4592                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4593                     (push (cons (car extra)
4594                                 (buffer-substring (match-end 0)
4595                                                   (std11-field-end)))
4596                           out))
4597                   (pop extra))
4598                 out))))
4599           (goto-char p)
4600           (if (and (search-forward "\ncontent-type: " nil t)
4601                    (setq ctype
4602                          (buffer-substring (match-end 0) (std11-field-end))))
4603               (mime-entity-set-content-type-internal
4604                header (mime-parse-Content-Type ctype)))
4605           (when (equal id ref)
4606             (setq ref nil))
4607
4608           (when gnus-alter-header-function
4609             (funcall gnus-alter-header-function header)
4610             (setq id (mail-header-id header)
4611                   ref (gnus-parent-id (mail-header-references header))))
4612
4613           (when (setq header
4614                       (gnus-dependencies-add-header
4615                        header dependencies force-new))
4616             (push header headers))
4617           (goto-char (point-max))
4618           (widen))
4619         (nreverse headers)))))
4620
4621 ;; Goes through the xover lines and returns a list of vectors
4622 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4623                                                   force-new dependencies
4624                                                   group also-fetch-heads)
4625   "Parse the news overview data in the server buffer, and return a
4626 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4627   ;; Get the Xref when the users reads the articles since most/some
4628   ;; NNTP servers do not include Xrefs when using XOVER.
4629   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4630   (let ((mail-parse-charset gnus-newsgroup-charset)
4631         (cur nntp-server-buffer)
4632         (dependencies (or dependencies gnus-newsgroup-dependencies))
4633         number headers header)
4634     (save-excursion
4635       (set-buffer nntp-server-buffer)
4636       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4637       ;; Allow the user to mangle the headers before parsing them.
4638       (gnus-run-hooks 'gnus-parse-headers-hook)
4639       (goto-char (point-min))
4640       (while (not (eobp))
4641         (condition-case ()
4642             (while (and sequence (not (eobp)))
4643               (setq number (read cur))
4644               (while (and sequence
4645                           (< (car sequence) number))
4646                 (setq sequence (cdr sequence)))
4647               (and sequence
4648                    (eq number (car sequence))
4649                    (progn
4650                      (setq sequence (cdr sequence))
4651                      (setq header (inline
4652                                     (gnus-nov-parse-line
4653                                      number dependencies force-new))))
4654                    (push header headers))
4655               (forward-line 1))
4656           (error
4657            (gnus-error 4 "Strange nov line (%d)"
4658                        (count-lines (point-min) (point)))))
4659         (forward-line 1))
4660       ;; A common bug in inn is that if you have posted an article and
4661       ;; then retrieves the active file, it will answer correctly --
4662       ;; the new article is included.  However, a NOV entry for the
4663       ;; article may not have been generated yet, so this may fail.
4664       ;; We work around this problem by retrieving the last few
4665       ;; headers using HEAD.
4666       (if (or (not also-fetch-heads)
4667               (not sequence))
4668           ;; We (probably) got all the headers.
4669           (nreverse headers)
4670         (let ((gnus-nov-is-evil t))
4671           (nconc
4672            (nreverse headers)
4673            (gnus-retrieve-parsed-headers sequence group)
4674            ))))))
4675
4676 (defun gnus-article-get-xrefs ()
4677   "Fill in the Xref value in `gnus-current-headers', if necessary.
4678 This is meant to be called in `gnus-article-internal-prepare-hook'."
4679   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4680                                  gnus-current-headers)))
4681     (or (not gnus-use-cross-reference)
4682         (not headers)
4683         (and (mail-header-xref headers)
4684              (not (string= (mail-header-xref headers) "")))
4685         (let ((case-fold-search t)
4686               xref)
4687           (save-restriction
4688             (nnheader-narrow-to-headers)
4689             (goto-char (point-min))
4690             (when (or (and (eq (downcase (char-after)) ?x)
4691                            (looking-at "Xref:"))
4692                       (search-forward "\nXref:" nil t))
4693               (goto-char (1+ (match-end 0)))
4694               (setq xref (buffer-substring (point)
4695                                            (progn (end-of-line) (point))))
4696               (mail-header-set-xref headers xref)))))))
4697
4698 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4699   "Find article ID and insert the summary line for that article.
4700 OLD-HEADER can either be a header or a line number to insert
4701 the subject line on."
4702   (let* ((line (and (numberp old-header) old-header))
4703          (old-header (and (vectorp old-header) old-header))
4704          (header (cond ((and old-header use-old-header)
4705                         old-header)
4706                        ((and (numberp id)
4707                              (gnus-number-to-header id))
4708                         (gnus-number-to-header id))
4709                        (t
4710                         (gnus-read-header id))))
4711          (number (and (numberp id) id))
4712          d)
4713     (when header
4714       ;; Rebuild the thread that this article is part of and go to the
4715       ;; article we have fetched.
4716       (when (and (not gnus-show-threads)
4717                  old-header)
4718         (when (and number
4719                    (setq d (gnus-data-find (mail-header-number old-header))))
4720           (goto-char (gnus-data-pos d))
4721           (gnus-data-remove
4722            number
4723            (- (gnus-point-at-bol)
4724               (prog1
4725                   (1+ (gnus-point-at-eol))
4726                 (gnus-delete-line))))))
4727       (when old-header
4728         (mail-header-set-number header (mail-header-number old-header)))
4729       (setq gnus-newsgroup-sparse
4730             (delq (setq number (mail-header-number header))
4731                   gnus-newsgroup-sparse))
4732       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4733       (push number gnus-newsgroup-limit)
4734       (gnus-rebuild-thread (mail-header-id header) line)
4735       (gnus-summary-goto-subject number nil t))
4736     (when (and (numberp number)
4737                (> number 0))
4738       ;; We have to update the boundaries even if we can't fetch the
4739       ;; article if ID is a number -- so that the next `P' or `N'
4740       ;; command will fetch the previous (or next) article even
4741       ;; if the one we tried to fetch this time has been canceled.
4742       (when (> number gnus-newsgroup-end)
4743         (setq gnus-newsgroup-end number))
4744       (when (< number gnus-newsgroup-begin)
4745         (setq gnus-newsgroup-begin number))
4746       (setq gnus-newsgroup-unselected
4747             (delq number gnus-newsgroup-unselected)))
4748     ;; Report back a success?
4749     (and header (mail-header-number header))))
4750
4751 ;;; Process/prefix in the summary buffer
4752
4753 (defun gnus-summary-work-articles (n)
4754   "Return a list of articles to be worked upon.
4755 The prefix argument, the list of process marked articles, and the
4756 current article will be taken into consideration."
4757   (save-excursion
4758     (set-buffer gnus-summary-buffer)
4759     (cond
4760      (n
4761       ;; A numerical prefix has been given.
4762       (setq n (prefix-numeric-value n))
4763       (let ((backward (< n 0))
4764             (n (abs (prefix-numeric-value n)))
4765             articles article)
4766         (save-excursion
4767           (while
4768               (and (> n 0)
4769                    (push (setq article (gnus-summary-article-number))
4770                          articles)
4771                    (if backward
4772                        (gnus-summary-find-prev nil article)
4773                      (gnus-summary-find-next nil article)))
4774             (decf n)))
4775         (nreverse articles)))
4776      ((and (gnus-region-active-p) (mark))
4777       (message "region active")
4778       ;; Work on the region between point and mark.
4779       (let ((max (max (point) (mark)))
4780             articles article)
4781         (save-excursion
4782           (goto-char (min (point) (mark)))
4783           (while
4784               (and
4785                (push (setq article (gnus-summary-article-number)) articles)
4786                (gnus-summary-find-next nil article)
4787                (< (point) max)))
4788           (nreverse articles))))
4789      (gnus-newsgroup-processable
4790       ;; There are process-marked articles present.
4791       ;; Save current state.
4792       (gnus-summary-save-process-mark)
4793       ;; Return the list.
4794       (reverse gnus-newsgroup-processable))
4795      (t
4796       ;; Just return the current article.
4797       (list (gnus-summary-article-number))))))
4798
4799 (defmacro gnus-summary-iterate (arg &rest forms)
4800   "Iterate over the process/prefixed articles and do FORMS.
4801 ARG is the interactive prefix given to the command.  FORMS will be
4802 executed with point over the summary line of the articles."
4803   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4804     `(let ((,articles (gnus-summary-work-articles ,arg)))
4805        (while ,articles
4806          (gnus-summary-goto-subject (car ,articles))
4807          ,@forms))))
4808
4809 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4810 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4811
4812 (defun gnus-summary-save-process-mark ()
4813   "Push the current set of process marked articles on the stack."
4814   (interactive)
4815   (push (copy-sequence gnus-newsgroup-processable)
4816         gnus-newsgroup-process-stack))
4817
4818 (defun gnus-summary-kill-process-mark ()
4819   "Push the current set of process marked articles on the stack and unmark."
4820   (interactive)
4821   (gnus-summary-save-process-mark)
4822   (gnus-summary-unmark-all-processable))
4823
4824 (defun gnus-summary-yank-process-mark ()
4825   "Pop the last process mark state off the stack and restore it."
4826   (interactive)
4827   (unless gnus-newsgroup-process-stack
4828     (error "Empty mark stack"))
4829   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4830
4831 (defun gnus-summary-process-mark-set (set)
4832   "Make SET into the current process marked articles."
4833   (gnus-summary-unmark-all-processable)
4834   (while set
4835     (gnus-summary-set-process-mark (pop set))))
4836
4837 ;;; Searching and stuff
4838
4839 (defun gnus-summary-search-group (&optional backward use-level)
4840   "Search for next unread newsgroup.
4841 If optional argument BACKWARD is non-nil, search backward instead."
4842   (save-excursion
4843     (set-buffer gnus-group-buffer)
4844     (when (gnus-group-search-forward
4845            backward nil (if use-level (gnus-group-group-level) nil))
4846       (gnus-group-group-name))))
4847
4848 (defun gnus-summary-best-group (&optional exclude-group)
4849   "Find the name of the best unread group.
4850 If EXCLUDE-GROUP, do not go to this group."
4851   (save-excursion
4852     (set-buffer gnus-group-buffer)
4853     (save-excursion
4854       (gnus-group-best-unread-group exclude-group))))
4855
4856 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4857   (if backward (gnus-summary-find-prev)
4858     (let* ((dummy (gnus-summary-article-intangible-p))
4859            (article (or article (gnus-summary-article-number)))
4860            (arts (gnus-data-find-list article))
4861            result)
4862       (when (and (not dummy)
4863                  (or (not gnus-summary-check-current)
4864                      (not unread)
4865                      (not (gnus-data-unread-p (car arts)))))
4866         (setq arts (cdr arts)))
4867       (when (setq result
4868                   (if unread
4869                       (progn
4870                         (while arts
4871                           (when (or (and undownloaded
4872                                          (eq gnus-undownloaded-mark
4873                                              (gnus-data-mark (car arts))))
4874                                     (gnus-data-unread-p (car arts)))
4875                             (setq result (car arts)
4876                                   arts nil))
4877                           (setq arts (cdr arts)))
4878                         result)
4879                     (car arts)))
4880         (goto-char (gnus-data-pos result))
4881         (gnus-data-number result)))))
4882
4883 (defun gnus-summary-find-prev (&optional unread article)
4884   (let* ((eobp (eobp))
4885          (article (or article (gnus-summary-article-number)))
4886          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4887          result)
4888     (when (and (not eobp)
4889                (or (not gnus-summary-check-current)
4890                    (not unread)
4891                    (not (gnus-data-unread-p (car arts)))))
4892       (setq arts (cdr arts)))
4893     (when (setq result
4894                 (if unread
4895                     (progn
4896                       (while arts
4897                         (when (gnus-data-unread-p (car arts))
4898                           (setq result (car arts)
4899                                 arts nil))
4900                         (setq arts (cdr arts)))
4901                       result)
4902                   (car arts)))
4903       (goto-char (gnus-data-pos result))
4904       (gnus-data-number result))))
4905
4906 (defun gnus-summary-find-subject (subject &optional unread backward article)
4907   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4908          (article (or article (gnus-summary-article-number)))
4909          (articles (gnus-data-list backward))
4910          (arts (gnus-data-find-list article articles))
4911          result)
4912     (when (or (not gnus-summary-check-current)
4913               (not unread)
4914               (not (gnus-data-unread-p (car arts))))
4915       (setq arts (cdr arts)))
4916     (while arts
4917       (and (or (not unread)
4918                (gnus-data-unread-p (car arts)))
4919            (vectorp (gnus-data-header (car arts)))
4920            (gnus-subject-equal
4921             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4922            (setq result (car arts)
4923                  arts nil))
4924       (setq arts (cdr arts)))
4925     (and result
4926          (goto-char (gnus-data-pos result))
4927          (gnus-data-number result))))
4928
4929 (defun gnus-summary-search-forward (&optional unread subject backward)
4930   "Search forward for an article.
4931 If UNREAD, look for unread articles.  If SUBJECT, look for
4932 articles with that subject.  If BACKWARD, search backward instead."
4933   (cond (subject (gnus-summary-find-subject subject unread backward))
4934         (backward (gnus-summary-find-prev unread))
4935         (t (gnus-summary-find-next unread))))
4936
4937 (defun gnus-recenter (&optional n)
4938   "Center point in window and redisplay frame.
4939 Also do horizontal recentering."
4940   (interactive "P")
4941   (when (and gnus-auto-center-summary
4942              (not (eq gnus-auto-center-summary 'vertical)))
4943     (gnus-horizontal-recenter))
4944   (recenter n))
4945
4946 (defun gnus-summary-recenter ()
4947   "Center point in the summary window.
4948 If `gnus-auto-center-summary' is nil, or the article buffer isn't
4949 displayed, no centering will be performed."
4950   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
4951   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
4952   (let* ((top (cond ((< (window-height) 4) 0)
4953                     ((< (window-height) 7) 1)
4954                     (t (if (numberp gnus-auto-center-summary)
4955                            gnus-auto-center-summary
4956                          2))))
4957          (height (1- (window-height)))
4958          (bottom (save-excursion (goto-char (point-max))
4959                                  (forward-line (- height))
4960                                  (point)))
4961          (window (get-buffer-window (current-buffer))))
4962     ;; The user has to want it.
4963     (when gnus-auto-center-summary
4964       (when (get-buffer-window gnus-article-buffer)
4965         ;; Only do recentering when the article buffer is displayed,
4966         ;; Set the window start to either `bottom', which is the biggest
4967         ;; possible valid number, or the second line from the top,
4968         ;; whichever is the least.
4969         (set-window-start
4970          window (min bottom (save-excursion
4971                               (forward-line (- top)) (point)))))
4972       ;; Do horizontal recentering while we're at it.
4973       (when (and (get-buffer-window (current-buffer) t)
4974                  (not (eq gnus-auto-center-summary 'vertical)))
4975         (let ((selected (selected-window)))
4976           (select-window (get-buffer-window (current-buffer) t))
4977           (gnus-summary-position-point)
4978           (gnus-horizontal-recenter)
4979           (select-window selected))))))
4980
4981 (defun gnus-summary-jump-to-group (newsgroup)
4982   "Move point to NEWSGROUP in group mode buffer."
4983   ;; Keep update point of group mode buffer if visible.
4984   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
4985       (save-window-excursion
4986         ;; Take care of tree window mode.
4987         (when (get-buffer-window gnus-group-buffer)
4988           (pop-to-buffer gnus-group-buffer))
4989         (gnus-group-jump-to-group newsgroup))
4990     (save-excursion
4991       ;; Take care of tree window mode.
4992       (if (get-buffer-window gnus-group-buffer)
4993           (pop-to-buffer gnus-group-buffer)
4994         (set-buffer gnus-group-buffer))
4995       (gnus-group-jump-to-group newsgroup))))
4996
4997 ;; This function returns a list of article numbers based on the
4998 ;; difference between the ranges of read articles in this group and
4999 ;; the range of active articles.
5000 (defun gnus-list-of-unread-articles (group)
5001   (let* ((read (gnus-info-read (gnus-get-info group)))
5002          (active (or (gnus-active group) (gnus-activate-group group)))
5003          (last (cdr active))
5004          first nlast unread)
5005     ;; If none are read, then all are unread.
5006     (if (not read)
5007         (setq first (car active))
5008       ;; If the range of read articles is a single range, then the
5009       ;; first unread article is the article after the last read
5010       ;; article.  Sounds logical, doesn't it?
5011       (if (not (listp (cdr read)))
5012           (setq first (max (car active) (1+ (cdr read))))
5013         ;; `read' is a list of ranges.
5014         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5015                                   (caar read)))
5016                   1)
5017           (setq first (car active)))
5018         (while read
5019           (when first
5020             (while (< first nlast)
5021               (push first unread)
5022               (setq first (1+ first))))
5023           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5024           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5025           (setq read (cdr read)))))
5026     ;; And add the last unread articles.
5027     (while (<= first last)
5028       (push first unread)
5029       (setq first (1+ first)))
5030     ;; Return the list of unread articles.
5031     (delq 0 (nreverse unread))))
5032
5033 (defun gnus-list-of-read-articles (group)
5034   "Return a list of unread, unticked and non-dormant articles."
5035   (let* ((info (gnus-get-info group))
5036          (marked (gnus-info-marks info))
5037          (active (gnus-active group)))
5038     (and info active
5039          (gnus-set-difference
5040           (gnus-sorted-complement
5041            (gnus-uncompress-range active)
5042            (gnus-list-of-unread-articles group))
5043           (append
5044            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5045            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5046
5047 ;; Various summary commands
5048
5049 (defun gnus-summary-select-article-buffer ()
5050   "Reconfigure windows to show article buffer."
5051   (interactive)
5052   (if (not (gnus-buffer-live-p gnus-article-buffer))
5053       (error "There is no article buffer for this summary buffer")
5054     (gnus-configure-windows 'article)
5055     (select-window (get-buffer-window gnus-article-buffer))))
5056
5057 (defun gnus-summary-universal-argument (arg)
5058   "Perform any operation on all articles that are process/prefixed."
5059   (interactive "P")
5060   (let ((articles (gnus-summary-work-articles arg))
5061         func article)
5062     (if (eq
5063          (setq
5064           func
5065           (key-binding
5066            (read-key-sequence
5067             (substitute-command-keys
5068              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5069              ))))
5070          'undefined)
5071         (gnus-error 1 "Undefined key")
5072       (save-excursion
5073         (while articles
5074           (gnus-summary-goto-subject (setq article (pop articles)))
5075           (let (gnus-newsgroup-processable)
5076             (command-execute func))
5077           (gnus-summary-remove-process-mark article)))))
5078   (gnus-summary-position-point))
5079
5080 (defun gnus-summary-toggle-truncation (&optional arg)
5081   "Toggle truncation of summary lines.
5082 With arg, turn line truncation on iff arg is positive."
5083   (interactive "P")
5084   (setq truncate-lines
5085         (if (null arg) (not truncate-lines)
5086           (> (prefix-numeric-value arg) 0)))
5087   (redraw-display))
5088
5089 (defun gnus-summary-reselect-current-group (&optional all rescan)
5090   "Exit and then reselect the current newsgroup.
5091 The prefix argument ALL means to select all articles."
5092   (interactive "P")
5093   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5094     (error "Ephemeral groups can't be reselected"))
5095   (let ((current-subject (gnus-summary-article-number))
5096         (group gnus-newsgroup-name))
5097     (setq gnus-newsgroup-begin nil)
5098     (gnus-summary-exit)
5099     ;; We have to adjust the point of group mode buffer because
5100     ;; point was moved to the next unread newsgroup by exiting.
5101     (gnus-summary-jump-to-group group)
5102     (when rescan
5103       (save-excursion
5104         (gnus-group-get-new-news-this-group 1)))
5105     (gnus-group-read-group all t)
5106     (gnus-summary-goto-subject current-subject nil t)))
5107
5108 (defun gnus-summary-rescan-group (&optional all)
5109   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5110   (interactive "P")
5111   (gnus-summary-reselect-current-group all t))
5112
5113 (defun gnus-summary-update-info (&optional non-destructive)
5114   (save-excursion
5115     (let ((group gnus-newsgroup-name))
5116       (when group
5117         (when gnus-newsgroup-kill-headers
5118           (setq gnus-newsgroup-killed
5119                 (gnus-compress-sequence
5120                  (nconc
5121                   (gnus-set-sorted-intersection
5122                    (gnus-uncompress-range gnus-newsgroup-killed)
5123                    (setq gnus-newsgroup-unselected
5124                          (sort gnus-newsgroup-unselected '<)))
5125                   (setq gnus-newsgroup-unreads
5126                         (sort gnus-newsgroup-unreads '<)))
5127                  t)))
5128         (unless (listp (cdr gnus-newsgroup-killed))
5129           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5130         (let ((headers gnus-newsgroup-headers))
5131           ;; Set the new ranges of read articles.
5132           (save-excursion
5133             (set-buffer gnus-group-buffer)
5134             (gnus-undo-force-boundary))
5135           (gnus-update-read-articles
5136            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5137           ;; Set the current article marks.
5138           (let ((gnus-newsgroup-scored
5139                  (if (and (not gnus-save-score)
5140                           (not non-destructive))
5141                      nil
5142                    gnus-newsgroup-scored)))
5143             (save-excursion
5144               (gnus-update-marks)))
5145           ;; Do the cross-ref thing.
5146           (when gnus-use-cross-reference
5147             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5148           ;; Do not switch windows but change the buffer to work.
5149           (set-buffer gnus-group-buffer)
5150           (unless (gnus-ephemeral-group-p group)
5151             (gnus-group-update-group group)))))))
5152
5153 (defun gnus-summary-save-newsrc (&optional force)
5154   "Save the current number of read/marked articles in the dribble buffer.
5155 The dribble buffer will then be saved.
5156 If FORCE (the prefix), also save the .newsrc file(s)."
5157   (interactive "P")
5158   (gnus-summary-update-info t)
5159   (if force
5160       (gnus-save-newsrc-file)
5161     (gnus-dribble-save)))
5162
5163 (defun gnus-summary-exit (&optional temporary)
5164   "Exit reading current newsgroup, and then return to group selection mode.
5165 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5166   (interactive)
5167   (gnus-set-global-variables)
5168   (gnus-kill-save-kill-buffer)
5169   (gnus-async-halt-prefetch)
5170   (let* ((group gnus-newsgroup-name)
5171          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5172          (mode major-mode)
5173          (group-point nil)
5174          (buf (current-buffer)))
5175     (unless quit-config
5176       ;; Do adaptive scoring, and possibly save score files.
5177       (when gnus-newsgroup-adaptive
5178         (gnus-score-adaptive))
5179       (when gnus-use-scoring
5180         (gnus-score-save)))
5181     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5182     ;; If we have several article buffers, we kill them at exit.
5183     (unless gnus-single-article-buffer
5184       (gnus-kill-buffer gnus-original-article-buffer)
5185       (setq gnus-article-current nil))
5186     (when gnus-use-cache
5187       (gnus-cache-possibly-remove-articles)
5188       (gnus-cache-save-buffers))
5189     (gnus-async-prefetch-remove-group group)
5190     (when gnus-suppress-duplicates
5191       (gnus-dup-enter-articles))
5192     (when gnus-use-trees
5193       (gnus-tree-close group))
5194     ;; Remove entries for this group.
5195     (nnmail-purge-split-history (gnus-group-real-name group))
5196     ;; Make all changes in this group permanent.
5197     (unless quit-config
5198       (gnus-run-hooks 'gnus-exit-group-hook)
5199       (gnus-summary-update-info))
5200     (gnus-close-group group)
5201     ;; Make sure where we were, and go to next newsgroup.
5202     (set-buffer gnus-group-buffer)
5203     (unless quit-config
5204       (gnus-group-jump-to-group group))
5205     (gnus-run-hooks 'gnus-summary-exit-hook)
5206     (unless (or quit-config
5207                 ;; If this group has disappeared from the summary
5208                 ;; buffer, don't skip forwards.
5209                 (not (string= group (gnus-group-group-name))))
5210       (gnus-group-next-unread-group 1))
5211     (setq group-point (point))
5212     (if temporary
5213         nil                             ;Nothing to do.
5214       ;; If we have several article buffers, we kill them at exit.
5215       (unless gnus-single-article-buffer
5216         (gnus-kill-buffer gnus-article-buffer)
5217         (gnus-kill-buffer gnus-original-article-buffer)
5218         (setq gnus-article-current nil))
5219       (set-buffer buf)
5220       (if (not gnus-kill-summary-on-exit)
5221           (gnus-deaden-summary)
5222         ;; We set all buffer-local variables to nil.  It is unclear why
5223         ;; this is needed, but if we don't, buffer-local variables are
5224         ;; not garbage-collected, it seems.  This would the lead to en
5225         ;; ever-growing Emacs.
5226         (gnus-summary-clear-local-variables)
5227         (when (get-buffer gnus-article-buffer)
5228           (bury-buffer gnus-article-buffer))
5229         ;; We clear the global counterparts of the buffer-local
5230         ;; variables as well, just to be on the safe side.
5231         (set-buffer gnus-group-buffer)
5232         (gnus-summary-clear-local-variables)
5233         ;; Return to group mode buffer.
5234         (when (eq mode 'gnus-summary-mode)
5235           (gnus-kill-buffer buf)))
5236       (setq gnus-current-select-method gnus-select-method)
5237       (pop-to-buffer gnus-group-buffer)
5238       (if (not quit-config)
5239           (progn
5240             (goto-char group-point)
5241             (gnus-configure-windows 'group 'force))
5242         (gnus-handle-ephemeral-exit quit-config))
5243       ;; Clear the current group name.
5244       (unless quit-config
5245         (setq gnus-newsgroup-name nil)))))
5246
5247 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5248 (defun gnus-summary-exit-no-update (&optional no-questions)
5249   "Quit reading current newsgroup without updating read article info."
5250   (interactive)
5251   (let* ((group gnus-newsgroup-name)
5252          (quit-config (gnus-group-quit-config group)))
5253     (when (or no-questions
5254               gnus-expert-user
5255               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5256       (gnus-async-halt-prefetch)
5257       (mapcar 'funcall
5258               (delq 'gnus-summary-expire-articles
5259                     (copy-list gnus-summary-prepare-exit-hook)))
5260       ;; If we have several article buffers, we kill them at exit.
5261       (unless gnus-single-article-buffer
5262         (gnus-kill-buffer gnus-article-buffer)
5263         (gnus-kill-buffer gnus-original-article-buffer)
5264         (setq gnus-article-current nil))
5265       (if (not gnus-kill-summary-on-exit)
5266           (gnus-deaden-summary)
5267         (gnus-close-group group)
5268         (gnus-summary-clear-local-variables)
5269         (set-buffer gnus-group-buffer)
5270         (gnus-summary-clear-local-variables)
5271         (when (get-buffer gnus-summary-buffer)
5272           (kill-buffer gnus-summary-buffer)))
5273       (unless gnus-single-article-buffer
5274         (setq gnus-article-current nil))
5275       (when gnus-use-trees
5276         (gnus-tree-close group))
5277       (gnus-async-prefetch-remove-group group)
5278       (when (get-buffer gnus-article-buffer)
5279         (bury-buffer gnus-article-buffer))
5280       ;; Return to the group buffer.
5281       (gnus-configure-windows 'group 'force)
5282       ;; Clear the current group name.
5283       (setq gnus-newsgroup-name nil)
5284       (when (equal (gnus-group-group-name) group)
5285         (gnus-group-next-unread-group 1))
5286       (when quit-config
5287         (gnus-handle-ephemeral-exit quit-config)))))
5288
5289 (defun gnus-handle-ephemeral-exit (quit-config)
5290   "Handle movement when leaving an ephemeral group.
5291 The state which existed when entering the ephemeral is reset."
5292   (if (not (buffer-name (car quit-config)))
5293       (gnus-configure-windows 'group 'force)
5294     (set-buffer (car quit-config))
5295     (cond ((eq major-mode 'gnus-summary-mode)
5296            (gnus-set-global-variables))
5297           ((eq major-mode 'gnus-article-mode)
5298            (save-excursion
5299              ;; The `gnus-summary-buffer' variable may point
5300              ;; to the old summary buffer when using a single
5301              ;; article buffer.
5302              (unless (gnus-buffer-live-p gnus-summary-buffer)
5303                (set-buffer gnus-group-buffer))
5304              (set-buffer gnus-summary-buffer)
5305              (gnus-set-global-variables))))
5306     (if (or (eq (cdr quit-config) 'article)
5307             (eq (cdr quit-config) 'pick))
5308         (progn
5309           ;; The current article may be from the ephemeral group
5310           ;; thus it is best that we reload this article
5311           (gnus-summary-show-article)
5312           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5313               (gnus-configure-windows 'pick 'force)
5314             (gnus-configure-windows (cdr quit-config) 'force)))
5315       (gnus-configure-windows (cdr quit-config) 'force))
5316     (when (eq major-mode 'gnus-summary-mode)
5317       (gnus-summary-next-subject 1 nil t)
5318       (gnus-summary-recenter)
5319       (gnus-summary-position-point))))
5320
5321 (defun gnus-summary-preview-mime-message ()
5322   "MIME decode and play this message."
5323   (interactive)
5324   (let ((gnus-break-pages nil)
5325         (gnus-show-mime t))
5326     (gnus-summary-select-article gnus-show-all-headers t))
5327   (select-window (get-buffer-window gnus-article-buffer)))
5328
5329 ;;; Dead summaries.
5330
5331 (defvar gnus-dead-summary-mode-map nil)
5332
5333 (unless gnus-dead-summary-mode-map
5334   (setq gnus-dead-summary-mode-map (make-keymap))
5335   (suppress-keymap gnus-dead-summary-mode-map)
5336   (substitute-key-definition
5337    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5338   (let ((keys '("\C-d" "\r" "\177" [delete])))
5339     (while keys
5340       (define-key gnus-dead-summary-mode-map
5341         (pop keys) 'gnus-summary-wake-up-the-dead))))
5342
5343 (defvar gnus-dead-summary-mode nil
5344   "Minor mode for Gnus summary buffers.")
5345
5346 (defun gnus-dead-summary-mode (&optional arg)
5347   "Minor mode for Gnus summary buffers."
5348   (interactive "P")
5349   (when (eq major-mode 'gnus-summary-mode)
5350     (make-local-variable 'gnus-dead-summary-mode)
5351     (setq gnus-dead-summary-mode
5352           (if (null arg) (not gnus-dead-summary-mode)
5353             (> (prefix-numeric-value arg) 0)))
5354     (when gnus-dead-summary-mode
5355       (gnus-add-minor-mode
5356        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5357
5358 (defun gnus-deaden-summary ()
5359   "Make the current summary buffer into a dead summary buffer."
5360   ;; Kill any previous dead summary buffer.
5361   (when (and gnus-dead-summary
5362              (buffer-name gnus-dead-summary))
5363     (save-excursion
5364       (set-buffer gnus-dead-summary)
5365       (when gnus-dead-summary-mode
5366         (kill-buffer (current-buffer)))))
5367   ;; Make this the current dead summary.
5368   (setq gnus-dead-summary (current-buffer))
5369   (gnus-dead-summary-mode 1)
5370   (let ((name (buffer-name)))
5371     (when (string-match "Summary" name)
5372       (rename-buffer
5373        (concat (substring name 0 (match-beginning 0)) "Dead "
5374                (substring name (match-beginning 0)))
5375        t))))
5376
5377 (defun gnus-kill-or-deaden-summary (buffer)
5378   "Kill or deaden the summary BUFFER."
5379   (save-excursion
5380     (when (and (buffer-name buffer)
5381                (not gnus-single-article-buffer))
5382       (save-excursion
5383         (set-buffer buffer)
5384         (gnus-kill-buffer gnus-article-buffer)
5385         (gnus-kill-buffer gnus-original-article-buffer)))
5386     (cond (gnus-kill-summary-on-exit
5387            (when (and gnus-use-trees
5388                       (gnus-buffer-exists-p buffer))
5389              (save-excursion
5390                (set-buffer buffer)
5391                (gnus-tree-close gnus-newsgroup-name)))
5392            (gnus-kill-buffer buffer))
5393           ((gnus-buffer-exists-p buffer)
5394            (save-excursion
5395              (set-buffer buffer)
5396              (gnus-deaden-summary))))))
5397
5398 (defun gnus-summary-wake-up-the-dead (&rest args)
5399   "Wake up the dead summary buffer."
5400   (interactive)
5401   (gnus-dead-summary-mode -1)
5402   (let ((name (buffer-name)))
5403     (when (string-match "Dead " name)
5404       (rename-buffer
5405        (concat (substring name 0 (match-beginning 0))
5406                (substring name (match-end 0)))
5407        t)))
5408   (gnus-message 3 "This dead summary is now alive again"))
5409
5410 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5411 (defun gnus-summary-fetch-faq (&optional faq-dir)
5412   "Fetch the FAQ for the current group.
5413 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5414 in."
5415   (interactive
5416    (list
5417     (when current-prefix-arg
5418       (completing-read
5419        "Faq dir: " (and (listp gnus-group-faq-directory)
5420                         (mapcar (lambda (file) (list file))
5421                                 gnus-group-faq-directory))))))
5422   (let (gnus-faq-buffer)
5423     (when (setq gnus-faq-buffer
5424                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5425       (gnus-configure-windows 'summary-faq))))
5426
5427 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5428 (defun gnus-summary-describe-group (&optional force)
5429   "Describe the current newsgroup."
5430   (interactive "P")
5431   (gnus-group-describe-group force gnus-newsgroup-name))
5432
5433 (defun gnus-summary-describe-briefly ()
5434   "Describe summary mode commands briefly."
5435   (interactive)
5436   (gnus-message 6 (substitute-command-keys "\\<gnus-summary-mode-map>\\[gnus-summary-next-page]:Select  \\[gnus-summary-next-unread-article]:Forward  \\[gnus-summary-prev-unread-article]:Backward  \\[gnus-summary-exit]:Exit  \\[gnus-info-find-node]:Run Info        \\[gnus-summary-describe-briefly]:This help")))
5437
5438 ;; Walking around group mode buffer from summary mode.
5439
5440 (defun gnus-summary-next-group (&optional no-article target-group backward)
5441   "Exit current newsgroup and then select next unread newsgroup.
5442 If prefix argument NO-ARTICLE is non-nil, no article is selected
5443 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5444 previous group instead."
5445   (interactive "P")
5446   ;; Stop pre-fetching.
5447   (gnus-async-halt-prefetch)
5448   (let ((current-group gnus-newsgroup-name)
5449         (current-buffer (current-buffer))
5450         entered)
5451     ;; First we semi-exit this group to update Xrefs and all variables.
5452     ;; We can't do a real exit, because the window conf must remain
5453     ;; the same in case the user is prompted for info, and we don't
5454     ;; want the window conf to change before that...
5455     (gnus-summary-exit t)
5456     (while (not entered)
5457       ;; Then we find what group we are supposed to enter.
5458       (set-buffer gnus-group-buffer)
5459       (gnus-group-jump-to-group current-group)
5460       (setq target-group
5461             (or target-group
5462                 (if (eq gnus-keep-same-level 'best)
5463                     (gnus-summary-best-group gnus-newsgroup-name)
5464                   (gnus-summary-search-group backward gnus-keep-same-level))))
5465       (if (not target-group)
5466           ;; There are no further groups, so we return to the group
5467           ;; buffer.
5468           (progn
5469             (gnus-message 5 "Returning to the group buffer")
5470             (setq entered t)
5471             (when (gnus-buffer-live-p current-buffer)
5472               (set-buffer current-buffer)
5473               (gnus-summary-exit))
5474             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5475         ;; We try to enter the target group.
5476         (gnus-group-jump-to-group target-group)
5477         (let ((unreads (gnus-group-group-unread)))
5478           (if (and (or (eq t unreads)
5479                        (and unreads (not (zerop unreads))))
5480                    (gnus-summary-read-group
5481                     target-group nil no-article
5482                     (and (buffer-name current-buffer) current-buffer)
5483                     nil backward))
5484               (setq entered t)
5485             (setq current-group target-group
5486                   target-group nil)))))))
5487
5488 (defun gnus-summary-prev-group (&optional no-article)
5489   "Exit current newsgroup and then select previous unread newsgroup.
5490 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5491   (interactive "P")
5492   (gnus-summary-next-group no-article nil t))
5493
5494 ;; Walking around summary lines.
5495
5496 (defun gnus-summary-first-subject (&optional unread undownloaded)
5497   "Go to the first unread subject.
5498 If UNREAD is non-nil, go to the first unread article.
5499 Returns the article selected or nil if there are no unread articles."
5500   (interactive "P")
5501   (prog1
5502       (cond
5503        ;; Empty summary.
5504        ((null gnus-newsgroup-data)
5505         (gnus-message 3 "No articles in the group")
5506         nil)
5507        ;; Pick the first article.
5508        ((not unread)
5509         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5510         (gnus-data-number (car gnus-newsgroup-data)))
5511        ;; No unread articles.
5512        ((null gnus-newsgroup-unreads)
5513         (gnus-message 3 "No more unread articles")
5514         nil)
5515        ;; Find the first unread article.
5516        (t
5517         (let ((data gnus-newsgroup-data))
5518           (while (and data
5519                       (and (not (and undownloaded
5520                                      (eq gnus-undownloaded-mark
5521                                          (gnus-data-mark (car data)))))
5522                            (not (gnus-data-unread-p (car data)))))
5523             (setq data (cdr data)))
5524           (when data
5525             (goto-char (gnus-data-pos (car data)))
5526             (gnus-data-number (car data))))))
5527     (gnus-summary-position-point)))
5528
5529 (defun gnus-summary-next-subject (n &optional unread dont-display)
5530   "Go to next N'th summary line.
5531 If N is negative, go to the previous N'th subject line.
5532 If UNREAD is non-nil, only unread articles are selected.
5533 The difference between N and the actual number of steps taken is
5534 returned."
5535   (interactive "p")
5536   (let ((backward (< n 0))
5537         (n (abs n)))
5538     (while (and (> n 0)
5539                 (if backward
5540                     (gnus-summary-find-prev unread)
5541                   (gnus-summary-find-next unread)))
5542       (gnus-summary-show-thread)
5543       (setq n (1- n)))
5544     (when (/= 0 n)
5545       (gnus-message 7 "No more%s articles"
5546                     (if unread " unread" "")))
5547     (unless dont-display
5548       (gnus-summary-recenter)
5549       (gnus-summary-position-point))
5550     n))
5551
5552 (defun gnus-summary-next-unread-subject (n)
5553   "Go to next N'th unread summary line."
5554   (interactive "p")
5555   (gnus-summary-next-subject n t))
5556
5557 (defun gnus-summary-prev-subject (n &optional unread)
5558   "Go to previous N'th summary line.
5559 If optional argument UNREAD is non-nil, only unread article is selected."
5560   (interactive "p")
5561   (gnus-summary-next-subject (- n) unread))
5562
5563 (defun gnus-summary-prev-unread-subject (n)
5564   "Go to previous N'th unread summary line."
5565   (interactive "p")
5566   (gnus-summary-next-subject (- n) t))
5567
5568 (defun gnus-summary-goto-subject (article &optional force silent)
5569   "Go the subject line of ARTICLE.
5570 If FORCE, also allow jumping to articles not currently shown."
5571   (interactive "nArticle number: ")
5572   (let ((b (point))
5573         (data (gnus-data-find article)))
5574     ;; We read in the article if we have to.
5575     (and (not data)
5576          force
5577          (gnus-summary-insert-subject
5578           article
5579           (if (or (numberp force) (vectorp force)) force)
5580           t)
5581          (setq data (gnus-data-find article)))
5582     (goto-char b)
5583     (if (not data)
5584         (progn
5585           (unless silent
5586             (gnus-message 3 "Can't find article %d" article))
5587           nil)
5588       (goto-char (gnus-data-pos data))
5589       (gnus-summary-position-point)
5590       article)))
5591
5592 ;; Walking around summary lines with displaying articles.
5593
5594 (defun gnus-summary-expand-window (&optional arg)
5595   "Make the summary buffer take up the entire Emacs frame.
5596 Given a prefix, will force an `article' buffer configuration."
5597   (interactive "P")
5598   (if arg
5599       (gnus-configure-windows 'article 'force)
5600     (gnus-configure-windows 'summary 'force)))
5601
5602 (defun gnus-summary-display-article (article &optional all-header)
5603   "Display ARTICLE in article buffer."
5604   (gnus-set-global-variables)
5605   (if (null article)
5606       nil
5607     (prog1
5608         (if gnus-summary-display-article-function
5609             (funcall gnus-summary-display-article-function article all-header)
5610           (gnus-article-prepare article all-header))
5611       (gnus-run-hooks 'gnus-select-article-hook)
5612       (when (and gnus-current-article
5613                  (not (zerop gnus-current-article)))
5614         (gnus-summary-goto-subject gnus-current-article))
5615       (gnus-summary-recenter)
5616       (when (and gnus-use-trees gnus-show-threads)
5617         (gnus-possibly-generate-tree article)
5618         (gnus-highlight-selected-tree article))
5619       ;; Successfully display article.
5620       (gnus-article-set-window-start
5621        (cdr (assq article gnus-newsgroup-bookmarks))))))
5622
5623 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5624   "Select the current article.
5625 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5626 non-nil, the article will be re-fetched even if it already present in
5627 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5628 be displayed."
5629   ;; Make sure we are in the summary buffer to work around bbdb bug.
5630   (unless (eq major-mode 'gnus-summary-mode)
5631     (set-buffer gnus-summary-buffer))
5632   (let ((article (or article (gnus-summary-article-number)))
5633         (all-headers (not (not all-headers))) ;Must be T or NIL.
5634         gnus-summary-display-article-function
5635         did)
5636     (and (not pseudo)
5637          (gnus-summary-article-pseudo-p article)
5638          (error "This is a pseudo-article"))
5639     (prog1
5640         (save-excursion
5641           (set-buffer gnus-summary-buffer)
5642           (if (or (and gnus-single-article-buffer
5643                        (or (null gnus-current-article)
5644                            (null gnus-article-current)
5645                            (null (get-buffer gnus-article-buffer))
5646                            (not (eq article (cdr gnus-article-current)))
5647                            (not (equal (car gnus-article-current)
5648                                        gnus-newsgroup-name))))
5649                   (and (not gnus-single-article-buffer)
5650                        (or (null gnus-current-article)
5651                            (not (eq gnus-current-article article))))
5652                   force)
5653               ;; The requested article is different from the current article.
5654               (prog1
5655                   (gnus-summary-display-article article all-headers)
5656                 (setq did article)
5657                 (when (or all-headers gnus-show-all-headers)
5658                   (gnus-article-show-all-headers)))
5659             (when (or all-headers gnus-show-all-headers)
5660               (gnus-article-show-all-headers))
5661             'old))
5662       (when did
5663         (gnus-article-set-window-start
5664          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5665
5666 (defun gnus-summary-set-current-mark (&optional current-mark)
5667   "Obsolete function."
5668   nil)
5669
5670 (defun gnus-summary-next-article (&optional unread subject backward push)
5671   "Select the next article.
5672 If UNREAD, only unread articles are selected.
5673 If SUBJECT, only articles with SUBJECT are selected.
5674 If BACKWARD, the previous article is selected instead of the next."
5675   (interactive "P")
5676   (cond
5677    ;; Is there such an article?
5678    ((and (gnus-summary-search-forward unread subject backward)
5679          (or (gnus-summary-display-article (gnus-summary-article-number))
5680              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5681     (gnus-summary-position-point))
5682    ;; If not, we try the first unread, if that is wanted.
5683    ((and subject
5684          gnus-auto-select-same
5685          (gnus-summary-first-unread-article))
5686     (gnus-summary-position-point)
5687     (gnus-message 6 "Wrapped"))
5688    ;; Try to get next/previous article not displayed in this group.
5689    ((and gnus-auto-extend-newsgroup
5690          (not unread) (not subject))
5691     (gnus-summary-goto-article
5692      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5693      nil (count-lines (point-min) (point))))
5694    ;; Go to next/previous group.
5695    (t
5696     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5697       (gnus-summary-jump-to-group gnus-newsgroup-name))
5698     (let ((cmd last-command-char)
5699           (point
5700            (save-excursion
5701              (set-buffer gnus-group-buffer)
5702              (point)))
5703           (group
5704            (if (eq gnus-keep-same-level 'best)
5705                (gnus-summary-best-group gnus-newsgroup-name)
5706              (gnus-summary-search-group backward gnus-keep-same-level))))
5707       ;; For some reason, the group window gets selected.  We change
5708       ;; it back.
5709       (select-window (get-buffer-window (current-buffer)))
5710       ;; Select next unread newsgroup automagically.
5711       (cond
5712        ((or (not gnus-auto-select-next)
5713             (not cmd))
5714         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5715        ((or (eq gnus-auto-select-next 'quietly)
5716             (and (eq gnus-auto-select-next 'slightly-quietly)
5717                  push)
5718             (and (eq gnus-auto-select-next 'almost-quietly)
5719                  (gnus-summary-last-article-p)))
5720         ;; Select quietly.
5721         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5722             (gnus-summary-exit)
5723           (gnus-message 7 "No more%s articles (%s)..."
5724                         (if unread " unread" "")
5725                         (if group (concat "selecting " group)
5726                           "exiting"))
5727           (gnus-summary-next-group nil group backward)))
5728        (t
5729         (when (gnus-key-press-event-p last-input-event)
5730           (gnus-summary-walk-group-buffer
5731            gnus-newsgroup-name cmd unread backward point))))))))
5732
5733 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5734   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5735                       (?\C-p (gnus-group-prev-unread-group 1))))
5736         (cursor-in-echo-area t)
5737         keve key group ended)
5738     (save-excursion
5739       (set-buffer gnus-group-buffer)
5740       (goto-char start)
5741       (setq group
5742             (if (eq gnus-keep-same-level 'best)
5743                 (gnus-summary-best-group gnus-newsgroup-name)
5744               (gnus-summary-search-group backward gnus-keep-same-level))))
5745     (while (not ended)
5746       (gnus-message
5747        5 "No more%s articles%s" (if unread " unread" "")
5748        (if (and group
5749                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5750            (format " (Type %s for %s [%s])"
5751                    (single-key-description cmd) group
5752                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5753          (format " (Type %s to exit %s)"
5754                  (single-key-description cmd)
5755                  gnus-newsgroup-name)))
5756       ;; Confirm auto selection.
5757       (setq key (car (setq keve (gnus-read-event-char))))
5758       (setq ended t)
5759       (cond
5760        ((assq key keystrokes)
5761         (let ((obuf (current-buffer)))
5762           (switch-to-buffer gnus-group-buffer)
5763           (when group
5764             (gnus-group-jump-to-group group))
5765           (eval (cadr (assq key keystrokes)))
5766           (setq group (gnus-group-group-name))
5767           (switch-to-buffer obuf))
5768         (setq ended nil))
5769        ((equal key cmd)
5770         (if (or (not group)
5771                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5772             (gnus-summary-exit)
5773           (gnus-summary-next-group nil group backward)))
5774        (t
5775         (push (cdr keve) unread-command-events))))))
5776
5777 (defun gnus-summary-next-unread-article ()
5778   "Select unread article after current one."
5779   (interactive)
5780   (gnus-summary-next-article
5781    (or (not (eq gnus-summary-goto-unread 'never))
5782        (gnus-summary-last-article-p (gnus-summary-article-number)))
5783    (and gnus-auto-select-same
5784         (gnus-summary-article-subject))))
5785
5786 (defun gnus-summary-prev-article (&optional unread subject)
5787   "Select the article after the current one.
5788 If UNREAD is non-nil, only unread articles are selected."
5789   (interactive "P")
5790   (gnus-summary-next-article unread subject t))
5791
5792 (defun gnus-summary-prev-unread-article ()
5793   "Select unread article before current one."
5794   (interactive)
5795   (gnus-summary-prev-article
5796    (or (not (eq gnus-summary-goto-unread 'never))
5797        (gnus-summary-first-article-p (gnus-summary-article-number)))
5798    (and gnus-auto-select-same
5799         (gnus-summary-article-subject))))
5800
5801 (defun gnus-summary-next-page (&optional lines circular)
5802   "Show next page of the selected article.
5803 If at the end of the current article, select the next article.
5804 LINES says how many lines should be scrolled up.
5805
5806 If CIRCULAR is non-nil, go to the start of the article instead of
5807 selecting the next article when reaching the end of the current
5808 article."
5809   (interactive "P")
5810   (setq gnus-summary-buffer (current-buffer))
5811   (gnus-set-global-variables)
5812   (let ((article (gnus-summary-article-number))
5813         (article-window (get-buffer-window gnus-article-buffer t))
5814         endp)
5815     ;; If the buffer is empty, we have no article.
5816     (unless article
5817       (error "No article to select"))
5818     (gnus-configure-windows 'article)
5819     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5820         (if (and (eq gnus-summary-goto-unread 'never)
5821                  (not (gnus-summary-last-article-p article)))
5822             (gnus-summary-next-article)
5823           (gnus-summary-next-unread-article))
5824       (if (or (null gnus-current-article)
5825               (null gnus-article-current)
5826               (/= article (cdr gnus-article-current))
5827               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5828           ;; Selected subject is different from current article's.
5829           (gnus-summary-display-article article)
5830         (when article-window
5831           (gnus-eval-in-buffer-window gnus-article-buffer
5832             (setq endp (gnus-article-next-page lines)))
5833           (when endp
5834             (cond (circular
5835                    (gnus-summary-beginning-of-article))
5836                   (lines
5837                    (gnus-message 3 "End of message"))
5838                   ((null lines)
5839                    (if (and (eq gnus-summary-goto-unread 'never)
5840                             (not (gnus-summary-last-article-p article)))
5841                        (gnus-summary-next-article)
5842                      (gnus-summary-next-unread-article))))))))
5843     (gnus-summary-recenter)
5844     (gnus-summary-position-point)))
5845
5846 (defun gnus-summary-prev-page (&optional lines move)
5847   "Show previous page of selected article.
5848 Argument LINES specifies lines to be scrolled down.
5849 If MOVE, move to the previous unread article if point is at
5850 the beginning of the buffer."
5851   (interactive "P")
5852   (let ((article (gnus-summary-article-number))
5853         (article-window (get-buffer-window gnus-article-buffer t))
5854         endp)
5855     (gnus-configure-windows 'article)
5856     (if (or (null gnus-current-article)
5857             (null gnus-article-current)
5858             (/= article (cdr gnus-article-current))
5859             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5860         ;; Selected subject is different from current article's.
5861         (gnus-summary-display-article article)
5862       (gnus-summary-recenter)
5863       (when article-window
5864         (gnus-eval-in-buffer-window gnus-article-buffer
5865           (setq endp (gnus-article-prev-page lines)))
5866         (when (and move endp)
5867           (cond (lines
5868                  (gnus-message 3 "Beginning of message"))
5869                 ((null lines)
5870                  (if (and (eq gnus-summary-goto-unread 'never)
5871                           (not (gnus-summary-first-article-p article)))
5872                      (gnus-summary-prev-article)
5873                    (gnus-summary-prev-unread-article))))))))
5874   (gnus-summary-position-point))
5875
5876 (defun gnus-summary-prev-page-or-article (&optional lines)
5877   "Show previous page of selected article.
5878 Argument LINES specifies lines to be scrolled down.
5879 If at the beginning of the article, go to the next article."
5880   (interactive "P")
5881   (gnus-summary-prev-page lines t))
5882
5883 (defun gnus-summary-scroll-up (lines)
5884   "Scroll up (or down) one line current article.
5885 Argument LINES specifies lines to be scrolled up (or down if negative)."
5886   (interactive "p")
5887   (gnus-configure-windows 'article)
5888   (gnus-summary-show-thread)
5889   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5890     (gnus-eval-in-buffer-window gnus-article-buffer
5891       (cond ((> lines 0)
5892              (when (gnus-article-next-page lines)
5893                (gnus-message 3 "End of message")))
5894             ((< lines 0)
5895              (gnus-article-prev-page (- lines))))))
5896   (gnus-summary-recenter)
5897   (gnus-summary-position-point))
5898
5899 (defun gnus-summary-scroll-down (lines)
5900   "Scroll down (or up) one line current article.
5901 Argument LINES specifies lines to be scrolled down (or up if negative)."
5902   (interactive "p")
5903   (gnus-summary-scroll-up (- lines)))
5904
5905 (defun gnus-summary-next-same-subject ()
5906   "Select next article which has the same subject as current one."
5907   (interactive)
5908   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5909
5910 (defun gnus-summary-prev-same-subject ()
5911   "Select previous article which has the same subject as current one."
5912   (interactive)
5913   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5914
5915 (defun gnus-summary-next-unread-same-subject ()
5916   "Select next unread article which has the same subject as current one."
5917   (interactive)
5918   (gnus-summary-next-article t (gnus-summary-article-subject)))
5919
5920 (defun gnus-summary-prev-unread-same-subject ()
5921   "Select previous unread article which has the same subject as current one."
5922   (interactive)
5923   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5924
5925 (defun gnus-summary-first-unread-article ()
5926   "Select the first unread article.
5927 Return nil if there are no unread articles."
5928   (interactive)
5929   (prog1
5930       (when (gnus-summary-first-subject t)
5931         (gnus-summary-show-thread)
5932         (gnus-summary-first-subject t)
5933         (gnus-summary-display-article (gnus-summary-article-number)))
5934     (gnus-summary-position-point)))
5935
5936 (defun gnus-summary-first-unread-subject ()
5937   "Place the point on the subject line of the first unread article.
5938 Return nil if there are no unread articles."
5939   (interactive)
5940   (prog1
5941       (when (gnus-summary-first-subject t)
5942         (gnus-summary-show-thread)
5943         (gnus-summary-first-subject t))
5944     (gnus-summary-position-point)))
5945
5946 (defun gnus-summary-first-article ()
5947   "Select the first article.
5948 Return nil if there are no articles."
5949   (interactive)
5950   (prog1
5951       (when (gnus-summary-first-subject)
5952         (gnus-summary-show-thread)
5953         (gnus-summary-first-subject)
5954         (gnus-summary-display-article (gnus-summary-article-number)))
5955     (gnus-summary-position-point)))
5956
5957 (defun gnus-summary-best-unread-article ()
5958   "Select the unread article with the highest score."
5959   (interactive)
5960   (let ((best -1000000)
5961         (data gnus-newsgroup-data)
5962         article score)
5963     (while data
5964       (and (gnus-data-unread-p (car data))
5965            (> (setq score
5966                     (gnus-summary-article-score (gnus-data-number (car data))))
5967               best)
5968            (setq best score
5969                  article (gnus-data-number (car data))))
5970       (setq data (cdr data)))
5971     (prog1
5972         (if article
5973             (gnus-summary-goto-article article)
5974           (error "No unread articles"))
5975       (gnus-summary-position-point))))
5976
5977 (defun gnus-summary-last-subject ()
5978   "Go to the last displayed subject line in the group."
5979   (let ((article (gnus-data-number (car (gnus-data-list t)))))
5980     (when article
5981       (gnus-summary-goto-subject article))))
5982
5983 (defun gnus-summary-goto-article (article &optional all-headers force)
5984   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
5985 If ALL-HEADERS is non-nil, no header lines are hidden.
5986 If FORCE, go to the article even if it isn't displayed.  If FORCE
5987 is a number, it is the line the article is to be displayed on."
5988   (interactive
5989    (list
5990     (completing-read
5991      "Article number or Message-ID: "
5992      (mapcar (lambda (number) (list (int-to-string number)))
5993              gnus-newsgroup-limit))
5994     current-prefix-arg
5995     t))
5996   (prog1
5997       (if (and (stringp article)
5998                (string-match "@" article))
5999           (gnus-summary-refer-article article)
6000         (when (stringp article)
6001           (setq article (string-to-number article)))
6002         (if (gnus-summary-goto-subject article force)
6003             (gnus-summary-display-article article all-headers)
6004           (gnus-message 4 "Couldn't go to article %s" article) nil))
6005     (gnus-summary-position-point)))
6006
6007 (defun gnus-summary-goto-last-article ()
6008   "Go to the previously read article."
6009   (interactive)
6010   (prog1
6011       (when gnus-last-article
6012         (gnus-summary-goto-article gnus-last-article nil t))
6013     (gnus-summary-position-point)))
6014
6015 (defun gnus-summary-pop-article (number)
6016   "Pop one article off the history and go to the previous.
6017 NUMBER articles will be popped off."
6018   (interactive "p")
6019   (let (to)
6020     (setq gnus-newsgroup-history
6021           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6022     (if to
6023         (gnus-summary-goto-article (car to) nil t)
6024       (error "Article history empty")))
6025   (gnus-summary-position-point))
6026
6027 ;; Summary commands and functions for limiting the summary buffer.
6028
6029 (defun gnus-summary-limit-to-articles (n)
6030   "Limit the summary buffer to the next N articles.
6031 If not given a prefix, use the process marked articles instead."
6032   (interactive "P")
6033   (prog1
6034       (let ((articles (gnus-summary-work-articles n)))
6035         (setq gnus-newsgroup-processable nil)
6036         (gnus-summary-limit articles))
6037     (gnus-summary-position-point)))
6038
6039 (defun gnus-summary-pop-limit (&optional total)
6040   "Restore the previous limit.
6041 If given a prefix, remove all limits."
6042   (interactive "P")
6043   (when total
6044     (setq gnus-newsgroup-limits
6045           (list (mapcar (lambda (h) (mail-header-number h))
6046                         gnus-newsgroup-headers))))
6047   (unless gnus-newsgroup-limits
6048     (error "No limit to pop"))
6049   (prog1
6050       (gnus-summary-limit nil 'pop)
6051     (gnus-summary-position-point)))
6052
6053 (defun gnus-summary-limit-to-subject (subject &optional header)
6054   "Limit the summary buffer to articles that have subjects that match a regexp."
6055   (interactive "sLimit to subject (regexp): ")
6056   (unless header
6057     (setq header "subject"))
6058   (when (not (equal "" subject))
6059     (prog1
6060         (let ((articles (gnus-summary-find-matching
6061                          (or header "subject") subject 'all)))
6062           (unless articles
6063             (error "Found no matches for \"%s\"" subject))
6064           (gnus-summary-limit articles))
6065       (gnus-summary-position-point))))
6066
6067 (defun gnus-summary-limit-to-author (from)
6068   "Limit the summary buffer to articles that have authors that match a regexp."
6069   (interactive "sLimit to author (regexp): ")
6070   (gnus-summary-limit-to-subject from "from"))
6071
6072 (defun gnus-summary-limit-to-age (age &optional younger-p)
6073   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6074 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6075 articles that are younger than AGE days."
6076   (interactive "nTime in days: \nP")
6077   (prog1
6078       (let ((data gnus-newsgroup-data)
6079             (cutoff (days-to-time age))
6080             articles d date is-younger)
6081         (while (setq d (pop data))
6082           (when (and (vectorp (gnus-data-header d))
6083                      (setq date (mail-header-date (gnus-data-header d))))
6084             (setq is-younger (time-less-p
6085                               (time-since (date-to-time date))
6086                               cutoff))
6087             (when (if younger-p
6088                       is-younger
6089                     (not is-younger))
6090               (push (gnus-data-number d) articles))))
6091         (gnus-summary-limit (nreverse articles)))
6092     (gnus-summary-position-point)))
6093
6094 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6095 (make-obsolete
6096  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6097
6098 (defun gnus-summary-limit-to-unread (&optional all)
6099   "Limit the summary buffer to articles that are not marked as read.
6100 If ALL is non-nil, limit strictly to unread articles."
6101   (interactive "P")
6102   (if all
6103       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6104     (gnus-summary-limit-to-marks
6105      ;; Concat all the marks that say that an article is read and have
6106      ;; those removed.
6107      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6108            gnus-killed-mark gnus-kill-file-mark
6109            gnus-low-score-mark gnus-expirable-mark
6110            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6111            gnus-duplicate-mark gnus-souped-mark)
6112      'reverse)))
6113
6114 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6115 (make-obsolete 'gnus-summary-delete-marked-with
6116                'gnus-summary-limit-exlude-marks)
6117
6118 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6119   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6120 If REVERSE, limit the summary buffer to articles that are marked
6121 with MARKS.  MARKS can either be a string of marks or a list of marks.
6122 Returns how many articles were removed."
6123   (interactive "sMarks: ")
6124   (gnus-summary-limit-to-marks marks t))
6125
6126 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6127   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6128 If REVERSE (the prefix), limit the summary buffer to articles that are
6129 not marked with MARKS.  MARKS can either be a string of marks or a
6130 list of marks.
6131 Returns how many articles were removed."
6132   (interactive "sMarks: \nP")
6133   (prog1
6134       (let ((data gnus-newsgroup-data)
6135             (marks (if (listp marks) marks
6136                      (append marks nil))) ; Transform to list.
6137             articles)
6138         (while data
6139           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6140                   (memq (gnus-data-mark (car data)) marks))
6141             (push (gnus-data-number (car data)) articles))
6142           (setq data (cdr data)))
6143         (gnus-summary-limit articles))
6144     (gnus-summary-position-point)))
6145
6146 (defun gnus-summary-limit-to-score (&optional score)
6147   "Limit to articles with score at or above SCORE."
6148   (interactive "P")
6149   (setq score (if score
6150                   (prefix-numeric-value score)
6151                 (or gnus-summary-default-score 0)))
6152   (let ((data gnus-newsgroup-data)
6153         articles)
6154     (while data
6155       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6156                 score)
6157         (push (gnus-data-number (car data)) articles))
6158       (setq data (cdr data)))
6159     (prog1
6160         (gnus-summary-limit articles)
6161       (gnus-summary-position-point))))
6162
6163 (defun gnus-summary-limit-include-thread (id)
6164   "Display all the hidden articles that in the current thread."
6165   (interactive (list (mail-header-id (gnus-summary-article-header))))
6166   (let ((articles (gnus-articles-in-thread
6167                    (gnus-id-to-thread (gnus-root-id id)))))
6168     (prog1
6169         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6170       (gnus-summary-position-point))))
6171
6172 (defun gnus-summary-limit-include-dormant ()
6173   "Display all the hidden articles that are marked as dormant.
6174 Note that this command only works on a subset of the articles currently
6175 fetched for this group."
6176   (interactive)
6177   (unless gnus-newsgroup-dormant
6178     (error "There are no dormant articles in this group"))
6179   (prog1
6180       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6181     (gnus-summary-position-point)))
6182
6183 (defun gnus-summary-limit-exclude-dormant ()
6184   "Hide all dormant articles."
6185   (interactive)
6186   (prog1
6187       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6188     (gnus-summary-position-point)))
6189
6190 (defun gnus-summary-limit-exclude-childless-dormant ()
6191   "Hide all dormant articles that have no children."
6192   (interactive)
6193   (let ((data (gnus-data-list t))
6194         articles d children)
6195     ;; Find all articles that are either not dormant or have
6196     ;; children.
6197     (while (setq d (pop data))
6198       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6199                 (and (setq children
6200                            (gnus-article-children (gnus-data-number d)))
6201                      (let (found)
6202                        (while children
6203                          (when (memq (car children) articles)
6204                            (setq children nil
6205                                  found t))
6206                          (pop children))
6207                        found)))
6208         (push (gnus-data-number d) articles)))
6209     ;; Do the limiting.
6210     (prog1
6211         (gnus-summary-limit articles)
6212       (gnus-summary-position-point))))
6213
6214 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6215   "Mark all unread excluded articles as read.
6216 If ALL, mark even excluded ticked and dormants as read."
6217   (interactive "P")
6218   (let ((articles (gnus-sorted-complement
6219                    (sort
6220                     (mapcar (lambda (h) (mail-header-number h))
6221                             gnus-newsgroup-headers)
6222                     '<)
6223                    (sort gnus-newsgroup-limit '<)))
6224         article)
6225     (setq gnus-newsgroup-unreads
6226           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6227     (if all
6228         (setq gnus-newsgroup-dormant nil
6229               gnus-newsgroup-marked nil
6230               gnus-newsgroup-reads
6231               (nconc
6232                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6233                gnus-newsgroup-reads))
6234       (while (setq article (pop articles))
6235         (unless (or (memq article gnus-newsgroup-dormant)
6236                     (memq article gnus-newsgroup-marked))
6237           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6238
6239 (defun gnus-summary-limit (articles &optional pop)
6240   (if pop
6241       ;; We pop the previous limit off the stack and use that.
6242       (setq articles (car gnus-newsgroup-limits)
6243             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6244     ;; We use the new limit, so we push the old limit on the stack.
6245     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6246   ;; Set the limit.
6247   (setq gnus-newsgroup-limit articles)
6248   (let ((total (length gnus-newsgroup-data))
6249         (data (gnus-data-find-list (gnus-summary-article-number)))
6250         (gnus-summary-mark-below nil)   ; Inhibit this.
6251         found)
6252     ;; This will do all the work of generating the new summary buffer
6253     ;; according to the new limit.
6254     (gnus-summary-prepare)
6255     ;; Hide any threads, possibly.
6256     (and gnus-show-threads
6257          gnus-thread-hide-subtree
6258          (gnus-summary-hide-all-threads))
6259     ;; Try to return to the article you were at, or one in the
6260     ;; neighborhood.
6261     (when data
6262       ;; We try to find some article after the current one.
6263       (while data
6264         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6265           (setq data nil
6266                 found t))
6267         (setq data (cdr data))))
6268     (unless found
6269       ;; If there is no data, that means that we were after the last
6270       ;; article.  The same goes when we can't find any articles
6271       ;; after the current one.
6272       (goto-char (point-max))
6273       (gnus-summary-find-prev))
6274     (gnus-set-mode-line 'summary)
6275     ;; We return how many articles were removed from the summary
6276     ;; buffer as a result of the new limit.
6277     (- total (length gnus-newsgroup-data))))
6278
6279 (defsubst gnus-invisible-cut-children (threads)
6280   (let ((num 0))
6281     (while threads
6282       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6283         (incf num))
6284       (pop threads))
6285     (< num 2)))
6286
6287 (defsubst gnus-cut-thread (thread)
6288   "Go forwards in the thread until we find an article that we want to display."
6289   (when (or (eq gnus-fetch-old-headers 'some)
6290             (eq gnus-fetch-old-headers 'invisible)
6291             (eq gnus-build-sparse-threads 'some)
6292             (eq gnus-build-sparse-threads 'more))
6293     ;; Deal with old-fetched headers and sparse threads.
6294     (while (and
6295             thread
6296             (or
6297              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6298              (gnus-summary-article-ancient-p
6299               (mail-header-number (car thread))))
6300             (if (or (<= (length (cdr thread)) 1)
6301                     (eq gnus-fetch-old-headers 'invisible))
6302                 (setq gnus-newsgroup-limit
6303                       (delq (mail-header-number (car thread))
6304                             gnus-newsgroup-limit)
6305                       thread (cadr thread))
6306               (when (gnus-invisible-cut-children (cdr thread))
6307                 (let ((th (cdr thread)))
6308                   (while th
6309                     (if (memq (mail-header-number (caar th))
6310                               gnus-newsgroup-limit)
6311                         (setq thread (car th)
6312                               th nil)
6313                       (setq th (cdr th))))))))))
6314   thread)
6315
6316 (defun gnus-cut-threads (threads)
6317   "Cut off all uninteresting articles from the beginning of threads."
6318   (when (or (eq gnus-fetch-old-headers 'some)
6319             (eq gnus-fetch-old-headers 'invisible)
6320             (eq gnus-build-sparse-threads 'some)
6321             (eq gnus-build-sparse-threads 'more))
6322     (let ((th threads))
6323       (while th
6324         (setcar th (gnus-cut-thread (car th)))
6325         (setq th (cdr th)))))
6326   ;; Remove nixed out threads.
6327   (delq nil threads))
6328
6329 (defun gnus-summary-initial-limit (&optional show-if-empty)
6330   "Figure out what the initial limit is supposed to be on group entry.
6331 This entails weeding out unwanted dormants, low-scored articles,
6332 fetch-old-headers verbiage, and so on."
6333   ;; Most groups have nothing to remove.
6334   (if (or gnus-inhibit-limiting
6335           (and (null gnus-newsgroup-dormant)
6336                (not (eq gnus-fetch-old-headers 'some))
6337                (not (eq gnus-fetch-old-headers 'invisible))
6338                (null gnus-summary-expunge-below)
6339                (not (eq gnus-build-sparse-threads 'some))
6340                (not (eq gnus-build-sparse-threads 'more))
6341                (null gnus-thread-expunge-below)
6342                (not gnus-use-nocem)))
6343       ()                                ; Do nothing.
6344     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6345     (setq gnus-newsgroup-limit nil)
6346     (mapatoms
6347      (lambda (node)
6348        (unless (car (symbol-value node))
6349          ;; These threads have no parents -- they are roots.
6350          (let ((nodes (cdr (symbol-value node)))
6351                thread)
6352            (while nodes
6353              (if (and gnus-thread-expunge-below
6354                       (< (gnus-thread-total-score (car nodes))
6355                          gnus-thread-expunge-below))
6356                  (gnus-expunge-thread (pop nodes))
6357                (setq thread (pop nodes))
6358                (gnus-summary-limit-children thread))))))
6359      gnus-newsgroup-dependencies)
6360     ;; If this limitation resulted in an empty group, we might
6361     ;; pop the previous limit and use it instead.
6362     (when (and (not gnus-newsgroup-limit)
6363                show-if-empty)
6364       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6365     gnus-newsgroup-limit))
6366
6367 (defun gnus-summary-limit-children (thread)
6368   "Return 1 if this subthread is visible and 0 if it is not."
6369   ;; First we get the number of visible children to this thread.  This
6370   ;; is done by recursing down the thread using this function, so this
6371   ;; will really go down to a leaf article first, before slowly
6372   ;; working its way up towards the root.
6373   (when thread
6374     (let ((children
6375            (if (cdr thread)
6376                (apply '+ (mapcar 'gnus-summary-limit-children
6377                                  (cdr thread)))
6378              0))
6379           (number (mail-header-number (car thread)))
6380           score)
6381       (if (and
6382            (not (memq number gnus-newsgroup-marked))
6383            (or
6384             ;; If this article is dormant and has absolutely no visible
6385             ;; children, then this article isn't visible.
6386             (and (memq number gnus-newsgroup-dormant)
6387                  (zerop children))
6388             ;; If this is "fetch-old-headered" and there is no
6389             ;; visible children, then we don't want this article.
6390             (and (eq gnus-fetch-old-headers 'some)
6391                  (gnus-summary-article-ancient-p number)
6392                  (zerop children))
6393             ;; If this is "fetch-old-headered" and `invisible', then
6394             ;; we don't want this article.
6395             (and (eq gnus-fetch-old-headers 'invisible)
6396                  (gnus-summary-article-ancient-p number))
6397             ;; If this is a sparsely inserted article with no children,
6398             ;; we don't want it.
6399             (and (eq gnus-build-sparse-threads 'some)
6400                  (gnus-summary-article-sparse-p number)
6401                  (zerop children))
6402             ;; If we use expunging, and this article is really
6403             ;; low-scored, then we don't want this article.
6404             (when (and gnus-summary-expunge-below
6405                        (< (setq score
6406                                 (or (cdr (assq number gnus-newsgroup-scored))
6407                                     gnus-summary-default-score))
6408                           gnus-summary-expunge-below))
6409               ;; We increase the expunge-tally here, but that has
6410               ;; nothing to do with the limits, really.
6411               (incf gnus-newsgroup-expunged-tally)
6412               ;; We also mark as read here, if that's wanted.
6413               (when (and gnus-summary-mark-below
6414                          (< score gnus-summary-mark-below))
6415                 (setq gnus-newsgroup-unreads
6416                       (delq number gnus-newsgroup-unreads))
6417                 (if gnus-newsgroup-auto-expire
6418                     (push number gnus-newsgroup-expirable)
6419                   (push (cons number gnus-low-score-mark)
6420                         gnus-newsgroup-reads)))
6421               t)
6422             ;; Check NoCeM things.
6423             (if (and gnus-use-nocem
6424                      (gnus-nocem-unwanted-article-p
6425                       (mail-header-id (car thread))))
6426                 (progn
6427                   (setq gnus-newsgroup-unreads
6428                         (delq number gnus-newsgroup-unreads))
6429                   t))))
6430           ;; Nope, invisible article.
6431           0
6432         ;; Ok, this article is to be visible, so we add it to the limit
6433         ;; and return 1.
6434         (push number gnus-newsgroup-limit)
6435         1))))
6436
6437 (defun gnus-expunge-thread (thread)
6438   "Mark all articles in THREAD as read."
6439   (let* ((number (mail-header-number (car thread))))
6440     (incf gnus-newsgroup-expunged-tally)
6441     ;; We also mark as read here, if that's wanted.
6442     (setq gnus-newsgroup-unreads
6443           (delq number gnus-newsgroup-unreads))
6444     (if gnus-newsgroup-auto-expire
6445         (push number gnus-newsgroup-expirable)
6446       (push (cons number gnus-low-score-mark)
6447             gnus-newsgroup-reads)))
6448   ;; Go recursively through all subthreads.
6449   (mapcar 'gnus-expunge-thread (cdr thread)))
6450
6451 ;; Summary article oriented commands
6452
6453 (defun gnus-summary-refer-parent-article (n)
6454   "Refer parent article N times.
6455 If N is negative, go to ancestor -N instead.
6456 The difference between N and the number of articles fetched is returned."
6457   (interactive "p")
6458   (let ((skip 1)
6459         error header ref)
6460     (when (not (natnump n))
6461       (setq skip (abs n)
6462             n 1))
6463     (while (and (> n 0)
6464                 (not error))
6465       (setq header (gnus-summary-article-header))
6466       (if (and (eq (mail-header-number header)
6467                    (cdr gnus-article-current))
6468                (equal gnus-newsgroup-name
6469                       (car gnus-article-current)))
6470           ;; If we try to find the parent of the currently
6471           ;; displayed article, then we take a look at the actual
6472           ;; References header, since this is slightly more
6473           ;; reliable than the References field we got from the
6474           ;; server.
6475           (save-excursion
6476             (set-buffer gnus-original-article-buffer)
6477             (nnheader-narrow-to-headers)
6478             (unless (setq ref (message-fetch-field "references"))
6479               (setq ref (message-fetch-field "in-reply-to")))
6480             (widen))
6481         (setq ref
6482               ;; It's not the current article, so we take a bet on
6483               ;; the value we got from the server.
6484               (mail-header-references header)))
6485       (if (and ref
6486                (not (equal ref "")))
6487           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6488             (gnus-message 1 "Couldn't find parent"))
6489         (gnus-message 1 "No references in article %d"
6490                       (gnus-summary-article-number))
6491         (setq error t))
6492       (decf n))
6493     (gnus-summary-position-point)
6494     n))
6495
6496 (defun gnus-summary-refer-references ()
6497   "Fetch all articles mentioned in the References header.
6498 Return the number of articles fetched."
6499   (interactive)
6500   (let ((ref (mail-header-references (gnus-summary-article-header)))
6501         (current (gnus-summary-article-number))
6502         (n 0))
6503     (if (or (not ref)
6504             (equal ref ""))
6505         (error "No References in the current article")
6506       ;; For each Message-ID in the References header...
6507       (while (string-match "<[^>]*>" ref)
6508         (incf n)
6509         ;; ... fetch that article.
6510         (gnus-summary-refer-article
6511          (prog1 (match-string 0 ref)
6512            (setq ref (substring ref (match-end 0))))))
6513       (gnus-summary-goto-subject current)
6514       (gnus-summary-position-point)
6515       n)))
6516
6517 (defun gnus-summary-refer-thread (&optional limit)
6518   "Fetch all articles in the current thread.
6519 If LIMIT (the numerical prefix), fetch that many old headers instead
6520 of what's specified by the `gnus-refer-thread-limit' variable."
6521   (interactive "P")
6522   (let ((id (mail-header-id (gnus-summary-article-header)))
6523         (limit (if limit (prefix-numeric-value limit)
6524                  gnus-refer-thread-limit)))
6525     ;; We want to fetch LIMIT *old* headers, but we also have to
6526     ;; re-fetch all the headers in the current buffer, because many of
6527     ;; them may be undisplayed.  So we adjust LIMIT.
6528     (when (numberp limit)
6529       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6530     (unless (eq gnus-fetch-old-headers 'invisible)
6531       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6532       ;; Retrieve the headers and read them in.
6533       (if (eq (gnus-retrieve-headers
6534                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6535               'nov)
6536           (gnus-build-all-threads)
6537         (error "Can't fetch thread from backends that don't support NOV"))
6538       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6539     (gnus-summary-limit-include-thread id)))
6540
6541 (defun gnus-summary-refer-article (message-id &optional arg)
6542   "Fetch an article specified by MESSAGE-ID.
6543 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6544 or `gnus-select-method', no matter what backend the article comes from."
6545   (interactive "sMessage-ID: \nP")
6546   (when (and (stringp message-id)
6547              (not (zerop (length message-id))))
6548     ;; Construct the correct Message-ID if necessary.
6549     ;; Suggested by tale@pawl.rpi.edu.
6550     (unless (string-match "^<" message-id)
6551       (setq message-id (concat "<" message-id)))
6552     (unless (string-match ">$" message-id)
6553       (setq message-id (concat message-id ">")))
6554     (let* ((header (gnus-id-to-header message-id))
6555            (sparse (and header
6556                         (gnus-summary-article-sparse-p
6557                          (mail-header-number header))
6558                         (memq (mail-header-number header)
6559                               gnus-newsgroup-limit))))
6560       (cond
6561        ;; If the article is present in the buffer we just go to it.
6562        ((and header
6563              (or (not (gnus-summary-article-sparse-p
6564                        (mail-header-number header)))
6565                  sparse))
6566         (prog1
6567             (gnus-summary-goto-article
6568              (mail-header-number header) nil t)
6569           (when sparse
6570             (gnus-summary-update-article (mail-header-number header)))))
6571        (t
6572         ;; We fetch the article
6573         (let ((gnus-override-method
6574                (cond ((gnus-news-group-p gnus-newsgroup-name)
6575                       gnus-refer-article-method)
6576                      (arg
6577                       (or gnus-refer-article-method gnus-select-method))
6578                      (t nil)))
6579               number)
6580           ;; Start the special refer-article method, if necessary.
6581           (when (and gnus-refer-article-method
6582                      (gnus-news-group-p gnus-newsgroup-name))
6583             (gnus-check-server gnus-refer-article-method))
6584           ;; Fetch the header, and display the article.
6585           (if (setq number (gnus-summary-insert-subject message-id))
6586               (gnus-summary-select-article nil nil nil number)
6587             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6588
6589 (defun gnus-summary-edit-parameters ()
6590   "Edit the group parameters of the current group."
6591   (interactive)
6592   (gnus-group-edit-group gnus-newsgroup-name 'params))
6593
6594 (defun gnus-summary-customize-parameters ()
6595   "Customize the group parameters of the current group."
6596   (interactive)
6597   (gnus-group-customize gnus-newsgroup-name))
6598
6599 (defun gnus-summary-enter-digest-group (&optional force)
6600   "Enter an nndoc group based on the current article.
6601 If FORCE, force a digest interpretation.  If not, try
6602 to guess what the document format is."
6603   (interactive "P")
6604   (let ((conf gnus-current-window-configuration))
6605     (save-excursion
6606       (gnus-summary-select-article))
6607     (setq gnus-current-window-configuration conf)
6608     (let* ((name (format "%s-%d"
6609                          (gnus-group-prefixed-name
6610                           gnus-newsgroup-name (list 'nndoc ""))
6611                          (save-excursion
6612                            (set-buffer gnus-summary-buffer)
6613                            gnus-current-article)))
6614            (ogroup gnus-newsgroup-name)
6615            (params (append (gnus-info-params (gnus-get-info ogroup))
6616                            (list (cons 'to-group ogroup))
6617                            (list (cons 'save-article-group ogroup))))
6618            (case-fold-search t)
6619            (buf (current-buffer))
6620            dig)
6621       (save-excursion
6622         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6623         (insert-buffer-substring gnus-original-article-buffer)
6624         ;; Remove lines that may lead nndoc to misinterpret the
6625         ;; document type.
6626         (narrow-to-region
6627          (goto-char (point-min))
6628          (or (search-forward "\n\n" nil t) (point)))
6629         (goto-char (point-min))
6630         (delete-matching-lines "^\\(Path\\):\\|^From ")
6631         (widen))
6632       (unwind-protect
6633           (if (gnus-group-read-ephemeral-group
6634                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6635                             (nndoc-article-type
6636                              ,(if force 'digest 'guess))) t)
6637               ;; Make all postings to this group go to the parent group.
6638               (nconc (gnus-info-params (gnus-get-info name))
6639                      params)
6640             ;; Couldn't select this doc group.
6641             (switch-to-buffer buf)
6642             (gnus-set-global-variables)
6643             (gnus-configure-windows 'summary)
6644             (gnus-message 3 "Article couldn't be entered?"))
6645         (kill-buffer dig)))))
6646
6647 (defun gnus-summary-read-document (n)
6648   "Open a new group based on the current article(s).
6649 This will allow you to read digests and other similar
6650 documents as newsgroups.
6651 Obeys the standard process/prefix convention."
6652   (interactive "P")
6653   (let* ((articles (gnus-summary-work-articles n))
6654          (ogroup gnus-newsgroup-name)
6655          (params (append (gnus-info-params (gnus-get-info ogroup))
6656                          (list (cons 'to-group ogroup))))
6657          article group egroup groups vgroup)
6658     (while (setq article (pop articles))
6659       (setq group (format "%s-%d" gnus-newsgroup-name article))
6660       (gnus-summary-remove-process-mark article)
6661       (when (gnus-summary-display-article article)
6662         (save-excursion
6663           (with-temp-buffer
6664             (insert-buffer-substring gnus-original-article-buffer)
6665             ;; Remove some headers that may lead nndoc to make
6666             ;; the wrong guess.
6667             (message-narrow-to-head)
6668             (goto-char (point-min))
6669             (delete-matching-lines "^\\(Path\\):\\|^From ")
6670             (widen)
6671             (if (setq egroup
6672                       (gnus-group-read-ephemeral-group
6673                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6674                                      (nndoc-article-type guess))
6675                        t nil t))
6676                 (progn
6677                   ;; Make all postings to this group go to the parent group.
6678                   (nconc (gnus-info-params (gnus-get-info egroup))
6679                          params)
6680                   (push egroup groups))
6681               ;; Couldn't select this doc group.
6682               (gnus-error 3 "Article couldn't be entered"))))))
6683     ;; Now we have selected all the documents.
6684     (cond
6685      ((not groups)
6686       (error "None of the articles could be interpreted as documents"))
6687      ((gnus-group-read-ephemeral-group
6688        (setq vgroup (format
6689                      "nnvirtual:%s-%s" gnus-newsgroup-name
6690                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6691        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6692        t
6693        (cons (current-buffer) 'summary)))
6694      (t
6695       (error "Couldn't select virtual nndoc group")))))
6696
6697 (defun gnus-summary-isearch-article (&optional regexp-p)
6698   "Do incremental search forward on the current article.
6699 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6700   (interactive "P")
6701   (gnus-summary-select-article)
6702   (gnus-configure-windows 'article)
6703   (gnus-eval-in-buffer-window gnus-article-buffer
6704     (save-restriction
6705       (widen)
6706       (isearch-forward regexp-p))))
6707
6708 (defun gnus-summary-search-article-forward (regexp &optional backward)
6709   "Search for an article containing REGEXP forward.
6710 If BACKWARD, search backward instead."
6711   (interactive
6712    (list (read-string
6713           (format "Search article %s (regexp%s): "
6714                   (if current-prefix-arg "backward" "forward")
6715                   (if gnus-last-search-regexp
6716                       (concat ", default " gnus-last-search-regexp)
6717                     "")))
6718          current-prefix-arg))
6719   (if (string-equal regexp "")
6720       (setq regexp (or gnus-last-search-regexp ""))
6721     (setq gnus-last-search-regexp regexp))
6722   (if (gnus-summary-search-article regexp backward)
6723       (gnus-summary-show-thread)
6724     (error "Search failed: \"%s\"" regexp)))
6725
6726 (defun gnus-summary-search-article-backward (regexp)
6727   "Search for an article containing REGEXP backward."
6728   (interactive
6729    (list (read-string
6730           (format "Search article backward (regexp%s): "
6731                   (if gnus-last-search-regexp
6732                       (concat ", default " gnus-last-search-regexp)
6733                     "")))))
6734   (gnus-summary-search-article-forward regexp 'backward))
6735
6736 (defun gnus-summary-search-article (regexp &optional backward)
6737   "Search for an article containing REGEXP.
6738 Optional argument BACKWARD means do search for backward.
6739 `gnus-select-article-hook' is not called during the search."
6740   ;; We have to require this here to make sure that the following
6741   ;; dynamic binding isn't shadowed by autoloading.
6742   (require 'gnus-async)
6743   (require 'gnus-art)
6744   (let ((gnus-select-article-hook nil)  ;Disable hook.
6745         (gnus-article-display-hook nil)
6746         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6747         (gnus-use-article-prefetch nil)
6748         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6749         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6750         (sum (current-buffer))
6751         (found nil)
6752         point)
6753     (gnus-save-hidden-threads
6754       (gnus-summary-select-article)
6755       (set-buffer gnus-article-buffer)
6756       (when backward
6757         (forward-line -1))
6758       (while (not found)
6759         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6760         (if (if backward
6761                 (re-search-backward regexp nil t)
6762               (re-search-forward regexp nil t))
6763             ;; We found the regexp.
6764             (progn
6765               (setq found 'found)
6766               (beginning-of-line)
6767               (set-window-start
6768                (get-buffer-window (current-buffer))
6769                (point))
6770               (forward-line 1)
6771               (set-buffer sum)
6772               (setq point (point)))
6773           ;; We didn't find it, so we go to the next article.
6774           (set-buffer sum)
6775           (setq found 'not)
6776           (while (eq found 'not)
6777             (if (not (if backward (gnus-summary-find-prev)
6778                        (gnus-summary-find-next)))
6779                 ;; No more articles.
6780                 (setq found t)
6781               ;; Select the next article and adjust point.
6782               (unless (gnus-summary-article-sparse-p
6783                        (gnus-summary-article-number))
6784                 (setq found nil)
6785                 (gnus-summary-select-article)
6786                 (set-buffer gnus-article-buffer)
6787                 (widen)
6788                 (goto-char (if backward (point-max) (point-min))))))))
6789       (gnus-message 7 ""))
6790     ;; Return whether we found the regexp.
6791     (when (eq found 'found)
6792       (goto-char point)
6793       (gnus-summary-show-thread)
6794       (gnus-summary-goto-subject gnus-current-article)
6795       (gnus-summary-position-point)
6796       t)))
6797
6798 (defun gnus-summary-find-matching (header regexp &optional backward unread
6799                                           not-case-fold)
6800   "Return a list of all articles that match REGEXP on HEADER.
6801 The search stars on the current article and goes forwards unless
6802 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6803 If UNREAD is non-nil, only unread articles will
6804 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6805 in the comparisons."
6806   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6807                 (gnus-data-find-list
6808                  (gnus-summary-article-number) (gnus-data-list backward))))
6809         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6810         (case-fold-search (not not-case-fold))
6811         articles d)
6812     (unless (fboundp (intern (concat "mail-header-" header)))
6813       (error "%s is not a valid header" header))
6814     (while data
6815       (setq d (car data))
6816       (and (or (not unread)             ; We want all articles...
6817                (gnus-data-unread-p d))  ; Or just unreads.
6818            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6819            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6820            (push (gnus-data-number d) articles)) ; Success!
6821       (setq data (cdr data)))
6822     (nreverse articles)))
6823
6824 (defun gnus-summary-execute-command (header regexp command &optional backward)
6825   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6826 If HEADER is an empty string (or nil), the match is done on the entire
6827 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6828   (interactive
6829    (list (let ((completion-ignore-case t))
6830            (completing-read
6831             "Header name: "
6832             (mapcar (lambda (string) (list string))
6833                     '("Number" "Subject" "From" "Lines" "Date"
6834                       "Message-ID" "Xref" "References" "Body"))
6835             nil 'require-match))
6836          (read-string "Regexp: ")
6837          (read-key-sequence "Command: ")
6838          current-prefix-arg))
6839   (when (equal header "Body")
6840     (setq header ""))
6841   ;; Hidden thread subtrees must be searched as well.
6842   (gnus-summary-show-all-threads)
6843   ;; We don't want to change current point nor window configuration.
6844   (save-excursion
6845     (save-window-excursion
6846       (gnus-message 6 "Executing %s..." (key-description command))
6847       ;; We'd like to execute COMMAND interactively so as to give arguments.
6848       (gnus-execute header regexp
6849                     `(call-interactively ',(key-binding command))
6850                     backward)
6851       (gnus-message 6 "Executing %s...done" (key-description command)))))
6852
6853 (defun gnus-summary-beginning-of-article ()
6854   "Scroll the article back to the beginning."
6855   (interactive)
6856   (gnus-summary-select-article)
6857   (gnus-configure-windows 'article)
6858   (gnus-eval-in-buffer-window gnus-article-buffer
6859     (widen)
6860     (goto-char (point-min))
6861     (when gnus-page-broken
6862       (gnus-narrow-to-page))))
6863
6864 (defun gnus-summary-end-of-article ()
6865   "Scroll to the end of the article."
6866   (interactive)
6867   (gnus-summary-select-article)
6868   (gnus-configure-windows 'article)
6869   (gnus-eval-in-buffer-window gnus-article-buffer
6870     (widen)
6871     (goto-char (point-max))
6872     (recenter -3)
6873     (when gnus-page-broken
6874       (gnus-narrow-to-page))))
6875
6876 (defun gnus-summary-print-article (&optional filename n)
6877   "Generate and print a PostScript image of the N next (mail) articles.
6878
6879 If N is negative, print the N previous articles.  If N is nil and articles
6880 have been marked with the process mark, print these instead.
6881
6882 If the optional second argument FILENAME is nil, send the image to the
6883 printer.  If FILENAME is a string, save the PostScript image in a file with
6884 that name.  If FILENAME is a number, prompt the user for the name of the file
6885 to save in."
6886   (interactive (list (ps-print-preprint current-prefix-arg)
6887                      current-prefix-arg))
6888   (dolist (article (gnus-summary-work-articles n))
6889     (gnus-summary-select-article nil nil 'pseudo article)
6890     (gnus-eval-in-buffer-window gnus-article-buffer
6891       (let ((buffer (generate-new-buffer " *print*")))
6892         (unwind-protect
6893             (progn
6894               (copy-to-buffer buffer (point-min) (point-max))
6895               (set-buffer buffer)
6896               (gnus-article-delete-invisible-text)
6897               (let ((ps-left-header
6898                      (list
6899                       (concat "("
6900                               (mail-header-subject gnus-current-headers) ")")
6901                       (concat "("
6902                               (mail-header-from gnus-current-headers) ")")))
6903                     (ps-right-header
6904                      (list
6905                       "/pagenumberstring load"
6906                       (concat "("
6907                               (mail-header-date gnus-current-headers) ")"))))
6908                 (gnus-run-hooks 'gnus-ps-print-hook)
6909                 (save-excursion
6910                   (ps-print-buffer-with-faces filename))))
6911           (kill-buffer buffer))))))
6912
6913 (defun gnus-summary-show-article (&optional arg)
6914   "Force re-fetching of the current article.
6915 If ARG (the prefix) is non-nil, show the raw article without any
6916 article massaging functions being run."
6917   (interactive "P")
6918   (if (not arg)
6919       ;; Select the article the normal way.
6920       (gnus-summary-select-article nil 'force)
6921     ;; We have to require this here to make sure that the following
6922     ;; dynamic binding isn't shadowed by autoloading.
6923     (require 'gnus-async)
6924     (require 'gnus-art)
6925     ;; Bind the article treatment functions to nil.
6926     (let ((gnus-have-all-headers t)
6927           gnus-article-display-hook
6928           gnus-article-prepare-hook
6929           gnus-article-decode-hook
6930           gnus-break-pages
6931           gnus-show-mime
6932           gnus-visual)
6933       (gnus-summary-select-article nil 'force)))
6934   (gnus-summary-goto-subject gnus-current-article)
6935   (gnus-summary-position-point))
6936
6937 (defun gnus-summary-verbose-headers (&optional arg)
6938   "Toggle permanent full header display.
6939 If ARG is a positive number, turn header display on.
6940 If ARG is a negative number, turn header display off."
6941   (interactive "P")
6942   (setq gnus-show-all-headers
6943         (cond ((or (not (numberp arg))
6944                    (zerop arg))
6945                (not gnus-show-all-headers))
6946               ((natnump arg)
6947                t)))
6948   (gnus-summary-show-article))
6949
6950 (defun gnus-summary-toggle-header (&optional arg)
6951   "Show the headers if they are hidden, or hide them if they are shown.
6952 If ARG is a positive number, show the entire header.
6953 If ARG is a negative number, hide the unwanted header lines."
6954   (interactive "P")
6955   (save-excursion
6956     (set-buffer gnus-article-buffer)
6957     (let* ((buffer-read-only nil)
6958            (inhibit-point-motion-hooks t)
6959            (hidden (text-property-any
6960                     (goto-char (point-min)) (search-forward "\n\n")
6961                     'invisible t))
6962            e)
6963       (goto-char (point-min))
6964       (when (search-forward "\n\n" nil t)
6965         (delete-region (point-min) (1- (point))))
6966       (goto-char (point-min))
6967       (save-excursion
6968         (set-buffer gnus-original-article-buffer)
6969         (goto-char (point-min))
6970         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
6971       (insert-buffer-substring gnus-original-article-buffer 1 e)
6972       (let ((article-inhibit-hiding t))
6973         (gnus-run-hooks 'gnus-article-display-hook))
6974       (if (or (not hidden) (and (numberp arg) (< arg 0)))
6975           (let ((gnus-treat-hide-headers nil)
6976                 (gnus-treat-hide-boring-headers nil))
6977             (gnus-treat-article 'head))
6978         (gnus-treat-article 'head)))))
6979
6980 (defun gnus-summary-show-all-headers ()
6981   "Make all header lines visible."
6982   (interactive)
6983   (gnus-article-show-all-headers))
6984
6985 (defun gnus-summary-toggle-mime (&optional arg)
6986   "Toggle MIME processing.
6987 If ARG is a positive number, turn MIME processing on."
6988   (interactive "P")
6989   (setq gnus-show-mime
6990         (if (null arg)
6991             (not gnus-show-mime)
6992           (> (prefix-numeric-value arg) 0)))
6993   (gnus-summary-select-article t 'force))
6994
6995 (defun gnus-summary-caesar-message (&optional arg)
6996   "Caesar rotate the current article by 13.
6997 The numerical prefix specifies how many places to rotate each letter
6998 forward."
6999   (interactive "P")
7000   (gnus-summary-select-article)
7001   (let ((mail-header-separator ""))
7002     (gnus-eval-in-buffer-window gnus-article-buffer
7003       (save-restriction
7004         (widen)
7005         (let ((start (window-start))
7006               buffer-read-only)
7007           (message-caesar-buffer-body arg)
7008           (set-window-start (get-buffer-window (current-buffer)) start))))))
7009
7010 (defun gnus-summary-stop-page-breaking ()
7011   "Stop page breaking in the current article."
7012   (interactive)
7013   (gnus-summary-select-article)
7014   (gnus-eval-in-buffer-window gnus-article-buffer
7015     (widen)
7016     (when (gnus-visual-p 'page-marker)
7017       (let ((buffer-read-only nil))
7018         (gnus-remove-text-with-property 'gnus-prev)
7019         (gnus-remove-text-with-property 'gnus-next))
7020       (setq gnus-page-broken nil))))
7021
7022 (defun gnus-summary-move-article (&optional n to-newsgroup
7023                                             select-method action)
7024   "Move the current article to a different newsgroup.
7025 If N is a positive number, move the N next articles.
7026 If N is a negative number, move the N previous articles.
7027 If N is nil and any articles have been marked with the process mark,
7028 move those articles instead.
7029 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7030 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7031 re-spool using this method.
7032
7033 For this function to work, both the current newsgroup and the
7034 newsgroup that you want to move to have to support the `request-move'
7035 and `request-accept' functions."
7036   (interactive "P")
7037   (unless action
7038     (setq action 'move))
7039   ;; Disable marking as read.
7040   (let (gnus-mark-article-hook)
7041     (save-window-excursion
7042       (gnus-summary-select-article)))
7043   ;; Check whether the source group supports the required functions.
7044   (cond ((and (eq action 'move)
7045               (not (gnus-check-backend-function
7046                     'request-move-article gnus-newsgroup-name)))
7047          (error "The current group does not support article moving"))
7048         ((and (eq action 'crosspost)
7049               (not (gnus-check-backend-function
7050                     'request-replace-article gnus-newsgroup-name)))
7051          (error "The current group does not support article editing")))
7052   (let ((articles (gnus-summary-work-articles n))
7053         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7054         (names '((move "Move" "Moving")
7055                  (copy "Copy" "Copying")
7056                  (crosspost "Crosspost" "Crossposting")))
7057         (copy-buf (save-excursion
7058                     (nnheader-set-temp-buffer " *copy article*")))
7059         art-group to-method new-xref article to-groups)
7060     (unless (assq action names)
7061       (error "Unknown action %s" action))
7062     ;; Read the newsgroup name.
7063     (when (and (not to-newsgroup)
7064                (not select-method))
7065       (setq to-newsgroup
7066             (gnus-read-move-group-name
7067              (cadr (assq action names))
7068              (symbol-value (intern (format "gnus-current-%s-group" action)))
7069              articles prefix))
7070       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7071     (setq to-method (or select-method
7072                         (gnus-group-name-to-method to-newsgroup)))
7073     ;; Check the method we are to move this article to...
7074     (unless (gnus-check-backend-function
7075              'request-accept-article (car to-method))
7076       (error "%s does not support article copying" (car to-method)))
7077     (unless (gnus-check-server to-method)
7078       (error "Can't open server %s" (car to-method)))
7079     (gnus-message 6 "%s to %s: %s..."
7080                   (caddr (assq action names))
7081                   (or (car select-method) to-newsgroup) articles)
7082     (while articles
7083       (setq article (pop articles))
7084       (setq
7085        art-group
7086        (cond
7087         ;; Move the article.
7088         ((eq action 'move)
7089          ;; Remove this article from future suppression.
7090          (gnus-dup-unsuppress-article article)
7091          (gnus-request-move-article
7092           article                       ; Article to move
7093           gnus-newsgroup-name           ; From newsgroup
7094           (nth 1 (gnus-find-method-for-group
7095                   gnus-newsgroup-name)) ; Server
7096           (list 'gnus-request-accept-article
7097                 to-newsgroup (list 'quote select-method)
7098                 (not articles) t)               ; Accept form
7099           (not articles)))              ; Only save nov last time
7100         ;; Copy the article.
7101         ((eq action 'copy)
7102          (save-excursion
7103            (set-buffer copy-buf)
7104            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7105              (gnus-request-accept-article
7106               to-newsgroup select-method (not articles) t))))
7107         ;; Crosspost the article.
7108         ((eq action 'crosspost)
7109          (let ((xref (message-tokenize-header
7110                       (mail-header-xref (gnus-summary-article-header article))
7111                       " ")))
7112            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7113                                   ":" article))
7114            (unless xref
7115              (setq xref (list (system-name))))
7116            (setq new-xref
7117                  (concat
7118                   (mapconcat 'identity
7119                              (delete "Xref:" (delete new-xref xref))
7120                              " ")
7121                   " " new-xref))
7122            (save-excursion
7123              (set-buffer copy-buf)
7124              ;; First put the article in the destination group.
7125              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7126              (when (consp (setq art-group
7127                                 (gnus-request-accept-article
7128                                  to-newsgroup select-method (not articles))))
7129                (setq new-xref (concat new-xref " " (car art-group)
7130                                       ":" (cdr art-group)))
7131                ;; Now we have the new Xrefs header, so we insert
7132                ;; it and replace the new article.
7133                (nnheader-replace-header "Xref" new-xref)
7134                (gnus-request-replace-article
7135                 (cdr art-group) to-newsgroup (current-buffer))
7136                art-group))))))
7137       (cond
7138        ((not art-group)
7139         (gnus-message 1 "Couldn't %s article %s"
7140                       (cadr (assq action names)) article))
7141        ((and (eq art-group 'junk)
7142              (eq action 'move))
7143         (gnus-summary-mark-article article gnus-canceled-mark)
7144         (gnus-message 4 "Deleted article %s" article))
7145        (t
7146         (let* ((pto-group (gnus-group-prefixed-name
7147                            (car art-group) to-method))
7148                (entry
7149                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7150                (info (nth 2 entry))
7151                (to-group (gnus-info-group info)))
7152           ;; Update the group that has been moved to.
7153           (when (and info
7154                      (memq action '(move copy)))
7155             (unless (member to-group to-groups)
7156               (push to-group to-groups))
7157
7158             (unless (memq article gnus-newsgroup-unreads)
7159               (gnus-info-set-read
7160                info (gnus-add-to-range (gnus-info-read info)
7161                                        (list (cdr art-group)))))
7162
7163             ;; Copy any marks over to the new group.
7164             (let ((marks gnus-article-mark-lists)
7165                   (to-article (cdr art-group)))
7166               (unless (gnus-group-auto-expirable-p to-group)
7167                 (setq marks (delete '(expirable . expire) marks)))
7168
7169               ;; See whether the article is to be put in the cache.
7170               (when gnus-use-cache
7171                 (gnus-cache-possibly-enter-article
7172                  to-group to-article
7173                  (let ((header (copy-sequence
7174                                 (gnus-summary-article-header article))))
7175                    (mail-header-set-number header to-article)
7176                    header)
7177                  (memq article gnus-newsgroup-marked)
7178                  (memq article gnus-newsgroup-dormant)
7179                  (memq article gnus-newsgroup-unreads)))
7180
7181               (when (and (equal to-group gnus-newsgroup-name)
7182                          (not (memq article gnus-newsgroup-unreads)))
7183                 ;; Mark this article as read in this group.
7184                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7185                 (setcdr (gnus-active to-group) to-article)
7186                 (setcdr gnus-newsgroup-active to-article))
7187
7188               (while marks
7189                 (when (memq article (symbol-value
7190                                      (intern (format "gnus-newsgroup-%s"
7191                                                      (caar marks)))))
7192                   ;; If the other group is the same as this group,
7193                   ;; then we have to add the mark to the list.
7194                   (when (equal to-group gnus-newsgroup-name)
7195                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7196                          (cons to-article
7197                                (symbol-value
7198                                 (intern (format "gnus-newsgroup-%s"
7199                                                 (caar marks)))))))
7200                   ;; Copy the marks to other group.
7201                   (gnus-add-marked-articles
7202                    to-group (cdar marks) (list to-article) info))
7203                 (setq marks (cdr marks)))
7204
7205               (gnus-dribble-enter
7206                (concat "(gnus-group-set-info '"
7207                        (gnus-prin1-to-string (gnus-get-info to-group))
7208                        ")"))))
7209
7210           ;; Update the Xref header in this article to point to
7211           ;; the new crossposted article we have just created.
7212           (when (eq action 'crosspost)
7213             (save-excursion
7214               (set-buffer copy-buf)
7215               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7216               (nnheader-replace-header "Xref" new-xref)
7217               (gnus-request-replace-article
7218                article gnus-newsgroup-name (current-buffer)))))
7219
7220         ;;;!!!Why is this necessary?
7221         (set-buffer gnus-summary-buffer)
7222
7223         (gnus-summary-goto-subject article)
7224         (when (eq action 'move)
7225           (gnus-summary-mark-article article gnus-canceled-mark))))
7226       (gnus-summary-remove-process-mark article))
7227     ;; Re-activate all groups that have been moved to.
7228     (while to-groups
7229       (save-excursion
7230         (set-buffer gnus-group-buffer)
7231         (when (gnus-group-goto-group (car to-groups) t)
7232           (gnus-group-get-new-news-this-group 1 t))
7233         (pop to-groups)))
7234
7235     (gnus-kill-buffer copy-buf)
7236     (gnus-summary-position-point)
7237     (gnus-set-mode-line 'summary)))
7238
7239 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7240   "Move the current article to a different newsgroup.
7241 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7242 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7243 re-spool using this method."
7244   (interactive "P")
7245   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7246
7247 (defun gnus-summary-crosspost-article (&optional n)
7248   "Crosspost the current article to some other group."
7249   (interactive "P")
7250   (gnus-summary-move-article n nil nil 'crosspost))
7251
7252 (defcustom gnus-summary-respool-default-method nil
7253   "Default method for respooling an article.
7254 If nil, use to the current newsgroup method."
7255   :type '(choice (gnus-select-method :value (nnml ""))
7256                  (const nil))
7257   :group 'gnus-summary-mail)
7258
7259 (defun gnus-summary-respool-article (&optional n method)
7260   "Respool the current article.
7261 The article will be squeezed through the mail spooling process again,
7262 which means that it will be put in some mail newsgroup or other
7263 depending on `nnmail-split-methods'.
7264 If N is a positive number, respool the N next articles.
7265 If N is a negative number, respool the N previous articles.
7266 If N is nil and any articles have been marked with the process mark,
7267 respool those articles instead.
7268
7269 Respooling can be done both from mail groups and \"real\" newsgroups.
7270 In the former case, the articles in question will be moved from the
7271 current group into whatever groups they are destined to.  In the
7272 latter case, they will be copied into the relevant groups."
7273   (interactive
7274    (list current-prefix-arg
7275          (let* ((methods (gnus-methods-using 'respool))
7276                 (methname
7277                  (symbol-name (or gnus-summary-respool-default-method
7278                                   (car (gnus-find-method-for-group
7279                                         gnus-newsgroup-name)))))
7280                 (method
7281                  (gnus-completing-read
7282                   methname "What backend do you want to use when respooling?"
7283                   methods nil t nil 'gnus-mail-method-history))
7284                 ms)
7285            (cond
7286             ((zerop (length (setq ms (gnus-servers-using-backend
7287                                       (intern method)))))
7288              (list (intern method) ""))
7289             ((= 1 (length ms))
7290              (car ms))
7291             (t
7292              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7293                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7294                            ms-alist))))))))
7295   (unless method
7296     (error "No method given for respooling"))
7297   (if (assoc (symbol-name
7298               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7299              (gnus-methods-using 'respool))
7300       (gnus-summary-move-article n nil method)
7301     (gnus-summary-copy-article n nil method)))
7302
7303 (defun gnus-summary-import-article (file)
7304   "Import an arbitrary file into a mail newsgroup."
7305   (interactive "fImport file: ")
7306   (let ((group gnus-newsgroup-name)
7307         (now (current-time))
7308         atts lines)
7309     (unless (gnus-check-backend-function 'request-accept-article group)
7310       (error "%s does not support article importing" group))
7311     (or (file-readable-p file)
7312         (not (file-regular-p file))
7313         (error "Can't read %s" file))
7314     (save-excursion
7315       (set-buffer (gnus-get-buffer-create " *import file*"))
7316       (erase-buffer)
7317       (nnheader-insert-file-contents file)
7318       (goto-char (point-min))
7319       (unless (nnheader-article-p)
7320         ;; This doesn't look like an article, so we fudge some headers.
7321         (setq atts (file-attributes file)
7322               lines (count-lines (point-min) (point-max)))
7323         (insert "From: " (read-string "From: ") "\n"
7324                 "Subject: " (read-string "Subject: ") "\n"
7325                 "Date: " (message-make-date (nth 5 atts))
7326                 "\n"
7327                 "Message-ID: " (message-make-message-id) "\n"
7328                 "Lines: " (int-to-string lines) "\n"
7329                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7330       (gnus-request-accept-article group nil t)
7331       (kill-buffer (current-buffer)))))
7332
7333 (defun gnus-summary-article-posted-p ()
7334   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7335 This will be the case if the article has both been mailed and posted."
7336   (interactive)
7337   (let ((id (mail-header-references (gnus-summary-article-header)))
7338         (gnus-override-method
7339          (or gnus-refer-article-method gnus-select-method)))
7340     (if (gnus-request-head id "")
7341         (gnus-message 2 "The current message was found on %s"
7342                       gnus-override-method)
7343       (gnus-message 2 "The current message couldn't be found on %s"
7344                     gnus-override-method)
7345       nil)))
7346
7347 (defun gnus-summary-expire-articles (&optional now)
7348   "Expire all articles that are marked as expirable in the current group."
7349   (interactive)
7350   (when (gnus-check-backend-function
7351          'request-expire-articles gnus-newsgroup-name)
7352     ;; This backend supports expiry.
7353     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7354            (expirable (if total
7355                           (progn
7356                             ;; We need to update the info for
7357                             ;; this group for `gnus-list-of-read-articles'
7358                             ;; to give us the right answer.
7359                             (gnus-run-hooks 'gnus-exit-group-hook)
7360                             (gnus-summary-update-info)
7361                             (gnus-list-of-read-articles gnus-newsgroup-name))
7362                         (setq gnus-newsgroup-expirable
7363                               (sort gnus-newsgroup-expirable '<))))
7364            (expiry-wait (if now 'immediate
7365                           (gnus-group-find-parameter
7366                            gnus-newsgroup-name 'expiry-wait)))
7367            es)
7368       (when expirable
7369         ;; There are expirable articles in this group, so we run them
7370         ;; through the expiry process.
7371         (gnus-message 6 "Expiring articles...")
7372         ;; The list of articles that weren't expired is returned.
7373         (save-excursion
7374           (if expiry-wait
7375               (let ((nnmail-expiry-wait-function nil)
7376                     (nnmail-expiry-wait expiry-wait))
7377                 (setq es (gnus-request-expire-articles
7378                           expirable gnus-newsgroup-name)))
7379             (setq es (gnus-request-expire-articles
7380                       expirable gnus-newsgroup-name))))
7381         (unless total
7382           (setq gnus-newsgroup-expirable es))
7383         ;; We go through the old list of expirable, and mark all
7384         ;; really expired articles as nonexistent.
7385         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7386           (let ((gnus-use-cache nil))
7387             (while expirable
7388               (unless (memq (car expirable) es)
7389                 (when (gnus-data-find (car expirable))
7390                   (gnus-summary-mark-article
7391                    (car expirable) gnus-canceled-mark)))
7392               (setq expirable (cdr expirable)))))
7393         (gnus-message 6 "Expiring articles...done")))))
7394
7395 (defun gnus-summary-expire-articles-now ()
7396   "Expunge all expirable articles in the current group.
7397 This means that *all* articles that are marked as expirable will be
7398 deleted forever, right now."
7399   (interactive)
7400   (or gnus-expert-user
7401       (gnus-yes-or-no-p
7402        "Are you really, really, really sure you want to delete all these messages? ")
7403       (error "Phew!"))
7404   (gnus-summary-expire-articles t))
7405
7406 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7407 (defun gnus-summary-delete-article (&optional n)
7408   "Delete the N next (mail) articles.
7409 This command actually deletes articles.  This is not a marking
7410 command.  The article will disappear forever from your life, never to
7411 return.
7412 If N is negative, delete backwards.
7413 If N is nil and articles have been marked with the process mark,
7414 delete these instead."
7415   (interactive "P")
7416   (unless (gnus-check-backend-function 'request-expire-articles
7417                                        gnus-newsgroup-name)
7418     (error "The current newsgroup does not support article deletion"))
7419   ;; Compute the list of articles to delete.
7420   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7421         not-deleted)
7422     (if (and gnus-novice-user
7423              (not (gnus-yes-or-no-p
7424                    (format "Do you really want to delete %s forever? "
7425                            (if (> (length articles) 1)
7426                                (format "these %s articles" (length articles))
7427                              "this article")))))
7428         ()
7429       ;; Delete the articles.
7430       (setq not-deleted (gnus-request-expire-articles
7431                          articles gnus-newsgroup-name 'force))
7432       (while articles
7433         (gnus-summary-remove-process-mark (car articles))
7434         ;; The backend might not have been able to delete the article
7435         ;; after all.
7436         (unless (memq (car articles) not-deleted)
7437           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7438         (setq articles (cdr articles)))
7439       (when not-deleted
7440         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7441     (gnus-summary-position-point)
7442     (gnus-set-mode-line 'summary)
7443     not-deleted))
7444
7445 (defun gnus-summary-edit-article (&optional force)
7446   "Edit the current article.
7447 This will have permanent effect only in mail groups.
7448 If FORCE is non-nil, allow editing of articles even in read-only
7449 groups."
7450   (interactive "P")
7451   (save-excursion
7452     (set-buffer gnus-summary-buffer)
7453     (gnus-set-global-variables)
7454     (when (and (not force)
7455                (gnus-group-read-only-p))
7456       (error "The current newsgroup does not support article editing"))
7457     ;; Select article if needed.
7458     (unless (eq (gnus-summary-article-number)
7459                 gnus-current-article)
7460       (gnus-summary-select-article t))
7461     (gnus-article-date-original)
7462     (gnus-article-edit-article
7463      `(lambda (no-highlight)
7464         (gnus-summary-edit-article-done
7465          ,(or (mail-header-references gnus-current-headers) "")
7466          ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))
7467
7468 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7469
7470 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7471                                                  no-highlight)
7472   "Make edits to the current article permanent."
7473   (interactive)
7474   ;; Replace the article.
7475   (let ((buf (current-buffer)))
7476     (with-temp-buffer
7477       (insert-buffer-substring buf)
7478       (if (and (not read-only)
7479                (not (gnus-request-replace-article
7480                      (cdr gnus-article-current) (car gnus-article-current)
7481                      (current-buffer))))
7482           (error "Couldn't replace article")
7483         ;; Update the summary buffer.
7484         (if (and references
7485                  (equal (message-tokenize-header references " ")
7486                         (message-tokenize-header
7487                          (or (message-fetch-field "references") "") " ")))
7488             ;; We only have to update this line.
7489             (save-excursion
7490               (save-restriction
7491                 (message-narrow-to-head)
7492                 (let ((head (buffer-string))
7493                       header)
7494                   (with-temp-buffer
7495                     (insert (format "211 %d Article retrieved.\n"
7496                                     (cdr gnus-article-current)))
7497                     (insert head)
7498                     (insert ".\n")
7499                     (let ((nntp-server-buffer (current-buffer)))
7500                       (setq header (car (gnus-get-newsgroup-headers
7501                                          (save-excursion
7502                                            (set-buffer gnus-summary-buffer)
7503                                            gnus-newsgroup-dependencies)
7504                                          t))))
7505                     (save-excursion
7506                       (set-buffer gnus-summary-buffer)
7507                       (gnus-data-set-header
7508                        (gnus-data-find (cdr gnus-article-current))
7509                        header)
7510                       (gnus-summary-update-article-line
7511                        (cdr gnus-article-current) header))))))
7512           ;; Update threads.
7513           (set-buffer (or buffer gnus-summary-buffer))
7514           (gnus-summary-update-article (cdr gnus-article-current)))
7515         ;; Prettify the article buffer again.
7516         (unless no-highlight
7517           (save-excursion
7518             (set-buffer gnus-article-buffer)
7519             ;;;!!! Fix this -- article should be rehighlighted.
7520             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7521             (set-buffer gnus-original-article-buffer)
7522             (gnus-request-article
7523              (cdr gnus-article-current)
7524              (car gnus-article-current) (current-buffer))))
7525         ;; Prettify the summary buffer line.
7526         (when (gnus-visual-p 'summary-highlight 'highlight)
7527           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7528
7529 (defun gnus-summary-edit-wash (key)
7530   "Perform editing command KEY in the article buffer."
7531   (interactive
7532    (list
7533     (progn
7534       (message "%s" (concat (this-command-keys) "- "))
7535       (read-char))))
7536   (message "")
7537   (gnus-summary-edit-article)
7538   (execute-kbd-macro (concat (this-command-keys) key))
7539   (gnus-article-edit-done))
7540
7541 ;;; Respooling
7542
7543 (defun gnus-summary-respool-query (&optional silent trace)
7544   "Query where the respool algorithm would put this article."
7545   (interactive)
7546   (let (gnus-mark-article-hook)
7547     (gnus-summary-select-article)
7548     (save-excursion
7549       (set-buffer gnus-original-article-buffer)
7550       (save-restriction
7551         (message-narrow-to-head)
7552         (let ((groups (nnmail-article-group 'identity trace)))
7553           (unless silent
7554             (if groups
7555                 (message "This message would go to %s"
7556                          (mapconcat 'car groups ", "))
7557               (message "This message would go to no groups"))
7558             groups))))))
7559
7560 (defun gnus-summary-respool-trace ()
7561   "Trace where the respool algorithm would put this article.
7562 Display a buffer showing all fancy splitting patterns which matched."
7563   (interactive)
7564   (gnus-summary-respool-query nil t))
7565
7566 ;; Summary marking commands.
7567
7568 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7569   "Mark articles which has the same subject as read, and then select the next.
7570 If UNMARK is positive, remove any kind of mark.
7571 If UNMARK is negative, tick articles."
7572   (interactive "P")
7573   (when unmark
7574     (setq unmark (prefix-numeric-value unmark)))
7575   (let ((count
7576          (gnus-summary-mark-same-subject
7577           (gnus-summary-article-subject) unmark)))
7578     ;; Select next unread article.  If auto-select-same mode, should
7579     ;; select the first unread article.
7580     (gnus-summary-next-article t (and gnus-auto-select-same
7581                                       (gnus-summary-article-subject)))
7582     (gnus-message 7 "%d article%s marked as %s"
7583                   count (if (= count 1) " is" "s are")
7584                   (if unmark "unread" "read"))))
7585
7586 (defun gnus-summary-kill-same-subject (&optional unmark)
7587   "Mark articles which has the same subject as read.
7588 If UNMARK is positive, remove any kind of mark.
7589 If UNMARK is negative, tick articles."
7590   (interactive "P")
7591   (when unmark
7592     (setq unmark (prefix-numeric-value unmark)))
7593   (let ((count
7594          (gnus-summary-mark-same-subject
7595           (gnus-summary-article-subject) unmark)))
7596     ;; If marked as read, go to next unread subject.
7597     (when (null unmark)
7598       ;; Go to next unread subject.
7599       (gnus-summary-next-subject 1 t))
7600     (gnus-message 7 "%d articles are marked as %s"
7601                   count (if unmark "unread" "read"))))
7602
7603 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7604   "Mark articles with same SUBJECT as read, and return marked number.
7605 If optional argument UNMARK is positive, remove any kinds of marks.
7606 If optional argument UNMARK is negative, mark articles as unread instead."
7607   (let ((count 1))
7608     (save-excursion
7609       (cond
7610        ((null unmark)                   ; Mark as read.
7611         (while (and
7612                 (progn
7613                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7614                   (gnus-summary-show-thread) t)
7615                 (gnus-summary-find-subject subject))
7616           (setq count (1+ count))))
7617        ((> unmark 0)                    ; Tick.
7618         (while (and
7619                 (progn
7620                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7621                   (gnus-summary-show-thread) t)
7622                 (gnus-summary-find-subject subject))
7623           (setq count (1+ count))))
7624        (t                               ; Mark as unread.
7625         (while (and
7626                 (progn
7627                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7628                   (gnus-summary-show-thread) t)
7629                 (gnus-summary-find-subject subject))
7630           (setq count (1+ count)))))
7631       (gnus-set-mode-line 'summary)
7632       ;; Return the number of marked articles.
7633       count)))
7634
7635 (defun gnus-summary-mark-as-processable (n &optional unmark)
7636   "Set the process mark on the next N articles.
7637 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7638 the process mark instead.  The difference between N and the actual
7639 number of articles marked is returned."
7640   (interactive "p")
7641   (let ((backward (< n 0))
7642         (n (abs n)))
7643     (while (and
7644             (> n 0)
7645             (if unmark
7646                 (gnus-summary-remove-process-mark
7647                  (gnus-summary-article-number))
7648               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7649             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7650       (setq n (1- n)))
7651     (when (/= 0 n)
7652       (gnus-message 7 "No more articles"))
7653     (gnus-summary-recenter)
7654     (gnus-summary-position-point)
7655     n))
7656
7657 (defun gnus-summary-unmark-as-processable (n)
7658   "Remove the process mark from the next N articles.
7659 If N is negative, unmark backward instead.  The difference between N and
7660 the actual number of articles unmarked is returned."
7661   (interactive "p")
7662   (gnus-summary-mark-as-processable n t))
7663
7664 (defun gnus-summary-unmark-all-processable ()
7665   "Remove the process mark from all articles."
7666   (interactive)
7667   (save-excursion
7668     (while gnus-newsgroup-processable
7669       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7670   (gnus-summary-position-point))
7671
7672 (defun gnus-summary-mark-as-expirable (n)
7673   "Mark N articles forward as expirable.
7674 If N is negative, mark backward instead.  The difference between N and
7675 the actual number of articles marked is returned."
7676   (interactive "p")
7677   (gnus-summary-mark-forward n gnus-expirable-mark))
7678
7679 (defun gnus-summary-mark-article-as-replied (article)
7680   "Mark ARTICLE replied and update the summary line."
7681   (push article gnus-newsgroup-replied)
7682   (let ((buffer-read-only nil))
7683     (when (gnus-summary-goto-subject article nil t)
7684       (gnus-summary-update-secondary-mark article))))
7685
7686 (defun gnus-summary-set-bookmark (article)
7687   "Set a bookmark in current article."
7688   (interactive (list (gnus-summary-article-number)))
7689   (when (or (not (get-buffer gnus-article-buffer))
7690             (not gnus-current-article)
7691             (not gnus-article-current)
7692             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7693     (error "No current article selected"))
7694   ;; Remove old bookmark, if one exists.
7695   (let ((old (assq article gnus-newsgroup-bookmarks)))
7696     (when old
7697       (setq gnus-newsgroup-bookmarks
7698             (delq old gnus-newsgroup-bookmarks))))
7699   ;; Set the new bookmark, which is on the form
7700   ;; (article-number . line-number-in-body).
7701   (push
7702    (cons article
7703          (save-excursion
7704            (set-buffer gnus-article-buffer)
7705            (count-lines
7706             (min (point)
7707                  (save-excursion
7708                    (goto-char (point-min))
7709                    (search-forward "\n\n" nil t)
7710                    (point)))
7711             (point))))
7712    gnus-newsgroup-bookmarks)
7713   (gnus-message 6 "A bookmark has been added to the current article."))
7714
7715 (defun gnus-summary-remove-bookmark (article)
7716   "Remove the bookmark from the current article."
7717   (interactive (list (gnus-summary-article-number)))
7718   ;; Remove old bookmark, if one exists.
7719   (let ((old (assq article gnus-newsgroup-bookmarks)))
7720     (if old
7721         (progn
7722           (setq gnus-newsgroup-bookmarks
7723                 (delq old gnus-newsgroup-bookmarks))
7724           (gnus-message 6 "Removed bookmark."))
7725       (gnus-message 6 "No bookmark in current article."))))
7726
7727 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7728 (defun gnus-summary-mark-as-dormant (n)
7729   "Mark N articles forward as dormant.
7730 If N is negative, mark backward instead.  The difference between N and
7731 the actual number of articles marked is returned."
7732   (interactive "p")
7733   (gnus-summary-mark-forward n gnus-dormant-mark))
7734
7735 (defun gnus-summary-set-process-mark (article)
7736   "Set the process mark on ARTICLE and update the summary line."
7737   (setq gnus-newsgroup-processable
7738         (cons article
7739               (delq article gnus-newsgroup-processable)))
7740   (when (gnus-summary-goto-subject article)
7741     (gnus-summary-show-thread)
7742     (gnus-summary-goto-subject article)
7743     (gnus-summary-update-secondary-mark article)))
7744
7745 (defun gnus-summary-remove-process-mark (article)
7746   "Remove the process mark from ARTICLE and update the summary line."
7747   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7748   (when (gnus-summary-goto-subject article)
7749     (gnus-summary-show-thread)
7750     (gnus-summary-goto-subject article)
7751     (gnus-summary-update-secondary-mark article)))
7752
7753 (defun gnus-summary-set-saved-mark (article)
7754   "Set the process mark on ARTICLE and update the summary line."
7755   (push article gnus-newsgroup-saved)
7756   (when (gnus-summary-goto-subject article)
7757     (gnus-summary-update-secondary-mark article)))
7758
7759 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7760   "Mark N articles as read forwards.
7761 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7762 The difference between N and the actual number of articles marked is
7763 returned."
7764   (interactive "p")
7765   (gnus-summary-show-thread)
7766   (let ((backward (< n 0))
7767         (gnus-summary-goto-unread
7768          (and gnus-summary-goto-unread
7769               (not (eq gnus-summary-goto-unread 'never))
7770               (not (memq mark (list gnus-unread-mark
7771                                     gnus-ticked-mark gnus-dormant-mark)))))
7772         (n (abs n))
7773         (mark (or mark gnus-del-mark)))
7774     (while (and (> n 0)
7775                 (gnus-summary-mark-article nil mark no-expire)
7776                 (zerop (gnus-summary-next-subject
7777                         (if backward -1 1)
7778                         (and gnus-summary-goto-unread
7779                              (not (eq gnus-summary-goto-unread 'never)))
7780                         t)))
7781       (setq n (1- n)))
7782     (when (/= 0 n)
7783       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7784     (gnus-summary-recenter)
7785     (gnus-summary-position-point)
7786     (gnus-set-mode-line 'summary)
7787     n))
7788
7789 (defun gnus-summary-mark-article-as-read (mark)
7790   "Mark the current article quickly as read with MARK."
7791   (let ((article (gnus-summary-article-number)))
7792     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7793     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7794     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7795     (push (cons article mark) gnus-newsgroup-reads)
7796     ;; Possibly remove from cache, if that is used.
7797     (when gnus-use-cache
7798       (gnus-cache-enter-remove-article article))
7799     ;; Allow the backend to change the mark.
7800     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7801     ;; Check for auto-expiry.
7802     (when (and gnus-newsgroup-auto-expire
7803                (memq mark gnus-auto-expirable-marks))
7804       (setq mark gnus-expirable-mark)
7805       ;; Let the backend know about the mark change.
7806       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7807       (push article gnus-newsgroup-expirable))
7808     ;; Set the mark in the buffer.
7809     (gnus-summary-update-mark mark 'unread)
7810     t))
7811
7812 (defun gnus-summary-mark-article-as-unread (mark)
7813   "Mark the current article quickly as unread with MARK."
7814   (let* ((article (gnus-summary-article-number))
7815          (old-mark (gnus-summary-article-mark article)))
7816     ;; Allow the backend to change the mark.
7817     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7818     (if (eq mark old-mark)
7819         t
7820       (if (<= article 0)
7821           (progn
7822             (gnus-error 1 "Can't mark negative article numbers")
7823             nil)
7824         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7825         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7826         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7827         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7828         (cond ((= mark gnus-ticked-mark)
7829                (push article gnus-newsgroup-marked))
7830               ((= mark gnus-dormant-mark)
7831                (push article gnus-newsgroup-dormant))
7832               (t
7833                (push article gnus-newsgroup-unreads)))
7834         (gnus-pull article gnus-newsgroup-reads)
7835
7836         ;; See whether the article is to be put in the cache.
7837         (and gnus-use-cache
7838              (vectorp (gnus-summary-article-header article))
7839              (save-excursion
7840                (gnus-cache-possibly-enter-article
7841                 gnus-newsgroup-name article
7842                 (gnus-summary-article-header article)
7843                 (= mark gnus-ticked-mark)
7844                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7845
7846         ;; Fix the mark.
7847         (gnus-summary-update-mark mark 'unread)
7848         t))))
7849
7850 (defun gnus-summary-mark-article (&optional article mark no-expire)
7851   "Mark ARTICLE with MARK.  MARK can be any character.
7852 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7853 `??' (dormant) and `?E' (expirable).
7854 If MARK is nil, then the default character `?r' is used.
7855 If ARTICLE is nil, then the article on the current line will be
7856 marked."
7857   ;; The mark might be a string.
7858   (when (stringp mark)
7859     (setq mark (aref mark 0)))
7860   ;; If no mark is given, then we check auto-expiring.
7861   (when (null mark)
7862     (setq mark gnus-del-mark))
7863   (when (and (not no-expire)
7864              gnus-newsgroup-auto-expire
7865              (memq mark gnus-auto-expirable-marks))
7866     (setq mark gnus-expirable-mark))
7867   (let ((article (or article (gnus-summary-article-number)))
7868         (old-mark (gnus-summary-article-mark article)))
7869     ;; Allow the backend to change the mark.
7870     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7871     (if (eq mark old-mark)
7872         t
7873       (unless article
7874         (error "No article on current line"))
7875       (if (not (if (or (= mark gnus-unread-mark)
7876                        (= mark gnus-ticked-mark)
7877                        (= mark gnus-dormant-mark))
7878                    (gnus-mark-article-as-unread article mark)
7879                  (gnus-mark-article-as-read article mark)))
7880           t
7881         ;; See whether the article is to be put in the cache.
7882         (and gnus-use-cache
7883              (not (= mark gnus-canceled-mark))
7884              (vectorp (gnus-summary-article-header article))
7885              (save-excursion
7886                (gnus-cache-possibly-enter-article
7887                 gnus-newsgroup-name article
7888                 (gnus-summary-article-header article)
7889                 (= mark gnus-ticked-mark)
7890                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7891
7892         (when (gnus-summary-goto-subject article nil t)
7893           (let ((buffer-read-only nil))
7894             (gnus-summary-show-thread)
7895             ;; Fix the mark.
7896             (gnus-summary-update-mark mark 'unread)
7897             t))))))
7898
7899 (defun gnus-summary-update-secondary-mark (article)
7900   "Update the secondary (read, process, cache) mark."
7901   (gnus-summary-update-mark
7902    (cond ((memq article gnus-newsgroup-processable)
7903           gnus-process-mark)
7904          ((memq article gnus-newsgroup-cached)
7905           gnus-cached-mark)
7906          ((memq article gnus-newsgroup-replied)
7907           gnus-replied-mark)
7908          ((memq article gnus-newsgroup-saved)
7909           gnus-saved-mark)
7910          (t gnus-unread-mark))
7911    'replied)
7912   (when (gnus-visual-p 'summary-highlight 'highlight)
7913     (gnus-run-hooks 'gnus-summary-update-hook))
7914   t)
7915
7916 (defun gnus-summary-update-mark (mark type)
7917   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7918         (buffer-read-only nil))
7919     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7920     (when forward
7921       (when (looking-at "\r")
7922         (incf forward))
7923       (when (<= (+ forward (point)) (point-max))
7924         ;; Go to the right position on the line.
7925         (goto-char (+ forward (point)))
7926         ;; Replace the old mark with the new mark.
7927         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
7928         ;; Optionally update the marks by some user rule.
7929         (when (eq type 'unread)
7930           (gnus-data-set-mark
7931            (gnus-data-find (gnus-summary-article-number)) mark)
7932           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
7933
7934 (defun gnus-mark-article-as-read (article &optional mark)
7935   "Enter ARTICLE in the pertinent lists and remove it from others."
7936   ;; Make the article expirable.
7937   (let ((mark (or mark gnus-del-mark)))
7938     (if (= mark gnus-expirable-mark)
7939         (push article gnus-newsgroup-expirable)
7940       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7941     ;; Remove from unread and marked lists.
7942     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7943     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7944     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7945     (push (cons article mark) gnus-newsgroup-reads)
7946     ;; Possibly remove from cache, if that is used.
7947     (when gnus-use-cache
7948       (gnus-cache-enter-remove-article article))
7949     t))
7950
7951 (defun gnus-mark-article-as-unread (article &optional mark)
7952   "Enter ARTICLE in the pertinent lists and remove it from others."
7953   (let ((mark (or mark gnus-ticked-mark)))
7954     (if (<= article 0)
7955         (progn
7956           (gnus-error 1 "Can't mark negative article numbers")
7957           nil)
7958       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
7959             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
7960             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
7961             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7962
7963       ;; Unsuppress duplicates?
7964       (when gnus-suppress-duplicates
7965         (gnus-dup-unsuppress-article article))
7966
7967       (cond ((= mark gnus-ticked-mark)
7968              (push article gnus-newsgroup-marked))
7969             ((= mark gnus-dormant-mark)
7970              (push article gnus-newsgroup-dormant))
7971             (t
7972              (push article gnus-newsgroup-unreads)))
7973       (gnus-pull article gnus-newsgroup-reads)
7974       t)))
7975
7976 (defalias 'gnus-summary-mark-as-unread-forward
7977   'gnus-summary-tick-article-forward)
7978 (make-obsolete 'gnus-summary-mark-as-unread-forward
7979                'gnus-summary-tick-article-forward)
7980 (defun gnus-summary-tick-article-forward (n)
7981   "Tick N articles forwards.
7982 If N is negative, tick backwards instead.
7983 The difference between N and the number of articles ticked is returned."
7984   (interactive "p")
7985   (gnus-summary-mark-forward n gnus-ticked-mark))
7986
7987 (defalias 'gnus-summary-mark-as-unread-backward
7988   'gnus-summary-tick-article-backward)
7989 (make-obsolete 'gnus-summary-mark-as-unread-backward
7990                'gnus-summary-tick-article-backward)
7991 (defun gnus-summary-tick-article-backward (n)
7992   "Tick N articles backwards.
7993 The difference between N and the number of articles ticked is returned."
7994   (interactive "p")
7995   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
7996
7997 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7998 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7999 (defun gnus-summary-tick-article (&optional article clear-mark)
8000   "Mark current article as unread.
8001 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8002 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8003   (interactive)
8004   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8005                                        gnus-ticked-mark)))
8006
8007 (defun gnus-summary-mark-as-read-forward (n)
8008   "Mark N articles as read forwards.
8009 If N is negative, mark backwards instead.
8010 The difference between N and the actual number of articles marked is
8011 returned."
8012   (interactive "p")
8013   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8014
8015 (defun gnus-summary-mark-as-read-backward (n)
8016   "Mark the N articles as read backwards.
8017 The difference between N and the actual number of articles marked is
8018 returned."
8019   (interactive "p")
8020   (gnus-summary-mark-forward
8021    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8022
8023 (defun gnus-summary-mark-as-read (&optional article mark)
8024   "Mark current article as read.
8025 ARTICLE specifies the article to be marked as read.
8026 MARK specifies a string to be inserted at the beginning of the line."
8027   (gnus-summary-mark-article article mark))
8028
8029 (defun gnus-summary-clear-mark-forward (n)
8030   "Clear marks from N articles forward.
8031 If N is negative, clear backward instead.
8032 The difference between N and the number of marks cleared is returned."
8033   (interactive "p")
8034   (gnus-summary-mark-forward n gnus-unread-mark))
8035
8036 (defun gnus-summary-clear-mark-backward (n)
8037   "Clear marks from N articles backward.
8038 The difference between N and the number of marks cleared is returned."
8039   (interactive "p")
8040   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8041
8042 (defun gnus-summary-mark-unread-as-read ()
8043   "Intended to be used by `gnus-summary-mark-article-hook'."
8044   (when (memq gnus-current-article gnus-newsgroup-unreads)
8045     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8046
8047 (defun gnus-summary-mark-read-and-unread-as-read ()
8048   "Intended to be used by `gnus-summary-mark-article-hook'."
8049   (let ((mark (gnus-summary-article-mark)))
8050     (when (or (gnus-unread-mark-p mark)
8051               (gnus-read-mark-p mark))
8052       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8053
8054 (defun gnus-summary-mark-region-as-read (point mark all)
8055   "Mark all unread articles between point and mark as read.
8056 If given a prefix, mark all articles between point and mark as read,
8057 even ticked and dormant ones."
8058   (interactive "r\nP")
8059   (save-excursion
8060     (let (article)
8061       (goto-char point)
8062       (beginning-of-line)
8063       (while (and
8064               (< (point) mark)
8065               (progn
8066                 (when (or all
8067                           (memq (setq article (gnus-summary-article-number))
8068                                 gnus-newsgroup-unreads))
8069                   (gnus-summary-mark-article article gnus-del-mark))
8070                 t)
8071               (gnus-summary-find-next))))))
8072
8073 (defun gnus-summary-mark-below (score mark)
8074   "Mark articles with score less than SCORE with MARK."
8075   (interactive "P\ncMark: ")
8076   (setq score (if score
8077                   (prefix-numeric-value score)
8078                 (or gnus-summary-default-score 0)))
8079   (save-excursion
8080     (set-buffer gnus-summary-buffer)
8081     (goto-char (point-min))
8082     (while
8083         (progn
8084           (and (< (gnus-summary-article-score) score)
8085                (gnus-summary-mark-article nil mark))
8086           (gnus-summary-find-next)))))
8087
8088 (defun gnus-summary-kill-below (&optional score)
8089   "Mark articles with score below SCORE as read."
8090   (interactive "P")
8091   (gnus-summary-mark-below score gnus-killed-mark))
8092
8093 (defun gnus-summary-clear-above (&optional score)
8094   "Clear all marks from articles with score above SCORE."
8095   (interactive "P")
8096   (gnus-summary-mark-above score gnus-unread-mark))
8097
8098 (defun gnus-summary-tick-above (&optional score)
8099   "Tick all articles with score above SCORE."
8100   (interactive "P")
8101   (gnus-summary-mark-above score gnus-ticked-mark))
8102
8103 (defun gnus-summary-mark-above (score mark)
8104   "Mark articles with score over SCORE with MARK."
8105   (interactive "P\ncMark: ")
8106   (setq score (if score
8107                   (prefix-numeric-value score)
8108                 (or gnus-summary-default-score 0)))
8109   (save-excursion
8110     (set-buffer gnus-summary-buffer)
8111     (goto-char (point-min))
8112     (while (and (progn
8113                   (when (> (gnus-summary-article-score) score)
8114                     (gnus-summary-mark-article nil mark))
8115                   t)
8116                 (gnus-summary-find-next)))))
8117
8118 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8119 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8120 (defun gnus-summary-limit-include-expunged (&optional no-error)
8121   "Display all the hidden articles that were expunged for low scores."
8122   (interactive)
8123   (let ((buffer-read-only nil))
8124     (let ((scored gnus-newsgroup-scored)
8125           headers h)
8126       (while scored
8127         (unless (gnus-summary-goto-subject (caar scored))
8128           (and (setq h (gnus-summary-article-header (caar scored)))
8129                (< (cdar scored) gnus-summary-expunge-below)
8130                (push h headers)))
8131         (setq scored (cdr scored)))
8132       (if (not headers)
8133           (when (not no-error)
8134             (error "No expunged articles hidden"))
8135         (goto-char (point-min))
8136         (gnus-summary-prepare-unthreaded (nreverse headers))
8137         (goto-char (point-min))
8138         (gnus-summary-position-point)
8139         t))))
8140
8141 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8142   "Mark all unread articles in this newsgroup as read.
8143 If prefix argument ALL is non-nil, ticked and dormant articles will
8144 also be marked as read.
8145 If QUIETLY is non-nil, no questions will be asked.
8146 If TO-HERE is non-nil, it should be a point in the buffer.  All
8147 articles before this point will be marked as read.
8148 Note that this function will only catch up the unread article
8149 in the current summary buffer limitation.
8150 The number of articles marked as read is returned."
8151   (interactive "P")
8152   (prog1
8153       (save-excursion
8154         (when (or quietly
8155                   (not gnus-interactive-catchup) ;Without confirmation?
8156                   gnus-expert-user
8157                   (gnus-y-or-n-p
8158                    (if all
8159                        "Mark absolutely all articles as read? "
8160                      "Mark all unread articles as read? ")))
8161           (if (and not-mark
8162                    (not gnus-newsgroup-adaptive)
8163                    (not gnus-newsgroup-auto-expire)
8164                    (not gnus-suppress-duplicates)
8165                    (or (not gnus-use-cache)
8166                        (eq gnus-use-cache 'passive)))
8167               (progn
8168                 (when all
8169                   (setq gnus-newsgroup-marked nil
8170                         gnus-newsgroup-dormant nil))
8171                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8172             ;; We actually mark all articles as canceled, which we
8173             ;; have to do when using auto-expiry or adaptive scoring.
8174             (gnus-summary-show-all-threads)
8175             (when (gnus-summary-first-subject (not all) t)
8176               (while (and
8177                       (if to-here (< (point) to-here) t)
8178                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8179                       (gnus-summary-find-next (not all) nil nil t))))
8180             (gnus-set-mode-line 'summary))
8181           t))
8182     (gnus-summary-position-point)))
8183
8184 (defun gnus-summary-catchup-to-here (&optional all)
8185   "Mark all unticked articles before the current one as read.
8186 If ALL is non-nil, also mark ticked and dormant articles as read."
8187   (interactive "P")
8188   (save-excursion
8189     (gnus-save-hidden-threads
8190       (let ((beg (point)))
8191         ;; We check that there are unread articles.
8192         (when (or all (gnus-summary-find-prev))
8193           (gnus-summary-catchup all t beg)))))
8194   (gnus-summary-position-point))
8195
8196 (defun gnus-summary-catchup-all (&optional quietly)
8197   "Mark all articles in this newsgroup as read."
8198   (interactive "P")
8199   (gnus-summary-catchup t quietly))
8200
8201 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8202   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8203 If prefix argument ALL is non-nil, all articles are marked as read."
8204   (interactive "P")
8205   (when (gnus-summary-catchup all quietly nil 'fast)
8206     ;; Select next newsgroup or exit.
8207     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8208              (eq gnus-auto-select-next 'quietly))
8209         (gnus-summary-next-group nil)
8210       (gnus-summary-exit))))
8211
8212 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8213   "Mark all articles in this newsgroup as read, and then exit."
8214   (interactive "P")
8215   (gnus-summary-catchup-and-exit t quietly))
8216
8217 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8218 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8219   "Mark all articles in this group as read and select the next group.
8220 If given a prefix, mark all articles, unread as well as ticked, as
8221 read."
8222   (interactive "P")
8223   (save-excursion
8224     (gnus-summary-catchup all))
8225   (gnus-summary-next-article t nil nil t))
8226
8227 ;; Thread-based commands.
8228
8229 (defun gnus-summary-articles-in-thread (&optional article)
8230   "Return a list of all articles in the current thread.
8231 If ARTICLE is non-nil, return all articles in the thread that starts
8232 with that article."
8233   (let* ((article (or article (gnus-summary-article-number)))
8234          (data (gnus-data-find-list article))
8235          (top-level (gnus-data-level (car data)))
8236          (top-subject
8237           (cond ((null gnus-thread-operation-ignore-subject)
8238                  (gnus-simplify-subject-re
8239                   (mail-header-subject (gnus-data-header (car data)))))
8240                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8241                  (gnus-simplify-subject-fuzzy
8242                   (mail-header-subject (gnus-data-header (car data)))))
8243                 (t nil)))
8244          (end-point (save-excursion
8245                       (if (gnus-summary-go-to-next-thread)
8246                           (point) (point-max))))
8247          articles)
8248     (while (and data
8249                 (< (gnus-data-pos (car data)) end-point))
8250       (when (or (not top-subject)
8251                 (string= top-subject
8252                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8253                              (gnus-simplify-subject-fuzzy
8254                               (mail-header-subject
8255                                (gnus-data-header (car data))))
8256                            (gnus-simplify-subject-re
8257                             (mail-header-subject
8258                              (gnus-data-header (car data)))))))
8259         (push (gnus-data-number (car data)) articles))
8260       (unless (and (setq data (cdr data))
8261                    (> (gnus-data-level (car data)) top-level))
8262         (setq data nil)))
8263     ;; Return the list of articles.
8264     (nreverse articles)))
8265
8266 (defun gnus-summary-rethread-current ()
8267   "Rethread the thread the current article is part of."
8268   (interactive)
8269   (let* ((gnus-show-threads t)
8270          (article (gnus-summary-article-number))
8271          (id (mail-header-id (gnus-summary-article-header)))
8272          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8273     (unless id
8274       (error "No article on the current line"))
8275     (gnus-rebuild-thread id)
8276     (gnus-summary-goto-subject article)))
8277
8278 (defun gnus-summary-reparent-thread ()
8279   "Make the current article child of the marked (or previous) article.
8280
8281 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8282 is non-nil or the Subject: of both articles are the same."
8283   (interactive)
8284   (unless (not (gnus-group-read-only-p))
8285     (error "The current newsgroup does not support article editing"))
8286   (unless (<= (length gnus-newsgroup-processable) 1)
8287     (error "No more than one article may be marked"))
8288   (save-window-excursion
8289     (let ((gnus-article-buffer " *reparent*")
8290           (current-article (gnus-summary-article-number))
8291           ;; First grab the marked article, otherwise one line up.
8292           (parent-article (if (not (null gnus-newsgroup-processable))
8293                               (car gnus-newsgroup-processable)
8294                             (save-excursion
8295                               (if (eq (forward-line -1) 0)
8296                                   (gnus-summary-article-number)
8297                                 (error "Beginning of summary buffer"))))))
8298       (unless (not (eq current-article parent-article))
8299         (error "An article may not be self-referential"))
8300       (let ((message-id (mail-header-id
8301                          (gnus-summary-article-header parent-article))))
8302         (unless (and message-id (not (equal message-id "")))
8303           (error "No message-id in desired parent"))
8304         (gnus-with-article current-article
8305           (goto-char (point-min))
8306           (if (re-search-forward "^References: " nil t)
8307               (progn
8308                 (re-search-forward "^[^ \t]" nil t)
8309                 (forward-line -1)
8310                 (end-of-line)
8311                 (insert " " message-id))
8312             (insert "References: " message-id "\n")))
8313         (set-buffer gnus-summary-buffer)
8314         (gnus-summary-unmark-all-processable)
8315         (gnus-summary-update-article current-article)
8316         (gnus-summary-rethread-current)
8317         (gnus-message 3 "Article %d is now the child of article %d"
8318                       current-article parent-article)))))
8319
8320 (defun gnus-summary-toggle-threads (&optional arg)
8321   "Toggle showing conversation threads.
8322 If ARG is positive number, turn showing conversation threads on."
8323   (interactive "P")
8324   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8325     (setq gnus-show-threads
8326           (if (null arg) (not gnus-show-threads)
8327             (> (prefix-numeric-value arg) 0)))
8328     (gnus-summary-prepare)
8329     (gnus-summary-goto-subject current)
8330     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8331     (gnus-summary-position-point)))
8332
8333 (defun gnus-summary-show-all-threads ()
8334   "Show all threads."
8335   (interactive)
8336   (save-excursion
8337     (let ((buffer-read-only nil))
8338       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8339   (gnus-summary-position-point))
8340
8341 (defun gnus-summary-show-thread ()
8342   "Show thread subtrees.
8343 Returns nil if no thread was there to be shown."
8344   (interactive)
8345   (let ((buffer-read-only nil)
8346         (orig (point))
8347         ;; first goto end then to beg, to have point at beg after let
8348         (end (progn (end-of-line) (point)))
8349         (beg (progn (beginning-of-line) (point))))
8350     (prog1
8351         ;; Any hidden lines here?
8352         (search-forward "\r" end t)
8353       (subst-char-in-region beg end ?\^M ?\n t)
8354       (goto-char orig)
8355       (gnus-summary-position-point))))
8356
8357 (defun gnus-summary-hide-all-threads ()
8358   "Hide all thread subtrees."
8359   (interactive)
8360   (save-excursion
8361     (goto-char (point-min))
8362     (gnus-summary-hide-thread)
8363     (while (zerop (gnus-summary-next-thread 1 t))
8364       (gnus-summary-hide-thread)))
8365   (gnus-summary-position-point))
8366
8367 (defun gnus-summary-hide-thread ()
8368   "Hide thread subtrees.
8369 Returns nil if no threads were there to be hidden."
8370   (interactive)
8371   (let ((buffer-read-only nil)
8372         (start (point))
8373         (article (gnus-summary-article-number)))
8374     (goto-char start)
8375     ;; Go forward until either the buffer ends or the subthread
8376     ;; ends.
8377     (when (and (not (eobp))
8378                (or (zerop (gnus-summary-next-thread 1 t))
8379                    (goto-char (point-max))))
8380       (prog1
8381           (if (and (> (point) start)
8382                    (search-backward "\n" start t))
8383               (progn
8384                 (subst-char-in-region start (point) ?\n ?\^M)
8385                 (gnus-summary-goto-subject article))
8386             (goto-char start)
8387             nil)
8388         ;;(gnus-summary-position-point)
8389         ))))
8390
8391 (defun gnus-summary-go-to-next-thread (&optional previous)
8392   "Go to the same level (or less) next thread.
8393 If PREVIOUS is non-nil, go to previous thread instead.
8394 Return the article number moved to, or nil if moving was impossible."
8395   (let ((level (gnus-summary-thread-level))
8396         (way (if previous -1 1))
8397         (beg (point)))
8398     (forward-line way)
8399     (while (and (not (eobp))
8400                 (< level (gnus-summary-thread-level)))
8401       (forward-line way))
8402     (if (eobp)
8403         (progn
8404           (goto-char beg)
8405           nil)
8406       (setq beg (point))
8407       (prog1
8408           (gnus-summary-article-number)
8409         (goto-char beg)))))
8410
8411 (defun gnus-summary-next-thread (n &optional silent)
8412   "Go to the same level next N'th thread.
8413 If N is negative, search backward instead.
8414 Returns the difference between N and the number of skips actually
8415 done.
8416
8417 If SILENT, don't output messages."
8418   (interactive "p")
8419   (let ((backward (< n 0))
8420         (n (abs n)))
8421     (while (and (> n 0)
8422                 (gnus-summary-go-to-next-thread backward))
8423       (decf n))
8424     (unless silent
8425       (gnus-summary-position-point))
8426     (when (and (not silent) (/= 0 n))
8427       (gnus-message 7 "No more threads"))
8428     n))
8429
8430 (defun gnus-summary-prev-thread (n)
8431   "Go to the same level previous N'th thread.
8432 Returns the difference between N and the number of skips actually
8433 done."
8434   (interactive "p")
8435   (gnus-summary-next-thread (- n)))
8436
8437 (defun gnus-summary-go-down-thread ()
8438   "Go down one level in the current thread."
8439   (let ((children (gnus-summary-article-children)))
8440     (when children
8441       (gnus-summary-goto-subject (car children)))))
8442
8443 (defun gnus-summary-go-up-thread ()
8444   "Go up one level in the current thread."
8445   (let ((parent (gnus-summary-article-parent)))
8446     (when parent
8447       (gnus-summary-goto-subject parent))))
8448
8449 (defun gnus-summary-down-thread (n)
8450   "Go down thread N steps.
8451 If N is negative, go up instead.
8452 Returns the difference between N and how many steps down that were
8453 taken."
8454   (interactive "p")
8455   (let ((up (< n 0))
8456         (n (abs n)))
8457     (while (and (> n 0)
8458                 (if up (gnus-summary-go-up-thread)
8459                   (gnus-summary-go-down-thread)))
8460       (setq n (1- n)))
8461     (gnus-summary-position-point)
8462     (when (/= 0 n)
8463       (gnus-message 7 "Can't go further"))
8464     n))
8465
8466 (defun gnus-summary-up-thread (n)
8467   "Go up thread N steps.
8468 If N is negative, go up instead.
8469 Returns the difference between N and how many steps down that were
8470 taken."
8471   (interactive "p")
8472   (gnus-summary-down-thread (- n)))
8473
8474 (defun gnus-summary-top-thread ()
8475   "Go to the top of the thread."
8476   (interactive)
8477   (while (gnus-summary-go-up-thread))
8478   (gnus-summary-article-number))
8479
8480 (defun gnus-summary-kill-thread (&optional unmark)
8481   "Mark articles under current thread as read.
8482 If the prefix argument is positive, remove any kinds of marks.
8483 If the prefix argument is negative, tick articles instead."
8484   (interactive "P")
8485   (when unmark
8486     (setq unmark (prefix-numeric-value unmark)))
8487   (let ((articles (gnus-summary-articles-in-thread)))
8488     (save-excursion
8489       ;; Expand the thread.
8490       (gnus-summary-show-thread)
8491       ;; Mark all the articles.
8492       (while articles
8493         (gnus-summary-goto-subject (car articles))
8494         (cond ((null unmark)
8495                (gnus-summary-mark-article-as-read gnus-killed-mark))
8496               ((> unmark 0)
8497                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8498               (t
8499                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8500         (setq articles (cdr articles))))
8501     ;; Hide killed subtrees.
8502     (and (null unmark)
8503          gnus-thread-hide-killed
8504          (gnus-summary-hide-thread))
8505     ;; If marked as read, go to next unread subject.
8506     (when (null unmark)
8507       ;; Go to next unread subject.
8508       (gnus-summary-next-subject 1 t)))
8509   (gnus-set-mode-line 'summary))
8510
8511 ;; Summary sorting commands
8512
8513 (defun gnus-summary-sort-by-number (&optional reverse)
8514   "Sort the summary buffer by article number.
8515 Argument REVERSE means reverse order."
8516   (interactive "P")
8517   (gnus-summary-sort 'number reverse))
8518
8519 (defun gnus-summary-sort-by-author (&optional reverse)
8520   "Sort the summary buffer by author name alphabetically.
8521 If case-fold-search is non-nil, case of letters is ignored.
8522 Argument REVERSE means reverse order."
8523   (interactive "P")
8524   (gnus-summary-sort 'author reverse))
8525
8526 (defun gnus-summary-sort-by-subject (&optional reverse)
8527   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8528 If case-fold-search is non-nil, case of letters is ignored.
8529 Argument REVERSE means reverse order."
8530   (interactive "P")
8531   (gnus-summary-sort 'subject reverse))
8532
8533 (defun gnus-summary-sort-by-date (&optional reverse)
8534   "Sort the summary buffer by date.
8535 Argument REVERSE means reverse order."
8536   (interactive "P")
8537   (gnus-summary-sort 'date reverse))
8538
8539 (defun gnus-summary-sort-by-score (&optional reverse)
8540   "Sort the summary buffer by score.
8541 Argument REVERSE means reverse order."
8542   (interactive "P")
8543   (gnus-summary-sort 'score reverse))
8544
8545 (defun gnus-summary-sort-by-lines (&optional reverse)
8546   "Sort the summary buffer by article length.
8547 Argument REVERSE means reverse order."
8548   (interactive "P")
8549   (gnus-summary-sort 'lines reverse))
8550
8551 (defun gnus-summary-sort (predicate reverse)
8552   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8553   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8554          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8555          (gnus-thread-sort-functions
8556           (if (not reverse)
8557               thread
8558             `(lambda (t1 t2)
8559                (,thread t2 t1))))
8560          (gnus-article-sort-functions
8561           (if (not reverse)
8562               article
8563             `(lambda (t1 t2)
8564                (,article t2 t1))))
8565          (buffer-read-only)
8566          (gnus-summary-prepare-hook nil))
8567     ;; We do the sorting by regenerating the threads.
8568     (gnus-summary-prepare)
8569     ;; Hide subthreads if needed.
8570     (when (and gnus-show-threads gnus-thread-hide-subtree)
8571       (gnus-summary-hide-all-threads))))
8572
8573 ;; Summary saving commands.
8574
8575 (defun gnus-summary-save-article (&optional n not-saved)
8576   "Save the current article using the default saver function.
8577 If N is a positive number, save the N next articles.
8578 If N is a negative number, save the N previous articles.
8579 If N is nil and any articles have been marked with the process mark,
8580 save those articles instead.
8581 The variable `gnus-default-article-saver' specifies the saver function."
8582   (interactive "P")
8583   (let* ((articles (gnus-summary-work-articles n))
8584          (save-buffer (save-excursion
8585                         (nnheader-set-temp-buffer " *Gnus Save*")))
8586          (num (length articles))
8587          header article file)
8588     (while articles
8589       (setq header (gnus-summary-article-header
8590                     (setq article (pop articles))))
8591       (if (not (vectorp header))
8592           ;; This is a pseudo-article.
8593           (if (assq 'name header)
8594               (gnus-copy-file (cdr (assq 'name header)))
8595             (gnus-message 1 "Article %d is unsaveable" article))
8596         ;; This is a real article.
8597         (save-window-excursion
8598           (gnus-summary-select-article t nil nil article))
8599         (save-excursion
8600           (set-buffer save-buffer)
8601           (erase-buffer)
8602           (insert-buffer-substring gnus-original-article-buffer))
8603         (setq file (gnus-article-save save-buffer file num))
8604         (gnus-summary-remove-process-mark article)
8605         (unless not-saved
8606           (gnus-summary-set-saved-mark article))))
8607     (gnus-kill-buffer save-buffer)
8608     (gnus-summary-position-point)
8609     (gnus-set-mode-line 'summary)
8610     n))
8611
8612 (defun gnus-summary-pipe-output (&optional arg)
8613   "Pipe the current article to a subprocess.
8614 If N is a positive number, pipe the N next articles.
8615 If N is a negative number, pipe the N previous articles.
8616 If N is nil and any articles have been marked with the process mark,
8617 pipe those articles instead."
8618   (interactive "P")
8619   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8620     (gnus-summary-save-article arg t))
8621   (gnus-configure-windows 'pipe))
8622
8623 (defun gnus-summary-save-article-mail (&optional arg)
8624   "Append the current article to an mail file.
8625 If N is a positive number, save the N next articles.
8626 If N is a negative number, save the N previous articles.
8627 If N is nil and any articles have been marked with the process mark,
8628 save those articles instead."
8629   (interactive "P")
8630   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8631     (gnus-summary-save-article arg)))
8632
8633 (defun gnus-summary-save-article-rmail (&optional arg)
8634   "Append the current article to an rmail file.
8635 If N is a positive number, save the N next articles.
8636 If N is a negative number, save the N previous articles.
8637 If N is nil and any articles have been marked with the process mark,
8638 save those articles instead."
8639   (interactive "P")
8640   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8641     (gnus-summary-save-article arg)))
8642
8643 (defun gnus-summary-save-article-file (&optional arg)
8644   "Append the current article to a file.
8645 If N is a positive number, save the N next articles.
8646 If N is a negative number, save the N previous articles.
8647 If N is nil and any articles have been marked with the process mark,
8648 save those articles instead."
8649   (interactive "P")
8650   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8651     (gnus-summary-save-article arg)))
8652
8653 (defun gnus-summary-write-article-file (&optional arg)
8654   "Write the current article to a file, deleting the previous file.
8655 If N is a positive number, save the N next articles.
8656 If N is a negative number, save the N previous articles.
8657 If N is nil and any articles have been marked with the process mark,
8658 save those articles instead."
8659   (interactive "P")
8660   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8661     (gnus-summary-save-article arg)))
8662
8663 (defun gnus-summary-save-article-body-file (&optional arg)
8664   "Append the current article body to a file.
8665 If N is a positive number, save the N next articles.
8666 If N is a negative number, save the N previous articles.
8667 If N is nil and any articles have been marked with the process mark,
8668 save those articles instead."
8669   (interactive "P")
8670   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8671     (gnus-summary-save-article arg)))
8672
8673 (defun gnus-summary-pipe-message (program)
8674   "Pipe the current article through PROGRAM."
8675   (interactive "sProgram: ")
8676   (gnus-summary-select-article)
8677   (let ((mail-header-separator ""))
8678     (gnus-eval-in-buffer-window gnus-article-buffer
8679       (save-restriction
8680         (widen)
8681         (let ((start (window-start))
8682               buffer-read-only)
8683           (message-pipe-buffer-body program)
8684           (set-window-start (get-buffer-window (current-buffer)) start))))))
8685
8686 (defun gnus-get-split-value (methods)
8687   "Return a value based on the split METHODS."
8688   (let (split-name method result match)
8689     (when methods
8690       (save-excursion
8691         (set-buffer gnus-original-article-buffer)
8692         (save-restriction
8693           (nnheader-narrow-to-headers)
8694           (while methods
8695             (goto-char (point-min))
8696             (setq method (pop methods))
8697             (setq match (car method))
8698             (when (cond
8699                    ((stringp match)
8700                     ;; Regular expression.
8701                     (ignore-errors
8702                       (re-search-forward match nil t)))
8703                    ((gnus-functionp match)
8704                     ;; Function.
8705                     (save-restriction
8706                       (widen)
8707                       (setq result (funcall match gnus-newsgroup-name))))
8708                    ((consp match)
8709                     ;; Form.
8710                     (save-restriction
8711                       (widen)
8712                       (setq result (eval match)))))
8713               (setq split-name (append (cdr method) split-name))
8714               (cond ((stringp result)
8715                      (push (expand-file-name
8716                             result gnus-article-save-directory)
8717                            split-name))
8718                     ((consp result)
8719                      (setq split-name (append result split-name)))))))))
8720     split-name))
8721
8722 (defun gnus-valid-move-group-p (group)
8723   (and (boundp group)
8724        (symbol-name group)
8725        (symbol-value group)
8726        (memq 'respool
8727              (assoc (symbol-name
8728                      (car (gnus-find-method-for-group
8729                            (symbol-name group))))
8730                     gnus-valid-select-methods))))
8731
8732 (defun gnus-read-move-group-name (prompt default articles prefix)
8733   "Read a group name."
8734   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8735          (minibuffer-confirm-incomplete nil) ; XEmacs
8736          (prom
8737           (format "%s %s to:"
8738                   prompt
8739                   (if (> (length articles) 1)
8740                       (format "these %d articles" (length articles))
8741                     "this article")))
8742          (to-newsgroup
8743           (cond
8744            ((null split-name)
8745             (gnus-completing-read default prom
8746                                   gnus-active-hashtb
8747                                   'gnus-valid-move-group-p
8748                                   nil prefix
8749                                   'gnus-group-history))
8750            ((= 1 (length split-name))
8751             (gnus-completing-read (car split-name) prom
8752                                   gnus-active-hashtb
8753                                   'gnus-valid-move-group-p
8754                                   nil nil
8755                                   'gnus-group-history))
8756            (t
8757             (gnus-completing-read nil prom
8758                                   (mapcar (lambda (el) (list el))
8759                                           (nreverse split-name))
8760                                   nil nil nil
8761                                   'gnus-group-history)))))
8762     (when to-newsgroup
8763       (if (or (string= to-newsgroup "")
8764               (string= to-newsgroup prefix))
8765           (setq to-newsgroup default))
8766       (unless to-newsgroup
8767         (error "No group name entered"))
8768       (or (gnus-active to-newsgroup)
8769           (gnus-activate-group to-newsgroup)
8770           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8771                                      to-newsgroup))
8772               (or (and (gnus-request-create-group
8773                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8774                        (gnus-activate-group to-newsgroup nil nil
8775                                             (gnus-group-name-to-method
8776                                              to-newsgroup)))
8777                   (error "Couldn't create group %s" to-newsgroup)))
8778           (error "No such group: %s" to-newsgroup)))
8779     to-newsgroup))
8780
8781 ;; Summary extract commands
8782
8783 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8784   (let ((buffer-read-only nil)
8785         (article (gnus-summary-article-number))
8786         after-article b e)
8787     (unless (gnus-summary-goto-subject article)
8788       (error "No such article: %d" article))
8789     (gnus-summary-position-point)
8790     ;; If all commands are to be bunched up on one line, we collect
8791     ;; them here.
8792     (unless gnus-view-pseudos-separately
8793       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8794             files action)
8795         (while ps
8796           (setq action (cdr (assq 'action (car ps))))
8797           (setq files (list (cdr (assq 'name (car ps)))))
8798           (while (and ps (cdr ps)
8799                       (string= (or action "1")
8800                                (or (cdr (assq 'action (cadr ps))) "2")))
8801             (push (cdr (assq 'name (cadr ps))) files)
8802             (setcdr ps (cddr ps)))
8803           (when files
8804             (when (not (string-match "%s" action))
8805               (push " " files))
8806             (push " " files)
8807             (when (assq 'execute (car ps))
8808               (setcdr (assq 'execute (car ps))
8809                       (funcall (if (string-match "%s" action)
8810                                    'format 'concat)
8811                                action
8812                                (mapconcat
8813                                 (lambda (f)
8814                                   (if (equal f " ")
8815                                       f
8816                                     (gnus-quote-arg-for-sh-or-csh f)))
8817                                 files " ")))))
8818           (setq ps (cdr ps)))))
8819     (if (and gnus-view-pseudos (not not-view))
8820         (while pslist
8821           (when (assq 'execute (car pslist))
8822             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8823                                   (eq gnus-view-pseudos 'not-confirm)))
8824           (setq pslist (cdr pslist)))
8825       (save-excursion
8826         (while pslist
8827           (setq after-article (or (cdr (assq 'article (car pslist)))
8828                                   (gnus-summary-article-number)))
8829           (gnus-summary-goto-subject after-article)
8830           (forward-line 1)
8831           (setq b (point))
8832           (insert "    " (file-name-nondirectory
8833                           (cdr (assq 'name (car pslist))))
8834                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8835           (setq e (point))
8836           (forward-line -1)             ; back to `b'
8837           (gnus-add-text-properties
8838            b (1- e) (list 'gnus-number gnus-reffed-article-number
8839                           gnus-mouse-face-prop gnus-mouse-face))
8840           (gnus-data-enter
8841            after-article gnus-reffed-article-number
8842            gnus-unread-mark b (car pslist) 0 (- e b))
8843           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8844           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8845           (setq pslist (cdr pslist)))))))
8846
8847 (defun gnus-pseudos< (p1 p2)
8848   (let ((c1 (cdr (assq 'action p1)))
8849         (c2 (cdr (assq 'action p2))))
8850     (and c1 c2 (string< c1 c2))))
8851
8852 (defun gnus-request-pseudo-article (props)
8853   (cond ((assq 'execute props)
8854          (gnus-execute-command (cdr (assq 'execute props)))))
8855   (let ((gnus-current-article (gnus-summary-article-number)))
8856     (gnus-run-hooks 'gnus-mark-article-hook)))
8857
8858 (defun gnus-execute-command (command &optional automatic)
8859   (save-excursion
8860     (gnus-article-setup-buffer)
8861     (set-buffer gnus-article-buffer)
8862     (setq buffer-read-only nil)
8863     (let ((command (if automatic command
8864                      (read-string "Command: " (cons command 0)))))
8865       (erase-buffer)
8866       (insert "$ " command "\n\n")
8867       (if gnus-view-pseudo-asynchronously
8868           (start-process "gnus-execute" (current-buffer) shell-file-name
8869                          shell-command-switch command)
8870         (call-process shell-file-name nil t nil
8871                       shell-command-switch command)))))
8872
8873 ;; Summary kill commands.
8874
8875 (defun gnus-summary-edit-global-kill (article)
8876   "Edit the \"global\" kill file."
8877   (interactive (list (gnus-summary-article-number)))
8878   (gnus-group-edit-global-kill article))
8879
8880 (defun gnus-summary-edit-local-kill ()
8881   "Edit a local kill file applied to the current newsgroup."
8882   (interactive)
8883   (setq gnus-current-headers (gnus-summary-article-header))
8884   (gnus-group-edit-local-kill
8885    (gnus-summary-article-number) gnus-newsgroup-name))
8886
8887 ;;; Header reading.
8888
8889 (defun gnus-read-header (id &optional header)
8890   "Read the headers of article ID and enter them into the Gnus system."
8891   (let ((group gnus-newsgroup-name)
8892         (gnus-override-method
8893          (and (gnus-news-group-p gnus-newsgroup-name)
8894               gnus-refer-article-method))
8895         where)
8896     ;; First we check to see whether the header in question is already
8897     ;; fetched.
8898     (if (stringp id)
8899         ;; This is a Message-ID.
8900         (setq header (or header (gnus-id-to-header id)))
8901       ;; This is an article number.
8902       (setq header (or header (gnus-summary-article-header id))))
8903     (if (and header
8904              (not (gnus-summary-article-sparse-p (mail-header-number header))))
8905         ;; We have found the header.
8906         header
8907       ;; If this is a sparse article, we have to nix out its
8908       ;; previous entry in the thread hashtb.
8909       (when (and header
8910                  (gnus-summary-article-sparse-p (mail-header-number header)))
8911         (let* ((parent (gnus-parent-id (mail-header-references header)))
8912                (thread (and parent (gnus-id-to-thread parent))))
8913           (when thread
8914             (delq (assq header thread) thread))))
8915       ;; We have to really fetch the header to this article.
8916       (save-excursion
8917         (set-buffer nntp-server-buffer)
8918         (when (setq where (gnus-request-head id group))
8919           (nnheader-fold-continuation-lines)
8920           (goto-char (point-max))
8921           (insert ".\n")
8922           (goto-char (point-min))
8923           (insert "211 ")
8924           (princ (cond
8925                   ((numberp id) id)
8926                   ((cdr where) (cdr where))
8927                   (header (mail-header-number header))
8928                   (t gnus-reffed-article-number))
8929                  (current-buffer))
8930           (insert " Article retrieved.\n"))
8931         (if (or (not where)
8932                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
8933             ()                          ; Malformed head.
8934           (unless (gnus-summary-article-sparse-p (mail-header-number header))
8935             (when (and (stringp id)
8936                        (not (string= (gnus-group-real-name group)
8937                                      (car where))))
8938               ;; If we fetched by Message-ID and the article came
8939               ;; from a different group, we fudge some bogus article
8940               ;; numbers for this article.
8941               (mail-header-set-number header gnus-reffed-article-number))
8942             (save-excursion
8943               (set-buffer gnus-summary-buffer)
8944               (decf gnus-reffed-article-number)
8945               (gnus-remove-header (mail-header-number header))
8946               (push header gnus-newsgroup-headers)
8947               (setq gnus-current-headers header)
8948               (push (mail-header-number header) gnus-newsgroup-limit)))
8949           header)))))
8950
8951 (defun gnus-remove-header (number)
8952   "Remove header NUMBER from `gnus-newsgroup-headers'."
8953   (if (and gnus-newsgroup-headers
8954            (= number (mail-header-number (car gnus-newsgroup-headers))))
8955       (pop gnus-newsgroup-headers)
8956     (let ((headers gnus-newsgroup-headers))
8957       (while (and (cdr headers)
8958                   (not (= number (mail-header-number (cadr headers)))))
8959         (pop headers))
8960       (when (cdr headers)
8961         (setcdr headers (cddr headers))))))
8962
8963 ;;;
8964 ;;; summary highlights
8965 ;;;
8966
8967 (defun gnus-highlight-selected-summary ()
8968   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8969   ;; Highlight selected article in summary buffer
8970   (when gnus-summary-selected-face
8971     (save-excursion
8972       (let* ((beg (progn (beginning-of-line) (point)))
8973              (end (progn (end-of-line) (point)))
8974              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
8975              (from (if (get-text-property beg gnus-mouse-face-prop)
8976                        beg
8977                      (or (next-single-property-change
8978                           beg gnus-mouse-face-prop nil end)
8979                          beg)))
8980              (to
8981               (if (= from end)
8982                   (- from 2)
8983                 (or (next-single-property-change
8984                      from gnus-mouse-face-prop nil end)
8985                     end))))
8986         ;; If no mouse-face prop on line we will have to = from = end,
8987         ;; so we highlight the entire line instead.
8988         (when (= (+ to 2) from)
8989           (setq from beg)
8990           (setq to end))
8991         (if gnus-newsgroup-selected-overlay
8992             ;; Move old overlay.
8993             (gnus-move-overlay
8994              gnus-newsgroup-selected-overlay from to (current-buffer))
8995           ;; Create new overlay.
8996           (gnus-overlay-put
8997            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
8998            'face gnus-summary-selected-face))))))
8999
9000 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9001 (defun gnus-summary-highlight-line ()
9002   "Highlight current line according to `gnus-summary-highlight'."
9003   (let* ((list gnus-summary-highlight)
9004          (p (point))
9005          (end (progn (end-of-line) (point)))
9006          ;; now find out where the line starts and leave point there.
9007          (beg (progn (beginning-of-line) (point)))
9008          (article (gnus-summary-article-number))
9009          (score (or (cdr (assq (or article gnus-current-article)
9010                                gnus-newsgroup-scored))
9011                     gnus-summary-default-score 0))
9012          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9013          (inhibit-read-only t))
9014     ;; Eval the cars of the lists until we find a match.
9015     (let ((default gnus-summary-default-score))
9016       (while (and list
9017                   (not (eval (caar list))))
9018         (setq list (cdr list))))
9019     (let ((face (cdar list)))
9020       (unless (eq face (get-text-property beg 'face))
9021         (gnus-put-text-property-excluding-characters-with-faces
9022          beg end 'face
9023          (setq face (if (boundp face) (symbol-value face) face)))
9024         (when gnus-summary-highlight-line-function
9025           (funcall gnus-summary-highlight-line-function article face))))
9026     (goto-char p)))
9027
9028 (defun gnus-update-read-articles (group unread &optional compute)
9029   "Update the list of read articles in GROUP."
9030   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9031          (entry (gnus-gethash group gnus-newsrc-hashtb))
9032          (info (nth 2 entry))
9033          (prev 1)
9034          (unread (sort (copy-sequence unread) '<))
9035          read)
9036     (if (or (not info) (not active))
9037         ;; There is no info on this group if it was, in fact,
9038         ;; killed.  Gnus stores no information on killed groups, so
9039         ;; there's nothing to be done.
9040         ;; One could store the information somewhere temporarily,
9041         ;; perhaps...  Hmmm...
9042         ()
9043       ;; Remove any negative articles numbers.
9044       (while (and unread (< (car unread) 0))
9045         (setq unread (cdr unread)))
9046       ;; Remove any expired article numbers
9047       (while (and unread (< (car unread) (car active)))
9048         (setq unread (cdr unread)))
9049       ;; Compute the ranges of read articles by looking at the list of
9050       ;; unread articles.
9051       (while unread
9052         (when (/= (car unread) prev)
9053           (push (if (= prev (1- (car unread))) prev
9054                   (cons prev (1- (car unread))))
9055                 read))
9056         (setq prev (1+ (car unread)))
9057         (setq unread (cdr unread)))
9058       (when (<= prev (cdr active))
9059         (push (cons prev (cdr active)) read))
9060       (setq read (if (> (length read) 1) (nreverse read) read))
9061       (if compute
9062           read
9063         (save-excursion
9064           (set-buffer gnus-group-buffer)
9065           (gnus-undo-register
9066             `(progn
9067                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9068                (gnus-info-set-read ',info ',(gnus-info-read info))
9069                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9070                (gnus-group-update-group ,group t))))
9071        ;; Propagate the read marks to the backend.
9072        (if (gnus-check-backend-function 'request-set-mark group)
9073            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9074                  (add (gnus-remove-from-range read (gnus-info-read info))))
9075              (when (or add del)
9076                (gnus-request-set-mark
9077                 group (delq nil (list (if add (list add 'add '(read)))
9078                                       (if del (list del 'del '(read)))))))))
9079         ;; Enter this list into the group info.
9080         (gnus-info-set-read info read)
9081         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9082         (gnus-get-unread-articles-in-group info (gnus-active group))
9083         t))))
9084
9085 (defun gnus-offer-save-summaries ()
9086   "Offer to save all active summary buffers."
9087   (save-excursion
9088     (let ((buflist (buffer-list))
9089           buffers bufname)
9090       ;; Go through all buffers and find all summaries.
9091       (while buflist
9092         (and (setq bufname (buffer-name (car buflist)))
9093              (string-match "Summary" bufname)
9094              (save-excursion
9095                (set-buffer bufname)
9096                ;; We check that this is, indeed, a summary buffer.
9097                (and (eq major-mode 'gnus-summary-mode)
9098                     ;; Also make sure this isn't bogus.
9099                     gnus-newsgroup-prepared
9100                     ;; Also make sure that this isn't a dead summary buffer.
9101                     (not gnus-dead-summary-mode)))
9102              (push bufname buffers))
9103         (setq buflist (cdr buflist)))
9104       ;; Go through all these summary buffers and offer to save them.
9105       (when buffers
9106         (map-y-or-n-p
9107          "Update summary buffer %s? "
9108          (lambda (buf)
9109            (switch-to-buffer buf)
9110            (gnus-summary-exit))
9111          buffers)))))
9112
9113
9114 ;;; @ for mime-partial
9115 ;;;
9116
9117 (defun gnus-request-partial-message ()
9118   (save-excursion
9119     (let ((number (gnus-summary-article-number))
9120           (group gnus-newsgroup-name)
9121           (mother gnus-article-buffer))
9122       (set-buffer (get-buffer-create " *Partial Article*"))
9123       (erase-buffer)
9124       (setq mime-preview-buffer mother)
9125       (gnus-request-article-this-buffer number group)
9126       (mime-parse-buffer)
9127       )))
9128
9129 (autoload 'mime-combine-message/partial-pieces-automatically
9130   "mime-partial"
9131   "Internal method to combine message/partial messages automatically.")
9132
9133 (mime-add-condition
9134  'action '((type . message)(subtype . partial)
9135            (major-mode . gnus-original-article-mode)
9136            (method . mime-combine-message/partial-pieces-automatically)
9137            (summary-buffer-exp . gnus-summary-buffer)
9138            (request-partial-message-method . gnus-request-partial-message)
9139            ))
9140
9141
9142 ;;; @ end
9143 ;;;
9144
9145 (defun gnus-summary-setup-default-charset ()
9146   "Setup newsgroup default charset."
9147   (let ((name (and gnus-newsgroup-name
9148                    (gnus-group-real-name gnus-newsgroup-name))))
9149     (setq gnus-newsgroup-charset
9150           (or (and gnus-newsgroup-name
9151                    (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9152                        (let ((alist gnus-group-charset-alist)
9153                              elem (charset nil))
9154                          (while (setq elem (pop alist))
9155                            (when (and name
9156                                       (string-match (car elem) name))
9157                              (setq alist nil
9158                                    charset (cadr elem))))
9159                          charset)))
9160               gnus-default-charset))))
9161
9162 ;;;
9163 ;;; Mime Commands
9164 ;;;
9165
9166 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9167   "Display the current article buffer fully MIME-buttonized.
9168 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9169 treated as multipart/mixed."
9170   (interactive "P")
9171   (require 'gnus-art)
9172   (let ((gnus-unbuttonized-mime-types nil)
9173         (gnus-mime-display-multipart-as-mixed show-all-parts))
9174     (gnus-summary-show-article)))
9175
9176 (defun gnus-summary-repair-multipart (article)
9177   "Add a Content-Type header to a multipart article without one."
9178   (interactive (list (gnus-summary-article-number)))
9179   (gnus-with-article article
9180     (message-narrow-to-head)
9181     (goto-char (point-max))
9182     (widen)
9183     (when (search-forward "\n--" nil t)
9184       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9185         (message-narrow-to-head)
9186         (message-remove-header "Mime-Version")
9187         (message-remove-header "Content-Type")
9188         (goto-char (point-max))
9189         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9190                         separator))
9191         (insert "Mime-Version: 1.0\n")
9192         (widen))))
9193   (let (gnus-mark-article-hook)
9194     (gnus-summary-select-article t t nil article)))
9195
9196 (defun gnus-summary-toggle-display-buttonized ()
9197   "Toggle the buttonizing of the article buffer."
9198   (interactive)
9199   (require 'gnus-art)
9200   (if (setq gnus-inhibit-mime-unbuttonizing
9201             (not gnus-inhibit-mime-unbuttonizing))
9202       (let ((gnus-unbuttonized-mime-types nil))
9203         (gnus-summary-show-article))
9204     (gnus-summary-show-article)))
9205
9206 (gnus-ems-redefine)
9207
9208 (provide 'gnus-sum)
9209
9210 (run-hooks 'gnus-sum-load-hook)
9211
9212 ;;; gnus-sum.el ends here