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