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