*** 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                     (gnus-delete-line)
3238                     (gnus-data-compute-positions))
3239                   (setq thread (cdr thread))
3240                   (while thread
3241                     (gnus-remove-thread-1 (car thread))
3242                     (setq thread (cdr thread))))
3243               (gnus-remove-thread-1 thread))))))))
3244
3245 (defun gnus-remove-thread-1 (thread)
3246   "Remove the thread THREAD recursively."
3247   (let ((number (mail-header-number (pop thread)))
3248         d)
3249     (setq thread (reverse thread))
3250     (while thread
3251       (gnus-remove-thread-1 (pop thread)))
3252     (when (setq d (gnus-data-find number))
3253       (goto-char (gnus-data-pos d))
3254       (gnus-data-remove
3255        number
3256        (- (gnus-point-at-bol)
3257           (prog1
3258               (1+ (gnus-point-at-eol))
3259             (gnus-delete-line)))))))
3260
3261 (defun gnus-sort-threads (threads)
3262   "Sort THREADS."
3263   (if (not gnus-thread-sort-functions)
3264       threads
3265     (gnus-message 7 "Sorting threads...")
3266     (prog1
3267         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3268       (gnus-message 7 "Sorting threads...done"))))
3269
3270 (defun gnus-sort-articles (articles)
3271   "Sort ARTICLES."
3272   (when gnus-article-sort-functions
3273     (gnus-message 7 "Sorting articles...")
3274     (prog1
3275         (setq gnus-newsgroup-headers
3276               (sort articles (gnus-make-sort-function
3277                               gnus-article-sort-functions)))
3278       (gnus-message 7 "Sorting articles...done"))))
3279
3280 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3281 (defmacro gnus-thread-header (thread)
3282   ;; Return header of first article in THREAD.
3283   ;; Note that THREAD must never, ever be anything else than a variable -
3284   ;; using some other form will lead to serious barfage.
3285   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3286   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3287   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3288         (vector thread) 2))
3289
3290 (defsubst gnus-article-sort-by-number (h1 h2)
3291   "Sort articles by article number."
3292   (< (mail-header-number h1)
3293      (mail-header-number h2)))
3294
3295 (defun gnus-thread-sort-by-number (h1 h2)
3296   "Sort threads by root article number."
3297   (gnus-article-sort-by-number
3298    (gnus-thread-header h1) (gnus-thread-header h2)))
3299
3300 (defsubst gnus-article-sort-by-lines (h1 h2)
3301   "Sort articles by article Lines header."
3302   (< (mail-header-lines h1)
3303      (mail-header-lines h2)))
3304
3305 (defun gnus-thread-sort-by-lines (h1 h2)
3306   "Sort threads by root article Lines header."
3307   (gnus-article-sort-by-lines
3308    (gnus-thread-header h1) (gnus-thread-header h2)))
3309
3310 (defsubst gnus-article-sort-by-author (h1 h2)
3311   "Sort articles by root author."
3312   (string-lessp
3313    (let ((extract (funcall
3314                    gnus-extract-address-components
3315                    (mail-header-from h1))))
3316      (or (car extract) (cadr extract) ""))
3317    (let ((extract (funcall
3318                    gnus-extract-address-components
3319                    (mail-header-from h2))))
3320      (or (car extract) (cadr extract) ""))))
3321
3322 (defun gnus-thread-sort-by-author (h1 h2)
3323   "Sort threads by root author."
3324   (gnus-article-sort-by-author
3325    (gnus-thread-header h1)  (gnus-thread-header h2)))
3326
3327 (defsubst gnus-article-sort-by-subject (h1 h2)
3328   "Sort articles by root subject."
3329   (string-lessp
3330    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3331    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3332
3333 (defun gnus-thread-sort-by-subject (h1 h2)
3334   "Sort threads by root subject."
3335   (gnus-article-sort-by-subject
3336    (gnus-thread-header h1) (gnus-thread-header h2)))
3337
3338 (defsubst gnus-article-sort-by-date (h1 h2)
3339   "Sort articles by root article date."
3340   (gnus-time-less
3341    (gnus-date-get-time (mail-header-date h1))
3342    (gnus-date-get-time (mail-header-date h2))))
3343
3344 (defun gnus-thread-sort-by-date (h1 h2)
3345   "Sort threads by root article date."
3346   (gnus-article-sort-by-date
3347    (gnus-thread-header h1) (gnus-thread-header h2)))
3348
3349 (defsubst gnus-article-sort-by-score (h1 h2)
3350   "Sort articles by root article score.
3351 Unscored articles will be counted as having a score of zero."
3352   (> (or (cdr (assq (mail-header-number h1)
3353                     gnus-newsgroup-scored))
3354          gnus-summary-default-score 0)
3355      (or (cdr (assq (mail-header-number h2)
3356                     gnus-newsgroup-scored))
3357          gnus-summary-default-score 0)))
3358
3359 (defun gnus-thread-sort-by-score (h1 h2)
3360   "Sort threads by root article score."
3361   (gnus-article-sort-by-score
3362    (gnus-thread-header h1) (gnus-thread-header h2)))
3363
3364 (defun gnus-thread-sort-by-total-score (h1 h2)
3365   "Sort threads by the sum of all scores in the thread.
3366 Unscored articles will be counted as having a score of zero."
3367   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3368
3369 (defun gnus-thread-total-score (thread)
3370   ;;  This function find the total score of THREAD.
3371   (cond ((null thread)
3372          0)
3373         ((consp thread)
3374          (if (stringp (car thread))
3375              (apply gnus-thread-score-function 0
3376                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3377            (gnus-thread-total-score-1 thread)))
3378         (t
3379          (gnus-thread-total-score-1 (list thread)))))
3380
3381 (defun gnus-thread-total-score-1 (root)
3382   ;; This function find the total score of the thread below ROOT.
3383   (setq root (car root))
3384   (apply gnus-thread-score-function
3385          (or (append
3386               (mapcar 'gnus-thread-total-score
3387                       (cdr (gnus-gethash (mail-header-id root)
3388                                          gnus-newsgroup-dependencies)))
3389               (when (> (mail-header-number root) 0)
3390                 (list (or (cdr (assq (mail-header-number root)
3391                                      gnus-newsgroup-scored))
3392                           gnus-summary-default-score 0))))
3393              (list gnus-summary-default-score)
3394              '(0))))
3395
3396 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3397 (defvar gnus-tmp-prev-subject nil)
3398 (defvar gnus-tmp-false-parent nil)
3399 (defvar gnus-tmp-root-expunged nil)
3400 (defvar gnus-tmp-dummy-line nil)
3401
3402 (defun gnus-summary-prepare-threads (threads)
3403   "Prepare summary buffer from THREADS and indentation LEVEL.
3404 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3405 or a straight list of headers."
3406   (gnus-message 7 "Generating summary...")
3407
3408   (setq gnus-newsgroup-threads threads)
3409   (beginning-of-line)
3410
3411   (let ((gnus-tmp-level 0)
3412         (default-score (or gnus-summary-default-score 0))
3413         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3414         thread number subject stack state gnus-tmp-gathered beg-match
3415         new-roots gnus-tmp-new-adopts thread-end
3416         gnus-tmp-header gnus-tmp-unread
3417         gnus-tmp-replied gnus-tmp-subject-or-nil
3418         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3419         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3420         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3421
3422     (setq gnus-tmp-prev-subject nil)
3423
3424     (if (vectorp (car threads))
3425         ;; If this is a straight (sic) list of headers, then a
3426         ;; threaded summary display isn't required, so we just create
3427         ;; an unthreaded one.
3428         (gnus-summary-prepare-unthreaded threads)
3429
3430       ;; Do the threaded display.
3431
3432       (while (or threads stack gnus-tmp-new-adopts new-roots)
3433
3434         (if (and (= gnus-tmp-level 0)
3435                  (or (not stack)
3436                      (= (caar stack) 0))
3437                  (not gnus-tmp-false-parent)
3438                  (or gnus-tmp-new-adopts new-roots))
3439             (if gnus-tmp-new-adopts
3440                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3441                       thread (list (car gnus-tmp-new-adopts))
3442                       gnus-tmp-header (caar thread)
3443                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3444               (when new-roots
3445                 (setq thread (list (car new-roots))
3446                       gnus-tmp-header (caar thread)
3447                       new-roots (cdr new-roots))))
3448
3449           (if threads
3450               ;; If there are some threads, we do them before the
3451               ;; threads on the stack.
3452               (setq thread threads
3453                     gnus-tmp-header (caar thread))
3454             ;; There were no current threads, so we pop something off
3455             ;; the stack.
3456             (setq state (car stack)
3457                   gnus-tmp-level (car state)
3458                   thread (cdr state)
3459                   stack (cdr stack)
3460                   gnus-tmp-header (caar thread))))
3461
3462         (setq gnus-tmp-false-parent nil)
3463         (setq gnus-tmp-root-expunged nil)
3464         (setq thread-end nil)
3465
3466         (if (stringp gnus-tmp-header)
3467             ;; The header is a dummy root.
3468             (cond
3469              ((eq gnus-summary-make-false-root 'adopt)
3470               ;; We let the first article adopt the rest.
3471               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3472                                                (cddar thread)))
3473               (setq gnus-tmp-gathered
3474                     (nconc (mapcar
3475                             (lambda (h) (mail-header-number (car h)))
3476                             (cddar thread))
3477                            gnus-tmp-gathered))
3478               (setq thread (cons (list (caar thread)
3479                                        (cadar thread))
3480                                  (cdr thread)))
3481               (setq gnus-tmp-level -1
3482                     gnus-tmp-false-parent t))
3483              ((eq gnus-summary-make-false-root 'empty)
3484               ;; We print adopted articles with empty subject fields.
3485               (setq gnus-tmp-gathered
3486                     (nconc (mapcar
3487                             (lambda (h) (mail-header-number (car h)))
3488                             (cddar thread))
3489                            gnus-tmp-gathered))
3490               (setq gnus-tmp-level -1))
3491              ((eq gnus-summary-make-false-root 'dummy)
3492               ;; We remember that we probably want to output a dummy
3493               ;; root.
3494               (setq gnus-tmp-dummy-line gnus-tmp-header)
3495               (setq gnus-tmp-prev-subject gnus-tmp-header))
3496              (t
3497               ;; We do not make a root for the gathered
3498               ;; sub-threads at all.
3499               (setq gnus-tmp-level -1)))
3500
3501           (setq number (mail-header-number gnus-tmp-header)
3502                 subject (mail-header-subject gnus-tmp-header))
3503
3504           (cond
3505            ;; If the thread has changed subject, we might want to make
3506            ;; this subthread into a root.
3507            ((and (null gnus-thread-ignore-subject)
3508                  (not (zerop gnus-tmp-level))
3509                  gnus-tmp-prev-subject
3510                  (not (inline
3511                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3512             (setq new-roots (nconc new-roots (list (car thread)))
3513                   thread-end t
3514                   gnus-tmp-header nil))
3515            ;; If the article lies outside the current limit,
3516            ;; then we do not display it.
3517            ((not (memq number gnus-newsgroup-limit))
3518             (setq gnus-tmp-gathered
3519                   (nconc (mapcar
3520                           (lambda (h) (mail-header-number (car h)))
3521                           (cdar thread))
3522                          gnus-tmp-gathered))
3523             (setq gnus-tmp-new-adopts (if (cdar thread)
3524                                           (append gnus-tmp-new-adopts
3525                                                   (cdar thread))
3526                                         gnus-tmp-new-adopts)
3527                   thread-end t
3528                   gnus-tmp-header nil)
3529             (when (zerop gnus-tmp-level)
3530               (setq gnus-tmp-root-expunged t)))
3531            ;; Perhaps this article is to be marked as read?
3532            ((and gnus-summary-mark-below
3533                  (< (or (cdr (assq number gnus-newsgroup-scored))
3534                         default-score)
3535                     gnus-summary-mark-below)
3536                  ;; Don't touch sparse articles.
3537                  (not (gnus-summary-article-sparse-p number))
3538                  (not (gnus-summary-article-ancient-p number)))
3539             (setq gnus-newsgroup-unreads
3540                   (delq number gnus-newsgroup-unreads))
3541             (if gnus-newsgroup-auto-expire
3542                 (push number gnus-newsgroup-expirable)
3543               (push (cons number gnus-low-score-mark)
3544                     gnus-newsgroup-reads))))
3545
3546           (when gnus-tmp-header
3547             ;; We may have an old dummy line to output before this
3548             ;; article.
3549             (when (and gnus-tmp-dummy-line
3550                        (gnus-subject-equal
3551                         gnus-tmp-dummy-line
3552                         (mail-header-subject gnus-tmp-header)))
3553               (gnus-summary-insert-dummy-line
3554                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3555               (setq gnus-tmp-dummy-line nil))
3556
3557             ;; Compute the mark.
3558             (setq gnus-tmp-unread (gnus-article-mark number))
3559
3560             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3561                                   gnus-tmp-header gnus-tmp-level)
3562                   gnus-newsgroup-data)
3563
3564             ;; Actually insert the line.
3565             (setq
3566              gnus-tmp-subject-or-nil
3567              (cond
3568               ((and gnus-thread-ignore-subject
3569                     gnus-tmp-prev-subject
3570                     (not (inline (gnus-subject-equal
3571                                   gnus-tmp-prev-subject subject))))
3572                subject)
3573               ((zerop gnus-tmp-level)
3574                (if (and (eq gnus-summary-make-false-root 'empty)
3575                         (memq number gnus-tmp-gathered)
3576                         gnus-tmp-prev-subject
3577                         (inline (gnus-subject-equal
3578                                  gnus-tmp-prev-subject subject)))
3579                    gnus-summary-same-subject
3580                  subject))
3581               (t gnus-summary-same-subject)))
3582             (if (and (eq gnus-summary-make-false-root 'adopt)
3583                      (= gnus-tmp-level 1)
3584                      (memq number gnus-tmp-gathered))
3585                 (setq gnus-tmp-opening-bracket ?\<
3586                       gnus-tmp-closing-bracket ?\>)
3587               (setq gnus-tmp-opening-bracket ?\[
3588                     gnus-tmp-closing-bracket ?\]))
3589             (setq
3590              gnus-tmp-indentation
3591              (aref gnus-thread-indent-array gnus-tmp-level)
3592              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3593              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3594                                 gnus-summary-default-score 0)
3595              gnus-tmp-score-char
3596              (if (or (null gnus-summary-default-score)
3597                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3598                          gnus-summary-zcore-fuzz))
3599                  ? 
3600                (if (< gnus-tmp-score gnus-summary-default-score)
3601                    gnus-score-below-mark gnus-score-over-mark))
3602              gnus-tmp-replied
3603              (cond ((memq number gnus-newsgroup-processable)
3604                     gnus-process-mark)
3605                    ((memq number gnus-newsgroup-cached)
3606                     gnus-cached-mark)
3607                    ((memq number gnus-newsgroup-replied)
3608                     gnus-replied-mark)
3609                    ((memq number gnus-newsgroup-saved)
3610                     gnus-saved-mark)
3611                    (t gnus-unread-mark))
3612              gnus-tmp-from (mail-header-from gnus-tmp-header)
3613              gnus-tmp-name
3614              (cond
3615               ((string-match "<[^>]+> *$" gnus-tmp-from)
3616                (setq beg-match (match-beginning 0))
3617                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3618                         (substring gnus-tmp-from (1+ (match-beginning 0))
3619                                    (1- (match-end 0))))
3620                    (substring gnus-tmp-from 0 beg-match)))
3621               ((string-match "(.+)" gnus-tmp-from)
3622                (substring gnus-tmp-from
3623                           (1+ (match-beginning 0)) (1- (match-end 0))))
3624               (t gnus-tmp-from)))
3625             (when (string= gnus-tmp-name "")
3626               (setq gnus-tmp-name gnus-tmp-from))
3627             (unless (numberp gnus-tmp-lines)
3628               (setq gnus-tmp-lines 0))
3629             (gnus-put-text-property
3630              (point)
3631              (progn (eval gnus-summary-line-format-spec) (point))
3632              'gnus-number number)
3633             (when gnus-visual-p
3634               (forward-line -1)
3635               (gnus-run-hooks 'gnus-summary-update-hook)
3636               (forward-line 1))
3637
3638             (setq gnus-tmp-prev-subject subject)))
3639
3640         (when (nth 1 thread)
3641           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3642         (incf gnus-tmp-level)
3643         (setq threads (if thread-end nil (cdar thread)))
3644         (unless threads
3645           (setq gnus-tmp-level 0)))))
3646   (gnus-message 7 "Generating summary...done"))
3647
3648 (defun gnus-summary-prepare-unthreaded (headers)
3649   "Generate an unthreaded summary buffer based on HEADERS."
3650   (let (header number mark)
3651
3652     (beginning-of-line)
3653
3654     (while headers
3655       ;; We may have to root out some bad articles...
3656       (when (memq (setq number (mail-header-number
3657                                 (setq header (pop headers))))
3658                   gnus-newsgroup-limit)
3659         ;; Mark article as read when it has a low score.
3660         (when (and gnus-summary-mark-below
3661                    (< (or (cdr (assq number gnus-newsgroup-scored))
3662                           gnus-summary-default-score 0)
3663                       gnus-summary-mark-below)
3664                    (not (gnus-summary-article-ancient-p number)))
3665           (setq gnus-newsgroup-unreads
3666                 (delq number gnus-newsgroup-unreads))
3667           (if gnus-newsgroup-auto-expire
3668               (push number gnus-newsgroup-expirable)
3669             (push (cons number gnus-low-score-mark)
3670                   gnus-newsgroup-reads)))
3671
3672         (setq mark (gnus-article-mark number))
3673         (push (gnus-data-make number mark (1+ (point)) header 0)
3674               gnus-newsgroup-data)
3675         (gnus-summary-insert-line
3676          header 0 number
3677          mark (memq number gnus-newsgroup-replied)
3678          (memq number gnus-newsgroup-expirable)
3679          (mail-header-subject header) nil
3680          (cdr (assq number gnus-newsgroup-scored))
3681          (memq number gnus-newsgroup-processable))))))
3682
3683 (defun gnus-select-newsgroup (group &optional read-all)
3684   "Select newsgroup GROUP.
3685 If READ-ALL is non-nil, all articles in the group are selected."
3686   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3687          ;;!!! Dirty hack; should be removed.
3688          (gnus-summary-ignore-duplicates
3689           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
3690               t
3691             gnus-summary-ignore-duplicates))
3692          (info (nth 2 entry))
3693          articles fetched-articles cached)
3694
3695     (unless (gnus-check-server
3696              (setq gnus-current-select-method
3697                    (gnus-find-method-for-group group)))
3698       (error "Couldn't open server"))
3699
3700     (or (and entry (not (eq (car entry) t))) ; Either it's active...
3701         (gnus-activate-group group)     ; Or we can activate it...
3702         (progn                          ; Or we bug out.
3703           (when (equal major-mode 'gnus-summary-mode)
3704             (kill-buffer (current-buffer)))
3705           (error "Couldn't request group %s: %s"
3706                  group (gnus-status-message group))))
3707
3708     (unless (gnus-request-group group t)
3709       (when (equal major-mode 'gnus-summary-mode)
3710         (kill-buffer (current-buffer)))
3711       (error "Couldn't request group %s: %s"
3712              group (gnus-status-message group)))
3713
3714     (setq gnus-newsgroup-name group)
3715     (setq gnus-newsgroup-unselected nil)
3716     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
3717
3718     ;; Adjust and set lists of article marks.
3719     (when info
3720       (gnus-adjust-marked-articles info))
3721
3722     ;; Kludge to avoid having cached articles nixed out in virtual groups.
3723     (when (gnus-virtual-group-p group)
3724       (setq cached gnus-newsgroup-cached))
3725
3726     (setq gnus-newsgroup-unreads
3727           (gnus-set-difference
3728            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
3729            gnus-newsgroup-dormant))
3730
3731     (setq gnus-newsgroup-processable nil)
3732
3733     (gnus-update-read-articles group gnus-newsgroup-unreads)
3734     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
3735       (gnus-group-update-group group))
3736
3737     (setq articles (gnus-articles-to-read group read-all))
3738
3739     (cond
3740      ((null articles)
3741       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
3742       'quit)
3743      ((eq articles 0) nil)
3744      (t
3745       ;; Init the dependencies hash table.
3746       (setq gnus-newsgroup-dependencies
3747             (gnus-make-hashtable (length articles)))
3748       ;; Retrieve the headers and read them in.
3749       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
3750       (setq gnus-newsgroup-headers
3751             (if (eq 'nov
3752                     (setq gnus-headers-retrieved-by
3753                           (gnus-retrieve-headers
3754                            articles gnus-newsgroup-name
3755                            ;; We might want to fetch old headers, but
3756                            ;; not if there is only 1 article.
3757                            (and (or (and
3758                                      (not (eq gnus-fetch-old-headers 'some))
3759                                      (not (numberp gnus-fetch-old-headers)))
3760                                     (> (length articles) 1))
3761                                 gnus-fetch-old-headers))))
3762                 (gnus-get-newsgroup-headers-xover
3763                  articles nil nil gnus-newsgroup-name t)
3764               (gnus-get-newsgroup-headers)))
3765       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
3766
3767       ;; Kludge to avoid having cached articles nixed out in virtual groups.
3768       (when cached
3769         (setq gnus-newsgroup-cached cached))
3770
3771       ;; Suppress duplicates?
3772       (when gnus-suppress-duplicates
3773         (gnus-dup-suppress-articles))
3774
3775       ;; Set the initial limit.
3776       (setq gnus-newsgroup-limit (copy-sequence articles))
3777       ;; Remove canceled articles from the list of unread articles.
3778       (setq gnus-newsgroup-unreads
3779             (gnus-set-sorted-intersection
3780              gnus-newsgroup-unreads
3781              (setq fetched-articles
3782                    (mapcar (lambda (headers) (mail-header-number headers))
3783                            gnus-newsgroup-headers))))
3784       ;; Removed marked articles that do not exist.
3785       (gnus-update-missing-marks
3786        (gnus-sorted-complement fetched-articles articles))
3787       ;; Let the Gnus agent mark articles as read.
3788       (when gnus-agent
3789         (gnus-agent-get-undownloaded-list))
3790       ;; We might want to build some more threads first.
3791       (when (and gnus-fetch-old-headers
3792                  (eq gnus-headers-retrieved-by 'nov))
3793         (if (eq gnus-fetch-old-headers 'invisible)
3794             (gnus-build-all-threads)
3795           (gnus-build-old-threads)))
3796       ;; Check whether auto-expire is to be done in this group.
3797       (setq gnus-newsgroup-auto-expire
3798             (gnus-group-auto-expirable-p group))
3799       ;; Set up the article buffer now, if necessary.
3800       (unless gnus-single-article-buffer
3801         (gnus-article-setup-buffer))
3802       ;; First and last article in this newsgroup.
3803       (when gnus-newsgroup-headers
3804         (setq gnus-newsgroup-begin
3805               (mail-header-number (car gnus-newsgroup-headers))
3806               gnus-newsgroup-end
3807               (mail-header-number
3808                (gnus-last-element gnus-newsgroup-headers))))
3809       ;; GROUP is successfully selected.
3810       (or gnus-newsgroup-headers t)))))
3811
3812 (defun gnus-articles-to-read (group &optional read-all)
3813   ;; Find out what articles the user wants to read.
3814   (let* ((articles
3815           ;; Select all articles if `read-all' is non-nil, or if there
3816           ;; are no unread articles.
3817           (if (or read-all
3818                   (and (zerop (length gnus-newsgroup-marked))
3819                        (zerop (length gnus-newsgroup-unreads)))
3820                   (eq (gnus-group-find-parameter group 'display)
3821                       'all))
3822               (gnus-uncompress-range (gnus-active group))
3823             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
3824                           (copy-sequence gnus-newsgroup-unreads))
3825                   '<)))
3826          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
3827          (scored (length scored-list))
3828          (number (length articles))
3829          (marked (+ (length gnus-newsgroup-marked)
3830                     (length gnus-newsgroup-dormant)))
3831          (select
3832           (cond
3833            ((numberp read-all)
3834             read-all)
3835            (t
3836             (condition-case ()
3837                 (cond
3838                  ((and (or (<= scored marked) (= scored number))
3839                        (numberp gnus-large-newsgroup)
3840                        (> number gnus-large-newsgroup))
3841                   (let ((input
3842                          (read-string
3843                           (format
3844                            "How many articles from %s (default %d): "
3845                            (gnus-limit-string gnus-newsgroup-name 35)
3846                            number))))
3847                     (if (string-match "^[ \t]*$" input) number input)))
3848                  ((and (> scored marked) (< scored number)
3849                        (> (- scored number) 20))
3850                   (let ((input
3851                          (read-string
3852                           (format "%s %s (%d scored, %d total): "
3853                                   "How many articles from"
3854                                   group scored number))))
3855                     (if (string-match "^[ \t]*$" input)
3856                         number input)))
3857                  (t number))
3858               (quit nil))))))
3859     (setq select (if (stringp select) (string-to-number select) select))
3860     (if (or (null select) (zerop select))
3861         select
3862       (if (and (not (zerop scored)) (<= (abs select) scored))
3863           (progn
3864             (setq articles (sort scored-list '<))
3865             (setq number (length articles)))
3866         (setq articles (copy-sequence articles)))
3867
3868       (when (< (abs select) number)
3869         (if (< select 0)
3870             ;; Select the N oldest articles.
3871             (setcdr (nthcdr (1- (abs select)) articles) nil)
3872           ;; Select the N most recent articles.
3873           (setq articles (nthcdr (- number select) articles))))
3874       (setq gnus-newsgroup-unselected
3875             (gnus-sorted-intersection
3876              gnus-newsgroup-unreads
3877              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
3878       articles)))
3879
3880 (defun gnus-killed-articles (killed articles)
3881   (let (out)
3882     (while articles
3883       (when (inline (gnus-member-of-range (car articles) killed))
3884         (push (car articles) out))
3885       (setq articles (cdr articles)))
3886     out))
3887
3888 (defun gnus-uncompress-marks (marks)
3889   "Uncompress the mark ranges in MARKS."
3890   (let ((uncompressed '(score bookmark))
3891         out)
3892     (while marks
3893       (if (memq (caar marks) uncompressed)
3894           (push (car marks) out)
3895         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
3896       (setq marks (cdr marks)))
3897     out))
3898
3899 (defun gnus-adjust-marked-articles (info)
3900   "Set all article lists and remove all marks that are no longer legal."
3901   (let* ((marked-lists (gnus-info-marks info))
3902          (active (gnus-active (gnus-info-group info)))
3903          (min (car active))
3904          (max (cdr active))
3905          (types gnus-article-mark-lists)
3906          (uncompressed '(score bookmark killed))
3907          marks var articles article mark)
3908
3909     (while marked-lists
3910       (setq marks (pop marked-lists))
3911       (set (setq var (intern (format "gnus-newsgroup-%s"
3912                                      (car (rassq (setq mark (car marks))
3913                                                  types)))))
3914            (if (memq (car marks) uncompressed) (cdr marks)
3915              (gnus-uncompress-range (cdr marks))))
3916
3917       (setq articles (symbol-value var))
3918
3919       ;; All articles have to be subsets of the active articles.
3920       (cond
3921        ;; Adjust "simple" lists.
3922        ((memq mark '(tick dormant expire reply save))
3923         (while articles
3924           (when (or (< (setq article (pop articles)) min) (> article max))
3925             (set var (delq article (symbol-value var))))))
3926        ;; Adjust assocs.
3927        ((memq mark uncompressed)
3928         (when (not (listp (cdr (symbol-value var))))
3929           (set var (list (symbol-value var))))
3930         (when (not (listp (cdr articles)))
3931           (setq articles (list articles)))
3932         (while articles
3933           (when (or (not (consp (setq article (pop articles))))
3934                     (< (car article) min)
3935                     (> (car article) max))
3936             (set var (delq article (symbol-value var))))))))))
3937
3938 (defun gnus-update-missing-marks (missing)
3939   "Go through the list of MISSING articles and remove them from the mark lists."
3940   (when missing
3941     (let ((types gnus-article-mark-lists)
3942           var m)
3943       ;; Go through all types.
3944       (while types
3945         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
3946         (when (symbol-value var)
3947           ;; This list has articles.  So we delete all missing articles
3948           ;; from it.
3949           (setq m missing)
3950           (while m
3951             (set var (delq (pop m) (symbol-value var)))))))))
3952
3953 (defun gnus-update-marks ()
3954   "Enter the various lists of marked articles into the newsgroup info list."
3955   (let ((types gnus-article-mark-lists)
3956         (info (gnus-get-info gnus-newsgroup-name))
3957         (uncompressed '(score bookmark killed))
3958         type list newmarked symbol)
3959     (when info
3960       ;; Add all marks lists that are non-nil to the list of marks lists.
3961       (while (setq type (pop types))
3962         (when (setq list (symbol-value
3963                           (setq symbol
3964                                 (intern (format "gnus-newsgroup-%s"
3965                                                 (car type))))))
3966
3967           ;; Get rid of the entries of the articles that have the
3968           ;; default score.
3969           (when (and (eq (cdr type) 'score)
3970                      gnus-save-score
3971                      list)
3972             (let* ((arts list)
3973                    (prev (cons nil list))
3974                    (all prev))
3975               (while arts
3976                 (if (or (not (consp (car arts)))
3977                         (= (cdar arts) gnus-summary-default-score))
3978                     (setcdr prev (cdr arts))
3979                   (setq prev arts))
3980                 (setq arts (cdr arts)))
3981               (setq list (cdr all))))
3982
3983           (push (cons (cdr type)
3984                       (if (memq (cdr type) uncompressed) list
3985                         (gnus-compress-sequence
3986                          (set symbol (sort list '<)) t)))
3987                 newmarked)))
3988
3989       ;; Enter these new marks into the info of the group.
3990       (if (nthcdr 3 info)
3991           (setcar (nthcdr 3 info) newmarked)
3992         ;; Add the marks lists to the end of the info.
3993         (when newmarked
3994           (setcdr (nthcdr 2 info) (list newmarked))))
3995
3996       ;; Cut off the end of the info if there's nothing else there.
3997       (let ((i 5))
3998         (while (and (> i 2)
3999                     (not (nth i info)))
4000           (when (nthcdr (decf i) info)
4001             (setcdr (nthcdr i info) nil)))))))
4002
4003 (defun gnus-set-mode-line (where)
4004   "This function sets the mode line of the article or summary buffers.
4005 If WHERE is `summary', the summary mode line format will be used."
4006   ;; Is this mode line one we keep updated?
4007   (when (memq where gnus-updated-mode-lines)
4008     (let (mode-string)
4009       (save-excursion
4010         ;; We evaluate this in the summary buffer since these
4011         ;; variables are buffer-local to that buffer.
4012         (set-buffer gnus-summary-buffer)
4013         ;; We bind all these variables that are used in the `eval' form
4014         ;; below.
4015         (let* ((mformat (symbol-value
4016                          (intern
4017                           (format "gnus-%s-mode-line-format-spec" where))))
4018                (gnus-tmp-group-name gnus-newsgroup-name)
4019                (gnus-tmp-article-number (or gnus-current-article 0))
4020                (gnus-tmp-unread gnus-newsgroup-unreads)
4021                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4022                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4023                (gnus-tmp-unread-and-unselected
4024                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4025                             (zerop gnus-tmp-unselected))
4026                        "")
4027                       ((zerop gnus-tmp-unselected)
4028                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4029                       (t (format "{%d(+%d) more}"
4030                                  gnus-tmp-unread-and-unticked
4031                                  gnus-tmp-unselected))))
4032                (gnus-tmp-subject
4033                 (if (and gnus-current-headers
4034                          (vectorp gnus-current-headers))
4035                     (gnus-mode-string-quote
4036                      (mail-header-subject gnus-current-headers))
4037                   ""))
4038                bufname-length max-len
4039                gnus-tmp-header);; passed as argument to any user-format-funcs
4040           (setq mode-string (eval mformat))
4041           (setq bufname-length (if (string-match "%b" mode-string)
4042                                    (- (length
4043                                        (buffer-name
4044                                         (if (eq where 'summary)
4045                                             nil
4046                                           (get-buffer gnus-article-buffer))))
4047                                       2)
4048                                  0))
4049           (setq max-len (max 4 (if gnus-mode-non-string-length
4050                                    (- (window-width)
4051                                       gnus-mode-non-string-length
4052                                       bufname-length)
4053                                  (length mode-string))))
4054           ;; We might have to chop a bit of the string off...
4055           (when (> (length mode-string) max-len)
4056             (setq mode-string
4057                   (concat (gnus-truncate-string mode-string (- max-len 3))
4058                           "...")))
4059           ;; Pad the mode string a bit.
4060           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4061       ;; Update the mode line.
4062       (setq mode-line-buffer-identification
4063             (gnus-mode-line-buffer-identification (list mode-string)))
4064       (set-buffer-modified-p t))))
4065
4066 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4067   "Go through the HEADERS list and add all Xrefs to a hash table.
4068 The resulting hash table is returned, or nil if no Xrefs were found."
4069   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4070          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4071          (xref-hashtb (gnus-make-hashtable))
4072          start group entry number xrefs header)
4073     (while headers
4074       (setq header (pop headers))
4075       (when (and (setq xrefs (mail-header-xref header))
4076                  (not (memq (setq number (mail-header-number header))
4077                             unreads)))
4078         (setq start 0)
4079         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4080           (setq start (match-end 0))
4081           (setq group (if prefix
4082                           (concat prefix (substring xrefs (match-beginning 1)
4083                                                     (match-end 1)))
4084                         (substring xrefs (match-beginning 1) (match-end 1))))
4085           (setq number
4086                 (string-to-int (substring xrefs (match-beginning 2)
4087                                           (match-end 2))))
4088           (if (setq entry (gnus-gethash group xref-hashtb))
4089               (setcdr entry (cons number (cdr entry)))
4090             (gnus-sethash group (cons number nil) xref-hashtb)))))
4091     (and start xref-hashtb)))
4092
4093 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4094   "Look through all the headers and mark the Xrefs as read."
4095   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4096         name entry info xref-hashtb idlist method nth4)
4097     (save-excursion
4098       (set-buffer gnus-group-buffer)
4099       (when (setq xref-hashtb
4100                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4101         (mapatoms
4102          (lambda (group)
4103            (unless (string= from-newsgroup (setq name (symbol-name group)))
4104              (setq idlist (symbol-value group))
4105              ;; Dead groups are not updated.
4106              (and (prog1
4107                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4108                             info (nth 2 entry))
4109                     (when (stringp (setq nth4 (gnus-info-method info)))
4110                       (setq nth4 (gnus-server-to-method nth4))))
4111                   ;; Only do the xrefs if the group has the same
4112                   ;; select method as the group we have just read.
4113                   (or (gnus-methods-equal-p
4114                        nth4 (gnus-find-method-for-group from-newsgroup))
4115                       virtual
4116                       (equal nth4 (setq method (gnus-find-method-for-group
4117                                                 from-newsgroup)))
4118                       (and (equal (car nth4) (car method))
4119                            (equal (nth 1 nth4) (nth 1 method))))
4120                   gnus-use-cross-reference
4121                   (or (not (eq gnus-use-cross-reference t))
4122                       virtual
4123                       ;; Only do cross-references on subscribed
4124                       ;; groups, if that is what is wanted.
4125                       (<= (gnus-info-level info) gnus-level-subscribed))
4126                   (gnus-group-make-articles-read name idlist))))
4127          xref-hashtb)))))
4128
4129 (defun gnus-compute-read-articles (group articles)
4130   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4131          (info (nth 2 entry))
4132          (active (gnus-active group))
4133          ninfo)
4134     (when entry
4135       ;; First peel off all illegal article numbers.
4136       (when active
4137         (let ((ids articles)
4138               id first)
4139           (while (setq id (pop ids))
4140             (when (and first (> id (cdr active)))
4141               ;; We'll end up in this situation in one particular
4142               ;; obscure situation.  If you re-scan a group and get
4143               ;; a new article that is cross-posted to a different
4144               ;; group that has not been re-scanned, you might get
4145               ;; crossposted article that has a higher number than
4146               ;; Gnus believes possible.  So we re-activate this
4147               ;; group as well.  This might mean doing the
4148               ;; crossposting thingy will *increase* the number
4149               ;; of articles in some groups.  Tsk, tsk.
4150               (setq active (or (gnus-activate-group group) active)))
4151             (when (or (> id (cdr active))
4152                       (< id (car active)))
4153               (setq articles (delq id articles))))))
4154       ;; If the read list is nil, we init it.
4155       (if (and active
4156                (null (gnus-info-read info))
4157                (> (car active) 1))
4158           (setq ninfo (cons 1 (1- (car active))))
4159         (setq ninfo (gnus-info-read info)))
4160       ;; Then we add the read articles to the range.
4161       (gnus-add-to-range
4162        ninfo (setq articles (sort articles '<))))))
4163   
4164 (defun gnus-group-make-articles-read (group articles)
4165   "Update the info of GROUP to say that ARTICLES are read."
4166   (let* ((num 0)
4167          (entry (gnus-gethash group gnus-newsrc-hashtb))
4168          (info (nth 2 entry))
4169          (active (gnus-active group))
4170          range)
4171     (when entry
4172       (setq range (gnus-compute-read-articles group articles))
4173       (save-excursion
4174         (set-buffer gnus-group-buffer)
4175         (gnus-undo-register
4176           `(progn
4177              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4178              (gnus-info-set-read ',info ',(gnus-info-read info))
4179              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4180              (gnus-group-update-group ,group t))))
4181       ;; Add the read articles to the range.
4182       (gnus-info-set-read info range)
4183       ;; Then we have to re-compute how many unread
4184       ;; articles there are in this group.
4185       (when active
4186         (cond
4187          ((not range)
4188           (setq num (- (1+ (cdr active)) (car active))))
4189          ((not (listp (cdr range)))
4190           (setq num (- (cdr active) (- (1+ (cdr range))
4191                                        (car range)))))
4192          (t
4193           (while range
4194             (if (numberp (car range))
4195                 (setq num (1+ num))
4196               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4197             (setq range (cdr range)))
4198           (setq num (- (cdr active) num))))
4199         ;; Update the number of unread articles.
4200         (setcar entry num)
4201         ;; Update the group buffer.
4202         (gnus-group-update-group group t)))))
4203
4204 (defun gnus-methods-equal-p (m1 m2)
4205   (let ((m1 (or m1 gnus-select-method))
4206         (m2 (or m2 gnus-select-method)))
4207     (or (equal m1 m2)
4208         (and (eq (car m1) (car m2))
4209              (or (not (memq 'address (assoc (symbol-name (car m1))
4210                                             gnus-valid-select-methods)))
4211                  (equal (nth 1 m1) (nth 1 m2)))))))
4212
4213 (defvar gnus-newsgroup-none-id 0)
4214
4215 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4216   (let ((cur nntp-server-buffer)
4217         (dependencies
4218          (or dependencies
4219              (save-excursion (set-buffer gnus-summary-buffer)
4220                              gnus-newsgroup-dependencies)))
4221         headers id id-dep ref-dep end ref)
4222     (save-excursion
4223       (set-buffer nntp-server-buffer)
4224       ;; Translate all TAB characters into SPACE characters.
4225       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4226       (gnus-run-hooks 'gnus-parse-headers-hook)
4227       (let ((case-fold-search t)
4228             in-reply-to header p lines)
4229         (goto-char (point-min))
4230         ;; Search to the beginning of the next header.  Error messages
4231         ;; do not begin with 2 or 3.
4232         (while (re-search-forward "^[23][0-9]+ " nil t)
4233           (setq id nil
4234                 ref nil)
4235           ;; This implementation of this function, with nine
4236           ;; search-forwards instead of the one re-search-forward and
4237           ;; a case (which basically was the old function) is actually
4238           ;; about twice as fast, even though it looks messier.  You
4239           ;; can't have everything, I guess.  Speed and elegance
4240           ;; doesn't always go hand in hand.
4241           (setq
4242            header
4243            (vector
4244             ;; Number.
4245             (prog1
4246                 (read cur)
4247               (end-of-line)
4248               (setq p (point))
4249               (narrow-to-region (point)
4250                                 (or (and (search-forward "\n.\n" nil t)
4251                                          (- (point) 2))
4252                                     (point))))
4253             ;; Subject.
4254             (progn
4255               (goto-char p)
4256               (if (search-forward "\nsubject: " nil t)
4257                   (funcall
4258                    gnus-unstructured-field-decoder (nnheader-header-value))
4259                 "(none)"))
4260             ;; From.
4261             (progn
4262               (goto-char p)
4263               (if (search-forward "\nfrom: " nil t)
4264                   (funcall
4265                    gnus-structured-field-decoder (nnheader-header-value))
4266                 "(nobody)"))
4267             ;; Date.
4268             (progn
4269               (goto-char p)
4270               (if (search-forward "\ndate: " nil t)
4271                   (nnheader-header-value) ""))
4272             ;; Message-ID.
4273             (progn
4274               (goto-char p)
4275               (setq id (if (re-search-forward
4276                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4277                            ;; We do it this way to make sure the Message-ID
4278                            ;; is (somewhat) syntactically valid.
4279                            (buffer-substring (match-beginning 1)
4280                                              (match-end 1))
4281                          ;; If there was no message-id, we just fake one
4282                          ;; to make subsequent routines simpler.
4283                          (nnheader-generate-fake-message-id))))
4284             ;; References.
4285             (progn
4286               (goto-char p)
4287               (if (search-forward "\nreferences: " nil t)
4288                   (progn
4289                     (setq end (point))
4290                     (prog1
4291                         (nnheader-header-value)
4292                       (setq ref
4293                             (buffer-substring
4294                              (progn
4295                                (end-of-line)
4296                                (search-backward ">" end t)
4297                                (1+ (point)))
4298                              (progn
4299                                (search-backward "<" end t)
4300                                (point))))))
4301                 ;; Get the references from the in-reply-to header if there
4302                 ;; were no references and the in-reply-to header looks
4303                 ;; promising.
4304                 (if (and (search-forward "\nin-reply-to: " nil t)
4305                          (setq in-reply-to (nnheader-header-value))
4306                          (string-match "<[^>]+>" in-reply-to))
4307                     (let (ref2)
4308                       (setq ref (substring in-reply-to (match-beginning 0)
4309                                            (match-end 0)))
4310                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4311                         (setq ref2 (substring in-reply-to (match-beginning 0)
4312                                               (match-end 0)))
4313                         (when (> (length ref2) (length ref))
4314                           (setq ref ref2))))
4315                   (setq ref nil))))
4316             ;; Chars.
4317             0
4318             ;; Lines.
4319             (progn
4320               (goto-char p)
4321               (if (search-forward "\nlines: " nil t)
4322                   (if (numberp (setq lines (ignore-errors (read cur))))
4323                       lines 0)
4324                 0))
4325             ;; Xref.
4326             (progn
4327               (goto-char p)
4328               (and (search-forward "\nxref: " nil t)
4329                    (nnheader-header-value)))))
4330           (when (equal id ref)
4331             (setq ref nil))
4332
4333           (when gnus-alter-header-function
4334             (funcall gnus-alter-header-function header)
4335             (setq id (mail-header-id header)
4336                   ref (gnus-parent-id (mail-header-references header))))
4337     
4338           ;; We do the threading while we read the headers.  The
4339           ;; message-id and the last reference are both entered into
4340           ;; the same hash table.  Some tippy-toeing around has to be
4341           ;; done in case an article has arrived before the article
4342           ;; which it refers to.
4343           (if (boundp (setq id-dep (intern id dependencies)))
4344               (if (and (car (symbol-value id-dep))
4345                        (not force-new))
4346                   ;; An article with this Message-ID has already been seen.
4347                   (if gnus-summary-ignore-duplicates
4348                       ;; We ignore this one, except we add
4349                       ;; any additional Xrefs (in case the two articles
4350                       ;; came from different servers).
4351                       (progn
4352                         (mail-header-set-xref
4353                          (car (symbol-value id-dep))
4354                          (concat (or (mail-header-xref
4355                                       (car (symbol-value id-dep)))
4356                                      "")
4357                                  (or (mail-header-xref header) "")))
4358                         (setq header nil))
4359                     ;; We rename the Message-ID.
4360                     (set
4361                      (setq id-dep (intern (setq id (nnmail-message-id))
4362                                           dependencies))
4363                      (list header))
4364                     (mail-header-set-id header id))
4365                 (setcar (symbol-value id-dep) header))
4366             (set id-dep (list header)))
4367           (when header
4368             (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
4369                 (setcdr (symbol-value ref-dep)
4370                         (nconc (cdr (symbol-value ref-dep))
4371                                (list (symbol-value id-dep))))
4372               (set ref-dep (list nil (symbol-value id-dep))))
4373             (push header headers))
4374           (goto-char (point-max))
4375           (widen))
4376         (nreverse headers)))))
4377
4378 ;; The following macros and functions were written by Felix Lee
4379 ;; <flee@cse.psu.edu>.
4380
4381 (defmacro gnus-nov-read-integer ()
4382   '(prog1
4383        (if (= (following-char) ?\t)
4384            0
4385          (let ((num (ignore-errors (read buffer))))
4386            (if (numberp num) num 0)))
4387      (unless (eobp)
4388        (search-forward "\t" eol 'move))))
4389
4390 (defmacro gnus-nov-skip-field ()
4391   '(search-forward "\t" eol 'move))
4392
4393 (defmacro gnus-nov-field ()
4394   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
4395
4396 ;; (defvar gnus-nov-none-counter 0)
4397
4398 ;; This function has to be called with point after the article number
4399 ;; on the beginning of the line.
4400 (defun gnus-nov-parse-line (number dependencies &optional force-new)
4401   (let ((eol (gnus-point-at-eol))
4402         (buffer (current-buffer))
4403         header ref id id-dep ref-dep)
4404
4405     ;; overview: [num subject from date id refs chars lines misc]
4406     (unwind-protect
4407         (progn
4408           (narrow-to-region (point) eol)
4409           (unless (eobp)
4410             (forward-char))
4411
4412           (setq header
4413                 (vector
4414                  number                 ; number
4415                  (funcall
4416                   gnus-unstructured-field-decoder (gnus-nov-field)) ; subject
4417                  (funcall
4418                   gnus-structured-field-decoder (gnus-nov-field)) ; from
4419                  (gnus-nov-field)       ; date
4420                  (setq id (or (gnus-nov-field)
4421                               (nnheader-generate-fake-message-id))) ; id
4422                  (progn
4423                    (let ((beg (point)))
4424                      (search-forward "\t" eol)
4425                      (if (search-backward ">" beg t)
4426                          (setq ref
4427                                (buffer-substring
4428                                 (1+ (point))
4429                                 (or (search-backward "<" beg t) beg)))
4430                        (setq ref nil))
4431                      (goto-char beg))
4432                    (gnus-nov-field))    ; refs
4433                  (gnus-nov-read-integer) ; chars
4434                  (gnus-nov-read-integer) ; lines
4435                  (if (= (following-char) ?\n)
4436                      nil
4437                    (gnus-nov-field))))) ; misc
4438
4439       (widen))
4440
4441     (when gnus-alter-header-function
4442       (funcall gnus-alter-header-function header)
4443       (setq id (mail-header-id header)
4444             ref (gnus-parent-id (mail-header-references header))))
4445     
4446     ;; We build the thread tree.
4447     (when (equal id ref)
4448       ;; This article refers back to itself.  Naughty, naughty.
4449       (setq ref nil))
4450     (if (boundp (setq id-dep (intern id dependencies)))
4451         (if (and (car (symbol-value id-dep))
4452                  (not force-new))
4453             ;; An article with this Message-ID has already been seen.
4454             (if gnus-summary-ignore-duplicates
4455                 ;; We ignore this one, except we add any additional
4456                 ;; Xrefs (in case the two articles came from different
4457                 ;; servers.
4458                 (progn
4459                   (mail-header-set-xref
4460                    (car (symbol-value id-dep))
4461                    (concat (or (mail-header-xref
4462                                 (car (symbol-value id-dep)))
4463                                "")
4464                            (or (mail-header-xref header) "")))
4465                   (setq header nil))
4466               ;; We rename the Message-ID.
4467               (set
4468                (setq id-dep (intern (setq id (nnmail-message-id))
4469                                     dependencies))
4470                (list header))
4471               (mail-header-set-id header id))
4472           (setcar (symbol-value id-dep) header))
4473       (set id-dep (list header)))
4474     (when header
4475       (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
4476           (setcdr (symbol-value ref-dep)
4477                   (nconc (cdr (symbol-value ref-dep))
4478                          (list (symbol-value id-dep))))
4479         (set ref-dep (list nil (symbol-value id-dep)))))
4480     header))
4481
4482 ;; Goes through the xover lines and returns a list of vectors
4483 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4484                                                   force-new dependencies
4485                                                   group also-fetch-heads)
4486   "Parse the news overview data in the server buffer, and return a
4487 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4488   ;; Get the Xref when the users reads the articles since most/some
4489   ;; NNTP servers do not include Xrefs when using XOVER.
4490   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4491   (let ((cur nntp-server-buffer)
4492         (dependencies (or dependencies gnus-newsgroup-dependencies))
4493         number headers header)
4494     (save-excursion
4495       (set-buffer nntp-server-buffer)
4496       ;; Allow the user to mangle the headers before parsing them.
4497       (gnus-run-hooks 'gnus-parse-headers-hook)
4498       (goto-char (point-min))
4499       (while (not (eobp))
4500         (condition-case ()
4501             (while (and sequence (not (eobp)))
4502               (setq number (read cur))
4503               (while (and sequence
4504                           (< (car sequence) number))
4505                 (setq sequence (cdr sequence)))
4506               (and sequence
4507                    (eq number (car sequence))
4508                    (progn
4509                      (setq sequence (cdr sequence))
4510                      (setq header (inline
4511                                     (gnus-nov-parse-line
4512                                      number dependencies force-new))))
4513                    (push header headers))
4514               (forward-line 1))
4515           (error
4516            (gnus-error 4 "Strange nov line (%d)"
4517                        (count-lines (point-min) (point)))))
4518         (forward-line 1))
4519       ;; A common bug in inn is that if you have posted an article and
4520       ;; then retrieves the active file, it will answer correctly --
4521       ;; the new article is included.  However, a NOV entry for the
4522       ;; article may not have been generated yet, so this may fail.
4523       ;; We work around this problem by retrieving the last few
4524       ;; headers using HEAD.
4525       (if (or (not also-fetch-heads)
4526               (not sequence))
4527           ;; We (probably) got all the headers.
4528           (nreverse headers)
4529         (let ((gnus-nov-is-evil t))
4530           (nconc
4531            (nreverse headers)
4532            (when (gnus-retrieve-headers sequence group)
4533              (gnus-get-newsgroup-headers))))))))
4534
4535 (defun gnus-article-get-xrefs ()
4536   "Fill in the Xref value in `gnus-current-headers', if necessary.
4537 This is meant to be called in `gnus-article-internal-prepare-hook'."
4538   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4539                                  gnus-current-headers)))
4540     (or (not gnus-use-cross-reference)
4541         (not headers)
4542         (and (mail-header-xref headers)
4543              (not (string= (mail-header-xref headers) "")))
4544         (let ((case-fold-search t)
4545               xref)
4546           (save-restriction
4547             (nnheader-narrow-to-headers)
4548             (goto-char (point-min))
4549             (when (or (and (eq (downcase (following-char)) ?x)
4550                            (looking-at "Xref:"))
4551                       (search-forward "\nXref:" nil t))
4552               (goto-char (1+ (match-end 0)))
4553               (setq xref (buffer-substring (point)
4554                                            (progn (end-of-line) (point))))
4555               (mail-header-set-xref headers xref)))))))
4556
4557 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4558   "Find article ID and insert the summary line for that article."
4559   (let ((header (cond ((and old-header use-old-header)
4560                        old-header)
4561                       ((and (numberp id)
4562                             (gnus-number-to-header id))
4563                        (gnus-number-to-header id))
4564                       (t
4565                        (gnus-read-header id))))
4566         (number (and (numberp id) id))
4567         pos d)
4568     (when header
4569       ;; Rebuild the thread that this article is part of and go to the
4570       ;; article we have fetched.
4571       (when (and (not gnus-show-threads)
4572                  old-header)
4573         (when (and number
4574                    (setq d (gnus-data-find (mail-header-number old-header))))
4575           (goto-char (gnus-data-pos d))
4576           (gnus-data-remove
4577            number
4578            (- (gnus-point-at-bol)
4579               (prog1
4580                   (1+ (gnus-point-at-eol))
4581                 (gnus-delete-line))))))
4582       (when old-header
4583         (mail-header-set-number header (mail-header-number old-header)))
4584       (setq gnus-newsgroup-sparse
4585             (delq (setq number (mail-header-number header))
4586                   gnus-newsgroup-sparse))
4587       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4588       (push number gnus-newsgroup-limit)
4589       (gnus-rebuild-thread (mail-header-id header))
4590       (gnus-summary-goto-subject number nil t))
4591     (when (and (numberp number)
4592                (> number 0))
4593       ;; We have to update the boundaries even if we can't fetch the
4594       ;; article if ID is a number -- so that the next `P' or `N'
4595       ;; command will fetch the previous (or next) article even
4596       ;; if the one we tried to fetch this time has been canceled.
4597       (when (> number gnus-newsgroup-end)
4598         (setq gnus-newsgroup-end number))
4599       (when (< number gnus-newsgroup-begin)
4600         (setq gnus-newsgroup-begin number))
4601       (setq gnus-newsgroup-unselected
4602             (delq number gnus-newsgroup-unselected)))
4603     ;; Report back a success?
4604     (and header (mail-header-number header))))
4605
4606 ;;; Process/prefix in the summary buffer
4607
4608 (defun gnus-summary-work-articles (n)
4609   "Return a list of articles to be worked upon.  The prefix argument,
4610 the list of process marked articles, and the current article will be
4611 taken into consideration."
4612   (save-excursion
4613     (set-buffer gnus-summary-buffer)
4614     (cond
4615      (n
4616       ;; A numerical prefix has been given.
4617       (setq n (prefix-numeric-value n))
4618       (let ((backward (< n 0))
4619             (n (abs (prefix-numeric-value n)))
4620             articles article)
4621         (save-excursion
4622           (while
4623               (and (> n 0)
4624                    (push (setq article (gnus-summary-article-number))
4625                          articles)
4626                    (if backward
4627                        (gnus-summary-find-prev nil article)
4628                      (gnus-summary-find-next nil article)))
4629             (decf n)))
4630         (nreverse articles)))
4631      ((and (gnus-region-active-p) (mark))
4632       (message "region active")
4633       ;; Work on the region between point and mark.
4634       (let ((max (max (point) (mark)))
4635             articles article)
4636         (save-excursion
4637           (goto-char (min (min (point) (mark))))
4638           (while
4639               (and
4640                (push (setq article (gnus-summary-article-number)) articles)
4641                (gnus-summary-find-next nil article)
4642                (< (point) max)))
4643           (nreverse articles))))
4644      (gnus-newsgroup-processable
4645       ;; There are process-marked articles present.
4646       ;; Save current state.
4647       (gnus-summary-save-process-mark)
4648       ;; Return the list.
4649       (reverse gnus-newsgroup-processable))
4650      (t
4651       ;; Just return the current article.
4652       (list (gnus-summary-article-number))))))
4653
4654 (defun gnus-summary-save-process-mark ()
4655   "Push the current set of process marked articles on the stack."
4656   (interactive)
4657   (push (copy-sequence gnus-newsgroup-processable)
4658         gnus-newsgroup-process-stack))
4659
4660 (defun gnus-summary-kill-process-mark ()
4661   "Push the current set of process marked articles on the stack and unmark."
4662   (interactive)
4663   (gnus-summary-save-process-mark)
4664   (gnus-summary-unmark-all-processable))
4665
4666 (defun gnus-summary-yank-process-mark ()
4667   "Pop the last process mark state off the stack and restore it."
4668   (interactive)
4669   (unless gnus-newsgroup-process-stack
4670     (error "Empty mark stack"))
4671   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4672
4673 (defun gnus-summary-process-mark-set (set)
4674   "Make SET into the current process marked articles."
4675   (gnus-summary-unmark-all-processable)
4676   (while set
4677     (gnus-summary-set-process-mark (pop set))))
4678
4679 ;;; Searching and stuff
4680
4681 (defun gnus-summary-search-group (&optional backward use-level)
4682   "Search for next unread newsgroup.
4683 If optional argument BACKWARD is non-nil, search backward instead."
4684   (save-excursion
4685     (set-buffer gnus-group-buffer)
4686     (when (gnus-group-search-forward
4687            backward nil (if use-level (gnus-group-group-level) nil))
4688       (gnus-group-group-name))))
4689
4690 (defun gnus-summary-best-group (&optional exclude-group)
4691   "Find the name of the best unread group.
4692 If EXCLUDE-GROUP, do not go to this group."
4693   (save-excursion
4694     (set-buffer gnus-group-buffer)
4695     (save-excursion
4696       (gnus-group-best-unread-group exclude-group))))
4697
4698 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4699   (if backward (gnus-summary-find-prev)
4700     (let* ((dummy (gnus-summary-article-intangible-p))
4701            (article (or article (gnus-summary-article-number)))
4702            (arts (gnus-data-find-list article))
4703            result)
4704       (when (and (not dummy)
4705                  (or (not gnus-summary-check-current)
4706                      (not unread)
4707                      (not (gnus-data-unread-p (car arts)))))
4708         (setq arts (cdr arts)))
4709       (when (setq result
4710                   (if unread
4711                       (progn
4712                         (while arts
4713                           (when (or (and undownloaded
4714                                          (eq gnus-undownloaded-mark
4715                                              (gnus-data-mark (car arts))))
4716                                     (gnus-data-unread-p (car arts)))
4717                             (setq result (car arts)
4718                                   arts nil))
4719                           (setq arts (cdr arts)))
4720                         result)
4721                     (car arts)))
4722         (goto-char (gnus-data-pos result))
4723         (gnus-data-number result)))))
4724
4725 (defun gnus-summary-find-prev (&optional unread article)
4726   (let* ((eobp (eobp))
4727          (article (or article (gnus-summary-article-number)))
4728          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4729          result)
4730     (when (and (not eobp)
4731                (or (not gnus-summary-check-current)
4732                    (not unread)
4733                    (not (gnus-data-unread-p (car arts)))))
4734       (setq arts (cdr arts)))
4735     (when (setq result
4736                 (if unread
4737                     (progn
4738                       (while arts
4739                         (when (gnus-data-unread-p (car arts))
4740                           (setq result (car arts)
4741                                 arts nil))
4742                         (setq arts (cdr arts)))
4743                       result)
4744                   (car arts)))
4745       (goto-char (gnus-data-pos result))
4746       (gnus-data-number result))))
4747
4748 (defun gnus-summary-find-subject (subject &optional unread backward article)
4749   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4750          (article (or article (gnus-summary-article-number)))
4751          (articles (gnus-data-list backward))
4752          (arts (gnus-data-find-list article articles))
4753          result)
4754     (when (or (not gnus-summary-check-current)
4755               (not unread)
4756               (not (gnus-data-unread-p (car arts))))
4757       (setq arts (cdr arts)))
4758     (while arts
4759       (and (or (not unread)
4760                (gnus-data-unread-p (car arts)))
4761            (vectorp (gnus-data-header (car arts)))
4762            (gnus-subject-equal
4763             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4764            (setq result (car arts)
4765                  arts nil))
4766       (setq arts (cdr arts)))
4767     (and result
4768          (goto-char (gnus-data-pos result))
4769          (gnus-data-number result))))
4770
4771 (defun gnus-summary-search-forward (&optional unread subject backward)
4772   "Search forward for an article.
4773 If UNREAD, look for unread articles.  If SUBJECT, look for
4774 articles with that subject.  If BACKWARD, search backward instead."
4775   (cond (subject (gnus-summary-find-subject subject unread backward))
4776         (backward (gnus-summary-find-prev unread))
4777         (t (gnus-summary-find-next unread))))
4778
4779 (defun gnus-recenter (&optional n)
4780   "Center point in window and redisplay frame.
4781 Also do horizontal recentering."
4782   (interactive "P")
4783   (when (and gnus-auto-center-summary
4784              (not (eq gnus-auto-center-summary 'vertical)))
4785     (gnus-horizontal-recenter))
4786   (recenter n))
4787
4788 (defun gnus-summary-recenter ()
4789   "Center point in the summary window.
4790 If `gnus-auto-center-summary' is nil, or the article buffer isn't
4791 displayed, no centering will be performed."
4792   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
4793   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
4794   (let* ((top (cond ((< (window-height) 4) 0)
4795                     ((< (window-height) 7) 1)
4796                     (t 2)))
4797          (height (1- (window-height)))
4798          (bottom (save-excursion (goto-char (point-max))
4799                                  (forward-line (- height))
4800                                  (point)))
4801          (window (get-buffer-window (current-buffer))))
4802     ;; The user has to want it.
4803     (when gnus-auto-center-summary
4804       (when (get-buffer-window gnus-article-buffer)
4805         ;; Only do recentering when the article buffer is displayed,
4806         ;; Set the window start to either `bottom', which is the biggest
4807         ;; possible valid number, or the second line from the top,
4808         ;; whichever is the least.
4809         (set-window-start
4810          window (min bottom (save-excursion
4811                               (forward-line (- top)) (point)))))
4812       ;; Do horizontal recentering while we're at it.
4813       (when (and (get-buffer-window (current-buffer) t)
4814                  (not (eq gnus-auto-center-summary 'vertical)))
4815         (let ((selected (selected-window)))
4816           (select-window (get-buffer-window (current-buffer) t))
4817           (gnus-summary-position-point)
4818           (gnus-horizontal-recenter)
4819           (select-window selected))))))
4820
4821 (defun gnus-summary-jump-to-group (newsgroup)
4822   "Move point to NEWSGROUP in group mode buffer."
4823   ;; Keep update point of group mode buffer if visible.
4824   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
4825       (save-window-excursion
4826         ;; Take care of tree window mode.
4827         (when (get-buffer-window gnus-group-buffer)
4828           (pop-to-buffer gnus-group-buffer))
4829         (gnus-group-jump-to-group newsgroup))
4830     (save-excursion
4831       ;; Take care of tree window mode.
4832       (if (get-buffer-window gnus-group-buffer)
4833           (pop-to-buffer gnus-group-buffer)
4834         (set-buffer gnus-group-buffer))
4835       (gnus-group-jump-to-group newsgroup))))
4836
4837 ;; This function returns a list of article numbers based on the
4838 ;; difference between the ranges of read articles in this group and
4839 ;; the range of active articles.
4840 (defun gnus-list-of-unread-articles (group)
4841   (let* ((read (gnus-info-read (gnus-get-info group)))
4842          (active (or (gnus-active group) (gnus-activate-group group)))
4843          (last (cdr active))
4844          first nlast unread)
4845     ;; If none are read, then all are unread.
4846     (if (not read)
4847         (setq first (car active))
4848       ;; If the range of read articles is a single range, then the
4849       ;; first unread article is the article after the last read
4850       ;; article.  Sounds logical, doesn't it?
4851       (if (not (listp (cdr read)))
4852           (setq first (1+ (cdr read)))
4853         ;; `read' is a list of ranges.
4854         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
4855                                   (caar read)))
4856                   1)
4857           (setq first 1))
4858         (while read
4859           (when first
4860             (while (< first nlast)
4861               (push first unread)
4862               (setq first (1+ first))))
4863           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
4864           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
4865           (setq read (cdr read)))))
4866     ;; And add the last unread articles.
4867     (while (<= first last)
4868       (push first unread)
4869       (setq first (1+ first)))
4870     ;; Return the list of unread articles.
4871     (delq 0 (nreverse unread))))
4872
4873 (defun gnus-list-of-read-articles (group)
4874   "Return a list of unread, unticked and non-dormant articles."
4875   (let* ((info (gnus-get-info group))
4876          (marked (gnus-info-marks info))
4877          (active (gnus-active group)))
4878     (and info active
4879          (gnus-set-difference
4880           (gnus-sorted-complement
4881            (gnus-uncompress-range active)
4882            (gnus-list-of-unread-articles group))
4883           (append
4884            (gnus-uncompress-range (cdr (assq 'dormant marked)))
4885            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
4886
4887 ;; Various summary commands
4888
4889 (defun gnus-summary-select-article-buffer ()
4890   "Reconfigure windows to show article buffer."
4891   (interactive)
4892   (if (not (gnus-buffer-live-p gnus-article-buffer))
4893       (error "There is no article buffer for this summary buffer")
4894     (gnus-configure-windows 'article)
4895     (select-window (get-buffer-window gnus-article-buffer))))
4896
4897 (defun gnus-summary-universal-argument (arg)
4898   "Perform any operation on all articles that are process/prefixed."
4899   (interactive "P")
4900   (let ((articles (gnus-summary-work-articles arg))
4901         func article)
4902     (if (eq
4903          (setq
4904           func
4905           (key-binding
4906            (read-key-sequence
4907             (substitute-command-keys
4908              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
4909              ))))
4910          'undefined)
4911         (gnus-error 1 "Undefined key")
4912       (save-excursion
4913         (while articles
4914           (gnus-summary-goto-subject (setq article (pop articles)))
4915           (let (gnus-newsgroup-processable)
4916             (command-execute func))
4917           (gnus-summary-remove-process-mark article)))))
4918   (gnus-summary-position-point))
4919
4920 (defun gnus-summary-toggle-truncation (&optional arg)
4921   "Toggle truncation of summary lines.
4922 With arg, turn line truncation on iff arg is positive."
4923   (interactive "P")
4924   (setq truncate-lines
4925         (if (null arg) (not truncate-lines)
4926           (> (prefix-numeric-value arg) 0)))
4927   (redraw-display))
4928
4929 (defun gnus-summary-reselect-current-group (&optional all rescan)
4930   "Exit and then reselect the current newsgroup.
4931 The prefix argument ALL means to select all articles."
4932   (interactive "P")
4933   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
4934     (error "Ephemeral groups can't be reselected"))
4935   (let ((current-subject (gnus-summary-article-number))
4936         (group gnus-newsgroup-name))
4937     (setq gnus-newsgroup-begin nil)
4938     (gnus-summary-exit)
4939     ;; We have to adjust the point of group mode buffer because
4940     ;; point was moved to the next unread newsgroup by exiting.
4941     (gnus-summary-jump-to-group group)
4942     (when rescan
4943       (save-excursion
4944         (gnus-group-get-new-news-this-group 1)))
4945     (gnus-group-read-group all t)
4946     (gnus-summary-goto-subject current-subject nil t)))
4947
4948 (defun gnus-summary-rescan-group (&optional all)
4949   "Exit the newsgroup, ask for new articles, and select the newsgroup."
4950   (interactive "P")
4951   (gnus-summary-reselect-current-group all t))
4952
4953 (defun gnus-summary-update-info (&optional non-destructive)
4954   (save-excursion
4955     (let ((group gnus-newsgroup-name))
4956       (when group
4957         (when gnus-newsgroup-kill-headers
4958           (setq gnus-newsgroup-killed
4959                 (gnus-compress-sequence
4960                  (nconc
4961                   (gnus-set-sorted-intersection
4962                    (gnus-uncompress-range gnus-newsgroup-killed)
4963                    (setq gnus-newsgroup-unselected
4964                          (sort gnus-newsgroup-unselected '<)))
4965                   (setq gnus-newsgroup-unreads
4966                         (sort gnus-newsgroup-unreads '<)))
4967                  t)))
4968         (unless (listp (cdr gnus-newsgroup-killed))
4969           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
4970         (let ((headers gnus-newsgroup-headers))
4971           ;; Set the new ranges of read articles.
4972           (save-excursion
4973             (set-buffer gnus-group-buffer)
4974             (gnus-undo-force-boundary))
4975           (gnus-update-read-articles
4976            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
4977           ;; Set the current article marks.
4978           (let ((gnus-newsgroup-scored 
4979                  (if (and (not gnus-save-score)
4980                           (not non-destructive))
4981                      nil
4982                    gnus-newsgroup-scored)))
4983             (save-excursion
4984               (gnus-update-marks)))
4985           ;; Do the cross-ref thing.
4986           (when gnus-use-cross-reference
4987             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
4988           ;; Do not switch windows but change the buffer to work.
4989           (set-buffer gnus-group-buffer)
4990           (unless (gnus-ephemeral-group-p group)
4991             (gnus-group-update-group group)))))))
4992
4993 (defun gnus-summary-save-newsrc (&optional force)
4994   "Save the current number of read/marked articles in the dribble buffer.
4995 The dribble buffer will then be saved.
4996 If FORCE (the prefix), also save the .newsrc file(s)."
4997   (interactive "P")
4998   (gnus-summary-update-info t)
4999   (if force
5000       (gnus-save-newsrc-file)
5001     (gnus-dribble-save)))
5002
5003 (defun gnus-summary-exit (&optional temporary)
5004   "Exit reading current newsgroup, and then return to group selection mode.
5005 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5006   (interactive)
5007   (gnus-set-global-variables)
5008   (gnus-kill-save-kill-buffer)
5009   (gnus-async-halt-prefetch)
5010   (let* ((group gnus-newsgroup-name)
5011          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5012          (mode major-mode)
5013          (group-point nil)
5014          (buf (current-buffer)))
5015     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5016     ;; If we have several article buffers, we kill them at exit.
5017     (unless gnus-single-article-buffer
5018       (gnus-kill-buffer gnus-original-article-buffer)
5019       (setq gnus-article-current nil))
5020     (when gnus-use-cache
5021       (gnus-cache-possibly-remove-articles)
5022       (gnus-cache-save-buffers))
5023     (gnus-async-prefetch-remove-group group)
5024     (when gnus-suppress-duplicates
5025       (gnus-dup-enter-articles))
5026     (when gnus-use-trees
5027       (gnus-tree-close group))
5028     ;; Remove entries for this group.
5029     (nnmail-purge-split-history (gnus-group-real-name group))
5030     ;; Make all changes in this group permanent.
5031     (unless quit-config
5032       (gnus-run-hooks 'gnus-exit-group-hook)
5033       (gnus-summary-update-info)
5034       ;; Do adaptive scoring, and possibly save score files.
5035       (when gnus-newsgroup-adaptive
5036         (gnus-score-adaptive))
5037       (when gnus-use-scoring
5038         (gnus-score-save)))
5039     (gnus-close-group group)
5040     ;; Make sure where we were, and go to next newsgroup.
5041     (set-buffer gnus-group-buffer)
5042     (unless quit-config
5043       (gnus-group-jump-to-group group))
5044     (gnus-run-hooks 'gnus-summary-exit-hook)
5045     (unless (or quit-config
5046                 ;; If this group has disappeared from the summary
5047                 ;; buffer, don't skip forwards.
5048                 (not (string= group (gnus-group-group-name))))
5049       (gnus-group-next-unread-group 1))
5050     (setq group-point (point))
5051     (if temporary
5052         nil                             ;Nothing to do.
5053       ;; If we have several article buffers, we kill them at exit.
5054       (unless gnus-single-article-buffer
5055         (gnus-kill-buffer gnus-article-buffer)
5056         (gnus-kill-buffer gnus-original-article-buffer)
5057         (setq gnus-article-current nil))
5058       (set-buffer buf)
5059       (if (not gnus-kill-summary-on-exit)
5060           (gnus-deaden-summary)
5061         ;; We set all buffer-local variables to nil.  It is unclear why
5062         ;; this is needed, but if we don't, buffer-local variables are
5063         ;; not garbage-collected, it seems.  This would the lead to en
5064         ;; ever-growing Emacs.
5065         (gnus-summary-clear-local-variables)
5066         (when (get-buffer gnus-article-buffer)
5067           (bury-buffer gnus-article-buffer))
5068         ;; We clear the global counterparts of the buffer-local
5069         ;; variables as well, just to be on the safe side.
5070         (set-buffer gnus-group-buffer)
5071         (gnus-summary-clear-local-variables)
5072         ;; Return to group mode buffer.
5073         (when (eq mode 'gnus-summary-mode)
5074           (gnus-kill-buffer buf)))
5075       (setq gnus-current-select-method gnus-select-method)
5076       (pop-to-buffer gnus-group-buffer)
5077       ;; Clear the current group name.
5078       (if (not quit-config)
5079           (progn
5080             (goto-char group-point)
5081             (gnus-configure-windows 'group 'force))
5082         (gnus-handle-ephemeral-exit quit-config))
5083       (unless quit-config
5084         (setq gnus-newsgroup-name nil)))))
5085
5086 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5087 (defun gnus-summary-exit-no-update (&optional no-questions)
5088   "Quit reading current newsgroup without updating read article info."
5089   (interactive)
5090   (let* ((group gnus-newsgroup-name)
5091          (quit-config (gnus-group-quit-config group)))
5092     (when (or no-questions
5093               gnus-expert-user
5094               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5095       (gnus-async-halt-prefetch)
5096       (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5097       ;; If we have several article buffers, we kill them at exit.
5098       (unless gnus-single-article-buffer
5099         (gnus-kill-buffer gnus-article-buffer)
5100         (gnus-kill-buffer gnus-original-article-buffer)
5101         (setq gnus-article-current nil))
5102       (if (not gnus-kill-summary-on-exit)
5103           (gnus-deaden-summary)
5104         (gnus-close-group group)
5105         (gnus-summary-clear-local-variables)
5106         (set-buffer gnus-group-buffer)
5107         (gnus-summary-clear-local-variables)
5108         (when (get-buffer gnus-summary-buffer)
5109           (kill-buffer gnus-summary-buffer)))
5110       (unless gnus-single-article-buffer
5111         (setq gnus-article-current nil))
5112       (when gnus-use-trees
5113         (gnus-tree-close group))
5114       (gnus-async-prefetch-remove-group group)
5115       (when (get-buffer gnus-article-buffer)
5116         (bury-buffer gnus-article-buffer))
5117       ;; Return to the group buffer.
5118       (gnus-configure-windows 'group 'force)
5119       ;; Clear the current group name.
5120       (setq gnus-newsgroup-name nil)
5121       (when (equal (gnus-group-group-name) group)
5122         (gnus-group-next-unread-group 1))
5123       (when quit-config
5124         (gnus-handle-ephemeral-exit quit-config)))))
5125
5126 (defun gnus-handle-ephemeral-exit (quit-config)
5127   "Handle movement when leaving an ephemeral group.  The state
5128 which existed when entering the ephemeral is reset."
5129   (if (not (buffer-name (car quit-config)))
5130       (gnus-configure-windows 'group 'force)
5131     (set-buffer (car quit-config))
5132     (cond ((eq major-mode 'gnus-summary-mode)
5133            (gnus-set-global-variables))
5134           ((eq major-mode 'gnus-article-mode)
5135            (save-excursion
5136              ;; The `gnus-summary-buffer' variable may point
5137              ;; to the old summary buffer when using a single
5138              ;; article buffer.
5139              (unless (gnus-buffer-live-p gnus-summary-buffer)
5140                (set-buffer gnus-group-buffer))
5141              (set-buffer gnus-summary-buffer)
5142              (gnus-set-global-variables))))
5143     (if (or (eq (cdr quit-config) 'article)
5144             (eq (cdr quit-config) 'pick))
5145         (progn
5146           ;; The current article may be from the ephemeral group
5147           ;; thus it is best that we reload this article
5148           (gnus-summary-show-article)
5149           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5150               (gnus-configure-windows 'pick 'force)
5151             (gnus-configure-windows (cdr quit-config) 'force)))
5152       (gnus-configure-windows (cdr quit-config) 'force))
5153     (when (eq major-mode 'gnus-summary-mode)
5154       (gnus-summary-next-subject 1 nil t)
5155       (gnus-summary-recenter)
5156       (gnus-summary-position-point))))
5157
5158 ;;; Dead summaries.
5159
5160 (defvar gnus-dead-summary-mode-map nil)
5161
5162 (unless gnus-dead-summary-mode-map
5163   (setq gnus-dead-summary-mode-map (make-keymap))
5164   (suppress-keymap gnus-dead-summary-mode-map)
5165   (substitute-key-definition
5166    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5167   (let ((keys '("\C-d" "\r" "\177" [delete])))
5168     (while keys
5169       (define-key gnus-dead-summary-mode-map
5170         (pop keys) 'gnus-summary-wake-up-the-dead))))
5171
5172 (defvar gnus-dead-summary-mode nil
5173   "Minor mode for Gnus summary buffers.")
5174
5175 (defun gnus-dead-summary-mode (&optional arg)
5176   "Minor mode for Gnus summary buffers."
5177   (interactive "P")
5178   (when (eq major-mode 'gnus-summary-mode)
5179     (make-local-variable 'gnus-dead-summary-mode)
5180     (setq gnus-dead-summary-mode
5181           (if (null arg) (not gnus-dead-summary-mode)
5182             (> (prefix-numeric-value arg) 0)))
5183     (when gnus-dead-summary-mode
5184       (gnus-add-minor-mode
5185        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5186
5187 (defun gnus-deaden-summary ()
5188   "Make the current summary buffer into a dead summary buffer."
5189   ;; Kill any previous dead summary buffer.
5190   (when (and gnus-dead-summary
5191              (buffer-name gnus-dead-summary))
5192     (save-excursion
5193       (set-buffer gnus-dead-summary)
5194       (when gnus-dead-summary-mode
5195         (kill-buffer (current-buffer)))))
5196   ;; Make this the current dead summary.
5197   (setq gnus-dead-summary (current-buffer))
5198   (gnus-dead-summary-mode 1)
5199   (let ((name (buffer-name)))
5200     (when (string-match "Summary" name)
5201       (rename-buffer
5202        (concat (substring name 0 (match-beginning 0)) "Dead "
5203                (substring name (match-beginning 0)))
5204        t))))
5205
5206 (defun gnus-kill-or-deaden-summary (buffer)
5207   "Kill or deaden the summary BUFFER."
5208   (save-excursion
5209     (when (and (buffer-name buffer)
5210                (not gnus-single-article-buffer))
5211       (save-excursion
5212         (set-buffer buffer)
5213         (gnus-kill-buffer gnus-article-buffer)
5214         (gnus-kill-buffer gnus-original-article-buffer)))
5215     (cond (gnus-kill-summary-on-exit
5216            (when (and gnus-use-trees
5217                       (gnus-buffer-exists-p buffer))
5218              (save-excursion
5219                (set-buffer buffer)
5220                (gnus-tree-close gnus-newsgroup-name)))
5221            (gnus-kill-buffer buffer))
5222           ((gnus-buffer-exists-p buffer)
5223            (save-excursion
5224              (set-buffer buffer)
5225              (gnus-deaden-summary))))))
5226
5227 (defun gnus-summary-wake-up-the-dead (&rest args)
5228   "Wake up the dead summary buffer."
5229   (interactive)
5230   (gnus-dead-summary-mode -1)
5231   (let ((name (buffer-name)))
5232     (when (string-match "Dead " name)
5233       (rename-buffer
5234        (concat (substring name 0 (match-beginning 0))
5235                (substring name (match-end 0)))
5236        t)))
5237   (gnus-message 3 "This dead summary is now alive again"))
5238
5239 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5240 (defun gnus-summary-fetch-faq (&optional faq-dir)
5241   "Fetch the FAQ for the current group.
5242 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5243 in."
5244   (interactive
5245    (list
5246     (when current-prefix-arg
5247       (completing-read
5248        "Faq dir: " (and (listp gnus-group-faq-directory)
5249                         (mapcar (lambda (file) (list file))
5250                                 gnus-group-faq-directory))))))
5251   (let (gnus-faq-buffer)
5252     (when (setq gnus-faq-buffer
5253                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5254       (gnus-configure-windows 'summary-faq))))
5255
5256 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5257 (defun gnus-summary-describe-group (&optional force)
5258   "Describe the current newsgroup."
5259   (interactive "P")
5260   (gnus-group-describe-group force gnus-newsgroup-name))
5261
5262 (defun gnus-summary-describe-briefly ()
5263   "Describe summary mode commands briefly."
5264   (interactive)
5265   (gnus-message 6
5266                 (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")))
5267
5268 ;; Walking around group mode buffer from summary mode.
5269
5270 (defun gnus-summary-next-group (&optional no-article target-group backward)
5271   "Exit current newsgroup and then select next unread newsgroup.
5272 If prefix argument NO-ARTICLE is non-nil, no article is selected
5273 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5274 previous group instead."
5275   (interactive "P")
5276   ;; Stop pre-fetching.
5277   (gnus-async-halt-prefetch)
5278   (let ((current-group gnus-newsgroup-name)
5279         (current-buffer (current-buffer))
5280         entered)
5281     ;; First we semi-exit this group to update Xrefs and all variables.
5282     ;; We can't do a real exit, because the window conf must remain
5283     ;; the same in case the user is prompted for info, and we don't
5284     ;; want the window conf to change before that...
5285     (gnus-summary-exit t)
5286     (while (not entered)
5287       ;; Then we find what group we are supposed to enter.
5288       (set-buffer gnus-group-buffer)
5289       (gnus-group-jump-to-group current-group)
5290       (setq target-group
5291             (or target-group
5292                 (if (eq gnus-keep-same-level 'best)
5293                     (gnus-summary-best-group gnus-newsgroup-name)
5294                   (gnus-summary-search-group backward gnus-keep-same-level))))
5295       (if (not target-group)
5296           ;; There are no further groups, so we return to the group
5297           ;; buffer.
5298           (progn
5299             (gnus-message 5 "Returning to the group buffer")
5300             (setq entered t)
5301             (when (gnus-buffer-live-p current-buffer)
5302               (set-buffer current-buffer)
5303               (gnus-summary-exit))
5304             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5305         ;; We try to enter the target group.
5306         (gnus-group-jump-to-group target-group)
5307         (let ((unreads (gnus-group-group-unread)))
5308           (if (and (or (eq t unreads)
5309                        (and unreads (not (zerop unreads))))
5310                    (gnus-summary-read-group
5311                     target-group nil no-article
5312                     (and (buffer-name current-buffer) current-buffer)
5313                     nil backward))
5314               (setq entered t)
5315             (setq current-group target-group
5316                   target-group nil)))))))
5317
5318 (defun gnus-summary-prev-group (&optional no-article)
5319   "Exit current newsgroup and then select previous unread newsgroup.
5320 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5321   (interactive "P")
5322   (gnus-summary-next-group no-article nil t))
5323
5324 ;; Walking around summary lines.
5325
5326 (defun gnus-summary-first-subject (&optional unread undownloaded)
5327   "Go to the first unread subject.
5328 If UNREAD is non-nil, go to the first unread article.
5329 Returns the article selected or nil if there are no unread articles."
5330   (interactive "P")
5331   (prog1
5332       (cond
5333        ;; Empty summary.
5334        ((null gnus-newsgroup-data)
5335         (gnus-message 3 "No articles in the group")
5336         nil)
5337        ;; Pick the first article.
5338        ((not unread)
5339         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5340         (gnus-data-number (car gnus-newsgroup-data)))
5341        ;; No unread articles.
5342        ((null gnus-newsgroup-unreads)
5343         (gnus-message 3 "No more unread articles")
5344         nil)
5345        ;; Find the first unread article.
5346        (t
5347         (let ((data gnus-newsgroup-data))
5348           (while (and data
5349                       (and (not (and undownloaded
5350                                      (eq gnus-undownloaded-mark
5351                                          (gnus-data-mark (car data)))))
5352                            (not (gnus-data-unread-p (car data)))))
5353             (setq data (cdr data)))
5354           (when data
5355             (goto-char (gnus-data-pos (car data)))
5356             (gnus-data-number (car data))))))
5357     (gnus-summary-position-point)))
5358
5359 (defun gnus-summary-next-subject (n &optional unread dont-display)
5360   "Go to next N'th summary line.
5361 If N is negative, go to the previous N'th subject line.
5362 If UNREAD is non-nil, only unread articles are selected.
5363 The difference between N and the actual number of steps taken is
5364 returned."
5365   (interactive "p")
5366   (let ((backward (< n 0))
5367         (n (abs n)))
5368     (while (and (> n 0)
5369                 (if backward
5370                     (gnus-summary-find-prev unread)
5371                   (gnus-summary-find-next unread)))
5372       (gnus-summary-show-thread)
5373       (setq n (1- n)))
5374     (when (/= 0 n)
5375       (gnus-message 7 "No more%s articles"
5376                     (if unread " unread" "")))
5377     (unless dont-display
5378       (gnus-summary-recenter)
5379       (gnus-summary-position-point))
5380     n))
5381
5382 (defun gnus-summary-next-unread-subject (n)
5383   "Go to next N'th unread summary line."
5384   (interactive "p")
5385   (gnus-summary-next-subject n t))
5386
5387 (defun gnus-summary-prev-subject (n &optional unread)
5388   "Go to previous N'th summary line.
5389 If optional argument UNREAD is non-nil, only unread article is selected."
5390   (interactive "p")
5391   (gnus-summary-next-subject (- n) unread))
5392
5393 (defun gnus-summary-prev-unread-subject (n)
5394   "Go to previous N'th unread summary line."
5395   (interactive "p")
5396   (gnus-summary-next-subject (- n) t))
5397
5398 (defun gnus-summary-goto-subject (article &optional force silent)
5399   "Go the subject line of ARTICLE.
5400 If FORCE, also allow jumping to articles not currently shown."
5401   (interactive "nArticle number: ")
5402   (let ((b (point))
5403         (data (gnus-data-find article)))
5404     ;; We read in the article if we have to.
5405     (and (not data)
5406          force
5407          (gnus-summary-insert-subject article (and (vectorp force) force) t)
5408          (setq data (gnus-data-find article)))
5409     (goto-char b)
5410     (if (not data)
5411         (progn
5412           (unless silent
5413             (gnus-message 3 "Can't find article %d" article))
5414           nil)
5415       (goto-char (gnus-data-pos data))
5416       article)))
5417
5418 ;; Walking around summary lines with displaying articles.
5419
5420 (defun gnus-summary-expand-window (&optional arg)
5421   "Make the summary buffer take up the entire Emacs frame.
5422 Given a prefix, will force an `article' buffer configuration."
5423   (interactive "P")
5424   (if arg
5425       (gnus-configure-windows 'article 'force)
5426     (gnus-configure-windows 'summary 'force)))
5427
5428 (defun gnus-summary-display-article (article &optional all-header)
5429   "Display ARTICLE in article buffer."
5430   (gnus-set-global-variables)
5431   (if (null article)
5432       nil
5433     (prog1
5434         (if gnus-summary-display-article-function
5435             (funcall gnus-summary-display-article-function article all-header)
5436           (gnus-article-prepare article all-header))
5437       (gnus-run-hooks 'gnus-select-article-hook)
5438       (when (and gnus-current-article
5439                  (not (zerop gnus-current-article)))
5440         (gnus-summary-goto-subject gnus-current-article))
5441       (gnus-summary-recenter)
5442       (when (and gnus-use-trees gnus-show-threads)
5443         (gnus-possibly-generate-tree article)
5444         (gnus-highlight-selected-tree article))
5445       ;; Successfully display article.
5446       (gnus-article-set-window-start
5447        (cdr (assq article gnus-newsgroup-bookmarks))))))
5448
5449 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5450   "Select the current article.
5451 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5452 non-nil, the article will be re-fetched even if it already present in
5453 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5454 be displayed."
5455   ;; Make sure we are in the summary buffer to work around bbdb bug.
5456   (unless (eq major-mode 'gnus-summary-mode)
5457     (set-buffer gnus-summary-buffer))
5458   (let ((article (or article (gnus-summary-article-number)))
5459         (all-headers (not (not all-headers))) ;Must be T or NIL.
5460         gnus-summary-display-article-function
5461         did)
5462     (and (not pseudo)
5463          (gnus-summary-article-pseudo-p article)
5464          (error "This is a pseudo-article"))
5465     (prog1
5466         (save-excursion
5467           (set-buffer gnus-summary-buffer)
5468           (if (or (and gnus-single-article-buffer
5469                        (or (null gnus-current-article)
5470                            (null gnus-article-current)
5471                            (null (get-buffer gnus-article-buffer))
5472                            (not (eq article (cdr gnus-article-current)))
5473                            (not (equal (car gnus-article-current)
5474                                        gnus-newsgroup-name))))
5475                   (and (not gnus-single-article-buffer)
5476                        (or (null gnus-current-article)
5477                            (not (eq gnus-current-article article))))
5478                   force)
5479               ;; The requested article is different from the current article.
5480               (prog1
5481                   (gnus-summary-display-article article all-headers)
5482                 (setq did article))
5483             (when (or all-headers gnus-show-all-headers)
5484               (gnus-article-show-all-headers))
5485             'old))
5486       (when did
5487         (gnus-article-set-window-start
5488          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5489
5490 (defun gnus-summary-set-current-mark (&optional current-mark)
5491   "Obsolete function."
5492   nil)
5493
5494 (defun gnus-summary-next-article (&optional unread subject backward push)
5495   "Select the next article.
5496 If UNREAD, only unread articles are selected.
5497 If SUBJECT, only articles with SUBJECT are selected.
5498 If BACKWARD, the previous article is selected instead of the next."
5499   (interactive "P")
5500   (cond
5501    ;; Is there such an article?
5502    ((and (gnus-summary-search-forward unread subject backward)
5503          (or (gnus-summary-display-article (gnus-summary-article-number))
5504              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5505     (gnus-summary-position-point))
5506    ;; If not, we try the first unread, if that is wanted.
5507    ((and subject
5508          gnus-auto-select-same
5509          (gnus-summary-first-unread-article))
5510     (gnus-summary-position-point)
5511     (gnus-message 6 "Wrapped"))
5512    ;; Try to get next/previous article not displayed in this group.
5513    ((and gnus-auto-extend-newsgroup
5514          (not unread) (not subject))
5515     (gnus-summary-goto-article
5516      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5517      nil t))
5518    ;; Go to next/previous group.
5519    (t
5520     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5521       (gnus-summary-jump-to-group gnus-newsgroup-name))
5522     (let ((cmd last-command-char)
5523           (point
5524            (save-excursion
5525              (set-buffer gnus-group-buffer)
5526              (point)))
5527           (group
5528            (if (eq gnus-keep-same-level 'best)
5529                (gnus-summary-best-group gnus-newsgroup-name)
5530              (gnus-summary-search-group backward gnus-keep-same-level))))
5531       ;; For some reason, the group window gets selected.  We change
5532       ;; it back.
5533       (select-window (get-buffer-window (current-buffer)))
5534       ;; Select next unread newsgroup automagically.
5535       (cond
5536        ((or (not gnus-auto-select-next)
5537             (not cmd))
5538         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5539        ((or (eq gnus-auto-select-next 'quietly)
5540             (and (eq gnus-auto-select-next 'slightly-quietly)
5541                  push)
5542             (and (eq gnus-auto-select-next 'almost-quietly)
5543                  (gnus-summary-last-article-p)))
5544         ;; Select quietly.
5545         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5546             (gnus-summary-exit)
5547           (gnus-message 7 "No more%s articles (%s)..."
5548                         (if unread " unread" "")
5549                         (if group (concat "selecting " group)
5550                           "exiting"))
5551           (gnus-summary-next-group nil group backward)))
5552        (t
5553         (when (gnus-key-press-event-p last-input-event)
5554           (gnus-summary-walk-group-buffer
5555            gnus-newsgroup-name cmd unread backward point))))))))
5556
5557 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5558   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5559                       (?\C-p (gnus-group-prev-unread-group 1))))
5560         (cursor-in-echo-area t)
5561         keve key group ended)
5562     (save-excursion
5563       (set-buffer gnus-group-buffer)
5564       (goto-char start)
5565       (setq group
5566             (if (eq gnus-keep-same-level 'best)
5567                 (gnus-summary-best-group gnus-newsgroup-name)
5568               (gnus-summary-search-group backward gnus-keep-same-level))))
5569     (while (not ended)
5570       (gnus-message
5571        5 "No more%s articles%s" (if unread " unread" "")
5572        (if (and group
5573                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5574            (format " (Type %s for %s [%s])"
5575                    (single-key-description cmd) group
5576                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5577          (format " (Type %s to exit %s)"
5578                  (single-key-description cmd)
5579                  gnus-newsgroup-name)))
5580       ;; Confirm auto selection.
5581       (setq key (car (setq keve (gnus-read-event-char))))
5582       (setq ended t)
5583       (cond
5584        ((assq key keystrokes)
5585         (let ((obuf (current-buffer)))
5586           (switch-to-buffer gnus-group-buffer)
5587           (when group
5588             (gnus-group-jump-to-group group))
5589           (eval (cadr (assq key keystrokes)))
5590           (setq group (gnus-group-group-name))
5591           (switch-to-buffer obuf))
5592         (setq ended nil))
5593        ((equal key cmd)
5594         (if (or (not group)
5595                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5596             (gnus-summary-exit)
5597           (gnus-summary-next-group nil group backward)))
5598        (t
5599         (push (cdr keve) unread-command-events))))))
5600
5601 (defun gnus-summary-next-unread-article ()
5602   "Select unread article after current one."
5603   (interactive)
5604   (gnus-summary-next-article
5605    (or (not (eq gnus-summary-goto-unread 'never))
5606        (gnus-summary-last-article-p (gnus-summary-article-number)))
5607    (and gnus-auto-select-same
5608         (gnus-summary-article-subject))))
5609
5610 (defun gnus-summary-prev-article (&optional unread subject)
5611   "Select the article after the current one.
5612 If UNREAD is non-nil, only unread articles are selected."
5613   (interactive "P")
5614   (gnus-summary-next-article unread subject t))
5615
5616 (defun gnus-summary-prev-unread-article ()
5617   "Select unread article before current one."
5618   (interactive)
5619   (gnus-summary-prev-article
5620    (or (not (eq gnus-summary-goto-unread 'never))
5621        (gnus-summary-first-article-p (gnus-summary-article-number)))
5622    (and gnus-auto-select-same
5623         (gnus-summary-article-subject))))
5624
5625 (defun gnus-summary-next-page (&optional lines circular)
5626   "Show next page of the selected article.
5627 If at the end of the current article, select the next article.
5628 LINES says how many lines should be scrolled up.
5629
5630 If CIRCULAR is non-nil, go to the start of the article instead of
5631 selecting the next article when reaching the end of the current
5632 article."
5633   (interactive "P")
5634   (setq gnus-summary-buffer (current-buffer))
5635   (gnus-set-global-variables)
5636   (let ((article (gnus-summary-article-number))
5637         (article-window (get-buffer-window gnus-article-buffer t))
5638         endp)
5639     ;; If the buffer is empty, we have no article.
5640     (unless article
5641       (error "No article to select"))
5642     (gnus-configure-windows 'article)
5643     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5644         (if (and (eq gnus-summary-goto-unread 'never)
5645                  (not (gnus-summary-last-article-p article)))
5646             (gnus-summary-next-article)
5647           (gnus-summary-next-unread-article))
5648       (if (or (null gnus-current-article)
5649               (null gnus-article-current)
5650               (/= article (cdr gnus-article-current))
5651               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5652           ;; Selected subject is different from current article's.
5653           (gnus-summary-display-article article)
5654         (when article-window
5655           (gnus-eval-in-buffer-window gnus-article-buffer
5656             (setq endp (gnus-article-next-page lines)))
5657           (when endp
5658             (cond (circular
5659                    (gnus-summary-beginning-of-article))
5660                   (lines
5661                    (gnus-message 3 "End of message"))
5662                   ((null lines)
5663                    (if (and (eq gnus-summary-goto-unread 'never)
5664                             (not (gnus-summary-last-article-p article)))
5665                        (gnus-summary-next-article)
5666                      (gnus-summary-next-unread-article))))))))
5667     (gnus-summary-recenter)
5668     (gnus-summary-position-point)))
5669
5670 (defun gnus-summary-prev-page (&optional lines move)
5671   "Show previous page of selected article.
5672 Argument LINES specifies lines to be scrolled down.
5673 If MOVE, move to the previous unread article if point is at
5674 the beginning of the buffer."
5675   (interactive "P")
5676   (let ((article (gnus-summary-article-number))
5677         (article-window (get-buffer-window gnus-article-buffer t))
5678         endp)
5679     (gnus-configure-windows 'article)
5680     (if (or (null gnus-current-article)
5681             (null gnus-article-current)
5682             (/= article (cdr gnus-article-current))
5683             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5684         ;; Selected subject is different from current article's.
5685         (gnus-summary-display-article article)
5686       (gnus-summary-recenter)
5687       (when article-window
5688         (gnus-eval-in-buffer-window gnus-article-buffer
5689           (setq endp (gnus-article-prev-page lines)))
5690         (when (and move endp)
5691           (cond (lines
5692                  (gnus-message 3 "Beginning of message"))
5693                 ((null lines)
5694                  (if (and (eq gnus-summary-goto-unread 'never)
5695                           (not (gnus-summary-first-article-p article)))
5696                      (gnus-summary-prev-article)
5697                    (gnus-summary-prev-unread-article))))))))
5698   (gnus-summary-position-point))
5699
5700 (defun gnus-summary-prev-page-or-article (&optional lines)
5701   "Show previous page of selected article.
5702 Argument LINES specifies lines to be scrolled down.
5703 If at the beginning of the article, go to the next article."
5704   (interactive "P")
5705   (gnus-summary-prev-page lines t))
5706
5707 (defun gnus-summary-scroll-up (lines)
5708   "Scroll up (or down) one line current article.
5709 Argument LINES specifies lines to be scrolled up (or down if negative)."
5710   (interactive "p")
5711   (gnus-configure-windows 'article)
5712   (gnus-summary-show-thread)
5713   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5714     (gnus-eval-in-buffer-window gnus-article-buffer
5715       (cond ((> lines 0)
5716              (when (gnus-article-next-page lines)
5717                (gnus-message 3 "End of message")))
5718             ((< lines 0)
5719              (gnus-article-prev-page (- lines))))))
5720   (gnus-summary-recenter)
5721   (gnus-summary-position-point))
5722
5723 (defun gnus-summary-next-same-subject ()
5724   "Select next article which has the same subject as current one."
5725   (interactive)
5726   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5727
5728 (defun gnus-summary-prev-same-subject ()
5729   "Select previous article which has the same subject as current one."
5730   (interactive)
5731   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5732
5733 (defun gnus-summary-next-unread-same-subject ()
5734   "Select next unread article which has the same subject as current one."
5735   (interactive)
5736   (gnus-summary-next-article t (gnus-summary-article-subject)))
5737
5738 (defun gnus-summary-prev-unread-same-subject ()
5739   "Select previous unread article which has the same subject as current one."
5740   (interactive)
5741   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5742
5743 (defun gnus-summary-first-unread-article ()
5744   "Select the first unread article.
5745 Return nil if there are no unread articles."
5746   (interactive)
5747   (prog1
5748       (when (gnus-summary-first-subject t)
5749         (gnus-summary-show-thread)
5750         (gnus-summary-first-subject t)
5751         (gnus-summary-display-article (gnus-summary-article-number)))
5752     (gnus-summary-position-point)))
5753
5754 (defun gnus-summary-first-article ()
5755   "Select the first article.
5756 Return nil if there are no articles."
5757   (interactive)
5758   (prog1
5759       (when (gnus-summary-first-subject)
5760       (gnus-summary-show-thread)
5761       (gnus-summary-first-subject)
5762       (gnus-summary-display-article (gnus-summary-article-number)))
5763     (gnus-summary-position-point)))
5764
5765 (defun gnus-summary-best-unread-article ()
5766   "Select the unread article with the highest score."
5767   (interactive)
5768   (let ((best -1000000)
5769         (data gnus-newsgroup-data)
5770         article score)
5771     (while data
5772       (and (gnus-data-unread-p (car data))
5773            (> (setq score
5774                     (gnus-summary-article-score (gnus-data-number (car data))))
5775               best)
5776            (setq best score
5777                  article (gnus-data-number (car data))))
5778       (setq data (cdr data)))
5779     (prog1
5780         (if article
5781             (gnus-summary-goto-article article)
5782           (error "No unread articles"))
5783       (gnus-summary-position-point))))
5784
5785 (defun gnus-summary-last-subject ()
5786   "Go to the last displayed subject line in the group."
5787   (let ((article (gnus-data-number (car (gnus-data-list t)))))
5788     (when article
5789       (gnus-summary-goto-subject article))))
5790
5791 (defun gnus-summary-goto-article (article &optional all-headers force)
5792   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
5793 If ALL-HEADERS is non-nil, no header lines are hidden."
5794   (interactive
5795    (list
5796     (completing-read
5797      "Article number or Message-ID: "
5798      (mapcar (lambda (number) (list (int-to-string number)))
5799              gnus-newsgroup-limit))
5800     current-prefix-arg
5801     t))
5802   (prog1
5803       (if (and (stringp article)
5804                (string-match "@" article))
5805           (gnus-summary-refer-article article)
5806         (when (stringp article)
5807           (setq article (string-to-number article)))
5808         (if (gnus-summary-goto-subject article force)
5809             (gnus-summary-display-article article all-headers)
5810           (gnus-message 4 "Couldn't go to article %s" article) nil))
5811     (gnus-summary-position-point)))
5812
5813 (defun gnus-summary-goto-last-article ()
5814   "Go to the previously read article."
5815   (interactive)
5816   (prog1
5817       (when gnus-last-article
5818         (gnus-summary-goto-article gnus-last-article nil t))
5819     (gnus-summary-position-point)))
5820
5821 (defun gnus-summary-pop-article (number)
5822   "Pop one article off the history and go to the previous.
5823 NUMBER articles will be popped off."
5824   (interactive "p")
5825   (let (to)
5826     (setq gnus-newsgroup-history
5827           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
5828     (if to
5829         (gnus-summary-goto-article (car to) nil t)
5830       (error "Article history empty")))
5831   (gnus-summary-position-point))
5832
5833 ;; Summary commands and functions for limiting the summary buffer.
5834
5835 (defun gnus-summary-limit-to-articles (n)
5836   "Limit the summary buffer to the next N articles.
5837 If not given a prefix, use the process marked articles instead."
5838   (interactive "P")
5839   (prog1
5840       (let ((articles (gnus-summary-work-articles n)))
5841         (setq gnus-newsgroup-processable nil)
5842         (gnus-summary-limit articles))
5843     (gnus-summary-position-point)))
5844
5845 (defun gnus-summary-pop-limit (&optional total)
5846   "Restore the previous limit.
5847 If given a prefix, remove all limits."
5848   (interactive "P")
5849   (when total
5850     (setq gnus-newsgroup-limits
5851           (list (mapcar (lambda (h) (mail-header-number h))
5852                         gnus-newsgroup-headers))))
5853   (unless gnus-newsgroup-limits
5854     (error "No limit to pop"))
5855   (prog1
5856       (gnus-summary-limit nil 'pop)
5857     (gnus-summary-position-point)))
5858
5859 (defun gnus-summary-limit-to-subject (subject &optional header)
5860   "Limit the summary buffer to articles that have subjects that match a regexp."
5861   (interactive "sLimit to subject (regexp): ")
5862   (unless header
5863     (setq header "subject"))
5864   (when (not (equal "" subject))
5865     (prog1
5866         (let ((articles (gnus-summary-find-matching
5867                          (or header "subject") subject 'all)))
5868           (unless articles
5869             (error "Found no matches for \"%s\"" subject))
5870           (gnus-summary-limit articles))
5871       (gnus-summary-position-point))))
5872
5873 (defun gnus-summary-limit-to-author (from)
5874   "Limit the summary buffer to articles that have authors that match a regexp."
5875   (interactive "sLimit to author (regexp): ")
5876   (gnus-summary-limit-to-subject from "from"))
5877
5878 (defun gnus-summary-limit-to-age (age &optional younger-p)
5879   "Limit the summary buffer to articles that are older than (or equal) AGE days.
5880 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
5881 articles that are younger than AGE days."
5882   (interactive "nTime in days: \nP")
5883   (prog1
5884       (let ((data gnus-newsgroup-data)
5885             (cutoff (nnmail-days-to-time age))
5886             articles d date is-younger)
5887         (while (setq d (pop data))
5888           (when (and (vectorp (gnus-data-header d))
5889                      (setq date (mail-header-date (gnus-data-header d))))
5890             (setq is-younger (nnmail-time-less
5891                               (nnmail-time-since (nnmail-date-to-time date))
5892                               cutoff))
5893             (when (if younger-p is-younger (not is-younger))
5894               (push (gnus-data-number d) articles))))
5895         (gnus-summary-limit (nreverse articles)))
5896     (gnus-summary-position-point)))
5897
5898 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
5899 (make-obsolete
5900  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
5901
5902 (defun gnus-summary-limit-to-unread (&optional all)
5903   "Limit the summary buffer to articles that are not marked as read.
5904 If ALL is non-nil, limit strictly to unread articles."
5905   (interactive "P")
5906   (if all
5907       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
5908     (gnus-summary-limit-to-marks
5909      ;; Concat all the marks that say that an article is read and have
5910      ;; those removed.
5911      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
5912            gnus-killed-mark gnus-kill-file-mark
5913            gnus-low-score-mark gnus-expirable-mark
5914            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
5915            gnus-duplicate-mark gnus-souped-mark)
5916      'reverse)))
5917
5918 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
5919 (make-obsolete 'gnus-summary-delete-marked-with
5920                'gnus-summary-limit-exlude-marks)
5921
5922 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
5923   "Exclude articles that are marked with MARKS (e.g. \"DK\").
5924 If REVERSE, limit the summary buffer to articles that are marked
5925 with MARKS.  MARKS can either be a string of marks or a list of marks.
5926 Returns how many articles were removed."
5927   (interactive "sMarks: ")
5928   (gnus-summary-limit-to-marks marks t))
5929
5930 (defun gnus-summary-limit-to-marks (marks &optional reverse)
5931   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
5932 If REVERSE (the prefix), limit the summary buffer to articles that are
5933 not marked with MARKS.  MARKS can either be a string of marks or a
5934 list of marks.
5935 Returns how many articles were removed."
5936   (interactive "sMarks: \nP")
5937   (prog1
5938       (let ((data gnus-newsgroup-data)
5939             (marks (if (listp marks) marks
5940                      (append marks nil))) ; Transform to list.
5941             articles)
5942         (while data
5943           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
5944                   (memq (gnus-data-mark (car data)) marks))
5945             (push (gnus-data-number (car data)) articles))
5946           (setq data (cdr data)))
5947         (gnus-summary-limit articles))
5948     (gnus-summary-position-point)))
5949
5950 (defun gnus-summary-limit-to-score (&optional score)
5951   "Limit to articles with score at or above SCORE."
5952   (interactive "P")
5953   (setq score (if score
5954                   (prefix-numeric-value score)
5955                 (or gnus-summary-default-score 0)))
5956   (let ((data gnus-newsgroup-data)
5957         articles)
5958     (while data
5959       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
5960                 score)
5961         (push (gnus-data-number (car data)) articles))
5962       (setq data (cdr data)))
5963     (prog1
5964         (gnus-summary-limit articles)
5965       (gnus-summary-position-point))))
5966
5967 (defun gnus-summary-limit-include-thread (id)
5968   "Display all the hidden articles that in the current thread."
5969   (interactive (list (mail-header-id (gnus-summary-article-header))))
5970   (let ((articles (gnus-articles-in-thread
5971                    (gnus-id-to-thread (gnus-root-id id)))))
5972     (prog1
5973         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
5974       (gnus-summary-position-point))))
5975
5976 (defun gnus-summary-limit-include-dormant ()
5977   "Display all the hidden articles that are marked as dormant.
5978 Note that this command only works on a subset of the articles currently
5979 fetched for this group."
5980   (interactive)
5981   (unless gnus-newsgroup-dormant
5982     (error "There are no dormant articles in this group"))
5983   (prog1
5984       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
5985     (gnus-summary-position-point)))
5986
5987 (defun gnus-summary-limit-exclude-dormant ()
5988   "Hide all dormant articles."
5989   (interactive)
5990   (prog1
5991       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
5992     (gnus-summary-position-point)))
5993
5994 (defun gnus-summary-limit-exclude-childless-dormant ()
5995   "Hide all dormant articles that have no children."
5996   (interactive)
5997   (let ((data (gnus-data-list t))
5998         articles d children)
5999     ;; Find all articles that are either not dormant or have
6000     ;; children.
6001     (while (setq d (pop data))
6002       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6003                 (and (setq children
6004                            (gnus-article-children (gnus-data-number d)))
6005                      (let (found)
6006                        (while children
6007                          (when (memq (car children) articles)
6008                            (setq children nil
6009                                  found t))
6010                          (pop children))
6011                        found)))
6012         (push (gnus-data-number d) articles)))
6013     ;; Do the limiting.
6014     (prog1
6015         (gnus-summary-limit articles)
6016       (gnus-summary-position-point))))
6017
6018 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6019   "Mark all unread excluded articles as read.
6020 If ALL, mark even excluded ticked and dormants as read."
6021   (interactive "P")
6022   (let ((articles (gnus-sorted-complement
6023                    (sort
6024                     (mapcar (lambda (h) (mail-header-number h))
6025                             gnus-newsgroup-headers)
6026                     '<)
6027                    (sort gnus-newsgroup-limit '<)))
6028         article)
6029     (setq gnus-newsgroup-unreads gnus-newsgroup-limit)
6030     (if all
6031         (setq gnus-newsgroup-dormant nil
6032               gnus-newsgroup-marked nil
6033               gnus-newsgroup-reads
6034               (nconc
6035                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6036                gnus-newsgroup-reads))
6037       (while (setq article (pop articles))
6038         (unless (or (memq article gnus-newsgroup-dormant)
6039                     (memq article gnus-newsgroup-marked))
6040           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6041
6042 (defun gnus-summary-limit (articles &optional pop)
6043   (if pop
6044       ;; We pop the previous limit off the stack and use that.
6045       (setq articles (car gnus-newsgroup-limits)
6046             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6047     ;; We use the new limit, so we push the old limit on the stack.
6048     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6049   ;; Set the limit.
6050   (setq gnus-newsgroup-limit articles)
6051   (let ((total (length gnus-newsgroup-data))
6052         (data (gnus-data-find-list (gnus-summary-article-number)))
6053         (gnus-summary-mark-below nil)   ; Inhibit this.
6054         found)
6055     ;; This will do all the work of generating the new summary buffer
6056     ;; according to the new limit.
6057     (gnus-summary-prepare)
6058     ;; Hide any threads, possibly.
6059     (and gnus-show-threads
6060          gnus-thread-hide-subtree
6061          (gnus-summary-hide-all-threads))
6062     ;; Try to return to the article you were at, or one in the
6063     ;; neighborhood.
6064     (when data
6065       ;; We try to find some article after the current one.
6066       (while data
6067         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6068           (setq data nil
6069                 found t))
6070         (setq data (cdr data))))
6071     (unless found
6072       ;; If there is no data, that means that we were after the last
6073       ;; article.  The same goes when we can't find any articles
6074       ;; after the current one.
6075       (goto-char (point-max))
6076       (gnus-summary-find-prev))
6077     ;; We return how many articles were removed from the summary
6078     ;; buffer as a result of the new limit.
6079     (- total (length gnus-newsgroup-data))))
6080
6081 (defsubst gnus-invisible-cut-children (threads)
6082   (let ((num 0))
6083     (while threads
6084       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6085         (incf num))
6086       (pop threads))
6087     (< num 2)))
6088
6089 (defsubst gnus-cut-thread (thread)
6090   "Go forwards in the thread until we find an article that we want to display."
6091   (when (or (eq gnus-fetch-old-headers 'some)
6092             (eq gnus-fetch-old-headers 'invisible)          
6093             (eq gnus-build-sparse-threads 'some)
6094             (eq gnus-build-sparse-threads 'more))
6095     ;; Deal with old-fetched headers and sparse threads.
6096     (while (and
6097             thread
6098             (or
6099              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6100              (gnus-summary-article-ancient-p
6101               (mail-header-number (car thread))))
6102             (if (or (<= (length (cdr thread)) 1)
6103                     (eq gnus-fetch-old-headers 'invisible))
6104                 (setq gnus-newsgroup-limit
6105                       (delq (mail-header-number (car thread))
6106                             gnus-newsgroup-limit)
6107                       thread (cadr thread))
6108               (when (gnus-invisible-cut-children (cdr thread))
6109                 (let ((th (cdr thread)))
6110                   (while th
6111                     (if (memq (mail-header-number (caar th))
6112                               gnus-newsgroup-limit)
6113                         (setq thread (car th)
6114                               th nil)
6115                       (setq th (cdr th))))))))))
6116   thread)
6117
6118 (defun gnus-cut-threads (threads)
6119   "Cut off all uninteresting articles from the beginning of threads."
6120   (when (or (eq gnus-fetch-old-headers 'some)
6121             (eq gnus-fetch-old-headers 'invisible)
6122             (eq gnus-build-sparse-threads 'some)
6123             (eq gnus-build-sparse-threads 'more))
6124     (let ((th threads))
6125       (while th
6126         (setcar th (gnus-cut-thread (car th)))
6127         (setq th (cdr th)))))
6128   ;; Remove nixed out threads.
6129   (delq nil threads))
6130
6131 (defun gnus-summary-initial-limit (&optional show-if-empty)
6132   "Figure out what the initial limit is supposed to be on group entry.
6133 This entails weeding out unwanted dormants, low-scored articles,
6134 fetch-old-headers verbiage, and so on."
6135   ;; Most groups have nothing to remove.
6136   (if (or gnus-inhibit-limiting
6137           (and (null gnus-newsgroup-dormant)
6138                (not (eq gnus-fetch-old-headers 'some))
6139                (not (eq gnus-fetch-old-headers 'invisible))
6140                (null gnus-summary-expunge-below)
6141                (not (eq gnus-build-sparse-threads 'some))
6142                (not (eq gnus-build-sparse-threads 'more))
6143                (null gnus-thread-expunge-below)
6144                (not gnus-use-nocem)))
6145       ()                                ; Do nothing.
6146     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6147     (setq gnus-newsgroup-limit nil)
6148     (mapatoms
6149      (lambda (node)
6150        (unless (car (symbol-value node))
6151          ;; These threads have no parents -- they are roots.
6152          (let ((nodes (cdr (symbol-value node)))
6153                thread)
6154            (while nodes
6155              (if (and gnus-thread-expunge-below
6156                       (< (gnus-thread-total-score (car nodes))
6157                          gnus-thread-expunge-below))
6158                  (gnus-expunge-thread (pop nodes))
6159                (setq thread (pop nodes))
6160                (gnus-summary-limit-children thread))))))
6161      gnus-newsgroup-dependencies)
6162     ;; If this limitation resulted in an empty group, we might
6163     ;; pop the previous limit and use it instead.
6164     (when (and (not gnus-newsgroup-limit)
6165                show-if-empty)
6166       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6167     gnus-newsgroup-limit))
6168
6169 (defun gnus-summary-limit-children (thread)
6170   "Return 1 if this subthread is visible and 0 if it is not."
6171   ;; First we get the number of visible children to this thread.  This
6172   ;; is done by recursing down the thread using this function, so this
6173   ;; will really go down to a leaf article first, before slowly
6174   ;; working its way up towards the root.
6175   (when thread
6176     (let ((children
6177            (if (cdr thread)
6178                (apply '+ (mapcar 'gnus-summary-limit-children
6179                                  (cdr thread)))
6180              0))
6181           (number (mail-header-number (car thread)))
6182           score)
6183       (if (and
6184            (not (memq number gnus-newsgroup-marked))
6185            (or
6186             ;; If this article is dormant and has absolutely no visible
6187             ;; children, then this article isn't visible.
6188             (and (memq number gnus-newsgroup-dormant)
6189                  (zerop children))
6190             ;; If this is "fetch-old-headered" and there is no
6191             ;; visible children, then we don't want this article.
6192             (and (eq gnus-fetch-old-headers 'some)
6193                  (gnus-summary-article-ancient-p number)
6194                  (zerop children))
6195             ;; If this is "fetch-old-headered" and `invisible', then
6196             ;; we don't want this article.
6197             (and (eq gnus-fetch-old-headers 'invisible)
6198                  (gnus-summary-article-ancient-p number))
6199             ;; If this is a sparsely inserted article with no children,
6200             ;; we don't want it.
6201             (and (eq gnus-build-sparse-threads 'some)
6202                  (gnus-summary-article-sparse-p number)
6203                  (zerop children))
6204             ;; If we use expunging, and this article is really
6205             ;; low-scored, then we don't want this article.
6206             (when (and gnus-summary-expunge-below
6207                        (< (setq score
6208                                 (or (cdr (assq number gnus-newsgroup-scored))
6209                                     gnus-summary-default-score))
6210                           gnus-summary-expunge-below))
6211               ;; We increase the expunge-tally here, but that has
6212               ;; nothing to do with the limits, really.
6213               (incf gnus-newsgroup-expunged-tally)
6214               ;; We also mark as read here, if that's wanted.
6215               (when (and gnus-summary-mark-below
6216                          (< score gnus-summary-mark-below))
6217                 (setq gnus-newsgroup-unreads
6218                       (delq number gnus-newsgroup-unreads))
6219                 (if gnus-newsgroup-auto-expire
6220                     (push number gnus-newsgroup-expirable)
6221                   (push (cons number gnus-low-score-mark)
6222                         gnus-newsgroup-reads)))
6223               t)
6224             ;; Check NoCeM things.
6225             (if (and gnus-use-nocem
6226                      (gnus-nocem-unwanted-article-p
6227                       (mail-header-id (car thread))))
6228                 (progn
6229                   (setq gnus-newsgroup-unreads
6230                         (delq number gnus-newsgroup-unreads))
6231                   t))))
6232           ;; Nope, invisible article.
6233           0
6234         ;; Ok, this article is to be visible, so we add it to the limit
6235         ;; and return 1.
6236         (push number gnus-newsgroup-limit)
6237         1))))
6238
6239 (defun gnus-expunge-thread (thread)
6240   "Mark all articles in THREAD as read."
6241   (let* ((number (mail-header-number (car thread))))
6242     (incf gnus-newsgroup-expunged-tally)
6243     ;; We also mark as read here, if that's wanted.
6244     (setq gnus-newsgroup-unreads
6245           (delq number gnus-newsgroup-unreads))
6246     (if gnus-newsgroup-auto-expire
6247         (push number gnus-newsgroup-expirable)
6248       (push (cons number gnus-low-score-mark)
6249             gnus-newsgroup-reads)))
6250   ;; Go recursively through all subthreads.
6251   (mapcar 'gnus-expunge-thread (cdr thread)))
6252
6253 ;; Summary article oriented commands
6254
6255 (defun gnus-summary-refer-parent-article (n)
6256   "Refer parent article N times.
6257 If N is negative, go to ancestor -N instead.
6258 The difference between N and the number of articles fetched is returned."
6259   (interactive "p")
6260   (let ((skip 1)
6261         error header ref)
6262     (when (not (natnump n))
6263       (setq skip (abs n)
6264             n 1))
6265     (while (and (> n 0)
6266                 (not error))
6267       (setq header (gnus-summary-article-header))
6268       (if (and (eq (mail-header-number header)
6269                    (cdr gnus-article-current))
6270                (equal gnus-newsgroup-name
6271                       (car gnus-article-current)))
6272           ;; If we try to find the parent of the currently
6273           ;; displayed article, then we take a look at the actual
6274           ;; References header, since this is slightly more
6275           ;; reliable than the References field we got from the
6276           ;; server.
6277           (save-excursion
6278             (set-buffer gnus-original-article-buffer)
6279             (nnheader-narrow-to-headers)
6280             (unless (setq ref (message-fetch-field "references"))
6281               (setq ref (message-fetch-field "in-reply-to")))
6282             (widen))
6283         (setq ref
6284               ;; It's not the current article, so we take a bet on
6285               ;; the value we got from the server.
6286               (mail-header-references header)))
6287       (if (and ref
6288                (not (equal ref "")))
6289           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6290             (gnus-message 1 "Couldn't find parent"))
6291         (gnus-message 1 "No references in article %d"
6292                       (gnus-summary-article-number))
6293         (setq error t))
6294       (decf n))
6295     (gnus-summary-position-point)
6296     n))
6297
6298 (defun gnus-summary-refer-references ()
6299   "Fetch all articles mentioned in the References header.
6300 Return the number of articles fetched."
6301   (interactive)
6302   (let ((ref (mail-header-references (gnus-summary-article-header)))
6303         (current (gnus-summary-article-number))
6304         (n 0))
6305     (if (or (not ref)
6306             (equal ref ""))
6307         (error "No References in the current article")
6308       ;; For each Message-ID in the References header...
6309       (while (string-match "<[^>]*>" ref)
6310         (incf n)
6311         ;; ... fetch that article.
6312         (gnus-summary-refer-article
6313          (prog1 (match-string 0 ref)
6314            (setq ref (substring ref (match-end 0))))))
6315       (gnus-summary-goto-subject current)
6316       (gnus-summary-position-point)
6317       n)))
6318
6319 (defun gnus-summary-refer-thread (&optional limit)
6320   "Fetch all articles in the current thread.
6321 If LIMIT (the numerical prefix), fetch that many old headers instead
6322 of what's specified by the `gnus-refer-thread-limit' variable."
6323   (interactive "P")
6324   (let ((id (mail-header-id (gnus-summary-article-header)))
6325         (limit (if limit (prefix-numeric-value limit)
6326                  gnus-refer-thread-limit))
6327         fmethod root)
6328     ;; We want to fetch LIMIT *old* headers, but we also have to
6329     ;; re-fetch all the headers in the current buffer, because many of
6330     ;; them may be undisplayed.  So we adjust LIMIT.
6331     (when (numberp limit)
6332       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6333     (unless (eq gnus-fetch-old-headers 'invisible)
6334       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6335       ;; Retrieve the headers and read them in.
6336       (if (eq (gnus-retrieve-headers
6337                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6338               'nov)
6339           (gnus-build-all-threads)
6340         (error "Can't fetch thread from backends that don't support NOV"))
6341       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6342     (gnus-summary-limit-include-thread id)))
6343
6344 (defun gnus-summary-refer-article (message-id &optional arg)
6345   "Fetch an article specified by MESSAGE-ID.
6346 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6347 or `gnus-select-method', no matter what backend the article comes from."
6348   (interactive "sMessage-ID: \nP")
6349   (when (and (stringp message-id)
6350              (not (zerop (length message-id))))
6351     ;; Construct the correct Message-ID if necessary.
6352     ;; Suggested by tale@pawl.rpi.edu.
6353     (unless (string-match "^<" message-id)
6354       (setq message-id (concat "<" message-id)))
6355     (unless (string-match ">$" message-id)
6356       (setq message-id (concat message-id ">")))
6357     (let* ((header (gnus-id-to-header message-id))
6358            (sparse (and header
6359                         (gnus-summary-article-sparse-p
6360                          (mail-header-number header))
6361                         (memq (mail-header-number header)
6362                               gnus-newsgroup-limit)))
6363            h)
6364       (cond
6365        ;; If the article is present in the buffer we just go to it.
6366        ((and header
6367              (or (not (gnus-summary-article-sparse-p
6368                        (mail-header-number header)))
6369                  sparse))
6370         (prog1
6371             (gnus-summary-goto-article
6372              (mail-header-number header) nil t)
6373           (when sparse
6374             (gnus-summary-update-article (mail-header-number header)))))
6375        (t
6376         ;; We fetch the article
6377         (let ((gnus-override-method
6378                (cond ((gnus-news-group-p gnus-newsgroup-name)
6379                       gnus-refer-article-method)
6380                      (arg
6381                       (or gnus-refer-article-method gnus-select-method))
6382                      (t nil)))
6383               number)
6384           ;; Start the special refer-article method, if necessary.
6385           (when (and gnus-refer-article-method
6386                      (gnus-news-group-p gnus-newsgroup-name))
6387             (gnus-check-server gnus-refer-article-method))
6388           ;; Fetch the header, and display the article.
6389           (if (setq number (gnus-summary-insert-subject message-id))
6390               (gnus-summary-select-article nil nil nil number)
6391             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6392
6393 (defun gnus-summary-edit-parameters ()
6394   "Edit the group parameters of the current group."
6395   (interactive)
6396   (gnus-group-edit-group gnus-newsgroup-name 'params))
6397
6398 (defun gnus-summary-enter-digest-group (&optional force)
6399   "Enter an nndoc group based on the current article.
6400 If FORCE, force a digest interpretation.  If not, try
6401 to guess what the document format is."
6402   (interactive "P")
6403   (let ((conf gnus-current-window-configuration))
6404     (save-excursion
6405       (gnus-summary-select-article))
6406     (setq gnus-current-window-configuration conf)
6407     (let* ((name (format "%s-%d"
6408                          (gnus-group-prefixed-name
6409                           gnus-newsgroup-name (list 'nndoc ""))
6410                          (save-excursion
6411                            (set-buffer gnus-summary-buffer)
6412                            gnus-current-article)))
6413            (ogroup gnus-newsgroup-name)
6414            (params (append (gnus-info-params (gnus-get-info ogroup))
6415                            (list (cons 'to-group ogroup))
6416                            (list (cons 'save-article-group ogroup))))
6417            (case-fold-search t)
6418            (buf (current-buffer))
6419            dig)
6420       (save-excursion
6421         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6422         (insert-buffer-substring gnus-original-article-buffer)
6423         ;; Remove lines that may lead nndoc to misinterpret the
6424         ;; document type.
6425         (narrow-to-region
6426          (goto-char (point-min))
6427          (or (search-forward "\n\n" nil t) (point)))
6428         (goto-char (point-min))
6429         (delete-matching-lines "^\\(Path\\):\\|^From ")
6430         (widen))
6431       (unwind-protect
6432           (if (gnus-group-read-ephemeral-group
6433                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6434                             (nndoc-article-type
6435                              ,(if force 'digest 'guess))) t)
6436               ;; Make all postings to this group go to the parent group.
6437               (nconc (gnus-info-params (gnus-get-info name))
6438                      params)
6439             ;; Couldn't select this doc group.
6440             (switch-to-buffer buf)
6441             (gnus-set-global-variables)
6442             (gnus-configure-windows 'summary)
6443             (gnus-message 3 "Article couldn't be entered?"))
6444         (kill-buffer dig)))))
6445
6446 (defun gnus-summary-read-document (n)
6447   "Open a new group based on the current article(s).
6448 This will allow you to read digests and other similar
6449 documents as newsgroups.
6450 Obeys the standard process/prefix convention."
6451   (interactive "P")
6452   (let* ((articles (gnus-summary-work-articles n))
6453          (ogroup gnus-newsgroup-name)
6454          (params (append (gnus-info-params (gnus-get-info ogroup))
6455                          (list (cons 'to-group ogroup))))
6456          article group egroup groups vgroup)
6457     (while (setq article (pop articles))
6458       (setq group (format "%s-%d" gnus-newsgroup-name article))
6459       (gnus-summary-remove-process-mark article)
6460       (when (gnus-summary-display-article article)
6461         (save-excursion
6462           (nnheader-temp-write nil
6463             (insert-buffer-substring gnus-original-article-buffer)
6464             ;; Remove some headers that may lead nndoc to make
6465             ;; the wrong guess.
6466             (message-narrow-to-head)
6467             (goto-char (point-min))
6468             (delete-matching-lines "^\\(Path\\):\\|^From ")
6469             (widen)
6470             (if (setq egroup
6471                       (gnus-group-read-ephemeral-group
6472                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6473                                      (nndoc-article-type guess))
6474                        t nil t))
6475                 (progn
6476                   ;; Make all postings to this group go to the parent group.
6477                   (nconc (gnus-info-params (gnus-get-info egroup))
6478                          params)
6479                   (push egroup groups))
6480               ;; Couldn't select this doc group.
6481               (gnus-error 3 "Article couldn't be entered"))))))
6482     ;; Now we have selected all the documents.
6483     (cond
6484      ((not groups)
6485       (error "None of the articles could be interpreted as documents"))
6486      ((gnus-group-read-ephemeral-group
6487        (setq vgroup (format
6488                      "nnvirtual:%s-%s" gnus-newsgroup-name
6489                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6490        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6491        t
6492        (cons (current-buffer) 'summary)))
6493      (t
6494       (error "Couldn't select virtual nndoc group")))))
6495
6496 (defun gnus-summary-isearch-article (&optional regexp-p)
6497   "Do incremental search forward on the current article.
6498 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6499   (interactive "P")
6500   (gnus-summary-select-article)
6501   (gnus-configure-windows 'article)
6502   (gnus-eval-in-buffer-window gnus-article-buffer
6503     (save-restriction
6504       (widen)
6505       (isearch-forward regexp-p))))
6506
6507 (defun gnus-summary-search-article-forward (regexp &optional backward)
6508   "Search for an article containing REGEXP forward.
6509 If BACKWARD, search backward instead."
6510   (interactive
6511    (list (read-string
6512           (format "Search article %s (regexp%s): "
6513                   (if current-prefix-arg "backward" "forward")
6514                   (if gnus-last-search-regexp
6515                       (concat ", default " gnus-last-search-regexp)
6516                     "")))
6517          current-prefix-arg))
6518   (if (string-equal regexp "")
6519       (setq regexp (or gnus-last-search-regexp ""))
6520     (setq gnus-last-search-regexp regexp))
6521   (if (gnus-summary-search-article regexp backward)
6522       (gnus-summary-show-thread)
6523     (error "Search failed: \"%s\"" regexp)))
6524
6525 (defun gnus-summary-search-article-backward (regexp)
6526   "Search for an article containing REGEXP backward."
6527   (interactive
6528    (list (read-string
6529           (format "Search article backward (regexp%s): "
6530                   (if gnus-last-search-regexp
6531                       (concat ", default " gnus-last-search-regexp)
6532                     "")))))
6533   (gnus-summary-search-article-forward regexp 'backward))
6534
6535 (defun gnus-summary-search-article (regexp &optional backward)
6536   "Search for an article containing REGEXP.
6537 Optional argument BACKWARD means do search for backward.
6538 `gnus-select-article-hook' is not called during the search."
6539   ;; We have to require this here to make sure that the following
6540   ;; dynamic binding isn't shadowed by autoloading.
6541   (require 'gnus-async)
6542   (let ((gnus-select-article-hook nil)  ;Disable hook.
6543         (gnus-article-display-hook nil)
6544         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6545         (gnus-use-article-prefetch nil)
6546         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6547         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6548         (sum (current-buffer))
6549         (found nil)
6550         point)
6551     (gnus-save-hidden-threads
6552       (gnus-summary-select-article)
6553       (set-buffer gnus-article-buffer)
6554       (when backward
6555         (forward-line -1))
6556       (while (not found)
6557         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6558         (if (if backward
6559                 (re-search-backward regexp nil t)
6560               (re-search-forward regexp nil t))
6561             ;; We found the regexp.
6562             (progn
6563               (setq found 'found)
6564               (beginning-of-line)
6565               (set-window-start
6566                (get-buffer-window (current-buffer))
6567                (point))
6568               (forward-line 1)
6569               (set-buffer sum)
6570               (setq point (point)))
6571           ;; We didn't find it, so we go to the next article.
6572           (set-buffer sum)
6573           (setq found 'not)
6574           (while (eq found 'not)
6575             (if (not (if backward (gnus-summary-find-prev)
6576                        (gnus-summary-find-next)))
6577                 ;; No more articles.
6578                 (setq found t)
6579               ;; Select the next article and adjust point.
6580               (unless (gnus-summary-article-sparse-p
6581                        (gnus-summary-article-number))
6582                 (setq found nil)
6583                 (gnus-summary-select-article)
6584                 (set-buffer gnus-article-buffer)
6585                 (widen)
6586                 (goto-char (if backward (point-max) (point-min))))))))
6587       (gnus-message 7 ""))
6588     ;; Return whether we found the regexp.
6589     (when (eq found 'found)
6590       (goto-char point)
6591       (gnus-summary-show-thread)
6592       (gnus-summary-goto-subject gnus-current-article)
6593       (gnus-summary-position-point)
6594       t)))
6595
6596 (defun gnus-summary-find-matching (header regexp &optional backward unread
6597                                           not-case-fold)
6598   "Return a list of all articles that match REGEXP on HEADER.
6599 The search stars on the current article and goes forwards unless
6600 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6601 If UNREAD is non-nil, only unread articles will
6602 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6603 in the comparisons."
6604   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6605                 (gnus-data-find-list
6606                  (gnus-summary-article-number) (gnus-data-list backward))))
6607         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6608         (case-fold-search (not not-case-fold))
6609         articles d)
6610     (unless (fboundp (intern (concat "mail-header-" header)))
6611       (error "%s is not a valid header" header))
6612     (while data
6613       (setq d (car data))
6614       (and (or (not unread)             ; We want all articles...
6615                (gnus-data-unread-p d))  ; Or just unreads.
6616            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6617            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6618            (push (gnus-data-number d) articles)) ; Success!
6619       (setq data (cdr data)))
6620     (nreverse articles)))
6621
6622 (defun gnus-summary-execute-command (header regexp command &optional backward)
6623   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6624 If HEADER is an empty string (or nil), the match is done on the entire
6625 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6626   (interactive
6627    (list (let ((completion-ignore-case t))
6628            (completing-read
6629             "Header name: "
6630             (mapcar (lambda (string) (list string))
6631                     '("Number" "Subject" "From" "Lines" "Date"
6632                       "Message-ID" "Xref" "References" "Body"))
6633             nil 'require-match))
6634          (read-string "Regexp: ")
6635          (read-key-sequence "Command: ")
6636          current-prefix-arg))
6637   (when (equal header "Body")
6638     (setq header ""))
6639   ;; Hidden thread subtrees must be searched as well.
6640   (gnus-summary-show-all-threads)
6641   ;; We don't want to change current point nor window configuration.
6642   (save-excursion
6643     (save-window-excursion
6644       (gnus-message 6 "Executing %s..." (key-description command))
6645       ;; We'd like to execute COMMAND interactively so as to give arguments.
6646       (gnus-execute header regexp
6647                     `(call-interactively ',(key-binding command))
6648                     backward)
6649       (gnus-message 6 "Executing %s...done" (key-description command)))))
6650
6651 (defun gnus-summary-beginning-of-article ()
6652   "Scroll the article back to the beginning."
6653   (interactive)
6654   (gnus-summary-select-article)
6655   (gnus-configure-windows 'article)
6656   (gnus-eval-in-buffer-window gnus-article-buffer
6657     (widen)
6658     (goto-char (point-min))
6659     (when gnus-page-broken
6660       (gnus-narrow-to-page))))
6661
6662 (defun gnus-summary-end-of-article ()
6663   "Scroll to the end of the article."
6664   (interactive)
6665   (gnus-summary-select-article)
6666   (gnus-configure-windows 'article)
6667   (gnus-eval-in-buffer-window gnus-article-buffer
6668     (widen)
6669     (goto-char (point-max))
6670     (recenter -3)
6671     (when gnus-page-broken
6672       (gnus-narrow-to-page))))
6673
6674 (defun gnus-summary-print-article (&optional filename n)
6675   "Generate and print a PostScript image of the N next (mail) articles.
6676
6677 If N is negative, print the N previous articles.  If N is nil and articles
6678 have been marked with the process mark, print these instead.
6679
6680 If the optional second argument FILENAME is nil, send the image to the
6681 printer.  If FILENAME is a string, save the PostScript image in a file with
6682 that name.  If FILENAME is a number, prompt the user for the name of the file
6683 to save in."
6684   (interactive (list (ps-print-preprint current-prefix-arg)
6685                      current-prefix-arg))
6686   (dolist (article (gnus-summary-work-articles n))
6687     (gnus-summary-select-article nil nil 'pseudo article)
6688     (gnus-eval-in-buffer-window gnus-article-buffer
6689       (let ((buffer (generate-new-buffer " *print*")))
6690         (unwind-protect
6691             (progn
6692               (copy-to-buffer buffer (point-min) (point-max))
6693               (set-buffer buffer)
6694               (gnus-article-delete-invisible-text)
6695               (let ((ps-left-header
6696                      (list 
6697                       (concat "("
6698                               (mail-header-subject gnus-current-headers) ")")
6699                       (concat "("
6700                               (mail-header-from gnus-current-headers) ")")))
6701                     (ps-right-header 
6702                      (list 
6703                       "/pagenumberstring load" 
6704                       (concat "("
6705                               (mail-header-date gnus-current-headers) ")"))))
6706                 (gnus-run-hooks 'gnus-ps-print-hook)
6707                 (save-excursion
6708                   (ps-print-buffer-with-faces filename))))
6709           (kill-buffer buffer))))))
6710
6711 (defun gnus-summary-show-article (&optional arg)
6712   "Force re-fetching of the current article.
6713 If ARG (the prefix) is non-nil, show the raw article without any
6714 article massaging functions being run."
6715   (interactive "P")
6716   (if (not arg)
6717       ;; Select the article the normal way.
6718       (gnus-summary-select-article nil 'force)
6719     ;; Bind the article treatment functions to nil.
6720     (let ((gnus-have-all-headers t)
6721           gnus-article-display-hook
6722           gnus-article-prepare-hook
6723           gnus-break-pages
6724           gnus-show-mime
6725           gnus-visual)
6726       (gnus-summary-select-article nil 'force)))
6727   (gnus-summary-goto-subject gnus-current-article)
6728   (gnus-summary-position-point))
6729
6730 (defun gnus-summary-verbose-headers (&optional arg)
6731   "Toggle permanent full header display.
6732 If ARG is a positive number, turn header display on.
6733 If ARG is a negative number, turn header display off."
6734   (interactive "P")
6735   (setq gnus-show-all-headers
6736         (cond ((or (not (numberp arg))
6737                    (zerop arg))
6738                (not gnus-show-all-headers))
6739               ((natnump arg)
6740                t)))
6741   (gnus-summary-show-article))
6742
6743 (defun gnus-summary-toggle-header (&optional arg)
6744   "Show the headers if they are hidden, or hide them if they are shown.
6745 If ARG is a positive number, show the entire header.
6746 If ARG is a negative number, hide the unwanted header lines."
6747   (interactive "P")
6748   (save-excursion
6749     (set-buffer gnus-article-buffer)
6750     (let* ((buffer-read-only nil)
6751            (inhibit-point-motion-hooks t)
6752            (hidden (text-property-any
6753                     (goto-char (point-min)) (search-forward "\n\n")
6754                     'invisible t))
6755            e)
6756       (goto-char (point-min))
6757       (when (search-forward "\n\n" nil t)
6758         (delete-region (point-min) (1- (point))))
6759       (goto-char (point-min))
6760       (save-excursion
6761         (set-buffer gnus-original-article-buffer)
6762         (goto-char (point-min))
6763         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
6764       (insert-buffer-substring gnus-original-article-buffer 1 e)
6765       (let ((article-inhibit-hiding t))
6766         (gnus-run-hooks 'gnus-article-display-hook))
6767       (when (or (not hidden) (and (numberp arg) (< arg 0)))
6768         (gnus-article-hide-headers)))))
6769
6770 (defun gnus-summary-show-all-headers ()
6771   "Make all header lines visible."
6772   (interactive)
6773   (gnus-article-show-all-headers))
6774
6775 (defun gnus-summary-toggle-mime (&optional arg)
6776   "Toggle MIME processing.
6777 If ARG is a positive number, turn MIME processing on."
6778   (interactive "P")
6779   (setq gnus-show-mime
6780         (if (null arg) (not gnus-show-mime)
6781           (> (prefix-numeric-value arg) 0)))
6782   (gnus-summary-select-article t 'force))
6783
6784 (defun gnus-summary-caesar-message (&optional arg)
6785   "Caesar rotate the current article by 13.
6786 The numerical prefix specifies how many places to rotate each letter
6787 forward."
6788   (interactive "P")
6789   (gnus-summary-select-article)
6790   (let ((mail-header-separator ""))
6791     (gnus-eval-in-buffer-window gnus-article-buffer
6792       (save-restriction
6793         (widen)
6794         (let ((start (window-start))
6795               buffer-read-only)
6796           (message-caesar-buffer-body arg)
6797           (set-window-start (get-buffer-window (current-buffer)) start))))))
6798
6799 (defun gnus-summary-stop-page-breaking ()
6800   "Stop page breaking in the current article."
6801   (interactive)
6802   (gnus-summary-select-article)
6803   (gnus-eval-in-buffer-window gnus-article-buffer
6804     (widen)
6805     (when (gnus-visual-p 'page-marker)
6806       (let ((buffer-read-only nil))
6807         (gnus-remove-text-with-property 'gnus-prev)
6808         (gnus-remove-text-with-property 'gnus-next))
6809       (setq gnus-page-broken nil))))
6810
6811 (defun gnus-summary-move-article (&optional n to-newsgroup
6812                                             select-method action)
6813   "Move the current article to a different newsgroup.
6814 If N is a positive number, move the N next articles.
6815 If N is a negative number, move the N previous articles.
6816 If N is nil and any articles have been marked with the process mark,
6817 move those articles instead.
6818 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
6819 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
6820 re-spool using this method.
6821
6822 For this function to work, both the current newsgroup and the
6823 newsgroup that you want to move to have to support the `request-move'
6824 and `request-accept' functions."
6825   (interactive "P")
6826   (unless action
6827     (setq action 'move))
6828   ;; Disable marking as read.
6829   (let (gnus-mark-article-hook)
6830     (save-window-excursion
6831       (gnus-summary-select-article)))
6832   ;; Check whether the source group supports the required functions.
6833   (cond ((and (eq action 'move)
6834               (not (gnus-check-backend-function
6835                     'request-move-article gnus-newsgroup-name)))
6836          (error "The current group does not support article moving"))
6837         ((and (eq action 'crosspost)
6838               (not (gnus-check-backend-function
6839                     'request-replace-article gnus-newsgroup-name)))
6840          (error "The current group does not support article editing")))
6841   (let ((articles (gnus-summary-work-articles n))
6842         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
6843         (names '((move "Move" "Moving")
6844                  (copy "Copy" "Copying")
6845                  (crosspost "Crosspost" "Crossposting")))
6846         (copy-buf (save-excursion
6847                     (nnheader-set-temp-buffer " *copy article*")))
6848         art-group to-method new-xref article to-groups)
6849     (unless (assq action names)
6850       (error "Unknown action %s" action))
6851     ;; Read the newsgroup name.
6852     (when (and (not to-newsgroup)
6853                (not select-method))
6854       (setq to-newsgroup
6855             (gnus-read-move-group-name
6856              (cadr (assq action names))
6857              (symbol-value (intern (format "gnus-current-%s-group" action)))
6858              articles prefix))
6859       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
6860     (setq to-method (or select-method
6861                         (gnus-group-name-to-method to-newsgroup)))
6862     ;; Check the method we are to move this article to...
6863     (unless (gnus-check-backend-function
6864              'request-accept-article (car to-method))
6865       (error "%s does not support article copying" (car to-method)))
6866     (unless (gnus-check-server to-method)
6867       (error "Can't open server %s" (car to-method)))
6868     (gnus-message 6 "%s to %s: %s..."
6869                   (caddr (assq action names))
6870                   (or (car select-method) to-newsgroup) articles)
6871     (while articles
6872       (setq article (pop articles))
6873       (setq
6874        art-group
6875        (cond
6876         ;; Move the article.
6877         ((eq action 'move)
6878          ;; Remove this article from future suppression.
6879          (gnus-dup-unsuppress-article article)
6880          (gnus-request-move-article
6881           article                       ; Article to move
6882           gnus-newsgroup-name           ; From newsgroup
6883           (nth 1 (gnus-find-method-for-group
6884                   gnus-newsgroup-name)) ; Server
6885           (list 'gnus-request-accept-article
6886                 to-newsgroup (list 'quote select-method)
6887                 (not articles))         ; Accept form
6888           (not articles)))              ; Only save nov last time
6889         ;; Copy the article.
6890         ((eq action 'copy)
6891          (save-excursion
6892            (set-buffer copy-buf)
6893            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
6894              (gnus-request-accept-article
6895               to-newsgroup select-method (not articles)))))
6896         ;; Crosspost the article.
6897         ((eq action 'crosspost)
6898          (let ((xref (message-tokenize-header
6899                       (mail-header-xref (gnus-summary-article-header article))
6900                       " ")))
6901            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
6902                                   ":" article))
6903            (unless xref
6904              (setq xref (list (system-name))))
6905            (setq new-xref
6906                  (concat
6907                   (mapconcat 'identity
6908                              (delete "Xref:" (delete new-xref xref))
6909                              " ")
6910                   " " new-xref))
6911            (save-excursion
6912              (set-buffer copy-buf)
6913              ;; First put the article in the destination group.
6914              (gnus-request-article-this-buffer article gnus-newsgroup-name)
6915              (when (consp (setq art-group
6916                                 (gnus-request-accept-article
6917                                  to-newsgroup select-method (not articles))))
6918                (setq new-xref (concat new-xref " " (car art-group)
6919                                       ":" (cdr art-group)))
6920                ;; Now we have the new Xrefs header, so we insert
6921                ;; it and replace the new article.
6922                (nnheader-replace-header "Xref" new-xref)
6923                (gnus-request-replace-article
6924                 (cdr art-group) to-newsgroup (current-buffer))
6925                art-group))))))
6926       (cond
6927        ((not art-group)
6928         (gnus-message 1 "Couldn't %s article %s"
6929                       (cadr (assq action names)) article))
6930        ((and (eq art-group 'junk)
6931              (eq action 'move))
6932         (gnus-summary-mark-article article gnus-canceled-mark)
6933         (gnus-message 4 "Deleted article %s" article))
6934        (t
6935         (let* ((entry
6936                 (or
6937                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
6938                  (gnus-gethash
6939                   (gnus-group-prefixed-name
6940                    (car art-group)
6941                    (or select-method
6942                        (gnus-find-method-for-group to-newsgroup)))
6943                   gnus-newsrc-hashtb)))
6944                (info (nth 2 entry))
6945                (to-group (gnus-info-group info)))
6946           ;; Update the group that has been moved to.
6947           (when (and info
6948                      (memq action '(move copy)))
6949             (unless (member to-group to-groups)
6950               (push to-group to-groups))
6951
6952             (unless (memq article gnus-newsgroup-unreads)
6953               (gnus-info-set-read
6954                info (gnus-add-to-range (gnus-info-read info)
6955                                        (list (cdr art-group)))))
6956
6957             ;; Copy any marks over to the new group.
6958             (let ((marks gnus-article-mark-lists)
6959                   (to-article (cdr art-group)))
6960
6961               ;; See whether the article is to be put in the cache.
6962               (when gnus-use-cache
6963                 (gnus-cache-possibly-enter-article
6964                  to-group to-article
6965                  (let ((header (copy-sequence
6966                                 (gnus-summary-article-header article))))
6967                    (mail-header-set-number header to-article)
6968                    header)
6969                  (memq article gnus-newsgroup-marked)
6970                  (memq article gnus-newsgroup-dormant)
6971                  (memq article gnus-newsgroup-unreads)))
6972
6973               (when (and (equal to-group gnus-newsgroup-name)
6974                          (not (memq article gnus-newsgroup-unreads)))
6975                 ;; Mark this article as read in this group.
6976                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
6977                 (setcdr (gnus-active to-group) to-article)
6978                 (setcdr gnus-newsgroup-active to-article))
6979
6980               (while marks
6981                 (when (memq article (symbol-value
6982                                      (intern (format "gnus-newsgroup-%s"
6983                                                      (caar marks)))))
6984                   ;; If the other group is the same as this group,
6985                   ;; then we have to add the mark to the list.
6986                   (when (equal to-group gnus-newsgroup-name)
6987                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
6988                          (cons to-article
6989                                (symbol-value
6990                                 (intern (format "gnus-newsgroup-%s"
6991                                                 (caar marks)))))))
6992                   ;; Copy the marks to other group.
6993                   (gnus-add-marked-articles
6994                    to-group (cdar marks) (list to-article) info))
6995                 (setq marks (cdr marks)))
6996
6997               (gnus-dribble-enter
6998                (concat "(gnus-group-set-info '"
6999                        (gnus-prin1-to-string (gnus-get-info to-group))
7000                        ")"))))
7001
7002           ;; Update the Xref header in this article to point to
7003           ;; the new crossposted article we have just created.
7004           (when (eq action 'crosspost)
7005             (save-excursion
7006               (set-buffer copy-buf)
7007               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7008               (nnheader-replace-header "Xref" new-xref)
7009               (gnus-request-replace-article
7010                article gnus-newsgroup-name (current-buffer)))))
7011
7012         ;;;!!!Why is this necessary?
7013         (set-buffer gnus-summary-buffer)
7014         
7015         (gnus-summary-goto-subject article)
7016         (when (eq action 'move)
7017           (gnus-summary-mark-article article gnus-canceled-mark))))
7018       (gnus-summary-remove-process-mark article))
7019     ;; Re-activate all groups that have been moved to.
7020     (while to-groups
7021       (save-excursion
7022         (set-buffer gnus-group-buffer)
7023         (when (gnus-group-goto-group (car to-groups) t)
7024           (gnus-group-get-new-news-this-group 1 t))
7025         (pop to-groups)))
7026
7027     (gnus-kill-buffer copy-buf)
7028     (gnus-summary-position-point)
7029     (gnus-set-mode-line 'summary)))
7030
7031 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7032   "Move the current article to a different newsgroup.
7033 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7034 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7035 re-spool using this method."
7036   (interactive "P")
7037   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7038
7039 (defun gnus-summary-crosspost-article (&optional n)
7040   "Crosspost the current article to some other group."
7041   (interactive "P")
7042   (gnus-summary-move-article n nil nil 'crosspost))
7043
7044 (defcustom gnus-summary-respool-default-method nil
7045   "Default method for respooling an article.
7046 If nil, use to the current newsgroup method."
7047   :type `(choice (gnus-select-method :value (nnml ""))
7048                  (const nil))
7049   :group 'gnus-summary-mail)
7050
7051 (defun gnus-summary-respool-article (&optional n method)
7052   "Respool the current article.
7053 The article will be squeezed through the mail spooling process again,
7054 which means that it will be put in some mail newsgroup or other
7055 depending on `nnmail-split-methods'.
7056 If N is a positive number, respool the N next articles.
7057 If N is a negative number, respool the N previous articles.
7058 If N is nil and any articles have been marked with the process mark,
7059 respool those articles instead.
7060
7061 Respooling can be done both from mail groups and \"real\" newsgroups.
7062 In the former case, the articles in question will be moved from the
7063 current group into whatever groups they are destined to.  In the
7064 latter case, they will be copied into the relevant groups."
7065   (interactive
7066    (list current-prefix-arg
7067          (let* ((methods (gnus-methods-using 'respool))
7068                 (methname
7069                  (symbol-name (or gnus-summary-respool-default-method
7070                                   (car (gnus-find-method-for-group
7071                                         gnus-newsgroup-name)))))
7072                 (method
7073                  (gnus-completing-read
7074                   methname "What backend do you want to use when respooling?"
7075                   methods nil t nil 'gnus-mail-method-history))
7076                 ms)
7077            (cond
7078             ((zerop (length (setq ms (gnus-servers-using-backend
7079                                       (intern method)))))
7080              (list (intern method) ""))
7081             ((= 1 (length ms))
7082              (car ms))
7083             (t
7084              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7085                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7086                            ms-alist))))))))
7087   (unless method
7088     (error "No method given for respooling"))
7089   (if (assoc (symbol-name
7090               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7091              (gnus-methods-using 'respool))
7092       (gnus-summary-move-article n nil method)
7093     (gnus-summary-copy-article n nil method)))
7094
7095 (defun gnus-summary-import-article (file)
7096   "Import an arbitrary file into a mail newsgroup."
7097   (interactive "fImport file: ")
7098   (let ((group gnus-newsgroup-name)
7099         (now (current-time))
7100         atts lines)
7101     (unless (gnus-check-backend-function 'request-accept-article group)
7102       (error "%s does not support article importing" group))
7103     (or (file-readable-p file)
7104         (not (file-regular-p file))
7105         (error "Can't read %s" file))
7106     (save-excursion
7107       (set-buffer (get-buffer-create " *import file*"))
7108       (buffer-disable-undo (current-buffer))
7109       (erase-buffer)
7110       (insert-file-contents file)
7111       (goto-char (point-min))
7112       (unless (nnheader-article-p)
7113         ;; This doesn't look like an article, so we fudge some headers.
7114         (setq atts (file-attributes file)
7115               lines (count-lines (point-min) (point-max)))
7116         (insert "From: " (read-string "From: ") "\n"
7117                 "Subject: " (read-string "Subject: ") "\n"
7118                 "Date: " (timezone-make-date-arpa-standard
7119                           (current-time-string (nth 5 atts))
7120                           (current-time-zone now)
7121                           (current-time-zone now))
7122                 "\n"
7123                 "Message-ID: " (message-make-message-id) "\n"
7124                 "Lines: " (int-to-string lines) "\n"
7125                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7126       (gnus-request-accept-article group nil t)
7127       (kill-buffer (current-buffer)))))
7128
7129 (defun gnus-summary-article-posted-p ()
7130   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7131 This will be the case if the article has both been mailed and posted."
7132   (interactive)
7133   (let ((id (mail-header-references (gnus-summary-article-header)))
7134         (gnus-override-method
7135          (or gnus-refer-article-method gnus-select-method)))
7136     (if (gnus-request-head id "")
7137         (gnus-message 2 "The current message was found on %s"
7138                       gnus-override-method)
7139       (gnus-message 2 "The current message couldn't be found on %s"
7140                     gnus-override-method)
7141       nil)))
7142
7143 (defun gnus-summary-expire-articles (&optional now)
7144   "Expire all articles that are marked as expirable in the current group."
7145   (interactive)
7146   (when (gnus-check-backend-function
7147          'request-expire-articles gnus-newsgroup-name)
7148     ;; This backend supports expiry.
7149     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7150            (expirable (if total
7151                           (progn
7152                             ;; We need to update the info for
7153                             ;; this group for `gnus-list-of-read-articles'
7154                             ;; to give us the right answer.
7155                             (gnus-run-hooks 'gnus-exit-group-hook)
7156                             (gnus-summary-update-info)
7157                             (gnus-list-of-read-articles gnus-newsgroup-name))
7158                         (setq gnus-newsgroup-expirable
7159                               (sort gnus-newsgroup-expirable '<))))
7160            (expiry-wait (if now 'immediate
7161                           (gnus-group-find-parameter
7162                            gnus-newsgroup-name 'expiry-wait)))
7163            es)
7164       (when expirable
7165         ;; There are expirable articles in this group, so we run them
7166         ;; through the expiry process.
7167         (gnus-message 6 "Expiring articles...")
7168         ;; The list of articles that weren't expired is returned.
7169         (save-excursion
7170           (if expiry-wait
7171               (let ((nnmail-expiry-wait-function nil)
7172                     (nnmail-expiry-wait expiry-wait))
7173                 (setq es (gnus-request-expire-articles
7174                           expirable gnus-newsgroup-name)))
7175             (setq es (gnus-request-expire-articles
7176                       expirable gnus-newsgroup-name))))
7177         (unless total
7178           (setq gnus-newsgroup-expirable es))
7179         ;; We go through the old list of expirable, and mark all
7180         ;; really expired articles as nonexistent.
7181         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7182           (let ((gnus-use-cache nil))
7183             (while expirable
7184               (unless (memq (car expirable) es)
7185                 (when (gnus-data-find (car expirable))
7186                   (gnus-summary-mark-article
7187                    (car expirable) gnus-canceled-mark)))
7188               (setq expirable (cdr expirable)))))
7189         (gnus-message 6 "Expiring articles...done")))))
7190
7191 (defun gnus-summary-expire-articles-now ()
7192   "Expunge all expirable articles in the current group.
7193 This means that *all* articles that are marked as expirable will be
7194 deleted forever, right now."
7195   (interactive)
7196   (or gnus-expert-user
7197       (gnus-yes-or-no-p
7198        "Are you really, really, really sure you want to delete all these messages? ")
7199       (error "Phew!"))
7200   (gnus-summary-expire-articles t))
7201
7202 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7203 (defun gnus-summary-delete-article (&optional n)
7204   "Delete the N next (mail) articles.
7205 This command actually deletes articles.  This is not a marking
7206 command.  The article will disappear forever from your life, never to
7207 return.
7208 If N is negative, delete backwards.
7209 If N is nil and articles have been marked with the process mark,
7210 delete these instead."
7211   (interactive "P")
7212   (unless (gnus-check-backend-function 'request-expire-articles
7213                                        gnus-newsgroup-name)
7214     (error "The current newsgroup does not support article deletion"))
7215   ;; Compute the list of articles to delete.
7216   (let ((articles (gnus-summary-work-articles n))
7217         not-deleted)
7218     (if (and gnus-novice-user
7219              (not (gnus-yes-or-no-p
7220                    (format "Do you really want to delete %s forever? "
7221                            (if (> (length articles) 1)
7222                                (format "these %s articles" (length articles))
7223                              "this article")))))
7224         ()
7225       ;; Delete the articles.
7226       (setq not-deleted (gnus-request-expire-articles
7227                          articles gnus-newsgroup-name 'force))
7228       (while articles
7229         (gnus-summary-remove-process-mark (car articles))
7230         ;; The backend might not have been able to delete the article
7231         ;; after all.
7232         (unless (memq (car articles) not-deleted)
7233           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7234         (setq articles (cdr articles)))
7235       (when not-deleted
7236         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7237     (gnus-summary-position-point)
7238     (gnus-set-mode-line 'summary)
7239     not-deleted))
7240
7241 (defun gnus-summary-edit-article (&optional force)
7242   "Edit the current article.
7243 This will have permanent effect only in mail groups.
7244 If FORCE is non-nil, allow editing of articles even in read-only
7245 groups."
7246   (interactive "P")
7247   (save-excursion
7248     (set-buffer gnus-summary-buffer)
7249     (gnus-set-global-variables)
7250     (when (and (not force)
7251                (gnus-group-read-only-p))
7252       (error "The current newsgroup does not support article editing"))
7253     ;; Select article if needed.
7254     (unless (eq (gnus-summary-article-number)
7255                 gnus-current-article)
7256       (gnus-summary-select-article t))
7257     (gnus-article-date-original)
7258     (gnus-article-edit-article
7259      `(lambda (no-highlight)
7260         (gnus-summary-edit-article-done
7261          ,(or (mail-header-references gnus-current-headers) "")
7262          ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))
7263
7264 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7265
7266 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7267                                                  no-highlight)
7268   "Make edits to the current article permanent."
7269   (interactive)
7270   ;; Replace the article.
7271   (if (and (not read-only)
7272            (not (gnus-request-replace-article
7273                  (cdr gnus-article-current) (car gnus-article-current)
7274                  (current-buffer))))
7275       (error "Couldn't replace article")
7276     ;; Update the summary buffer.
7277     (if (and references
7278              (equal (message-tokenize-header references " ")
7279                     (message-tokenize-header
7280                      (or (message-fetch-field "references") "") " ")))
7281         ;; We only have to update this line.
7282         (save-excursion
7283           (save-restriction
7284             (message-narrow-to-head)
7285             (let ((head (buffer-string))
7286                   header)
7287               (nnheader-temp-write nil
7288                 (insert (format "211 %d Article retrieved.\n"
7289                                 (cdr gnus-article-current)))
7290                 (insert head)
7291                 (insert ".\n")
7292                 (let ((nntp-server-buffer (current-buffer)))
7293                   (setq header (car (gnus-get-newsgroup-headers
7294                                      (save-excursion
7295                                        (set-buffer gnus-summary-buffer)
7296                                        gnus-newsgroup-dependencies)
7297                                      t))))
7298                 (save-excursion
7299                   (set-buffer gnus-summary-buffer)
7300                   (gnus-data-set-header
7301                    (gnus-data-find (cdr gnus-article-current))
7302                    header)
7303                   (gnus-summary-update-article-line
7304                    (cdr gnus-article-current) header))))))
7305       ;; Update threads.
7306       (set-buffer (or buffer gnus-summary-buffer))
7307       (gnus-summary-update-article (cdr gnus-article-current)))
7308     ;; Prettify the article buffer again.
7309     (unless no-highlight
7310       (save-excursion
7311         (set-buffer gnus-article-buffer)
7312         (gnus-run-hooks 'gnus-article-display-hook)
7313         (set-buffer gnus-original-article-buffer)
7314         (gnus-request-article
7315          (cdr gnus-article-current)
7316          (car gnus-article-current) (current-buffer))))
7317     ;; Prettify the summary buffer line.
7318     (when (gnus-visual-p 'summary-highlight 'highlight)
7319       (gnus-run-hooks 'gnus-visual-mark-article-hook))))
7320
7321 (defun gnus-summary-edit-wash (key)
7322   "Perform editing command KEY in the article buffer."
7323   (interactive
7324    (list
7325     (progn
7326       (message "%s" (concat (this-command-keys) "- "))
7327       (read-char))))
7328   (message "")
7329   (gnus-summary-edit-article)
7330   (execute-kbd-macro (concat (this-command-keys) key))
7331   (gnus-article-edit-done))
7332
7333 ;;; Respooling
7334
7335 (defun gnus-summary-respool-query (&optional silent)
7336   "Query where the respool algorithm would put this article."
7337   (interactive)
7338   (let (gnus-mark-article-hook)
7339     (gnus-summary-select-article)
7340     (save-excursion
7341       (set-buffer gnus-original-article-buffer)
7342       (save-restriction
7343         (message-narrow-to-head)
7344         (let ((groups (nnmail-article-group 'identity)))
7345           (unless silent
7346             (if groups
7347                 (message "This message would go to %s"
7348                          (mapconcat 'car groups ", "))
7349               (message "This message would go to no groups"))
7350             groups))))))
7351
7352 ;; Summary marking commands.
7353
7354 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7355   "Mark articles which has the same subject as read, and then select the next.
7356 If UNMARK is positive, remove any kind of mark.
7357 If UNMARK is negative, tick articles."
7358   (interactive "P")
7359   (when unmark
7360     (setq unmark (prefix-numeric-value unmark)))
7361   (let ((count
7362          (gnus-summary-mark-same-subject
7363           (gnus-summary-article-subject) unmark)))
7364     ;; Select next unread article.  If auto-select-same mode, should
7365     ;; select the first unread article.
7366     (gnus-summary-next-article t (and gnus-auto-select-same
7367                                       (gnus-summary-article-subject)))
7368     (gnus-message 7 "%d article%s marked as %s"
7369                   count (if (= count 1) " is" "s are")
7370                   (if unmark "unread" "read"))))
7371
7372 (defun gnus-summary-kill-same-subject (&optional unmark)
7373   "Mark articles which has the same subject as read.
7374 If UNMARK is positive, remove any kind of mark.
7375 If UNMARK is negative, tick articles."
7376   (interactive "P")
7377   (when unmark
7378     (setq unmark (prefix-numeric-value unmark)))
7379   (let ((count
7380          (gnus-summary-mark-same-subject
7381           (gnus-summary-article-subject) unmark)))
7382     ;; If marked as read, go to next unread subject.
7383     (when (null unmark)
7384       ;; Go to next unread subject.
7385       (gnus-summary-next-subject 1 t))
7386     (gnus-message 7 "%d articles are marked as %s"
7387                   count (if unmark "unread" "read"))))
7388
7389 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7390   "Mark articles with same SUBJECT as read, and return marked number.
7391 If optional argument UNMARK is positive, remove any kinds of marks.
7392 If optional argument UNMARK is negative, mark articles as unread instead."
7393   (let ((count 1))
7394     (save-excursion
7395       (cond
7396        ((null unmark)                   ; Mark as read.
7397         (while (and
7398                 (progn
7399                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7400                   (gnus-summary-show-thread) t)
7401                 (gnus-summary-find-subject subject))
7402           (setq count (1+ count))))
7403        ((> unmark 0)                    ; Tick.
7404         (while (and
7405                 (progn
7406                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7407                   (gnus-summary-show-thread) t)
7408                 (gnus-summary-find-subject subject))
7409           (setq count (1+ count))))
7410        (t                               ; Mark as unread.
7411         (while (and
7412                 (progn
7413                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7414                   (gnus-summary-show-thread) t)
7415                 (gnus-summary-find-subject subject))
7416           (setq count (1+ count)))))
7417       (gnus-set-mode-line 'summary)
7418       ;; Return the number of marked articles.
7419       count)))
7420
7421 (defun gnus-summary-mark-as-processable (n &optional unmark)
7422   "Set the process mark on the next N articles.
7423 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7424 the process mark instead.  The difference between N and the actual
7425 number of articles marked is returned."
7426   (interactive "p")
7427   (let ((backward (< n 0))
7428         (n (abs n)))
7429     (while (and
7430             (> n 0)
7431             (if unmark
7432                 (gnus-summary-remove-process-mark
7433                  (gnus-summary-article-number))
7434               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7435             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7436       (setq n (1- n)))
7437     (when (/= 0 n)
7438       (gnus-message 7 "No more articles"))
7439     (gnus-summary-recenter)
7440     (gnus-summary-position-point)
7441     n))
7442
7443 (defun gnus-summary-unmark-as-processable (n)
7444   "Remove the process mark from the next N articles.
7445 If N is negative, unmark backward instead.  The difference between N and
7446 the actual number of articles unmarked is returned."
7447   (interactive "p")
7448   (gnus-summary-mark-as-processable n t))
7449
7450 (defun gnus-summary-unmark-all-processable ()
7451   "Remove the process mark from all articles."
7452   (interactive)
7453   (save-excursion
7454     (while gnus-newsgroup-processable
7455       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7456   (gnus-summary-position-point))
7457
7458 (defun gnus-summary-mark-as-expirable (n)
7459   "Mark N articles forward as expirable.
7460 If N is negative, mark backward instead.  The difference between N and
7461 the actual number of articles marked is returned."
7462   (interactive "p")
7463   (gnus-summary-mark-forward n gnus-expirable-mark))
7464
7465 (defun gnus-summary-mark-article-as-replied (article)
7466   "Mark ARTICLE replied and update the summary line."
7467   (push article gnus-newsgroup-replied)
7468   (let ((buffer-read-only nil))
7469     (when (gnus-summary-goto-subject article)
7470       (gnus-summary-update-secondary-mark article))))
7471
7472 (defun gnus-summary-set-bookmark (article)
7473   "Set a bookmark in current article."
7474   (interactive (list (gnus-summary-article-number)))
7475   (when (or (not (get-buffer gnus-article-buffer))
7476             (not gnus-current-article)
7477             (not gnus-article-current)
7478             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7479     (error "No current article selected"))
7480   ;; Remove old bookmark, if one exists.
7481   (let ((old (assq article gnus-newsgroup-bookmarks)))
7482     (when old
7483       (setq gnus-newsgroup-bookmarks
7484             (delq old gnus-newsgroup-bookmarks))))
7485   ;; Set the new bookmark, which is on the form
7486   ;; (article-number . line-number-in-body).
7487   (push
7488    (cons article
7489          (save-excursion
7490            (set-buffer gnus-article-buffer)
7491            (count-lines
7492             (min (point)
7493                  (save-excursion
7494                    (goto-char (point-min))
7495                    (search-forward "\n\n" nil t)
7496                    (point)))
7497             (point))))
7498    gnus-newsgroup-bookmarks)
7499   (gnus-message 6 "A bookmark has been added to the current article."))
7500
7501 (defun gnus-summary-remove-bookmark (article)
7502   "Remove the bookmark from the current article."
7503   (interactive (list (gnus-summary-article-number)))
7504   ;; Remove old bookmark, if one exists.
7505   (let ((old (assq article gnus-newsgroup-bookmarks)))
7506     (if old
7507         (progn
7508           (setq gnus-newsgroup-bookmarks
7509                 (delq old gnus-newsgroup-bookmarks))
7510           (gnus-message 6 "Removed bookmark."))
7511       (gnus-message 6 "No bookmark in current article."))))
7512
7513 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7514 (defun gnus-summary-mark-as-dormant (n)
7515   "Mark N articles forward as dormant.
7516 If N is negative, mark backward instead.  The difference between N and
7517 the actual number of articles marked is returned."
7518   (interactive "p")
7519   (gnus-summary-mark-forward n gnus-dormant-mark))
7520
7521 (defun gnus-summary-set-process-mark (article)
7522   "Set the process mark on ARTICLE and update the summary line."
7523   (setq gnus-newsgroup-processable
7524         (cons article
7525               (delq article gnus-newsgroup-processable)))
7526   (when (gnus-summary-goto-subject article)
7527     (gnus-summary-show-thread)
7528     (gnus-summary-update-secondary-mark article)))
7529
7530 (defun gnus-summary-remove-process-mark (article)
7531   "Remove the process mark from ARTICLE and update the summary line."
7532   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7533   (when (gnus-summary-goto-subject article)
7534     (gnus-summary-show-thread)
7535     (gnus-summary-update-secondary-mark article)))
7536
7537 (defun gnus-summary-set-saved-mark (article)
7538   "Set the process mark on ARTICLE and update the summary line."
7539   (push article gnus-newsgroup-saved)
7540   (when (gnus-summary-goto-subject article)
7541     (gnus-summary-update-secondary-mark article)))
7542
7543 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7544   "Mark N articles as read forwards.
7545 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7546 The difference between N and the actual number of articles marked is
7547 returned."
7548   (interactive "p")
7549   (let ((backward (< n 0))
7550         (gnus-summary-goto-unread
7551          (and gnus-summary-goto-unread
7552               (not (eq gnus-summary-goto-unread 'never))
7553               (not (memq mark (list gnus-unread-mark
7554                                     gnus-ticked-mark gnus-dormant-mark)))))
7555         (n (abs n))
7556         (mark (or mark gnus-del-mark)))
7557     (while (and (> n 0)
7558                 (gnus-summary-mark-article nil mark no-expire)
7559                 (zerop (gnus-summary-next-subject
7560                         (if backward -1 1)
7561                         (and gnus-summary-goto-unread
7562                              (not (eq gnus-summary-goto-unread 'never)))
7563                         t)))
7564       (setq n (1- n)))
7565     (when (/= 0 n)
7566       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7567     (gnus-summary-recenter)
7568     (gnus-summary-position-point)
7569     (gnus-set-mode-line 'summary)
7570     n))
7571
7572 (defun gnus-summary-mark-article-as-read (mark)
7573   "Mark the current article quickly as read with MARK."
7574   (let ((article (gnus-summary-article-number)))
7575     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7576     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7577     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7578     (push (cons article mark) gnus-newsgroup-reads)
7579     ;; Possibly remove from cache, if that is used.
7580     (when gnus-use-cache
7581       (gnus-cache-enter-remove-article article))
7582     ;; Allow the backend to change the mark.
7583     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7584     ;; Check for auto-expiry.
7585     (when (and gnus-newsgroup-auto-expire
7586                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
7587                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
7588                    (= mark gnus-ancient-mark)
7589                    (= mark gnus-read-mark) (= mark gnus-souped-mark)
7590                    (= mark gnus-duplicate-mark)))
7591       (setq mark gnus-expirable-mark)
7592       (push article gnus-newsgroup-expirable))
7593     ;; Set the mark in the buffer.
7594     (gnus-summary-update-mark mark 'unread)
7595     t))
7596
7597 (defun gnus-summary-mark-article-as-unread (mark)
7598   "Mark the current article quickly as unread with MARK."
7599   (let* ((article (gnus-summary-article-number))
7600          (old-mark (gnus-summary-article-mark article)))
7601     (if (eq mark old-mark)
7602         t
7603       (if (<= article 0)
7604           (progn
7605             (gnus-error 1 "Can't mark negative article numbers")
7606             nil)
7607         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7608         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7609         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7610         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7611         (cond ((= mark gnus-ticked-mark)
7612                (push article gnus-newsgroup-marked))
7613               ((= mark gnus-dormant-mark)
7614                (push article gnus-newsgroup-dormant))
7615               (t
7616                (push article gnus-newsgroup-unreads)))
7617         (setq gnus-newsgroup-reads
7618               (delq (assq article gnus-newsgroup-reads)
7619                     gnus-newsgroup-reads))
7620
7621         ;; See whether the article is to be put in the cache.
7622         (and gnus-use-cache
7623              (vectorp (gnus-summary-article-header article))
7624              (save-excursion
7625                (gnus-cache-possibly-enter-article
7626                 gnus-newsgroup-name article
7627                 (gnus-summary-article-header article)
7628                 (= mark gnus-ticked-mark)
7629                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7630
7631         ;; Fix the mark.
7632         (gnus-summary-update-mark mark 'unread)
7633         t))))
7634
7635 (defun gnus-summary-mark-article (&optional article mark no-expire)
7636   "Mark ARTICLE with MARK.  MARK can be any character.
7637 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7638 `??' (dormant) and `?E' (expirable).
7639 If MARK is nil, then the default character `?D' is used.
7640 If ARTICLE is nil, then the article on the current line will be
7641 marked."
7642   ;; The mark might be a string.
7643   (when (stringp mark)
7644     (setq mark (aref mark 0)))
7645   ;; If no mark is given, then we check auto-expiring.
7646   (and (not no-expire)
7647        gnus-newsgroup-auto-expire
7648        (or (not mark)
7649            (and (gnus-characterp mark)
7650                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
7651                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
7652                     (= mark gnus-read-mark) (= mark gnus-souped-mark)
7653                     (= mark gnus-duplicate-mark))))
7654        (setq mark gnus-expirable-mark))
7655   (let* ((mark (or mark gnus-del-mark))
7656          (article (or article (gnus-summary-article-number)))
7657          (old-mark (gnus-summary-article-mark article)))
7658     (if (eq mark old-mark)
7659         t
7660       (unless article
7661         (error "No article on current line"))
7662       (if (not (if (or (= mark gnus-unread-mark)
7663                        (= mark gnus-ticked-mark)
7664                        (= mark gnus-dormant-mark))
7665                    (gnus-mark-article-as-unread article mark)
7666                  (gnus-mark-article-as-read article mark)))
7667           t
7668         ;; See whether the article is to be put in the cache.
7669         (and gnus-use-cache
7670              (not (= mark gnus-canceled-mark))
7671              (vectorp (gnus-summary-article-header article))
7672              (save-excursion
7673                (gnus-cache-possibly-enter-article
7674                 gnus-newsgroup-name article
7675                 (gnus-summary-article-header article)
7676                 (= mark gnus-ticked-mark)
7677                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7678
7679         (when (gnus-summary-goto-subject article nil t)
7680           (let ((buffer-read-only nil))
7681             (gnus-summary-show-thread)
7682             ;; Fix the mark.
7683             (gnus-summary-update-mark mark 'unread)
7684             t))))))
7685
7686 (defun gnus-summary-update-secondary-mark (article)
7687   "Update the secondary (read, process, cache) mark."
7688   (gnus-summary-update-mark
7689    (cond ((memq article gnus-newsgroup-processable)
7690           gnus-process-mark)
7691          ((memq article gnus-newsgroup-cached)
7692           gnus-cached-mark)
7693          ((memq article gnus-newsgroup-replied)
7694           gnus-replied-mark)
7695          ((memq article gnus-newsgroup-saved)
7696           gnus-saved-mark)
7697          (t gnus-unread-mark))
7698    'replied)
7699   (when (gnus-visual-p 'summary-highlight 'highlight)
7700     (gnus-run-hooks 'gnus-summary-update-hook))
7701   t)
7702
7703 (defun gnus-summary-update-mark (mark type)
7704   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7705         (buffer-read-only nil))
7706     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7707     (when (looking-at "\r")
7708       (incf forward))
7709     (when (and forward
7710                (<= (+ forward (point)) (point-max)))
7711       ;; Go to the right position on the line.
7712       (goto-char (+ forward (point)))
7713       ;; Replace the old mark with the new mark.
7714       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
7715       ;; Optionally update the marks by some user rule.
7716       (when (eq type 'unread)
7717         (gnus-data-set-mark
7718          (gnus-data-find (gnus-summary-article-number)) mark)
7719         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
7720
7721 (defun gnus-mark-article-as-read (article &optional mark)
7722   "Enter ARTICLE in the pertinent lists and remove it from others."
7723   ;; Make the article expirable.
7724   (let ((mark (or mark gnus-del-mark)))
7725     (if (= mark gnus-expirable-mark)
7726         (push article gnus-newsgroup-expirable)
7727       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7728     ;; Remove from unread and marked lists.
7729     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7730     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7731     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7732     (push (cons article mark) gnus-newsgroup-reads)
7733     ;; Possibly remove from cache, if that is used.
7734     (when gnus-use-cache
7735       (gnus-cache-enter-remove-article article))
7736     t))
7737
7738 (defun gnus-mark-article-as-unread (article &optional mark)
7739   "Enter ARTICLE in the pertinent lists and remove it from others."
7740   (let ((mark (or mark gnus-ticked-mark)))
7741     (if (<= article 0)
7742         (progn
7743           (gnus-error 1 "Can't mark negative article numbers")
7744           nil)
7745       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
7746             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
7747             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
7748             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7749
7750       ;; Unsuppress duplicates?
7751       (when gnus-suppress-duplicates
7752         (gnus-dup-unsuppress-article article))
7753
7754       (cond ((= mark gnus-ticked-mark)
7755              (push article gnus-newsgroup-marked))
7756             ((= mark gnus-dormant-mark)
7757              (push article gnus-newsgroup-dormant))
7758             (t
7759              (push article gnus-newsgroup-unreads)))
7760       (setq gnus-newsgroup-reads
7761             (delq (assq article gnus-newsgroup-reads)
7762                   gnus-newsgroup-reads))
7763       t)))
7764
7765 (defalias 'gnus-summary-mark-as-unread-forward
7766   'gnus-summary-tick-article-forward)
7767 (make-obsolete 'gnus-summary-mark-as-unread-forward
7768                'gnus-summary-tick-article-forward)
7769 (defun gnus-summary-tick-article-forward (n)
7770   "Tick N articles forwards.
7771 If N is negative, tick backwards instead.
7772 The difference between N and the number of articles ticked is returned."
7773   (interactive "p")
7774   (gnus-summary-mark-forward n gnus-ticked-mark))
7775
7776 (defalias 'gnus-summary-mark-as-unread-backward
7777   'gnus-summary-tick-article-backward)
7778 (make-obsolete 'gnus-summary-mark-as-unread-backward
7779                'gnus-summary-tick-article-backward)
7780 (defun gnus-summary-tick-article-backward (n)
7781   "Tick N articles backwards.
7782 The difference between N and the number of articles ticked is returned."
7783   (interactive "p")
7784   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
7785
7786 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7787 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7788 (defun gnus-summary-tick-article (&optional article clear-mark)
7789   "Mark current article as unread.
7790 Optional 1st argument ARTICLE specifies article number to be marked as unread.
7791 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
7792   (interactive)
7793   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
7794                                        gnus-ticked-mark)))
7795
7796 (defun gnus-summary-mark-as-read-forward (n)
7797   "Mark N articles as read forwards.
7798 If N is negative, mark backwards instead.
7799 The difference between N and the actual number of articles marked is
7800 returned."
7801   (interactive "p")
7802   (gnus-summary-mark-forward n gnus-del-mark t))
7803
7804 (defun gnus-summary-mark-as-read-backward (n)
7805   "Mark the N articles as read backwards.
7806 The difference between N and the actual number of articles marked is
7807 returned."
7808   (interactive "p")
7809   (gnus-summary-mark-forward (- n) gnus-del-mark t))
7810
7811 (defun gnus-summary-mark-as-read (&optional article mark)
7812   "Mark current article as read.
7813 ARTICLE specifies the article to be marked as read.
7814 MARK specifies a string to be inserted at the beginning of the line."
7815   (gnus-summary-mark-article article mark))
7816
7817 (defun gnus-summary-clear-mark-forward (n)
7818   "Clear marks from N articles forward.
7819 If N is negative, clear backward instead.
7820 The difference between N and the number of marks cleared is returned."
7821   (interactive "p")
7822   (gnus-summary-mark-forward n gnus-unread-mark))
7823
7824 (defun gnus-summary-clear-mark-backward (n)
7825   "Clear marks from N articles backward.
7826 The difference between N and the number of marks cleared is returned."
7827   (interactive "p")
7828   (gnus-summary-mark-forward (- n) gnus-unread-mark))
7829
7830 (defun gnus-summary-mark-unread-as-read ()
7831   "Intended to be used by `gnus-summary-mark-article-hook'."
7832   (when (memq gnus-current-article gnus-newsgroup-unreads)
7833     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
7834
7835 (defun gnus-summary-mark-read-and-unread-as-read ()
7836   "Intended to be used by `gnus-summary-mark-article-hook'."
7837   (let ((mark (gnus-summary-article-mark)))
7838     (when (or (gnus-unread-mark-p mark)
7839               (gnus-read-mark-p mark))
7840       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
7841
7842 (defun gnus-summary-mark-region-as-read (point mark all)
7843   "Mark all unread articles between point and mark as read.
7844 If given a prefix, mark all articles between point and mark as read,
7845 even ticked and dormant ones."
7846   (interactive "r\nP")
7847   (save-excursion
7848     (let (article)
7849       (goto-char point)
7850       (beginning-of-line)
7851       (while (and
7852               (< (point) mark)
7853               (progn
7854                 (when (or all
7855                           (memq (setq article (gnus-summary-article-number))
7856                                 gnus-newsgroup-unreads))
7857                   (gnus-summary-mark-article article gnus-del-mark))
7858                 t)
7859               (gnus-summary-find-next))))))
7860
7861 (defun gnus-summary-mark-below (score mark)
7862   "Mark articles with score less than SCORE with MARK."
7863   (interactive "P\ncMark: ")
7864   (setq score (if score
7865                   (prefix-numeric-value score)
7866                 (or gnus-summary-default-score 0)))
7867   (save-excursion
7868     (set-buffer gnus-summary-buffer)
7869     (goto-char (point-min))
7870     (while
7871         (progn
7872           (and (< (gnus-summary-article-score) score)
7873                (gnus-summary-mark-article nil mark))
7874           (gnus-summary-find-next)))))
7875
7876 (defun gnus-summary-kill-below (&optional score)
7877   "Mark articles with score below SCORE as read."
7878   (interactive "P")
7879   (gnus-summary-mark-below score gnus-killed-mark))
7880
7881 (defun gnus-summary-clear-above (&optional score)
7882   "Clear all marks from articles with score above SCORE."
7883   (interactive "P")
7884   (gnus-summary-mark-above score gnus-unread-mark))
7885
7886 (defun gnus-summary-tick-above (&optional score)
7887   "Tick all articles with score above SCORE."
7888   (interactive "P")
7889   (gnus-summary-mark-above score gnus-ticked-mark))
7890
7891 (defun gnus-summary-mark-above (score mark)
7892   "Mark articles with score over SCORE with MARK."
7893   (interactive "P\ncMark: ")
7894   (setq score (if score
7895                   (prefix-numeric-value score)
7896                 (or gnus-summary-default-score 0)))
7897   (save-excursion
7898     (set-buffer gnus-summary-buffer)
7899     (goto-char (point-min))
7900     (while (and (progn
7901                   (when (> (gnus-summary-article-score) score)
7902                     (gnus-summary-mark-article nil mark))
7903                   t)
7904                 (gnus-summary-find-next)))))
7905
7906 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7907 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
7908 (defun gnus-summary-limit-include-expunged (&optional no-error)
7909   "Display all the hidden articles that were expunged for low scores."
7910   (interactive)
7911   (let ((buffer-read-only nil))
7912     (let ((scored gnus-newsgroup-scored)
7913           headers h)
7914       (while scored
7915         (unless (gnus-summary-goto-subject (caar scored))
7916           (and (setq h (gnus-summary-article-header (caar scored)))
7917                (< (cdar scored) gnus-summary-expunge-below)
7918                (push h headers)))
7919         (setq scored (cdr scored)))
7920       (if (not headers)
7921           (when (not no-error)
7922             (error "No expunged articles hidden"))
7923         (goto-char (point-min))
7924         (gnus-summary-prepare-unthreaded (nreverse headers))
7925         (goto-char (point-min))
7926         (gnus-summary-position-point)
7927         t))))
7928
7929 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
7930   "Mark all unread articles in this newsgroup as read.
7931 If prefix argument ALL is non-nil, ticked and dormant articles will
7932 also be marked as read.
7933 If QUIETLY is non-nil, no questions will be asked.
7934 If TO-HERE is non-nil, it should be a point in the buffer.  All
7935 articles before this point will be marked as read.
7936 Note that this function will only catch up the unread article
7937 in the current summary buffer limitation.
7938 The number of articles marked as read is returned."
7939   (interactive "P")
7940   (prog1
7941       (save-excursion
7942         (when (or quietly
7943                   (not gnus-interactive-catchup) ;Without confirmation?
7944                   gnus-expert-user
7945                   (gnus-y-or-n-p
7946                    (if all
7947                        "Mark absolutely all articles as read? "
7948                      "Mark all unread articles as read? ")))
7949           (if (and not-mark
7950                    (not gnus-newsgroup-adaptive)
7951                    (not gnus-newsgroup-auto-expire)
7952                    (not gnus-suppress-duplicates)
7953                    (or (not gnus-use-cache)
7954                        (eq gnus-use-cache 'passive)))
7955               (progn
7956                 (when all
7957                   (setq gnus-newsgroup-marked nil
7958                         gnus-newsgroup-dormant nil))
7959                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
7960             ;; We actually mark all articles as canceled, which we
7961             ;; have to do when using auto-expiry or adaptive scoring.
7962             (gnus-summary-show-all-threads)
7963             (when (gnus-summary-first-subject (not all) t)
7964               (while (and
7965                       (if to-here (< (point) to-here) t)
7966                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
7967                       (gnus-summary-find-next (not all) nil nil t))))
7968             (gnus-set-mode-line 'summary))
7969           t))
7970     (gnus-summary-position-point)))
7971
7972 (defun gnus-summary-catchup-to-here (&optional all)
7973   "Mark all unticked articles before the current one as read.
7974 If ALL is non-nil, also mark ticked and dormant articles as read."
7975   (interactive "P")
7976   (save-excursion
7977     (gnus-save-hidden-threads
7978       (let ((beg (point)))
7979         ;; We check that there are unread articles.
7980         (when (or all (gnus-summary-find-prev))
7981           (gnus-summary-catchup all t beg)))))
7982   (gnus-summary-position-point))
7983
7984 (defun gnus-summary-catchup-all (&optional quietly)
7985   "Mark all articles in this newsgroup as read."
7986   (interactive "P")
7987   (gnus-summary-catchup t quietly))
7988
7989 (defun gnus-summary-catchup-and-exit (&optional all quietly)
7990   "Mark all articles not marked as unread in this newsgroup as read, then exit.
7991 If prefix argument ALL is non-nil, all articles are marked as read."
7992   (interactive "P")
7993   (when (gnus-summary-catchup all quietly nil 'fast)
7994     ;; Select next newsgroup or exit.
7995     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
7996              (eq gnus-auto-select-next 'quietly))
7997         (gnus-summary-next-group nil)
7998       (gnus-summary-exit))))
7999
8000 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8001   "Mark all articles in this newsgroup as read, and then exit."
8002   (interactive "P")
8003   (gnus-summary-catchup-and-exit t quietly))
8004
8005 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8006 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8007   "Mark all articles in this group as read and select the next group.
8008 If given a prefix, mark all articles, unread as well as ticked, as
8009 read."
8010   (interactive "P")
8011   (save-excursion
8012     (gnus-summary-catchup all))
8013   (gnus-summary-next-article t nil nil t))
8014
8015 ;; Thread-based commands.
8016
8017 (defun gnus-summary-articles-in-thread (&optional article)
8018   "Return a list of all articles in the current thread.
8019 If ARTICLE is non-nil, return all articles in the thread that starts
8020 with that article."
8021   (let* ((article (or article (gnus-summary-article-number)))
8022          (data (gnus-data-find-list article))
8023          (top-level (gnus-data-level (car data)))
8024          (top-subject
8025           (cond ((null gnus-thread-operation-ignore-subject)
8026                  (gnus-simplify-subject-re
8027                   (mail-header-subject (gnus-data-header (car data)))))
8028                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8029                  (gnus-simplify-subject-fuzzy
8030                   (mail-header-subject (gnus-data-header (car data)))))
8031                 (t nil)))
8032          (end-point (save-excursion
8033                       (if (gnus-summary-go-to-next-thread)
8034                           (point) (point-max))))
8035          articles)
8036     (while (and data
8037                 (< (gnus-data-pos (car data)) end-point))
8038       (when (or (not top-subject)
8039                 (string= top-subject
8040                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8041                              (gnus-simplify-subject-fuzzy
8042                               (mail-header-subject
8043                                (gnus-data-header (car data))))
8044                            (gnus-simplify-subject-re
8045                             (mail-header-subject
8046                              (gnus-data-header (car data)))))))
8047         (push (gnus-data-number (car data)) articles))
8048       (unless (and (setq data (cdr data))
8049                    (> (gnus-data-level (car data)) top-level))
8050         (setq data nil)))
8051     ;; Return the list of articles.
8052     (nreverse articles)))
8053
8054 (defun gnus-summary-rethread-current ()
8055   "Rethread the thread the current article is part of."
8056   (interactive)
8057   (let* ((gnus-show-threads t)
8058          (article (gnus-summary-article-number))
8059          (id (mail-header-id (gnus-summary-article-header)))
8060          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8061     (unless id
8062       (error "No article on the current line"))
8063     (gnus-rebuild-thread id)
8064     (gnus-summary-goto-subject article)))
8065
8066 (defun gnus-summary-reparent-thread ()
8067   "Make the current article child of the marked (or previous) article.
8068
8069 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8070 is non-nil or the Subject: of both articles are the same."
8071   (interactive)
8072   (unless (not (gnus-group-read-only-p))
8073     (error "The current newsgroup does not support article editing"))
8074   (unless (<= (length gnus-newsgroup-processable) 1)
8075     (error "No more than one article may be marked"))
8076   (save-window-excursion
8077     (let ((gnus-article-buffer " *reparent*")
8078           (current-article (gnus-summary-article-number))
8079           ;; First grab the marked article, otherwise one line up.
8080           (parent-article (if (not (null gnus-newsgroup-processable))
8081                               (car gnus-newsgroup-processable)
8082                             (save-excursion
8083                               (if (eq (forward-line -1) 0)
8084                                   (gnus-summary-article-number)
8085                                 (error "Beginning of summary buffer"))))))
8086       (unless (not (eq current-article parent-article))
8087         (error "An article may not be self-referential"))
8088       (let ((message-id (mail-header-id
8089                          (gnus-summary-article-header parent-article))))
8090         (unless (and message-id (not (equal message-id "")))
8091           (error "No message-id in desired parent"))
8092         ;; We don't want the article to be marked as read.
8093         (let (gnus-mark-article-hook)
8094           (gnus-summary-select-article t t nil current-article))
8095         (set-buffer gnus-original-article-buffer)
8096         (let ((buf (format "%s" (buffer-string))))
8097           (nnheader-temp-write nil
8098             (insert buf)
8099             (goto-char (point-min))
8100             (if (re-search-forward "^References: " nil t)
8101                 (progn
8102                   (re-search-forward "^[^ \t]" nil t)
8103                   (forward-line -1)
8104                   (end-of-line)
8105                   (insert " " message-id))
8106               (insert "References: " message-id "\n"))
8107             (unless (gnus-request-replace-article
8108                      current-article (car gnus-article-current)
8109                      (current-buffer))
8110               (error "Couldn't replace article"))))
8111         (set-buffer gnus-summary-buffer)
8112         (gnus-summary-unmark-all-processable)
8113         (gnus-summary-update-article current-article)
8114         (gnus-summary-rethread-current)
8115         (gnus-message 3 "Article %d is now the child of article %d"
8116                       current-article parent-article)))))
8117
8118 (defun gnus-summary-toggle-threads (&optional arg)
8119   "Toggle showing conversation threads.
8120 If ARG is positive number, turn showing conversation threads on."
8121   (interactive "P")
8122   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8123     (setq gnus-show-threads
8124           (if (null arg) (not gnus-show-threads)
8125             (> (prefix-numeric-value arg) 0)))
8126     (gnus-summary-prepare)
8127     (gnus-summary-goto-subject current)
8128     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8129     (gnus-summary-position-point)))
8130
8131 (defun gnus-summary-show-all-threads ()
8132   "Show all threads."
8133   (interactive)
8134   (save-excursion
8135     (let ((buffer-read-only nil))
8136       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8137   (gnus-summary-position-point))
8138
8139 (defun gnus-summary-show-thread ()
8140   "Show thread subtrees.
8141 Returns nil if no thread was there to be shown."
8142   (interactive)
8143   (let ((buffer-read-only nil)
8144         (orig (point))
8145         ;; first goto end then to beg, to have point at beg after let
8146         (end (progn (end-of-line) (point)))
8147         (beg (progn (beginning-of-line) (point))))
8148     (prog1
8149         ;; Any hidden lines here?
8150         (search-forward "\r" end t)
8151       (subst-char-in-region beg end ?\^M ?\n t)
8152       (goto-char orig)
8153       (gnus-summary-position-point))))
8154
8155 (defun gnus-summary-hide-all-threads ()
8156   "Hide all thread subtrees."
8157   (interactive)
8158   (save-excursion
8159     (goto-char (point-min))
8160     (gnus-summary-hide-thread)
8161     (while (zerop (gnus-summary-next-thread 1 t))
8162       (gnus-summary-hide-thread)))
8163   (gnus-summary-position-point))
8164
8165 (defun gnus-summary-hide-thread ()
8166   "Hide thread subtrees.
8167 Returns nil if no threads were there to be hidden."
8168   (interactive)
8169   (let ((buffer-read-only nil)
8170         (start (point))
8171         (article (gnus-summary-article-number)))
8172     (goto-char start)
8173     ;; Go forward until either the buffer ends or the subthread
8174     ;; ends.
8175     (when (and (not (eobp))
8176                (or (zerop (gnus-summary-next-thread 1 t))
8177                    (goto-char (point-max))))
8178       (prog1
8179           (if (and (> (point) start)
8180                    (search-backward "\n" start t))
8181               (progn
8182                 (subst-char-in-region start (point) ?\n ?\^M)
8183                 (gnus-summary-goto-subject article))
8184             (goto-char start)
8185             nil)
8186         ;;(gnus-summary-position-point)
8187         ))))
8188
8189 (defun gnus-summary-go-to-next-thread (&optional previous)
8190   "Go to the same level (or less) next thread.
8191 If PREVIOUS is non-nil, go to previous thread instead.
8192 Return the article number moved to, or nil if moving was impossible."
8193   (let ((level (gnus-summary-thread-level))
8194         (way (if previous -1 1))
8195         (beg (point)))
8196     (forward-line way)
8197     (while (and (not (eobp))
8198                 (< level (gnus-summary-thread-level)))
8199       (forward-line way))
8200     (if (eobp)
8201         (progn
8202           (goto-char beg)
8203           nil)
8204       (setq beg (point))
8205       (prog1
8206           (gnus-summary-article-number)
8207         (goto-char beg)))))
8208
8209 (defun gnus-summary-next-thread (n &optional silent)
8210   "Go to the same level next N'th thread.
8211 If N is negative, search backward instead.
8212 Returns the difference between N and the number of skips actually
8213 done.
8214
8215 If SILENT, don't output messages."
8216   (interactive "p")
8217   (let ((backward (< n 0))
8218         (n (abs n)))
8219     (while (and (> n 0)
8220                 (gnus-summary-go-to-next-thread backward))
8221       (decf n))
8222     (unless silent
8223       (gnus-summary-position-point))
8224     (when (and (not silent) (/= 0 n))
8225       (gnus-message 7 "No more threads"))
8226     n))
8227
8228 (defun gnus-summary-prev-thread (n)
8229   "Go to the same level previous N'th thread.
8230 Returns the difference between N and the number of skips actually
8231 done."
8232   (interactive "p")
8233   (gnus-summary-next-thread (- n)))
8234
8235 (defun gnus-summary-go-down-thread ()
8236   "Go down one level in the current thread."
8237   (let ((children (gnus-summary-article-children)))
8238     (when children
8239       (gnus-summary-goto-subject (car children)))))
8240
8241 (defun gnus-summary-go-up-thread ()
8242   "Go up one level in the current thread."
8243   (let ((parent (gnus-summary-article-parent)))
8244     (when parent
8245       (gnus-summary-goto-subject parent))))
8246
8247 (defun gnus-summary-down-thread (n)
8248   "Go down thread N steps.
8249 If N is negative, go up instead.
8250 Returns the difference between N and how many steps down that were
8251 taken."
8252   (interactive "p")
8253   (let ((up (< n 0))
8254         (n (abs n)))
8255     (while (and (> n 0)
8256                 (if up (gnus-summary-go-up-thread)
8257                   (gnus-summary-go-down-thread)))
8258       (setq n (1- n)))
8259     (gnus-summary-position-point)
8260     (when (/= 0 n)
8261       (gnus-message 7 "Can't go further"))
8262     n))
8263
8264 (defun gnus-summary-up-thread (n)
8265   "Go up thread N steps.
8266 If N is negative, go up instead.
8267 Returns the difference between N and how many steps down that were
8268 taken."
8269   (interactive "p")
8270   (gnus-summary-down-thread (- n)))
8271
8272 (defun gnus-summary-top-thread ()
8273   "Go to the top of the thread."
8274   (interactive)
8275   (while (gnus-summary-go-up-thread))
8276   (gnus-summary-article-number))
8277
8278 (defun gnus-summary-kill-thread (&optional unmark)
8279   "Mark articles under current thread as read.
8280 If the prefix argument is positive, remove any kinds of marks.
8281 If the prefix argument is negative, tick articles instead."
8282   (interactive "P")
8283   (when unmark
8284     (setq unmark (prefix-numeric-value unmark)))
8285   (let ((articles (gnus-summary-articles-in-thread)))
8286     (save-excursion
8287       ;; Expand the thread.
8288       (gnus-summary-show-thread)
8289       ;; Mark all the articles.
8290       (while articles
8291         (gnus-summary-goto-subject (car articles))
8292         (cond ((null unmark)
8293                (gnus-summary-mark-article-as-read gnus-killed-mark))
8294               ((> unmark 0)
8295                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8296               (t
8297                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8298         (setq articles (cdr articles))))
8299     ;; Hide killed subtrees.
8300     (and (null unmark)
8301          gnus-thread-hide-killed
8302          (gnus-summary-hide-thread))
8303     ;; If marked as read, go to next unread subject.
8304     (when (null unmark)
8305       ;; Go to next unread subject.
8306       (gnus-summary-next-subject 1 t)))
8307   (gnus-set-mode-line 'summary))
8308
8309 ;; Summary sorting commands
8310
8311 (defun gnus-summary-sort-by-number (&optional reverse)
8312   "Sort the summary buffer by article number.
8313 Argument REVERSE means reverse order."
8314   (interactive "P")
8315   (gnus-summary-sort 'number reverse))
8316
8317 (defun gnus-summary-sort-by-author (&optional reverse)
8318   "Sort the summary buffer by author name alphabetically.
8319 If case-fold-search is non-nil, case of letters is ignored.
8320 Argument REVERSE means reverse order."
8321   (interactive "P")
8322   (gnus-summary-sort 'author reverse))
8323
8324 (defun gnus-summary-sort-by-subject (&optional reverse)
8325   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8326 If case-fold-search is non-nil, case of letters is ignored.
8327 Argument REVERSE means reverse order."
8328   (interactive "P")
8329   (gnus-summary-sort 'subject reverse))
8330
8331 (defun gnus-summary-sort-by-date (&optional reverse)
8332   "Sort the summary buffer by date.
8333 Argument REVERSE means reverse order."
8334   (interactive "P")
8335   (gnus-summary-sort 'date reverse))
8336
8337 (defun gnus-summary-sort-by-score (&optional reverse)
8338   "Sort the summary buffer by score.
8339 Argument REVERSE means reverse order."
8340   (interactive "P")
8341   (gnus-summary-sort 'score reverse))
8342
8343 (defun gnus-summary-sort-by-lines (&optional reverse)
8344   "Sort the summary buffer by article length.
8345 Argument REVERSE means reverse order."
8346   (interactive "P")
8347   (gnus-summary-sort 'lines reverse))
8348
8349 (defun gnus-summary-sort (predicate reverse)
8350   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8351   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8352          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8353          (gnus-thread-sort-functions
8354           (list
8355            (if (not reverse)
8356                thread
8357              `(lambda (t1 t2)
8358                 (,thread t2 t1)))))
8359          (gnus-article-sort-functions
8360           (list
8361            (if (not reverse)
8362                article
8363              `(lambda (t1 t2)
8364                 (,article t2 t1)))))
8365          (buffer-read-only)
8366          (gnus-summary-prepare-hook nil))
8367     ;; We do the sorting by regenerating the threads.
8368     (gnus-summary-prepare)
8369     ;; Hide subthreads if needed.
8370     (when (and gnus-show-threads gnus-thread-hide-subtree)
8371       (gnus-summary-hide-all-threads))))
8372
8373 ;; Summary saving commands.
8374
8375 (defun gnus-summary-save-article (&optional n not-saved)
8376   "Save the current article using the default saver function.
8377 If N is a positive number, save the N next articles.
8378 If N is a negative number, save the N previous articles.
8379 If N is nil and any articles have been marked with the process mark,
8380 save those articles instead.
8381 The variable `gnus-default-article-saver' specifies the saver function."
8382   (interactive "P")
8383   (let* ((articles (gnus-summary-work-articles n))
8384          (save-buffer (save-excursion
8385                         (nnheader-set-temp-buffer " *Gnus Save*")))
8386          (num (length articles))
8387          header article file)
8388     (while articles
8389       (setq header (gnus-summary-article-header
8390                     (setq article (pop articles))))
8391       (if (not (vectorp header))
8392           ;; This is a pseudo-article.
8393           (if (assq 'name header)
8394               (gnus-copy-file (cdr (assq 'name header)))
8395             (gnus-message 1 "Article %d is unsaveable" article))
8396         ;; This is a real article.
8397         (save-window-excursion
8398           (gnus-summary-select-article t nil nil article))
8399         (save-excursion
8400           (set-buffer save-buffer)
8401           (erase-buffer)
8402           (insert-buffer-substring gnus-original-article-buffer))
8403         (setq file (gnus-article-save save-buffer file num))
8404         (gnus-summary-remove-process-mark article)
8405         (unless not-saved
8406           (gnus-summary-set-saved-mark article))))
8407     (gnus-kill-buffer save-buffer)
8408     (gnus-summary-position-point)
8409     (gnus-set-mode-line 'summary)
8410     n))
8411
8412 (defun gnus-summary-pipe-output (&optional arg)
8413   "Pipe the current article to a subprocess.
8414 If N is a positive number, pipe the N next articles.
8415 If N is a negative number, pipe the N previous articles.
8416 If N is nil and any articles have been marked with the process mark,
8417 pipe those articles instead."
8418   (interactive "P")
8419   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8420     (gnus-summary-save-article arg t))
8421   (gnus-configure-windows 'pipe))
8422
8423 (defun gnus-summary-save-article-mail (&optional arg)
8424   "Append the current article to an mail file.
8425 If N is a positive number, save the N next articles.
8426 If N is a negative number, save the N previous articles.
8427 If N is nil and any articles have been marked with the process mark,
8428 save those articles instead."
8429   (interactive "P")
8430   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8431     (gnus-summary-save-article arg)))
8432
8433 (defun gnus-summary-save-article-rmail (&optional arg)
8434   "Append the current article to an rmail file.
8435 If N is a positive number, save the N next articles.
8436 If N is a negative number, save the N previous articles.
8437 If N is nil and any articles have been marked with the process mark,
8438 save those articles instead."
8439   (interactive "P")
8440   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8441     (gnus-summary-save-article arg)))
8442
8443 (defun gnus-summary-save-article-file (&optional arg)
8444   "Append the current article to a file.
8445 If N is a positive number, save the N next articles.
8446 If N is a negative number, save the N previous articles.
8447 If N is nil and any articles have been marked with the process mark,
8448 save those articles instead."
8449   (interactive "P")
8450   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8451     (gnus-summary-save-article arg)))
8452
8453 (defun gnus-summary-write-article-file (&optional arg)
8454   "Write the current article to a file, deleting the previous file.
8455 If N is a positive number, save the N next articles.
8456 If N is a negative number, save the N previous articles.
8457 If N is nil and any articles have been marked with the process mark,
8458 save those articles instead."
8459   (interactive "P")
8460   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8461     (gnus-summary-save-article arg)))
8462
8463 (defun gnus-summary-save-article-body-file (&optional arg)
8464   "Append the current article body to a file.
8465 If N is a positive number, save the N next articles.
8466 If N is a negative number, save the N previous articles.
8467 If N is nil and any articles have been marked with the process mark,
8468 save those articles instead."
8469   (interactive "P")
8470   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8471     (gnus-summary-save-article arg)))
8472
8473 (defun gnus-summary-pipe-message (program)
8474   "Pipe the current article through PROGRAM."
8475   (interactive "sProgram: ")
8476   (gnus-summary-select-article)
8477   (let ((mail-header-separator "")
8478         (art-buf (get-buffer gnus-article-buffer)))
8479     (gnus-eval-in-buffer-window gnus-article-buffer
8480       (save-restriction
8481         (widen)
8482         (let ((start (window-start))
8483               buffer-read-only)
8484           (message-pipe-buffer-body program)
8485           (set-window-start (get-buffer-window (current-buffer)) start))))))
8486
8487 (defun gnus-get-split-value (methods)
8488   "Return a value based on the split METHODS."
8489   (let (split-name method result match)
8490     (when methods
8491       (save-excursion
8492         (set-buffer gnus-original-article-buffer)
8493         (save-restriction
8494           (nnheader-narrow-to-headers)
8495           (while methods
8496             (goto-char (point-min))
8497             (setq method (pop methods))
8498             (setq match (car method))
8499             (when (cond
8500                    ((stringp match)
8501                     ;; Regular expression.
8502                     (ignore-errors
8503                       (re-search-forward match nil t)))
8504                    ((gnus-functionp match)
8505                     ;; Function.
8506                     (save-restriction
8507                       (widen)
8508                       (setq result (funcall match gnus-newsgroup-name))))
8509                    ((consp match)
8510                     ;; Form.
8511                     (save-restriction
8512                       (widen)
8513                       (setq result (eval match)))))
8514               (setq split-name (append (cdr method) split-name))
8515               (cond ((stringp result)
8516                      (push (expand-file-name
8517                             result gnus-article-save-directory)
8518                            split-name))
8519                     ((consp result)
8520                      (setq split-name (append result split-name)))))))))
8521     split-name))
8522
8523 (defun gnus-valid-move-group-p (group)
8524   (and (boundp group)
8525        (symbol-name group)
8526        (memq 'respool
8527              (assoc (symbol-name
8528                      (car (gnus-find-method-for-group
8529                            (symbol-name group))))
8530                     gnus-valid-select-methods))))
8531
8532 (defun gnus-read-move-group-name (prompt default articles prefix)
8533   "Read a group name."
8534   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8535          (minibuffer-confirm-incomplete nil) ; XEmacs
8536          (prom
8537           (format "%s %s to:"
8538                   prompt
8539                   (if (> (length articles) 1)
8540                       (format "these %d articles" (length articles))
8541                     "this article")))
8542          (to-newsgroup
8543           (cond
8544            ((null split-name)
8545             (gnus-completing-read default prom
8546                                   gnus-active-hashtb
8547                                   'gnus-valid-move-group-p
8548                                   nil prefix
8549                                   'gnus-group-history))
8550            ((= 1 (length split-name))
8551             (gnus-completing-read (car split-name) prom
8552                                   gnus-active-hashtb
8553                                   'gnus-valid-move-group-p
8554                                   nil nil
8555                                   'gnus-group-history))
8556            (t
8557             (gnus-completing-read nil prom
8558                                   (mapcar (lambda (el) (list el))
8559                                           (nreverse split-name))
8560                                   nil nil nil
8561                                   'gnus-group-history)))))
8562     (when to-newsgroup
8563       (if (or (string= to-newsgroup "")
8564               (string= to-newsgroup prefix))
8565           (setq to-newsgroup default))
8566       (unless to-newsgroup
8567         (error "No group name entered"))
8568       (or (gnus-active to-newsgroup)
8569           (gnus-activate-group to-newsgroup)
8570           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8571                                      to-newsgroup))
8572               (or (and (gnus-request-create-group
8573                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8574                        (gnus-activate-group to-newsgroup nil nil
8575                                             (gnus-group-name-to-method
8576                                              to-newsgroup)))
8577                   (error "Couldn't create group %s" to-newsgroup)))
8578           (error "No such group: %s" to-newsgroup)))
8579     to-newsgroup))
8580
8581 ;; Summary extract commands
8582
8583 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8584   (let ((buffer-read-only nil)
8585         (article (gnus-summary-article-number))
8586         after-article b e)
8587     (unless (gnus-summary-goto-subject article)
8588       (error "No such article: %d" article))
8589     (gnus-summary-position-point)
8590     ;; If all commands are to be bunched up on one line, we collect
8591     ;; them here.
8592     (unless gnus-view-pseudos-separately
8593       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8594             files action)
8595         (while ps
8596           (setq action (cdr (assq 'action (car ps))))
8597           (setq files (list (cdr (assq 'name (car ps)))))
8598           (while (and ps (cdr ps)
8599                       (string= (or action "1")
8600                                (or (cdr (assq 'action (cadr ps))) "2")))
8601             (push (cdr (assq 'name (cadr ps))) files)
8602             (setcdr ps (cddr ps)))
8603           (when files
8604             (when (not (string-match "%s" action))
8605               (push " " files))
8606             (push " " files)
8607             (when (assq 'execute (car ps))
8608               (setcdr (assq 'execute (car ps))
8609                       (funcall (if (string-match "%s" action)
8610                                    'format 'concat)
8611                                action
8612                                (mapconcat
8613                                 (lambda (f)
8614                                   (if (equal f " ")
8615                                       f
8616                                     (gnus-quote-arg-for-sh-or-csh f)))
8617                                 files " ")))))
8618           (setq ps (cdr ps)))))
8619     (if (and gnus-view-pseudos (not not-view))
8620         (while pslist
8621           (when (assq 'execute (car pslist))
8622             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8623                                   (eq gnus-view-pseudos 'not-confirm)))
8624           (setq pslist (cdr pslist)))
8625       (save-excursion
8626         (while pslist
8627           (setq after-article (or (cdr (assq 'article (car pslist)))
8628                                   (gnus-summary-article-number)))
8629           (gnus-summary-goto-subject after-article)
8630           (forward-line 1)
8631           (setq b (point))
8632           (insert "    " (file-name-nondirectory
8633                           (cdr (assq 'name (car pslist))))
8634                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8635           (setq e (point))
8636           (forward-line -1)             ; back to `b'
8637           (gnus-add-text-properties
8638            b (1- e) (list 'gnus-number gnus-reffed-article-number
8639                           gnus-mouse-face-prop gnus-mouse-face))
8640           (gnus-data-enter
8641            after-article gnus-reffed-article-number
8642            gnus-unread-mark b (car pslist) 0 (- e b))
8643           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8644           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8645           (setq pslist (cdr pslist)))))))
8646
8647 (defun gnus-pseudos< (p1 p2)
8648   (let ((c1 (cdr (assq 'action p1)))
8649         (c2 (cdr (assq 'action p2))))
8650     (and c1 c2 (string< c1 c2))))
8651
8652 (defun gnus-request-pseudo-article (props)
8653   (cond ((assq 'execute props)
8654          (gnus-execute-command (cdr (assq 'execute props)))))
8655   (let ((gnus-current-article (gnus-summary-article-number)))
8656     (gnus-run-hooks 'gnus-mark-article-hook)))
8657
8658 (defun gnus-execute-command (command &optional automatic)
8659   (save-excursion
8660     (gnus-article-setup-buffer)
8661     (set-buffer gnus-article-buffer)
8662     (setq buffer-read-only nil)
8663     (let ((command (if automatic command
8664                      (read-string "Command: " (cons command 0)))))
8665       (erase-buffer)
8666       (insert "$ " command "\n\n")
8667       (if gnus-view-pseudo-asynchronously
8668           (start-process "gnus-execute" (current-buffer) shell-file-name
8669                          shell-command-switch command)
8670         (call-process shell-file-name nil t nil
8671                       shell-command-switch command)))))
8672
8673 ;; Summary kill commands.
8674
8675 (defun gnus-summary-edit-global-kill (article)
8676   "Edit the \"global\" kill file."
8677   (interactive (list (gnus-summary-article-number)))
8678   (gnus-group-edit-global-kill article))
8679
8680 (defun gnus-summary-edit-local-kill ()
8681   "Edit a local kill file applied to the current newsgroup."
8682   (interactive)
8683   (setq gnus-current-headers (gnus-summary-article-header))
8684   (gnus-group-edit-local-kill
8685    (gnus-summary-article-number) gnus-newsgroup-name))
8686
8687 ;;; Header reading.
8688
8689 (defun gnus-read-header (id &optional header)
8690   "Read the headers of article ID and enter them into the Gnus system."
8691   (let ((group gnus-newsgroup-name)
8692         (gnus-override-method
8693          (and (gnus-news-group-p gnus-newsgroup-name)
8694               gnus-refer-article-method))
8695         where)
8696     ;; First we check to see whether the header in question is already
8697     ;; fetched.
8698     (if (stringp id)
8699         ;; This is a Message-ID.
8700         (setq header (or header (gnus-id-to-header id)))
8701       ;; This is an article number.
8702       (setq header (or header (gnus-summary-article-header id))))
8703     (if (and header
8704              (not (gnus-summary-article-sparse-p (mail-header-number header))))
8705         ;; We have found the header.
8706         header
8707       ;; If this is a sparse article, we have to nix out its
8708       ;; previous entry in the thread hashtb.
8709       (when (and header
8710                  (gnus-summary-article-sparse-p (mail-header-number header)))
8711         (let* ((parent (gnus-parent-id (mail-header-references header)))
8712                (thread
8713                 (and parent
8714                      (gnus-gethash parent gnus-newsgroup-dependencies))))
8715           (when thread
8716             (delq (assq header thread) thread))))
8717       ;; We have to really fetch the header to this article.
8718       (save-excursion
8719         (set-buffer nntp-server-buffer)
8720         (when (setq where (gnus-request-head id group))
8721           (nnheader-fold-continuation-lines)
8722           (goto-char (point-max))
8723           (insert ".\n")
8724           (goto-char (point-min))
8725           (insert "211 ")
8726           (princ (cond
8727                   ((numberp id) id)
8728                   ((cdr where) (cdr where))
8729                   (header (mail-header-number header))
8730                   (t gnus-reffed-article-number))
8731                  (current-buffer))
8732           (insert " Article retrieved.\n"))
8733         (if (or (not where)
8734                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
8735             ()                          ; Malformed head.
8736           (unless (gnus-summary-article-sparse-p (mail-header-number header))
8737             (when (and (stringp id)
8738                        (not (string= (gnus-group-real-name group)
8739                                      (car where))))
8740               ;; If we fetched by Message-ID and the article came
8741               ;; from a different group, we fudge some bogus article
8742               ;; numbers for this article.
8743               (mail-header-set-number header gnus-reffed-article-number))
8744             (save-excursion
8745               (set-buffer gnus-summary-buffer)
8746               (decf gnus-reffed-article-number)
8747               (gnus-remove-header (mail-header-number header))
8748               (push header gnus-newsgroup-headers)
8749               (setq gnus-current-headers header)
8750               (push (mail-header-number header) gnus-newsgroup-limit)))
8751           header)))))
8752
8753 (defun gnus-remove-header (number)
8754   "Remove header NUMBER from `gnus-newsgroup-headers'."
8755   (if (and gnus-newsgroup-headers
8756            (= number (mail-header-number (car gnus-newsgroup-headers))))
8757       (pop gnus-newsgroup-headers)
8758     (let ((headers gnus-newsgroup-headers))
8759       (while (and (cdr headers)
8760                   (not (= number (mail-header-number (cadr headers)))))
8761         (pop headers))
8762       (when (cdr headers)
8763         (setcdr headers (cddr headers))))))
8764
8765 ;;;
8766 ;;; summary highlights
8767 ;;;
8768
8769 (defun gnus-highlight-selected-summary ()
8770   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8771   ;; Highlight selected article in summary buffer
8772   (when gnus-summary-selected-face
8773     (save-excursion
8774       (let* ((beg (progn (beginning-of-line) (point)))
8775              (end (progn (end-of-line) (point)))
8776              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
8777              (from (if (get-text-property beg gnus-mouse-face-prop)
8778                        beg
8779                      (or (next-single-property-change
8780                           beg gnus-mouse-face-prop nil end)
8781                          beg)))
8782              (to
8783               (if (= from end)
8784                   (- from 2)
8785                 (or (next-single-property-change
8786                      from gnus-mouse-face-prop nil end)
8787                     end))))
8788         ;; If no mouse-face prop on line we will have to = from = end,
8789         ;; so we highlight the entire line instead.
8790         (when (= (+ to 2) from)
8791           (setq from beg)
8792           (setq to end))
8793         (if gnus-newsgroup-selected-overlay
8794             ;; Move old overlay.
8795             (gnus-move-overlay
8796              gnus-newsgroup-selected-overlay from to (current-buffer))
8797           ;; Create new overlay.
8798           (gnus-overlay-put
8799            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
8800            'face gnus-summary-selected-face))))))
8801
8802 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
8803 (defun gnus-summary-highlight-line ()
8804   "Highlight current line according to `gnus-summary-highlight'."
8805   (let* ((list gnus-summary-highlight)
8806          (p (point))
8807          (end (progn (end-of-line) (point)))
8808          ;; now find out where the line starts and leave point there.
8809          (beg (progn (beginning-of-line) (point)))
8810          (article (gnus-summary-article-number))
8811          (score (or (cdr (assq (or article gnus-current-article)
8812                                gnus-newsgroup-scored))
8813                     gnus-summary-default-score 0))
8814          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
8815          (inhibit-read-only t))
8816     ;; Eval the cars of the lists until we find a match.
8817     (let ((default gnus-summary-default-score))
8818       (while (and list
8819                   (not (eval (caar list))))
8820         (setq list (cdr list))))
8821     (let ((face (cdar list)))
8822       (unless (eq face (get-text-property beg 'face))
8823         (gnus-put-text-property
8824          beg end 'face
8825          (setq face (if (boundp face) (symbol-value face) face)))
8826         (when gnus-summary-highlight-line-function
8827           (funcall gnus-summary-highlight-line-function article face))))
8828     (goto-char p)))
8829
8830 (defun gnus-update-read-articles (group unread &optional compute)
8831   "Update the list of read articles in GROUP."
8832   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
8833          (entry (gnus-gethash group gnus-newsrc-hashtb))
8834          (info (nth 2 entry))
8835          (prev 1)
8836          (unread (sort (copy-sequence unread) '<))
8837          read)
8838     (if (or (not info) (not active))
8839         ;; There is no info on this group if it was, in fact,
8840         ;; killed.  Gnus stores no information on killed groups, so
8841         ;; there's nothing to be done.
8842         ;; One could store the information somewhere temporarily,
8843         ;; perhaps...  Hmmm...
8844         ()
8845       ;; Remove any negative articles numbers.
8846       (while (and unread (< (car unread) 0))
8847         (setq unread (cdr unread)))
8848       ;; Remove any expired article numbers
8849       (while (and unread (< (car unread) (car active)))
8850         (setq unread (cdr unread)))
8851       ;; Compute the ranges of read articles by looking at the list of
8852       ;; unread articles.
8853       (while unread
8854         (when (/= (car unread) prev)
8855           (push (if (= prev (1- (car unread))) prev
8856                   (cons prev (1- (car unread))))
8857                 read))
8858         (setq prev (1+ (car unread)))
8859         (setq unread (cdr unread)))
8860       (when (<= prev (cdr active))
8861         (push (cons prev (cdr active)) read))
8862       (if compute
8863           (if (> (length read) 1) (nreverse read) read)
8864         (save-excursion
8865           (set-buffer gnus-group-buffer)
8866           (gnus-undo-register
8867             `(progn
8868                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
8869                (gnus-info-set-read ',info ',(gnus-info-read info))
8870                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
8871                (gnus-group-update-group ,group t))))
8872         ;; Enter this list into the group info.
8873         (gnus-info-set-read
8874          info (if (> (length read) 1) (nreverse read) read))
8875         ;; Set the number of unread articles in gnus-newsrc-hashtb.
8876         (gnus-get-unread-articles-in-group info (gnus-active group))
8877         t))))
8878
8879 (defun gnus-offer-save-summaries ()
8880   "Offer to save all active summary buffers."
8881   (save-excursion
8882     (let ((buflist (buffer-list))
8883           buffers bufname)
8884       ;; Go through all buffers and find all summaries.
8885       (while buflist
8886         (and (setq bufname (buffer-name (car buflist)))
8887              (string-match "Summary" bufname)
8888              (save-excursion
8889                (set-buffer bufname)
8890                ;; We check that this is, indeed, a summary buffer.
8891                (and (eq major-mode 'gnus-summary-mode)
8892                     ;; Also make sure this isn't bogus.
8893                     gnus-newsgroup-prepared
8894                     ;; Also make sure that this isn't a dead summary buffer.
8895                     (not gnus-dead-summary-mode)))
8896              (push bufname buffers))
8897         (setq buflist (cdr buflist)))
8898       ;; Go through all these summary buffers and offer to save them.
8899       (when buffers
8900         (map-y-or-n-p
8901          "Update summary buffer %s? "
8902          (lambda (buf)
8903            (switch-to-buffer buf)
8904            (gnus-summary-exit))
8905          buffers)))))
8906
8907 (gnus-ems-redefine)
8908
8909 (provide 'gnus-sum)
8910
8911 (run-hooks 'gnus-sum-load-hook)
8912
8913 ;;; gnus-sum.el ends here