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