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