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