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