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