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