*** empty log message ***
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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 (require 'gnus-util)
37 (require 'mm-decode)
38 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
39
40 (defcustom gnus-kill-summary-on-exit t
41   "*If non-nil, kill the summary buffer when you exit from it.
42 If nil, the summary will become a \"*Dead Summary*\" buffer, and
43 it will be killed sometime later."
44   :group 'gnus-summary-exit
45   :type 'boolean)
46
47 (defcustom gnus-fetch-old-headers nil
48   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
49 If an unread article in the group refers to an older, already read (or
50 just marked as read) article, the old article will not normally be
51 displayed in the Summary buffer.  If this variable is non-nil, Gnus
52 will attempt to grab the headers to the old articles, and thereby
53 build complete threads.  If it has the value `some', only enough
54 headers to connect otherwise loose threads will be displayed.  This
55 variable can also be a number.  In that case, no more than that number
56 of old headers will be fetched.  If it has the value `invisible', all
57 old headers will be fetched, but none will be displayed.
58
59 The server has to support NOV for any of this to work."
60   :group 'gnus-thread
61   :type '(choice (const :tag "off" nil)
62                  (const some)
63                  number
64                  (sexp :menu-tag "other" t)))
65
66 (defcustom gnus-refer-thread-limit 200
67   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
68 If t, fetch all the available old headers."
69   :group 'gnus-thread
70   :type '(choice number
71                  (sexp :menu-tag "other" t)))
72
73 (defcustom gnus-summary-make-false-root 'adopt
74   "*nil means that Gnus won't gather loose threads.
75 If the root of a thread has expired or been read in a previous
76 session, the information necessary to build a complete thread has been
77 lost.  Instead of having many small sub-threads from this original thread
78 scattered all over the summary buffer, Gnus can gather them.
79
80 If non-nil, Gnus will try to gather all loose sub-threads from an
81 original thread into one large thread.
82
83 If this variable is non-nil, it should be one of `none', `adopt',
84 `dummy' or `empty'.
85
86 If this variable is `none', Gnus will not make a false root, but just
87 present the sub-threads after another.
88 If this variable is `dummy', Gnus will create a dummy root that will
89 have all the sub-threads as children.
90 If this variable is `adopt', Gnus will make one of the \"children\"
91 the parent and mark all the step-children as such.
92 If this variable is `empty', the \"children\" are printed with empty
93 subject fields.  (Or rather, they will be printed with a string
94 given by the `gnus-summary-same-subject' variable.)"
95   :group 'gnus-thread
96   :type '(choice (const :tag "off" nil)
97                  (const none)
98                  (const dummy)
99                  (const adopt)
100                  (const empty)))
101
102 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
103   "*A regexp to match subjects to be excluded from loose thread gathering.
104 As loose thread gathering is done on subjects only, that means that
105 there can be many false gatherings performed.  By rooting out certain
106 common subjects, gathering might become saner."
107   :group 'gnus-thread
108   :type 'regexp)
109
110 (defcustom gnus-summary-gather-subject-limit nil
111   "*Maximum length of subject comparisons when gathering loose threads.
112 Use nil to compare full subjects.  Setting this variable to a low
113 number will help gather threads that have been corrupted by
114 newsreaders chopping off subject lines, but it might also mean that
115 unrelated articles that have subject that happen to begin with the
116 same few characters will be incorrectly gathered.
117
118 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
119 comparing subjects."
120   :group 'gnus-thread
121   :type '(choice (const :tag "off" nil)
122                  (const fuzzy)
123                  (sexp :menu-tag "on" t)))
124
125 (defcustom gnus-simplify-subject-functions nil
126   "List of functions taking a string argument that simplify subjects.
127 The functions are applied recursively.
128
129 Useful functions to put in this list include: `gnus-simplify-subject-re',
130 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
131   :group 'gnus-thread
132   :type '(repeat function))
133
134 (defcustom gnus-simplify-ignored-prefixes nil
135   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
136   :group 'gnus-thread
137   :type '(choice (const :tag "off" nil)
138                  regexp))
139
140 (defcustom gnus-build-sparse-threads nil
141   "*If non-nil, fill in the gaps in threads.
142 If `some', only fill in the gaps that are needed to tie loose threads
143 together.  If `more', fill in all leaf nodes that Gnus can find.  If
144 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  (const some)
148                  (const more)
149                  (sexp :menu-tag "all" t)))
150
151 (defcustom gnus-summary-thread-gathering-function
152   'gnus-gather-threads-by-subject
153   "*Function used for gathering loose threads.
154 There are two pre-defined functions: `gnus-gather-threads-by-subject',
155 which only takes Subjects into consideration; and
156 `gnus-gather-threads-by-references', which compared the References
157 headers of the articles to find matches."
158   :group 'gnus-thread
159   :type '(radio (function-item gnus-gather-threads-by-subject)
160                 (function-item gnus-gather-threads-by-references)
161                 (function :tag "other")))
162
163 (defcustom gnus-summary-same-subject ""
164   "*String indicating that the current article has the same subject as the previous.
165 This variable will only be used if the value of
166 `gnus-summary-make-false-root' is `empty'."
167   :group 'gnus-summary-format
168   :type 'string)
169
170 (defcustom gnus-summary-goto-unread t
171   "*If t, marking commands will go to the next unread article.
172 If `never', commands that usually go to the next unread article, will
173 go to the next article, whether it is read or not.
174 If nil, only the marking commands will go to the next (un)read article."
175   :group 'gnus-summary-marks
176   :link '(custom-manual "(gnus)Setting Marks")
177   :type '(choice (const :tag "off" nil)
178                  (const never)
179                  (sexp :menu-tag "on" t)))
180
181 (defcustom gnus-summary-default-score 0
182   "*Default article score level.
183 All scores generated by the score files will be added to this score.
184 If this variable is nil, scoring will be disabled."
185   :group 'gnus-score-default
186   :type '(choice (const :tag "disable")
187                  integer))
188
189 (defcustom gnus-summary-zcore-fuzz 0
190   "*Fuzziness factor for the zcore in the summary buffer.
191 Articles with scores closer than this to `gnus-summary-default-score'
192 will not be marked."
193   :group 'gnus-summary-format
194   :type 'integer)
195
196 (defcustom gnus-simplify-subject-fuzzy-regexp nil
197   "*Strings to be removed when doing fuzzy matches.
198 This can either be a regular expression or list of regular expressions
199 that will be removed from subject strings if fuzzy subject
200 simplification is selected."
201   :group 'gnus-thread
202   :type '(repeat regexp))
203
204 (defcustom gnus-show-threads t
205   "*If non-nil, display threads in summary mode."
206   :group 'gnus-thread
207   :type 'boolean)
208
209 (defcustom gnus-thread-hide-subtree nil
210   "*If non-nil, hide all threads initially.
211 If threads are hidden, you have to run the command
212 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
213 to expose hidden threads."
214   :group 'gnus-thread
215   :type 'boolean)
216
217 (defcustom gnus-thread-hide-killed t
218   "*If non-nil, hide killed threads automatically."
219   :group 'gnus-thread
220   :type 'boolean)
221
222 (defcustom gnus-thread-ignore-subject t
223   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
224 If nil, articles that have different subjects from their parents will
225 start separate threads."
226   :group 'gnus-thread
227   :type 'boolean)
228
229 (defcustom gnus-thread-operation-ignore-subject t
230   "*If non-nil, subjects will be ignored when doing thread commands.
231 This affects commands like `gnus-summary-kill-thread' and
232 `gnus-summary-lower-thread'.
233
234 If this variable is nil, articles in the same thread with different
235 subjects will not be included in the operation in question.  If this
236 variable is `fuzzy', only articles that have subjects that are fuzzily
237 equal will be included."
238   :group 'gnus-thread
239   :type '(choice (const :tag "off" nil)
240                  (const fuzzy)
241                  (sexp :tag "on" t)))
242
243 (defcustom gnus-thread-indent-level 4
244   "*Number that says how much each sub-thread should be indented."
245   :group 'gnus-thread
246   :type 'integer)
247
248 (defcustom gnus-auto-extend-newsgroup t
249   "*If non-nil, extend newsgroup forward and backward when requested."
250   :group 'gnus-summary-choose
251   :type 'boolean)
252
253 (defcustom gnus-auto-select-first t
254   "*If nil, don't select the first unread article when entering a group.
255 If this variable is `best', select the highest-scored unread article
256 in the group.  If t, select the first unread article.
257
258 This variable can also be a function to place point on a likely
259 subject line.  Useful values include `gnus-summary-first-unread-subject', 
260 `gnus-summary-first-unread-article' and 
261 `gnus-summary-best-unread-article'.
262
263 If you want to prevent automatic selection of the first unread article
264 in some newsgroups, set the variable to nil in
265 `gnus-select-group-hook'."
266   :group 'gnus-group-select
267   :type '(choice (const :tag "none" nil)
268                  (const best)
269                  (sexp :menu-tag "first" t)
270                  (function-item gnus-summary-first-unread-subject)
271                  (function-item gnus-summary-first-unread-article)
272                  (function-item gnus-summary-best-unread-article)))
273
274 (defcustom gnus-auto-select-next t
275   "*If non-nil, offer to go to the next group from the end of the previous.
276 If the value is t and the next newsgroup is empty, Gnus will exit
277 summary mode and go back to group mode.  If the value is neither nil
278 nor t, Gnus will select the following unread newsgroup.  In
279 particular, if the value is the symbol `quietly', the next unread
280 newsgroup will be selected without any confirmation, and if it is
281 `almost-quietly', the next group will be selected without any
282 confirmation if you are located on the last article in the group.
283 Finally, if this variable is `slightly-quietly', the `Z n' command
284 will go to the next group without confirmation."
285   :group 'gnus-summary-maneuvering
286   :type '(choice (const :tag "off" nil)
287                  (const quietly)
288                  (const almost-quietly)
289                  (const slightly-quietly)
290                  (sexp :menu-tag "on" t)))
291
292 (defcustom gnus-auto-select-same nil
293   "*If non-nil, select the next article with the same subject.
294 If there are no more articles with the same subject, go to
295 the first unread article."
296   :group 'gnus-summary-maneuvering
297   :type 'boolean)
298
299 (defcustom gnus-summary-check-current nil
300   "*If non-nil, consider the current article when moving.
301 The \"unread\" movement commands will stay on the same line if the
302 current article is unread."
303   :group 'gnus-summary-maneuvering
304   :type 'boolean)
305
306 (defcustom gnus-auto-center-summary t
307   "*If non-nil, always center the current summary buffer.
308 In particular, if `vertical' do only vertical recentering.  If non-nil
309 and non-`vertical', do both horizontal and vertical recentering."
310   :group 'gnus-summary-maneuvering
311   :type '(choice (const :tag "none" nil)
312                  (const vertical)
313                  (integer :tag "height")
314                  (sexp :menu-tag "both" t)))
315
316 (defcustom gnus-show-all-headers nil
317   "*If non-nil, don't hide any headers."
318   :group 'gnus-article-hiding
319   :group 'gnus-article-headers
320   :type 'boolean)
321
322 (defcustom gnus-summary-ignore-duplicates nil
323   "*If non-nil, ignore articles with identical Message-ID headers."
324   :group 'gnus-summary
325   :type 'boolean)
326
327 (defcustom gnus-single-article-buffer t
328   "*If non-nil, display all articles in the same buffer.
329 If nil, each group will get its own article buffer."
330   :group 'gnus-article-various
331   :type 'boolean)
332
333 (defcustom gnus-break-pages t
334   "*If non-nil, do page breaking on articles.
335 The page delimiter is specified by the `gnus-page-delimiter'
336 variable."
337   :group 'gnus-article-various
338   :type 'boolean)
339
340 (defcustom gnus-move-split-methods nil
341   "*Variable used to suggest where articles are to be moved to.
342 It uses the same syntax as the `gnus-split-methods' variable."
343   :group 'gnus-summary-mail
344   :type '(repeat (choice (list :value (fun) function)
345                          (cons :value ("" "") regexp (repeat string))
346                          (sexp :value nil))))
347
348 (defcustom gnus-unread-mark ? 
349   "*Mark used for unread articles."
350   :group 'gnus-summary-marks
351   :type 'character)
352
353 (defcustom gnus-ticked-mark ?!
354   "*Mark used for ticked articles."
355   :group 'gnus-summary-marks
356   :type 'character)
357
358 (defcustom gnus-dormant-mark ??
359   "*Mark used for dormant articles."
360   :group 'gnus-summary-marks
361   :type 'character)
362
363 (defcustom gnus-del-mark ?r
364   "*Mark used for del'd articles."
365   :group 'gnus-summary-marks
366   :type 'character)
367
368 (defcustom gnus-read-mark ?R
369   "*Mark used for read articles."
370   :group 'gnus-summary-marks
371   :type 'character)
372
373 (defcustom gnus-expirable-mark ?E
374   "*Mark used for expirable articles."
375   :group 'gnus-summary-marks
376   :type 'character)
377
378 (defcustom gnus-killed-mark ?K
379   "*Mark used for killed articles."
380   :group 'gnus-summary-marks
381   :type 'character)
382
383 (defcustom gnus-souped-mark ?F
384   "*Mark used for killed articles."
385   :group 'gnus-summary-marks
386   :type 'character)
387
388 (defcustom gnus-kill-file-mark ?X
389   "*Mark used for articles killed by kill files."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-low-score-mark ?Y
394   "*Mark used for articles with a low score."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-catchup-mark ?C
399   "*Mark used for articles that are caught up."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-replied-mark ?A
404   "*Mark used for articles that have been replied to."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-cached-mark ?*
409   "*Mark used for articles that are in the cache."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-saved-mark ?S
414   "*Mark used for articles that have been saved to."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-ancient-mark ?O
419   "*Mark used for ancient articles."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-sparse-mark ?Q
424   "*Mark used for sparsely reffed articles."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-canceled-mark ?G
429   "*Mark used for canceled articles."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-duplicate-mark ?M
434   "*Mark used for duplicate articles."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-undownloaded-mark ?@
439   "*Mark used for articles that weren't downloaded."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-downloadable-mark ?%
444   "*Mark used for articles that are to be downloaded."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-unsendable-mark ?=
449   "*Mark used for articles that won't be sent."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-score-over-mark ?+
454   "*Score mark used for articles with high scores."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-score-below-mark ?-
459   "*Score mark used for articles with low scores."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-empty-thread-mark ? 
464   "*There is no thread under the article."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-not-empty-thread-mark ?=
469   "*There is a thread under the article."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-view-pseudo-asynchronously nil
474   "*If non-nil, Gnus will view pseudo-articles asynchronously."
475   :group 'gnus-extract-view
476   :type 'boolean)
477
478 (defcustom gnus-auto-expirable-marks
479   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
480         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
481         gnus-souped-mark gnus-duplicate-mark)
482   "*The list of marks converted into expiration if a group is auto-expirable."
483   :group 'gnus-summary
484   :type '(repeat character))
485
486 (defcustom gnus-inhibit-user-auto-expire t
487   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
488   :group 'gnus-summary
489   :type 'boolean)
490
491 (defcustom gnus-view-pseudos nil
492   "*If `automatic', pseudo-articles will be viewed automatically.
493 If `not-confirm', pseudos will be viewed automatically, and the user
494 will not be asked to confirm the command."
495   :group 'gnus-extract-view
496   :type '(choice (const :tag "off" nil)
497                  (const automatic)
498                  (const not-confirm)))
499
500 (defcustom gnus-view-pseudos-separately t
501   "*If non-nil, one pseudo-article will be created for each file to be viewed.
502 If nil, all files that use the same viewing command will be given as a
503 list of parameters to that command."
504   :group 'gnus-extract-view
505   :type 'boolean)
506
507 (defcustom gnus-insert-pseudo-articles t
508   "*If non-nil, insert pseudo-articles when decoding articles."
509   :group 'gnus-extract-view
510   :type 'boolean)
511
512 (defcustom gnus-summary-dummy-line-format
513   "  %(:                          :%) %S\n"
514   "*The format specification for the dummy roots in the summary buffer.
515 It works along the same lines as a normal formatting string,
516 with some simple extensions.
517
518 %S  The subject"
519   :group 'gnus-threading
520   :type 'string)
521
522 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
523   "*The format specification for the summary mode line.
524 It works along the same lines as a normal formatting string,
525 with some simple extensions:
526
527 %G  Group name
528 %p  Unprefixed group name
529 %A  Current article number
530 %z  Current article score
531 %V  Gnus version
532 %U  Number of unread articles in the group
533 %e  Number of unselected articles in the group
534 %Z  A string with unread/unselected article counts
535 %g  Shortish group name
536 %S  Subject of the current article
537 %u  User-defined spec
538 %s  Current score file name
539 %d  Number of dormant articles
540 %r  Number of articles that have been marked as read in this session
541 %E  Number of articles expunged by the score files"
542   :group 'gnus-summary-format
543   :type 'string)
544
545 (defcustom gnus-summary-mark-below 0
546   "*Mark all articles with a score below this variable as read.
547 This variable is local to each summary buffer and usually set by the
548 score file."
549   :group 'gnus-score-default
550   :type 'integer)
551
552 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
553   "*List of functions used for sorting articles in the summary buffer.
554 This variable is only used when not using a threaded display."
555   :group 'gnus-summary-sort
556   :type '(repeat (choice (function-item gnus-article-sort-by-number)
557                          (function-item gnus-article-sort-by-author)
558                          (function-item gnus-article-sort-by-subject)
559                          (function-item gnus-article-sort-by-date)
560                          (function-item gnus-article-sort-by-score)
561                          (function :tag "other"))))
562
563 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
564   "*List of functions used for sorting threads in the summary buffer.
565 By default, threads are sorted by article number.
566
567 Each function takes two threads and return non-nil if the first thread
568 should be sorted before the other.  If you use more than one function,
569 the primary sort function should be the last.  You should probably
570 always include `gnus-thread-sort-by-number' in the list of sorting
571 functions -- preferably first.
572
573 Ready-made functions include `gnus-thread-sort-by-number',
574 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
575 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
576 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
577   :group 'gnus-summary-sort
578   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
579                          (function-item gnus-thread-sort-by-author)
580                          (function-item gnus-thread-sort-by-subject)
581                          (function-item gnus-thread-sort-by-date)
582                          (function-item gnus-thread-sort-by-score)
583                          (function-item gnus-thread-sort-by-total-score)
584                          (function :tag "other"))))
585
586 (defcustom gnus-thread-score-function '+
587   "*Function used for calculating the total score of a thread.
588
589 The function is called with the scores of the article and each
590 subthread and should then return the score of the thread.
591
592 Some functions you can use are `+', `max', or `min'."
593   :group 'gnus-summary-sort
594   :type 'function)
595
596 (defcustom gnus-summary-expunge-below nil
597   "All articles that have a score less than this variable will be expunged.
598 This variable is local to the summary buffers."
599   :group 'gnus-score-default
600   :type '(choice (const :tag "off" nil)
601                  integer))
602
603 (defcustom gnus-thread-expunge-below nil
604   "All threads that have a total score less than this variable will be expunged.
605 See `gnus-thread-score-function' for en explanation of what a
606 \"thread score\" is.
607
608 This variable is local to the summary buffers."
609   :group 'gnus-treading
610   :group 'gnus-score-default
611   :type '(choice (const :tag "off" nil)
612                  integer))
613
614 (defcustom gnus-summary-mode-hook nil
615   "*A hook for Gnus summary mode.
616 This hook is run before any variables are set in the summary buffer."
617   :group 'gnus-summary-various
618   :type 'hook)
619
620 (defcustom gnus-summary-menu-hook nil
621   "*Hook run after the creation of the summary mode menu."
622   :group 'gnus-summary-visual
623   :type 'hook)
624
625 (defcustom gnus-summary-exit-hook nil
626   "*A hook called on exit from the summary buffer.
627 It will be called with point in the group buffer."
628   :group 'gnus-summary-exit
629   :type 'hook)
630
631 (defcustom gnus-summary-prepare-hook nil
632   "*A hook called after the summary buffer has been generated.
633 If you want to modify the summary buffer, you can use this hook."
634   :group 'gnus-summary-various
635   :type 'hook)
636
637 (defcustom gnus-summary-prepared-hook nil
638   "*A hook called as the last thing after the summary buffer has been generated."
639   :group 'gnus-summary-various
640   :type 'hook)
641
642 (defcustom gnus-summary-generate-hook nil
643   "*A hook run just before generating the summary buffer.
644 This hook is commonly used to customize threading variables and the
645 like."
646   :group 'gnus-summary-various
647   :type 'hook)
648
649 (defcustom gnus-select-group-hook nil
650   "*A hook called when a newsgroup is selected.
651
652 If you'd like to simplify subjects like the
653 `gnus-summary-next-same-subject' command does, you can use the
654 following hook:
655
656  (setq gnus-select-group-hook
657       (list
658         (lambda ()
659           (mapcar (lambda (header)
660                      (mail-header-set-subject
661                       header
662                       (gnus-simplify-subject
663                        (mail-header-subject header) 're-only)))
664                   gnus-newsgroup-headers))))"
665   :group 'gnus-group-select
666   :type 'hook)
667
668 (defcustom gnus-select-article-hook nil
669   "*A hook called when an article is selected."
670   :group 'gnus-summary-choose
671   :type 'hook)
672
673 (defcustom gnus-visual-mark-article-hook
674   (list 'gnus-highlight-selected-summary)
675   "*Hook run after selecting an article in the summary buffer.
676 It is meant to be used for highlighting the article in some way.  It
677 is not run if `gnus-visual' is nil."
678   :group 'gnus-summary-visual
679   :type 'hook)
680
681 (defcustom gnus-parse-headers-hook nil
682   "*A hook called before parsing the headers."
683   :group 'gnus-various
684   :type 'hook)
685
686 (defcustom gnus-exit-group-hook nil
687   "*A hook called when exiting (not quitting) summary mode."
688   :group 'gnus-various
689   :type 'hook)
690
691 (defcustom gnus-summary-update-hook
692   (list 'gnus-summary-highlight-line)
693   "*A hook called when a summary line is changed.
694 The hook will not be called if `gnus-visual' is nil.
695
696 The default function `gnus-summary-highlight-line' will
697 highlight the line according to the `gnus-summary-highlight'
698 variable."
699   :group 'gnus-summary-visual
700   :type 'hook)
701
702 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
703   "*A hook called when an article is selected for the first time.
704 The hook is intended to mark an article as read (or unread)
705 automatically when it is selected."
706   :group 'gnus-summary-choose
707   :type 'hook)
708
709 (defcustom gnus-group-no-more-groups-hook nil
710   "*A hook run when returning to group mode having no more (unread) groups."
711   :group 'gnus-group-select
712   :type 'hook)
713
714 (defcustom gnus-ps-print-hook nil
715   "*A hook run before ps-printing something from Gnus."
716   :group 'gnus-summary
717   :type 'hook)
718
719 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
720   "Face used for highlighting the current article in the summary buffer."
721   :group 'gnus-summary-visual
722   :type 'face)
723
724 (defcustom gnus-summary-highlight
725   '(((= mark gnus-canceled-mark)
726      . gnus-summary-cancelled-face)
727     ((and (> score default)
728           (or (= mark gnus-dormant-mark)
729               (= mark gnus-ticked-mark)))
730      . gnus-summary-high-ticked-face)
731     ((and (< score default)
732           (or (= mark gnus-dormant-mark)
733               (= mark gnus-ticked-mark)))
734      . gnus-summary-low-ticked-face)
735     ((or (= mark gnus-dormant-mark)
736          (= mark gnus-ticked-mark))
737      . gnus-summary-normal-ticked-face)
738     ((and (> score default) (= mark gnus-ancient-mark))
739      . gnus-summary-high-ancient-face)
740     ((and (< score default) (= mark gnus-ancient-mark))
741      . gnus-summary-low-ancient-face)
742     ((= mark gnus-ancient-mark)
743      . gnus-summary-normal-ancient-face)
744     ((and (> score default) (= mark gnus-unread-mark))
745      . gnus-summary-high-unread-face)
746     ((and (< score default) (= mark gnus-unread-mark))
747      . gnus-summary-low-unread-face)
748     ((= mark gnus-unread-mark)
749      . gnus-summary-normal-unread-face)
750     ((and (> score default) (memq mark (list gnus-downloadable-mark
751                                              gnus-undownloaded-mark)))
752      . gnus-summary-high-unread-face)
753     ((and (< score default) (memq mark (list gnus-downloadable-mark
754                                              gnus-undownloaded-mark)))
755      . gnus-summary-low-unread-face)
756     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
757      . gnus-summary-normal-unread-face)
758     ((> score default)
759      . gnus-summary-high-read-face)
760     ((< score default)
761      . gnus-summary-low-read-face)
762     (t
763      . gnus-summary-normal-read-face))
764   "*Controls the highlighting of summary buffer lines.
765
766 A list of (FORM . FACE) pairs.  When deciding how a a particular
767 summary line should be displayed, each form is evaluated.  The content
768 of the face field after the first true form is used.  You can change
769 how those summary lines are displayed, by editing the face field.
770
771 You can use the following variables in the FORM field.
772
773 score:   The articles score
774 default: The default article score.
775 below:   The score below which articles are automatically marked as read.
776 mark:    The articles mark."
777   :group 'gnus-summary-visual
778   :type '(repeat (cons (sexp :tag "Form" nil)
779                        face)))
780
781 (defcustom gnus-alter-header-function nil
782   "Function called to allow alteration of article header structures.
783 The function is called with one parameter, the article header vector,
784 which it may alter in any way.")
785
786 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
787   "Variable that says which function should be used to decode a string with encoded words.")
788
789 (defcustom gnus-extra-headers nil
790   "*Extra headers to parse."
791   :group 'gnus-summary
792   :type '(repeat symbol))
793
794 (defcustom gnus-ignored-from-addresses
795   (and user-mail-address (regexp-quote user-mail-address))
796   "*Regexp of From headers that may be suppressed in favor of To headers."
797   :group 'gnus-summary
798   :type 'regexp)
799
800 (defcustom gnus-default-charset 'iso-8859-1
801   "Default charset assumed to be used when viewing non-ASCII characters.")
802
803 (defcustom gnus-newsgroup-default-charset-alist 
804   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" . cn-big5)
805     ("^cn\\>\\|\\<chinese\\>" . cn-gb-2312)
806     ("^fj\\>\\|^japan\\>" . iso-2022-jp-2)
807     ("^relcom\\>" . koi8-r))
808   "Alist of Regexps (to match group names) and default charsets to be applied."
809   :type '(repeat (cons (regexp :tag "Group")
810                        (symbol :tag "Charset")))
811   :group 'gnus)
812
813 (defcustom gnus-newsgroup-iso-8859-1-forced-regexp 
814   "^tw\\>\\|^hk\\>\\|^cn\\>\\|\\<chinese\\>"
815   "Regexp of newsgroup in which ISO-8859-1 is forced to other charset."
816   :type 'regexp
817   :group 'gnus)
818
819 ;;; Internal variables
820
821 (defvar gnus-article-mime-handles nil)
822 (defvar gnus-article-decoded-p nil)
823 (defvar gnus-scores-exclude-files nil)
824 (defvar gnus-page-broken nil)
825 (defvar gnus-inhibit-mime-unbuttonizing nil)
826
827 (defvar gnus-original-article nil)
828 (defvar gnus-article-internal-prepare-hook nil)
829 (defvar gnus-newsgroup-process-stack nil)
830
831 (defvar gnus-thread-indent-array nil)
832 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
833
834 ;; Avoid highlighting in kill files.
835 (defvar gnus-summary-inhibit-highlight nil)
836 (defvar gnus-newsgroup-selected-overlay nil)
837 (defvar gnus-inhibit-limiting nil)
838 (defvar gnus-newsgroup-adaptive-score-file nil)
839 (defvar gnus-current-score-file nil)
840 (defvar gnus-current-move-group nil)
841 (defvar gnus-current-copy-group nil)
842 (defvar gnus-current-crosspost-group nil)
843
844 (defvar gnus-newsgroup-dependencies nil)
845 (defvar gnus-newsgroup-adaptive nil)
846 (defvar gnus-summary-display-article-function nil)
847 (defvar gnus-summary-highlight-line-function nil
848   "Function called after highlighting a summary line.")
849
850 (defvar gnus-summary-line-format-alist
851   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
852     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
853     (?s gnus-tmp-subject-or-nil ?s)
854     (?n gnus-tmp-name ?s)
855     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
856         ?s)
857     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
858             gnus-tmp-from) ?s)
859     (?F gnus-tmp-from ?s)
860     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
861     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
862     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
863     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
864     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
865     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
866     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
867     (?L gnus-tmp-lines ?d)
868     (?I gnus-tmp-indentation ?s)
869     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
870     (?R gnus-tmp-replied ?c)
871     (?\[ gnus-tmp-opening-bracket ?c)
872     (?\] gnus-tmp-closing-bracket ?c)
873     (?\> (make-string gnus-tmp-level ? ) ?s)
874     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
875     (?i gnus-tmp-score ?d)
876     (?z gnus-tmp-score-char ?c)
877     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
878     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
879     (?U gnus-tmp-unread ?c)
880     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
881     (?t (gnus-summary-number-of-articles-in-thread
882          (and (boundp 'thread) (car thread)) gnus-tmp-level)
883         ?d)
884     (?e (gnus-summary-number-of-articles-in-thread
885          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
886         ?c)
887     (?u gnus-tmp-user-defined ?s)
888     (?P (gnus-pick-line-number) ?d))
889   "An alist of format specifications that can appear in summary lines,
890 and what variables they correspond with, along with the type of the
891 variable (string, integer, character, etc).")
892
893 (defvar gnus-summary-dummy-line-format-alist
894   `((?S gnus-tmp-subject ?s)
895     (?N gnus-tmp-number ?d)
896     (?u gnus-tmp-user-defined ?s)))
897
898 (defvar gnus-summary-mode-line-format-alist
899   `((?G gnus-tmp-group-name ?s)
900     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
901     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
902     (?A gnus-tmp-article-number ?d)
903     (?Z gnus-tmp-unread-and-unselected ?s)
904     (?V gnus-version ?s)
905     (?U gnus-tmp-unread-and-unticked ?d)
906     (?S gnus-tmp-subject ?s)
907     (?e gnus-tmp-unselected ?d)
908     (?u gnus-tmp-user-defined ?s)
909     (?d (length gnus-newsgroup-dormant) ?d)
910     (?t (length gnus-newsgroup-marked) ?d)
911     (?r (length gnus-newsgroup-reads) ?d)
912     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
913     (?E gnus-newsgroup-expunged-tally ?d)
914     (?s (gnus-current-score-file-nondirectory) ?s)))
915
916 (defvar gnus-last-search-regexp nil
917   "Default regexp for article search command.")
918
919 (defvar gnus-last-shell-command nil
920   "Default shell command on article.")
921
922 (defvar gnus-newsgroup-begin nil)
923 (defvar gnus-newsgroup-end nil)
924 (defvar gnus-newsgroup-last-rmail nil)
925 (defvar gnus-newsgroup-last-mail nil)
926 (defvar gnus-newsgroup-last-folder nil)
927 (defvar gnus-newsgroup-last-file nil)
928 (defvar gnus-newsgroup-auto-expire nil)
929 (defvar gnus-newsgroup-active nil)
930
931 (defvar gnus-newsgroup-data nil)
932 (defvar gnus-newsgroup-data-reverse nil)
933 (defvar gnus-newsgroup-limit nil)
934 (defvar gnus-newsgroup-limits nil)
935
936 (defvar gnus-newsgroup-unreads nil
937   "List of unread articles in the current newsgroup.")
938
939 (defvar gnus-newsgroup-unselected nil
940   "List of unselected unread articles in the current newsgroup.")
941
942 (defvar gnus-newsgroup-reads nil
943   "Alist of read articles and article marks in the current newsgroup.")
944
945 (defvar gnus-newsgroup-expunged-tally nil)
946
947 (defvar gnus-newsgroup-marked nil
948   "List of ticked articles in the current newsgroup (a subset of unread art).")
949
950 (defvar gnus-newsgroup-killed nil
951   "List of ranges of articles that have been through the scoring process.")
952
953 (defvar gnus-newsgroup-cached nil
954   "List of articles that come from the article cache.")
955
956 (defvar gnus-newsgroup-saved nil
957   "List of articles that have been saved.")
958
959 (defvar gnus-newsgroup-kill-headers nil)
960
961 (defvar gnus-newsgroup-replied nil
962   "List of articles that have been replied to in the current newsgroup.")
963
964 (defvar gnus-newsgroup-expirable nil
965   "List of articles in the current newsgroup that can be expired.")
966
967 (defvar gnus-newsgroup-processable nil
968   "List of articles in the current newsgroup that can be processed.")
969
970 (defvar gnus-newsgroup-downloadable nil
971   "List of articles in the current newsgroup that can be processed.")
972
973 (defvar gnus-newsgroup-undownloaded nil
974   "List of articles in the current newsgroup that haven't been downloaded..")
975
976 (defvar gnus-newsgroup-unsendable nil
977   "List of articles in the current newsgroup that won't be sent.")
978
979 (defvar gnus-newsgroup-bookmarks nil
980   "List of articles in the current newsgroup that have bookmarks.")
981
982 (defvar gnus-newsgroup-dormant nil
983   "List of dormant articles in the current newsgroup.")
984
985 (defvar gnus-newsgroup-scored nil
986   "List of scored articles in the current newsgroup.")
987
988 (defvar gnus-newsgroup-headers nil
989   "List of article headers in the current newsgroup.")
990
991 (defvar gnus-newsgroup-threads nil)
992
993 (defvar gnus-newsgroup-prepared nil
994   "Whether the current group has been prepared properly.")
995
996 (defvar gnus-newsgroup-ancient nil
997   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
998
999 (defvar gnus-newsgroup-sparse nil)
1000
1001 (defvar gnus-current-article nil)
1002 (defvar gnus-article-current nil)
1003 (defvar gnus-current-headers nil)
1004 (defvar gnus-have-all-headers nil)
1005 (defvar gnus-last-article nil)
1006 (defvar gnus-newsgroup-history nil)
1007
1008 (defvar gnus-newsgroup-default-charset gnus-default-charset)
1009 (defvar gnus-newsgroup-iso-8859-1-forced nil)
1010
1011 (defconst gnus-summary-local-variables
1012   '(gnus-newsgroup-name
1013     gnus-newsgroup-begin gnus-newsgroup-end
1014     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1015     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1016     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1017     gnus-newsgroup-unselected gnus-newsgroup-marked
1018     gnus-newsgroup-reads gnus-newsgroup-saved
1019     gnus-newsgroup-replied gnus-newsgroup-expirable
1020     gnus-newsgroup-processable gnus-newsgroup-killed
1021     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1022     gnus-newsgroup-unsendable
1023     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1024     gnus-newsgroup-headers gnus-newsgroup-threads
1025     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1026     gnus-current-article gnus-current-headers gnus-have-all-headers
1027     gnus-last-article gnus-article-internal-prepare-hook
1028     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1029     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1030     gnus-thread-expunge-below
1031     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1032     (gnus-summary-mark-below . global)
1033     gnus-newsgroup-active gnus-scores-exclude-files
1034     gnus-newsgroup-history gnus-newsgroup-ancient
1035     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1036     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1037     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1038     (gnus-newsgroup-expunged-tally . 0)
1039     gnus-cache-removable-articles gnus-newsgroup-cached
1040     gnus-newsgroup-data gnus-newsgroup-data-reverse
1041     gnus-newsgroup-limit gnus-newsgroup-limits
1042     gnus-newsgroup-default-charset gnus-newsgroup-iso-8859-1-forced)
1043   "Variables that are buffer-local to the summary buffers.")
1044
1045 ;; Byte-compiler warning.
1046 (defvar gnus-article-mode-map)
1047
1048 ;; MIME stuff.
1049
1050 (defvar gnus-decode-encoded-word-methods
1051   '(mail-decode-encoded-word-string)
1052   "List of methods used to decode encoded words.
1053
1054 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
1055 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
1056 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1057 whose names match REGEXP.
1058
1059 For example: 
1060 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1061  mail-decode-encoded-word-string 
1062  (\"chinese\" . rfc1843-decode-string))
1063 ")
1064
1065 (defvar gnus-decode-encoded-word-methods-cache nil)
1066
1067 (defun gnus-multi-decode-encoded-word-string (string)
1068   "Apply the functions from `gnus-encoded-word-methods' that match."
1069   (unless (and gnus-decode-encoded-word-methods-cache
1070                (eq gnus-newsgroup-name 
1071                    (car gnus-decode-encoded-word-methods-cache)))
1072     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1073     (mapc '(lambda (x) 
1074              (if (symbolp x)
1075                  (nconc gnus-decode-encoded-word-methods-cache (list x))
1076                (if (and gnus-newsgroup-name 
1077                         (string-match (car x) gnus-newsgroup-name))
1078                    (nconc gnus-decode-encoded-word-methods-cache 
1079                           (list (cdr x))))))
1080           gnus-decode-encoded-word-methods))
1081   (let ((xlist gnus-decode-encoded-word-methods-cache))
1082     (pop xlist)
1083     (while xlist
1084       (setq string (funcall (pop xlist) string))))
1085   string)
1086
1087 ;; Subject simplification.
1088
1089 (defun gnus-simplify-whitespace (str)
1090   "Remove excessive whitespace."
1091   (let ((mystr str))
1092     ;; Multiple spaces.
1093     (while (string-match "[ \t][ \t]+" mystr)
1094       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1095                           " "
1096                           (substring mystr (match-end 0)))))
1097     ;; Leading spaces.
1098     (when (string-match "^[ \t]+" mystr)
1099       (setq mystr (substring mystr (match-end 0))))
1100     ;; Trailing spaces.
1101     (when (string-match "[ \t]+$" mystr)
1102       (setq mystr (substring mystr 0 (match-beginning 0))))
1103     mystr))
1104
1105 (defsubst gnus-simplify-subject-re (subject)
1106   "Remove \"Re:\" from subject lines."
1107   (if (string-match "^[Rr][Ee]: *" subject)
1108       (substring subject (match-end 0))
1109     subject))
1110
1111 (defun gnus-simplify-subject (subject &optional re-only)
1112   "Remove `Re:' and words in parentheses.
1113 If RE-ONLY is non-nil, strip leading `Re:'s only."
1114   (let ((case-fold-search t))           ;Ignore case.
1115     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1116     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1117       (setq subject (substring subject (match-end 0))))
1118     ;; Remove uninteresting prefixes.
1119     (when (and (not re-only)
1120                gnus-simplify-ignored-prefixes
1121                (string-match gnus-simplify-ignored-prefixes subject))
1122       (setq subject (substring subject (match-end 0))))
1123     ;; Remove words in parentheses from end.
1124     (unless re-only
1125       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1126         (setq subject (substring subject 0 (match-beginning 0)))))
1127     ;; Return subject string.
1128     subject))
1129
1130 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1131 ;; all whitespace.
1132 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1133   (goto-char (point-min))
1134   (while (re-search-forward regexp nil t)
1135       (replace-match (or newtext ""))))
1136
1137 (defun gnus-simplify-buffer-fuzzy ()
1138   "Simplify string in the buffer fuzzily.
1139 The string in the accessible portion of the current buffer is simplified.
1140 It is assumed to be a single-line subject.
1141 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1142 matter is removed.  Additional things can be deleted by setting
1143 gnus-simplify-subject-fuzzy-regexp."
1144   (let ((case-fold-search t)
1145         (modified-tick))
1146     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1147
1148     (while (not (eq modified-tick (buffer-modified-tick)))
1149       (setq modified-tick (buffer-modified-tick))
1150       (cond
1151        ((listp gnus-simplify-subject-fuzzy-regexp)
1152         (mapcar 'gnus-simplify-buffer-fuzzy-step
1153                 gnus-simplify-subject-fuzzy-regexp))
1154        (gnus-simplify-subject-fuzzy-regexp
1155         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1156       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1157       (gnus-simplify-buffer-fuzzy-step
1158        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1159       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1160
1161     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1162     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1163     (gnus-simplify-buffer-fuzzy-step " $")
1164     (gnus-simplify-buffer-fuzzy-step "^ +")))
1165
1166 (defun gnus-simplify-subject-fuzzy (subject)
1167   "Simplify a subject string fuzzily.
1168 See `gnus-simplify-buffer-fuzzy' for details."
1169   (save-excursion
1170     (gnus-set-work-buffer)
1171     (let ((case-fold-search t))
1172       ;; Remove uninteresting prefixes.
1173       (when (and gnus-simplify-ignored-prefixes
1174                  (string-match gnus-simplify-ignored-prefixes subject))
1175         (setq subject (substring subject (match-end 0))))
1176       (insert subject)
1177       (inline (gnus-simplify-buffer-fuzzy))
1178       (buffer-string))))
1179
1180 (defsubst gnus-simplify-subject-fully (subject)
1181   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1182   (cond
1183    (gnus-simplify-subject-functions
1184     (gnus-map-function gnus-simplify-subject-functions subject))
1185    ((null gnus-summary-gather-subject-limit)
1186     (gnus-simplify-subject-re subject))
1187    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1188     (gnus-simplify-subject-fuzzy subject))
1189    ((numberp gnus-summary-gather-subject-limit)
1190     (gnus-limit-string (gnus-simplify-subject-re subject)
1191                        gnus-summary-gather-subject-limit))
1192    (t
1193     subject)))
1194
1195 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1196   "Check whether two subjects are equal.
1197 If optional argument simple-first is t, first argument is already
1198 simplified."
1199   (cond
1200    ((null simple-first)
1201     (equal (gnus-simplify-subject-fully s1)
1202            (gnus-simplify-subject-fully s2)))
1203    (t
1204     (equal s1
1205            (gnus-simplify-subject-fully s2)))))
1206
1207 (defun gnus-summary-bubble-group ()
1208   "Increase the score of the current group.
1209 This is a handy function to add to `gnus-summary-exit-hook' to
1210 increase the score of each group you read."
1211   (gnus-group-add-score gnus-newsgroup-name))
1212
1213 \f
1214 ;;;
1215 ;;; Gnus summary mode
1216 ;;;
1217
1218 (put 'gnus-summary-mode 'mode-class 'special)
1219
1220 (when t
1221   ;; Non-orthogonal keys
1222
1223   (gnus-define-keys gnus-summary-mode-map
1224     " " gnus-summary-next-page
1225     "\177" gnus-summary-prev-page
1226     [delete] gnus-summary-prev-page
1227     [backspace] gnus-summary-prev-page
1228     "\r" gnus-summary-scroll-up
1229     "\M-\r" gnus-summary-scroll-down
1230     "n" gnus-summary-next-unread-article
1231     "p" gnus-summary-prev-unread-article
1232     "N" gnus-summary-next-article
1233     "P" gnus-summary-prev-article
1234     "\M-\C-n" gnus-summary-next-same-subject
1235     "\M-\C-p" gnus-summary-prev-same-subject
1236     "\M-n" gnus-summary-next-unread-subject
1237     "\M-p" gnus-summary-prev-unread-subject
1238     "." gnus-summary-first-unread-article
1239     "," gnus-summary-best-unread-article
1240     "\M-s" gnus-summary-search-article-forward
1241     "\M-r" gnus-summary-search-article-backward
1242     "<" gnus-summary-beginning-of-article
1243     ">" gnus-summary-end-of-article
1244     "j" gnus-summary-goto-article
1245     "^" gnus-summary-refer-parent-article
1246     "\M-^" gnus-summary-refer-article
1247     "u" gnus-summary-tick-article-forward
1248     "!" gnus-summary-tick-article-forward
1249     "U" gnus-summary-tick-article-backward
1250     "d" gnus-summary-mark-as-read-forward
1251     "D" gnus-summary-mark-as-read-backward
1252     "E" gnus-summary-mark-as-expirable
1253     "\M-u" gnus-summary-clear-mark-forward
1254     "\M-U" gnus-summary-clear-mark-backward
1255     "k" gnus-summary-kill-same-subject-and-select
1256     "\C-k" gnus-summary-kill-same-subject
1257     "\M-\C-k" gnus-summary-kill-thread
1258     "\M-\C-l" gnus-summary-lower-thread
1259     "e" gnus-summary-edit-article
1260     "#" gnus-summary-mark-as-processable
1261     "\M-#" gnus-summary-unmark-as-processable
1262     "\M-\C-t" gnus-summary-toggle-threads
1263     "\M-\C-s" gnus-summary-show-thread
1264     "\M-\C-h" gnus-summary-hide-thread
1265     "\M-\C-f" gnus-summary-next-thread
1266     "\M-\C-b" gnus-summary-prev-thread
1267     "\M-\C-u" gnus-summary-up-thread
1268     "\M-\C-d" gnus-summary-down-thread
1269     "&" gnus-summary-execute-command
1270     "c" gnus-summary-catchup-and-exit
1271     "\C-w" gnus-summary-mark-region-as-read
1272     "\C-t" gnus-summary-toggle-truncation
1273     "?" gnus-summary-mark-as-dormant
1274     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1275     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1276     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1277     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1278     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1279     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1280     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1281     "=" gnus-summary-expand-window
1282     "\C-x\C-s" gnus-summary-reselect-current-group
1283     "\M-g" gnus-summary-rescan-group
1284     "w" gnus-summary-stop-page-breaking
1285     "\C-c\C-r" gnus-summary-caesar-message
1286     "f" gnus-summary-followup
1287     "F" gnus-summary-followup-with-original
1288     "C" gnus-summary-cancel-article
1289     "r" gnus-summary-reply
1290     "R" gnus-summary-reply-with-original
1291     "\C-c\C-f" gnus-summary-mail-forward
1292     "o" gnus-summary-save-article
1293     "\C-o" gnus-summary-save-article-mail
1294     "|" gnus-summary-pipe-output
1295     "\M-k" gnus-summary-edit-local-kill
1296     "\M-K" gnus-summary-edit-global-kill
1297     ;; "V" gnus-version
1298     "\C-c\C-d" gnus-summary-describe-group
1299     "q" gnus-summary-exit
1300     "Q" gnus-summary-exit-no-update
1301     "\C-c\C-i" gnus-info-find-node
1302     gnus-mouse-2 gnus-mouse-pick-article
1303     "m" gnus-summary-mail-other-window
1304     "a" gnus-summary-post-news
1305     "x" gnus-summary-limit-to-unread
1306     "s" gnus-summary-isearch-article
1307     "t" gnus-article-hide-headers
1308     "g" gnus-summary-show-article
1309     "l" gnus-summary-goto-last-article
1310     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1311     "\C-d" gnus-summary-enter-digest-group
1312     "\M-\C-d" gnus-summary-read-document
1313     "\M-\C-e" gnus-summary-edit-parameters
1314     "\M-\C-g" gnus-summary-customize-parameters
1315     "\C-c\C-b" gnus-bug
1316     "*" gnus-cache-enter-article
1317     "\M-*" gnus-cache-remove-article
1318     "\M-&" gnus-summary-universal-argument
1319     "\C-l" gnus-recenter
1320     "I" gnus-summary-increase-score
1321     "L" gnus-summary-lower-score
1322     "\M-i" gnus-symbolic-argument
1323     "h" gnus-summary-select-article-buffer
1324
1325     "b" gnus-article-view-part
1326     "\M-t" gnus-summary-toggle-display-buttonized
1327     
1328     "V" gnus-summary-score-map
1329     "X" gnus-uu-extract-map
1330     "S" gnus-summary-send-map)
1331
1332   ;; Sort of orthogonal keymap
1333   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1334     "t" gnus-summary-tick-article-forward
1335     "!" gnus-summary-tick-article-forward
1336     "d" gnus-summary-mark-as-read-forward
1337     "r" gnus-summary-mark-as-read-forward
1338     "c" gnus-summary-clear-mark-forward
1339     " " gnus-summary-clear-mark-forward
1340     "e" gnus-summary-mark-as-expirable
1341     "x" gnus-summary-mark-as-expirable
1342     "?" gnus-summary-mark-as-dormant
1343     "b" gnus-summary-set-bookmark
1344     "B" gnus-summary-remove-bookmark
1345     "#" gnus-summary-mark-as-processable
1346     "\M-#" gnus-summary-unmark-as-processable
1347     "S" gnus-summary-limit-include-expunged
1348     "C" gnus-summary-catchup
1349     "H" gnus-summary-catchup-to-here
1350     "\C-c" gnus-summary-catchup-all
1351     "k" gnus-summary-kill-same-subject-and-select
1352     "K" gnus-summary-kill-same-subject
1353     "P" gnus-uu-mark-map)
1354
1355   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1356     "c" gnus-summary-clear-above
1357     "u" gnus-summary-tick-above
1358     "m" gnus-summary-mark-above
1359     "k" gnus-summary-kill-below)
1360
1361   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1362     "/" gnus-summary-limit-to-subject
1363     "n" gnus-summary-limit-to-articles
1364     "w" gnus-summary-pop-limit
1365     "s" gnus-summary-limit-to-subject
1366     "a" gnus-summary-limit-to-author
1367     "u" gnus-summary-limit-to-unread
1368     "m" gnus-summary-limit-to-marks
1369     "v" gnus-summary-limit-to-score
1370     "*" gnus-summary-limit-include-cached
1371     "D" gnus-summary-limit-include-dormant
1372     "T" gnus-summary-limit-include-thread
1373     "d" gnus-summary-limit-exclude-dormant
1374     "t" gnus-summary-limit-to-age
1375     "E" gnus-summary-limit-include-expunged
1376     "c" gnus-summary-limit-exclude-childless-dormant
1377     "C" gnus-summary-limit-mark-excluded-as-read)
1378
1379   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1380     "n" gnus-summary-next-unread-article
1381     "p" gnus-summary-prev-unread-article
1382     "N" gnus-summary-next-article
1383     "P" gnus-summary-prev-article
1384     "\C-n" gnus-summary-next-same-subject
1385     "\C-p" gnus-summary-prev-same-subject
1386     "\M-n" gnus-summary-next-unread-subject
1387     "\M-p" gnus-summary-prev-unread-subject
1388     "f" gnus-summary-first-unread-article
1389     "b" gnus-summary-best-unread-article
1390     "j" gnus-summary-goto-article
1391     "g" gnus-summary-goto-subject
1392     "l" gnus-summary-goto-last-article
1393     "o" gnus-summary-pop-article)
1394
1395   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1396     "k" gnus-summary-kill-thread
1397     "l" gnus-summary-lower-thread
1398     "i" gnus-summary-raise-thread
1399     "T" gnus-summary-toggle-threads
1400     "t" gnus-summary-rethread-current
1401     "^" gnus-summary-reparent-thread
1402     "s" gnus-summary-show-thread
1403     "S" gnus-summary-show-all-threads
1404     "h" gnus-summary-hide-thread
1405     "H" gnus-summary-hide-all-threads
1406     "n" gnus-summary-next-thread
1407     "p" gnus-summary-prev-thread
1408     "u" gnus-summary-up-thread
1409     "o" gnus-summary-top-thread
1410     "d" gnus-summary-down-thread
1411     "#" gnus-uu-mark-thread
1412     "\M-#" gnus-uu-unmark-thread)
1413
1414   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1415     "g" gnus-summary-prepare
1416     "c" gnus-summary-insert-cached-articles)
1417
1418   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1419     "c" gnus-summary-catchup-and-exit
1420     "C" gnus-summary-catchup-all-and-exit
1421     "E" gnus-summary-exit-no-update
1422     "Q" gnus-summary-exit
1423     "Z" gnus-summary-exit
1424     "n" gnus-summary-catchup-and-goto-next-group
1425     "R" gnus-summary-reselect-current-group
1426     "G" gnus-summary-rescan-group
1427     "N" gnus-summary-next-group
1428     "s" gnus-summary-save-newsrc
1429     "P" gnus-summary-prev-group)
1430
1431   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1432     " " gnus-summary-next-page
1433     "n" gnus-summary-next-page
1434     "\177" gnus-summary-prev-page
1435     [delete] gnus-summary-prev-page
1436     "p" gnus-summary-prev-page
1437     "\r" gnus-summary-scroll-up
1438     "\M-\r" gnus-summary-scroll-down
1439     "<" gnus-summary-beginning-of-article
1440     ">" gnus-summary-end-of-article
1441     "b" gnus-summary-beginning-of-article
1442     "e" gnus-summary-end-of-article
1443     "^" gnus-summary-refer-parent-article
1444     "r" gnus-summary-refer-parent-article
1445     "R" gnus-summary-refer-references
1446     "T" gnus-summary-refer-thread
1447     "g" gnus-summary-show-article
1448     "s" gnus-summary-isearch-article
1449     "P" gnus-summary-print-article)
1450
1451   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1452     "b" gnus-article-add-buttons
1453     "B" gnus-article-add-buttons-to-head
1454     "o" gnus-article-treat-overstrike
1455     "e" gnus-article-emphasize
1456     "w" gnus-article-fill-cited-article
1457     "c" gnus-article-remove-cr
1458     "q" gnus-article-de-quoted-unreadable
1459     "f" gnus-article-display-x-face
1460     "l" gnus-summary-stop-page-breaking
1461     "r" gnus-summary-caesar-message
1462     "t" gnus-article-hide-headers
1463     "v" gnus-summary-verbose-headers
1464     "h" gnus-article-treat-html
1465     "d" gnus-article-treat-dumbquotes)
1466
1467   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1468     "a" gnus-article-hide
1469     "h" gnus-article-hide-headers
1470     "b" gnus-article-hide-boring-headers
1471     "s" gnus-article-hide-signature
1472     "c" gnus-article-hide-citation
1473     "C" gnus-article-hide-citation-in-followups
1474     "p" gnus-article-hide-pgp
1475     "P" gnus-article-hide-pem
1476     "\C-c" gnus-article-hide-citation-maybe)
1477
1478   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1479     "a" gnus-article-highlight
1480     "h" gnus-article-highlight-headers
1481     "c" gnus-article-highlight-citation
1482     "s" gnus-article-highlight-signature)
1483
1484   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1485     "w" gnus-article-decode-mime-words
1486     "c" gnus-article-decode-charset
1487     "v" gnus-mime-view-all-parts
1488     "b" gnus-article-view-part)
1489
1490   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1491     "z" gnus-article-date-ut
1492     "u" gnus-article-date-ut
1493     "l" gnus-article-date-local
1494     "e" gnus-article-date-lapsed
1495     "o" gnus-article-date-original
1496     "i" gnus-article-date-iso8601
1497     "s" gnus-article-date-user)
1498
1499   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1500     "t" gnus-article-remove-trailing-blank-lines
1501     "l" gnus-article-strip-leading-blank-lines
1502     "m" gnus-article-strip-multiple-blank-lines
1503     "a" gnus-article-strip-blank-lines
1504     "A" gnus-article-strip-all-blank-lines
1505     "s" gnus-article-strip-leading-space
1506     "e" gnus-article-strip-trailing-space)
1507
1508   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1509     "v" gnus-version
1510     "f" gnus-summary-fetch-faq
1511     "d" gnus-summary-describe-group
1512     "h" gnus-summary-describe-briefly
1513     "i" gnus-info-find-node)
1514
1515   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1516     "e" gnus-summary-expire-articles
1517     "\M-\C-e" gnus-summary-expire-articles-now
1518     "\177" gnus-summary-delete-article
1519     [delete] gnus-summary-delete-article
1520     "m" gnus-summary-move-article
1521     "r" gnus-summary-respool-article
1522     "w" gnus-summary-edit-article
1523     "c" gnus-summary-copy-article
1524     "B" gnus-summary-crosspost-article
1525     "q" gnus-summary-respool-query
1526     "t" gnus-summary-respool-trace
1527     "i" gnus-summary-import-article
1528     "p" gnus-summary-article-posted-p)
1529
1530   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1531     "o" gnus-summary-save-article
1532     "m" gnus-summary-save-article-mail
1533     "F" gnus-summary-write-article-file
1534     "r" gnus-summary-save-article-rmail
1535     "f" gnus-summary-save-article-file
1536     "b" gnus-summary-save-article-body-file
1537     "h" gnus-summary-save-article-folder
1538     "v" gnus-summary-save-article-vm
1539     "p" gnus-summary-pipe-output
1540     "s" gnus-soup-add-article)
1541
1542   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1543     "b" gnus-summary-display-buttonized
1544     "m" gnus-summary-repair-multipart
1545     "v" gnus-article-view-part
1546     "o" gnus-article-save-part
1547     "c" gnus-article-copy-part
1548     "e" gnus-article-externalize-part
1549     "|" gnus-article-pipe-part)
1550   )
1551
1552 (defun gnus-summary-make-menu-bar ()
1553   (gnus-turn-off-edit-menu 'summary)
1554
1555   (unless (boundp 'gnus-summary-misc-menu)
1556
1557     (easy-menu-define
1558      gnus-summary-kill-menu gnus-summary-mode-map ""
1559      (cons
1560       "Score"
1561       (nconc
1562        (list
1563         ["Enter score..." gnus-summary-score-entry t]
1564         ["Customize" gnus-score-customize t])
1565        (gnus-make-score-map 'increase)
1566        (gnus-make-score-map 'lower)
1567        '(("Mark"
1568           ["Kill below" gnus-summary-kill-below t]
1569           ["Mark above" gnus-summary-mark-above t]
1570           ["Tick above" gnus-summary-tick-above t]
1571           ["Clear above" gnus-summary-clear-above t])
1572          ["Current score" gnus-summary-current-score t]
1573          ["Set score" gnus-summary-set-score t]
1574          ["Switch current score file..." gnus-score-change-score-file t]
1575          ["Set mark below..." gnus-score-set-mark-below t]
1576          ["Set expunge below..." gnus-score-set-expunge-below t]
1577          ["Edit current score file" gnus-score-edit-current-scores t]
1578          ["Edit score file" gnus-score-edit-file t]
1579          ["Trace score" gnus-score-find-trace t]
1580          ["Find words" gnus-score-find-favourite-words t]
1581          ["Rescore buffer" gnus-summary-rescore t]
1582          ["Increase score..." gnus-summary-increase-score t]
1583          ["Lower score..." gnus-summary-lower-score t]))))
1584
1585     ;; Define both the Article menu in the summary buffer and the equivalent
1586     ;; Commands menu in the article buffer here for consistency.
1587     (let ((innards
1588            '(("Hide"
1589               ["All" gnus-article-hide t]
1590               ["Headers" gnus-article-hide-headers t]
1591               ["Signature" gnus-article-hide-signature t]
1592               ["Citation" gnus-article-hide-citation t]
1593               ["PGP" gnus-article-hide-pgp t]
1594               ["Boring headers" gnus-article-hide-boring-headers t])
1595              ("Highlight"
1596               ["All" gnus-article-highlight t]
1597               ["Headers" gnus-article-highlight-headers t]
1598               ["Signature" gnus-article-highlight-signature t]
1599               ["Citation" gnus-article-highlight-citation t])
1600              ("MIME"
1601               ["Words" gnus-article-decode-mime-words t]
1602               ["Charset" gnus-article-decode-charset t]
1603               ["QP" gnus-article-de-quoted-unreadable t]
1604               ["View all" gnus-mime-view-all-parts t])
1605              ("Date"
1606               ["Local" gnus-article-date-local t]
1607               ["ISO8601" gnus-article-date-iso8601 t]
1608               ["UT" gnus-article-date-ut t]
1609               ["Original" gnus-article-date-original t]
1610               ["Lapsed" gnus-article-date-lapsed t]
1611               ["User-defined" gnus-article-date-user t])
1612              ("Washing"
1613               ("Remove Blanks"
1614                ["Leading" gnus-article-strip-leading-blank-lines t]
1615                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1616                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1617                ["All of the above" gnus-article-strip-blank-lines t]
1618                ["All" gnus-article-strip-all-blank-lines t]
1619                ["Leading space" gnus-article-strip-leading-space t]
1620                ["Trailing space" gnus-article-strip-trailing-space t])
1621               ["Overstrike" gnus-article-treat-overstrike t]
1622               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1623               ["Emphasis" gnus-article-emphasize t]
1624               ["Word wrap" gnus-article-fill-cited-article t]
1625               ["CR" gnus-article-remove-cr t]
1626               ["Show X-Face" gnus-article-display-x-face t]
1627               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1628               ["UnHTMLize" gnus-article-treat-html t]
1629               ["Rot 13" gnus-summary-caesar-message t]
1630               ["Unix pipe" gnus-summary-pipe-message t]
1631               ["Add buttons" gnus-article-add-buttons t]
1632               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1633               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1634               ["Verbose header" gnus-summary-verbose-headers t]
1635               ["Toggle header" gnus-summary-toggle-header t])
1636              ("Output"
1637               ["Save in default format" gnus-summary-save-article t]
1638               ["Save in file" gnus-summary-save-article-file t]
1639               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1640               ["Save in MH folder" gnus-summary-save-article-folder t]
1641               ["Save in VM folder" gnus-summary-save-article-vm t]
1642               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1643               ["Save body in file" gnus-summary-save-article-body-file t]
1644               ["Pipe through a filter" gnus-summary-pipe-output t]
1645               ["Add to SOUP packet" gnus-soup-add-article t]
1646               ["Print" gnus-summary-print-article t])
1647              ("Backend"
1648               ["Respool article..." gnus-summary-respool-article t]
1649               ["Move article..." gnus-summary-move-article
1650                (gnus-check-backend-function
1651                 'request-move-article gnus-newsgroup-name)]
1652               ["Copy article..." gnus-summary-copy-article t]
1653               ["Crosspost article..." gnus-summary-crosspost-article
1654                (gnus-check-backend-function
1655                 'request-replace-article gnus-newsgroup-name)]
1656               ["Import file..." gnus-summary-import-article t]
1657               ["Check if posted" gnus-summary-article-posted-p t]
1658               ["Edit article" gnus-summary-edit-article
1659                (not (gnus-group-read-only-p))]
1660               ["Delete article" gnus-summary-delete-article
1661                (gnus-check-backend-function
1662                 'request-expire-articles gnus-newsgroup-name)]
1663               ["Query respool" gnus-summary-respool-query t]
1664               ["Trace respool" gnus-summary-respool-trace t]
1665               ["Delete expirable articles" gnus-summary-expire-articles-now
1666                (gnus-check-backend-function
1667                 'request-expire-articles gnus-newsgroup-name)])
1668              ("Extract"
1669               ["Uudecode" gnus-uu-decode-uu t]
1670               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1671               ["Unshar" gnus-uu-decode-unshar t]
1672               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1673               ["Save" gnus-uu-decode-save t]
1674               ["Binhex" gnus-uu-decode-binhex t]
1675               ["Postscript" gnus-uu-decode-postscript t])
1676              ("Cache"
1677               ["Enter article" gnus-cache-enter-article t]
1678               ["Remove article" gnus-cache-remove-article t])
1679              ["Select article buffer" gnus-summary-select-article-buffer t]
1680              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1681              ["Isearch article..." gnus-summary-isearch-article t]
1682              ["Beginning of the article" gnus-summary-beginning-of-article t]
1683              ["End of the article" gnus-summary-end-of-article t]
1684              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1685              ["Fetch referenced articles" gnus-summary-refer-references t]
1686              ["Fetch current thread" gnus-summary-refer-thread t]
1687              ["Fetch article with id..." gnus-summary-refer-article t]
1688              ["Redisplay" gnus-summary-show-article t])))
1689       (easy-menu-define
1690        gnus-summary-article-menu gnus-summary-mode-map ""
1691        (cons "Article" innards))
1692
1693       (easy-menu-define
1694        gnus-article-commands-menu gnus-article-mode-map ""
1695        (cons "Commands" innards)))
1696
1697     (easy-menu-define
1698      gnus-summary-thread-menu gnus-summary-mode-map ""
1699      '("Threads"
1700        ["Toggle threading" gnus-summary-toggle-threads t]
1701        ["Hide threads" gnus-summary-hide-all-threads t]
1702        ["Show threads" gnus-summary-show-all-threads t]
1703        ["Hide thread" gnus-summary-hide-thread t]
1704        ["Show thread" gnus-summary-show-thread t]
1705        ["Go to next thread" gnus-summary-next-thread t]
1706        ["Go to previous thread" gnus-summary-prev-thread t]
1707        ["Go down thread" gnus-summary-down-thread t]
1708        ["Go up thread" gnus-summary-up-thread t]
1709        ["Top of thread" gnus-summary-top-thread t]
1710        ["Mark thread as read" gnus-summary-kill-thread t]
1711        ["Lower thread score" gnus-summary-lower-thread t]
1712        ["Raise thread score" gnus-summary-raise-thread t]
1713        ["Rethread current" gnus-summary-rethread-current t]
1714        ))
1715
1716     (easy-menu-define
1717      gnus-summary-post-menu gnus-summary-mode-map ""
1718      '("Post"
1719        ["Post an article" gnus-summary-post-news t]
1720        ["Followup" gnus-summary-followup t]
1721        ["Followup and yank" gnus-summary-followup-with-original t]
1722        ["Supersede article" gnus-summary-supersede-article t]
1723        ["Cancel article" gnus-summary-cancel-article t]
1724        ["Reply" gnus-summary-reply t]
1725        ["Reply and yank" gnus-summary-reply-with-original t]
1726        ["Wide reply" gnus-summary-wide-reply t]
1727        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1728        ["Mail forward" gnus-summary-mail-forward t]
1729        ["Post forward" gnus-summary-post-forward t]
1730        ["Digest and mail" gnus-uu-digest-mail-forward t]
1731        ["Digest and post" gnus-uu-digest-post-forward t]
1732        ["Resend message" gnus-summary-resend-message t]
1733        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1734        ["Send a mail" gnus-summary-mail-other-window t]
1735        ["Uuencode and post" gnus-uu-post-news t]
1736        ["Followup via news" gnus-summary-followup-to-mail t]
1737        ["Followup via news and yank"
1738         gnus-summary-followup-to-mail-with-original t]
1739        ;;("Draft"
1740        ;;["Send" gnus-summary-send-draft t]
1741        ;;["Send bounced" gnus-resend-bounced-mail t])
1742        ))
1743
1744     (easy-menu-define
1745      gnus-summary-misc-menu gnus-summary-mode-map ""
1746      '("Misc"
1747        ("Mark Read"
1748         ["Mark as read" gnus-summary-mark-as-read-forward t]
1749         ["Mark same subject and select"
1750          gnus-summary-kill-same-subject-and-select t]
1751         ["Mark same subject" gnus-summary-kill-same-subject t]
1752         ["Catchup" gnus-summary-catchup t]
1753         ["Catchup all" gnus-summary-catchup-all t]
1754         ["Catchup to here" gnus-summary-catchup-to-here t]
1755         ["Catchup region" gnus-summary-mark-region-as-read t]
1756         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1757        ("Mark Various"
1758         ["Tick" gnus-summary-tick-article-forward t]
1759         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1760         ["Remove marks" gnus-summary-clear-mark-forward t]
1761         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1762         ["Set bookmark" gnus-summary-set-bookmark t]
1763         ["Remove bookmark" gnus-summary-remove-bookmark t])
1764        ("Mark Limit"
1765         ["Marks..." gnus-summary-limit-to-marks t]
1766         ["Subject..." gnus-summary-limit-to-subject t]
1767         ["Author..." gnus-summary-limit-to-author t]
1768         ["Age..." gnus-summary-limit-to-age t]
1769         ["Score" gnus-summary-limit-to-score t]
1770         ["Unread" gnus-summary-limit-to-unread t]
1771         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1772         ["Articles" gnus-summary-limit-to-articles t]
1773         ["Pop limit" gnus-summary-pop-limit t]
1774         ["Show dormant" gnus-summary-limit-include-dormant t]
1775         ["Hide childless dormant"
1776          gnus-summary-limit-exclude-childless-dormant t]
1777         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1778         ["Show expunged" gnus-summary-show-all-expunged t])
1779        ("Process Mark"
1780         ["Set mark" gnus-summary-mark-as-processable t]
1781         ["Remove mark" gnus-summary-unmark-as-processable t]
1782         ["Remove all marks" gnus-summary-unmark-all-processable t]
1783         ["Mark above" gnus-uu-mark-over t]
1784         ["Mark series" gnus-uu-mark-series t]
1785         ["Mark region" gnus-uu-mark-region t]
1786         ["Unmark region" gnus-uu-unmark-region t]
1787         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1788         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1789         ["Mark all" gnus-uu-mark-all t]
1790         ["Mark buffer" gnus-uu-mark-buffer t]
1791         ["Mark sparse" gnus-uu-mark-sparse t]
1792         ["Mark thread" gnus-uu-mark-thread t]
1793         ["Unmark thread" gnus-uu-unmark-thread t]
1794         ("Process Mark Sets"
1795          ["Kill" gnus-summary-kill-process-mark t]
1796          ["Yank" gnus-summary-yank-process-mark
1797           gnus-newsgroup-process-stack]
1798          ["Save" gnus-summary-save-process-mark t]))
1799        ("Scroll article"
1800         ["Page forward" gnus-summary-next-page t]
1801         ["Page backward" gnus-summary-prev-page t]
1802         ["Line forward" gnus-summary-scroll-up t])
1803        ("Move"
1804         ["Next unread article" gnus-summary-next-unread-article t]
1805         ["Previous unread article" gnus-summary-prev-unread-article t]
1806         ["Next article" gnus-summary-next-article t]
1807         ["Previous article" gnus-summary-prev-article t]
1808         ["Next unread subject" gnus-summary-next-unread-subject t]
1809         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1810         ["Next article same subject" gnus-summary-next-same-subject t]
1811         ["Previous article same subject" gnus-summary-prev-same-subject t]
1812         ["First unread article" gnus-summary-first-unread-article t]
1813         ["Best unread article" gnus-summary-best-unread-article t]
1814         ["Go to subject number..." gnus-summary-goto-subject t]
1815         ["Go to article number..." gnus-summary-goto-article t]
1816         ["Go to the last article" gnus-summary-goto-last-article t]
1817         ["Pop article off history" gnus-summary-pop-article t])
1818        ("Sort"
1819         ["Sort by number" gnus-summary-sort-by-number t]
1820         ["Sort by author" gnus-summary-sort-by-author t]
1821         ["Sort by subject" gnus-summary-sort-by-subject t]
1822         ["Sort by date" gnus-summary-sort-by-date t]
1823         ["Sort by score" gnus-summary-sort-by-score t]
1824         ["Sort by lines" gnus-summary-sort-by-lines t])
1825        ("Help"
1826         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1827         ["Describe group" gnus-summary-describe-group t]
1828         ["Read manual" gnus-info-find-node t])
1829        ("Modes"
1830         ["Pick and read" gnus-pick-mode t]
1831         ["Binary" gnus-binary-mode t])
1832        ("Regeneration"
1833         ["Regenerate" gnus-summary-prepare t]
1834         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1835         ["Toggle threading" gnus-summary-toggle-threads t])
1836        ["Filter articles..." gnus-summary-execute-command t]
1837        ["Run command on subjects..." gnus-summary-universal-argument t]
1838        ["Search articles forward..." gnus-summary-search-article-forward t]
1839        ["Search articles backward..." gnus-summary-search-article-backward t]
1840        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1841        ["Expand window" gnus-summary-expand-window t]
1842        ["Expire expirable articles" gnus-summary-expire-articles
1843         (gnus-check-backend-function
1844          'request-expire-articles gnus-newsgroup-name)]
1845        ["Edit local kill file" gnus-summary-edit-local-kill t]
1846        ["Edit main kill file" gnus-summary-edit-global-kill t]
1847        ["Edit group parameters" gnus-summary-edit-parameters t]
1848        ["Customize group parameters" gnus-summary-customize-parameters t]
1849        ["Send a bug report" gnus-bug t]
1850        ("Exit"
1851         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1852         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1853         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1854         ["Exit group" gnus-summary-exit t]
1855         ["Exit group without updating" gnus-summary-exit-no-update t]
1856         ["Exit and goto next group" gnus-summary-next-group t]
1857         ["Exit and goto prev group" gnus-summary-prev-group t]
1858         ["Reselect group" gnus-summary-reselect-current-group t]
1859         ["Rescan group" gnus-summary-rescan-group t]
1860         ["Update dribble" gnus-summary-save-newsrc t])))
1861
1862     (gnus-run-hooks 'gnus-summary-menu-hook)))
1863
1864 (defun gnus-score-set-default (var value)
1865   "A version of set that updates the GNU Emacs menu-bar."
1866   (set var value)
1867   ;; It is the message that forces the active status to be updated.
1868   (message ""))
1869
1870 (defun gnus-make-score-map (type)
1871   "Make a summary score map of type TYPE."
1872   (if t
1873       nil
1874     (let ((headers '(("author" "from" string)
1875                      ("subject" "subject" string)
1876                      ("article body" "body" string)
1877                      ("article head" "head" string)
1878                      ("xref" "xref" string)
1879                      ("extra header" "extra" string)
1880                      ("lines" "lines" number)
1881                      ("followups to author" "followup" string)))
1882           (types '((number ("less than" <)
1883                            ("greater than" >)
1884                            ("equal" =))
1885                    (string ("substring" s)
1886                            ("exact string" e)
1887                            ("fuzzy string" f)
1888                            ("regexp" r))))
1889           (perms '(("temporary" (current-time-string))
1890                    ("permanent" nil)
1891                    ("immediate" now)))
1892           header)
1893       (list
1894        (apply
1895         'nconc
1896         (list
1897          (if (eq type 'lower)
1898              "Lower score"
1899            "Increase score"))
1900         (let (outh)
1901           (while headers
1902             (setq header (car headers))
1903             (setq outh
1904                   (cons
1905                    (apply
1906                     'nconc
1907                     (list (car header))
1908                     (let ((ts (cdr (assoc (nth 2 header) types)))
1909                           outt)
1910                       (while ts
1911                         (setq outt
1912                               (cons
1913                                (apply
1914                                 'nconc
1915                                 (list (caar ts))
1916                                 (let ((ps perms)
1917                                       outp)
1918                                   (while ps
1919                                     (setq outp
1920                                           (cons
1921                                            (vector
1922                                             (caar ps)
1923                                             (list
1924                                              'gnus-summary-score-entry
1925                                              (nth 1 header)
1926                                              (if (or (string= (nth 1 header)
1927                                                               "head")
1928                                                      (string= (nth 1 header)
1929                                                               "body"))
1930                                                  ""
1931                                                (list 'gnus-summary-header
1932                                                      (nth 1 header)))
1933                                              (list 'quote (nth 1 (car ts)))
1934                                              (list 'gnus-score-default nil)
1935                                              (nth 1 (car ps))
1936                                              t)
1937                                             t)
1938                                            outp))
1939                                     (setq ps (cdr ps)))
1940                                   (list (nreverse outp))))
1941                                outt))
1942                         (setq ts (cdr ts)))
1943                       (list (nreverse outt))))
1944                    outh))
1945             (setq headers (cdr headers)))
1946           (list (nreverse outh))))))))
1947
1948 \f
1949
1950 (defun gnus-summary-mode (&optional group)
1951   "Major mode for reading articles.
1952
1953 All normal editing commands are switched off.
1954 \\<gnus-summary-mode-map>
1955 Each line in this buffer represents one article.  To read an
1956 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1957 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1958 respectively.
1959
1960 You can also post articles and send mail from this buffer.  To
1961 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1962 of an article, type `\\[gnus-summary-reply]'.
1963
1964 There are approx. one gazillion commands you can execute in this
1965 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1966
1967 The following commands are available:
1968
1969 \\{gnus-summary-mode-map}"
1970   (interactive)
1971   (when (gnus-visual-p 'summary-menu 'menu)
1972     (gnus-summary-make-menu-bar))
1973   (kill-all-local-variables)
1974   (gnus-summary-make-local-variables)
1975   (gnus-make-thread-indent-array)
1976   (gnus-simplify-mode-line)
1977   (setq major-mode 'gnus-summary-mode)
1978   (setq mode-name "Summary")
1979   (make-local-variable 'minor-mode-alist)
1980   (use-local-map gnus-summary-mode-map)
1981   (buffer-disable-undo)
1982   (setq buffer-read-only t)             ;Disable modification
1983   (setq truncate-lines t)
1984   (setq selective-display t)
1985   (setq selective-display-ellipses t)   ;Display `...'
1986   (gnus-summary-set-display-table)
1987   (gnus-set-default-directory)
1988   (setq gnus-newsgroup-name group)
1989   (make-local-variable 'gnus-summary-line-format)
1990   (make-local-variable 'gnus-summary-line-format-spec)
1991   (make-local-variable 'gnus-summary-dummy-line-format)
1992   (make-local-variable 'gnus-summary-dummy-line-format-spec)
1993   (make-local-variable 'gnus-summary-mark-positions)
1994   (make-local-hook 'post-command-hook)
1995   (add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
1996   (make-local-hook 'pre-command-hook)
1997   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
1998   (gnus-run-hooks 'gnus-summary-mode-hook)
1999   (mm-enable-multibyte)
2000   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2001   (gnus-update-summary-mark-positions))
2002
2003 (defun gnus-summary-make-local-variables ()
2004   "Make all the local summary buffer variables."
2005   (let ((locals gnus-summary-local-variables)
2006         global local)
2007     (while (setq local (pop locals))
2008       (if (consp local)
2009           (progn
2010             (if (eq (cdr local) 'global)
2011                 ;; Copy the global value of the variable.
2012                 (setq global (symbol-value (car local)))
2013               ;; Use the value from the list.
2014               (setq global (eval (cdr local))))
2015             (make-local-variable (car local))
2016             (set (car local) global))
2017         ;; Simple nil-valued local variable.
2018         (make-local-variable local)
2019         (set local nil)))))
2020
2021 (defun gnus-summary-clear-local-variables ()
2022   (let ((locals gnus-summary-local-variables))
2023     (while locals
2024       (if (consp (car locals))
2025           (and (vectorp (caar locals))
2026                (set (caar locals) nil))
2027         (and (vectorp (car locals))
2028              (set (car locals) nil)))
2029       (setq locals (cdr locals)))))
2030
2031 ;; Summary data functions.
2032
2033 (defmacro gnus-data-number (data)
2034   `(car ,data))
2035
2036 (defmacro gnus-data-set-number (data number)
2037   `(setcar ,data ,number))
2038
2039 (defmacro gnus-data-mark (data)
2040   `(nth 1 ,data))
2041
2042 (defmacro gnus-data-set-mark (data mark)
2043   `(setcar (nthcdr 1 ,data) ,mark))
2044
2045 (defmacro gnus-data-pos (data)
2046   `(nth 2 ,data))
2047
2048 (defmacro gnus-data-set-pos (data pos)
2049   `(setcar (nthcdr 2 ,data) ,pos))
2050
2051 (defmacro gnus-data-header (data)
2052   `(nth 3 ,data))
2053
2054 (defmacro gnus-data-set-header (data header)
2055   `(setf (nth 3 ,data) ,header))
2056
2057 (defmacro gnus-data-level (data)
2058   `(nth 4 ,data))
2059
2060 (defmacro gnus-data-unread-p (data)
2061   `(= (nth 1 ,data) gnus-unread-mark))
2062
2063 (defmacro gnus-data-read-p (data)
2064   `(/= (nth 1 ,data) gnus-unread-mark))
2065
2066 (defmacro gnus-data-pseudo-p (data)
2067   `(consp (nth 3 ,data)))
2068
2069 (defmacro gnus-data-find (number)
2070   `(assq ,number gnus-newsgroup-data))
2071
2072 (defmacro gnus-data-find-list (number &optional data)
2073   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2074      (memq (assq ,number bdata)
2075            bdata)))
2076
2077 (defmacro gnus-data-make (number mark pos header level)
2078   `(list ,number ,mark ,pos ,header ,level))
2079
2080 (defun gnus-data-enter (after-article number mark pos header level offset)
2081   (let ((data (gnus-data-find-list after-article)))
2082     (unless data
2083       (error "No such article: %d" after-article))
2084     (setcdr data (cons (gnus-data-make number mark pos header level)
2085                        (cdr data)))
2086     (setq gnus-newsgroup-data-reverse nil)
2087     (gnus-data-update-list (cddr data) offset)))
2088
2089 (defun gnus-data-enter-list (after-article list &optional offset)
2090   (when list
2091     (let ((data (and after-article (gnus-data-find-list after-article)))
2092           (ilist list))
2093       (if (not (or data
2094                    after-article))
2095           (let ((odata gnus-newsgroup-data))
2096             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2097             (when offset
2098               (gnus-data-update-list odata offset)))
2099         ;; Find the last element in the list to be spliced into the main
2100         ;; list.
2101         (while (cdr list)
2102           (setq list (cdr list)))
2103         (if (not data)
2104             (progn
2105               (setcdr list gnus-newsgroup-data)
2106               (setq gnus-newsgroup-data ilist)
2107               (when offset
2108                 (gnus-data-update-list (cdr list) offset)))
2109           (setcdr list (cdr data))
2110           (setcdr data ilist)
2111           (when offset
2112             (gnus-data-update-list (cdr list) offset))))
2113       (setq gnus-newsgroup-data-reverse nil))))
2114
2115 (defun gnus-data-remove (article &optional offset)
2116   (let ((data gnus-newsgroup-data))
2117     (if (= (gnus-data-number (car data)) article)
2118         (progn
2119           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2120                 gnus-newsgroup-data-reverse nil)
2121           (when offset
2122             (gnus-data-update-list gnus-newsgroup-data offset)))
2123       (while (cdr data)
2124         (when (= (gnus-data-number (cadr data)) article)
2125           (setcdr data (cddr data))
2126           (when offset
2127             (gnus-data-update-list (cdr data) offset))
2128           (setq data nil
2129                 gnus-newsgroup-data-reverse nil))
2130         (setq data (cdr data))))))
2131
2132 (defmacro gnus-data-list (backward)
2133   `(if ,backward
2134        (or gnus-newsgroup-data-reverse
2135            (setq gnus-newsgroup-data-reverse
2136                  (reverse gnus-newsgroup-data)))
2137      gnus-newsgroup-data))
2138
2139 (defun gnus-data-update-list (data offset)
2140   "Add OFFSET to the POS of all data entries in DATA."
2141   (setq gnus-newsgroup-data-reverse nil)
2142   (while data
2143     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2144     (setq data (cdr data))))
2145
2146 (defun gnus-summary-article-pseudo-p (article)
2147   "Say whether this article is a pseudo article or not."
2148   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2149
2150 (defmacro gnus-summary-article-sparse-p (article)
2151   "Say whether this article is a sparse article or not."
2152   `(memq ,article gnus-newsgroup-sparse))
2153
2154 (defmacro gnus-summary-article-ancient-p (article)
2155   "Say whether this article is a sparse article or not."
2156   `(memq ,article gnus-newsgroup-ancient))
2157
2158 (defun gnus-article-parent-p (number)
2159   "Say whether this article is a parent or not."
2160   (let ((data (gnus-data-find-list number)))
2161     (and (cdr data)                     ; There has to be an article after...
2162          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2163             (gnus-data-level (nth 1 data))))))
2164
2165 (defun gnus-article-children (number)
2166   "Return a list of all children to NUMBER."
2167   (let* ((data (gnus-data-find-list number))
2168          (level (gnus-data-level (car data)))
2169          children)
2170     (setq data (cdr data))
2171     (while (and data
2172                 (= (gnus-data-level (car data)) (1+ level)))
2173       (push (gnus-data-number (car data)) children)
2174       (setq data (cdr data)))
2175     children))
2176
2177 (defmacro gnus-summary-skip-intangible ()
2178   "If the current article is intangible, then jump to a different article."
2179   '(let ((to (get-text-property (point) 'gnus-intangible)))
2180      (and to (gnus-summary-goto-subject to))))
2181
2182 (defmacro gnus-summary-article-intangible-p ()
2183   "Say whether this article is intangible or not."
2184   '(get-text-property (point) 'gnus-intangible))
2185
2186 (defun gnus-article-read-p (article)
2187   "Say whether ARTICLE is read or not."
2188   (not (or (memq article gnus-newsgroup-marked)
2189            (memq article gnus-newsgroup-unreads)
2190            (memq article gnus-newsgroup-unselected)
2191            (memq article gnus-newsgroup-dormant))))
2192
2193 ;; Some summary mode macros.
2194
2195 (defmacro gnus-summary-article-number ()
2196   "The article number of the article on the current line.
2197 If there isn's an article number here, then we return the current
2198 article number."
2199   '(progn
2200      (gnus-summary-skip-intangible)
2201      (or (get-text-property (point) 'gnus-number)
2202          (gnus-summary-last-subject))))
2203
2204 (defmacro gnus-summary-article-header (&optional number)
2205   "Return the header of article NUMBER."
2206   `(gnus-data-header (gnus-data-find
2207                       ,(or number '(gnus-summary-article-number)))))
2208
2209 (defmacro gnus-summary-thread-level (&optional number)
2210   "Return the level of thread that starts with article NUMBER."
2211   `(if (and (eq gnus-summary-make-false-root 'dummy)
2212             (get-text-property (point) 'gnus-intangible))
2213        0
2214      (gnus-data-level (gnus-data-find
2215                        ,(or number '(gnus-summary-article-number))))))
2216
2217 (defmacro gnus-summary-article-mark (&optional number)
2218   "Return the mark of article NUMBER."
2219   `(gnus-data-mark (gnus-data-find
2220                     ,(or number '(gnus-summary-article-number)))))
2221
2222 (defmacro gnus-summary-article-pos (&optional number)
2223   "Return the position of the line of article NUMBER."
2224   `(gnus-data-pos (gnus-data-find
2225                    ,(or number '(gnus-summary-article-number)))))
2226
2227 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2228 (defmacro gnus-summary-article-subject (&optional number)
2229   "Return current subject string or nil if nothing."
2230   `(let ((headers
2231           ,(if number
2232                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2233              '(gnus-data-header (assq (gnus-summary-article-number)
2234                                       gnus-newsgroup-data)))))
2235      (and headers
2236           (vectorp headers)
2237           (mail-header-subject headers))))
2238
2239 (defmacro gnus-summary-article-score (&optional number)
2240   "Return current article score."
2241   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2242                   gnus-newsgroup-scored))
2243        gnus-summary-default-score 0))
2244
2245 (defun gnus-summary-article-children (&optional number)
2246   "Return a list of article numbers that are children of article NUMBER."
2247   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2248          (level (gnus-data-level (car data)))
2249          l children)
2250     (while (and (setq data (cdr data))
2251                 (> (setq l (gnus-data-level (car data))) level))
2252       (and (= (1+ level) l)
2253            (push (gnus-data-number (car data))
2254                  children)))
2255     (nreverse children)))
2256
2257 (defun gnus-summary-article-parent (&optional number)
2258   "Return the article number of the parent of article NUMBER."
2259   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2260                                     (gnus-data-list t)))
2261          (level (gnus-data-level (car data))))
2262     (if (zerop level)
2263         ()                              ; This is a root.
2264       ;; We search until we find an article with a level less than
2265       ;; this one.  That function has to be the parent.
2266       (while (and (setq data (cdr data))
2267                   (not (< (gnus-data-level (car data)) level))))
2268       (and data (gnus-data-number (car data))))))
2269
2270 (defun gnus-unread-mark-p (mark)
2271   "Say whether MARK is the unread mark."
2272   (= mark gnus-unread-mark))
2273
2274 (defun gnus-read-mark-p (mark)
2275   "Say whether MARK is one of the marks that mark as read.
2276 This is all marks except unread, ticked, dormant, and expirable."
2277   (not (or (= mark gnus-unread-mark)
2278            (= mark gnus-ticked-mark)
2279            (= mark gnus-dormant-mark)
2280            (= mark gnus-expirable-mark))))
2281
2282 (defmacro gnus-article-mark (number)
2283   "Return the MARK of article NUMBER.
2284 This macro should only be used when computing the mark the \"first\"
2285 time; i.e., when generating the summary lines.  After that,
2286 `gnus-summary-article-mark' should be used to examine the
2287 marks of articles."
2288   `(cond
2289     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2290     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2291     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2292     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2293     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2294     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2295     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2296     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2297            gnus-ancient-mark))))
2298
2299 ;; Saving hidden threads.
2300
2301 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2302 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2303
2304 (defmacro gnus-save-hidden-threads (&rest forms)
2305   "Save hidden threads, eval FORMS, and restore the hidden threads."
2306   (let ((config (make-symbol "config")))
2307     `(let ((,config (gnus-hidden-threads-configuration)))
2308        (unwind-protect
2309            (save-excursion
2310              ,@forms)
2311          (gnus-restore-hidden-threads-configuration ,config)))))
2312
2313 (defun gnus-data-compute-positions ()
2314   "Compute the positions of all articles."
2315   (setq gnus-newsgroup-data-reverse nil)
2316   (let ((data gnus-newsgroup-data))
2317     (save-excursion
2318       (gnus-save-hidden-threads
2319         (gnus-summary-show-all-threads)
2320         (goto-char (point-min))
2321         (while data
2322           (while (get-text-property (point) 'gnus-intangible)
2323             (forward-line 1))
2324           (gnus-data-set-pos (car data) (+ (point) 3))
2325           (setq data (cdr data))
2326           (forward-line 1))))))
2327
2328 (defun gnus-hidden-threads-configuration ()
2329   "Return the current hidden threads configuration."
2330   (save-excursion
2331     (let (config)
2332       (goto-char (point-min))
2333       (while (search-forward "\r" nil t)
2334         (push (1- (point)) config))
2335       config)))
2336
2337 (defun gnus-restore-hidden-threads-configuration (config)
2338   "Restore hidden threads configuration from CONFIG."
2339   (let (point buffer-read-only)
2340     (while (setq point (pop config))
2341       (when (and (< point (point-max))
2342                  (goto-char point)
2343                  (eq (char-after) ?\n))
2344         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2345
2346 ;; Various summary mode internalish functions.
2347
2348 (defun gnus-mouse-pick-article (e)
2349   (interactive "e")
2350   (mouse-set-point e)
2351   (gnus-summary-next-page nil t))
2352
2353 (defun gnus-summary-set-display-table ()
2354   ;; Change the display table.  Odd characters have a tendency to mess
2355   ;; up nicely formatted displays - we make all possible glyphs
2356   ;; display only a single character.
2357
2358   ;; We start from the standard display table, if any.
2359   (let ((table (or (copy-sequence standard-display-table)
2360                    (make-display-table)))
2361         (i 32))
2362     ;; Nix out all the control chars...
2363     (while (>= (setq i (1- i)) 0)
2364       (aset table i [??]))
2365     ;; ... but not newline and cr, of course.  (cr is necessary for the
2366     ;; selective display).
2367     (aset table ?\n nil)
2368     (aset table ?\r nil)
2369     ;; We keep TAB as well.
2370     (aset table ?\t nil)
2371     ;; We nix out any glyphs over 126 that are not set already.
2372     (let ((i 256))
2373       (while (>= (setq i (1- i)) 127)
2374         ;; Only modify if the entry is nil.
2375         (unless (aref table i)
2376           (aset table i [??]))))
2377     (setq buffer-display-table table)))
2378
2379 (defun gnus-summary-setup-buffer (group)
2380   "Initialize summary buffer."
2381   (let ((buffer (concat "*Summary " group "*")))
2382     (if (get-buffer buffer)
2383         (progn
2384           (set-buffer buffer)
2385           (setq gnus-summary-buffer (current-buffer))
2386           (not gnus-newsgroup-prepared))
2387       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2388       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2389       (gnus-summary-mode group)
2390       (when gnus-carpal
2391         (gnus-carpal-setup-buffer 'summary))
2392       (unless gnus-single-article-buffer
2393         (make-local-variable 'gnus-article-buffer)
2394         (make-local-variable 'gnus-article-current)
2395         (make-local-variable 'gnus-original-article-buffer))
2396       (setq gnus-newsgroup-name group)
2397       t)))
2398
2399 (defun gnus-set-global-variables ()
2400   ;; Set the global equivalents of the summary buffer-local variables
2401   ;; to the latest values they had.  These reflect the summary buffer
2402   ;; that was in action when the last article was fetched.
2403   (when (eq major-mode 'gnus-summary-mode)
2404     (setq gnus-summary-buffer (current-buffer))
2405     (let ((name gnus-newsgroup-name)
2406           (marked gnus-newsgroup-marked)
2407           (unread gnus-newsgroup-unreads)
2408           (headers gnus-current-headers)
2409           (data gnus-newsgroup-data)
2410           (summary gnus-summary-buffer)
2411           (article-buffer gnus-article-buffer)
2412           (original gnus-original-article-buffer)
2413           (gac gnus-article-current)
2414           (reffed gnus-reffed-article-number)
2415           (score-file gnus-current-score-file)
2416           (default-charset gnus-newsgroup-default-charset)
2417           (iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
2418       (save-excursion
2419         (set-buffer gnus-group-buffer)
2420         (setq gnus-newsgroup-name name
2421               gnus-newsgroup-marked marked
2422               gnus-newsgroup-unreads unread
2423               gnus-current-headers headers
2424               gnus-newsgroup-data data
2425               gnus-article-current gac
2426               gnus-summary-buffer summary
2427               gnus-article-buffer article-buffer
2428               gnus-original-article-buffer original
2429               gnus-reffed-article-number reffed
2430               gnus-current-score-file score-file
2431               gnus-newsgroup-default-charset default-charset
2432               gnus-newsgroup-iso-8859-1-forced iso-8859-1-forced)
2433         ;; The article buffer also has local variables.
2434         (when (gnus-buffer-live-p gnus-article-buffer)
2435           (set-buffer gnus-article-buffer)
2436           (setq gnus-summary-buffer summary))))))
2437
2438 (defun gnus-summary-article-unread-p (article)
2439   "Say whether ARTICLE is unread or not."
2440   (memq article gnus-newsgroup-unreads))
2441
2442 (defun gnus-summary-first-article-p (&optional article)
2443   "Return whether ARTICLE is the first article in the buffer."
2444   (if (not (setq article (or article (gnus-summary-article-number))))
2445       nil
2446     (eq article (caar gnus-newsgroup-data))))
2447
2448 (defun gnus-summary-last-article-p (&optional article)
2449   "Return whether ARTICLE is the last article in the buffer."
2450   (if (not (setq article (or article (gnus-summary-article-number))))
2451       t                                 ; All non-existent numbers are the last article.  :-)
2452     (not (cdr (gnus-data-find-list article)))))
2453
2454 (defun gnus-make-thread-indent-array ()
2455   (let ((n 200))
2456     (unless (and gnus-thread-indent-array
2457                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2458       (setq gnus-thread-indent-array (make-vector 201 "")
2459             gnus-thread-indent-array-level gnus-thread-indent-level)
2460       (while (>= n 0)
2461         (aset gnus-thread-indent-array n
2462               (make-string (* n gnus-thread-indent-level) ? ))
2463         (setq n (1- n))))))
2464
2465 (defun gnus-update-summary-mark-positions ()
2466   "Compute where the summary marks are to go."
2467   (save-excursion
2468     (when (gnus-buffer-exists-p gnus-summary-buffer)
2469       (set-buffer gnus-summary-buffer))
2470     (let ((gnus-replied-mark 129)
2471           (gnus-score-below-mark 130)
2472           (gnus-score-over-mark 130)
2473           (gnus-download-mark 131)
2474           (spec gnus-summary-line-format-spec)
2475           gnus-visual pos)
2476       (save-excursion
2477         (gnus-set-work-buffer)
2478         (let ((gnus-summary-line-format-spec spec)
2479               (gnus-newsgroup-downloadable '((0 . t))))
2480           (gnus-summary-insert-line
2481            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2482           (goto-char (point-min))
2483           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2484                                              (- (point) 2)))))
2485           (goto-char (point-min))
2486           (push (cons 'replied (and (search-forward "\201" nil t)
2487                                     (- (point) 2)))
2488                 pos)
2489           (goto-char (point-min))
2490           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2491                 pos)
2492           (goto-char (point-min))
2493           (push (cons 'download
2494                       (and (search-forward "\203" nil t) (- (point) 2)))
2495                 pos)))
2496       (setq gnus-summary-mark-positions pos))))
2497
2498 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2499   "Insert a dummy root in the summary buffer."
2500   (beginning-of-line)
2501   (gnus-add-text-properties
2502    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2503    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2504
2505 (defun gnus-summary-from-or-to-or-newsgroups (header)
2506   (let ((to (cdr (assq 'To (mail-header-extra header))))
2507         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2508         (rfc2047-default-charset gnus-newsgroup-default-charset)
2509         (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
2510     (cond
2511      ((and to
2512            gnus-ignored-from-addresses
2513            (string-match gnus-ignored-from-addresses
2514                          (mail-header-from header)))
2515       (concat "-> "
2516               (or (car (funcall gnus-extract-address-components
2517                                 (funcall
2518                                  gnus-decode-encoded-word-function to)))
2519                   (funcall gnus-decode-encoded-word-function to))))
2520      ((and newsgroups
2521            gnus-ignored-from-addresses
2522            (string-match gnus-ignored-from-addresses
2523                          (mail-header-from header)))
2524       (concat "=> " newsgroups))
2525      (t
2526       (or (car (funcall gnus-extract-address-components
2527                         (mail-header-from header)))
2528           (mail-header-from header))))))
2529
2530 (defun gnus-summary-insert-line (gnus-tmp-header
2531                                  gnus-tmp-level gnus-tmp-current
2532                                  gnus-tmp-unread gnus-tmp-replied
2533                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2534                                  &optional gnus-tmp-dummy gnus-tmp-score
2535                                  gnus-tmp-process)
2536   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2537          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2538          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2539          (gnus-tmp-score-char
2540           (if (or (null gnus-summary-default-score)
2541                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2542                       gnus-summary-zcore-fuzz))
2543               ? 
2544             (if (< gnus-tmp-score gnus-summary-default-score)
2545                 gnus-score-below-mark gnus-score-over-mark)))
2546          (gnus-tmp-replied
2547           (cond (gnus-tmp-process gnus-process-mark)
2548                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2549                  gnus-cached-mark)
2550                 (gnus-tmp-replied gnus-replied-mark)
2551                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2552                  gnus-saved-mark)
2553                 (t gnus-unread-mark)))
2554          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2555          (gnus-tmp-name
2556           (cond
2557            ((string-match "<[^>]+> *$" gnus-tmp-from)
2558             (let ((beg (match-beginning 0)))
2559               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2560                        (substring gnus-tmp-from (1+ (match-beginning 0))
2561                                   (1- (match-end 0))))
2562                   (substring gnus-tmp-from 0 beg))))
2563            ((string-match "(.+)" gnus-tmp-from)
2564             (substring gnus-tmp-from
2565                        (1+ (match-beginning 0)) (1- (match-end 0))))
2566            (t gnus-tmp-from)))
2567          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2568          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2569          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2570          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2571          (buffer-read-only nil))
2572     (when (string= gnus-tmp-name "")
2573       (setq gnus-tmp-name gnus-tmp-from))
2574     (unless (numberp gnus-tmp-lines)
2575       (setq gnus-tmp-lines 0))
2576     (gnus-put-text-property
2577      (point)
2578      (progn (eval gnus-summary-line-format-spec) (point))
2579      'gnus-number gnus-tmp-number)
2580     (when (gnus-visual-p 'summary-highlight 'highlight)
2581       (forward-line -1)
2582       (gnus-run-hooks 'gnus-summary-update-hook)
2583       (forward-line 1))))
2584
2585 (defun gnus-summary-update-line (&optional dont-update)
2586   ;; Update summary line after change.
2587   (when (and gnus-summary-default-score
2588              (not gnus-summary-inhibit-highlight))
2589     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2590            (article (gnus-summary-article-number))
2591            (score (gnus-summary-article-score article)))
2592       (unless dont-update
2593         (if (and gnus-summary-mark-below
2594                  (< (gnus-summary-article-score)
2595                     gnus-summary-mark-below))
2596             ;; This article has a low score, so we mark it as read.
2597             (when (memq article gnus-newsgroup-unreads)
2598               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2599           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2600             ;; This article was previously marked as read on account
2601             ;; of a low score, but now it has risen, so we mark it as
2602             ;; unread.
2603             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2604         (gnus-summary-update-mark
2605          (if (or (null gnus-summary-default-score)
2606                  (<= (abs (- score gnus-summary-default-score))
2607                      gnus-summary-zcore-fuzz))
2608              ? 
2609            (if (< score gnus-summary-default-score)
2610                gnus-score-below-mark gnus-score-over-mark))
2611          'score))
2612       ;; Do visual highlighting.
2613       (when (gnus-visual-p 'summary-highlight 'highlight)
2614         (gnus-run-hooks 'gnus-summary-update-hook)))))
2615
2616 (defvar gnus-tmp-new-adopts nil)
2617
2618 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2619   "Return the number of articles in THREAD.
2620 This may be 0 in some cases -- if none of the articles in
2621 the thread are to be displayed."
2622   (let* ((number
2623           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2624           (cond
2625            ((not (listp thread))
2626             1)
2627            ((and (consp thread) (cdr thread))
2628             (apply
2629              '+ 1 (mapcar
2630                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2631            ((null thread)
2632             1)
2633            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2634             1)
2635            (t 0))))
2636     (when (and level (zerop level) gnus-tmp-new-adopts)
2637       (incf number
2638             (apply '+ (mapcar
2639                        'gnus-summary-number-of-articles-in-thread
2640                        gnus-tmp-new-adopts))))
2641     (if char
2642         (if (> number 1) gnus-not-empty-thread-mark
2643           gnus-empty-thread-mark)
2644       number)))
2645
2646 (defun gnus-summary-set-local-parameters (group)
2647   "Go through the local params of GROUP and set all variable specs in that list."
2648   (let ((params (gnus-group-find-parameter group))
2649         elem)
2650     (while params
2651       (setq elem (car params)
2652             params (cdr params))
2653       (and (consp elem)                 ; Has to be a cons.
2654            (consp (cdr elem))           ; The cdr has to be a list.
2655            (symbolp (car elem))         ; Has to be a symbol in there.
2656            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2657            (ignore-errors               ; So we set it.
2658              (make-local-variable (car elem))
2659              (set (car elem) (eval (nth 1 elem))))))))
2660
2661 (defun gnus-summary-read-group (group &optional show-all no-article
2662                                       kill-buffer no-display backward
2663                                       select-articles)
2664   "Start reading news in newsgroup GROUP.
2665 If SHOW-ALL is non-nil, already read articles are also listed.
2666 If NO-ARTICLE is non-nil, no article is selected initially.
2667 If NO-DISPLAY, don't generate a summary buffer."
2668   (let (result)
2669     (while (and group
2670                 (null (setq result
2671                             (let ((gnus-auto-select-next nil))
2672                               (or (gnus-summary-read-group-1
2673                                    group show-all no-article
2674                                    kill-buffer no-display
2675                                    select-articles)
2676                                   (setq show-all nil
2677                                         select-articles nil)))))
2678                 (eq gnus-auto-select-next 'quietly))
2679       (set-buffer gnus-group-buffer)
2680       ;; The entry function called above goes to the next
2681       ;; group automatically, so we go two groups back
2682       ;; if we are searching for the previous group.
2683       (when backward
2684         (gnus-group-prev-unread-group 2))
2685       (if (not (equal group (gnus-group-group-name)))
2686           (setq group (gnus-group-group-name))
2687         (setq group nil)))
2688     result))
2689
2690 (defun gnus-summary-read-group-1 (group show-all no-article
2691                                         kill-buffer no-display
2692                                         &optional select-articles)
2693   ;; Killed foreign groups can't be entered.
2694   (when (and (not (gnus-group-native-p group))
2695              (not (gnus-gethash group gnus-newsrc-hashtb)))
2696     (error "Dead non-native groups can't be entered"))
2697   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2698   (let* ((new-group (gnus-summary-setup-buffer group))
2699          (quit-config (gnus-group-quit-config group))
2700          (did-select (and new-group (gnus-select-newsgroup
2701                                      group show-all select-articles))))
2702     (cond
2703      ;; This summary buffer exists already, so we just select it.
2704      ((not new-group)
2705       (gnus-set-global-variables)
2706       (when kill-buffer
2707         (gnus-kill-or-deaden-summary kill-buffer))
2708       (gnus-configure-windows 'summary 'force)
2709       (gnus-set-mode-line 'summary)
2710       (gnus-summary-position-point)
2711       (message "")
2712       t)
2713      ;; We couldn't select this group.
2714      ((null did-select)
2715       (when (and (eq major-mode 'gnus-summary-mode)
2716                  (not (equal (current-buffer) kill-buffer)))
2717         (kill-buffer (current-buffer))
2718         (if (not quit-config)
2719             (progn
2720               ;; Update the info -- marks might need to be removed,
2721               ;; for instance.
2722               (gnus-summary-update-info)
2723               (set-buffer gnus-group-buffer)
2724               (gnus-group-jump-to-group group)
2725               (gnus-group-next-unread-group 1))
2726           (gnus-handle-ephemeral-exit quit-config)))
2727       (gnus-message 3 "Can't select group")
2728       nil)
2729      ;; The user did a `C-g' while prompting for number of articles,
2730      ;; so we exit this group.
2731      ((eq did-select 'quit)
2732       (and (eq major-mode 'gnus-summary-mode)
2733            (not (equal (current-buffer) kill-buffer))
2734            (kill-buffer (current-buffer)))
2735       (when kill-buffer
2736         (gnus-kill-or-deaden-summary kill-buffer))
2737       (if (not quit-config)
2738           (progn
2739             (set-buffer gnus-group-buffer)
2740             (gnus-group-jump-to-group group)
2741             (gnus-group-next-unread-group 1)
2742             (gnus-configure-windows 'group 'force))
2743         (gnus-handle-ephemeral-exit quit-config))
2744       ;; Finally signal the quit.
2745       (signal 'quit nil))
2746      ;; The group was successfully selected.
2747      (t
2748       (gnus-set-global-variables)
2749       ;; Save the active value in effect when the group was entered.
2750       (setq gnus-newsgroup-active
2751             (gnus-copy-sequence
2752              (gnus-active gnus-newsgroup-name)))
2753       ;; You can change the summary buffer in some way with this hook.
2754       (gnus-run-hooks 'gnus-select-group-hook)
2755       ;; Set any local variables in the group parameters.
2756       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2757       (gnus-update-format-specifications
2758        nil 'summary 'summary-mode 'summary-dummy)
2759       ;; Do score processing.
2760       (when gnus-use-scoring
2761         (gnus-possibly-score-headers))
2762       ;; Check whether to fill in the gaps in the threads.
2763       (when gnus-build-sparse-threads
2764         (gnus-build-sparse-threads))
2765       ;; Find the initial limit.
2766       (if gnus-show-threads
2767           (if show-all
2768               (let ((gnus-newsgroup-dormant nil))
2769                 (gnus-summary-initial-limit show-all))
2770             (gnus-summary-initial-limit show-all))
2771         (setq gnus-newsgroup-limit
2772               (mapcar
2773                (lambda (header) (mail-header-number header))
2774                gnus-newsgroup-headers)))
2775       ;; Generate the summary buffer.
2776       (unless no-display
2777         (gnus-summary-prepare))
2778       (when gnus-use-trees
2779         (gnus-tree-open group)
2780         (setq gnus-summary-highlight-line-function
2781               'gnus-tree-highlight-article))
2782       ;; If the summary buffer is empty, but there are some low-scored
2783       ;; articles or some excluded dormants, we include these in the
2784       ;; buffer.
2785       (when (and (zerop (buffer-size))
2786                  (not no-display))
2787         (cond (gnus-newsgroup-dormant
2788                (gnus-summary-limit-include-dormant))
2789               ((and gnus-newsgroup-scored show-all)
2790                (gnus-summary-limit-include-expunged t))))
2791       ;; Function `gnus-apply-kill-file' must be called in this hook.
2792       (gnus-run-hooks 'gnus-apply-kill-hook)
2793       (if (and (zerop (buffer-size))
2794                (not no-display))
2795           (progn
2796             ;; This newsgroup is empty.
2797             (gnus-summary-catchup-and-exit nil t)
2798             (gnus-message 6 "No unread news")
2799             (when kill-buffer
2800               (gnus-kill-or-deaden-summary kill-buffer))
2801             ;; Return nil from this function.
2802             nil)
2803         ;; Hide conversation thread subtrees.  We cannot do this in
2804         ;; gnus-summary-prepare-hook since kill processing may not
2805         ;; work with hidden articles.
2806         (and gnus-show-threads
2807              gnus-thread-hide-subtree
2808              (gnus-summary-hide-all-threads))
2809         (when kill-buffer
2810           (gnus-kill-or-deaden-summary kill-buffer))
2811         ;; Show first unread article if requested.
2812         (if (and (not no-article)
2813                  (not no-display)
2814                  gnus-newsgroup-unreads
2815                  gnus-auto-select-first)
2816             (progn
2817               (gnus-configure-windows 'summary)
2818               (cond
2819                ((eq gnus-auto-select-first 'best)
2820                 (gnus-summary-best-unread-article))
2821                ((eq gnus-auto-select-first t)
2822                 (gnus-summary-first-unread-article))
2823                ((gnus-functionp gnus-auto-select-first)
2824                 (funcall gnus-auto-select-first))))
2825           ;; Don't select any articles, just move point to the first
2826           ;; article in the group.
2827           (goto-char (point-min))
2828           (gnus-summary-position-point)
2829           (gnus-configure-windows 'summary 'force)
2830           (gnus-set-mode-line 'summary))
2831         (when (get-buffer-window gnus-group-buffer t)
2832           ;; Gotta use windows, because recenter does weird stuff if
2833           ;; the current buffer ain't the displayed window.
2834           (let ((owin (selected-window)))
2835             (select-window (get-buffer-window gnus-group-buffer t))
2836             (when (gnus-group-goto-group group)
2837               (recenter))
2838             (select-window owin)))
2839         ;; Mark this buffer as "prepared".
2840         (setq gnus-newsgroup-prepared t)
2841         (gnus-run-hooks 'gnus-summary-prepared-hook)
2842         t)))))
2843
2844 (defun gnus-summary-prepare ()
2845   "Generate the summary buffer."
2846   (interactive)
2847   (let ((buffer-read-only nil))
2848     (erase-buffer)
2849     (setq gnus-newsgroup-data nil
2850           gnus-newsgroup-data-reverse nil)
2851     (gnus-run-hooks 'gnus-summary-generate-hook)
2852     ;; Generate the buffer, either with threads or without.
2853     (when gnus-newsgroup-headers
2854       (gnus-summary-prepare-threads
2855        (if gnus-show-threads
2856            (gnus-sort-gathered-threads
2857             (funcall gnus-summary-thread-gathering-function
2858                      (gnus-sort-threads
2859                       (gnus-cut-threads (gnus-make-threads)))))
2860          ;; Unthreaded display.
2861          (gnus-sort-articles gnus-newsgroup-headers))))
2862     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2863     ;; Call hooks for modifying summary buffer.
2864     (goto-char (point-min))
2865     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2866
2867 (defsubst gnus-general-simplify-subject (subject)
2868   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2869   (setq subject
2870         (cond
2871          ;; Truncate the subject.
2872          (gnus-simplify-subject-functions
2873           (gnus-map-function gnus-simplify-subject-functions subject))
2874          ((numberp gnus-summary-gather-subject-limit)
2875           (setq subject (gnus-simplify-subject-re subject))
2876           (if (> (length subject) gnus-summary-gather-subject-limit)
2877               (substring subject 0 gnus-summary-gather-subject-limit)
2878             subject))
2879          ;; Fuzzily simplify it.
2880          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2881           (gnus-simplify-subject-fuzzy subject))
2882          ;; Just remove the leading "Re:".
2883          (t
2884           (gnus-simplify-subject-re subject))))
2885
2886   (if (and gnus-summary-gather-exclude-subject
2887            (string-match gnus-summary-gather-exclude-subject subject))
2888       nil                               ; This article shouldn't be gathered
2889     subject))
2890
2891 (defun gnus-summary-simplify-subject-query ()
2892   "Query where the respool algorithm would put this article."
2893   (interactive)
2894   (gnus-summary-select-article)
2895   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2896
2897 (defun gnus-gather-threads-by-subject (threads)
2898   "Gather threads by looking at Subject headers."
2899   (if (not gnus-summary-make-false-root)
2900       threads
2901     (let ((hashtb (gnus-make-hashtable 1024))
2902           (prev threads)
2903           (result threads)
2904           subject hthread whole-subject)
2905       (while threads
2906         (setq subject (gnus-general-simplify-subject
2907                        (setq whole-subject (mail-header-subject
2908                                             (caar threads)))))
2909         (when subject
2910           (if (setq hthread (gnus-gethash subject hashtb))
2911               (progn
2912                 ;; We enter a dummy root into the thread, if we
2913                 ;; haven't done that already.
2914                 (unless (stringp (caar hthread))
2915                   (setcar hthread (list whole-subject (car hthread))))
2916                 ;; We add this new gathered thread to this gathered
2917                 ;; thread.
2918                 (setcdr (car hthread)
2919                         (nconc (cdar hthread) (list (car threads))))
2920                 ;; Remove it from the list of threads.
2921                 (setcdr prev (cdr threads))
2922                 (setq threads prev))
2923             ;; Enter this thread into the hash table.
2924             (gnus-sethash subject threads hashtb)))
2925         (setq prev threads)
2926         (setq threads (cdr threads)))
2927       result)))
2928
2929 (defun gnus-gather-threads-by-references (threads)
2930   "Gather threads by looking at References headers."
2931   (let ((idhashtb (gnus-make-hashtable 1024))
2932         (thhashtb (gnus-make-hashtable 1024))
2933         (prev threads)
2934         (result threads)
2935         ids references id gthread gid entered ref)
2936     (while threads
2937       (when (setq references (mail-header-references (caar threads)))
2938         (setq id (mail-header-id (caar threads))
2939               ids (gnus-split-references references)
2940               entered nil)
2941         (while (setq ref (pop ids))
2942           (setq ids (delete ref ids))
2943           (if (not (setq gid (gnus-gethash ref idhashtb)))
2944               (progn
2945                 (gnus-sethash ref id idhashtb)
2946                 (gnus-sethash id threads thhashtb))
2947             (setq gthread (gnus-gethash gid thhashtb))
2948             (unless entered
2949               ;; We enter a dummy root into the thread, if we
2950               ;; haven't done that already.
2951               (unless (stringp (caar gthread))
2952                 (setcar gthread (list (mail-header-subject (caar gthread))
2953                                       (car gthread))))
2954               ;; We add this new gathered thread to this gathered
2955               ;; thread.
2956               (setcdr (car gthread)
2957                       (nconc (cdar gthread) (list (car threads)))))
2958             ;; Add it into the thread hash table.
2959             (gnus-sethash id gthread thhashtb)
2960             (setq entered t)
2961             ;; Remove it from the list of threads.
2962             (setcdr prev (cdr threads))
2963             (setq threads prev))))
2964       (setq prev threads)
2965       (setq threads (cdr threads)))
2966     result))
2967
2968 (defun gnus-sort-gathered-threads (threads)
2969   "Sort subtreads inside each gathered thread by article number."
2970   (let ((result threads))
2971     (while threads
2972       (when (stringp (caar threads))
2973         (setcdr (car threads)
2974                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2975       (setq threads (cdr threads)))
2976     result))
2977
2978 (defun gnus-thread-loop-p (root thread)
2979   "Say whether ROOT is in THREAD."
2980   (let ((stack (list thread))
2981         (infloop 0)
2982         th)
2983     (while (setq thread (pop stack))
2984       (setq th (cdr thread))
2985       (while (and th
2986                   (not (eq (caar th) root)))
2987         (pop th))
2988       (if th
2989           ;; We have found a loop.
2990           (let (ref-dep)
2991             (setcdr thread (delq (car th) (cdr thread)))
2992             (if (boundp (setq ref-dep (intern "none"
2993                                               gnus-newsgroup-dependencies)))
2994                 (setcdr (symbol-value ref-dep)
2995                         (nconc (cdr (symbol-value ref-dep))
2996                                (list (car th))))
2997               (set ref-dep (list nil (car th))))
2998             (setq infloop 1
2999                   stack nil))
3000         ;; Push all the subthreads onto the stack.
3001         (push (cdr thread) stack)))
3002     infloop))
3003
3004 (defun gnus-make-threads ()
3005   "Go through the dependency hashtb and find the roots.  Return all threads."
3006   (let (threads)
3007     (while (catch 'infloop
3008              (mapatoms
3009               (lambda (refs)
3010                 ;; Deal with self-referencing References loops.
3011                 (when (and (car (symbol-value refs))
3012                            (not (zerop
3013                                  (apply
3014                                   '+
3015                                   (mapcar
3016                                    (lambda (thread)
3017                                      (gnus-thread-loop-p
3018                                       (car (symbol-value refs)) thread))
3019                                    (cdr (symbol-value refs)))))))
3020                   (setq threads nil)
3021                   (throw 'infloop t))
3022                 (unless (car (symbol-value refs))
3023                   ;; These threads do not refer back to any other articles,
3024                   ;; so they're roots.
3025                   (setq threads (append (cdr (symbol-value refs)) threads))))
3026               gnus-newsgroup-dependencies)))
3027     threads))
3028
3029 ;; Build the thread tree.
3030 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3031   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3032
3033 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3034 if it was already present.
3035
3036 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3037 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3038 Message-IDs will be renamed be renamed to a unique Message-ID before
3039 being entered.
3040
3041 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3042   (let* ((id (mail-header-id header))
3043          (id-dep (and id (intern id dependencies)))
3044          ref ref-dep ref-header)
3045     ;; Enter this `header' in the `dependencies' table.
3046     (cond
3047      ((not id-dep)
3048       (setq header nil))
3049      ;; The first two cases do the normal part: enter a new `header'
3050      ;; in the `dependencies' table.
3051      ((not (boundp id-dep))
3052       (set id-dep (list header)))
3053      ((null (car (symbol-value id-dep)))
3054       (setcar (symbol-value id-dep) header))
3055
3056      ;; From here the `header' was already present in the
3057      ;; `dependencies' table.
3058      (force-new
3059       ;; Overrides an existing entry;
3060       ;; just set the header part of the entry.
3061       (setcar (symbol-value id-dep) header))
3062
3063      ;; Renames the existing `header' to a unique Message-ID.
3064      ((not gnus-summary-ignore-duplicates)
3065       ;; An article with this Message-ID has already been seen.
3066       ;; We rename the Message-ID.
3067       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3068            (list header))
3069       (mail-header-set-id header id))
3070
3071      ;; The last case ignores an existing entry, except it adds any
3072      ;; additional Xrefs (in case the two articles came from different
3073      ;; servers.
3074      ;; Also sets `header' to `nil' meaning that the `dependencies'
3075      ;; table was *not* modified.
3076      (t
3077       (mail-header-set-xref
3078        (car (symbol-value id-dep))
3079        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3080                    "")
3081                (or (mail-header-xref header) "")))
3082       (setq header nil)))
3083
3084     (when header
3085       ;; First check if that we are not creating a References loop.
3086       (setq ref (gnus-parent-id (mail-header-references header)))
3087       (while (and ref
3088                   (setq ref-dep (intern-soft ref dependencies))
3089                   (boundp ref-dep)
3090                   (setq ref-header (car (symbol-value ref-dep))))
3091         (if (string= id ref)
3092             ;; Yuk!  This is a reference loop.  Make the article be a
3093             ;; root article.
3094             (progn
3095               (mail-header-set-references (car (symbol-value id-dep)) "none")
3096               (setq ref nil))
3097           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3098       (setq ref (gnus-parent-id (mail-header-references header)))
3099       (setq ref-dep (intern (or ref "none") dependencies))
3100       (if (boundp ref-dep)
3101           (setcdr (symbol-value ref-dep)
3102                   (nconc (cdr (symbol-value ref-dep))
3103                          (list (symbol-value id-dep))))
3104         (set ref-dep (list nil (symbol-value id-dep)))))
3105     header))
3106
3107 (defun gnus-build-sparse-threads ()
3108   (let ((headers gnus-newsgroup-headers)
3109         (gnus-summary-ignore-duplicates t)
3110         header references generation relations
3111         subject child end new-child date)
3112     ;; First we create an alist of generations/relations, where
3113     ;; generations is how much we trust the relation, and the relation
3114     ;; is parent/child.
3115     (gnus-message 7 "Making sparse threads...")
3116     (save-excursion
3117       (nnheader-set-temp-buffer " *gnus sparse threads*")
3118       (while (setq header (pop headers))
3119         (when (and (setq references (mail-header-references header))
3120                    (not (string= references "")))
3121           (insert references)
3122           (setq child (mail-header-id header)
3123                 subject (mail-header-subject header)
3124                 date (mail-header-date header)
3125                 generation 0)
3126           (while (search-backward ">" nil t)
3127             (setq end (1+ (point)))
3128             (when (search-backward "<" nil t)
3129               (setq new-child (buffer-substring (point) end))
3130               (push (list (incf generation)
3131                           child (setq child new-child)
3132                           subject date)
3133                     relations)))
3134           (when child
3135             (push (list (1+ generation) child nil subject) relations))
3136           (erase-buffer)))
3137       (kill-buffer (current-buffer)))
3138     ;; Sort over trustworthiness.
3139     (mapcar
3140      (lambda (relation)
3141        (when (gnus-dependencies-add-header
3142               (make-full-mail-header
3143                gnus-reffed-article-number
3144                (nth 3 relation) "" (or (nth 4 relation) "")
3145                (nth 1 relation)
3146                (or (nth 2 relation) "") 0 0 "")
3147               gnus-newsgroup-dependencies nil)
3148          (push gnus-reffed-article-number gnus-newsgroup-limit)
3149          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3150          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3151                gnus-newsgroup-reads)
3152          (decf gnus-reffed-article-number)))
3153      (sort relations 'car-less-than-car))
3154     (gnus-message 7 "Making sparse threads...done")))
3155
3156 (defun gnus-build-old-threads ()
3157   ;; Look at all the articles that refer back to old articles, and
3158   ;; fetch the headers for the articles that aren't there.  This will
3159   ;; build complete threads - if the roots haven't been expired by the
3160   ;; server, that is.
3161   (let (id heads)
3162     (mapatoms
3163      (lambda (refs)
3164        (when (not (car (symbol-value refs)))
3165          (setq heads (cdr (symbol-value refs)))
3166          (while heads
3167            (if (memq (mail-header-number (caar heads))
3168                      gnus-newsgroup-dormant)
3169                (setq heads (cdr heads))
3170              (setq id (symbol-name refs))
3171              (while (and (setq id (gnus-build-get-header id))
3172                          (not (car (gnus-id-to-thread id)))))
3173              (setq heads nil)))))
3174      gnus-newsgroup-dependencies)))
3175
3176 (defmacro gnus-nov-read-integer ()
3177   '(prog1
3178        (if (eq (char-after) ?\t)
3179            0
3180          (let ((num (ignore-errors (read buffer))))
3181            (if (numberp num) num 0)))
3182      (unless (eobp)
3183        (search-forward "\t" eol 'move))))
3184
3185 (defmacro gnus-nov-skip-field ()
3186   '(search-forward "\t" eol 'move))
3187
3188 (defmacro gnus-nov-field ()
3189   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
3190
3191 (defmacro gnus-nov-parse-extra ()
3192   '(let (out string)
3193      (while (not (memq (char-after) '(?\n nil)))
3194        (setq string (gnus-nov-field))
3195        (when (string-match "^\\([^ :]+\\): " string)
3196          (push (cons (intern (match-string 1 string))
3197                      (substring string (match-end 0)))
3198                out)))
3199      out))
3200
3201 ;; This function has to be called with point after the article number
3202 ;; on the beginning of the line.
3203 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3204   (let ((eol (gnus-point-at-eol))
3205         (buffer (current-buffer))
3206         header)
3207
3208     ;; overview: [num subject from date id refs chars lines misc]
3209     (unwind-protect
3210         (progn
3211           (narrow-to-region (point) eol)
3212           (unless (eobp)
3213             (forward-char))
3214
3215           (setq header
3216                 (make-full-mail-header
3217                  number                 ; number
3218                  (funcall gnus-decode-encoded-word-function
3219                           (gnus-nov-field)) ; subject
3220                  (funcall gnus-decode-encoded-word-function
3221                           (gnus-nov-field)) ; from
3222                  (gnus-nov-field)       ; date
3223                  (or (gnus-nov-field)
3224                      (nnheader-generate-fake-message-id)) ; id
3225                  (gnus-nov-field)       ; refs
3226                  (gnus-nov-read-integer) ; chars
3227                  (gnus-nov-read-integer) ; lines
3228                  (unless (eq (char-after) ?\n)
3229                    (gnus-nov-field))    ; misc
3230                  (gnus-nov-parse-extra)))) ; extra
3231
3232       (widen))
3233
3234     (when gnus-alter-header-function
3235       (funcall gnus-alter-header-function header))
3236     (gnus-dependencies-add-header header dependencies force-new)))
3237
3238 (defun gnus-build-get-header (id)
3239   ;; Look through the buffer of NOV lines and find the header to
3240   ;; ID.  Enter this line into the dependencies hash table, and return
3241   ;; the id of the parent article (if any).
3242   (let ((deps gnus-newsgroup-dependencies)
3243         found header)
3244     (prog1
3245         (save-excursion
3246           (set-buffer nntp-server-buffer)
3247           (let ((case-fold-search nil))
3248             (goto-char (point-min))
3249             (while (and (not found)
3250                         (search-forward id nil t))
3251               (beginning-of-line)
3252               (setq found (looking-at
3253                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3254                                    (regexp-quote id))))
3255               (or found (beginning-of-line 2)))
3256             (when found
3257               (beginning-of-line)
3258               (and
3259                (setq header (gnus-nov-parse-line
3260                              (read (current-buffer)) deps))
3261                (gnus-parent-id (mail-header-references header))))))
3262       (when header
3263         (let ((number (mail-header-number header)))
3264           (push number gnus-newsgroup-limit)
3265           (push header gnus-newsgroup-headers)
3266           (if (memq number gnus-newsgroup-unselected)
3267               (progn
3268                 (push number gnus-newsgroup-unreads)
3269                 (setq gnus-newsgroup-unselected
3270                       (delq number gnus-newsgroup-unselected)))
3271             (push number gnus-newsgroup-ancient)))))))
3272
3273 (defun gnus-build-all-threads ()
3274   "Read all the headers."
3275   (let ((gnus-summary-ignore-duplicates t)
3276         (dependencies gnus-newsgroup-dependencies)
3277         header article)
3278     (save-excursion
3279       (set-buffer nntp-server-buffer)
3280       (let ((case-fold-search nil))
3281         (goto-char (point-min))
3282         (while (not (eobp))
3283           (ignore-errors
3284             (setq article (read (current-buffer))
3285                   header (gnus-nov-parse-line
3286                           article dependencies)))
3287           (when header
3288             (save-excursion
3289               (set-buffer gnus-summary-buffer)
3290               (push header gnus-newsgroup-headers)
3291               (if (memq (setq article (mail-header-number header))
3292                         gnus-newsgroup-unselected)
3293                   (progn
3294                     (push article gnus-newsgroup-unreads)
3295                     (setq gnus-newsgroup-unselected
3296                           (delq article gnus-newsgroup-unselected)))
3297                 (push article gnus-newsgroup-ancient)))
3298             (forward-line 1)))))))
3299
3300 (defun gnus-summary-update-article-line (article header)
3301   "Update the line for ARTICLE using HEADERS."
3302   (let* ((id (mail-header-id header))
3303          (thread (gnus-id-to-thread id)))
3304     (unless thread
3305       (error "Article in no thread"))
3306     ;; Update the thread.
3307     (setcar thread header)
3308     (gnus-summary-goto-subject article)
3309     (let* ((datal (gnus-data-find-list article))
3310            (data (car datal))
3311            (length (when (cdr datal)
3312                      (- (gnus-data-pos data)
3313                         (gnus-data-pos (cadr datal)))))
3314            (buffer-read-only nil)
3315            (level (gnus-summary-thread-level)))
3316       (gnus-delete-line)
3317       (gnus-summary-insert-line
3318        header level nil (gnus-article-mark article)
3319        (memq article gnus-newsgroup-replied)
3320        (memq article gnus-newsgroup-expirable)
3321        ;; Only insert the Subject string when it's different
3322        ;; from the previous Subject string.
3323        (if (gnus-subject-equal
3324             (condition-case ()
3325                 (mail-header-subject
3326                  (gnus-data-header
3327                   (cadr
3328                    (gnus-data-find-list
3329                     article
3330                     (gnus-data-list t)))))
3331               ;; Error on the side of excessive subjects.
3332               (error ""))
3333             (mail-header-subject header))
3334            ""
3335          (mail-header-subject header))
3336        nil (cdr (assq article gnus-newsgroup-scored))
3337        (memq article gnus-newsgroup-processable))
3338       (when length
3339         (gnus-data-update-list
3340          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3341
3342 (defun gnus-summary-update-article (article &optional iheader)
3343   "Update ARTICLE in the summary buffer."
3344   (set-buffer gnus-summary-buffer)
3345   (let* ((header (gnus-summary-article-header article))
3346          (id (mail-header-id header))
3347          (data (gnus-data-find article))
3348          (thread (gnus-id-to-thread id))
3349          (references (mail-header-references header))
3350          (parent
3351           (gnus-id-to-thread
3352            (or (gnus-parent-id
3353                 (when (and references
3354                            (not (equal "" references)))
3355                   references))
3356                "none")))
3357          (buffer-read-only nil)
3358          (old (car thread)))
3359     (when thread
3360       (unless iheader
3361         (setcar thread nil)
3362         (when parent
3363           (delq thread parent)))
3364       (if (gnus-summary-insert-subject id header)
3365           ;; Set the (possibly) new article number in the data structure.
3366           (gnus-data-set-number data (gnus-id-to-article id))
3367         (setcar thread old)
3368         nil))))
3369
3370 (defun gnus-rebuild-thread (id &optional line)
3371   "Rebuild the thread containing ID.
3372 If LINE, insert the rebuilt thread starting on line LINE."
3373   (let ((buffer-read-only nil)
3374         old-pos current thread data)
3375     (if (not gnus-show-threads)
3376         (setq thread (list (car (gnus-id-to-thread id))))
3377       ;; Get the thread this article is part of.
3378       (setq thread (gnus-remove-thread id)))
3379     (setq old-pos (gnus-point-at-bol))
3380     (setq current (save-excursion
3381                     (and (zerop (forward-line -1))
3382                          (gnus-summary-article-number))))
3383     ;; If this is a gathered thread, we have to go some re-gathering.
3384     (when (stringp (car thread))
3385       (let ((subject (car thread))
3386             roots thr)
3387         (setq thread (cdr thread))
3388         (while thread
3389           (unless (memq (setq thr (gnus-id-to-thread
3390                                    (gnus-root-id
3391                                     (mail-header-id (caar thread)))))
3392                         roots)
3393             (push thr roots))
3394           (setq thread (cdr thread)))
3395         ;; We now have all (unique) roots.
3396         (if (= (length roots) 1)
3397             ;; All the loose roots are now one solid root.
3398             (setq thread (car roots))
3399           (setq thread (cons subject (gnus-sort-threads roots))))))
3400     (let (threads)
3401       ;; We then insert this thread into the summary buffer.
3402       (when line
3403         (goto-char (point-min))
3404         (forward-line (1- line)))
3405       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3406         (if gnus-show-threads
3407             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3408           (gnus-summary-prepare-unthreaded thread))
3409         (setq data (nreverse gnus-newsgroup-data))
3410         (setq threads gnus-newsgroup-threads))
3411       ;; We splice the new data into the data structure.
3412       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3413       ;;!!! then we want to insert at the beginning of the buffer.
3414       ;;!!! That happens to be true with Gnus now, but that may
3415       ;;!!! change in the future.  Perhaps.
3416       (gnus-data-enter-list
3417        (if line nil current) data (- (point) old-pos))
3418       (setq gnus-newsgroup-threads
3419             (nconc threads gnus-newsgroup-threads))
3420       (gnus-data-compute-positions))))
3421
3422 (defun gnus-number-to-header (number)
3423   "Return the header for article NUMBER."
3424   (let ((headers gnus-newsgroup-headers))
3425     (while (and headers
3426                 (not (= number (mail-header-number (car headers)))))
3427       (pop headers))
3428     (when headers
3429       (car headers))))
3430
3431 (defun gnus-parent-headers (in-headers &optional generation)
3432   "Return the headers of the GENERATIONeth parent of HEADERS."
3433   (unless generation
3434     (setq generation 1))
3435   (let ((parent t)
3436         (headers in-headers)
3437         references)
3438     (while (and parent
3439                 (not (zerop generation))
3440                 (setq references (mail-header-references headers)))
3441       (setq headers (if (and references
3442                              (setq parent (gnus-parent-id references)))
3443                         (car (gnus-id-to-thread parent))
3444                       nil))
3445       (decf generation))
3446     (and (not (eq headers in-headers))
3447          headers)))
3448
3449 (defun gnus-id-to-thread (id)
3450   "Return the (sub-)thread where ID appears."
3451   (gnus-gethash id gnus-newsgroup-dependencies))
3452
3453 (defun gnus-id-to-article (id)
3454   "Return the article number of ID."
3455   (let ((thread (gnus-id-to-thread id)))
3456     (when (and thread
3457                (car thread))
3458       (mail-header-number (car thread)))))
3459
3460 (defun gnus-id-to-header (id)
3461   "Return the article headers of ID."
3462   (car (gnus-id-to-thread id)))
3463
3464 (defun gnus-article-displayed-root-p (article)
3465   "Say whether ARTICLE is a root(ish) article."
3466   (let ((level (gnus-summary-thread-level article))
3467         (refs (mail-header-references  (gnus-summary-article-header article)))
3468         particle)
3469     (cond
3470      ((null level) nil)
3471      ((zerop level) t)
3472      ((null refs) t)
3473      ((null (gnus-parent-id refs)) t)
3474      ((and (= 1 level)
3475            (null (setq particle (gnus-id-to-article
3476                                  (gnus-parent-id refs))))
3477            (null (gnus-summary-thread-level particle)))))))
3478
3479 (defun gnus-root-id (id)
3480   "Return the id of the root of the thread where ID appears."
3481   (let (last-id prev)
3482     (while (and id (setq prev (car (gnus-id-to-thread id))))
3483       (setq last-id id
3484             id (gnus-parent-id (mail-header-references prev))))
3485     last-id))
3486
3487 (defun gnus-articles-in-thread (thread)
3488   "Return the list of articles in THREAD."
3489   (cons (mail-header-number (car thread))
3490         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3491
3492 (defun gnus-remove-thread (id &optional dont-remove)
3493   "Remove the thread that has ID in it."
3494   (let (headers thread last-id)
3495     ;; First go up in this thread until we find the root.
3496     (setq last-id (gnus-root-id id)
3497           headers (message-flatten-list (gnus-id-to-thread last-id)))
3498     ;; We have now found the real root of this thread.  It might have
3499     ;; been gathered into some loose thread, so we have to search
3500     ;; through the threads to find the thread we wanted.
3501     (let ((threads gnus-newsgroup-threads)
3502           sub)
3503       (while threads
3504         (setq sub (car threads))
3505         (if (stringp (car sub))
3506             ;; This is a gathered thread, so we look at the roots
3507             ;; below it to find whether this article is in this
3508             ;; gathered root.
3509             (progn
3510               (setq sub (cdr sub))
3511               (while sub
3512                 (when (member (caar sub) headers)
3513                   (setq thread (car threads)
3514                         threads nil
3515                         sub nil))
3516                 (setq sub (cdr sub))))
3517           ;; It's an ordinary thread, so we check it.
3518           (when (eq (car sub) (car headers))
3519             (setq thread sub
3520                   threads nil)))
3521         (setq threads (cdr threads)))
3522       ;; If this article is in no thread, then it's a root.
3523       (if thread
3524           (unless dont-remove
3525             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3526         (setq thread (gnus-id-to-thread last-id)))
3527       (when thread
3528         (prog1
3529             thread                      ; We return this thread.
3530           (unless dont-remove
3531             (if (stringp (car thread))
3532                 (progn
3533                   ;; If we use dummy roots, then we have to remove the
3534                   ;; dummy root as well.
3535                   (when (eq gnus-summary-make-false-root 'dummy)
3536                     ;; We go to the dummy root by going to
3537                     ;; the first sub-"thread", and then one line up.
3538                     (gnus-summary-goto-article
3539                      (mail-header-number (caadr thread)))
3540                     (forward-line -1)
3541                     (gnus-delete-line)
3542                     (gnus-data-compute-positions))
3543                   (setq thread (cdr thread))
3544                   (while thread
3545                     (gnus-remove-thread-1 (car thread))
3546                     (setq thread (cdr thread))))
3547               (gnus-summary-show-all-threads)
3548               (gnus-remove-thread-1 thread))))))))
3549
3550 (defun gnus-remove-thread-1 (thread)
3551   "Remove the thread THREAD recursively."
3552   (let ((number (mail-header-number (pop thread)))
3553         d)
3554     (setq thread (reverse thread))
3555     (while thread
3556       (gnus-remove-thread-1 (pop thread)))
3557     (when (setq d (gnus-data-find number))
3558       (goto-char (gnus-data-pos d))
3559       (gnus-data-remove
3560        number
3561        (- (gnus-point-at-bol)
3562           (prog1
3563               (1+ (gnus-point-at-eol))
3564             (gnus-delete-line)))))))
3565
3566 (defun gnus-sort-threads (threads)
3567   "Sort THREADS."
3568   (if (not gnus-thread-sort-functions)
3569       threads
3570     (gnus-message 8 "Sorting threads...")
3571     (prog1
3572         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3573       (gnus-message 8 "Sorting threads...done"))))
3574
3575 (defun gnus-sort-articles (articles)
3576   "Sort ARTICLES."
3577   (when gnus-article-sort-functions
3578     (gnus-message 7 "Sorting articles...")
3579     (prog1
3580         (setq gnus-newsgroup-headers
3581               (sort articles (gnus-make-sort-function
3582                               gnus-article-sort-functions)))
3583       (gnus-message 7 "Sorting articles...done"))))
3584
3585 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3586 (defmacro gnus-thread-header (thread)
3587   ;; Return header of first article in THREAD.
3588   ;; Note that THREAD must never, ever be anything else than a variable -
3589   ;; using some other form will lead to serious barfage.
3590   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3591   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3592   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3593         (vector thread) 2))
3594
3595 (defsubst gnus-article-sort-by-number (h1 h2)
3596   "Sort articles by article number."
3597   (< (mail-header-number h1)
3598      (mail-header-number h2)))
3599
3600 (defun gnus-thread-sort-by-number (h1 h2)
3601   "Sort threads by root article number."
3602   (gnus-article-sort-by-number
3603    (gnus-thread-header h1) (gnus-thread-header h2)))
3604
3605 (defsubst gnus-article-sort-by-lines (h1 h2)
3606   "Sort articles by article Lines header."
3607   (< (mail-header-lines h1)
3608      (mail-header-lines h2)))
3609
3610 (defun gnus-thread-sort-by-lines (h1 h2)
3611   "Sort threads by root article Lines header."
3612   (gnus-article-sort-by-lines
3613    (gnus-thread-header h1) (gnus-thread-header h2)))
3614
3615 (defsubst gnus-article-sort-by-author (h1 h2)
3616   "Sort articles by root author."
3617   (string-lessp
3618    (let ((extract (funcall
3619                    gnus-extract-address-components
3620                    (mail-header-from h1))))
3621      (or (car extract) (cadr extract) ""))
3622    (let ((extract (funcall
3623                    gnus-extract-address-components
3624                    (mail-header-from h2))))
3625      (or (car extract) (cadr extract) ""))))
3626
3627 (defun gnus-thread-sort-by-author (h1 h2)
3628   "Sort threads by root author."
3629   (gnus-article-sort-by-author
3630    (gnus-thread-header h1)  (gnus-thread-header h2)))
3631
3632 (defsubst gnus-article-sort-by-subject (h1 h2)
3633   "Sort articles by root subject."
3634   (string-lessp
3635    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3636    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3637
3638 (defun gnus-thread-sort-by-subject (h1 h2)
3639   "Sort threads by root subject."
3640   (gnus-article-sort-by-subject
3641    (gnus-thread-header h1) (gnus-thread-header h2)))
3642
3643 (defsubst gnus-article-sort-by-date (h1 h2)
3644   "Sort articles by root article date."
3645   (time-less-p
3646    (gnus-date-get-time (mail-header-date h1))
3647    (gnus-date-get-time (mail-header-date h2))))
3648
3649 (defun gnus-thread-sort-by-date (h1 h2)
3650   "Sort threads by root article date."
3651   (gnus-article-sort-by-date
3652    (gnus-thread-header h1) (gnus-thread-header h2)))
3653
3654 (defsubst gnus-article-sort-by-score (h1 h2)
3655   "Sort articles by root article score.
3656 Unscored articles will be counted as having a score of zero."
3657   (> (or (cdr (assq (mail-header-number h1)
3658                     gnus-newsgroup-scored))
3659          gnus-summary-default-score 0)
3660      (or (cdr (assq (mail-header-number h2)
3661                     gnus-newsgroup-scored))
3662          gnus-summary-default-score 0)))
3663
3664 (defun gnus-thread-sort-by-score (h1 h2)
3665   "Sort threads by root article score."
3666   (gnus-article-sort-by-score
3667    (gnus-thread-header h1) (gnus-thread-header h2)))
3668
3669 (defun gnus-thread-sort-by-total-score (h1 h2)
3670   "Sort threads by the sum of all scores in the thread.
3671 Unscored articles will be counted as having a score of zero."
3672   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3673
3674 (defun gnus-thread-total-score (thread)
3675   ;;  This function find the total score of THREAD.
3676   (cond ((null thread)
3677          0)
3678         ((consp thread)
3679          (if (stringp (car thread))
3680              (apply gnus-thread-score-function 0
3681                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3682            (gnus-thread-total-score-1 thread)))
3683         (t
3684          (gnus-thread-total-score-1 (list thread)))))
3685
3686 (defun gnus-thread-total-score-1 (root)
3687   ;; This function find the total score of the thread below ROOT.
3688   (setq root (car root))
3689   (apply gnus-thread-score-function
3690          (or (append
3691               (mapcar 'gnus-thread-total-score
3692                       (cdr (gnus-id-to-thread (mail-header-id root))))
3693               (when (> (mail-header-number root) 0)
3694                 (list (or (cdr (assq (mail-header-number root)
3695                                      gnus-newsgroup-scored))
3696                           gnus-summary-default-score 0))))
3697              (list gnus-summary-default-score)
3698              '(0))))
3699
3700 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3701 (defvar gnus-tmp-prev-subject nil)
3702 (defvar gnus-tmp-false-parent nil)
3703 (defvar gnus-tmp-root-expunged nil)
3704 (defvar gnus-tmp-dummy-line nil)
3705
3706 (defvar gnus-tmp-header)
3707 (defun gnus-extra-header (type &optional header)
3708   "Return the extra header of TYPE."
3709   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3710       ""))
3711
3712 (defun gnus-summary-prepare-threads (threads)
3713   "Prepare summary buffer from THREADS and indentation LEVEL.
3714 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3715 or a straight list of headers."
3716   (gnus-message 7 "Generating summary...")
3717
3718   (setq gnus-newsgroup-threads threads)
3719   (beginning-of-line)
3720
3721   (let ((gnus-tmp-level 0)
3722         (default-score (or gnus-summary-default-score 0))
3723         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3724         thread number subject stack state gnus-tmp-gathered beg-match
3725         new-roots gnus-tmp-new-adopts thread-end
3726         gnus-tmp-header gnus-tmp-unread
3727         gnus-tmp-replied gnus-tmp-subject-or-nil
3728         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3729         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3730         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3731
3732     (setq gnus-tmp-prev-subject nil)
3733
3734     (if (vectorp (car threads))
3735         ;; If this is a straight (sic) list of headers, then a
3736         ;; threaded summary display isn't required, so we just create
3737         ;; an unthreaded one.
3738         (gnus-summary-prepare-unthreaded threads)
3739
3740       ;; Do the threaded display.
3741
3742       (while (or threads stack gnus-tmp-new-adopts new-roots)
3743
3744         (if (and (= gnus-tmp-level 0)
3745                  (or (not stack)
3746                      (= (caar stack) 0))
3747                  (not gnus-tmp-false-parent)
3748                  (or gnus-tmp-new-adopts new-roots))
3749             (if gnus-tmp-new-adopts
3750                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3751                       thread (list (car gnus-tmp-new-adopts))
3752                       gnus-tmp-header (caar thread)
3753                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3754               (when new-roots
3755                 (setq thread (list (car new-roots))
3756                       gnus-tmp-header (caar thread)
3757                       new-roots (cdr new-roots))))
3758
3759           (if threads
3760               ;; If there are some threads, we do them before the
3761               ;; threads on the stack.
3762               (setq thread threads
3763                     gnus-tmp-header (caar thread))
3764             ;; There were no current threads, so we pop something off
3765             ;; the stack.
3766             (setq state (car stack)
3767                   gnus-tmp-level (car state)
3768                   thread (cdr state)
3769                   stack (cdr stack)
3770                   gnus-tmp-header (caar thread))))
3771
3772         (setq gnus-tmp-false-parent nil)
3773         (setq gnus-tmp-root-expunged nil)
3774         (setq thread-end nil)
3775
3776         (if (stringp gnus-tmp-header)
3777             ;; The header is a dummy root.
3778             (cond
3779              ((eq gnus-summary-make-false-root 'adopt)
3780               ;; We let the first article adopt the rest.
3781               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3782                                                (cddar thread)))
3783               (setq gnus-tmp-gathered
3784                     (nconc (mapcar
3785                             (lambda (h) (mail-header-number (car h)))
3786                             (cddar thread))
3787                            gnus-tmp-gathered))
3788               (setq thread (cons (list (caar thread)
3789                                        (cadar thread))
3790                                  (cdr thread)))
3791               (setq gnus-tmp-level -1
3792                     gnus-tmp-false-parent t))
3793              ((eq gnus-summary-make-false-root 'empty)
3794               ;; We print adopted articles with empty subject fields.
3795               (setq gnus-tmp-gathered
3796                     (nconc (mapcar
3797                             (lambda (h) (mail-header-number (car h)))
3798                             (cddar thread))
3799                            gnus-tmp-gathered))
3800               (setq gnus-tmp-level -1))
3801              ((eq gnus-summary-make-false-root 'dummy)
3802               ;; We remember that we probably want to output a dummy
3803               ;; root.
3804               (setq gnus-tmp-dummy-line gnus-tmp-header)
3805               (setq gnus-tmp-prev-subject gnus-tmp-header))
3806              (t
3807               ;; We do not make a root for the gathered
3808               ;; sub-threads at all.
3809               (setq gnus-tmp-level -1)))
3810
3811           (setq number (mail-header-number gnus-tmp-header)
3812                 subject (mail-header-subject gnus-tmp-header))
3813
3814           (cond
3815            ;; If the thread has changed subject, we might want to make
3816            ;; this subthread into a root.
3817            ((and (null gnus-thread-ignore-subject)
3818                  (not (zerop gnus-tmp-level))
3819                  gnus-tmp-prev-subject
3820                  (not (inline
3821                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3822             (setq new-roots (nconc new-roots (list (car thread)))
3823                   thread-end t
3824                   gnus-tmp-header nil))
3825            ;; If the article lies outside the current limit,
3826            ;; then we do not display it.
3827            ((not (memq number gnus-newsgroup-limit))
3828             (setq gnus-tmp-gathered
3829                   (nconc (mapcar
3830                           (lambda (h) (mail-header-number (car h)))
3831                           (cdar thread))
3832                          gnus-tmp-gathered))
3833             (setq gnus-tmp-new-adopts (if (cdar thread)
3834                                           (append gnus-tmp-new-adopts
3835                                                   (cdar thread))
3836                                         gnus-tmp-new-adopts)
3837                   thread-end t
3838                   gnus-tmp-header nil)
3839             (when (zerop gnus-tmp-level)
3840               (setq gnus-tmp-root-expunged t)))
3841            ;; Perhaps this article is to be marked as read?
3842            ((and gnus-summary-mark-below
3843                  (< (or (cdr (assq number gnus-newsgroup-scored))
3844                         default-score)
3845                     gnus-summary-mark-below)
3846                  ;; Don't touch sparse articles.
3847                  (not (gnus-summary-article-sparse-p number))
3848                  (not (gnus-summary-article-ancient-p number)))
3849             (setq gnus-newsgroup-unreads
3850                   (delq number gnus-newsgroup-unreads))
3851             (if gnus-newsgroup-auto-expire
3852                 (push number gnus-newsgroup-expirable)
3853               (push (cons number gnus-low-score-mark)
3854                     gnus-newsgroup-reads))))
3855
3856           (when gnus-tmp-header
3857             ;; We may have an old dummy line to output before this
3858             ;; article.
3859             (when (and gnus-tmp-dummy-line
3860                        (gnus-subject-equal
3861                         gnus-tmp-dummy-line
3862                         (mail-header-subject gnus-tmp-header)))
3863               (gnus-summary-insert-dummy-line
3864                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3865               (setq gnus-tmp-dummy-line nil))
3866
3867             ;; Compute the mark.
3868             (setq gnus-tmp-unread (gnus-article-mark number))
3869
3870             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3871                                   gnus-tmp-header gnus-tmp-level)
3872                   gnus-newsgroup-data)
3873
3874             ;; Actually insert the line.
3875             (setq
3876              gnus-tmp-subject-or-nil
3877              (cond
3878               ((and gnus-thread-ignore-subject
3879                     gnus-tmp-prev-subject
3880                     (not (inline (gnus-subject-equal
3881                                   gnus-tmp-prev-subject subject))))
3882                subject)
3883               ((zerop gnus-tmp-level)
3884                (if (and (eq gnus-summary-make-false-root 'empty)
3885                         (memq number gnus-tmp-gathered)
3886                         gnus-tmp-prev-subject
3887                         (inline (gnus-subject-equal
3888                                  gnus-tmp-prev-subject subject)))
3889                    gnus-summary-same-subject
3890                  subject))
3891               (t gnus-summary-same-subject)))
3892             (if (and (eq gnus-summary-make-false-root 'adopt)
3893                      (= gnus-tmp-level 1)
3894                      (memq number gnus-tmp-gathered))
3895                 (setq gnus-tmp-opening-bracket ?\<
3896                       gnus-tmp-closing-bracket ?\>)
3897               (setq gnus-tmp-opening-bracket ?\[
3898                     gnus-tmp-closing-bracket ?\]))
3899             (setq
3900              gnus-tmp-indentation
3901              (aref gnus-thread-indent-array gnus-tmp-level)
3902              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3903              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3904                                 gnus-summary-default-score 0)
3905              gnus-tmp-score-char
3906              (if (or (null gnus-summary-default-score)
3907                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3908                          gnus-summary-zcore-fuzz))
3909                  ? 
3910                (if (< gnus-tmp-score gnus-summary-default-score)
3911                    gnus-score-below-mark gnus-score-over-mark))
3912              gnus-tmp-replied
3913              (cond ((memq number gnus-newsgroup-processable)
3914                     gnus-process-mark)
3915                    ((memq number gnus-newsgroup-cached)
3916                     gnus-cached-mark)
3917                    ((memq number gnus-newsgroup-replied)
3918                     gnus-replied-mark)
3919                    ((memq number gnus-newsgroup-saved)
3920                     gnus-saved-mark)
3921                    (t gnus-unread-mark))
3922              gnus-tmp-from (mail-header-from gnus-tmp-header)
3923              gnus-tmp-name
3924              (cond
3925               ((string-match "<[^>]+> *$" gnus-tmp-from)
3926                (setq beg-match (match-beginning 0))
3927                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3928                         (substring gnus-tmp-from (1+ (match-beginning 0))
3929                                    (1- (match-end 0))))
3930                    (substring gnus-tmp-from 0 beg-match)))
3931               ((string-match "(.+)" gnus-tmp-from)
3932                (substring gnus-tmp-from
3933                           (1+ (match-beginning 0)) (1- (match-end 0))))
3934               (t gnus-tmp-from)))
3935             (when (string= gnus-tmp-name "")
3936               (setq gnus-tmp-name gnus-tmp-from))
3937             (unless (numberp gnus-tmp-lines)
3938               (setq gnus-tmp-lines 0))
3939             (gnus-put-text-property
3940              (point)
3941              (progn (eval gnus-summary-line-format-spec) (point))
3942              'gnus-number number)
3943             (when gnus-visual-p
3944               (forward-line -1)
3945               (gnus-run-hooks 'gnus-summary-update-hook)
3946               (forward-line 1))
3947
3948             (setq gnus-tmp-prev-subject subject)))
3949
3950         (when (nth 1 thread)
3951           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3952         (incf gnus-tmp-level)
3953         (setq threads (if thread-end nil (cdar thread)))
3954         (unless threads
3955           (setq gnus-tmp-level 0)))))
3956   (gnus-message 7 "Generating summary...done"))
3957
3958 (defun gnus-summary-prepare-unthreaded (headers)
3959   "Generate an unthreaded summary buffer based on HEADERS."
3960   (let (header number mark)
3961
3962     (beginning-of-line)
3963
3964     (while headers
3965       ;; We may have to root out some bad articles...
3966       (when (memq (setq number (mail-header-number
3967                                 (setq header (pop headers))))
3968                   gnus-newsgroup-limit)
3969         ;; Mark article as read when it has a low score.
3970         (when (and gnus-summary-mark-below
3971                    (< (or (cdr (assq number gnus-newsgroup-scored))
3972                           gnus-summary-default-score 0)
3973                       gnus-summary-mark-below)
3974                    (not (gnus-summary-article-ancient-p number)))
3975           (setq gnus-newsgroup-unreads
3976                 (delq number gnus-newsgroup-unreads))
3977           (if gnus-newsgroup-auto-expire
3978               (push number gnus-newsgroup-expirable)
3979             (push (cons number gnus-low-score-mark)
3980                   gnus-newsgroup-reads)))
3981
3982         (setq mark (gnus-article-mark number))
3983         (push (gnus-data-make number mark (1+ (point)) header 0)
3984               gnus-newsgroup-data)
3985         (gnus-summary-insert-line
3986          header 0 number
3987          mark (memq number gnus-newsgroup-replied)
3988          (memq number gnus-newsgroup-expirable)
3989          (mail-header-subject header) nil
3990          (cdr (assq number gnus-newsgroup-scored))
3991          (memq number gnus-newsgroup-processable))))))
3992
3993 (defun gnus-select-newsgroup (group &optional read-all select-articles)
3994   "Select newsgroup GROUP.
3995 If READ-ALL is non-nil, all articles in the group are selected.
3996 If SELECT-ARTICLES, only select those articles from GROUP."
3997   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3998          ;;!!! Dirty hack; should be removed.
3999          (gnus-summary-ignore-duplicates
4000           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4001               t
4002             gnus-summary-ignore-duplicates))
4003          (info (nth 2 entry))
4004          articles fetched-articles cached)
4005
4006     (unless (gnus-check-server
4007              (setq gnus-current-select-method
4008                    (gnus-find-method-for-group group)))
4009       (error "Couldn't open server"))
4010
4011     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4012         (gnus-activate-group group)     ; Or we can activate it...
4013         (progn                          ; Or we bug out.
4014           (when (equal major-mode 'gnus-summary-mode)
4015             (kill-buffer (current-buffer)))
4016           (error "Couldn't request group %s: %s"
4017                  group (gnus-status-message group))))
4018
4019     (unless (gnus-request-group group t)
4020       (when (equal major-mode 'gnus-summary-mode)
4021         (kill-buffer (current-buffer)))
4022       (error "Couldn't request group %s: %s"
4023              group (gnus-status-message group)))
4024
4025     (setq gnus-newsgroup-name group)
4026     (setq gnus-newsgroup-unselected nil)
4027     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4028     (gnus-newsgroup-setup-default-charset)
4029
4030     ;; Adjust and set lists of article marks.
4031     (when info
4032       (gnus-adjust-marked-articles info))
4033
4034     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4035     (when (gnus-virtual-group-p group)
4036       (setq cached gnus-newsgroup-cached))
4037
4038     (setq gnus-newsgroup-unreads
4039           (gnus-set-difference
4040            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4041            gnus-newsgroup-dormant))
4042
4043     (setq gnus-newsgroup-processable nil)
4044
4045     (gnus-update-read-articles group gnus-newsgroup-unreads)
4046
4047     (if (setq articles select-articles)
4048         (setq gnus-newsgroup-unselected
4049               (gnus-sorted-intersection
4050                gnus-newsgroup-unreads
4051                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4052       (setq articles (gnus-articles-to-read group read-all)))
4053
4054     (cond
4055      ((null articles)
4056       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4057       'quit)
4058      ((eq articles 0) nil)
4059      (t
4060       ;; Init the dependencies hash table.
4061       (setq gnus-newsgroup-dependencies
4062             (gnus-make-hashtable (length articles)))
4063       (gnus-set-global-variables)
4064       ;; Retrieve the headers and read them in.
4065       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4066       (setq gnus-newsgroup-headers
4067             (if (eq 'nov
4068                     (setq gnus-headers-retrieved-by
4069                           (gnus-retrieve-headers
4070                            articles gnus-newsgroup-name
4071                            ;; We might want to fetch old headers, but
4072                            ;; not if there is only 1 article.
4073                            (and (or (and
4074                                      (not (eq gnus-fetch-old-headers 'some))
4075                                      (not (numberp gnus-fetch-old-headers)))
4076                                     (> (length articles) 1))
4077                                 gnus-fetch-old-headers))))
4078                 (gnus-get-newsgroup-headers-xover
4079                  articles nil nil gnus-newsgroup-name t)
4080               (gnus-get-newsgroup-headers)))
4081       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4082
4083       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4084       (when cached
4085         (setq gnus-newsgroup-cached cached))
4086
4087       ;; Suppress duplicates?
4088       (when gnus-suppress-duplicates
4089         (gnus-dup-suppress-articles))
4090
4091       ;; Set the initial limit.
4092       (setq gnus-newsgroup-limit (copy-sequence articles))
4093       ;; Remove canceled articles from the list of unread articles.
4094       (setq gnus-newsgroup-unreads
4095             (gnus-set-sorted-intersection
4096              gnus-newsgroup-unreads
4097              (setq fetched-articles
4098                    (mapcar (lambda (headers) (mail-header-number headers))
4099                            gnus-newsgroup-headers))))
4100       ;; Removed marked articles that do not exist.
4101       (gnus-update-missing-marks
4102        (gnus-sorted-complement fetched-articles articles))
4103       ;; We might want to build some more threads first.
4104       (when (and gnus-fetch-old-headers
4105                  (eq gnus-headers-retrieved-by 'nov))
4106         (if (eq gnus-fetch-old-headers 'invisible)
4107             (gnus-build-all-threads)
4108           (gnus-build-old-threads)))
4109       ;; Let the Gnus agent mark articles as read.
4110       (when gnus-agent
4111         (gnus-agent-get-undownloaded-list))
4112       ;; Check whether auto-expire is to be done in this group.
4113       (setq gnus-newsgroup-auto-expire
4114             (gnus-group-auto-expirable-p group))
4115       ;; Set up the article buffer now, if necessary.
4116       (unless gnus-single-article-buffer
4117         (gnus-article-setup-buffer))
4118       ;; First and last article in this newsgroup.
4119       (when gnus-newsgroup-headers
4120         (setq gnus-newsgroup-begin
4121               (mail-header-number (car gnus-newsgroup-headers))
4122               gnus-newsgroup-end
4123               (mail-header-number
4124                (gnus-last-element gnus-newsgroup-headers))))
4125       ;; GROUP is successfully selected.
4126       (or gnus-newsgroup-headers t)))))
4127
4128 (defun gnus-articles-to-read (group &optional read-all)
4129   ;; Find out what articles the user wants to read.
4130   (let* ((articles
4131           ;; Select all articles if `read-all' is non-nil, or if there
4132           ;; are no unread articles.
4133           (if (or read-all
4134                   (and (zerop (length gnus-newsgroup-marked))
4135                        (zerop (length gnus-newsgroup-unreads)))
4136                   (eq (gnus-group-find-parameter group 'display)
4137                       'all))
4138               (gnus-uncompress-range (gnus-active group))
4139             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4140                           (copy-sequence gnus-newsgroup-unreads))
4141                   '<)))
4142          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4143          (scored (length scored-list))
4144          (number (length articles))
4145          (marked (+ (length gnus-newsgroup-marked)
4146                     (length gnus-newsgroup-dormant)))
4147          (select
4148           (cond
4149            ((numberp read-all)
4150             read-all)
4151            (t
4152             (condition-case ()
4153                 (cond
4154                  ((and (or (<= scored marked) (= scored number))
4155                        (numberp gnus-large-newsgroup)
4156                        (> number gnus-large-newsgroup))
4157                   (let ((input
4158                          (read-string
4159                           (format
4160                            "How many articles from %s (default %d): "
4161                            (gnus-limit-string gnus-newsgroup-name 35)
4162                            number))))
4163                     (if (string-match "^[ \t]*$" input) number input)))
4164                  ((and (> scored marked) (< scored number)
4165                        (> (- scored number) 20))
4166                   (let ((input
4167                          (read-string
4168                           (format "%s %s (%d scored, %d total): "
4169                                   "How many articles from"
4170                                   group scored number))))
4171                     (if (string-match "^[ \t]*$" input)
4172                         number input)))
4173                  (t number))
4174               (quit nil))))))
4175     (setq select (if (stringp select) (string-to-number select) select))
4176     (if (or (null select) (zerop select))
4177         select
4178       (if (and (not (zerop scored)) (<= (abs select) scored))
4179           (progn
4180             (setq articles (sort scored-list '<))
4181             (setq number (length articles)))
4182         (setq articles (copy-sequence articles)))
4183
4184       (when (< (abs select) number)
4185         (if (< select 0)
4186             ;; Select the N oldest articles.
4187             (setcdr (nthcdr (1- (abs select)) articles) nil)
4188           ;; Select the N most recent articles.
4189           (setq articles (nthcdr (- number select) articles))))
4190       (setq gnus-newsgroup-unselected
4191             (gnus-sorted-intersection
4192              gnus-newsgroup-unreads
4193              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4194       articles)))
4195
4196 (defun gnus-killed-articles (killed articles)
4197   (let (out)
4198     (while articles
4199       (when (inline (gnus-member-of-range (car articles) killed))
4200         (push (car articles) out))
4201       (setq articles (cdr articles)))
4202     out))
4203
4204 (defun gnus-uncompress-marks (marks)
4205   "Uncompress the mark ranges in MARKS."
4206   (let ((uncompressed '(score bookmark))
4207         out)
4208     (while marks
4209       (if (memq (caar marks) uncompressed)
4210           (push (car marks) out)
4211         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4212       (setq marks (cdr marks)))
4213     out))
4214
4215 (defun gnus-adjust-marked-articles (info)
4216   "Set all article lists and remove all marks that are no longer legal."
4217   (let* ((marked-lists (gnus-info-marks info))
4218          (active (gnus-active (gnus-info-group info)))
4219          (min (car active))
4220          (max (cdr active))
4221          (types gnus-article-mark-lists)
4222          (uncompressed '(score bookmark killed))
4223          marks var articles article mark)
4224
4225     (while marked-lists
4226       (setq marks (pop marked-lists))
4227       (set (setq var (intern (format "gnus-newsgroup-%s"
4228                                      (car (rassq (setq mark (car marks))
4229                                                  types)))))
4230            (if (memq (car marks) uncompressed) (cdr marks)
4231              (gnus-uncompress-range (cdr marks))))
4232
4233       (setq articles (symbol-value var))
4234
4235       ;; All articles have to be subsets of the active articles.
4236       (cond
4237        ;; Adjust "simple" lists.
4238        ((memq mark '(tick dormant expire reply save))
4239         (while articles
4240           (when (or (< (setq article (pop articles)) min) (> article max))
4241             (set var (delq article (symbol-value var))))))
4242        ;; Adjust assocs.
4243        ((memq mark uncompressed)
4244         (when (not (listp (cdr (symbol-value var))))
4245           (set var (list (symbol-value var))))
4246         (when (not (listp (cdr articles)))
4247           (setq articles (list articles)))
4248         (while articles
4249           (when (or (not (consp (setq article (pop articles))))
4250                     (< (car article) min)
4251                     (> (car article) max))
4252             (set var (delq article (symbol-value var))))))))))
4253
4254 (defun gnus-update-missing-marks (missing)
4255   "Go through the list of MISSING articles and remove them from the mark lists."
4256   (when missing
4257     (let ((types gnus-article-mark-lists)
4258           var m)
4259       ;; Go through all types.
4260       (while types
4261         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4262         (when (symbol-value var)
4263           ;; This list has articles.  So we delete all missing articles
4264           ;; from it.
4265           (setq m missing)
4266           (while m
4267             (set var (delq (pop m) (symbol-value var)))))))))
4268
4269 (defun gnus-update-marks ()
4270   "Enter the various lists of marked articles into the newsgroup info list."
4271   (let ((types gnus-article-mark-lists)
4272         (info (gnus-get-info gnus-newsgroup-name))
4273         (uncompressed '(score bookmark killed))
4274         type list newmarked symbol delta-marks)
4275     (when info
4276       ;; Add all marks lists that are non-nil to the list of marks lists.
4277       (while (setq type (pop types))
4278         (when (setq list (symbol-value
4279                           (setq symbol
4280                                 (intern (format "gnus-newsgroup-%s"
4281                                                 (car type))))))
4282
4283           ;; Get rid of the entries of the articles that have the
4284           ;; default score.
4285           (when (and (eq (cdr type) 'score)
4286                      gnus-save-score
4287                      list)
4288             (let* ((arts list)
4289                    (prev (cons nil list))
4290                    (all prev))
4291               (while arts
4292                 (if (or (not (consp (car arts)))
4293                         (= (cdar arts) gnus-summary-default-score))
4294                     (setcdr prev (cdr arts))
4295                   (setq prev arts))
4296                 (setq arts (cdr arts)))
4297               (setq list (cdr all))))
4298
4299          (when (gnus-check-backend-function 'request-set-mark 
4300                                             gnus-newsgroup-name)
4301            ;; score & bookmark are not proper flags (they are cons cells)
4302            ;; cache is a internal gnus flag
4303            (unless (memq (cdr type) '(cache score bookmark))
4304              (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4305                     (del (gnus-remove-from-range old list))
4306                     (add (gnus-remove-from-range list old)))
4307                (if add
4308                    (push (list add 'add (list (cdr type))) delta-marks))
4309                (if del
4310                    (push (list del 'del (list (cdr type))) delta-marks)))))
4311          
4312           (push (cons (cdr type)
4313                       (if (memq (cdr type) uncompressed) list
4314                         (gnus-compress-sequence
4315                          (set symbol (sort list '<)) t)))
4316                 newmarked)))
4317
4318       (if delta-marks
4319          (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4320
4321       ;; Enter these new marks into the info of the group.
4322       (if (nthcdr 3 info)
4323           (setcar (nthcdr 3 info) newmarked)
4324         ;; Add the marks lists to the end of the info.
4325         (when newmarked
4326           (setcdr (nthcdr 2 info) (list newmarked))))
4327
4328       ;; Cut off the end of the info if there's nothing else there.
4329       (let ((i 5))
4330         (while (and (> i 2)
4331                     (not (nth i info)))
4332           (when (nthcdr (decf i) info)
4333             (setcdr (nthcdr i info) nil)))))))
4334
4335 (defun gnus-set-mode-line (where)
4336   "This function sets the mode line of the article or summary buffers.
4337 If WHERE is `summary', the summary mode line format will be used."
4338   ;; Is this mode line one we keep updated?
4339   (when (and (memq where gnus-updated-mode-lines)
4340              (symbol-value
4341               (intern (format "gnus-%s-mode-line-format-spec" where))))
4342     (let (mode-string)
4343       (save-excursion
4344         ;; We evaluate this in the summary buffer since these
4345         ;; variables are buffer-local to that buffer.
4346         (set-buffer gnus-summary-buffer)
4347         ;; We bind all these variables that are used in the `eval' form
4348         ;; below.
4349         (let* ((mformat (symbol-value
4350                          (intern
4351                           (format "gnus-%s-mode-line-format-spec" where))))
4352                (gnus-tmp-group-name gnus-newsgroup-name)
4353                (gnus-tmp-article-number (or gnus-current-article 0))
4354                (gnus-tmp-unread gnus-newsgroup-unreads)
4355                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4356                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4357                (gnus-tmp-unread-and-unselected
4358                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4359                             (zerop gnus-tmp-unselected))
4360                        "")
4361                       ((zerop gnus-tmp-unselected)
4362                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4363                       (t (format "{%d(+%d) more}"
4364                                  gnus-tmp-unread-and-unticked
4365                                  gnus-tmp-unselected))))
4366                (gnus-tmp-subject
4367                 (if (and gnus-current-headers
4368                          (vectorp gnus-current-headers))
4369                     (gnus-mode-string-quote
4370                      (mail-header-subject gnus-current-headers))
4371                   ""))
4372                bufname-length max-len
4373                gnus-tmp-header);; passed as argument to any user-format-funcs
4374           (setq mode-string (eval mformat))
4375           (setq bufname-length (if (string-match "%b" mode-string)
4376                                    (- (length
4377                                        (buffer-name
4378                                         (if (eq where 'summary)
4379                                             nil
4380                                           (get-buffer gnus-article-buffer))))
4381                                       2)
4382                                  0))
4383           (setq max-len (max 4 (if gnus-mode-non-string-length
4384                                    (- (window-width)
4385                                       gnus-mode-non-string-length
4386                                       bufname-length)
4387                                  (length mode-string))))
4388           ;; We might have to chop a bit of the string off...
4389           (when (> (length mode-string) max-len)
4390             (setq mode-string
4391                   (concat (truncate-string-to-width mode-string (- max-len 3))
4392                           "...")))
4393           ;; Pad the mode string a bit.
4394           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4395       ;; Update the mode line.
4396       (setq mode-line-buffer-identification
4397             (gnus-mode-line-buffer-identification (list mode-string)))
4398       (set-buffer-modified-p t))))
4399
4400 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4401   "Go through the HEADERS list and add all Xrefs to a hash table.
4402 The resulting hash table is returned, or nil if no Xrefs were found."
4403   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4404          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4405          (xref-hashtb (gnus-make-hashtable))
4406          start group entry number xrefs header)
4407     (while headers
4408       (setq header (pop headers))
4409       (when (and (setq xrefs (mail-header-xref header))
4410                  (not (memq (setq number (mail-header-number header))
4411                             unreads)))
4412         (setq start 0)
4413         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4414           (setq start (match-end 0))
4415           (setq group (if prefix
4416                           (concat prefix (substring xrefs (match-beginning 1)
4417                                                     (match-end 1)))
4418                         (substring xrefs (match-beginning 1) (match-end 1))))
4419           (setq number
4420                 (string-to-int (substring xrefs (match-beginning 2)
4421                                           (match-end 2))))
4422           (if (setq entry (gnus-gethash group xref-hashtb))
4423               (setcdr entry (cons number (cdr entry)))
4424             (gnus-sethash group (cons number nil) xref-hashtb)))))
4425     (and start xref-hashtb)))
4426
4427 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4428   "Look through all the headers and mark the Xrefs as read."
4429   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4430         name entry info xref-hashtb idlist method nth4)
4431     (save-excursion
4432       (set-buffer gnus-group-buffer)
4433       (when (setq xref-hashtb
4434                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4435         (mapatoms
4436          (lambda (group)
4437            (unless (string= from-newsgroup (setq name (symbol-name group)))
4438              (setq idlist (symbol-value group))
4439              ;; Dead groups are not updated.
4440              (and (prog1
4441                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4442                             info (nth 2 entry))
4443                     (when (stringp (setq nth4 (gnus-info-method info)))
4444                       (setq nth4 (gnus-server-to-method nth4))))
4445                   ;; Only do the xrefs if the group has the same
4446                   ;; select method as the group we have just read.
4447                   (or (gnus-methods-equal-p
4448                        nth4 (gnus-find-method-for-group from-newsgroup))
4449                       virtual
4450                       (equal nth4 (setq method (gnus-find-method-for-group
4451                                                 from-newsgroup)))
4452                       (and (equal (car nth4) (car method))
4453                            (equal (nth 1 nth4) (nth 1 method))))
4454                   gnus-use-cross-reference
4455                   (or (not (eq gnus-use-cross-reference t))
4456                       virtual
4457                       ;; Only do cross-references on subscribed
4458                       ;; groups, if that is what is wanted.
4459                       (<= (gnus-info-level info) gnus-level-subscribed))
4460                   (gnus-group-make-articles-read name idlist))))
4461          xref-hashtb)))))
4462
4463 (defun gnus-compute-read-articles (group articles)
4464   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4465          (info (nth 2 entry))
4466          (active (gnus-active group))
4467          ninfo)
4468     (when entry
4469       ;; First peel off all illegal article numbers.
4470       (when active
4471         (let ((ids articles)
4472               id first)
4473           (while (setq id (pop ids))
4474             (when (and first (> id (cdr active)))
4475               ;; We'll end up in this situation in one particular
4476               ;; obscure situation.  If you re-scan a group and get
4477               ;; a new article that is cross-posted to a different
4478               ;; group that has not been re-scanned, you might get
4479               ;; crossposted article that has a higher number than
4480               ;; Gnus believes possible.  So we re-activate this
4481               ;; group as well.  This might mean doing the
4482               ;; crossposting thingy will *increase* the number
4483               ;; of articles in some groups.  Tsk, tsk.
4484               (setq active (or (gnus-activate-group group) active)))
4485             (when (or (> id (cdr active))
4486                       (< id (car active)))
4487               (setq articles (delq id articles))))))
4488       ;; If the read list is nil, we init it.
4489       (if (and active
4490                (null (gnus-info-read info))
4491                (> (car active) 1))
4492           (setq ninfo (cons 1 (1- (car active))))
4493         (setq ninfo (gnus-info-read info)))
4494       ;; Then we add the read articles to the range.
4495       (gnus-add-to-range
4496        ninfo (setq articles (sort articles '<))))))
4497
4498 (defun gnus-group-make-articles-read (group articles)
4499   "Update the info of GROUP to say that ARTICLES are read."
4500   (let* ((num 0)
4501          (entry (gnus-gethash group gnus-newsrc-hashtb))
4502          (info (nth 2 entry))
4503          (active (gnus-active group))
4504          range)
4505     (when entry
4506       (setq range (gnus-compute-read-articles group articles))
4507       (save-excursion
4508         (set-buffer gnus-group-buffer)
4509         (gnus-undo-register
4510           `(progn
4511              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4512              (gnus-info-set-read ',info ',(gnus-info-read info))
4513              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4514              (gnus-group-update-group ,group t))))
4515       ;; Add the read articles to the range.
4516       (gnus-info-set-read info range)
4517       ;; Then we have to re-compute how many unread
4518       ;; articles there are in this group.
4519       (when active
4520         (cond
4521          ((not range)
4522           (setq num (- (1+ (cdr active)) (car active))))
4523          ((not (listp (cdr range)))
4524           (setq num (- (cdr active) (- (1+ (cdr range))
4525                                        (car range)))))
4526          (t
4527           (while range
4528             (if (numberp (car range))
4529                 (setq num (1+ num))
4530               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4531             (setq range (cdr range)))
4532           (setq num (- (cdr active) num))))
4533         ;; Update the number of unread articles.
4534         (setcar entry num)
4535         ;; Update the group buffer.
4536         (gnus-group-update-group group t)))))
4537
4538 (defun gnus-methods-equal-p (m1 m2)
4539   (let ((m1 (or m1 gnus-select-method))
4540         (m2 (or m2 gnus-select-method)))
4541     (or (equal m1 m2)
4542         (and (eq (car m1) (car m2))
4543              (or (not (memq 'address (assoc (symbol-name (car m1))
4544                                             gnus-valid-select-methods)))
4545                  (equal (nth 1 m1) (nth 1 m2)))))))
4546
4547 (defvar gnus-newsgroup-none-id 0)
4548
4549 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4550   (let ((cur nntp-server-buffer)
4551         (dependencies
4552          (or dependencies
4553              (save-excursion (set-buffer gnus-summary-buffer)
4554                              gnus-newsgroup-dependencies)))
4555         headers id end ref
4556         (rfc2047-default-charset gnus-newsgroup-default-charset)
4557         (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
4558     (save-excursion
4559       (set-buffer nntp-server-buffer)
4560       ;; Translate all TAB characters into SPACE characters.
4561       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4562       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4563       (gnus-run-hooks 'gnus-parse-headers-hook)
4564       (let ((case-fold-search t)
4565             in-reply-to header p lines chars)
4566         (goto-char (point-min))
4567         ;; Search to the beginning of the next header.  Error messages
4568         ;; do not begin with 2 or 3.
4569         (while (re-search-forward "^[23][0-9]+ " nil t)
4570           (setq id nil
4571                 ref nil)
4572           ;; This implementation of this function, with nine
4573           ;; search-forwards instead of the one re-search-forward and
4574           ;; a case (which basically was the old function) is actually
4575           ;; about twice as fast, even though it looks messier.  You
4576           ;; can't have everything, I guess.  Speed and elegance
4577           ;; doesn't always go hand in hand.
4578           (setq
4579            header
4580            (vector
4581             ;; Number.
4582             (prog1
4583                 (read cur)
4584               (end-of-line)
4585               (setq p (point))
4586               (narrow-to-region (point)
4587                                 (or (and (search-forward "\n.\n" nil t)
4588                                          (- (point) 2))
4589                                     (point))))
4590             ;; Subject.
4591             (progn
4592               (goto-char p)
4593               (if (search-forward "\nsubject: " nil t)
4594                   (funcall gnus-decode-encoded-word-function
4595                            (nnheader-header-value))
4596                 "(none)"))
4597             ;; From.
4598             (progn
4599               (goto-char p)
4600               (if (search-forward "\nfrom: " nil t)
4601                   (funcall gnus-decode-encoded-word-function
4602                            (nnheader-header-value))
4603                 "(nobody)"))
4604             ;; Date.
4605             (progn
4606               (goto-char p)
4607               (if (search-forward "\ndate: " nil t)
4608                   (nnheader-header-value) ""))
4609             ;; Message-ID.
4610             (progn
4611               (goto-char p)
4612               (setq id (if (re-search-forward
4613                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4614                            ;; We do it this way to make sure the Message-ID
4615                            ;; is (somewhat) syntactically valid.
4616                            (buffer-substring (match-beginning 1)
4617                                              (match-end 1))
4618                          ;; If there was no message-id, we just fake one
4619                          ;; to make subsequent routines simpler.
4620                          (nnheader-generate-fake-message-id))))
4621             ;; References.
4622             (progn
4623               (goto-char p)
4624               (if (search-forward "\nreferences: " nil t)
4625                   (progn
4626                     (setq end (point))
4627                     (prog1
4628                         (nnheader-header-value)
4629                       (setq ref
4630                             (buffer-substring
4631                              (progn
4632                                (end-of-line)
4633                                (search-backward ">" end t)
4634                                (1+ (point)))
4635                              (progn
4636                                (search-backward "<" end t)
4637                                (point))))))
4638                 ;; Get the references from the in-reply-to header if there
4639                 ;; were no references and the in-reply-to header looks
4640                 ;; promising.
4641                 (if (and (search-forward "\nin-reply-to: " nil t)
4642                          (setq in-reply-to (nnheader-header-value))
4643                          (string-match "<[^>]+>" in-reply-to))
4644                     (let (ref2)
4645                       (setq ref (substring in-reply-to (match-beginning 0)
4646                                            (match-end 0)))
4647                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4648                         (setq ref2 (substring in-reply-to (match-beginning 0)
4649                                               (match-end 0)))
4650                         (when (> (length ref2) (length ref))
4651                           (setq ref ref2)))
4652                       ref)
4653                   (setq ref nil))))
4654             ;; Chars.
4655             (progn
4656               (goto-char p)
4657               (if (search-forward "\nchars: " nil t)
4658                   (if (numberp (setq chars (ignore-errors (read cur))))
4659                       chars 0)
4660                 0))
4661             ;; Lines.
4662             (progn
4663               (goto-char p)
4664               (if (search-forward "\nlines: " nil t)
4665                   (if (numberp (setq lines (ignore-errors (read cur))))
4666                       lines 0)
4667                 0))
4668             ;; Xref.
4669             (progn
4670               (goto-char p)
4671               (and (search-forward "\nxref: " nil t)
4672                    (nnheader-header-value)))
4673             ;; Extra.
4674             (when gnus-extra-headers
4675               (let ((extra gnus-extra-headers)
4676                     out)
4677                 (while extra
4678                   (goto-char p)
4679                   (when (search-forward
4680                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4681                     (push (cons (car extra) (nnheader-header-value))
4682                           out))
4683                   (pop extra))
4684                 out))))
4685           (when (equal id ref)
4686             (setq ref nil))
4687
4688           (when gnus-alter-header-function
4689             (funcall gnus-alter-header-function header)
4690             (setq id (mail-header-id header)
4691                   ref (gnus-parent-id (mail-header-references header))))
4692
4693           (when (setq header
4694                       (gnus-dependencies-add-header
4695                        header dependencies force-new))
4696             (push header headers))
4697           (goto-char (point-max))
4698           (widen))
4699         (nreverse headers)))))
4700
4701 ;; Goes through the xover lines and returns a list of vectors
4702 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4703                                                   force-new dependencies
4704                                                   group also-fetch-heads)
4705   "Parse the news overview data in the server buffer, and return a
4706 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4707   ;; Get the Xref when the users reads the articles since most/some
4708   ;; NNTP servers do not include Xrefs when using XOVER.
4709   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4710   (let ((rfc2047-default-charset gnus-newsgroup-default-charset)
4711         (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced)
4712         (cur nntp-server-buffer)
4713         (dependencies (or dependencies gnus-newsgroup-dependencies))
4714         number headers header)
4715     (save-excursion
4716       (set-buffer nntp-server-buffer)
4717       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4718       ;; Allow the user to mangle the headers before parsing them.
4719       (gnus-run-hooks 'gnus-parse-headers-hook)
4720       (goto-char (point-min))
4721       (while (not (eobp))
4722         (condition-case ()
4723             (while (and sequence (not (eobp)))
4724               (setq number (read cur))
4725               (while (and sequence
4726                           (< (car sequence) number))
4727                 (setq sequence (cdr sequence)))
4728               (and sequence
4729                    (eq number (car sequence))
4730                    (progn
4731                      (setq sequence (cdr sequence))
4732                      (setq header (inline
4733                                     (gnus-nov-parse-line
4734                                      number dependencies force-new))))
4735                    (push header headers))
4736               (forward-line 1))
4737           (error
4738            (gnus-error 4 "Strange nov line (%d)"
4739                        (count-lines (point-min) (point)))))
4740         (forward-line 1))
4741       ;; A common bug in inn is that if you have posted an article and
4742       ;; then retrieves the active file, it will answer correctly --
4743       ;; the new article is included.  However, a NOV entry for the
4744       ;; article may not have been generated yet, so this may fail.
4745       ;; We work around this problem by retrieving the last few
4746       ;; headers using HEAD.
4747       (if (or (not also-fetch-heads)
4748               (not sequence))
4749           ;; We (probably) got all the headers.
4750           (nreverse headers)
4751         (let ((gnus-nov-is-evil t))
4752           (nconc
4753            (nreverse headers)
4754            (when (gnus-retrieve-headers sequence group)
4755              (gnus-get-newsgroup-headers))))))))
4756
4757 (defun gnus-article-get-xrefs ()
4758   "Fill in the Xref value in `gnus-current-headers', if necessary.
4759 This is meant to be called in `gnus-article-internal-prepare-hook'."
4760   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4761                                  gnus-current-headers)))
4762     (or (not gnus-use-cross-reference)
4763         (not headers)
4764         (and (mail-header-xref headers)
4765              (not (string= (mail-header-xref headers) "")))
4766         (let ((case-fold-search t)
4767               xref)
4768           (save-restriction
4769             (nnheader-narrow-to-headers)
4770             (goto-char (point-min))
4771             (when (or (and (eq (downcase (char-after)) ?x)
4772                            (looking-at "Xref:"))
4773                       (search-forward "\nXref:" nil t))
4774               (goto-char (1+ (match-end 0)))
4775               (setq xref (buffer-substring (point)
4776                                            (progn (end-of-line) (point))))
4777               (mail-header-set-xref headers xref)))))))
4778
4779 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4780   "Find article ID and insert the summary line for that article.
4781 OLD-HEADER can either be a header or a line number to insert
4782 the subject line on."
4783   (let* ((line (and (numberp old-header) old-header))
4784          (old-header (and (vectorp old-header) old-header))
4785          (header (cond ((and old-header use-old-header)
4786                         old-header)
4787                        ((and (numberp id)
4788                              (gnus-number-to-header id))
4789                         (gnus-number-to-header id))
4790                        (t
4791                         (gnus-read-header id))))
4792          (number (and (numberp id) id))
4793          d)
4794     (when header
4795       ;; Rebuild the thread that this article is part of and go to the
4796       ;; article we have fetched.
4797       (when (and (not gnus-show-threads)
4798                  old-header)
4799         (when (and number
4800                    (setq d (gnus-data-find (mail-header-number old-header))))
4801           (goto-char (gnus-data-pos d))
4802           (gnus-data-remove
4803            number
4804            (- (gnus-point-at-bol)
4805               (prog1
4806                   (1+ (gnus-point-at-eol))
4807                 (gnus-delete-line))))))
4808       (when old-header
4809         (mail-header-set-number header (mail-header-number old-header)))
4810       (setq gnus-newsgroup-sparse
4811             (delq (setq number (mail-header-number header))
4812                   gnus-newsgroup-sparse))
4813       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4814       (push number gnus-newsgroup-limit)
4815       (gnus-rebuild-thread (mail-header-id header) line)
4816       (gnus-summary-goto-subject number nil t))
4817     (when (and (numberp number)
4818                (> number 0))
4819       ;; We have to update the boundaries even if we can't fetch the
4820       ;; article if ID is a number -- so that the next `P' or `N'
4821       ;; command will fetch the previous (or next) article even
4822       ;; if the one we tried to fetch this time has been canceled.
4823       (when (> number gnus-newsgroup-end)
4824         (setq gnus-newsgroup-end number))
4825       (when (< number gnus-newsgroup-begin)
4826         (setq gnus-newsgroup-begin number))
4827       (setq gnus-newsgroup-unselected
4828             (delq number gnus-newsgroup-unselected)))
4829     ;; Report back a success?
4830     (and header (mail-header-number header))))
4831
4832 ;;; Process/prefix in the summary buffer
4833
4834 (defun gnus-summary-work-articles (n)
4835   "Return a list of articles to be worked upon.
4836 The prefix argument, the list of process marked articles, and the
4837 current article will be taken into consideration."
4838   (save-excursion
4839     (set-buffer gnus-summary-buffer)
4840     (cond
4841      (n
4842       ;; A numerical prefix has been given.
4843       (setq n (prefix-numeric-value n))
4844       (let ((backward (< n 0))
4845             (n (abs (prefix-numeric-value n)))
4846             articles article)
4847         (save-excursion
4848           (while
4849               (and (> n 0)
4850                    (push (setq article (gnus-summary-article-number))
4851                          articles)
4852                    (if backward
4853                        (gnus-summary-find-prev nil article)
4854                      (gnus-summary-find-next nil article)))
4855             (decf n)))
4856         (nreverse articles)))
4857      ((and (gnus-region-active-p) (mark))
4858       (message "region active")
4859       ;; Work on the region between point and mark.
4860       (let ((max (max (point) (mark)))
4861             articles article)
4862         (save-excursion
4863           (goto-char (min (min (point) (mark))))
4864           (while
4865               (and
4866                (push (setq article (gnus-summary-article-number)) articles)
4867                (gnus-summary-find-next nil article)
4868                (< (point) max)))
4869           (nreverse articles))))
4870      (gnus-newsgroup-processable
4871       ;; There are process-marked articles present.
4872       ;; Save current state.
4873       (gnus-summary-save-process-mark)
4874       ;; Return the list.
4875       (reverse gnus-newsgroup-processable))
4876      (t
4877       ;; Just return the current article.
4878       (list (gnus-summary-article-number))))))
4879
4880 (defmacro gnus-summary-iterate (arg &rest forms)
4881   "Iterate over the process/prefixed articles and do FORMS.
4882 ARG is the interactive prefix given to the command.  FORMS will be
4883 executed with point over the summary line of the articles."
4884   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4885     `(let ((,articles (gnus-summary-work-articles ,arg)))
4886        (while ,articles
4887          (gnus-summary-goto-subject (car ,articles))
4888          ,@forms))))
4889
4890 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4891 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4892
4893 (defun gnus-summary-save-process-mark ()
4894   "Push the current set of process marked articles on the stack."
4895   (interactive)
4896   (push (copy-sequence gnus-newsgroup-processable)
4897         gnus-newsgroup-process-stack))
4898
4899 (defun gnus-summary-kill-process-mark ()
4900   "Push the current set of process marked articles on the stack and unmark."
4901   (interactive)
4902   (gnus-summary-save-process-mark)
4903   (gnus-summary-unmark-all-processable))
4904
4905 (defun gnus-summary-yank-process-mark ()
4906   "Pop the last process mark state off the stack and restore it."
4907   (interactive)
4908   (unless gnus-newsgroup-process-stack
4909     (error "Empty mark stack"))
4910   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4911
4912 (defun gnus-summary-process-mark-set (set)
4913   "Make SET into the current process marked articles."
4914   (gnus-summary-unmark-all-processable)
4915   (while set
4916     (gnus-summary-set-process-mark (pop set))))
4917
4918 ;;; Searching and stuff
4919
4920 (defun gnus-summary-search-group (&optional backward use-level)
4921   "Search for next unread newsgroup.
4922 If optional argument BACKWARD is non-nil, search backward instead."
4923   (save-excursion
4924     (set-buffer gnus-group-buffer)
4925     (when (gnus-group-search-forward
4926            backward nil (if use-level (gnus-group-group-level) nil))
4927       (gnus-group-group-name))))
4928
4929 (defun gnus-summary-best-group (&optional exclude-group)
4930   "Find the name of the best unread group.
4931 If EXCLUDE-GROUP, do not go to this group."
4932   (save-excursion
4933     (set-buffer gnus-group-buffer)
4934     (save-excursion
4935       (gnus-group-best-unread-group exclude-group))))
4936
4937 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4938   (if backward (gnus-summary-find-prev)
4939     (let* ((dummy (gnus-summary-article-intangible-p))
4940            (article (or article (gnus-summary-article-number)))
4941            (arts (gnus-data-find-list article))
4942            result)
4943       (when (and (not dummy)
4944                  (or (not gnus-summary-check-current)
4945                      (not unread)
4946                      (not (gnus-data-unread-p (car arts)))))
4947         (setq arts (cdr arts)))
4948       (when (setq result
4949                   (if unread
4950                       (progn
4951                         (while arts
4952                           (when (or (and undownloaded
4953                                          (eq gnus-undownloaded-mark
4954                                              (gnus-data-mark (car arts))))
4955                                     (gnus-data-unread-p (car arts)))
4956                             (setq result (car arts)
4957                                   arts nil))
4958                           (setq arts (cdr arts)))
4959                         result)
4960                     (car arts)))
4961         (goto-char (gnus-data-pos result))
4962         (gnus-data-number result)))))
4963
4964 (defun gnus-summary-find-prev (&optional unread article)
4965   (let* ((eobp (eobp))
4966          (article (or article (gnus-summary-article-number)))
4967          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4968          result)
4969     (when (and (not eobp)
4970                (or (not gnus-summary-check-current)
4971                    (not unread)
4972                    (not (gnus-data-unread-p (car arts)))))
4973       (setq arts (cdr arts)))
4974     (when (setq result
4975                 (if unread
4976                     (progn
4977                       (while arts
4978                         (when (gnus-data-unread-p (car arts))
4979                           (setq result (car arts)
4980                                 arts nil))
4981                         (setq arts (cdr arts)))
4982                       result)
4983                   (car arts)))
4984       (goto-char (gnus-data-pos result))
4985       (gnus-data-number result))))
4986
4987 (defun gnus-summary-find-subject (subject &optional unread backward article)
4988   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4989          (article (or article (gnus-summary-article-number)))
4990          (articles (gnus-data-list backward))
4991          (arts (gnus-data-find-list article articles))
4992          result)
4993     (when (or (not gnus-summary-check-current)
4994               (not unread)
4995               (not (gnus-data-unread-p (car arts))))
4996       (setq arts (cdr arts)))
4997     (while arts
4998       (and (or (not unread)
4999                (gnus-data-unread-p (car arts)))
5000            (vectorp (gnus-data-header (car arts)))
5001            (gnus-subject-equal
5002             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5003            (setq result (car arts)
5004                  arts nil))
5005       (setq arts (cdr arts)))
5006     (and result
5007          (goto-char (gnus-data-pos result))
5008          (gnus-data-number result))))
5009
5010 (defun gnus-summary-search-forward (&optional unread subject backward)
5011   "Search forward for an article.
5012 If UNREAD, look for unread articles.  If SUBJECT, look for
5013 articles with that subject.  If BACKWARD, search backward instead."
5014   (cond (subject (gnus-summary-find-subject subject unread backward))
5015         (backward (gnus-summary-find-prev unread))
5016         (t (gnus-summary-find-next unread))))
5017
5018 (defun gnus-recenter (&optional n)
5019   "Center point in window and redisplay frame.
5020 Also do horizontal recentering."
5021   (interactive "P")
5022   (when (and gnus-auto-center-summary
5023              (not (eq gnus-auto-center-summary 'vertical)))
5024     (gnus-horizontal-recenter))
5025   (recenter n))
5026
5027 (defun gnus-summary-recenter ()
5028   "Center point in the summary window.
5029 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5030 displayed, no centering will be performed."
5031   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5032   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5033   (let* ((top (cond ((< (window-height) 4) 0)
5034                     ((< (window-height) 7) 1)
5035                     (t (if (numberp gnus-auto-center-summary)
5036                            gnus-auto-center-summary
5037                          2))))
5038          (height (1- (window-height)))
5039          (bottom (save-excursion (goto-char (point-max))
5040                                  (forward-line (- height))
5041                                  (point)))
5042          (window (get-buffer-window (current-buffer))))
5043     ;; The user has to want it.
5044     (when gnus-auto-center-summary
5045       (when (get-buffer-window gnus-article-buffer)
5046         ;; Only do recentering when the article buffer is displayed,
5047         ;; Set the window start to either `bottom', which is the biggest
5048         ;; possible valid number, or the second line from the top,
5049         ;; whichever is the least.
5050         (set-window-start
5051          window (min bottom (save-excursion
5052                               (forward-line (- top)) (point)))))
5053       ;; Do horizontal recentering while we're at it.
5054       (when (and (get-buffer-window (current-buffer) t)
5055                  (not (eq gnus-auto-center-summary 'vertical)))
5056         (let ((selected (selected-window)))
5057           (select-window (get-buffer-window (current-buffer) t))
5058           (gnus-summary-position-point)
5059           (gnus-horizontal-recenter)
5060           (select-window selected))))))
5061
5062 (defun gnus-summary-jump-to-group (newsgroup)
5063   "Move point to NEWSGROUP in group mode buffer."
5064   ;; Keep update point of group mode buffer if visible.
5065   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5066       (save-window-excursion
5067         ;; Take care of tree window mode.
5068         (when (get-buffer-window gnus-group-buffer)
5069           (pop-to-buffer gnus-group-buffer))
5070         (gnus-group-jump-to-group newsgroup))
5071     (save-excursion
5072       ;; Take care of tree window mode.
5073       (if (get-buffer-window gnus-group-buffer)
5074           (pop-to-buffer gnus-group-buffer)
5075         (set-buffer gnus-group-buffer))
5076       (gnus-group-jump-to-group newsgroup))))
5077
5078 ;; This function returns a list of article numbers based on the
5079 ;; difference between the ranges of read articles in this group and
5080 ;; the range of active articles.
5081 (defun gnus-list-of-unread-articles (group)
5082   (let* ((read (gnus-info-read (gnus-get-info group)))
5083          (active (or (gnus-active group) (gnus-activate-group group)))
5084          (last (cdr active))
5085          first nlast unread)
5086     ;; If none are read, then all are unread.
5087     (if (not read)
5088         (setq first (car active))
5089       ;; If the range of read articles is a single range, then the
5090       ;; first unread article is the article after the last read
5091       ;; article.  Sounds logical, doesn't it?
5092       (if (not (listp (cdr read)))
5093           (setq first (max (car active) (1+ (cdr read))))
5094         ;; `read' is a list of ranges.
5095         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5096                                   (caar read)))
5097                   1)
5098           (setq first (car active)))
5099         (while read
5100           (when first
5101             (while (< first nlast)
5102               (push first unread)
5103               (setq first (1+ first))))
5104           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5105           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5106           (setq read (cdr read)))))
5107     ;; And add the last unread articles.
5108     (while (<= first last)
5109       (push first unread)
5110       (setq first (1+ first)))
5111     ;; Return the list of unread articles.
5112     (delq 0 (nreverse unread))))
5113
5114 (defun gnus-list-of-read-articles (group)
5115   "Return a list of unread, unticked and non-dormant articles."
5116   (let* ((info (gnus-get-info group))
5117          (marked (gnus-info-marks info))
5118          (active (gnus-active group)))
5119     (and info active
5120          (gnus-set-difference
5121           (gnus-sorted-complement
5122            (gnus-uncompress-range active)
5123            (gnus-list-of-unread-articles group))
5124           (append
5125            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5126            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5127
5128 ;; Various summary commands
5129
5130 (defun gnus-summary-select-article-buffer ()
5131   "Reconfigure windows to show article buffer."
5132   (interactive)
5133   (if (not (gnus-buffer-live-p gnus-article-buffer))
5134       (error "There is no article buffer for this summary buffer")
5135     (gnus-configure-windows 'article)
5136     (select-window (get-buffer-window gnus-article-buffer))))
5137
5138 (defun gnus-summary-universal-argument (arg)
5139   "Perform any operation on all articles that are process/prefixed."
5140   (interactive "P")
5141   (let ((articles (gnus-summary-work-articles arg))
5142         func article)
5143     (if (eq
5144          (setq
5145           func
5146           (key-binding
5147            (read-key-sequence
5148             (substitute-command-keys
5149              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5150              ))))
5151          'undefined)
5152         (gnus-error 1 "Undefined key")
5153       (save-excursion
5154         (while articles
5155           (gnus-summary-goto-subject (setq article (pop articles)))
5156           (let (gnus-newsgroup-processable)
5157             (command-execute func))
5158           (gnus-summary-remove-process-mark article)))))
5159   (gnus-summary-position-point))
5160
5161 (defun gnus-summary-toggle-truncation (&optional arg)
5162   "Toggle truncation of summary lines.
5163 With arg, turn line truncation on iff arg is positive."
5164   (interactive "P")
5165   (setq truncate-lines
5166         (if (null arg) (not truncate-lines)
5167           (> (prefix-numeric-value arg) 0)))
5168   (redraw-display))
5169
5170 (defun gnus-summary-reselect-current-group (&optional all rescan)
5171   "Exit and then reselect the current newsgroup.
5172 The prefix argument ALL means to select all articles."
5173   (interactive "P")
5174   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5175     (error "Ephemeral groups can't be reselected"))
5176   (let ((current-subject (gnus-summary-article-number))
5177         (group gnus-newsgroup-name))
5178     (setq gnus-newsgroup-begin nil)
5179     (gnus-summary-exit)
5180     ;; We have to adjust the point of group mode buffer because
5181     ;; point was moved to the next unread newsgroup by exiting.
5182     (gnus-summary-jump-to-group group)
5183     (when rescan
5184       (save-excursion
5185         (gnus-group-get-new-news-this-group 1)))
5186     (gnus-group-read-group all t)
5187     (gnus-summary-goto-subject current-subject nil t)))
5188
5189 (defun gnus-summary-rescan-group (&optional all)
5190   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5191   (interactive "P")
5192   (gnus-summary-reselect-current-group all t))
5193
5194 (defun gnus-summary-update-info (&optional non-destructive)
5195   (save-excursion
5196     (let ((group gnus-newsgroup-name))
5197       (when group
5198         (when gnus-newsgroup-kill-headers
5199           (setq gnus-newsgroup-killed
5200                 (gnus-compress-sequence
5201                  (nconc
5202                   (gnus-set-sorted-intersection
5203                    (gnus-uncompress-range gnus-newsgroup-killed)
5204                    (setq gnus-newsgroup-unselected
5205                          (sort gnus-newsgroup-unselected '<)))
5206                   (setq gnus-newsgroup-unreads
5207                         (sort gnus-newsgroup-unreads '<)))
5208                  t)))
5209         (unless (listp (cdr gnus-newsgroup-killed))
5210           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5211         (let ((headers gnus-newsgroup-headers))
5212           ;; Set the new ranges of read articles.
5213           (save-excursion
5214             (set-buffer gnus-group-buffer)
5215             (gnus-undo-force-boundary))
5216           (gnus-update-read-articles
5217            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5218           ;; Set the current article marks.
5219           (let ((gnus-newsgroup-scored
5220                  (if (and (not gnus-save-score)
5221                           (not non-destructive))
5222                      nil
5223                    gnus-newsgroup-scored)))
5224             (save-excursion
5225               (gnus-update-marks)))
5226           ;; Do the cross-ref thing.
5227           (when gnus-use-cross-reference
5228             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5229           ;; Do not switch windows but change the buffer to work.
5230           (set-buffer gnus-group-buffer)
5231           (unless (gnus-ephemeral-group-p group)
5232             (gnus-group-update-group group)))))))
5233
5234 (defun gnus-summary-save-newsrc (&optional force)
5235   "Save the current number of read/marked articles in the dribble buffer.
5236 The dribble buffer will then be saved.
5237 If FORCE (the prefix), also save the .newsrc file(s)."
5238   (interactive "P")
5239   (gnus-summary-update-info t)
5240   (if force
5241       (gnus-save-newsrc-file)
5242     (gnus-dribble-save)))
5243
5244 (defun gnus-summary-exit (&optional temporary)
5245   "Exit reading current newsgroup, and then return to group selection mode.
5246 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5247   (interactive)
5248   (gnus-set-global-variables)
5249   (when (gnus-buffer-live-p gnus-article-buffer)
5250     (save-excursion
5251       (set-buffer gnus-article-buffer)
5252       (mm-destroy-parts gnus-article-mime-handles)))
5253   (gnus-kill-save-kill-buffer)
5254   (gnus-async-halt-prefetch)
5255   (let* ((group gnus-newsgroup-name)
5256          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5257          (mode major-mode)
5258          (group-point nil)
5259          (buf (current-buffer)))
5260     (unless quit-config
5261       ;; Do adaptive scoring, and possibly save score files.
5262       (when gnus-newsgroup-adaptive
5263         (gnus-score-adaptive))
5264       (when gnus-use-scoring
5265         (gnus-score-save)))
5266     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5267     ;; If we have several article buffers, we kill them at exit.
5268     (unless gnus-single-article-buffer
5269       (gnus-kill-buffer gnus-original-article-buffer)
5270       (setq gnus-article-current nil))
5271     (when gnus-use-cache
5272       (gnus-cache-possibly-remove-articles)
5273       (gnus-cache-save-buffers))
5274     (gnus-async-prefetch-remove-group group)
5275     (when gnus-suppress-duplicates
5276       (gnus-dup-enter-articles))
5277     (when gnus-use-trees
5278       (gnus-tree-close group))
5279     ;; Remove entries for this group.
5280     (nnmail-purge-split-history (gnus-group-real-name group))
5281     ;; Make all changes in this group permanent.
5282     (unless quit-config
5283       (gnus-run-hooks 'gnus-exit-group-hook)
5284       (gnus-summary-update-info))
5285     (gnus-close-group group)
5286     ;; Make sure where we were, and go to next newsgroup.
5287     (set-buffer gnus-group-buffer)
5288     (unless quit-config
5289       (gnus-group-jump-to-group group))
5290     (gnus-run-hooks 'gnus-summary-exit-hook)
5291     (unless (or quit-config
5292                 ;; If this group has disappeared from the summary
5293                 ;; buffer, don't skip forwards.
5294                 (not (string= group (gnus-group-group-name))))
5295       (gnus-group-next-unread-group 1))
5296     (setq group-point (point))
5297     (if temporary
5298         nil                             ;Nothing to do.
5299       ;; If we have several article buffers, we kill them at exit.
5300       (unless gnus-single-article-buffer
5301         (gnus-kill-buffer gnus-article-buffer)
5302         (gnus-kill-buffer gnus-original-article-buffer)
5303         (setq gnus-article-current nil))
5304       (set-buffer buf)
5305       (if (not gnus-kill-summary-on-exit)
5306           (gnus-deaden-summary)
5307         ;; We set all buffer-local variables to nil.  It is unclear why
5308         ;; this is needed, but if we don't, buffer-local variables are
5309         ;; not garbage-collected, it seems.  This would the lead to en
5310         ;; ever-growing Emacs.
5311         (gnus-summary-clear-local-variables)
5312         (when (get-buffer gnus-article-buffer)
5313           (bury-buffer gnus-article-buffer))
5314         ;; We clear the global counterparts of the buffer-local
5315         ;; variables as well, just to be on the safe side.
5316         (set-buffer gnus-group-buffer)
5317         (gnus-summary-clear-local-variables)
5318         ;; Return to group mode buffer.
5319         (when (eq mode 'gnus-summary-mode)
5320           (gnus-kill-buffer buf)))
5321       (setq gnus-current-select-method gnus-select-method)
5322       (pop-to-buffer gnus-group-buffer)
5323       (if (not quit-config)
5324           (progn
5325             (goto-char group-point)
5326             (gnus-configure-windows 'group 'force))
5327         (gnus-handle-ephemeral-exit quit-config))
5328       ;; Clear the current group name.
5329       (unless quit-config
5330         (setq gnus-newsgroup-name nil)))))
5331
5332 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5333 (defun gnus-summary-exit-no-update (&optional no-questions)
5334   "Quit reading current newsgroup without updating read article info."
5335   (interactive)
5336   (let* ((group gnus-newsgroup-name)
5337          (quit-config (gnus-group-quit-config group)))
5338     (when (or no-questions
5339               gnus-expert-user
5340               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5341       (gnus-async-halt-prefetch)
5342       (mapcar 'funcall
5343               (delq 'gnus-summary-expire-articles 
5344                     (copy-list gnus-summary-prepare-exit-hook)))
5345       (when (gnus-buffer-live-p gnus-article-buffer)
5346         (save-excursion
5347           (set-buffer gnus-article-buffer)
5348           (mm-destroy-parts gnus-article-mime-handles)))
5349       ;; If we have several article buffers, we kill them at exit.
5350       (unless gnus-single-article-buffer
5351         (gnus-kill-buffer gnus-article-buffer)
5352         (gnus-kill-buffer gnus-original-article-buffer)
5353         (setq gnus-article-current nil))
5354       (if (not gnus-kill-summary-on-exit)
5355           (gnus-deaden-summary)
5356         (gnus-close-group group)
5357         (gnus-summary-clear-local-variables)
5358         (set-buffer gnus-group-buffer)
5359         (gnus-summary-clear-local-variables)
5360         (when (get-buffer gnus-summary-buffer)
5361           (kill-buffer gnus-summary-buffer)))
5362       (unless gnus-single-article-buffer
5363         (setq gnus-article-current nil))
5364       (when gnus-use-trees
5365         (gnus-tree-close group))
5366       (gnus-async-prefetch-remove-group group)
5367       (when (get-buffer gnus-article-buffer)
5368         (bury-buffer gnus-article-buffer))
5369       ;; Return to the group buffer.
5370       (gnus-configure-windows 'group 'force)
5371       ;; Clear the current group name.
5372       (setq gnus-newsgroup-name nil)
5373       (when (equal (gnus-group-group-name) group)
5374         (gnus-group-next-unread-group 1))
5375       (when quit-config
5376         (gnus-handle-ephemeral-exit quit-config)))))
5377
5378 (defun gnus-handle-ephemeral-exit (quit-config)
5379   "Handle movement when leaving an ephemeral group.
5380 The state which existed when entering the ephemeral is reset."
5381   (if (not (buffer-name (car quit-config)))
5382       (gnus-configure-windows 'group 'force)
5383     (set-buffer (car quit-config))
5384     (cond ((eq major-mode 'gnus-summary-mode)
5385            (gnus-set-global-variables))
5386           ((eq major-mode 'gnus-article-mode)
5387            (save-excursion
5388              ;; The `gnus-summary-buffer' variable may point
5389              ;; to the old summary buffer when using a single
5390              ;; article buffer.
5391              (unless (gnus-buffer-live-p gnus-summary-buffer)
5392                (set-buffer gnus-group-buffer))
5393              (set-buffer gnus-summary-buffer)
5394              (gnus-set-global-variables))))
5395     (if (or (eq (cdr quit-config) 'article)
5396             (eq (cdr quit-config) 'pick))
5397         (progn
5398           ;; The current article may be from the ephemeral group
5399           ;; thus it is best that we reload this article
5400           (gnus-summary-show-article)
5401           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5402               (gnus-configure-windows 'pick 'force)
5403             (gnus-configure-windows (cdr quit-config) 'force)))
5404       (gnus-configure-windows (cdr quit-config) 'force))
5405     (when (eq major-mode 'gnus-summary-mode)
5406       (gnus-summary-next-subject 1 nil t)
5407       (gnus-summary-recenter)
5408       (gnus-summary-position-point))))
5409
5410 ;;; Dead summaries.
5411
5412 (defvar gnus-dead-summary-mode-map nil)
5413
5414 (unless gnus-dead-summary-mode-map
5415   (setq gnus-dead-summary-mode-map (make-keymap))
5416   (suppress-keymap gnus-dead-summary-mode-map)
5417   (substitute-key-definition
5418    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5419   (let ((keys '("\C-d" "\r" "\177" [delete])))
5420     (while keys
5421       (define-key gnus-dead-summary-mode-map
5422         (pop keys) 'gnus-summary-wake-up-the-dead))))
5423
5424 (defvar gnus-dead-summary-mode nil
5425   "Minor mode for Gnus summary buffers.")
5426
5427 (defun gnus-dead-summary-mode (&optional arg)
5428   "Minor mode for Gnus summary buffers."
5429   (interactive "P")
5430   (when (eq major-mode 'gnus-summary-mode)
5431     (make-local-variable 'gnus-dead-summary-mode)
5432     (setq gnus-dead-summary-mode
5433           (if (null arg) (not gnus-dead-summary-mode)
5434             (> (prefix-numeric-value arg) 0)))
5435     (when gnus-dead-summary-mode
5436       (gnus-add-minor-mode
5437        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5438
5439 (defun gnus-deaden-summary ()
5440   "Make the current summary buffer into a dead summary buffer."
5441   ;; Kill any previous dead summary buffer.
5442   (when (and gnus-dead-summary
5443              (buffer-name gnus-dead-summary))
5444     (save-excursion
5445       (set-buffer gnus-dead-summary)
5446       (when gnus-dead-summary-mode
5447         (kill-buffer (current-buffer)))))
5448   ;; Make this the current dead summary.
5449   (setq gnus-dead-summary (current-buffer))
5450   (gnus-dead-summary-mode 1)
5451   (let ((name (buffer-name)))
5452     (when (string-match "Summary" name)
5453       (rename-buffer
5454        (concat (substring name 0 (match-beginning 0)) "Dead "
5455                (substring name (match-beginning 0)))
5456        t))))
5457
5458 (defun gnus-kill-or-deaden-summary (buffer)
5459   "Kill or deaden the summary BUFFER."
5460   (save-excursion
5461     (when (and (buffer-name buffer)
5462                (not gnus-single-article-buffer))
5463       (save-excursion
5464         (set-buffer buffer)
5465         (gnus-kill-buffer gnus-article-buffer)
5466         (gnus-kill-buffer gnus-original-article-buffer)))
5467     (cond (gnus-kill-summary-on-exit
5468            (when (and gnus-use-trees
5469                       (gnus-buffer-exists-p buffer))
5470              (save-excursion
5471                (set-buffer buffer)
5472                (gnus-tree-close gnus-newsgroup-name)))
5473            (gnus-kill-buffer buffer))
5474           ((gnus-buffer-exists-p buffer)
5475            (save-excursion
5476              (set-buffer buffer)
5477              (gnus-deaden-summary))))))
5478
5479 (defun gnus-summary-wake-up-the-dead (&rest args)
5480   "Wake up the dead summary buffer."
5481   (interactive)
5482   (gnus-dead-summary-mode -1)
5483   (let ((name (buffer-name)))
5484     (when (string-match "Dead " name)
5485       (rename-buffer
5486        (concat (substring name 0 (match-beginning 0))
5487                (substring name (match-end 0)))
5488        t)))
5489   (gnus-message 3 "This dead summary is now alive again"))
5490
5491 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5492 (defun gnus-summary-fetch-faq (&optional faq-dir)
5493   "Fetch the FAQ for the current group.
5494 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5495 in."
5496   (interactive
5497    (list
5498     (when current-prefix-arg
5499       (completing-read
5500        "Faq dir: " (and (listp gnus-group-faq-directory)
5501                         (mapcar (lambda (file) (list file))
5502                                 gnus-group-faq-directory))))))
5503   (let (gnus-faq-buffer)
5504     (when (setq gnus-faq-buffer
5505                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5506       (gnus-configure-windows 'summary-faq))))
5507
5508 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5509 (defun gnus-summary-describe-group (&optional force)
5510   "Describe the current newsgroup."
5511   (interactive "P")
5512   (gnus-group-describe-group force gnus-newsgroup-name))
5513
5514 (defun gnus-summary-describe-briefly ()
5515   "Describe summary mode commands briefly."
5516   (interactive)
5517   (gnus-message 6
5518                 (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")))
5519
5520 ;; Walking around group mode buffer from summary mode.
5521
5522 (defun gnus-summary-next-group (&optional no-article target-group backward)
5523   "Exit current newsgroup and then select next unread newsgroup.
5524 If prefix argument NO-ARTICLE is non-nil, no article is selected
5525 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5526 previous group instead."
5527   (interactive "P")
5528   ;; Stop pre-fetching.
5529   (gnus-async-halt-prefetch)
5530   (let ((current-group gnus-newsgroup-name)
5531         (current-buffer (current-buffer))
5532         entered)
5533     ;; First we semi-exit this group to update Xrefs and all variables.
5534     ;; We can't do a real exit, because the window conf must remain
5535     ;; the same in case the user is prompted for info, and we don't
5536     ;; want the window conf to change before that...
5537     (gnus-summary-exit t)
5538     (while (not entered)
5539       ;; Then we find what group we are supposed to enter.
5540       (set-buffer gnus-group-buffer)
5541       (gnus-group-jump-to-group current-group)
5542       (setq target-group
5543             (or target-group
5544                 (if (eq gnus-keep-same-level 'best)
5545                     (gnus-summary-best-group gnus-newsgroup-name)
5546                   (gnus-summary-search-group backward gnus-keep-same-level))))
5547       (if (not target-group)
5548           ;; There are no further groups, so we return to the group
5549           ;; buffer.
5550           (progn
5551             (gnus-message 5 "Returning to the group buffer")
5552             (setq entered t)
5553             (when (gnus-buffer-live-p current-buffer)
5554               (set-buffer current-buffer)
5555               (gnus-summary-exit))
5556             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5557         ;; We try to enter the target group.
5558         (gnus-group-jump-to-group target-group)
5559         (let ((unreads (gnus-group-group-unread)))
5560           (if (and (or (eq t unreads)
5561                        (and unreads (not (zerop unreads))))
5562                    (gnus-summary-read-group
5563                     target-group nil no-article
5564                     (and (buffer-name current-buffer) current-buffer)
5565                     nil backward))
5566               (setq entered t)
5567             (setq current-group target-group
5568                   target-group nil)))))))
5569
5570 (defun gnus-summary-prev-group (&optional no-article)
5571   "Exit current newsgroup and then select previous unread newsgroup.
5572 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5573   (interactive "P")
5574   (gnus-summary-next-group no-article nil t))
5575
5576 ;; Walking around summary lines.
5577
5578 (defun gnus-summary-first-subject (&optional unread undownloaded)
5579   "Go to the first unread subject.
5580 If UNREAD is non-nil, go to the first unread article.
5581 Returns the article selected or nil if there are no unread articles."
5582   (interactive "P")
5583   (prog1
5584       (cond
5585        ;; Empty summary.
5586        ((null gnus-newsgroup-data)
5587         (gnus-message 3 "No articles in the group")
5588         nil)
5589        ;; Pick the first article.
5590        ((not unread)
5591         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5592         (gnus-data-number (car gnus-newsgroup-data)))
5593        ;; No unread articles.
5594        ((null gnus-newsgroup-unreads)
5595         (gnus-message 3 "No more unread articles")
5596         nil)
5597        ;; Find the first unread article.
5598        (t
5599         (let ((data gnus-newsgroup-data))
5600           (while (and data
5601                       (and (not (and undownloaded
5602                                      (eq gnus-undownloaded-mark
5603                                          (gnus-data-mark (car data)))))
5604                            (not (gnus-data-unread-p (car data)))))
5605             (setq data (cdr data)))
5606           (when data
5607             (goto-char (gnus-data-pos (car data)))
5608             (gnus-data-number (car data))))))
5609     (gnus-summary-position-point)))
5610
5611 (defun gnus-summary-next-subject (n &optional unread dont-display)
5612   "Go to next N'th summary line.
5613 If N is negative, go to the previous N'th subject line.
5614 If UNREAD is non-nil, only unread articles are selected.
5615 The difference between N and the actual number of steps taken is
5616 returned."
5617   (interactive "p")
5618   (let ((backward (< n 0))
5619         (n (abs n)))
5620     (while (and (> n 0)
5621                 (if backward
5622                     (gnus-summary-find-prev unread)
5623                   (gnus-summary-find-next unread)))
5624       (gnus-summary-show-thread)
5625       (setq n (1- n)))
5626     (when (/= 0 n)
5627       (gnus-message 7 "No more%s articles"
5628                     (if unread " unread" "")))
5629     (unless dont-display
5630       (gnus-summary-recenter)
5631       (gnus-summary-position-point))
5632     n))
5633
5634 (defun gnus-summary-next-unread-subject (n)
5635   "Go to next N'th unread summary line."
5636   (interactive "p")
5637   (gnus-summary-next-subject n t))
5638
5639 (defun gnus-summary-prev-subject (n &optional unread)
5640   "Go to previous N'th summary line.
5641 If optional argument UNREAD is non-nil, only unread article is selected."
5642   (interactive "p")
5643   (gnus-summary-next-subject (- n) unread))
5644
5645 (defun gnus-summary-prev-unread-subject (n)
5646   "Go to previous N'th unread summary line."
5647   (interactive "p")
5648   (gnus-summary-next-subject (- n) t))
5649
5650 (defun gnus-summary-goto-subject (article &optional force silent)
5651   "Go the subject line of ARTICLE.
5652 If FORCE, also allow jumping to articles not currently shown."
5653   (interactive "nArticle number: ")
5654   (let ((b (point))
5655         (data (gnus-data-find article)))
5656     ;; We read in the article if we have to.
5657     (and (not data)
5658          force
5659          (gnus-summary-insert-subject
5660           article
5661           (if (or (numberp force) (vectorp force)) force)
5662           t)
5663          (setq data (gnus-data-find article)))
5664     (goto-char b)
5665     (if (not data)
5666         (progn
5667           (unless silent
5668             (gnus-message 3 "Can't find article %d" article))
5669           nil)
5670       (goto-char (gnus-data-pos data))
5671       (gnus-summary-position-point)
5672       article)))
5673
5674 ;; Walking around summary lines with displaying articles.
5675
5676 (defun gnus-summary-expand-window (&optional arg)
5677   "Make the summary buffer take up the entire Emacs frame.
5678 Given a prefix, will force an `article' buffer configuration."
5679   (interactive "P")
5680   (if arg
5681       (gnus-configure-windows 'article 'force)
5682     (gnus-configure-windows 'summary 'force)))
5683
5684 (defun gnus-summary-display-article (article &optional all-header)
5685   "Display ARTICLE in article buffer."
5686   (gnus-set-global-variables)
5687   (if (null article)
5688       nil
5689     (prog1
5690         (if gnus-summary-display-article-function
5691             (funcall gnus-summary-display-article-function article all-header)
5692           (gnus-article-prepare article all-header))
5693       (gnus-run-hooks 'gnus-select-article-hook)
5694       (when (and gnus-current-article
5695                  (not (zerop gnus-current-article)))
5696         (gnus-summary-goto-subject gnus-current-article))
5697       (gnus-summary-recenter)
5698       (when (and gnus-use-trees gnus-show-threads)
5699         (gnus-possibly-generate-tree article)
5700         (gnus-highlight-selected-tree article))
5701       ;; Successfully display article.
5702       (gnus-article-set-window-start
5703        (cdr (assq article gnus-newsgroup-bookmarks))))))
5704
5705 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5706   "Select the current article.
5707 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5708 non-nil, the article will be re-fetched even if it already present in
5709 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5710 be displayed."
5711   ;; Make sure we are in the summary buffer to work around bbdb bug.
5712   (unless (eq major-mode 'gnus-summary-mode)
5713     (set-buffer gnus-summary-buffer))
5714   (let ((article (or article (gnus-summary-article-number)))
5715         (all-headers (not (not all-headers))) ;Must be T or NIL.
5716         gnus-summary-display-article-function
5717         did)
5718     (and (not pseudo)
5719          (gnus-summary-article-pseudo-p article)
5720          (error "This is a pseudo-article"))
5721     (prog1
5722         (save-excursion
5723           (set-buffer gnus-summary-buffer)
5724           (if (or (and gnus-single-article-buffer
5725                        (or (null gnus-current-article)
5726                            (null gnus-article-current)
5727                            (null (get-buffer gnus-article-buffer))
5728                            (not (eq article (cdr gnus-article-current)))
5729                            (not (equal (car gnus-article-current)
5730                                        gnus-newsgroup-name))))
5731                   (and (not gnus-single-article-buffer)
5732                        (or (null gnus-current-article)
5733                            (not (eq gnus-current-article article))))
5734                   force)
5735               ;; The requested article is different from the current article.
5736               (prog1
5737                   (gnus-summary-display-article article all-headers)
5738                 (setq did article))
5739             (when (or all-headers gnus-show-all-headers)
5740               (gnus-article-show-all-headers))
5741             'old))
5742       (when did
5743         (gnus-article-set-window-start
5744          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5745
5746 (defun gnus-summary-set-current-mark (&optional current-mark)
5747   "Obsolete function."
5748   nil)
5749
5750 (defun gnus-summary-next-article (&optional unread subject backward push)
5751   "Select the next article.
5752 If UNREAD, only unread articles are selected.
5753 If SUBJECT, only articles with SUBJECT are selected.
5754 If BACKWARD, the previous article is selected instead of the next."
5755   (interactive "P")
5756   (cond
5757    ;; Is there such an article?
5758    ((and (gnus-summary-search-forward unread subject backward)
5759          (or (gnus-summary-display-article (gnus-summary-article-number))
5760              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5761     (gnus-summary-position-point))
5762    ;; If not, we try the first unread, if that is wanted.
5763    ((and subject
5764          gnus-auto-select-same
5765          (gnus-summary-first-unread-article))
5766     (gnus-summary-position-point)
5767     (gnus-message 6 "Wrapped"))
5768    ;; Try to get next/previous article not displayed in this group.
5769    ((and gnus-auto-extend-newsgroup
5770          (not unread) (not subject))
5771     (gnus-summary-goto-article
5772      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5773      nil (count-lines (point-min) (point))))
5774    ;; Go to next/previous group.
5775    (t
5776     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5777       (gnus-summary-jump-to-group gnus-newsgroup-name))
5778     (let ((cmd last-command-char)
5779           (point
5780            (save-excursion
5781              (set-buffer gnus-group-buffer)
5782              (point)))
5783           (group
5784            (if (eq gnus-keep-same-level 'best)
5785                (gnus-summary-best-group gnus-newsgroup-name)
5786              (gnus-summary-search-group backward gnus-keep-same-level))))
5787       ;; For some reason, the group window gets selected.  We change
5788       ;; it back.
5789       (select-window (get-buffer-window (current-buffer)))
5790       ;; Select next unread newsgroup automagically.
5791       (cond
5792        ((or (not gnus-auto-select-next)
5793             (not cmd))
5794         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5795        ((or (eq gnus-auto-select-next 'quietly)
5796             (and (eq gnus-auto-select-next 'slightly-quietly)
5797                  push)
5798             (and (eq gnus-auto-select-next 'almost-quietly)
5799                  (gnus-summary-last-article-p)))
5800         ;; Select quietly.
5801         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5802             (gnus-summary-exit)
5803           (gnus-message 7 "No more%s articles (%s)..."
5804                         (if unread " unread" "")
5805                         (if group (concat "selecting " group)
5806                           "exiting"))
5807           (gnus-summary-next-group nil group backward)))
5808        (t
5809         (when (gnus-key-press-event-p last-input-event)
5810           (gnus-summary-walk-group-buffer
5811            gnus-newsgroup-name cmd unread backward point))))))))
5812
5813 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5814   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5815                       (?\C-p (gnus-group-prev-unread-group 1))))
5816         (cursor-in-echo-area t)
5817         keve key group ended)
5818     (save-excursion
5819       (set-buffer gnus-group-buffer)
5820       (goto-char start)
5821       (setq group
5822             (if (eq gnus-keep-same-level 'best)
5823                 (gnus-summary-best-group gnus-newsgroup-name)
5824               (gnus-summary-search-group backward gnus-keep-same-level))))
5825     (while (not ended)
5826       (gnus-message
5827        5 "No more%s articles%s" (if unread " unread" "")
5828        (if (and group
5829                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5830            (format " (Type %s for %s [%s])"
5831                    (single-key-description cmd) group
5832                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5833          (format " (Type %s to exit %s)"
5834                  (single-key-description cmd)
5835                  gnus-newsgroup-name)))
5836       ;; Confirm auto selection.
5837       (setq key (car (setq keve (gnus-read-event-char))))
5838       (setq ended t)
5839       (cond
5840        ((assq key keystrokes)
5841         (let ((obuf (current-buffer)))
5842           (switch-to-buffer gnus-group-buffer)
5843           (when group
5844             (gnus-group-jump-to-group group))
5845           (eval (cadr (assq key keystrokes)))
5846           (setq group (gnus-group-group-name))
5847           (switch-to-buffer obuf))
5848         (setq ended nil))
5849        ((equal key cmd)
5850         (if (or (not group)
5851                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5852             (gnus-summary-exit)
5853           (gnus-summary-next-group nil group backward)))
5854        (t
5855         (push (cdr keve) unread-command-events))))))
5856
5857 (defun gnus-summary-next-unread-article ()
5858   "Select unread article after current one."
5859   (interactive)
5860   (gnus-summary-next-article
5861    (or (not (eq gnus-summary-goto-unread 'never))
5862        (gnus-summary-last-article-p (gnus-summary-article-number)))
5863    (and gnus-auto-select-same
5864         (gnus-summary-article-subject))))
5865
5866 (defun gnus-summary-prev-article (&optional unread subject)
5867   "Select the article after the current one.
5868 If UNREAD is non-nil, only unread articles are selected."
5869   (interactive "P")
5870   (gnus-summary-next-article unread subject t))
5871
5872 (defun gnus-summary-prev-unread-article ()
5873   "Select unread article before current one."
5874   (interactive)
5875   (gnus-summary-prev-article
5876    (or (not (eq gnus-summary-goto-unread 'never))
5877        (gnus-summary-first-article-p (gnus-summary-article-number)))
5878    (and gnus-auto-select-same
5879         (gnus-summary-article-subject))))
5880
5881 (defun gnus-summary-next-page (&optional lines circular)
5882   "Show next page of the selected article.
5883 If at the end of the current article, select the next article.
5884 LINES says how many lines should be scrolled up.
5885
5886 If CIRCULAR is non-nil, go to the start of the article instead of
5887 selecting the next article when reaching the end of the current
5888 article."
5889   (interactive "P")
5890   (setq gnus-summary-buffer (current-buffer))
5891   (gnus-set-global-variables)
5892   (let ((article (gnus-summary-article-number))
5893         (article-window (get-buffer-window gnus-article-buffer t))
5894         endp)
5895     ;; If the buffer is empty, we have no article.
5896     (unless article
5897       (error "No article to select"))
5898     (gnus-configure-windows 'article)
5899     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5900         (if (and (eq gnus-summary-goto-unread 'never)
5901                  (not (gnus-summary-last-article-p article)))
5902             (gnus-summary-next-article)
5903           (gnus-summary-next-unread-article))
5904       (if (or (null gnus-current-article)
5905               (null gnus-article-current)
5906               (/= article (cdr gnus-article-current))
5907               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5908           ;; Selected subject is different from current article's.
5909           (gnus-summary-display-article article)
5910         (when article-window
5911           (gnus-eval-in-buffer-window gnus-article-buffer
5912             (setq endp (gnus-article-next-page lines)))
5913           (when endp
5914             (cond (circular
5915                    (gnus-summary-beginning-of-article))
5916                   (lines
5917                    (gnus-message 3 "End of message"))
5918                   ((null lines)
5919                    (if (and (eq gnus-summary-goto-unread 'never)
5920                             (not (gnus-summary-last-article-p article)))
5921                        (gnus-summary-next-article)
5922                      (gnus-summary-next-unread-article))))))))
5923     (gnus-summary-recenter)
5924     (gnus-summary-position-point)))
5925
5926 (defun gnus-summary-prev-page (&optional lines move)
5927   "Show previous page of selected article.
5928 Argument LINES specifies lines to be scrolled down.
5929 If MOVE, move to the previous unread article if point is at
5930 the beginning of the buffer."
5931   (interactive "P")
5932   (let ((article (gnus-summary-article-number))
5933         (article-window (get-buffer-window gnus-article-buffer t))
5934         endp)
5935     (gnus-configure-windows 'article)
5936     (if (or (null gnus-current-article)
5937             (null gnus-article-current)
5938             (/= article (cdr gnus-article-current))
5939             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5940         ;; Selected subject is different from current article's.
5941         (gnus-summary-display-article article)
5942       (gnus-summary-recenter)
5943       (when article-window
5944         (gnus-eval-in-buffer-window gnus-article-buffer
5945           (setq endp (gnus-article-prev-page lines)))
5946         (when (and move endp)
5947           (cond (lines
5948                  (gnus-message 3 "Beginning of message"))
5949                 ((null lines)
5950                  (if (and (eq gnus-summary-goto-unread 'never)
5951                           (not (gnus-summary-first-article-p article)))
5952                      (gnus-summary-prev-article)
5953                    (gnus-summary-prev-unread-article))))))))
5954   (gnus-summary-position-point))
5955
5956 (defun gnus-summary-prev-page-or-article (&optional lines)
5957   "Show previous page of selected article.
5958 Argument LINES specifies lines to be scrolled down.
5959 If at the beginning of the article, go to the next article."
5960   (interactive "P")
5961   (gnus-summary-prev-page lines t))
5962
5963 (defun gnus-summary-scroll-up (lines)
5964   "Scroll up (or down) one line current article.
5965 Argument LINES specifies lines to be scrolled up (or down if negative)."
5966   (interactive "p")
5967   (gnus-configure-windows 'article)
5968   (gnus-summary-show-thread)
5969   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5970     (gnus-eval-in-buffer-window gnus-article-buffer
5971       (cond ((> lines 0)
5972              (when (gnus-article-next-page lines)
5973                (gnus-message 3 "End of message")))
5974             ((< lines 0)
5975              (gnus-article-prev-page (- lines))))))
5976   (gnus-summary-recenter)
5977   (gnus-summary-position-point))
5978
5979 (defun gnus-summary-scroll-down (lines)
5980   "Scroll down (or up) one line current article.
5981 Argument LINES specifies lines to be scrolled down (or up if negative)."
5982   (interactive "p")
5983   (gnus-summary-scroll-up (- lines)))
5984
5985 (defun gnus-summary-next-same-subject ()
5986   "Select next article which has the same subject as current one."
5987   (interactive)
5988   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5989
5990 (defun gnus-summary-prev-same-subject ()
5991   "Select previous article which has the same subject as current one."
5992   (interactive)
5993   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5994
5995 (defun gnus-summary-next-unread-same-subject ()
5996   "Select next unread article which has the same subject as current one."
5997   (interactive)
5998   (gnus-summary-next-article t (gnus-summary-article-subject)))
5999
6000 (defun gnus-summary-prev-unread-same-subject ()
6001   "Select previous unread article which has the same subject as current one."
6002   (interactive)
6003   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6004
6005 (defun gnus-summary-first-unread-article ()
6006   "Select the first unread article.
6007 Return nil if there are no unread articles."
6008   (interactive)
6009   (prog1
6010       (when (gnus-summary-first-subject t)
6011         (gnus-summary-show-thread)
6012         (gnus-summary-first-subject t)
6013         (gnus-summary-display-article (gnus-summary-article-number)))
6014     (gnus-summary-position-point)))
6015
6016 (defun gnus-summary-first-unread-subject ()
6017   "Place the point on the subject line of the first unread article.
6018 Return nil if there are no unread articles."
6019   (interactive)
6020   (prog1
6021       (when (gnus-summary-first-subject t)
6022         (gnus-summary-show-thread)
6023         (gnus-summary-first-subject t))
6024     (gnus-summary-position-point)))
6025
6026 (defun gnus-summary-first-article ()
6027   "Select the first article.
6028 Return nil if there are no articles."
6029   (interactive)
6030   (prog1
6031       (when (gnus-summary-first-subject)
6032         (gnus-summary-show-thread)
6033         (gnus-summary-first-subject)
6034         (gnus-summary-display-article (gnus-summary-article-number)))
6035     (gnus-summary-position-point)))
6036
6037 (defun gnus-summary-best-unread-article ()
6038   "Select the unread article with the highest score."
6039   (interactive)
6040   (let ((best -1000000)
6041         (data gnus-newsgroup-data)
6042         article score)
6043     (while data
6044       (and (gnus-data-unread-p (car data))
6045            (> (setq score
6046                     (gnus-summary-article-score (gnus-data-number (car data))))
6047               best)
6048            (setq best score
6049                  article (gnus-data-number (car data))))
6050       (setq data (cdr data)))
6051     (prog1
6052         (if article
6053             (gnus-summary-goto-article article)
6054           (error "No unread articles"))
6055       (gnus-summary-position-point))))
6056
6057 (defun gnus-summary-last-subject ()
6058   "Go to the last displayed subject line in the group."
6059   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6060     (when article
6061       (gnus-summary-goto-subject article))))
6062
6063 (defun gnus-summary-goto-article (article &optional all-headers force)
6064   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6065 If ALL-HEADERS is non-nil, no header lines are hidden.
6066 If FORCE, go to the article even if it isn't displayed.  If FORCE
6067 is a number, it is the line the article is to be displayed on."
6068   (interactive
6069    (list
6070     (completing-read
6071      "Article number or Message-ID: "
6072      (mapcar (lambda (number) (list (int-to-string number)))
6073              gnus-newsgroup-limit))
6074     current-prefix-arg
6075     t))
6076   (prog1
6077       (if (and (stringp article)
6078                (string-match "@" article))
6079           (gnus-summary-refer-article article)
6080         (when (stringp article)
6081           (setq article (string-to-number article)))
6082         (if (gnus-summary-goto-subject article force)
6083             (gnus-summary-display-article article all-headers)
6084           (gnus-message 4 "Couldn't go to article %s" article) nil))
6085     (gnus-summary-position-point)))
6086
6087 (defun gnus-summary-goto-last-article ()
6088   "Go to the previously read article."
6089   (interactive)
6090   (prog1
6091       (when gnus-last-article
6092         (gnus-summary-goto-article gnus-last-article nil t))
6093     (gnus-summary-position-point)))
6094
6095 (defun gnus-summary-pop-article (number)
6096   "Pop one article off the history and go to the previous.
6097 NUMBER articles will be popped off."
6098   (interactive "p")
6099   (let (to)
6100     (setq gnus-newsgroup-history
6101           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6102     (if to
6103         (gnus-summary-goto-article (car to) nil t)
6104       (error "Article history empty")))
6105   (gnus-summary-position-point))
6106
6107 ;; Summary commands and functions for limiting the summary buffer.
6108
6109 (defun gnus-summary-limit-to-articles (n)
6110   "Limit the summary buffer to the next N articles.
6111 If not given a prefix, use the process marked articles instead."
6112   (interactive "P")
6113   (prog1
6114       (let ((articles (gnus-summary-work-articles n)))
6115         (setq gnus-newsgroup-processable nil)
6116         (gnus-summary-limit articles))
6117     (gnus-summary-position-point)))
6118
6119 (defun gnus-summary-pop-limit (&optional total)
6120   "Restore the previous limit.
6121 If given a prefix, remove all limits."
6122   (interactive "P")
6123   (when total
6124     (setq gnus-newsgroup-limits
6125           (list (mapcar (lambda (h) (mail-header-number h))
6126                         gnus-newsgroup-headers))))
6127   (unless gnus-newsgroup-limits
6128     (error "No limit to pop"))
6129   (prog1
6130       (gnus-summary-limit nil 'pop)
6131     (gnus-summary-position-point)))
6132
6133 (defun gnus-summary-limit-to-subject (subject &optional header)
6134   "Limit the summary buffer to articles that have subjects that match a regexp."
6135   (interactive "sLimit to subject (regexp): ")
6136   (unless header
6137     (setq header "subject"))
6138   (when (not (equal "" subject))
6139     (prog1
6140         (let ((articles (gnus-summary-find-matching
6141                          (or header "subject") subject 'all)))
6142           (unless articles
6143             (error "Found no matches for \"%s\"" subject))
6144           (gnus-summary-limit articles))
6145       (gnus-summary-position-point))))
6146
6147 (defun gnus-summary-limit-to-author (from)
6148   "Limit the summary buffer to articles that have authors that match a regexp."
6149   (interactive "sLimit to author (regexp): ")
6150   (gnus-summary-limit-to-subject from "from"))
6151
6152 (defun gnus-summary-limit-to-age (age &optional younger-p)
6153   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6154 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6155 articles that are younger than AGE days."
6156   (interactive "nTime in days: \nP")
6157   (prog1
6158       (let ((data gnus-newsgroup-data)
6159             (cutoff (days-to-time age))
6160             articles d date is-younger)
6161         (while (setq d (pop data))
6162           (when (and (vectorp (gnus-data-header d))
6163                      (setq date (mail-header-date (gnus-data-header d))))
6164             (setq is-younger (time-less-p
6165                               (time-since (date-to-time date))
6166                               cutoff))
6167             (when (if younger-p
6168                       is-younger
6169                     (not is-younger))
6170               (push (gnus-data-number d) articles))))
6171         (gnus-summary-limit (nreverse articles)))
6172     (gnus-summary-position-point)))
6173
6174 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6175 (make-obsolete
6176  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6177
6178 (defun gnus-summary-limit-to-unread (&optional all)
6179   "Limit the summary buffer to articles that are not marked as read.
6180 If ALL is non-nil, limit strictly to unread articles."
6181   (interactive "P")
6182   (if all
6183       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6184     (gnus-summary-limit-to-marks
6185      ;; Concat all the marks that say that an article is read and have
6186      ;; those removed.
6187      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6188            gnus-killed-mark gnus-kill-file-mark
6189            gnus-low-score-mark gnus-expirable-mark
6190            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6191            gnus-duplicate-mark gnus-souped-mark)
6192      'reverse)))
6193
6194 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6195 (make-obsolete 'gnus-summary-delete-marked-with
6196                'gnus-summary-limit-exlude-marks)
6197
6198 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6199   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6200 If REVERSE, limit the summary buffer to articles that are marked
6201 with MARKS.  MARKS can either be a string of marks or a list of marks.
6202 Returns how many articles were removed."
6203   (interactive "sMarks: ")
6204   (gnus-summary-limit-to-marks marks t))
6205
6206 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6207   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6208 If REVERSE (the prefix), limit the summary buffer to articles that are
6209 not marked with MARKS.  MARKS can either be a string of marks or a
6210 list of marks.
6211 Returns how many articles were removed."
6212   (interactive "sMarks: \nP")
6213   (prog1
6214       (let ((data gnus-newsgroup-data)
6215             (marks (if (listp marks) marks
6216                      (append marks nil))) ; Transform to list.
6217             articles)
6218         (while data
6219           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6220                   (memq (gnus-data-mark (car data)) marks))
6221             (push (gnus-data-number (car data)) articles))
6222           (setq data (cdr data)))
6223         (gnus-summary-limit articles))
6224     (gnus-summary-position-point)))
6225
6226 (defun gnus-summary-limit-to-score (&optional score)
6227   "Limit to articles with score at or above SCORE."
6228   (interactive "P")
6229   (setq score (if score
6230                   (prefix-numeric-value score)
6231                 (or gnus-summary-default-score 0)))
6232   (let ((data gnus-newsgroup-data)
6233         articles)
6234     (while data
6235       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6236                 score)
6237         (push (gnus-data-number (car data)) articles))
6238       (setq data (cdr data)))
6239     (prog1
6240         (gnus-summary-limit articles)
6241       (gnus-summary-position-point))))
6242
6243 (defun gnus-summary-limit-include-thread (id)
6244   "Display all the hidden articles that in the current thread."
6245   (interactive (list (mail-header-id (gnus-summary-article-header))))
6246   (let ((articles (gnus-articles-in-thread
6247                    (gnus-id-to-thread (gnus-root-id id)))))
6248     (prog1
6249         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6250       (gnus-summary-position-point))))
6251
6252 (defun gnus-summary-limit-include-dormant ()
6253   "Display all the hidden articles that are marked as dormant.
6254 Note that this command only works on a subset of the articles currently
6255 fetched for this group."
6256   (interactive)
6257   (unless gnus-newsgroup-dormant
6258     (error "There are no dormant articles in this group"))
6259   (prog1
6260       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6261     (gnus-summary-position-point)))
6262
6263 (defun gnus-summary-limit-exclude-dormant ()
6264   "Hide all dormant articles."
6265   (interactive)
6266   (prog1
6267       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6268     (gnus-summary-position-point)))
6269
6270 (defun gnus-summary-limit-exclude-childless-dormant ()
6271   "Hide all dormant articles that have no children."
6272   (interactive)
6273   (let ((data (gnus-data-list t))
6274         articles d children)
6275     ;; Find all articles that are either not dormant or have
6276     ;; children.
6277     (while (setq d (pop data))
6278       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6279                 (and (setq children
6280                            (gnus-article-children (gnus-data-number d)))
6281                      (let (found)
6282                        (while children
6283                          (when (memq (car children) articles)
6284                            (setq children nil
6285                                  found t))
6286                          (pop children))
6287                        found)))
6288         (push (gnus-data-number d) articles)))
6289     ;; Do the limiting.
6290     (prog1
6291         (gnus-summary-limit articles)
6292       (gnus-summary-position-point))))
6293
6294 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6295   "Mark all unread excluded articles as read.
6296 If ALL, mark even excluded ticked and dormants as read."
6297   (interactive "P")
6298   (let ((articles (gnus-sorted-complement
6299                    (sort
6300                     (mapcar (lambda (h) (mail-header-number h))
6301                             gnus-newsgroup-headers)
6302                     '<)
6303                    (sort gnus-newsgroup-limit '<)))
6304         article)
6305     (setq gnus-newsgroup-unreads
6306           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6307     (if all
6308         (setq gnus-newsgroup-dormant nil
6309               gnus-newsgroup-marked nil
6310               gnus-newsgroup-reads
6311               (nconc
6312                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6313                gnus-newsgroup-reads))
6314       (while (setq article (pop articles))
6315         (unless (or (memq article gnus-newsgroup-dormant)
6316                     (memq article gnus-newsgroup-marked))
6317           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6318
6319 (defun gnus-summary-limit (articles &optional pop)
6320   (if pop
6321       ;; We pop the previous limit off the stack and use that.
6322       (setq articles (car gnus-newsgroup-limits)
6323             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6324     ;; We use the new limit, so we push the old limit on the stack.
6325     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6326   ;; Set the limit.
6327   (setq gnus-newsgroup-limit articles)
6328   (let ((total (length gnus-newsgroup-data))
6329         (data (gnus-data-find-list (gnus-summary-article-number)))
6330         (gnus-summary-mark-below nil)   ; Inhibit this.
6331         found)
6332     ;; This will do all the work of generating the new summary buffer
6333     ;; according to the new limit.
6334     (gnus-summary-prepare)
6335     ;; Hide any threads, possibly.
6336     (and gnus-show-threads
6337          gnus-thread-hide-subtree
6338          (gnus-summary-hide-all-threads))
6339     ;; Try to return to the article you were at, or one in the
6340     ;; neighborhood.
6341     (when data
6342       ;; We try to find some article after the current one.
6343       (while data
6344         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6345           (setq data nil
6346                 found t))
6347         (setq data (cdr data))))
6348     (unless found
6349       ;; If there is no data, that means that we were after the last
6350       ;; article.  The same goes when we can't find any articles
6351       ;; after the current one.
6352       (goto-char (point-max))
6353       (gnus-summary-find-prev))
6354     (gnus-set-mode-line 'summary)
6355     ;; We return how many articles were removed from the summary
6356     ;; buffer as a result of the new limit.
6357     (- total (length gnus-newsgroup-data))))
6358
6359 (defsubst gnus-invisible-cut-children (threads)
6360   (let ((num 0))
6361     (while threads
6362       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6363         (incf num))
6364       (pop threads))
6365     (< num 2)))
6366
6367 (defsubst gnus-cut-thread (thread)
6368   "Go forwards in the thread until we find an article that we want to display."
6369   (when (or (eq gnus-fetch-old-headers 'some)
6370             (eq gnus-fetch-old-headers 'invisible)
6371             (eq gnus-build-sparse-threads 'some)
6372             (eq gnus-build-sparse-threads 'more))
6373     ;; Deal with old-fetched headers and sparse threads.
6374     (while (and
6375             thread
6376             (or
6377              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6378              (gnus-summary-article-ancient-p
6379               (mail-header-number (car thread))))
6380             (if (or (<= (length (cdr thread)) 1)
6381                     (eq gnus-fetch-old-headers 'invisible))
6382                 (setq gnus-newsgroup-limit
6383                       (delq (mail-header-number (car thread))
6384                             gnus-newsgroup-limit)
6385                       thread (cadr thread))
6386               (when (gnus-invisible-cut-children (cdr thread))
6387                 (let ((th (cdr thread)))
6388                   (while th
6389                     (if (memq (mail-header-number (caar th))
6390                               gnus-newsgroup-limit)
6391                         (setq thread (car th)
6392                               th nil)
6393                       (setq th (cdr th))))))))))
6394   thread)
6395
6396 (defun gnus-cut-threads (threads)
6397   "Cut off all uninteresting articles from the beginning of threads."
6398   (when (or (eq gnus-fetch-old-headers 'some)
6399             (eq gnus-fetch-old-headers 'invisible)
6400             (eq gnus-build-sparse-threads 'some)
6401             (eq gnus-build-sparse-threads 'more))
6402     (let ((th threads))
6403       (while th
6404         (setcar th (gnus-cut-thread (car th)))
6405         (setq th (cdr th)))))
6406   ;; Remove nixed out threads.
6407   (delq nil threads))
6408
6409 (defun gnus-summary-initial-limit (&optional show-if-empty)
6410   "Figure out what the initial limit is supposed to be on group entry.
6411 This entails weeding out unwanted dormants, low-scored articles,
6412 fetch-old-headers verbiage, and so on."
6413   ;; Most groups have nothing to remove.
6414   (if (or gnus-inhibit-limiting
6415           (and (null gnus-newsgroup-dormant)
6416                (not (eq gnus-fetch-old-headers 'some))
6417                (not (eq gnus-fetch-old-headers 'invisible))
6418                (null gnus-summary-expunge-below)
6419                (not (eq gnus-build-sparse-threads 'some))
6420                (not (eq gnus-build-sparse-threads 'more))
6421                (null gnus-thread-expunge-below)
6422                (not gnus-use-nocem)))
6423       ()                                ; Do nothing.
6424     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6425     (setq gnus-newsgroup-limit nil)
6426     (mapatoms
6427      (lambda (node)
6428        (unless (car (symbol-value node))
6429          ;; These threads have no parents -- they are roots.
6430          (let ((nodes (cdr (symbol-value node)))
6431                thread)
6432            (while nodes
6433              (if (and gnus-thread-expunge-below
6434                       (< (gnus-thread-total-score (car nodes))
6435                          gnus-thread-expunge-below))
6436                  (gnus-expunge-thread (pop nodes))
6437                (setq thread (pop nodes))
6438                (gnus-summary-limit-children thread))))))
6439      gnus-newsgroup-dependencies)
6440     ;; If this limitation resulted in an empty group, we might
6441     ;; pop the previous limit and use it instead.
6442     (when (and (not gnus-newsgroup-limit)
6443                show-if-empty)
6444       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6445     gnus-newsgroup-limit))
6446
6447 (defun gnus-summary-limit-children (thread)
6448   "Return 1 if this subthread is visible and 0 if it is not."
6449   ;; First we get the number of visible children to this thread.  This
6450   ;; is done by recursing down the thread using this function, so this
6451   ;; will really go down to a leaf article first, before slowly
6452   ;; working its way up towards the root.
6453   (when thread
6454     (let ((children
6455            (if (cdr thread)
6456                (apply '+ (mapcar 'gnus-summary-limit-children
6457                                  (cdr thread)))
6458              0))
6459           (number (mail-header-number (car thread)))
6460           score)
6461       (if (and
6462            (not (memq number gnus-newsgroup-marked))
6463            (or
6464             ;; If this article is dormant and has absolutely no visible
6465             ;; children, then this article isn't visible.
6466             (and (memq number gnus-newsgroup-dormant)
6467                  (zerop children))
6468             ;; If this is "fetch-old-headered" and there is no
6469             ;; visible children, then we don't want this article.
6470             (and (eq gnus-fetch-old-headers 'some)
6471                  (gnus-summary-article-ancient-p number)
6472                  (zerop children))
6473             ;; If this is "fetch-old-headered" and `invisible', then
6474             ;; we don't want this article.
6475             (and (eq gnus-fetch-old-headers 'invisible)
6476                  (gnus-summary-article-ancient-p number))
6477             ;; If this is a sparsely inserted article with no children,
6478             ;; we don't want it.
6479             (and (eq gnus-build-sparse-threads 'some)
6480                  (gnus-summary-article-sparse-p number)
6481                  (zerop children))
6482             ;; If we use expunging, and this article is really
6483             ;; low-scored, then we don't want this article.
6484             (when (and gnus-summary-expunge-below
6485                        (< (setq score
6486                                 (or (cdr (assq number gnus-newsgroup-scored))
6487                                     gnus-summary-default-score))
6488                           gnus-summary-expunge-below))
6489               ;; We increase the expunge-tally here, but that has
6490               ;; nothing to do with the limits, really.
6491               (incf gnus-newsgroup-expunged-tally)
6492               ;; We also mark as read here, if that's wanted.
6493               (when (and gnus-summary-mark-below
6494                          (< score gnus-summary-mark-below))
6495                 (setq gnus-newsgroup-unreads
6496                       (delq number gnus-newsgroup-unreads))
6497                 (if gnus-newsgroup-auto-expire
6498                     (push number gnus-newsgroup-expirable)
6499                   (push (cons number gnus-low-score-mark)
6500                         gnus-newsgroup-reads)))
6501               t)
6502             ;; Check NoCeM things.
6503             (if (and gnus-use-nocem
6504                      (gnus-nocem-unwanted-article-p
6505                       (mail-header-id (car thread))))
6506                 (progn
6507                   (setq gnus-newsgroup-unreads
6508                         (delq number gnus-newsgroup-unreads))
6509                   t))))
6510           ;; Nope, invisible article.
6511           0
6512         ;; Ok, this article is to be visible, so we add it to the limit
6513         ;; and return 1.
6514         (push number gnus-newsgroup-limit)
6515         1))))
6516
6517 (defun gnus-expunge-thread (thread)
6518   "Mark all articles in THREAD as read."
6519   (let* ((number (mail-header-number (car thread))))
6520     (incf gnus-newsgroup-expunged-tally)
6521     ;; We also mark as read here, if that's wanted.
6522     (setq gnus-newsgroup-unreads
6523           (delq number gnus-newsgroup-unreads))
6524     (if gnus-newsgroup-auto-expire
6525         (push number gnus-newsgroup-expirable)
6526       (push (cons number gnus-low-score-mark)
6527             gnus-newsgroup-reads)))
6528   ;; Go recursively through all subthreads.
6529   (mapcar 'gnus-expunge-thread (cdr thread)))
6530
6531 ;; Summary article oriented commands
6532
6533 (defun gnus-summary-refer-parent-article (n)
6534   "Refer parent article N times.
6535 If N is negative, go to ancestor -N instead.
6536 The difference between N and the number of articles fetched is returned."
6537   (interactive "p")
6538   (let ((skip 1)
6539         error header ref)
6540     (when (not (natnump n))
6541       (setq skip (abs n)
6542             n 1))
6543     (while (and (> n 0)
6544                 (not error))
6545       (setq header (gnus-summary-article-header))
6546       (if (and (eq (mail-header-number header)
6547                    (cdr gnus-article-current))
6548                (equal gnus-newsgroup-name
6549                       (car gnus-article-current)))
6550           ;; If we try to find the parent of the currently
6551           ;; displayed article, then we take a look at the actual
6552           ;; References header, since this is slightly more
6553           ;; reliable than the References field we got from the
6554           ;; server.
6555           (save-excursion
6556             (set-buffer gnus-original-article-buffer)
6557             (nnheader-narrow-to-headers)
6558             (unless (setq ref (message-fetch-field "references"))
6559               (setq ref (message-fetch-field "in-reply-to")))
6560             (widen))
6561         (setq ref
6562               ;; It's not the current article, so we take a bet on
6563               ;; the value we got from the server.
6564               (mail-header-references header)))
6565       (if (and ref
6566                (not (equal ref "")))
6567           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6568             (gnus-message 1 "Couldn't find parent"))
6569         (gnus-message 1 "No references in article %d"
6570                       (gnus-summary-article-number))
6571         (setq error t))
6572       (decf n))
6573     (gnus-summary-position-point)
6574     n))
6575
6576 (defun gnus-summary-refer-references ()
6577   "Fetch all articles mentioned in the References header.
6578 Return the number of articles fetched."
6579   (interactive)
6580   (let ((ref (mail-header-references (gnus-summary-article-header)))
6581         (current (gnus-summary-article-number))
6582         (n 0))
6583     (if (or (not ref)
6584             (equal ref ""))
6585         (error "No References in the current article")
6586       ;; For each Message-ID in the References header...
6587       (while (string-match "<[^>]*>" ref)
6588         (incf n)
6589         ;; ... fetch that article.
6590         (gnus-summary-refer-article
6591          (prog1 (match-string 0 ref)
6592            (setq ref (substring ref (match-end 0))))))
6593       (gnus-summary-goto-subject current)
6594       (gnus-summary-position-point)
6595       n)))
6596
6597 (defun gnus-summary-refer-thread (&optional limit)
6598   "Fetch all articles in the current thread.
6599 If LIMIT (the numerical prefix), fetch that many old headers instead
6600 of what's specified by the `gnus-refer-thread-limit' variable."
6601   (interactive "P")
6602   (let ((id (mail-header-id (gnus-summary-article-header)))
6603         (limit (if limit (prefix-numeric-value limit)
6604                  gnus-refer-thread-limit)))
6605     ;; We want to fetch LIMIT *old* headers, but we also have to
6606     ;; re-fetch all the headers in the current buffer, because many of
6607     ;; them may be undisplayed.  So we adjust LIMIT.
6608     (when (numberp limit)
6609       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6610     (unless (eq gnus-fetch-old-headers 'invisible)
6611       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6612       ;; Retrieve the headers and read them in.
6613       (if (eq (gnus-retrieve-headers
6614                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6615               'nov)
6616           (gnus-build-all-threads)
6617         (error "Can't fetch thread from backends that don't support NOV"))
6618       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6619     (gnus-summary-limit-include-thread id)))
6620
6621 (defun gnus-summary-refer-article (message-id &optional arg)
6622   "Fetch an article specified by MESSAGE-ID.
6623 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6624 or `gnus-select-method', no matter what backend the article comes from."
6625   (interactive "sMessage-ID: \nP")
6626   (when (and (stringp message-id)
6627              (not (zerop (length message-id))))
6628     ;; Construct the correct Message-ID if necessary.
6629     ;; Suggested by tale@pawl.rpi.edu.
6630     (unless (string-match "^<" message-id)
6631       (setq message-id (concat "<" message-id)))
6632     (unless (string-match ">$" message-id)
6633       (setq message-id (concat message-id ">")))
6634     (let* ((header (gnus-id-to-header message-id))
6635            (sparse (and header
6636                         (gnus-summary-article-sparse-p
6637                          (mail-header-number header))
6638                         (memq (mail-header-number header)
6639                               gnus-newsgroup-limit))))
6640       (cond
6641        ;; If the article is present in the buffer we just go to it.
6642        ((and header
6643              (or (not (gnus-summary-article-sparse-p
6644                        (mail-header-number header)))
6645                  sparse))
6646         (prog1
6647             (gnus-summary-goto-article
6648              (mail-header-number header) nil t)
6649           (when sparse
6650             (gnus-summary-update-article (mail-header-number header)))))
6651        (t
6652         ;; We fetch the article
6653         (let ((gnus-override-method
6654                (cond ((gnus-news-group-p gnus-newsgroup-name)
6655                       gnus-refer-article-method)
6656                      (arg
6657                       (or gnus-refer-article-method gnus-select-method))
6658                      (t nil)))
6659               number)
6660           ;; Start the special refer-article method, if necessary.
6661           (when (and gnus-refer-article-method
6662                      (gnus-news-group-p gnus-newsgroup-name))
6663             (gnus-check-server gnus-refer-article-method))
6664           ;; Fetch the header, and display the article.
6665           (if (setq number (gnus-summary-insert-subject message-id))
6666               (gnus-summary-select-article nil nil nil number)
6667             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6668
6669 (defun gnus-summary-edit-parameters ()
6670   "Edit the group parameters of the current group."
6671   (interactive)
6672   (gnus-group-edit-group gnus-newsgroup-name 'params))
6673
6674 (defun gnus-summary-customize-parameters ()
6675   "Customize the group parameters of the current group."
6676   (interactive)
6677   (gnus-group-customize gnus-newsgroup-name))
6678
6679 (defun gnus-summary-enter-digest-group (&optional force)
6680   "Enter an nndoc group based on the current article.
6681 If FORCE, force a digest interpretation.  If not, try
6682 to guess what the document format is."
6683   (interactive "P")
6684   (let ((conf gnus-current-window-configuration))
6685     (save-excursion
6686       (gnus-summary-select-article))
6687     (setq gnus-current-window-configuration conf)
6688     (let* ((name (format "%s-%d"
6689                          (gnus-group-prefixed-name
6690                           gnus-newsgroup-name (list 'nndoc ""))
6691                          (save-excursion
6692                            (set-buffer gnus-summary-buffer)
6693                            gnus-current-article)))
6694            (ogroup gnus-newsgroup-name)
6695            (params (append (gnus-info-params (gnus-get-info ogroup))
6696                            (list (cons 'to-group ogroup))
6697                            (list (cons 'save-article-group ogroup))))
6698            (case-fold-search t)
6699            (buf (current-buffer))
6700            dig)
6701       (save-excursion
6702         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6703         (insert-buffer-substring gnus-original-article-buffer)
6704         ;; Remove lines that may lead nndoc to misinterpret the
6705         ;; document type.
6706         (narrow-to-region
6707          (goto-char (point-min))
6708          (or (search-forward "\n\n" nil t) (point)))
6709         (goto-char (point-min))
6710         (delete-matching-lines "^\\(Path\\):\\|^From ")
6711         (widen))
6712       (unwind-protect
6713           (if (gnus-group-read-ephemeral-group
6714                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6715                             (nndoc-article-type
6716                              ,(if force 'digest 'guess))) t)
6717               ;; Make all postings to this group go to the parent group.
6718               (nconc (gnus-info-params (gnus-get-info name))
6719                      params)
6720             ;; Couldn't select this doc group.
6721             (switch-to-buffer buf)
6722             (gnus-set-global-variables)
6723             (gnus-configure-windows 'summary)
6724             (gnus-message 3 "Article couldn't be entered?"))
6725         (kill-buffer dig)))))
6726
6727 (defun gnus-summary-read-document (n)
6728   "Open a new group based on the current article(s).
6729 This will allow you to read digests and other similar
6730 documents as newsgroups.
6731 Obeys the standard process/prefix convention."
6732   (interactive "P")
6733   (let* ((articles (gnus-summary-work-articles n))
6734          (ogroup gnus-newsgroup-name)
6735          (params (append (gnus-info-params (gnus-get-info ogroup))
6736                          (list (cons 'to-group ogroup))))
6737          article group egroup groups vgroup)
6738     (while (setq article (pop articles))
6739       (setq group (format "%s-%d" gnus-newsgroup-name article))
6740       (gnus-summary-remove-process-mark article)
6741       (when (gnus-summary-display-article article)
6742         (save-excursion
6743           (with-temp-buffer
6744             (insert-buffer-substring gnus-original-article-buffer)
6745             ;; Remove some headers that may lead nndoc to make
6746             ;; the wrong guess.
6747             (message-narrow-to-head)
6748             (goto-char (point-min))
6749             (delete-matching-lines "^\\(Path\\):\\|^From ")
6750             (widen)
6751             (if (setq egroup
6752                       (gnus-group-read-ephemeral-group
6753                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6754                                      (nndoc-article-type guess))
6755                        t nil t))
6756                 (progn
6757                   ;; Make all postings to this group go to the parent group.
6758                   (nconc (gnus-info-params (gnus-get-info egroup))
6759                          params)
6760                   (push egroup groups))
6761               ;; Couldn't select this doc group.
6762               (gnus-error 3 "Article couldn't be entered"))))))
6763     ;; Now we have selected all the documents.
6764     (cond
6765      ((not groups)
6766       (error "None of the articles could be interpreted as documents"))
6767      ((gnus-group-read-ephemeral-group
6768        (setq vgroup (format
6769                      "nnvirtual:%s-%s" gnus-newsgroup-name
6770                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6771        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6772        t
6773        (cons (current-buffer) 'summary)))
6774      (t
6775       (error "Couldn't select virtual nndoc group")))))
6776
6777 (defun gnus-summary-isearch-article (&optional regexp-p)
6778   "Do incremental search forward on the current article.
6779 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6780   (interactive "P")
6781   (gnus-summary-select-article)
6782   (gnus-configure-windows 'article)
6783   (gnus-eval-in-buffer-window gnus-article-buffer
6784     (save-restriction
6785       (widen)
6786       (isearch-forward regexp-p))))
6787
6788 (defun gnus-summary-search-article-forward (regexp &optional backward)
6789   "Search for an article containing REGEXP forward.
6790 If BACKWARD, search backward instead."
6791   (interactive
6792    (list (read-string
6793           (format "Search article %s (regexp%s): "
6794                   (if current-prefix-arg "backward" "forward")
6795                   (if gnus-last-search-regexp
6796                       (concat ", default " gnus-last-search-regexp)
6797                     "")))
6798          current-prefix-arg))
6799   (if (string-equal regexp "")
6800       (setq regexp (or gnus-last-search-regexp ""))
6801     (setq gnus-last-search-regexp regexp))
6802   (if (gnus-summary-search-article regexp backward)
6803       (gnus-summary-show-thread)
6804     (error "Search failed: \"%s\"" regexp)))
6805
6806 (defun gnus-summary-search-article-backward (regexp)
6807   "Search for an article containing REGEXP backward."
6808   (interactive
6809    (list (read-string
6810           (format "Search article backward (regexp%s): "
6811                   (if gnus-last-search-regexp
6812                       (concat ", default " gnus-last-search-regexp)
6813                     "")))))
6814   (gnus-summary-search-article-forward regexp 'backward))
6815
6816 (defun gnus-summary-search-article (regexp &optional backward)
6817   "Search for an article containing REGEXP.
6818 Optional argument BACKWARD means do search for backward.
6819 `gnus-select-article-hook' is not called during the search."
6820   ;; We have to require this here to make sure that the following
6821   ;; dynamic binding isn't shadowed by autoloading.
6822   (require 'gnus-async)
6823   (require 'gnus-art)
6824   (let ((gnus-select-article-hook nil)  ;Disable hook.
6825         (gnus-article-display-hook nil)
6826         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6827         (gnus-use-article-prefetch nil)
6828         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6829         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6830         (sum (current-buffer))
6831         (gnus-display-mime-function nil)
6832         (found nil)
6833         point)
6834     (gnus-save-hidden-threads
6835       (gnus-summary-select-article)
6836       (set-buffer gnus-article-buffer)
6837       (when backward
6838         (forward-line -1))
6839       (while (not found)
6840         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6841         (if (if backward
6842                 (re-search-backward regexp nil t)
6843               (re-search-forward regexp nil t))
6844             ;; We found the regexp.
6845             (progn
6846               (setq found 'found)
6847               (beginning-of-line)
6848               (set-window-start
6849                (get-buffer-window (current-buffer))
6850                (point))
6851               (forward-line 1)
6852               (set-buffer sum)
6853               (setq point (point)))
6854           ;; We didn't find it, so we go to the next article.
6855           (set-buffer sum)
6856           (setq found 'not)
6857           (while (eq found 'not)
6858             (if (not (if backward (gnus-summary-find-prev)
6859                        (gnus-summary-find-next)))
6860                 ;; No more articles.
6861                 (setq found t)
6862               ;; Select the next article and adjust point.
6863               (unless (gnus-summary-article-sparse-p
6864                        (gnus-summary-article-number))
6865                 (setq found nil)
6866                 (gnus-summary-select-article)
6867                 (set-buffer gnus-article-buffer)
6868                 (widen)
6869                 (goto-char (if backward (point-max) (point-min))))))))
6870       (gnus-message 7 ""))
6871     ;; Return whether we found the regexp.
6872     (when (eq found 'found)
6873       (goto-char point)
6874       (gnus-summary-show-thread)
6875       (gnus-summary-goto-subject gnus-current-article)
6876       (gnus-summary-position-point)
6877       t)))
6878
6879 (defun gnus-summary-find-matching (header regexp &optional backward unread
6880                                           not-case-fold)
6881   "Return a list of all articles that match REGEXP on HEADER.
6882 The search stars on the current article and goes forwards unless
6883 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6884 If UNREAD is non-nil, only unread articles will
6885 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6886 in the comparisons."
6887   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6888                 (gnus-data-find-list
6889                  (gnus-summary-article-number) (gnus-data-list backward))))
6890         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6891         (case-fold-search (not not-case-fold))
6892         articles d)
6893     (unless (fboundp (intern (concat "mail-header-" header)))
6894       (error "%s is not a valid header" header))
6895     (while data
6896       (setq d (car data))
6897       (and (or (not unread)             ; We want all articles...
6898                (gnus-data-unread-p d))  ; Or just unreads.
6899            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6900            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6901            (push (gnus-data-number d) articles)) ; Success!
6902       (setq data (cdr data)))
6903     (nreverse articles)))
6904
6905 (defun gnus-summary-execute-command (header regexp command &optional backward)
6906   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6907 If HEADER is an empty string (or nil), the match is done on the entire
6908 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6909   (interactive
6910    (list (let ((completion-ignore-case t))
6911            (completing-read
6912             "Header name: "
6913             (mapcar (lambda (string) (list string))
6914                     '("Number" "Subject" "From" "Lines" "Date"
6915                       "Message-ID" "Xref" "References" "Body"))
6916             nil 'require-match))
6917          (read-string "Regexp: ")
6918          (read-key-sequence "Command: ")
6919          current-prefix-arg))
6920   (when (equal header "Body")
6921     (setq header ""))
6922   ;; Hidden thread subtrees must be searched as well.
6923   (gnus-summary-show-all-threads)
6924   ;; We don't want to change current point nor window configuration.
6925   (save-excursion
6926     (save-window-excursion
6927       (gnus-message 6 "Executing %s..." (key-description command))
6928       ;; We'd like to execute COMMAND interactively so as to give arguments.
6929       (gnus-execute header regexp
6930                     `(call-interactively ',(key-binding command))
6931                     backward)
6932       (gnus-message 6 "Executing %s...done" (key-description command)))))
6933
6934 (defun gnus-summary-beginning-of-article ()
6935   "Scroll the article back to the beginning."
6936   (interactive)
6937   (gnus-summary-select-article)
6938   (gnus-configure-windows 'article)
6939   (gnus-eval-in-buffer-window gnus-article-buffer
6940     (widen)
6941     (goto-char (point-min))
6942     (when gnus-page-broken
6943       (gnus-narrow-to-page))))
6944
6945 (defun gnus-summary-end-of-article ()
6946   "Scroll to the end of the article."
6947   (interactive)
6948   (gnus-summary-select-article)
6949   (gnus-configure-windows 'article)
6950   (gnus-eval-in-buffer-window gnus-article-buffer
6951     (widen)
6952     (goto-char (point-max))
6953     (recenter -3)
6954     (when gnus-page-broken
6955       (gnus-narrow-to-page))))
6956
6957 (defun gnus-summary-print-article (&optional filename n)
6958   "Generate and print a PostScript image of the N next (mail) articles.
6959
6960 If N is negative, print the N previous articles.  If N is nil and articles
6961 have been marked with the process mark, print these instead.
6962
6963 If the optional second argument FILENAME is nil, send the image to the
6964 printer.  If FILENAME is a string, save the PostScript image in a file with
6965 that name.  If FILENAME is a number, prompt the user for the name of the file
6966 to save in."
6967   (interactive (list (ps-print-preprint current-prefix-arg)
6968                      current-prefix-arg))
6969   (dolist (article (gnus-summary-work-articles n))
6970     (gnus-summary-select-article nil nil 'pseudo article)
6971     (gnus-eval-in-buffer-window gnus-article-buffer
6972       (let ((buffer (generate-new-buffer " *print*")))
6973         (unwind-protect
6974             (progn
6975               (copy-to-buffer buffer (point-min) (point-max))
6976               (set-buffer buffer)
6977               (gnus-article-delete-invisible-text)
6978               (let ((ps-left-header
6979                      (list
6980                       (concat "("
6981                               (mail-header-subject gnus-current-headers) ")")
6982                       (concat "("
6983                               (mail-header-from gnus-current-headers) ")")))
6984                     (ps-right-header
6985                      (list
6986                       "/pagenumberstring load"
6987                       (concat "("
6988                               (mail-header-date gnus-current-headers) ")"))))
6989                 (gnus-run-hooks 'gnus-ps-print-hook)
6990                 (save-excursion
6991                   (ps-print-buffer-with-faces filename))))
6992           (kill-buffer buffer))))))
6993
6994 (defun gnus-summary-show-article (&optional arg)
6995   "Force re-fetching of the current article.
6996 If ARG (the prefix) is non-nil, show the raw article without any
6997 article massaging functions being run."
6998   (interactive "P")
6999   (if (not arg)
7000       ;; Select the article the normal way.
7001       (gnus-summary-select-article nil 'force)
7002     ;; We have to require this here to make sure that the following
7003     ;; dynamic binding isn't shadowed by autoloading.
7004     (require 'gnus-async)
7005     (require 'gnus-art)
7006     ;; Bind the article treatment functions to nil.
7007     (let ((gnus-have-all-headers t)
7008           gnus-article-display-hook
7009           gnus-article-prepare-hook
7010           gnus-article-decode-hook
7011           gnus-display-mime-function
7012           gnus-break-pages
7013           gnus-visual)
7014       ;; Destroy any MIME parts.
7015       (when (gnus-buffer-live-p gnus-article-buffer)
7016         (save-excursion
7017           (set-buffer gnus-article-buffer)
7018           (mm-destroy-parts gnus-article-mime-handles)))
7019       (gnus-summary-select-article nil 'force)))
7020   (gnus-summary-goto-subject gnus-current-article)
7021   (gnus-summary-position-point))
7022
7023 (defun gnus-summary-verbose-headers (&optional arg)
7024   "Toggle permanent full header display.
7025 If ARG is a positive number, turn header display on.
7026 If ARG is a negative number, turn header display off."
7027   (interactive "P")
7028   (setq gnus-show-all-headers
7029         (cond ((or (not (numberp arg))
7030                    (zerop arg))
7031                (not gnus-show-all-headers))
7032               ((natnump arg)
7033                t)))
7034   (gnus-summary-show-article))
7035
7036 (defun gnus-summary-toggle-header (&optional arg)
7037   "Show the headers if they are hidden, or hide them if they are shown.
7038 If ARG is a positive number, show the entire header.
7039 If ARG is a negative number, hide the unwanted header lines."
7040   (interactive "P")
7041   (save-excursion
7042     (set-buffer gnus-article-buffer)
7043     (let* ((buffer-read-only nil)
7044            (inhibit-point-motion-hooks t)
7045            (hidden (text-property-any
7046                     (goto-char (point-min)) (search-forward "\n\n")
7047                     'invisible t))
7048            e)
7049       (goto-char (point-min))
7050       (when (search-forward "\n\n" nil t)
7051         (delete-region (point-min) (1- (point))))
7052       (goto-char (point-min))
7053       (save-excursion
7054         (set-buffer gnus-original-article-buffer)
7055         (goto-char (point-min))
7056         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7057       (insert-buffer-substring gnus-original-article-buffer 1 e)
7058       (let ((article-inhibit-hiding t))
7059         (gnus-run-hooks 'gnus-article-display-hook))
7060       (when (or (not hidden) (and (numberp arg) (< arg 0)))
7061         (gnus-article-hide-headers)))))
7062
7063 (defun gnus-summary-show-all-headers ()
7064   "Make all header lines visible."
7065   (interactive)
7066   (gnus-article-show-all-headers))
7067
7068 (defun gnus-summary-caesar-message (&optional arg)
7069   "Caesar rotate the current article by 13.
7070 The numerical prefix specifies how many places to rotate each letter
7071 forward."
7072   (interactive "P")
7073   (gnus-summary-select-article)
7074   (let ((mail-header-separator ""))
7075     (gnus-eval-in-buffer-window gnus-article-buffer
7076       (save-restriction
7077         (widen)
7078         (let ((start (window-start))
7079               buffer-read-only)
7080           (message-caesar-buffer-body arg)
7081           (set-window-start (get-buffer-window (current-buffer)) start))))))
7082
7083 (defun gnus-summary-stop-page-breaking ()
7084   "Stop page breaking in the current article."
7085   (interactive)
7086   (gnus-summary-select-article)
7087   (gnus-eval-in-buffer-window gnus-article-buffer
7088     (widen)
7089     (when (gnus-visual-p 'page-marker)
7090       (let ((buffer-read-only nil))
7091         (gnus-remove-text-with-property 'gnus-prev)
7092         (gnus-remove-text-with-property 'gnus-next))
7093       (setq gnus-page-broken nil))))
7094
7095 (defun gnus-summary-move-article (&optional n to-newsgroup
7096                                             select-method action)
7097   "Move the current article to a different newsgroup.
7098 If N is a positive number, move the N next articles.
7099 If N is a negative number, move the N previous articles.
7100 If N is nil and any articles have been marked with the process mark,
7101 move those articles instead.
7102 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7103 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7104 re-spool using this method.
7105
7106 For this function to work, both the current newsgroup and the
7107 newsgroup that you want to move to have to support the `request-move'
7108 and `request-accept' functions."
7109   (interactive "P")
7110   (unless action
7111     (setq action 'move))
7112   ;; Disable marking as read.
7113   (let (gnus-mark-article-hook)
7114     (save-window-excursion
7115       (gnus-summary-select-article)))
7116   ;; Check whether the source group supports the required functions.
7117   (cond ((and (eq action 'move)
7118               (not (gnus-check-backend-function
7119                     'request-move-article gnus-newsgroup-name)))
7120          (error "The current group does not support article moving"))
7121         ((and (eq action 'crosspost)
7122               (not (gnus-check-backend-function
7123                     'request-replace-article gnus-newsgroup-name)))
7124          (error "The current group does not support article editing")))
7125   (let ((articles (gnus-summary-work-articles n))
7126         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7127         (names '((move "Move" "Moving")
7128                  (copy "Copy" "Copying")
7129                  (crosspost "Crosspost" "Crossposting")))
7130         (copy-buf (save-excursion
7131                     (nnheader-set-temp-buffer " *copy article*")))
7132         art-group to-method new-xref article to-groups)
7133     (unless (assq action names)
7134       (error "Unknown action %s" action))
7135     ;; Read the newsgroup name.
7136     (when (and (not to-newsgroup)
7137                (not select-method))
7138       (setq to-newsgroup
7139             (gnus-read-move-group-name
7140              (cadr (assq action names))
7141              (symbol-value (intern (format "gnus-current-%s-group" action)))
7142              articles prefix))
7143       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7144     (setq to-method (or select-method
7145                         (gnus-group-name-to-method to-newsgroup)))
7146     ;; Check the method we are to move this article to...
7147     (unless (gnus-check-backend-function
7148              'request-accept-article (car to-method))
7149       (error "%s does not support article copying" (car to-method)))
7150     (unless (gnus-check-server to-method)
7151       (error "Can't open server %s" (car to-method)))
7152     (gnus-message 6 "%s to %s: %s..."
7153                   (caddr (assq action names))
7154                   (or (car select-method) to-newsgroup) articles)
7155     (while articles
7156       (setq article (pop articles))
7157       (setq
7158        art-group
7159        (cond
7160         ;; Move the article.
7161         ((eq action 'move)
7162          ;; Remove this article from future suppression.
7163          (gnus-dup-unsuppress-article article)
7164          (gnus-request-move-article
7165           article                       ; Article to move
7166           gnus-newsgroup-name           ; From newsgroup
7167           (nth 1 (gnus-find-method-for-group
7168                   gnus-newsgroup-name)) ; Server
7169           (list 'gnus-request-accept-article
7170                 to-newsgroup (list 'quote select-method)
7171                 (not articles) t)               ; Accept form
7172           (not articles)))              ; Only save nov last time
7173         ;; Copy the article.
7174         ((eq action 'copy)
7175          (save-excursion
7176            (set-buffer copy-buf)
7177            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7178              (gnus-request-accept-article
7179               to-newsgroup select-method (not articles) t))))
7180         ;; Crosspost the article.
7181         ((eq action 'crosspost)
7182          (let ((xref (message-tokenize-header
7183                       (mail-header-xref (gnus-summary-article-header article))
7184                       " ")))
7185            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7186                                   ":" article))
7187            (unless xref
7188              (setq xref (list (system-name))))
7189            (setq new-xref
7190                  (concat
7191                   (mapconcat 'identity
7192                              (delete "Xref:" (delete new-xref xref))
7193                              " ")
7194                   " " new-xref))
7195            (save-excursion
7196              (set-buffer copy-buf)
7197              ;; First put the article in the destination group.
7198              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7199              (when (consp (setq art-group
7200                                 (gnus-request-accept-article
7201                                  to-newsgroup select-method (not articles))))
7202                (setq new-xref (concat new-xref " " (car art-group)
7203                                       ":" (cdr art-group)))
7204                ;; Now we have the new Xrefs header, so we insert
7205                ;; it and replace the new article.
7206                (nnheader-replace-header "Xref" new-xref)
7207                (gnus-request-replace-article
7208                 (cdr art-group) to-newsgroup (current-buffer))
7209                art-group))))))
7210       (cond
7211        ((not art-group)
7212         (gnus-message 1 "Couldn't %s article %s"
7213                       (cadr (assq action names)) article))
7214        ((and (eq art-group 'junk)
7215              (eq action 'move))
7216         (gnus-summary-mark-article article gnus-canceled-mark)
7217         (gnus-message 4 "Deleted article %s" article))
7218        (t
7219         (let* ((pto-group (gnus-group-prefixed-name
7220                            (car art-group) to-method))
7221                (entry
7222                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7223                (info (nth 2 entry))
7224                (to-group (gnus-info-group info)))
7225           ;; Update the group that has been moved to.
7226           (when (and info
7227                      (memq action '(move copy)))
7228             (unless (member to-group to-groups)
7229               (push to-group to-groups))
7230
7231             (unless (memq article gnus-newsgroup-unreads)
7232               (gnus-info-set-read
7233                info (gnus-add-to-range (gnus-info-read info)
7234                                        (list (cdr art-group)))))
7235
7236             ;; Copy any marks over to the new group.
7237             (let ((marks gnus-article-mark-lists)
7238                   (to-article (cdr art-group)))
7239
7240               ;; See whether the article is to be put in the cache.
7241               (when gnus-use-cache
7242                 (gnus-cache-possibly-enter-article
7243                  to-group to-article
7244                  (let ((header (copy-sequence
7245                                 (gnus-summary-article-header article))))
7246                    (mail-header-set-number header to-article)
7247                    header)
7248                  (memq article gnus-newsgroup-marked)
7249                  (memq article gnus-newsgroup-dormant)
7250                  (memq article gnus-newsgroup-unreads)))
7251
7252               (when (and (equal to-group gnus-newsgroup-name)
7253                          (not (memq article gnus-newsgroup-unreads)))
7254                 ;; Mark this article as read in this group.
7255                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7256                 (setcdr (gnus-active to-group) to-article)
7257                 (setcdr gnus-newsgroup-active to-article))
7258
7259               (while marks
7260                 (when (memq article (symbol-value
7261                                      (intern (format "gnus-newsgroup-%s"
7262                                                      (caar marks)))))
7263                   ;; If the other group is the same as this group,
7264                   ;; then we have to add the mark to the list.
7265                   (when (equal to-group gnus-newsgroup-name)
7266                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7267                          (cons to-article
7268                                (symbol-value
7269                                 (intern (format "gnus-newsgroup-%s"
7270                                                 (caar marks)))))))
7271                   ;; Copy the marks to other group.
7272                   (gnus-add-marked-articles
7273                    to-group (cdar marks) (list to-article) info))
7274                 (setq marks (cdr marks)))
7275
7276               (gnus-dribble-enter
7277                (concat "(gnus-group-set-info '"
7278                        (gnus-prin1-to-string (gnus-get-info to-group))
7279                        ")"))))
7280
7281           ;; Update the Xref header in this article to point to
7282           ;; the new crossposted article we have just created.
7283           (when (eq action 'crosspost)
7284             (save-excursion
7285               (set-buffer copy-buf)
7286               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7287               (nnheader-replace-header "Xref" new-xref)
7288               (gnus-request-replace-article
7289                article gnus-newsgroup-name (current-buffer)))))
7290
7291         ;;;!!!Why is this necessary?
7292         (set-buffer gnus-summary-buffer)
7293
7294         (gnus-summary-goto-subject article)
7295         (when (eq action 'move)
7296           (gnus-summary-mark-article article gnus-canceled-mark))))
7297       (gnus-summary-remove-process-mark article))
7298     ;; Re-activate all groups that have been moved to.
7299     (while to-groups
7300       (save-excursion
7301         (set-buffer gnus-group-buffer)
7302         (when (gnus-group-goto-group (car to-groups) t)
7303           (gnus-group-get-new-news-this-group 1 t))
7304         (pop to-groups)))
7305
7306     (gnus-kill-buffer copy-buf)
7307     (gnus-summary-position-point)
7308     (gnus-set-mode-line 'summary)))
7309
7310 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7311   "Move the current article to a different newsgroup.
7312 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7313 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7314 re-spool using this method."
7315   (interactive "P")
7316   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7317
7318 (defun gnus-summary-crosspost-article (&optional n)
7319   "Crosspost the current article to some other group."
7320   (interactive "P")
7321   (gnus-summary-move-article n nil nil 'crosspost))
7322
7323 (defcustom gnus-summary-respool-default-method nil
7324   "Default method for respooling an article.
7325 If nil, use to the current newsgroup method."
7326   :type '(choice (gnus-select-method :value (nnml ""))
7327                  (const nil))
7328   :group 'gnus-summary-mail)
7329
7330 (defun gnus-summary-respool-article (&optional n method)
7331   "Respool the current article.
7332 The article will be squeezed through the mail spooling process again,
7333 which means that it will be put in some mail newsgroup or other
7334 depending on `nnmail-split-methods'.
7335 If N is a positive number, respool the N next articles.
7336 If N is a negative number, respool the N previous articles.
7337 If N is nil and any articles have been marked with the process mark,
7338 respool those articles instead.
7339
7340 Respooling can be done both from mail groups and \"real\" newsgroups.
7341 In the former case, the articles in question will be moved from the
7342 current group into whatever groups they are destined to.  In the
7343 latter case, they will be copied into the relevant groups."
7344   (interactive
7345    (list current-prefix-arg
7346          (let* ((methods (gnus-methods-using 'respool))
7347                 (methname
7348                  (symbol-name (or gnus-summary-respool-default-method
7349                                   (car (gnus-find-method-for-group
7350                                         gnus-newsgroup-name)))))
7351                 (method
7352                  (gnus-completing-read
7353                   methname "What backend do you want to use when respooling?"
7354                   methods nil t nil 'gnus-mail-method-history))
7355                 ms)
7356            (cond
7357             ((zerop (length (setq ms (gnus-servers-using-backend
7358                                       (intern method)))))
7359              (list (intern method) ""))
7360             ((= 1 (length ms))
7361              (car ms))
7362             (t
7363              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7364                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7365                            ms-alist))))))))
7366   (unless method
7367     (error "No method given for respooling"))
7368   (if (assoc (symbol-name
7369               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7370              (gnus-methods-using 'respool))
7371       (gnus-summary-move-article n nil method)
7372     (gnus-summary-copy-article n nil method)))
7373
7374 (defun gnus-summary-import-article (file)
7375   "Import an arbitrary file into a mail newsgroup."
7376   (interactive "fImport file: ")
7377   (let ((group gnus-newsgroup-name)
7378         (now (current-time))
7379         atts lines)
7380     (unless (gnus-check-backend-function 'request-accept-article group)
7381       (error "%s does not support article importing" group))
7382     (or (file-readable-p file)
7383         (not (file-regular-p file))
7384         (error "Can't read %s" file))
7385     (save-excursion
7386       (set-buffer (gnus-get-buffer-create " *import file*"))
7387       (erase-buffer)
7388       (insert-file-contents file)
7389       (goto-char (point-min))
7390       (unless (nnheader-article-p)
7391         ;; This doesn't look like an article, so we fudge some headers.
7392         (setq atts (file-attributes file)
7393               lines (count-lines (point-min) (point-max)))
7394         (insert "From: " (read-string "From: ") "\n"
7395                 "Subject: " (read-string "Subject: ") "\n"
7396                 "Date: " (message-make-date (nth 5 atts))
7397                 "\n"
7398                 "Message-ID: " (message-make-message-id) "\n"
7399                 "Lines: " (int-to-string lines) "\n"
7400                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7401       (gnus-request-accept-article group nil t)
7402       (kill-buffer (current-buffer)))))
7403
7404 (defun gnus-summary-article-posted-p ()
7405   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7406 This will be the case if the article has both been mailed and posted."
7407   (interactive)
7408   (let ((id (mail-header-references (gnus-summary-article-header)))
7409         (gnus-override-method
7410          (or gnus-refer-article-method gnus-select-method)))
7411     (if (gnus-request-head id "")
7412         (gnus-message 2 "The current message was found on %s"
7413                       gnus-override-method)
7414       (gnus-message 2 "The current message couldn't be found on %s"
7415                     gnus-override-method)
7416       nil)))
7417
7418 (defun gnus-summary-expire-articles (&optional now)
7419   "Expire all articles that are marked as expirable in the current group."
7420   (interactive)
7421   (when (gnus-check-backend-function
7422          'request-expire-articles gnus-newsgroup-name)
7423     ;; This backend supports expiry.
7424     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7425            (expirable (if total
7426                           (progn
7427                             ;; We need to update the info for
7428                             ;; this group for `gnus-list-of-read-articles'
7429                             ;; to give us the right answer.
7430                             (gnus-run-hooks 'gnus-exit-group-hook)
7431                             (gnus-summary-update-info)
7432                             (gnus-list-of-read-articles gnus-newsgroup-name))
7433                         (setq gnus-newsgroup-expirable
7434                               (sort gnus-newsgroup-expirable '<))))
7435            (expiry-wait (if now 'immediate
7436                           (gnus-group-find-parameter
7437                            gnus-newsgroup-name 'expiry-wait)))
7438            es)
7439       (when expirable
7440         ;; There are expirable articles in this group, so we run them
7441         ;; through the expiry process.
7442         (gnus-message 6 "Expiring articles...")
7443         ;; The list of articles that weren't expired is returned.
7444         (save-excursion
7445           (if expiry-wait
7446               (let ((nnmail-expiry-wait-function nil)
7447                     (nnmail-expiry-wait expiry-wait))
7448                 (setq es (gnus-request-expire-articles
7449                           expirable gnus-newsgroup-name)))
7450             (setq es (gnus-request-expire-articles
7451                       expirable gnus-newsgroup-name))))
7452         (unless total
7453           (setq gnus-newsgroup-expirable es))
7454         ;; We go through the old list of expirable, and mark all
7455         ;; really expired articles as nonexistent.
7456         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7457           (let ((gnus-use-cache nil))
7458             (while expirable
7459               (unless (memq (car expirable) es)
7460                 (when (gnus-data-find (car expirable))
7461                   (gnus-summary-mark-article
7462                    (car expirable) gnus-canceled-mark)))
7463               (setq expirable (cdr expirable)))))
7464         (gnus-message 6 "Expiring articles...done")))))
7465
7466 (defun gnus-summary-expire-articles-now ()
7467   "Expunge all expirable articles in the current group.
7468 This means that *all* articles that are marked as expirable will be
7469 deleted forever, right now."
7470   (interactive)
7471   (or gnus-expert-user
7472       (gnus-yes-or-no-p
7473        "Are you really, really, really sure you want to delete all these messages? ")
7474       (error "Phew!"))
7475   (gnus-summary-expire-articles t))
7476
7477 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7478 (defun gnus-summary-delete-article (&optional n)
7479   "Delete the N next (mail) articles.
7480 This command actually deletes articles.  This is not a marking
7481 command.  The article will disappear forever from your life, never to
7482 return.
7483 If N is negative, delete backwards.
7484 If N is nil and articles have been marked with the process mark,
7485 delete these instead."
7486   (interactive "P")
7487   (unless (gnus-check-backend-function 'request-expire-articles
7488                                        gnus-newsgroup-name)
7489     (error "The current newsgroup does not support article deletion"))
7490   ;; Compute the list of articles to delete.
7491   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7492         not-deleted)
7493     (if (and gnus-novice-user
7494              (not (gnus-yes-or-no-p
7495                    (format "Do you really want to delete %s forever? "
7496                            (if (> (length articles) 1)
7497                                (format "these %s articles" (length articles))
7498                              "this article")))))
7499         ()
7500       ;; Delete the articles.
7501       (setq not-deleted (gnus-request-expire-articles
7502                          articles gnus-newsgroup-name 'force))
7503       (while articles
7504         (gnus-summary-remove-process-mark (car articles))
7505         ;; The backend might not have been able to delete the article
7506         ;; after all.
7507         (unless (memq (car articles) not-deleted)
7508           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7509         (setq articles (cdr articles)))
7510       (when not-deleted
7511         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7512     (gnus-summary-position-point)
7513     (gnus-set-mode-line 'summary)
7514     not-deleted))
7515
7516 (defun gnus-summary-edit-article (&optional force)
7517   "Edit the current article.
7518 This will have permanent effect only in mail groups.
7519 If FORCE is non-nil, allow editing of articles even in read-only
7520 groups."
7521   (interactive "P")
7522   (save-excursion
7523     (set-buffer gnus-summary-buffer)
7524     (gnus-set-global-variables)
7525     (when (and (not force)
7526                (gnus-group-read-only-p))
7527       (error "The current newsgroup does not support article editing"))
7528     ;; Select article if needed.
7529     (unless (eq (gnus-summary-article-number)
7530                 gnus-current-article)
7531       (gnus-summary-select-article t))
7532     (gnus-article-date-original)
7533     (gnus-article-edit-article
7534      `(lambda (no-highlight)
7535         (gnus-summary-edit-article-done
7536          ,(or (mail-header-references gnus-current-headers) "")
7537          ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))
7538
7539 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7540
7541 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7542                                                  no-highlight)
7543   "Make edits to the current article permanent."
7544   (interactive)
7545   ;; Replace the article.
7546   (let ((buf (current-buffer)))
7547     (with-temp-buffer
7548       (insert-buffer buf)
7549       (if (and (not read-only)
7550                (not (gnus-request-replace-article
7551                      (cdr gnus-article-current) (car gnus-article-current)
7552                      (current-buffer)
7553                      (not gnus-article-decoded-p))))
7554           (error "Couldn't replace article")
7555         ;; Update the summary buffer.
7556         (if (and references
7557                  (equal (message-tokenize-header references " ")
7558                         (message-tokenize-header
7559                          (or (message-fetch-field "references") "") " ")))
7560             ;; We only have to update this line.
7561             (save-excursion
7562               (save-restriction
7563                 (message-narrow-to-head)
7564                 (let ((head (buffer-string))
7565                       header)
7566                   (with-temp-buffer
7567                     (insert (format "211 %d Article retrieved.\n"
7568                                     (cdr gnus-article-current)))
7569                     (insert head)
7570                     (insert ".\n")
7571                     (let ((nntp-server-buffer (current-buffer)))
7572                       (setq header (car (gnus-get-newsgroup-headers
7573                                          (save-excursion
7574                                            (set-buffer gnus-summary-buffer)
7575                                            gnus-newsgroup-dependencies)
7576                                          t))))
7577                     (save-excursion
7578                       (set-buffer gnus-summary-buffer)
7579                       (gnus-data-set-header
7580                        (gnus-data-find (cdr gnus-article-current))
7581                        header)
7582                       (gnus-summary-update-article-line
7583                        (cdr gnus-article-current) header))))))
7584           ;; Update threads.
7585           (set-buffer (or buffer gnus-summary-buffer))
7586           (gnus-summary-update-article (cdr gnus-article-current)))
7587         ;; Prettify the article buffer again.
7588         (unless no-highlight
7589           (save-excursion
7590             (set-buffer gnus-article-buffer)
7591             (gnus-run-hooks 'gnus-article-display-hook)
7592             (set-buffer gnus-original-article-buffer)
7593             (gnus-request-article
7594              (cdr gnus-article-current)
7595              (car gnus-article-current) (current-buffer))))
7596         ;; Prettify the summary buffer line.
7597         (when (gnus-visual-p 'summary-highlight 'highlight)
7598           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7599
7600 (defun gnus-summary-edit-wash (key)
7601   "Perform editing command KEY in the article buffer."
7602   (interactive
7603    (list
7604     (progn
7605       (message "%s" (concat (this-command-keys) "- "))
7606       (read-char))))
7607   (message "")
7608   (gnus-summary-edit-article)
7609   (execute-kbd-macro (concat (this-command-keys) key))
7610   (gnus-article-edit-done))
7611
7612 ;;; Respooling
7613
7614 (defun gnus-summary-respool-query (&optional silent trace)
7615   "Query where the respool algorithm would put this article."
7616   (interactive)
7617   (let (gnus-mark-article-hook)
7618     (gnus-summary-select-article)
7619     (save-excursion
7620       (set-buffer gnus-original-article-buffer)
7621       (save-restriction
7622         (message-narrow-to-head)
7623         (let ((groups (nnmail-article-group 'identity trace)))
7624           (unless silent
7625             (if groups
7626                 (message "This message would go to %s"
7627                          (mapconcat 'car groups ", "))
7628               (message "This message would go to no groups"))
7629             groups))))))
7630
7631 (defun gnus-summary-respool-trace ()
7632   "Trace where the respool algorithm would put this article.
7633 Display a buffer showing all fancy splitting patterns which matched."
7634   (interactive)
7635   (gnus-summary-respool-query nil t))
7636
7637 ;; Summary marking commands.
7638
7639 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7640   "Mark articles which has the same subject as read, and then select the next.
7641 If UNMARK is positive, remove any kind of mark.
7642 If UNMARK is negative, tick articles."
7643   (interactive "P")
7644   (when unmark
7645     (setq unmark (prefix-numeric-value unmark)))
7646   (let ((count
7647          (gnus-summary-mark-same-subject
7648           (gnus-summary-article-subject) unmark)))
7649     ;; Select next unread article.  If auto-select-same mode, should
7650     ;; select the first unread article.
7651     (gnus-summary-next-article t (and gnus-auto-select-same
7652                                       (gnus-summary-article-subject)))
7653     (gnus-message 7 "%d article%s marked as %s"
7654                   count (if (= count 1) " is" "s are")
7655                   (if unmark "unread" "read"))))
7656
7657 (defun gnus-summary-kill-same-subject (&optional unmark)
7658   "Mark articles which has the same subject as read.
7659 If UNMARK is positive, remove any kind of mark.
7660 If UNMARK is negative, tick articles."
7661   (interactive "P")
7662   (when unmark
7663     (setq unmark (prefix-numeric-value unmark)))
7664   (let ((count
7665          (gnus-summary-mark-same-subject
7666           (gnus-summary-article-subject) unmark)))
7667     ;; If marked as read, go to next unread subject.
7668     (when (null unmark)
7669       ;; Go to next unread subject.
7670       (gnus-summary-next-subject 1 t))
7671     (gnus-message 7 "%d articles are marked as %s"
7672                   count (if unmark "unread" "read"))))
7673
7674 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7675   "Mark articles with same SUBJECT as read, and return marked number.
7676 If optional argument UNMARK is positive, remove any kinds of marks.
7677 If optional argument UNMARK is negative, mark articles as unread instead."
7678   (let ((count 1))
7679     (save-excursion
7680       (cond
7681        ((null unmark)                   ; Mark as read.
7682         (while (and
7683                 (progn
7684                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7685                   (gnus-summary-show-thread) t)
7686                 (gnus-summary-find-subject subject))
7687           (setq count (1+ count))))
7688        ((> unmark 0)                    ; Tick.
7689         (while (and
7690                 (progn
7691                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7692                   (gnus-summary-show-thread) t)
7693                 (gnus-summary-find-subject subject))
7694           (setq count (1+ count))))
7695        (t                               ; Mark as unread.
7696         (while (and
7697                 (progn
7698                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7699                   (gnus-summary-show-thread) t)
7700                 (gnus-summary-find-subject subject))
7701           (setq count (1+ count)))))
7702       (gnus-set-mode-line 'summary)
7703       ;; Return the number of marked articles.
7704       count)))
7705
7706 (defun gnus-summary-mark-as-processable (n &optional unmark)
7707   "Set the process mark on the next N articles.
7708 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7709 the process mark instead.  The difference between N and the actual
7710 number of articles marked is returned."
7711   (interactive "p")
7712   (let ((backward (< n 0))
7713         (n (abs n)))
7714     (while (and
7715             (> n 0)
7716             (if unmark
7717                 (gnus-summary-remove-process-mark
7718                  (gnus-summary-article-number))
7719               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7720             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7721       (setq n (1- n)))
7722     (when (/= 0 n)
7723       (gnus-message 7 "No more articles"))
7724     (gnus-summary-recenter)
7725     (gnus-summary-position-point)
7726     n))
7727
7728 (defun gnus-summary-unmark-as-processable (n)
7729   "Remove the process mark from the next N articles.
7730 If N is negative, unmark backward instead.  The difference between N and
7731 the actual number of articles unmarked is returned."
7732   (interactive "p")
7733   (gnus-summary-mark-as-processable n t))
7734
7735 (defun gnus-summary-unmark-all-processable ()
7736   "Remove the process mark from all articles."
7737   (interactive)
7738   (save-excursion
7739     (while gnus-newsgroup-processable
7740       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7741   (gnus-summary-position-point))
7742
7743 (defun gnus-summary-mark-as-expirable (n)
7744   "Mark N articles forward as expirable.
7745 If N is negative, mark backward instead.  The difference between N and
7746 the actual number of articles marked is returned."
7747   (interactive "p")
7748   (gnus-summary-mark-forward n gnus-expirable-mark))
7749
7750 (defun gnus-summary-mark-article-as-replied (article)
7751   "Mark ARTICLE replied and update the summary line."
7752   (push article gnus-newsgroup-replied)
7753   (let ((buffer-read-only nil))
7754     (when (gnus-summary-goto-subject article nil t)
7755       (gnus-summary-update-secondary-mark article))))
7756
7757 (defun gnus-summary-set-bookmark (article)
7758   "Set a bookmark in current article."
7759   (interactive (list (gnus-summary-article-number)))
7760   (when (or (not (get-buffer gnus-article-buffer))
7761             (not gnus-current-article)
7762             (not gnus-article-current)
7763             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7764     (error "No current article selected"))
7765   ;; Remove old bookmark, if one exists.
7766   (let ((old (assq article gnus-newsgroup-bookmarks)))
7767     (when old
7768       (setq gnus-newsgroup-bookmarks
7769             (delq old gnus-newsgroup-bookmarks))))
7770   ;; Set the new bookmark, which is on the form
7771   ;; (article-number . line-number-in-body).
7772   (push
7773    (cons article
7774          (save-excursion
7775            (set-buffer gnus-article-buffer)
7776            (count-lines
7777             (min (point)
7778                  (save-excursion
7779                    (goto-char (point-min))
7780                    (search-forward "\n\n" nil t)
7781                    (point)))
7782             (point))))
7783    gnus-newsgroup-bookmarks)
7784   (gnus-message 6 "A bookmark has been added to the current article."))
7785
7786 (defun gnus-summary-remove-bookmark (article)
7787   "Remove the bookmark from the current article."
7788   (interactive (list (gnus-summary-article-number)))
7789   ;; Remove old bookmark, if one exists.
7790   (let ((old (assq article gnus-newsgroup-bookmarks)))
7791     (if old
7792         (progn
7793           (setq gnus-newsgroup-bookmarks
7794                 (delq old gnus-newsgroup-bookmarks))
7795           (gnus-message 6 "Removed bookmark."))
7796       (gnus-message 6 "No bookmark in current article."))))
7797
7798 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7799 (defun gnus-summary-mark-as-dormant (n)
7800   "Mark N articles forward as dormant.
7801 If N is negative, mark backward instead.  The difference between N and
7802 the actual number of articles marked is returned."
7803   (interactive "p")
7804   (gnus-summary-mark-forward n gnus-dormant-mark))
7805
7806 (defun gnus-summary-set-process-mark (article)
7807   "Set the process mark on ARTICLE and update the summary line."
7808   (setq gnus-newsgroup-processable
7809         (cons article
7810               (delq article gnus-newsgroup-processable)))
7811   (when (gnus-summary-goto-subject article)
7812     (gnus-summary-show-thread)
7813     (gnus-summary-goto-subject article)
7814     (gnus-summary-update-secondary-mark article)))
7815
7816 (defun gnus-summary-remove-process-mark (article)
7817   "Remove the process mark from ARTICLE and update the summary line."
7818   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7819   (when (gnus-summary-goto-subject article)
7820     (gnus-summary-show-thread)
7821     (gnus-summary-goto-subject article)
7822     (gnus-summary-update-secondary-mark article)))
7823
7824 (defun gnus-summary-set-saved-mark (article)
7825   "Set the process mark on ARTICLE and update the summary line."
7826   (push article gnus-newsgroup-saved)
7827   (when (gnus-summary-goto-subject article)
7828     (gnus-summary-update-secondary-mark article)))
7829
7830 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7831   "Mark N articles as read forwards.
7832 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7833 The difference between N and the actual number of articles marked is
7834 returned."
7835   (interactive "p")
7836   (gnus-summary-show-thread)
7837   (let ((backward (< n 0))
7838         (gnus-summary-goto-unread
7839          (and gnus-summary-goto-unread
7840               (not (eq gnus-summary-goto-unread 'never))
7841               (not (memq mark (list gnus-unread-mark
7842                                     gnus-ticked-mark gnus-dormant-mark)))))
7843         (n (abs n))
7844         (mark (or mark gnus-del-mark)))
7845     (while (and (> n 0)
7846                 (gnus-summary-mark-article nil mark no-expire)
7847                 (zerop (gnus-summary-next-subject
7848                         (if backward -1 1)
7849                         (and gnus-summary-goto-unread
7850                              (not (eq gnus-summary-goto-unread 'never)))
7851                         t)))
7852       (setq n (1- n)))
7853     (when (/= 0 n)
7854       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7855     (gnus-summary-recenter)
7856     (gnus-summary-position-point)
7857     (gnus-set-mode-line 'summary)
7858     n))
7859
7860 (defun gnus-summary-mark-article-as-read (mark)
7861   "Mark the current article quickly as read with MARK."
7862   (let ((article (gnus-summary-article-number)))
7863     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7864     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7865     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7866     (push (cons article mark) gnus-newsgroup-reads)
7867     ;; Possibly remove from cache, if that is used.
7868     (when gnus-use-cache
7869       (gnus-cache-enter-remove-article article))
7870     ;; Allow the backend to change the mark.
7871     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7872     ;; Check for auto-expiry.
7873     (when (and gnus-newsgroup-auto-expire
7874                (memq mark gnus-auto-expirable-marks))
7875       (setq mark gnus-expirable-mark)
7876       ;; Let the backend know about the mark change.
7877       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7878       (push article gnus-newsgroup-expirable))
7879     ;; Set the mark in the buffer.
7880     (gnus-summary-update-mark mark 'unread)
7881     t))
7882
7883 (defun gnus-summary-mark-article-as-unread (mark)
7884   "Mark the current article quickly as unread with MARK."
7885   (let* ((article (gnus-summary-article-number))
7886          (old-mark (gnus-summary-article-mark article)))
7887     ;; Allow the backend to change the mark.
7888     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7889     (if (eq mark old-mark)
7890         t
7891       (if (<= article 0)
7892           (progn
7893             (gnus-error 1 "Can't mark negative article numbers")
7894             nil)
7895         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7896         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7897         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7898         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7899         (cond ((= mark gnus-ticked-mark)
7900                (push article gnus-newsgroup-marked))
7901               ((= mark gnus-dormant-mark)
7902                (push article gnus-newsgroup-dormant))
7903               (t
7904                (push article gnus-newsgroup-unreads)))
7905         (gnus-pull article gnus-newsgroup-reads)
7906
7907         ;; See whether the article is to be put in the cache.
7908         (and gnus-use-cache
7909              (vectorp (gnus-summary-article-header article))
7910              (save-excursion
7911                (gnus-cache-possibly-enter-article
7912                 gnus-newsgroup-name article
7913                 (gnus-summary-article-header article)
7914                 (= mark gnus-ticked-mark)
7915                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7916
7917         ;; Fix the mark.
7918         (gnus-summary-update-mark mark 'unread)
7919         t))))
7920
7921 (defun gnus-summary-mark-article (&optional article mark no-expire)
7922   "Mark ARTICLE with MARK.  MARK can be any character.
7923 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7924 `??' (dormant) and `?E' (expirable).
7925 If MARK is nil, then the default character `?r' is used.
7926 If ARTICLE is nil, then the article on the current line will be
7927 marked."
7928   ;; The mark might be a string.
7929   (when (stringp mark)
7930     (setq mark (aref mark 0)))
7931   ;; If no mark is given, then we check auto-expiring.
7932   (when (null mark)
7933     (setq mark gnus-del-mark))
7934   (when (and (not no-expire)
7935              gnus-newsgroup-auto-expire
7936              (memq mark gnus-auto-expirable-marks))
7937     (setq mark gnus-expirable-mark))
7938   (let ((article (or article (gnus-summary-article-number)))
7939         (old-mark (gnus-summary-article-mark article)))
7940     ;; Allow the backend to change the mark.
7941     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7942     (if (eq mark old-mark)
7943         t
7944       (unless article
7945         (error "No article on current line"))
7946       (if (not (if (or (= mark gnus-unread-mark)
7947                        (= mark gnus-ticked-mark)
7948                        (= mark gnus-dormant-mark))
7949                    (gnus-mark-article-as-unread article mark)
7950                  (gnus-mark-article-as-read article mark)))
7951           t
7952         ;; See whether the article is to be put in the cache.
7953         (and gnus-use-cache
7954              (not (= mark gnus-canceled-mark))
7955              (vectorp (gnus-summary-article-header article))
7956              (save-excursion
7957                (gnus-cache-possibly-enter-article
7958                 gnus-newsgroup-name article
7959                 (gnus-summary-article-header article)
7960                 (= mark gnus-ticked-mark)
7961                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7962
7963         (when (gnus-summary-goto-subject article nil t)
7964           (let ((buffer-read-only nil))
7965             (gnus-summary-show-thread)
7966             ;; Fix the mark.
7967             (gnus-summary-update-mark mark 'unread)
7968             t))))))
7969
7970 (defun gnus-summary-update-secondary-mark (article)
7971   "Update the secondary (read, process, cache) mark."
7972   (gnus-summary-update-mark
7973    (cond ((memq article gnus-newsgroup-processable)
7974           gnus-process-mark)
7975          ((memq article gnus-newsgroup-cached)
7976           gnus-cached-mark)
7977          ((memq article gnus-newsgroup-replied)
7978           gnus-replied-mark)
7979          ((memq article gnus-newsgroup-saved)
7980           gnus-saved-mark)
7981          (t gnus-unread-mark))
7982    'replied)
7983   (when (gnus-visual-p 'summary-highlight 'highlight)
7984     (gnus-run-hooks 'gnus-summary-update-hook))
7985   t)
7986
7987 (defun gnus-summary-update-mark (mark type)
7988   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7989         (buffer-read-only nil))
7990     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7991     (when forward
7992       (when (looking-at "\r")
7993         (incf forward))
7994       (when (<= (+ forward (point)) (point-max))
7995         ;; Go to the right position on the line.
7996         (goto-char (+ forward (point)))
7997         ;; Replace the old mark with the new mark.
7998         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
7999         ;; Optionally update the marks by some user rule.
8000         (when (eq type 'unread)
8001           (gnus-data-set-mark
8002            (gnus-data-find (gnus-summary-article-number)) mark)
8003           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8004
8005 (defun gnus-mark-article-as-read (article &optional mark)
8006   "Enter ARTICLE in the pertinent lists and remove it from others."
8007   ;; Make the article expirable.
8008   (let ((mark (or mark gnus-del-mark)))
8009     (if (= mark gnus-expirable-mark)
8010         (push article gnus-newsgroup-expirable)
8011       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8012     ;; Remove from unread and marked lists.
8013     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8014     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8015     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8016     (push (cons article mark) gnus-newsgroup-reads)
8017     ;; Possibly remove from cache, if that is used.
8018     (when gnus-use-cache
8019       (gnus-cache-enter-remove-article article))
8020     t))
8021
8022 (defun gnus-mark-article-as-unread (article &optional mark)
8023   "Enter ARTICLE in the pertinent lists and remove it from others."
8024   (let ((mark (or mark gnus-ticked-mark)))
8025     (if (<= article 0)
8026         (progn
8027           (gnus-error 1 "Can't mark negative article numbers")
8028           nil)
8029       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8030             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8031             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8032             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8033
8034       ;; Unsuppress duplicates?
8035       (when gnus-suppress-duplicates
8036         (gnus-dup-unsuppress-article article))
8037
8038       (cond ((= mark gnus-ticked-mark)
8039              (push article gnus-newsgroup-marked))
8040             ((= mark gnus-dormant-mark)
8041              (push article gnus-newsgroup-dormant))
8042             (t
8043              (push article gnus-newsgroup-unreads)))
8044       (gnus-pull article gnus-newsgroup-reads)
8045       t)))
8046
8047 (defalias 'gnus-summary-mark-as-unread-forward
8048   'gnus-summary-tick-article-forward)
8049 (make-obsolete 'gnus-summary-mark-as-unread-forward
8050                'gnus-summary-tick-article-forward)
8051 (defun gnus-summary-tick-article-forward (n)
8052   "Tick N articles forwards.
8053 If N is negative, tick backwards instead.
8054 The difference between N and the number of articles ticked is returned."
8055   (interactive "p")
8056   (gnus-summary-mark-forward n gnus-ticked-mark))
8057
8058 (defalias 'gnus-summary-mark-as-unread-backward
8059   'gnus-summary-tick-article-backward)
8060 (make-obsolete 'gnus-summary-mark-as-unread-backward
8061                'gnus-summary-tick-article-backward)
8062 (defun gnus-summary-tick-article-backward (n)
8063   "Tick N articles backwards.
8064 The difference between N and the number of articles ticked is returned."
8065   (interactive "p")
8066   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8067
8068 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8069 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8070 (defun gnus-summary-tick-article (&optional article clear-mark)
8071   "Mark current article as unread.
8072 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8073 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8074   (interactive)
8075   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8076                                        gnus-ticked-mark)))
8077
8078 (defun gnus-summary-mark-as-read-forward (n)
8079   "Mark N articles as read forwards.
8080 If N is negative, mark backwards instead.
8081 The difference between N and the actual number of articles marked is
8082 returned."
8083   (interactive "p")
8084   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8085
8086 (defun gnus-summary-mark-as-read-backward (n)
8087   "Mark the N articles as read backwards.
8088 The difference between N and the actual number of articles marked is
8089 returned."
8090   (interactive "p")
8091   (gnus-summary-mark-forward (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8092
8093 (defun gnus-summary-mark-as-read (&optional article mark)
8094   "Mark current article as read.
8095 ARTICLE specifies the article to be marked as read.
8096 MARK specifies a string to be inserted at the beginning of the line."
8097   (gnus-summary-mark-article article mark))
8098
8099 (defun gnus-summary-clear-mark-forward (n)
8100   "Clear marks from N articles forward.
8101 If N is negative, clear backward instead.
8102 The difference between N and the number of marks cleared is returned."
8103   (interactive "p")
8104   (gnus-summary-mark-forward n gnus-unread-mark))
8105
8106 (defun gnus-summary-clear-mark-backward (n)
8107   "Clear marks from N articles backward.
8108 The difference between N and the number of marks cleared is returned."
8109   (interactive "p")
8110   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8111
8112 (defun gnus-summary-mark-unread-as-read ()
8113   "Intended to be used by `gnus-summary-mark-article-hook'."
8114   (when (memq gnus-current-article gnus-newsgroup-unreads)
8115     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8116
8117 (defun gnus-summary-mark-read-and-unread-as-read ()
8118   "Intended to be used by `gnus-summary-mark-article-hook'."
8119   (let ((mark (gnus-summary-article-mark)))
8120     (when (or (gnus-unread-mark-p mark)
8121               (gnus-read-mark-p mark))
8122       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8123
8124 (defun gnus-summary-mark-region-as-read (point mark all)
8125   "Mark all unread articles between point and mark as read.
8126 If given a prefix, mark all articles between point and mark as read,
8127 even ticked and dormant ones."
8128   (interactive "r\nP")
8129   (save-excursion
8130     (let (article)
8131       (goto-char point)
8132       (beginning-of-line)
8133       (while (and
8134               (< (point) mark)
8135               (progn
8136                 (when (or all
8137                           (memq (setq article (gnus-summary-article-number))
8138                                 gnus-newsgroup-unreads))
8139                   (gnus-summary-mark-article article gnus-del-mark))
8140                 t)
8141               (gnus-summary-find-next))))))
8142
8143 (defun gnus-summary-mark-below (score mark)
8144   "Mark articles with score less than SCORE with MARK."
8145   (interactive "P\ncMark: ")
8146   (setq score (if score
8147                   (prefix-numeric-value score)
8148                 (or gnus-summary-default-score 0)))
8149   (save-excursion
8150     (set-buffer gnus-summary-buffer)
8151     (goto-char (point-min))
8152     (while
8153         (progn
8154           (and (< (gnus-summary-article-score) score)
8155                (gnus-summary-mark-article nil mark))
8156           (gnus-summary-find-next)))))
8157
8158 (defun gnus-summary-kill-below (&optional score)
8159   "Mark articles with score below SCORE as read."
8160   (interactive "P")
8161   (gnus-summary-mark-below score gnus-killed-mark))
8162
8163 (defun gnus-summary-clear-above (&optional score)
8164   "Clear all marks from articles with score above SCORE."
8165   (interactive "P")
8166   (gnus-summary-mark-above score gnus-unread-mark))
8167
8168 (defun gnus-summary-tick-above (&optional score)
8169   "Tick all articles with score above SCORE."
8170   (interactive "P")
8171   (gnus-summary-mark-above score gnus-ticked-mark))
8172
8173 (defun gnus-summary-mark-above (score mark)
8174   "Mark articles with score over SCORE with MARK."
8175   (interactive "P\ncMark: ")
8176   (setq score (if score
8177                   (prefix-numeric-value score)
8178                 (or gnus-summary-default-score 0)))
8179   (save-excursion
8180     (set-buffer gnus-summary-buffer)
8181     (goto-char (point-min))
8182     (while (and (progn
8183                   (when (> (gnus-summary-article-score) score)
8184                     (gnus-summary-mark-article nil mark))
8185                   t)
8186                 (gnus-summary-find-next)))))
8187
8188 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8189 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8190 (defun gnus-summary-limit-include-expunged (&optional no-error)
8191   "Display all the hidden articles that were expunged for low scores."
8192   (interactive)
8193   (let ((buffer-read-only nil))
8194     (let ((scored gnus-newsgroup-scored)
8195           headers h)
8196       (while scored
8197         (unless (gnus-summary-goto-subject (caar scored))
8198           (and (setq h (gnus-summary-article-header (caar scored)))
8199                (< (cdar scored) gnus-summary-expunge-below)
8200                (push h headers)))
8201         (setq scored (cdr scored)))
8202       (if (not headers)
8203           (when (not no-error)
8204             (error "No expunged articles hidden"))
8205         (goto-char (point-min))
8206         (gnus-summary-prepare-unthreaded (nreverse headers))
8207         (goto-char (point-min))
8208         (gnus-summary-position-point)
8209         t))))
8210
8211 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8212   "Mark all unread articles in this newsgroup as read.
8213 If prefix argument ALL is non-nil, ticked and dormant articles will
8214 also be marked as read.
8215 If QUIETLY is non-nil, no questions will be asked.
8216 If TO-HERE is non-nil, it should be a point in the buffer.  All
8217 articles before this point will be marked as read.
8218 Note that this function will only catch up the unread article
8219 in the current summary buffer limitation.
8220 The number of articles marked as read is returned."
8221   (interactive "P")
8222   (prog1
8223       (save-excursion
8224         (when (or quietly
8225                   (not gnus-interactive-catchup) ;Without confirmation?
8226                   gnus-expert-user
8227                   (gnus-y-or-n-p
8228                    (if all
8229                        "Mark absolutely all articles as read? "
8230                      "Mark all unread articles as read? ")))
8231           (if (and not-mark
8232                    (not gnus-newsgroup-adaptive)
8233                    (not gnus-newsgroup-auto-expire)
8234                    (not gnus-suppress-duplicates)
8235                    (or (not gnus-use-cache)
8236                        (eq gnus-use-cache 'passive)))
8237               (progn
8238                 (when all
8239                   (setq gnus-newsgroup-marked nil
8240                         gnus-newsgroup-dormant nil))
8241                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8242             ;; We actually mark all articles as canceled, which we
8243             ;; have to do when using auto-expiry or adaptive scoring.
8244             (gnus-summary-show-all-threads)
8245             (when (gnus-summary-first-subject (not all) t)
8246               (while (and
8247                       (if to-here (< (point) to-here) t)
8248                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8249                       (gnus-summary-find-next (not all) nil nil t))))
8250             (gnus-set-mode-line 'summary))
8251           t))
8252     (gnus-summary-position-point)))
8253
8254 (defun gnus-summary-catchup-to-here (&optional all)
8255   "Mark all unticked articles before the current one as read.
8256 If ALL is non-nil, also mark ticked and dormant articles as read."
8257   (interactive "P")
8258   (save-excursion
8259     (gnus-save-hidden-threads
8260       (let ((beg (point)))
8261         ;; We check that there are unread articles.
8262         (when (or all (gnus-summary-find-prev))
8263           (gnus-summary-catchup all t beg)))))
8264   (gnus-summary-position-point))
8265
8266 (defun gnus-summary-catchup-all (&optional quietly)
8267   "Mark all articles in this newsgroup as read."
8268   (interactive "P")
8269   (gnus-summary-catchup t quietly))
8270
8271 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8272   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8273 If prefix argument ALL is non-nil, all articles are marked as read."
8274   (interactive "P")
8275   (when (gnus-summary-catchup all quietly nil 'fast)
8276     ;; Select next newsgroup or exit.
8277     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8278              (eq gnus-auto-select-next 'quietly))
8279         (gnus-summary-next-group nil)
8280       (gnus-summary-exit))))
8281
8282 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8283   "Mark all articles in this newsgroup as read, and then exit."
8284   (interactive "P")
8285   (gnus-summary-catchup-and-exit t quietly))
8286
8287 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8288 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8289   "Mark all articles in this group as read and select the next group.
8290 If given a prefix, mark all articles, unread as well as ticked, as
8291 read."
8292   (interactive "P")
8293   (save-excursion
8294     (gnus-summary-catchup all))
8295   (gnus-summary-next-article t nil nil t))
8296
8297 ;; Thread-based commands.
8298
8299 (defun gnus-summary-articles-in-thread (&optional article)
8300   "Return a list of all articles in the current thread.
8301 If ARTICLE is non-nil, return all articles in the thread that starts
8302 with that article."
8303   (let* ((article (or article (gnus-summary-article-number)))
8304          (data (gnus-data-find-list article))
8305          (top-level (gnus-data-level (car data)))
8306          (top-subject
8307           (cond ((null gnus-thread-operation-ignore-subject)
8308                  (gnus-simplify-subject-re
8309                   (mail-header-subject (gnus-data-header (car data)))))
8310                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8311                  (gnus-simplify-subject-fuzzy
8312                   (mail-header-subject (gnus-data-header (car data)))))
8313                 (t nil)))
8314          (end-point (save-excursion
8315                       (if (gnus-summary-go-to-next-thread)
8316                           (point) (point-max))))
8317          articles)
8318     (while (and data
8319                 (< (gnus-data-pos (car data)) end-point))
8320       (when (or (not top-subject)
8321                 (string= top-subject
8322                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8323                              (gnus-simplify-subject-fuzzy
8324                               (mail-header-subject
8325                                (gnus-data-header (car data))))
8326                            (gnus-simplify-subject-re
8327                             (mail-header-subject
8328                              (gnus-data-header (car data)))))))
8329         (push (gnus-data-number (car data)) articles))
8330       (unless (and (setq data (cdr data))
8331                    (> (gnus-data-level (car data)) top-level))
8332         (setq data nil)))
8333     ;; Return the list of articles.
8334     (nreverse articles)))
8335
8336 (defun gnus-summary-rethread-current ()
8337   "Rethread the thread the current article is part of."
8338   (interactive)
8339   (let* ((gnus-show-threads t)
8340          (article (gnus-summary-article-number))
8341          (id (mail-header-id (gnus-summary-article-header)))
8342          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8343     (unless id
8344       (error "No article on the current line"))
8345     (gnus-rebuild-thread id)
8346     (gnus-summary-goto-subject article)))
8347
8348 (defun gnus-summary-reparent-thread ()
8349   "Make the current article child of the marked (or previous) article.
8350
8351 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8352 is non-nil or the Subject: of both articles are the same."
8353   (interactive)
8354   (unless (not (gnus-group-read-only-p))
8355     (error "The current newsgroup does not support article editing"))
8356   (unless (<= (length gnus-newsgroup-processable) 1)
8357     (error "No more than one article may be marked"))
8358   (save-window-excursion
8359     (let ((gnus-article-buffer " *reparent*")
8360           (current-article (gnus-summary-article-number))
8361           ;; First grab the marked article, otherwise one line up.
8362           (parent-article (if (not (null gnus-newsgroup-processable))
8363                               (car gnus-newsgroup-processable)
8364                             (save-excursion
8365                               (if (eq (forward-line -1) 0)
8366                                   (gnus-summary-article-number)
8367                                 (error "Beginning of summary buffer"))))))
8368       (unless (not (eq current-article parent-article))
8369         (error "An article may not be self-referential"))
8370       (let ((message-id (mail-header-id
8371                          (gnus-summary-article-header parent-article))))
8372         (unless (and message-id (not (equal message-id "")))
8373           (error "No message-id in desired parent"))
8374         (gnus-with-article current-article
8375           (goto-char (point-min))
8376           (if (re-search-forward "^References: " nil t)
8377               (progn
8378                 (re-search-forward "^[^ \t]" nil t)
8379                 (forward-line -1)
8380                 (end-of-line)
8381                 (insert " " message-id))
8382             (insert "References: " message-id "\n")))
8383         (set-buffer gnus-summary-buffer)
8384         (gnus-summary-unmark-all-processable)
8385         (gnus-summary-update-article current-article)
8386         (gnus-summary-rethread-current)
8387         (gnus-message 3 "Article %d is now the child of article %d"
8388                       current-article parent-article)))))
8389
8390 (defun gnus-summary-toggle-threads (&optional arg)
8391   "Toggle showing conversation threads.
8392 If ARG is positive number, turn showing conversation threads on."
8393   (interactive "P")
8394   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8395     (setq gnus-show-threads
8396           (if (null arg) (not gnus-show-threads)
8397             (> (prefix-numeric-value arg) 0)))
8398     (gnus-summary-prepare)
8399     (gnus-summary-goto-subject current)
8400     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8401     (gnus-summary-position-point)))
8402
8403 (defun gnus-summary-show-all-threads ()
8404   "Show all threads."
8405   (interactive)
8406   (save-excursion
8407     (let ((buffer-read-only nil))
8408       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8409   (gnus-summary-position-point))
8410
8411 (defun gnus-summary-show-thread ()
8412   "Show thread subtrees.
8413 Returns nil if no thread was there to be shown."
8414   (interactive)
8415   (let ((buffer-read-only nil)
8416         (orig (point))
8417         ;; first goto end then to beg, to have point at beg after let
8418         (end (progn (end-of-line) (point)))
8419         (beg (progn (beginning-of-line) (point))))
8420     (prog1
8421         ;; Any hidden lines here?
8422         (search-forward "\r" end t)
8423       (subst-char-in-region beg end ?\^M ?\n t)
8424       (goto-char orig)
8425       (gnus-summary-position-point))))
8426
8427 (defun gnus-summary-hide-all-threads ()
8428   "Hide all thread subtrees."
8429   (interactive)
8430   (save-excursion
8431     (goto-char (point-min))
8432     (gnus-summary-hide-thread)
8433     (while (zerop (gnus-summary-next-thread 1 t))
8434       (gnus-summary-hide-thread)))
8435   (gnus-summary-position-point))
8436
8437 (defun gnus-summary-hide-thread ()
8438   "Hide thread subtrees.
8439 Returns nil if no threads were there to be hidden."
8440   (interactive)
8441   (let ((buffer-read-only nil)
8442         (start (point))
8443         (article (gnus-summary-article-number)))
8444     (goto-char start)
8445     ;; Go forward until either the buffer ends or the subthread
8446     ;; ends.
8447     (when (and (not (eobp))
8448                (or (zerop (gnus-summary-next-thread 1 t))
8449                    (goto-char (point-max))))
8450       (prog1
8451           (if (and (> (point) start)
8452                    (search-backward "\n" start t))
8453               (progn
8454                 (subst-char-in-region start (point) ?\n ?\^M)
8455                 (gnus-summary-goto-subject article))
8456             (goto-char start)
8457             nil)
8458         ;;(gnus-summary-position-point)
8459         ))))
8460
8461 (defun gnus-summary-go-to-next-thread (&optional previous)
8462   "Go to the same level (or less) next thread.
8463 If PREVIOUS is non-nil, go to previous thread instead.
8464 Return the article number moved to, or nil if moving was impossible."
8465   (let ((level (gnus-summary-thread-level))
8466         (way (if previous -1 1))
8467         (beg (point)))
8468     (forward-line way)
8469     (while (and (not (eobp))
8470                 (< level (gnus-summary-thread-level)))
8471       (forward-line way))
8472     (if (eobp)
8473         (progn
8474           (goto-char beg)
8475           nil)
8476       (setq beg (point))
8477       (prog1
8478           (gnus-summary-article-number)
8479         (goto-char beg)))))
8480
8481 (defun gnus-summary-next-thread (n &optional silent)
8482   "Go to the same level next N'th thread.
8483 If N is negative, search backward instead.
8484 Returns the difference between N and the number of skips actually
8485 done.
8486
8487 If SILENT, don't output messages."
8488   (interactive "p")
8489   (let ((backward (< n 0))
8490         (n (abs n)))
8491     (while (and (> n 0)
8492                 (gnus-summary-go-to-next-thread backward))
8493       (decf n))
8494     (unless silent
8495       (gnus-summary-position-point))
8496     (when (and (not silent) (/= 0 n))
8497       (gnus-message 7 "No more threads"))
8498     n))
8499
8500 (defun gnus-summary-prev-thread (n)
8501   "Go to the same level previous N'th thread.
8502 Returns the difference between N and the number of skips actually
8503 done."
8504   (interactive "p")
8505   (gnus-summary-next-thread (- n)))
8506
8507 (defun gnus-summary-go-down-thread ()
8508   "Go down one level in the current thread."
8509   (let ((children (gnus-summary-article-children)))
8510     (when children
8511       (gnus-summary-goto-subject (car children)))))
8512
8513 (defun gnus-summary-go-up-thread ()
8514   "Go up one level in the current thread."
8515   (let ((parent (gnus-summary-article-parent)))
8516     (when parent
8517       (gnus-summary-goto-subject parent))))
8518
8519 (defun gnus-summary-down-thread (n)
8520   "Go down thread N steps.
8521 If N is negative, go up instead.
8522 Returns the difference between N and how many steps down that were
8523 taken."
8524   (interactive "p")
8525   (let ((up (< n 0))
8526         (n (abs n)))
8527     (while (and (> n 0)
8528                 (if up (gnus-summary-go-up-thread)
8529                   (gnus-summary-go-down-thread)))
8530       (setq n (1- n)))
8531     (gnus-summary-position-point)
8532     (when (/= 0 n)
8533       (gnus-message 7 "Can't go further"))
8534     n))
8535
8536 (defun gnus-summary-up-thread (n)
8537   "Go up thread N steps.
8538 If N is negative, go up instead.
8539 Returns the difference between N and how many steps down that were
8540 taken."
8541   (interactive "p")
8542   (gnus-summary-down-thread (- n)))
8543
8544 (defun gnus-summary-top-thread ()
8545   "Go to the top of the thread."
8546   (interactive)
8547   (while (gnus-summary-go-up-thread))
8548   (gnus-summary-article-number))
8549
8550 (defun gnus-summary-kill-thread (&optional unmark)
8551   "Mark articles under current thread as read.
8552 If the prefix argument is positive, remove any kinds of marks.
8553 If the prefix argument is negative, tick articles instead."
8554   (interactive "P")
8555   (when unmark
8556     (setq unmark (prefix-numeric-value unmark)))
8557   (let ((articles (gnus-summary-articles-in-thread)))
8558     (save-excursion
8559       ;; Expand the thread.
8560       (gnus-summary-show-thread)
8561       ;; Mark all the articles.
8562       (while articles
8563         (gnus-summary-goto-subject (car articles))
8564         (cond ((null unmark)
8565                (gnus-summary-mark-article-as-read gnus-killed-mark))
8566               ((> unmark 0)
8567                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8568               (t
8569                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8570         (setq articles (cdr articles))))
8571     ;; Hide killed subtrees.
8572     (and (null unmark)
8573          gnus-thread-hide-killed
8574          (gnus-summary-hide-thread))
8575     ;; If marked as read, go to next unread subject.
8576     (when (null unmark)
8577       ;; Go to next unread subject.
8578       (gnus-summary-next-subject 1 t)))
8579   (gnus-set-mode-line 'summary))
8580
8581 ;; Summary sorting commands
8582
8583 (defun gnus-summary-sort-by-number (&optional reverse)
8584   "Sort the summary buffer by article number.
8585 Argument REVERSE means reverse order."
8586   (interactive "P")
8587   (gnus-summary-sort 'number reverse))
8588
8589 (defun gnus-summary-sort-by-author (&optional reverse)
8590   "Sort the summary buffer by author name alphabetically.
8591 If case-fold-search is non-nil, case of letters is ignored.
8592 Argument REVERSE means reverse order."
8593   (interactive "P")
8594   (gnus-summary-sort 'author reverse))
8595
8596 (defun gnus-summary-sort-by-subject (&optional reverse)
8597   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8598 If case-fold-search is non-nil, case of letters is ignored.
8599 Argument REVERSE means reverse order."
8600   (interactive "P")
8601   (gnus-summary-sort 'subject reverse))
8602
8603 (defun gnus-summary-sort-by-date (&optional reverse)
8604   "Sort the summary buffer by date.
8605 Argument REVERSE means reverse order."
8606   (interactive "P")
8607   (gnus-summary-sort 'date reverse))
8608
8609 (defun gnus-summary-sort-by-score (&optional reverse)
8610   "Sort the summary buffer by score.
8611 Argument REVERSE means reverse order."
8612   (interactive "P")
8613   (gnus-summary-sort 'score reverse))
8614
8615 (defun gnus-summary-sort-by-lines (&optional reverse)
8616   "Sort the summary buffer by article length.
8617 Argument REVERSE means reverse order."
8618   (interactive "P")
8619   (gnus-summary-sort 'lines reverse))
8620
8621 (defun gnus-summary-sort (predicate reverse)
8622   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8623   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8624          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8625          (gnus-thread-sort-functions
8626           (if (not reverse)
8627               thread
8628             `(lambda (t1 t2)
8629                (,thread t2 t1))))
8630          (gnus-article-sort-functions
8631           (if (not reverse)
8632               article
8633             `(lambda (t1 t2)
8634                (,article t2 t1))))
8635          (buffer-read-only)
8636          (gnus-summary-prepare-hook nil))
8637     ;; We do the sorting by regenerating the threads.
8638     (gnus-summary-prepare)
8639     ;; Hide subthreads if needed.
8640     (when (and gnus-show-threads gnus-thread-hide-subtree)
8641       (gnus-summary-hide-all-threads))))
8642
8643 ;; Summary saving commands.
8644
8645 (defun gnus-summary-save-article (&optional n not-saved)
8646   "Save the current article using the default saver function.
8647 If N is a positive number, save the N next articles.
8648 If N is a negative number, save the N previous articles.
8649 If N is nil and any articles have been marked with the process mark,
8650 save those articles instead.
8651 The variable `gnus-default-article-saver' specifies the saver function."
8652   (interactive "P")
8653   (let* ((articles (gnus-summary-work-articles n))
8654          (save-buffer (save-excursion
8655                         (nnheader-set-temp-buffer " *Gnus Save*")))
8656          (num (length articles))
8657          header article file)
8658     (while articles
8659       (setq header (gnus-summary-article-header
8660                     (setq article (pop articles))))
8661       (if (not (vectorp header))
8662           ;; This is a pseudo-article.
8663           (if (assq 'name header)
8664               (gnus-copy-file (cdr (assq 'name header)))
8665             (gnus-message 1 "Article %d is unsaveable" article))
8666         ;; This is a real article.
8667         (save-window-excursion
8668           (gnus-summary-select-article t nil nil article))
8669         (save-excursion
8670           (set-buffer save-buffer)
8671           (erase-buffer)
8672           (insert-buffer-substring gnus-original-article-buffer))
8673         (setq file (gnus-article-save save-buffer file num))
8674         (gnus-summary-remove-process-mark article)
8675         (unless not-saved
8676           (gnus-summary-set-saved-mark article))))
8677     (gnus-kill-buffer save-buffer)
8678     (gnus-summary-position-point)
8679     (gnus-set-mode-line 'summary)
8680     n))
8681
8682 (defun gnus-summary-pipe-output (&optional arg)
8683   "Pipe the current article to a subprocess.
8684 If N is a positive number, pipe the N next articles.
8685 If N is a negative number, pipe the N previous articles.
8686 If N is nil and any articles have been marked with the process mark,
8687 pipe those articles instead."
8688   (interactive "P")
8689   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8690     (gnus-summary-save-article arg t))
8691   (gnus-configure-windows 'pipe))
8692
8693 (defun gnus-summary-save-article-mail (&optional arg)
8694   "Append the current article to an mail file.
8695 If N is a positive number, save the N next articles.
8696 If N is a negative number, save the N previous articles.
8697 If N is nil and any articles have been marked with the process mark,
8698 save those articles instead."
8699   (interactive "P")
8700   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8701     (gnus-summary-save-article arg)))
8702
8703 (defun gnus-summary-save-article-rmail (&optional arg)
8704   "Append the current article to an rmail file.
8705 If N is a positive number, save the N next articles.
8706 If N is a negative number, save the N previous articles.
8707 If N is nil and any articles have been marked with the process mark,
8708 save those articles instead."
8709   (interactive "P")
8710   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8711     (gnus-summary-save-article arg)))
8712
8713 (defun gnus-summary-save-article-file (&optional arg)
8714   "Append the current article to a file.
8715 If N is a positive number, save the N next articles.
8716 If N is a negative number, save the N previous articles.
8717 If N is nil and any articles have been marked with the process mark,
8718 save those articles instead."
8719   (interactive "P")
8720   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8721     (gnus-summary-save-article arg)))
8722
8723 (defun gnus-summary-write-article-file (&optional arg)
8724   "Write the current article to a file, deleting the previous file.
8725 If N is a positive number, save the N next articles.
8726 If N is a negative number, save the N previous articles.
8727 If N is nil and any articles have been marked with the process mark,
8728 save those articles instead."
8729   (interactive "P")
8730   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8731     (gnus-summary-save-article arg)))
8732
8733 (defun gnus-summary-save-article-body-file (&optional arg)
8734   "Append the current article body to a file.
8735 If N is a positive number, save the N next articles.
8736 If N is a negative number, save the N previous articles.
8737 If N is nil and any articles have been marked with the process mark,
8738 save those articles instead."
8739   (interactive "P")
8740   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8741     (gnus-summary-save-article arg)))
8742
8743 (defun gnus-summary-pipe-message (program)
8744   "Pipe the current article through PROGRAM."
8745   (interactive "sProgram: ")
8746   (gnus-summary-select-article)
8747   (let ((mail-header-separator ""))
8748     (gnus-eval-in-buffer-window gnus-article-buffer
8749       (save-restriction
8750         (widen)
8751         (let ((start (window-start))
8752               buffer-read-only)
8753           (message-pipe-buffer-body program)
8754           (set-window-start (get-buffer-window (current-buffer)) start))))))
8755
8756 (defun gnus-get-split-value (methods)
8757   "Return a value based on the split METHODS."
8758   (let (split-name method result match)
8759     (when methods
8760       (save-excursion
8761         (set-buffer gnus-original-article-buffer)
8762         (save-restriction
8763           (nnheader-narrow-to-headers)
8764           (while methods
8765             (goto-char (point-min))
8766             (setq method (pop methods))
8767             (setq match (car method))
8768             (when (cond
8769                    ((stringp match)
8770                     ;; Regular expression.
8771                     (ignore-errors
8772                       (re-search-forward match nil t)))
8773                    ((gnus-functionp match)
8774                     ;; Function.
8775                     (save-restriction
8776                       (widen)
8777                       (setq result (funcall match gnus-newsgroup-name))))
8778                    ((consp match)
8779                     ;; Form.
8780                     (save-restriction
8781                       (widen)
8782                       (setq result (eval match)))))
8783               (setq split-name (append (cdr method) split-name))
8784               (cond ((stringp result)
8785                      (push (expand-file-name
8786                             result gnus-article-save-directory)
8787                            split-name))
8788                     ((consp result)
8789                      (setq split-name (append result split-name)))))))))
8790     split-name))
8791
8792 (defun gnus-valid-move-group-p (group)
8793   (and (boundp group)
8794        (symbol-name group)
8795        (symbol-value group)
8796        (memq 'respool
8797              (assoc (symbol-name
8798                      (car (gnus-find-method-for-group
8799                            (symbol-name group))))
8800                     gnus-valid-select-methods))))
8801
8802 (defun gnus-read-move-group-name (prompt default articles prefix)
8803   "Read a group name."
8804   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8805          (minibuffer-confirm-incomplete nil) ; XEmacs
8806          (prom
8807           (format "%s %s to:"
8808                   prompt
8809                   (if (> (length articles) 1)
8810                       (format "these %d articles" (length articles))
8811                     "this article")))
8812          (to-newsgroup
8813           (cond
8814            ((null split-name)
8815             (gnus-completing-read default prom
8816                                   gnus-active-hashtb
8817                                   'gnus-valid-move-group-p
8818                                   nil prefix
8819                                   'gnus-group-history))
8820            ((= 1 (length split-name))
8821             (gnus-completing-read (car split-name) prom
8822                                   gnus-active-hashtb
8823                                   'gnus-valid-move-group-p
8824                                   nil nil
8825                                   'gnus-group-history))
8826            (t
8827             (gnus-completing-read nil prom
8828                                   (mapcar (lambda (el) (list el))
8829                                           (nreverse split-name))
8830                                   nil nil nil
8831                                   'gnus-group-history)))))
8832     (when to-newsgroup
8833       (if (or (string= to-newsgroup "")
8834               (string= to-newsgroup prefix))
8835           (setq to-newsgroup default))
8836       (unless to-newsgroup
8837         (error "No group name entered"))
8838       (or (gnus-active to-newsgroup)
8839           (gnus-activate-group to-newsgroup)
8840           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8841                                      to-newsgroup))
8842               (or (and (gnus-request-create-group
8843                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8844                        (gnus-activate-group to-newsgroup nil nil
8845                                             (gnus-group-name-to-method
8846                                              to-newsgroup)))
8847                   (error "Couldn't create group %s" to-newsgroup)))
8848           (error "No such group: %s" to-newsgroup)))
8849     to-newsgroup))
8850
8851 ;; Summary extract commands
8852
8853 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8854   (let ((buffer-read-only nil)
8855         (article (gnus-summary-article-number))
8856         after-article b e)
8857     (unless (gnus-summary-goto-subject article)
8858       (error "No such article: %d" article))
8859     (gnus-summary-position-point)
8860     ;; If all commands are to be bunched up on one line, we collect
8861     ;; them here.
8862     (unless gnus-view-pseudos-separately
8863       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8864             files action)
8865         (while ps
8866           (setq action (cdr (assq 'action (car ps))))
8867           (setq files (list (cdr (assq 'name (car ps)))))
8868           (while (and ps (cdr ps)
8869                       (string= (or action "1")
8870                                (or (cdr (assq 'action (cadr ps))) "2")))
8871             (push (cdr (assq 'name (cadr ps))) files)
8872             (setcdr ps (cddr ps)))
8873           (when files
8874             (when (not (string-match "%s" action))
8875               (push " " files))
8876             (push " " files)
8877             (when (assq 'execute (car ps))
8878               (setcdr (assq 'execute (car ps))
8879                       (funcall (if (string-match "%s" action)
8880                                    'format 'concat)
8881                                action
8882                                (mapconcat
8883                                 (lambda (f)
8884                                   (if (equal f " ")
8885                                       f
8886                                     (mm-quote-arg f)))
8887                                 files " ")))))
8888           (setq ps (cdr ps)))))
8889     (if (and gnus-view-pseudos (not not-view))
8890         (while pslist
8891           (when (assq 'execute (car pslist))
8892             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8893                                   (eq gnus-view-pseudos 'not-confirm)))
8894           (setq pslist (cdr pslist)))
8895       (save-excursion
8896         (while pslist
8897           (setq after-article (or (cdr (assq 'article (car pslist)))
8898                                   (gnus-summary-article-number)))
8899           (gnus-summary-goto-subject after-article)
8900           (forward-line 1)
8901           (setq b (point))
8902           (insert "    " (file-name-nondirectory
8903                           (cdr (assq 'name (car pslist))))
8904                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8905           (setq e (point))
8906           (forward-line -1)             ; back to `b'
8907           (gnus-add-text-properties
8908            b (1- e) (list 'gnus-number gnus-reffed-article-number
8909                           gnus-mouse-face-prop gnus-mouse-face))
8910           (gnus-data-enter
8911            after-article gnus-reffed-article-number
8912            gnus-unread-mark b (car pslist) 0 (- e b))
8913           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8914           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8915           (setq pslist (cdr pslist)))))))
8916
8917 (defun gnus-pseudos< (p1 p2)
8918   (let ((c1 (cdr (assq 'action p1)))
8919         (c2 (cdr (assq 'action p2))))
8920     (and c1 c2 (string< c1 c2))))
8921
8922 (defun gnus-request-pseudo-article (props)
8923   (cond ((assq 'execute props)
8924          (gnus-execute-command (cdr (assq 'execute props)))))
8925   (let ((gnus-current-article (gnus-summary-article-number)))
8926     (gnus-run-hooks 'gnus-mark-article-hook)))
8927
8928 (defun gnus-execute-command (command &optional automatic)
8929   (save-excursion
8930     (gnus-article-setup-buffer)
8931     (set-buffer gnus-article-buffer)
8932     (setq buffer-read-only nil)
8933     (let ((command (if automatic command
8934                      (read-string "Command: " (cons command 0)))))
8935       (erase-buffer)
8936       (insert "$ " command "\n\n")
8937       (if gnus-view-pseudo-asynchronously
8938           (start-process "gnus-execute" (current-buffer) shell-file-name
8939                          shell-command-switch command)
8940         (call-process shell-file-name nil t nil
8941                       shell-command-switch command)))))
8942
8943 ;; Summary kill commands.
8944
8945 (defun gnus-summary-edit-global-kill (article)
8946   "Edit the \"global\" kill file."
8947   (interactive (list (gnus-summary-article-number)))
8948   (gnus-group-edit-global-kill article))
8949
8950 (defun gnus-summary-edit-local-kill ()
8951   "Edit a local kill file applied to the current newsgroup."
8952   (interactive)
8953   (setq gnus-current-headers (gnus-summary-article-header))
8954   (gnus-group-edit-local-kill
8955    (gnus-summary-article-number) gnus-newsgroup-name))
8956
8957 ;;; Header reading.
8958
8959 (defun gnus-read-header (id &optional header)
8960   "Read the headers of article ID and enter them into the Gnus system."
8961   (let ((group gnus-newsgroup-name)
8962         (gnus-override-method
8963          (and (gnus-news-group-p gnus-newsgroup-name)
8964               gnus-refer-article-method))
8965         where)
8966     ;; First we check to see whether the header in question is already
8967     ;; fetched.
8968     (if (stringp id)
8969         ;; This is a Message-ID.
8970         (setq header (or header (gnus-id-to-header id)))
8971       ;; This is an article number.
8972       (setq header (or header (gnus-summary-article-header id))))
8973     (if (and header
8974              (not (gnus-summary-article-sparse-p (mail-header-number header))))
8975         ;; We have found the header.
8976         header
8977       ;; If this is a sparse article, we have to nix out its
8978       ;; previous entry in the thread hashtb.
8979       (when (and header
8980                  (gnus-summary-article-sparse-p (mail-header-number header)))
8981         (let* ((parent (gnus-parent-id (mail-header-references header)))
8982                (thread (and parent (gnus-id-to-thread parent))))
8983           (when thread
8984             (delq (assq header thread) thread))))
8985       ;; We have to really fetch the header to this article.
8986       (save-excursion
8987         (set-buffer nntp-server-buffer)
8988         (when (setq where (gnus-request-head id group))
8989           (nnheader-fold-continuation-lines)
8990           (goto-char (point-max))
8991           (insert ".\n")
8992           (goto-char (point-min))
8993           (insert "211 ")
8994           (princ (cond
8995                   ((numberp id) id)
8996                   ((cdr where) (cdr where))
8997                   (header (mail-header-number header))
8998                   (t gnus-reffed-article-number))
8999                  (current-buffer))
9000           (insert " Article retrieved.\n"))
9001         (if (or (not where)
9002                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9003             ()                          ; Malformed head.
9004           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9005             (when (and (stringp id)
9006                        (not (string= (gnus-group-real-name group)
9007                                      (car where))))
9008               ;; If we fetched by Message-ID and the article came
9009               ;; from a different group, we fudge some bogus article
9010               ;; numbers for this article.
9011               (mail-header-set-number header gnus-reffed-article-number))
9012             (save-excursion
9013               (set-buffer gnus-summary-buffer)
9014               (decf gnus-reffed-article-number)
9015               (gnus-remove-header (mail-header-number header))
9016               (push header gnus-newsgroup-headers)
9017               (setq gnus-current-headers header)
9018               (push (mail-header-number header) gnus-newsgroup-limit)))
9019           header)))))
9020
9021 (defun gnus-remove-header (number)
9022   "Remove header NUMBER from `gnus-newsgroup-headers'."
9023   (if (and gnus-newsgroup-headers
9024            (= number (mail-header-number (car gnus-newsgroup-headers))))
9025       (pop gnus-newsgroup-headers)
9026     (let ((headers gnus-newsgroup-headers))
9027       (while (and (cdr headers)
9028                   (not (= number (mail-header-number (cadr headers)))))
9029         (pop headers))
9030       (when (cdr headers)
9031         (setcdr headers (cddr headers))))))
9032
9033 ;;;
9034 ;;; summary highlights
9035 ;;;
9036
9037 (defun gnus-highlight-selected-summary ()
9038   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9039   ;; Highlight selected article in summary buffer
9040   (when gnus-summary-selected-face
9041     (save-excursion
9042       (let* ((beg (progn (beginning-of-line) (point)))
9043              (end (progn (end-of-line) (point)))
9044              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9045              (from (if (get-text-property beg gnus-mouse-face-prop)
9046                        beg
9047                      (or (next-single-property-change
9048                           beg gnus-mouse-face-prop nil end)
9049                          beg)))
9050              (to
9051               (if (= from end)
9052                   (- from 2)
9053                 (or (next-single-property-change
9054                      from gnus-mouse-face-prop nil end)
9055                     end))))
9056         ;; If no mouse-face prop on line we will have to = from = end,
9057         ;; so we highlight the entire line instead.
9058         (when (= (+ to 2) from)
9059           (setq from beg)
9060           (setq to end))
9061         (if gnus-newsgroup-selected-overlay
9062             ;; Move old overlay.
9063             (gnus-move-overlay
9064              gnus-newsgroup-selected-overlay from to (current-buffer))
9065           ;; Create new overlay.
9066           (gnus-overlay-put
9067            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9068            'face gnus-summary-selected-face))))))
9069
9070 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9071 (defun gnus-summary-highlight-line ()
9072   "Highlight current line according to `gnus-summary-highlight'."
9073   (let* ((list gnus-summary-highlight)
9074          (p (point))
9075          (end (progn (end-of-line) (point)))
9076          ;; now find out where the line starts and leave point there.
9077          (beg (progn (beginning-of-line) (point)))
9078          (article (gnus-summary-article-number))
9079          (score (or (cdr (assq (or article gnus-current-article)
9080                                gnus-newsgroup-scored))
9081                     gnus-summary-default-score 0))
9082          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9083          (inhibit-read-only t))
9084     ;; Eval the cars of the lists until we find a match.
9085     (let ((default gnus-summary-default-score))
9086       (while (and list
9087                   (not (eval (caar list))))
9088         (setq list (cdr list))))
9089     (let ((face (cdar list)))
9090       (unless (eq face (get-text-property beg 'face))
9091         (gnus-put-text-property-excluding-characters-with-faces
9092          beg end 'face
9093          (setq face (if (boundp face) (symbol-value face) face)))
9094         (when gnus-summary-highlight-line-function
9095           (funcall gnus-summary-highlight-line-function article face))))
9096     (goto-char p)))
9097
9098 (defun gnus-update-read-articles (group unread &optional compute)
9099   "Update the list of read articles in GROUP."
9100   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9101          (entry (gnus-gethash group gnus-newsrc-hashtb))
9102          (info (nth 2 entry))
9103          (prev 1)
9104          (unread (sort (copy-sequence unread) '<))
9105          read)
9106     (if (or (not info) (not active))
9107         ;; There is no info on this group if it was, in fact,
9108         ;; killed.  Gnus stores no information on killed groups, so
9109         ;; there's nothing to be done.
9110         ;; One could store the information somewhere temporarily,
9111         ;; perhaps...  Hmmm...
9112         ()
9113       ;; Remove any negative articles numbers.
9114       (while (and unread (< (car unread) 0))
9115         (setq unread (cdr unread)))
9116       ;; Remove any expired article numbers
9117       (while (and unread (< (car unread) (car active)))
9118         (setq unread (cdr unread)))
9119       ;; Compute the ranges of read articles by looking at the list of
9120       ;; unread articles.
9121       (while unread
9122         (when (/= (car unread) prev)
9123           (push (if (= prev (1- (car unread))) prev
9124                   (cons prev (1- (car unread))))
9125                 read))
9126         (setq prev (1+ (car unread)))
9127         (setq unread (cdr unread)))
9128       (when (<= prev (cdr active))
9129         (push (cons prev (cdr active)) read))
9130       (setq read (if (> (length read) 1) (nreverse read) read))
9131       (if compute
9132           read
9133         (save-excursion
9134           (set-buffer gnus-group-buffer)
9135           (gnus-undo-register
9136             `(progn
9137                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9138                (gnus-info-set-read ',info ',(gnus-info-read info))
9139                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9140                (gnus-group-update-group ,group t))))
9141        ;; Propagate the read marks to the backend.
9142        (if (gnus-check-backend-function 'request-set-mark group)
9143            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9144                  (add (gnus-remove-from-range read (gnus-info-read info))))
9145              (when (or add del)
9146                (gnus-request-set-mark
9147                 group (delq nil (list (if add (list add 'add '(read)))
9148                                       (if del (list del 'del '(read)))))))))
9149         ;; Enter this list into the group info.
9150         (gnus-info-set-read info read)
9151         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9152         (gnus-get-unread-articles-in-group info (gnus-active group))
9153         t))))
9154
9155 (defun gnus-offer-save-summaries ()
9156   "Offer to save all active summary buffers."
9157   (save-excursion
9158     (let ((buflist (buffer-list))
9159           buffers bufname)
9160       ;; Go through all buffers and find all summaries.
9161       (while buflist
9162         (and (setq bufname (buffer-name (car buflist)))
9163              (string-match "Summary" bufname)
9164              (save-excursion
9165                (set-buffer bufname)
9166                ;; We check that this is, indeed, a summary buffer.
9167                (and (eq major-mode 'gnus-summary-mode)
9168                     ;; Also make sure this isn't bogus.
9169                     gnus-newsgroup-prepared
9170                     ;; Also make sure that this isn't a dead summary buffer.
9171                     (not gnus-dead-summary-mode)))
9172              (push bufname buffers))
9173         (setq buflist (cdr buflist)))
9174       ;; Go through all these summary buffers and offer to save them.
9175       (when buffers
9176         (map-y-or-n-p
9177          "Update summary buffer %s? "
9178          (lambda (buf)
9179            (switch-to-buffer buf)
9180            (gnus-summary-exit))
9181          buffers)))))
9182
9183 (defun gnus-newsgroup-setup-default-charset ()
9184   "Setup newsgroup default charset."
9185   (let ((name (and gnus-newsgroup-name 
9186                    (string-match "[^:]+$" gnus-newsgroup-name)
9187                    (match-string 0 gnus-newsgroup-name))))
9188     (setq gnus-newsgroup-default-charset
9189           (or (and gnus-newsgroup-name
9190                    (or (gnus-group-find-parameter 
9191                         gnus-newsgroup-name 'charset)
9192                        (let ((alist gnus-newsgroup-default-charset-alist) 
9193                              elem (charset nil))
9194                          (while alist
9195                            (if (and name
9196                                     (string-match 
9197                                      (car (setq elem (pop alist)))
9198                                      name))
9199                                (setq alist nil
9200                                      charset (cdr elem))))
9201                          charset)))
9202               gnus-default-charset))
9203     (setq gnus-newsgroup-iso-8859-1-forced 
9204           (and gnus-newsgroup-name
9205                (or (gnus-group-find-parameter
9206                     gnus-newsgroup-name 'iso-8859-1-forced)
9207                    (and name
9208                         (string-match  gnus-newsgroup-iso-8859-1-forced-regexp 
9209                                        name))))))
9210   (if (stringp gnus-newsgroup-default-charset)
9211       (setq gnus-newsgroup-default-charset
9212             (intern (downcase gnus-newsgroup-default-charset))))
9213   (setq gnus-newsgroup-iso-8859-1-forced
9214         (if (stringp gnus-newsgroup-iso-8859-1-forced)
9215             (intern (downcase gnus-newsgroup-iso-8859-1-forced))
9216           (and gnus-newsgroup-iso-8859-1-forced
9217                gnus-newsgroup-default-charset))))
9218   
9219 ;;;
9220 ;;; MIME Commands
9221 ;;;
9222
9223 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9224   "Display the current article buffer fully MIME-buttonized.  
9225 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9226 treated as multipart/mixed."
9227   (interactive "P")
9228   (require 'gnus-art)
9229   (let ((gnus-unbuttonized-mime-types nil)
9230         (gnus-mime-display-multipart-as-mixed show-all-parts))
9231     (gnus-summary-show-article)))
9232
9233 (defun gnus-summary-repair-multipart (article)
9234   "Add a Content-Type header to a multipart article without one."
9235   (interactive (list (gnus-summary-article-number)))
9236   (gnus-with-article article
9237     (message-narrow-to-head)
9238     (goto-char (point-max))
9239     (widen)
9240     (when (search-forward "\n--" nil t)
9241       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9242         (message-narrow-to-head)
9243         (message-remove-header "Mime-Version")
9244         (message-remove-header "Content-Type")
9245         (goto-char (point-max))
9246         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9247                         separator))
9248         (insert "Mime-Version: 1.0\n")
9249         (widen))))
9250   (let (gnus-mark-article-hook)
9251     (gnus-summary-select-article t t nil article)))
9252
9253 (defun gnus-summary-toggle-display-buttonized ()
9254   "Toggle the buttonizing of the article buffer."
9255   (interactive)
9256   (require 'gnus-art)
9257   (if (setq gnus-inhibit-mime-unbuttonizing
9258             (not gnus-inhibit-mime-unbuttonizing))
9259       (let ((gnus-unbuttonized-mime-types nil))
9260         (gnus-summary-show-article))
9261     (gnus-summary-show-article)))
9262     
9263 (gnus-ems-redefine)
9264
9265 (provide 'gnus-sum)
9266
9267 (run-hooks 'gnus-sum-load-hook)
9268
9269 ;;; gnus-sum.el ends here