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