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