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