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