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