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