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